GeezerVison is showing github.com — Prepare for unsafe memory access deprecated for removal (#14090) · netty/netty@21de89f
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/buffer test/java/io/netty/buffer
main/java/io/netty/handler/codec/http/multipart test/java/io/netty/handler/codec/http/multipart
main/java/io/netty/handler/codec/compression test/java/io/netty/handler/codec
common/src/main/java/io/netty/util/internal microbench/src/main/java/io/netty/handler/codec/http
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -705,6 +705,7 @@ public boolean hasArray() { | 705 | 705 |
| 706 | 706 | @Override | 707 | 707 | public byte[] array() { | | 708 | + ensureAccessible(); | 708 | 709 | return rootParent.array(); | 709 | 710 | } | 710 | 711 |
| |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AlignedPooledByteBufAllocatorTest.java +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import static org.junit.jupiter.api.Assertions.assertNotNull; | 23 | 23 | import static org.junit.jupiter.api.Assertions.assertEquals; | 24 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue; | | 25 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; | 25 | 26 |
| 26 | 27 | public class AlignedPooledByteBufAllocatorTest extends PooledByteBufAllocatorTest { | 27 | 28 | @Override | 28 | 29 | protected PooledByteBufAllocator newAllocator(boolean preferDirect) { | | 30 | + assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported()); | 29 | 31 | int directMemoryCacheAlignment = 1; | 30 | 32 | return new PooledByteBufAllocator( | 31 | 33 | preferDirect, | @@ -42,6 +44,7 @@ protected PooledByteBufAllocator newAllocator(boolean preferDirect) { | 42 | 44 | // https://github.com/netty/netty/issues/11955 | 43 | 45 | @Test | 44 | 46 | public void testCorrectElementSize() { | | 47 | + assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported()); | 45 | 48 | ByteBufAllocator allocator = new PooledByteBufAllocator( | 46 | 49 | true, | 47 | 50 | PooledByteBufAllocator.defaultNumHeapArena(), | @@ -69,6 +72,7 @@ public void testCorrectElementSize() { | 69 | 72 |
| 70 | 73 | @Test | 71 | 74 | public void testDirectSubpageReleaseLock() { | | 75 | + assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported()); | 72 | 76 | int initialCapacity = 0; | 73 | 77 | int directMemoryCacheAlignment = 32; | 74 | 78 | PooledByteBufAllocator allocator = new PooledByteBufAllocator( | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -502,7 +502,10 @@ public void testHasArrayWhenEmptyAndIsDirect() { | 502 | 502 | assertTrue(buf.hasArray()); | 503 | 503 | assertArrayEquals(EMPTY_BUFFER.array(), buf.array()); | 504 | 504 | assertEquals(EMPTY_BUFFER.isDirect(), buf.isDirect()); | 505 | | - assertEquals(EMPTY_BUFFER.memoryAddress(), buf.memoryAddress()); | | 505 | + assertEquals(EMPTY_BUFFER.hasMemoryAddress(), buf.hasMemoryAddress()); | | 506 | + if (buf.hasMemoryAddress()) { | | 507 | + assertEquals(EMPTY_BUFFER.memoryAddress(), buf.memoryAddress()); | | 508 | + } | 506 | 509 | buf.release(); | 507 | 510 | } | 508 | 511 |
| |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/PooledAlignedBigEndianDirectByteBufTest.java +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import java.nio.ByteOrder; | 22 | 22 |
| 23 | 23 | import static org.junit.jupiter.api.Assertions.assertSame; | | 24 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; | 24 | 25 |
| 25 | 26 | public class PooledAlignedBigEndianDirectByteBufTest extends PooledBigEndianDirectByteBufTest { | 26 | 27 | private static final int directMemoryCacheAlignment = 1; | 27 | 28 | private static PooledByteBufAllocator allocator; | 28 | 29 |
| 29 | 30 | @BeforeAll | 30 | 31 | public static void setUpAllocator() { | | 32 | + assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported()); | 31 | 33 | allocator = new PooledByteBufAllocator( | 32 | 34 | true, | 33 | 35 | PooledByteBufAllocator.defaultNumHeapArena(), | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -277,7 +277,7 @@ public void testFreePoolChunk() { | 277 | 277 | public void testCollapse() { | 278 | 278 | int pageSize = 8192; | 279 | 279 | //no cache | 280 | | - ByteBufAllocator allocator = new PooledByteBufAllocator(true, 0, 1, 8192, 9, 0, 0, 0); | | 280 | + ByteBufAllocator allocator = new PooledByteBufAllocator(true, 1, 1, 8192, 9, 0, 0, 0); | 281 | 281 |
| 282 | 282 | ByteBuf b1 = allocator.buffer(pageSize * 4); | 283 | 283 | ByteBuf b2 = allocator.buffer(pageSize * 5); | @@ -310,7 +310,7 @@ public void testCollapse() { | 310 | 310 | @Test | 311 | 311 | public void testAllocateSmallOffset() { | 312 | 312 | int pageSize = 8192; | 313 | | - ByteBufAllocator allocator = new PooledByteBufAllocator(true, 0, 1, 8192, 9, 0, 0, 0); | | 313 | + ByteBufAllocator allocator = new PooledByteBufAllocator(true, 1, 1, 8192, 9, 0, 0, 0); | 314 | 314 |
| 315 | 315 | int size = pageSize * 5; | 316 | 316 |
| |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostStandardRequestDecoder.java +1-122Lines changed: 1 addition & 122 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -573,128 +573,7 @@ private void parseBodyAttributes() { | 573 | 573 | if (undecodedChunk == null) { | 574 | 574 | return; | 575 | 575 | } | 576 | | - if (!undecodedChunk.hasArray()) { | 577 | | - parseBodyAttributesStandard(); | 578 | | - return; | 579 | | - } | 580 | | - SeekAheadOptimize sao = new SeekAheadOptimize(undecodedChunk); | 581 | | - int firstpos = undecodedChunk.readerIndex(); | 582 | | - int currentpos = firstpos; | 583 | | - int equalpos; | 584 | | - int ampersandpos; | 585 | | - if (currentStatus == MultiPartStatus.NOTSTARTED) { | 586 | | - currentStatus = MultiPartStatus.DISPOSITION; | 587 | | - } | 588 | | - boolean contRead = true; | 589 | | - try { | 590 | | - loop: while (sao.pos < sao.limit) { | 591 | | - char read = (char) (sao.bytes[sao.pos++] & 0xFF); | 592 | | - currentpos++; | 593 | | - switch (currentStatus) { | 594 | | - case DISPOSITION:// search '=' | 595 | | - if (read == '=') { | 596 | | - currentStatus = MultiPartStatus.FIELD; | 597 | | - equalpos = currentpos - 1; | 598 | | - String key = decodeAttribute(undecodedChunk.toString(firstpos, equalpos - firstpos, charset), | 599 | | - charset); | 600 | | - currentAttribute = factory.createAttribute(request, key); | 601 | | - firstpos = currentpos; | 602 | | - } else if (read == '&' || | 603 | | - (isLastChunk && !undecodedChunk.isReadable() && hasFormBody())) { // special empty FIELD | 604 | | - currentStatus = MultiPartStatus.DISPOSITION; | 605 | | - ampersandpos = read == '&' ? currentpos - 1 : currentpos; | 606 | | - String key = decodeAttribute( | 607 | | - undecodedChunk.toString(firstpos, ampersandpos - firstpos, charset), charset); | 608 | | - // Some weird request bodies start with an '&' char, eg: &name=J&age=17. | 609 | | - // In that case, key would be "", will get exception: | 610 | | - // java.lang.IllegalArgumentException: Param 'name' must not be empty; | 611 | | - // Just check and skip empty key. | 612 | | - if (!key.isEmpty()) { | 613 | | - currentAttribute = factory.createAttribute(request, key); | 614 | | - currentAttribute.setValue(""); // empty | 615 | | - addHttpData(currentAttribute); | 616 | | - } | 617 | | - currentAttribute = null; | 618 | | - firstpos = currentpos; | 619 | | - contRead = true; | 620 | | - } | 621 | | - break; | 622 | | - case FIELD:// search '&' or end of line | 623 | | - if (read == '&') { | 624 | | - currentStatus = MultiPartStatus.DISPOSITION; | 625 | | - ampersandpos = currentpos - 1; | 626 | | - setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos)); | 627 | | - firstpos = currentpos; | 628 | | - contRead = true; | 629 | | - } else if (read == HttpConstants.CR) { | 630 | | - if (sao.pos < sao.limit) { | 631 | | - read = (char) (sao.bytes[sao.pos++] & 0xFF); | 632 | | - currentpos++; | 633 | | - if (read == HttpConstants.LF) { | 634 | | - currentStatus = MultiPartStatus.PREEPILOGUE; | 635 | | - ampersandpos = currentpos - 2; | 636 | | - sao.setReadPosition(0); | 637 | | - setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos)); | 638 | | - firstpos = currentpos; | 639 | | - contRead = false; | 640 | | - break loop; | 641 | | - } else { | 642 | | - // Error | 643 | | - sao.setReadPosition(0); | 644 | | - throw new ErrorDataDecoderException("Bad end of line"); | 645 | | - } | 646 | | - } else { | 647 | | - if (sao.limit > 0) { | 648 | | - currentpos--; | 649 | | - } | 650 | | - } | 651 | | - } else if (read == HttpConstants.LF) { | 652 | | - currentStatus = MultiPartStatus.PREEPILOGUE; | 653 | | - ampersandpos = currentpos - 1; | 654 | | - sao.setReadPosition(0); | 655 | | - setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos)); | 656 | | - firstpos = currentpos; | 657 | | - contRead = false; | 658 | | - break loop; | 659 | | - } | 660 | | - break; | 661 | | - default: | 662 | | - // just stop | 663 | | - sao.setReadPosition(0); | 664 | | - contRead = false; | 665 | | - break loop; | 666 | | - } | 667 | | - } | 668 | | - if (isLastChunk && currentAttribute != null) { | 669 | | - // special case | 670 | | - ampersandpos = currentpos; | 671 | | - if (ampersandpos > firstpos) { | 672 | | - setFinalBuffer(undecodedChunk.retainedSlice(firstpos, ampersandpos - firstpos)); | 673 | | - } else if (!currentAttribute.isCompleted()) { | 674 | | - setFinalBuffer(Unpooled.EMPTY_BUFFER); | 675 | | - } | 676 | | - firstpos = currentpos; | 677 | | - currentStatus = MultiPartStatus.EPILOGUE; | 678 | | - } else if (contRead && currentAttribute != null && currentStatus == MultiPartStatus.FIELD) { | 679 | | - // reset index except if to continue in case of FIELD getStatus | 680 | | - currentAttribute.addContent(undecodedChunk.retainedSlice(firstpos, currentpos - firstpos), | 681 | | - false); | 682 | | - firstpos = currentpos; | 683 | | - } | 684 | | - undecodedChunk.readerIndex(firstpos); | 685 | | - } catch (ErrorDataDecoderException e) { | 686 | | - // error while decoding | 687 | | - undecodedChunk.readerIndex(firstpos); | 688 | | - throw e; | 689 | | - } catch (IOException e) { | 690 | | - // error while decoding | 691 | | - undecodedChunk.readerIndex(firstpos); | 692 | | - throw new ErrorDataDecoderException(e); | 693 | | - } catch (IllegalArgumentException e) { | 694 | | - // error while decoding | 695 | | - undecodedChunk.readerIndex(firstpos); | 696 | | - throw new ErrorDataDecoderException(e); | 697 | | - } | | 576 | + parseBodyAttributesStandard(); | 698 | 577 | } | 699 | 578 |
| 700 | 579 | private void setFinalBuffer(ByteBuf buffer) throws IOException { | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java -7Lines changed: 0 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -757,10 +757,8 @@ public void testMultipartRequest() throws Exception { | 757 | 757 | assertEquals(2, decoder.getBodyHttpDatas().size()); | 758 | 758 |
| 759 | 759 | Attribute attrMsg = (Attribute) decoder.getBodyHttpData("msg"); | 760 | | - assertTrue(attrMsg.getByteBuf().isDirect()); | 761 | 760 | assertEquals("test message", attrMsg.getValue()); | 762 | 761 | Attribute attrMsgId = (Attribute) decoder.getBodyHttpData("msg_id"); | 763 | | - assertTrue(attrMsgId.getByteBuf().isDirect()); | 764 | 762 | assertEquals("15200", attrMsgId.getValue()); | 765 | 763 |
| 766 | 764 | decoder.destroy(); | @@ -876,23 +874,18 @@ public void testDecodeFullHttpRequestWithUrlEncodedBody() throws Exception { | 876 | 874 | assertEquals(5, decoder.getBodyHttpDatas().size()); | 877 | 875 |
| 878 | 876 | Attribute attr = (Attribute) decoder.getBodyHttpData("foo"); | 879 | | - assertTrue(attr.getByteBuf().isDirect()); | 880 | 877 | assertEquals("bar", attr.getValue()); | 881 | 878 |
| 882 | 879 | attr = (Attribute) decoder.getBodyHttpData("a"); | 883 | | - assertTrue(attr.getByteBuf().isDirect()); | 884 | 880 | assertEquals("b", attr.getValue()); | 885 | 881 |
| 886 | 882 | attr = (Attribute) decoder.getBodyHttpData("empty"); | 887 | | - assertTrue(attr.getByteBuf().isDirect()); | 888 | 883 | assertEquals("", attr.getValue()); | 889 | 884 |
| 890 | 885 | attr = (Attribute) decoder.getBodyHttpData("city"); | 891 | | - assertTrue(attr.getByteBuf().isDirect()); | 892 | 886 | assertEquals("<\"new\" york city>", attr.getValue()); | 893 | 887 |
| 894 | 888 | attr = (Attribute) decoder.getBodyHttpData("other_city"); | 895 | | - assertTrue(attr.getByteBuf().isDirect()); | 896 | 889 | assertEquals("los angeles", attr.getValue()); | 897 | 890 |
| 898 | 891 | decoder.destroy(); | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/LzfEncoder.java +23-2Lines changed: 23 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | import io.netty.buffer.ByteBuf; | 24 | 24 | import io.netty.channel.ChannelHandlerContext; | 25 | 25 | import io.netty.handler.codec.MessageToByteEncoder; | | 26 | +import io.netty.util.internal.PlatformDependent; | 26 | 27 |
| 27 | 28 | import static com.ning.compress.lzf.LZFChunk.MAX_CHUNK_LEN; | 28 | 29 |
| @@ -39,6 +40,7 @@ public class LzfEncoder extends MessageToByteEncoder<ByteBuf> { | 39 | 40 | * less than {@link #MIN_BLOCK_TO_COMPRESS} will write as uncompressed. | 40 | 41 | */ | 41 | 42 | private static final int MIN_BLOCK_TO_COMPRESS = 16; | | 43 | + private static final boolean DEFAULT_SAFE = !PlatformDependent.hasUnsafe(); | 42 | 44 |
| 43 | 45 | /** | 44 | 46 | * Compress threshold for LZF format. When the amount of input data is less than compressThreshold, | @@ -66,7 +68,7 @@ public class LzfEncoder extends MessageToByteEncoder<ByteBuf> { | 66 | 68 | * non-standard platforms it may be necessary to use {@link #LzfEncoder(boolean)} with {@code true} param. | 67 | 69 | */ | 68 | 70 | public LzfEncoder() { | 69 | | - this(false); | | 71 | + this(DEFAULT_SAFE); | 70 | 72 | } | 71 | 73 |
| 72 | 74 | /** | @@ -77,7 +79,9 @@ public LzfEncoder() { | 77 | 79 | * Otherwise encoder will try to use highly optimized {@link ChunkEncoder} | 78 | 80 | * implementation that uses Sun JDK's {@link sun.misc.Unsafe} | 79 | 81 | * class (which may be included by other JDK's as well). | | 82 | + * @deprecated Use the constructor without the {@code safeInstance} parameter. | 80 | 83 | */ | | 84 | + @Deprecated | 81 | 85 | public LzfEncoder(boolean safeInstance) { | 82 | 86 | this(safeInstance, MAX_CHUNK_LEN); | 83 | 87 | } | @@ -92,7 +96,9 @@ public LzfEncoder(boolean safeInstance) { | 92 | 96 | * class (which may be included by other JDK's as well). | 93 | 97 | * @param totalLength Expected total length of content to compress; only matters for outgoing messages | 94 | 98 | * that is smaller than maximum chunk size (64k), to optimize encoding hash tables. | | 99 | + * @deprecated Use the constructor without the {@code safeInstance} parameter. | 95 | 100 | */ | | 101 | + @Deprecated | 96 | 102 | public LzfEncoder(boolean safeInstance, int totalLength) { | 97 | 103 | this(safeInstance, totalLength, MIN_BLOCK_TO_COMPRESS); | 98 | 104 | } | @@ -106,7 +112,20 @@ public LzfEncoder(boolean safeInstance, int totalLength) { | 106 | 112 | * to optimize encoding hash tables. | 107 | 113 | */ | 108 | 114 | public LzfEncoder(int totalLength) { | 109 | | - this(false, totalLength); | | 115 | + this(DEFAULT_SAFE, totalLength); | | 116 | + } | | 117 | + | | 118 | + /** | | 119 | + * Creates a new LZF encoder with specified settings. | | 120 | + * | | 121 | + * @param totalLength Expected total length of content to compress; only matters for outgoing messages | | 122 | + * that is smaller than maximum chunk size (64k), to optimize encoding hash tables. | | 123 | + * @param compressThreshold Compress threshold for LZF format. When the amount of input data is less than | | 124 | + * compressThreshold, we will construct an uncompressed output according | | 125 | + * to the LZF format. | | 126 | + */ | | 127 | + public LzfEncoder(int totalLength, int compressThreshold) { | | 128 | + this(DEFAULT_SAFE, totalLength, compressThreshold); | 110 | 129 | } | 111 | 130 |
| 112 | 131 | /** | @@ -122,7 +141,9 @@ public LzfEncoder(int totalLength) { | 122 | 141 | * @param compressThreshold Compress threshold for LZF format. When the amount of input data is less than | 123 | 142 | * compressThreshold, we will construct an uncompressed output according | 124 | 143 | * to the LZF format. | | 144 | + * @deprecated Use the constructor without the {@code safeInstance} parameter. | 125 | 145 | */ | | 146 | + @Deprecated | 126 | 147 | public LzfEncoder(boolean safeInstance, int totalLength, int compressThreshold) { | 127 | 148 | super(false); | 128 | 149 | if (totalLength < MIN_BLOCK_TO_COMPRESS || totalLength > MAX_CHUNK_LEN) { | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/LengthAwareLzfIntegrationTest.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,6 @@ public class LengthAwareLzfIntegrationTest extends LzfIntegrationTest { | 23 | 23 |
| 24 | 24 | @Override | 25 | 25 | protected EmbeddedChannel createEncoder() { | 26 | | - return new EmbeddedChannel(new LzfEncoder(false, MAX_CHUNK_LEN, 2 * 1024 * 1024)); | | 26 | + return new EmbeddedChannel(new LzfEncoder(MAX_CHUNK_LEN, 2 * 1024 * 1024)); | 27 | 27 | } | 28 | 28 | } |
Collapse file Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/LzfDecoderTest.java +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import io.netty.buffer.ByteBuf; | 20 | 20 | import io.netty.buffer.Unpooled; | 21 | 21 | import io.netty.channel.embedded.EmbeddedChannel; | | 22 | +import io.netty.util.internal.PlatformDependent; | 22 | 23 | import org.junit.jupiter.api.Test; | 23 | 24 | import org.junit.jupiter.api.function.Executable; | 24 | 25 |
| @@ -68,6 +69,6 @@ public void execute() { | 68 | 69 |
| 69 | 70 | @Override | 70 | 71 | protected byte[] compress(byte[] data) throws Exception { | 71 | | - return LZFEncoder.encode(data); | | 72 | + return PlatformDependent.hasUnsafe() ? LZFEncoder.encode(data) : LZFEncoder.safeEncode(data); | 72 | 73 | } | 73 | 74 | } |
|