GeezerVison is showing github.com — Merge branch '4.2' into ocsp_test · netty/netty@31458a9
File treeExpand file treeCollapse file tree Open diff view settings buffer/src/main/java/io/netty/buffer main/java/io/netty/handler/codec test/java/io/netty/handler/codec
main/java/io/netty/handler/codec/http3 test/java/io/netty/handler/codec/http3
common/src/main/java/io/netty/util/internal main/java/io/netty/handler/ssl test/java/io/netty/handler/ssl
jfr-stub/src/main/java/jdk/jfr transport-blockhound-tests/src/test/java/io/netty/buffer
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/AbstractAllocatorEvent.java +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import jdk.jfr.Enabled; | 21 | 21 | import jdk.jfr.Event; | 22 | 22 | import jdk.jfr.Label; | | 23 | +import jdk.jfr.Registered; | 23 | 24 |
| 24 | 25 | /** | 25 | 26 | * An abstract memory allocator event. | | 27 | + * <p> | | 28 | + * The events are registered by {@link JfrEventRegistration} once a Flight Recorder exists. | 26 | 29 | */ | 27 | 30 | @Enabled(false) | | 31 | +@Registered(false) | 28 | 32 | @Category("Netty") | 29 | 33 | @SuppressWarnings("Since15") | 30 | 34 | abstract class AbstractAllocatorEvent extends Event { | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -221,6 +221,9 @@ final class AdaptivePoolingAllocator { | 221 | 221 | private final FastThreadLocal<MagazineGroup[]> threadLocalGroup; | 222 | 222 |
| 223 | 223 | AdaptivePoolingAllocator(ChunkAllocator chunkAllocator, boolean useCacheForNonEventLoopThreads) { | | 224 | + if (PlatformDependent.isJfrEnabled()) { | | 225 | + JfrEventRegistration.init(); | | 226 | + } | 224 | 227 | this.chunkAllocator = ObjectUtil.checkNotNull(chunkAllocator, "chunkAllocator"); | 225 | 228 | chunkRegistry = new ChunkRegistry(); | 226 | 229 | sizeClassedMagazineGroups = createMagazineGroupSizeClasses(this, false); | |
Collapse file Copy file name to clipboard +98Lines changed: 98 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2026 The Netty Project | | 3 | + * | | 4 | + * The Netty Project licenses this file to you under the Apache License, | | 5 | + * version 2.0 (the "License"); you may not use this file except in compliance | | 6 | + * with the License. You may obtain a copy of the License at: | | 7 | + * | | 8 | + * https://www.apache.org/licenses/LICENSE-2.0 | | 9 | + * | | 10 | + * Unless required by applicable law or agreed to in writing, software | | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | | 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | | 13 | + * License for the specific language governing permissions and limitations | | 14 | + * under the License. | | 15 | + */ | | 16 | +package io.netty.buffer; | | 17 | + | | 18 | +import io.netty.util.internal.PlatformDependent; | | 19 | +import io.netty.util.internal.logging.InternalLogger; | | 20 | +import io.netty.util.internal.logging.InternalLoggerFactory; | | 21 | +import jdk.jfr.Event; | | 22 | +import jdk.jfr.FlightRecorder; | | 23 | +import jdk.jfr.FlightRecorderListener; | | 24 | + | | 25 | +/** | | 26 | + * Registers the allocator events with JFR once a Flight Recorder has been initialized. | | 27 | + * <p> | | 28 | + * The events are {@link jdk.jfr.Registered @Registered(false)}, because registering the first event class sets up | | 29 | + * JFR's metadata repository, which is expensive and pointless in processes that never record. Until an event class | | 30 | + * is registered, JFR does not instrument it, so {@link Event#isEnabled()} is a constant {@code false} and the JIT | | 31 | + * removes the allocation path guards entirely. Registering it later retransforms the class, which deoptimizes the | | 32 | + * guards, exactly like starting a recording in a running process. | | 33 | + * <p> | | 34 | + * JFR notifies listeners while it holds the {@code PlatformRecorder} class lock. It registers its own JDK events | | 35 | + * under the same lock, and the event classes are loaded by this class's initializer and have trivial initializers, | | 36 | + * so registering from the callback does not wait for anything that may in turn wait for that lock. | | 37 | + * <p> | | 38 | + * A Flight Recorder is initialized at most once per JVM, so the listener removes itself after registering the events. | | 39 | + * Otherwise JFR's static listener list would keep this class, and with it the class loader that loaded Netty, | | 40 | + * reachable for the lifetime of the JVM. JFR iterates over a copy of the list, so this is safe from the callback. | | 41 | + * <p> | | 42 | + * If no Flight Recorder is ever initialized, the listener stays in that list, so the class loader that loaded Netty | | 43 | + * can't be unloaded, for example on a webapp redeploy. Holding the listener weakly doesn't help: before JDK 25, | | 44 | + * {@code addListener} stores the caller's {@code AccessControlContext} with it, which references Netty's protection | | 45 | + * domain and so its class loader. Set {@code -Dio.netty.jfr.enabled=false} to avoid this. | | 46 | + * <p> | | 47 | + * This must not be initialized from an event class initializer: registration initializes the event classes. | | 48 | + */ | | 49 | +@SuppressWarnings("Since15") | | 50 | +final class JfrEventRegistration implements FlightRecorderListener { | | 51 | + private static final InternalLogger logger = InternalLoggerFactory.getInstance(JfrEventRegistration.class); | | 52 | + | | 53 | + private static final Class<?>[] EVENT_CLASSES = { | | 54 | + AllocateChunkEvent.class, | | 55 | + FreeChunkEvent.class, | | 56 | + AllocateBufferEvent.class, | | 57 | + FreeBufferEvent.class, | | 58 | + ReallocateBufferEvent.class, | | 59 | + }; | | 60 | + | | 61 | + static { | | 62 | + try { | | 63 | + // Notifies the listener immediately if a recorder already exists, e.g. with -XX:StartFlightRecording. | | 64 | + FlightRecorder.addListener(new JfrEventRegistration()); | | 65 | + } catch (Throwable t) { | | 66 | + logger.debug("Failed to register the JFR recorder listener, allocator events are disabled", t); | | 67 | + } | | 68 | + } | | 69 | + | | 70 | + private JfrEventRegistration() { | | 71 | + } | | 72 | + | | 73 | + /** | | 74 | + * Make sure the allocator events are registered once a Flight Recorder exists. Allocators call this, guarded by | | 75 | + * {@link PlatformDependent#isJfrEnabled()}, before they can emit events. The class initializer does the work. | | 76 | + */ | | 77 | + static void init() { | | 78 | + } | | 79 | + | | 80 | + @SuppressWarnings("unchecked") | | 81 | + @Override | | 82 | + public void recorderInitialized(FlightRecorder recorder) { | | 83 | + try { | | 84 | + for (Class<?> eventClass : EVENT_CLASSES) { | | 85 | + FlightRecorder.register((Class<? extends Event>) eventClass); | | 86 | + } | | 87 | + } catch (Throwable t) { | | 88 | + // Don't break the recording that is being started. | | 89 | + logger.debug("Failed to register the allocator JFR events", t); | | 90 | + } finally { | | 91 | + try { | | 92 | + FlightRecorder.removeListener(this); | | 93 | + } catch (Throwable t) { | | 94 | + logger.debug("Failed to remove the JFR recorder listener", t); | | 95 | + } | | 96 | + } | | 97 | + } | | 98 | +} |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -272,6 +272,9 @@ public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectA | 272 | 272 | int smallCacheSize, int normalCacheSize, | 273 | 273 | boolean useCacheForAllThreads, int directMemoryCacheAlignment) { | 274 | 274 | super(preferDirect); | | 275 | + if (PlatformDependent.isJfrEnabled()) { | | 276 | + JfrEventRegistration.init(); | | 277 | + } | 275 | 278 | threadCache = new PoolThreadLocalCache(useCacheForAllThreads); | 276 | 279 | this.smallCacheSize = smallCacheSize; | 277 | 280 | this.normalCacheSize = normalCacheSize; | |
Collapse file Copy file name to clipboardExpand all lines: codec-base/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java +25-5Lines changed: 25 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -314,11 +314,14 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Except | 314 | 314 |
| 315 | 315 | return frame; | 316 | 316 | } else { | | 317 | + // The buffer may end with the first bytes of a delimiter whose remaining bytes were not received yet. | | 318 | + // They are neither counted as part of the frame nor discarded. | | 319 | + int frameBytes = readableBytes - partialDelimiterLength(buffer); | 317 | 320 | if (!discardingTooLongFrame) { | 318 | | - if (readableBytes > maxFrameLength) { | | 321 | + if (frameBytes > maxFrameLength) { | 319 | 322 | // Discard the content of the buffer until a delimiter is found. | 320 | | - tooLongFrameLength = readableBytes; | 321 | | - buffer.skipBytes(readableBytes); | | 323 | + tooLongFrameLength = frameBytes; | | 324 | + buffer.skipBytes(frameBytes); | 322 | 325 | discardingTooLongFrame = true; | 323 | 326 | scanOffset = 0; | 324 | 327 | if (failFast) { | @@ -331,14 +334,31 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Except | 331 | 334 | } | 332 | 335 | } else { | 333 | 336 | // Still discarding the buffer since a delimiter is not found. | 334 | | - tooLongFrameLength += buffer.readableBytes(); | 335 | | - buffer.skipBytes(buffer.readableBytes()); | | 337 | + tooLongFrameLength += frameBytes; | | 338 | + buffer.skipBytes(frameBytes); | 336 | 339 | scanOffset = 0; | 337 | 340 | } | 338 | 341 | return null; | 339 | 342 | } | 340 | 343 | } | 341 | 344 |
| | 345 | + /** | | 346 | + * Returns the length of the longest suffix of the readable bytes that is the beginning of a delimiter. | | 347 | + */ | | 348 | + private int partialDelimiterLength(ByteBuf buffer) { | | 349 | + int readableBytes = buffer.readableBytes(); | | 350 | + int partialLength = 0; | | 351 | + for (ByteBuf delim: delimiters) { | | 352 | + for (int length = Math.min(delim.capacity() - 1, readableBytes); length > partialLength; length--) { | | 353 | + if (ByteBufUtil.equals(buffer, buffer.writerIndex() - length, delim, 0, length)) { | | 354 | + partialLength = length; | | 355 | + break; | | 356 | + } | | 357 | + } | | 358 | + } | | 359 | + return partialLength; | | 360 | + } | | 361 | + | 342 | 362 | private void fail(long frameLength) { | 343 | 363 | if (frameLength > 0) { | 344 | 364 | throw new TooLongFrameException( | |
Collapse file Copy file name to clipboardExpand all lines: codec-base/src/test/java/io/netty/handler/codec/DelimiterBasedFrameDecoderTest.java +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | import static org.junit.jupiter.api.Assertions.assertEquals; | 31 | 31 | import static org.junit.jupiter.api.Assertions.assertFalse; | 32 | 32 | import static org.junit.jupiter.api.Assertions.assertNull; | | 33 | +import static org.junit.jupiter.api.Assertions.assertThrows; | 33 | 34 | import static org.junit.jupiter.api.Assertions.assertTrue; | 34 | 35 |
| 35 | 36 | public class DelimiterBasedFrameDecoderTest { | @@ -131,6 +132,65 @@ public void testDecode() throws Exception { | 131 | 132 | buf2.release(); | 132 | 133 | } | 133 | 134 |
| | 135 | + @Test | | 136 | + public void testMaxLengthFrameWithDelimiterSplitAcrossReads() { | | 137 | + EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(4, ascii("##"))); | | 138 | + // The frame has maxFrameLength bytes, the first delimiter byte is in the same read. | | 139 | + ch.writeInbound(ascii("ABCD#")); | | 140 | + ch.writeInbound(ascii("#ok##")); | | 141 | + assertFrame(ch, "ABCD"); | | 142 | + assertFrame(ch, "ok"); | | 143 | + assertNull(ch.readInbound()); | | 144 | + assertFalse(ch.finish()); | | 145 | + } | | 146 | + | | 147 | + @Test | | 148 | + public void testDelimiterSplitAcrossReadsWhileDiscarding() { | | 149 | + final EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(4, ascii("##"))); | | 150 | + // failFast: the exception is raised as soon as the frame is known to be too long. | | 151 | + assertThrows(TooLongFrameException.class, () -> ch.writeInbound(ascii("AAAAAAAAAA#"))); | | 152 | + // The rest of the delimiter ends the discarded frame, the next frame must not be lost. | | 153 | + ch.writeInbound(ascii("#ok##")); | | 154 | + assertFrame(ch, "ok"); | | 155 | + assertNull(ch.readInbound()); | | 156 | + assertFalse(ch.finish()); | | 157 | + } | | 158 | + | | 159 | + @Test | | 160 | + public void testDelimiterSplitAcrossReadsWhileDiscardingNoFailFast() { | | 161 | + final EmbeddedChannel ch = new EmbeddedChannel( | | 162 | + new DelimiterBasedFrameDecoder(4, true, false, ascii("##"))); | | 163 | + ch.writeInbound(ascii("AAAAAAAAAA#")); | | 164 | + // The exception is raised once the delimiter that ends the too long frame was read. | | 165 | + assertThrows(TooLongFrameException.class, () -> ch.writeInbound(ascii("#ok##"))); | | 166 | + ch.writeInbound(ascii("next##")); | | 167 | + assertFrame(ch, "ok"); | | 168 | + assertFrame(ch, "next"); | | 169 | + assertNull(ch.readInbound()); | | 170 | + assertFalse(ch.finish()); | | 171 | + } | | 172 | + | | 173 | + @Test | | 174 | + public void testMaxFrameLengthEnforcedWithDelimiterSplitAcrossReads() { | | 175 | + final EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(4, ascii("##"))); | | 176 | + // One byte more than maxFrameLength. | | 177 | + assertThrows(TooLongFrameException.class, () -> ch.writeInbound(ascii("ABCDE#"))); | | 178 | + ch.writeInbound(ascii("#ok##")); | | 179 | + assertFrame(ch, "ok"); | | 180 | + assertNull(ch.readInbound()); | | 181 | + assertFalse(ch.finish()); | | 182 | + } | | 183 | + | | 184 | + private static ByteBuf ascii(String s) { | | 185 | + return Unpooled.copiedBuffer(s, CharsetUtil.US_ASCII); | | 186 | + } | | 187 | + | | 188 | + private static void assertFrame(EmbeddedChannel ch, String expected) { | | 189 | + ByteBuf frame = ch.readInbound(); | | 190 | + assertEquals(expected, frame.toString(CharsetUtil.US_ASCII)); | | 191 | + frame.release(); | | 192 | + } | | 193 | + | 134 | 194 | /** | 135 | 195 | * A multi-byte, non-line delimiter that straddles the resumed-scan boundary must still be | 136 | 196 | * found. This guards the scan-offset optimization added to fix the quadratic rescanning of | |
Collapse file Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/QpackDecoder.java +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -150,6 +150,12 @@ public boolean decode(QpackAttributes qpackAttributes, long streamId, ByteBuf in | 150 | 150 | assert qpackAttributes.decoderStreamAvailable(); | 151 | 151 |
| 152 | 152 | stateSyncStrategy.sectionAcknowledged(requiredInsertCount); | | 153 | + // A Section Acknowledgment also acknowledges all insertions up to the Required Insert Count, so the next | | 154 | + // Insert Count Increment must not include them again. | | 155 | + // See https://www.rfc-editor.org/rfc/rfc9204.html#section-4.4.1 | | 156 | + if (requiredInsertCount > lastAckInsertCount) { | | 157 | + lastAckInsertCount = requiredInsertCount; | | 158 | + } | 153 | 159 | final ByteBuf sectionAck = qpackAttributes.decoderStream().alloc().buffer(8); | 154 | 160 | encodePrefixedInteger(sectionAck, (byte) 0b1000_0000, 7, streamId); | 155 | 161 | closeOnFailure(qpackAttributes.decoderStream().writeAndFlush(sectionAck)); | @@ -535,7 +541,9 @@ private void sendInsertCountIncrementIfRequired(QuicStreamChannel qpackDecoderSt | 535 | 541 | throw BLOCKED_STREAM_RESUMPTION_FAILED; | 536 | 542 | } | 537 | 543 | } | 538 | | - if (stateSyncStrategy.entryAdded(insertCount)) { | | 544 | + if (stateSyncStrategy.entryAdded(insertCount) && | | 545 | + // The insertions might have been acknowledged by a Section Acknowledgment already. | | 546 | + insertCount > lastAckInsertCount) { | 539 | 547 | // https://www.rfc-editor.org/rfc/rfc9204.html#name-insert-count-increment | 540 | 548 | // 0 1 2 3 4 5 6 7 | 541 | 549 | // +---+---+---+---+---+---+---+---+ | |
|