-
Notifications
You must be signed in to change notification settings - Fork 1
Read 64-bit varints through unchecked Netty accessors #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
873108e
WIP: unchecked Netty reads via io.netty.buffer helper — parked, do no…
merlimat 05ec568
Drop the per-varint readable gates; safety moves to message level
merlimat 930746a
Restrict unchecked reads to 64-bit varints
merlimat f1d5c5f
Inline short varint64 reads; emit the truncation check only where needed
merlimat 63411ab
Trim the buffer-access helper to the varint64 readers actually used
merlimat ea823cf
Address review: map truncation coverage, doc bounds, fail-fast templa…
merlimat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...ator/src/main/resources/io/streamnative/lightproto/generator/LightProtoByteBufAccess.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * Copyright 2026 StreamNative | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.netty.buffer; | ||
|
|
||
| /** | ||
| * Placed in the {@code io.netty.buffer} package to reach {@link AbstractByteBuf}'s | ||
| * protected unchecked accessors ({@code _getByte}) and its {@code readerIndex} | ||
| * field directly. The per-byte {@code checkReadableBytes0()} / | ||
| * {@code ensureAccessible()} checks that dominate parse profiles are skipped, and | ||
| * each varint performs a single {@code readerIndex} store instead of one per byte. | ||
| * | ||
| * <p>Reads are not bounds-checked here: on truncated input a read may advance up | ||
| * to 9 bytes past the intended message limit (a 10-byte read whose first byte is | ||
| * inside the limit). The generated {@code parseFrom()} detects that afterwards | ||
| * and throws, preserving the throw-on-truncated contract. | ||
| * | ||
| * <p>NOTE: this class creates a split package with netty-buffer under the JPMS | ||
| * module path; it is intended for classpath deployments. | ||
| */ | ||
| public final class LightProtoByteBufAccessTemplate { | ||
|
|
||
| private LightProtoByteBufAccessTemplate() { | ||
| } | ||
|
|
||
| /** | ||
| * Read a varint of up to 10 bytes as a long. | ||
| * | ||
| * <p>The 1-2 byte cases are decoded here so the method stays far below C2's | ||
| * FreqInlineSize: the full nested chain compiles to ~371 bytecodes and is | ||
| * never inlined, so routing short varints through it costs a call per read — | ||
| * a net loss on small values. Longer varints fall into the chain, whose | ||
| * multi-byte decode amortizes the call. | ||
| */ | ||
| public static long readVarInt64Unchecked(AbstractByteBuf buf) { | ||
| int i = buf.readerIndex; | ||
| byte b0 = buf._getByte(i); | ||
| if (b0 >= 0) { | ||
| buf.readerIndex = i + 1; | ||
| return b0; | ||
| } | ||
| byte b1 = buf._getByte(i + 1); | ||
| if (b1 >= 0) { | ||
| buf.readerIndex = i + 2; | ||
| return (b0 & 0x7f) | ((long) b1 << 7); | ||
| } | ||
| return readVarInt64UncheckedChain(buf); | ||
| } | ||
|
|
||
| private static long readVarInt64UncheckedChain(AbstractByteBuf buf) { | ||
| int i = buf.readerIndex; | ||
| long result; | ||
| byte tmp = buf._getByte(i++); | ||
| if (tmp >= 0) { | ||
| buf.readerIndex = i; | ||
| return tmp; | ||
| } | ||
| result = tmp & 0x7fL; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 7; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 7; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 14; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 14; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 21; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 21; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 28; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 28; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 35; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 35; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 42; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 42; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 49; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 49; | ||
| if ((tmp = buf._getByte(i++)) >= 0) { | ||
| result |= (long) tmp << 56; | ||
| } else { | ||
| result |= (tmp & 0x7fL) << 56; | ||
| result |= ((long) buf._getByte(i++)) << 63; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| buf.readerIndex = i; | ||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tests/src/test/java/io/streamnative/lightproto/tests/TruncatedInputTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /** | ||
| * Copyright 2026 StreamNative | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.streamnative.lightproto.tests; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import io.netty.buffer.ByteBuf; | ||
| import io.netty.buffer.PooledByteBufAllocator; | ||
| import io.netty.buffer.Unpooled; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Truncated input must throw IndexOutOfBoundsException: heap buffers fail | ||
| * safely on array bounds, and for pooled direct buffers the unchecked readers | ||
| * overrun by at most 9 bytes inside the arena chunk before the post-parse | ||
| * limit check detects it. (Exactly-sized unpooled unsafe direct buffers are | ||
| * outside this guarantee — see the readVarInt64() notes in LightProtoCodec.) | ||
| */ | ||
| public class TruncatedInputTest { | ||
|
|
||
| // int_field tag followed by a dangling continuation byte | ||
| private static final byte[] TRUNCATED_VARINT = {0x08, (byte) 0x80}; | ||
| // long_field (tag 0x10) truncated mid-varint64 | ||
| private static final byte[] TRUNCATED_VARINT64 = {0x10, (byte) 0xFF, (byte) 0xFF}; | ||
| // double_field (tag 0x21) with only 3 of 8 bytes | ||
| private static final byte[] TRUNCATED_FIXED64 = {0x21, 0x00, 0x00, 0x00}; | ||
|
|
||
| private static void assertThrowsOnAllBufferTypes(byte[] data) { | ||
| // Heap, exact size | ||
| assertThrows(IndexOutOfBoundsException.class, | ||
| () -> new Proto3Message().parseFrom(data)); | ||
|
|
||
| // Pooled direct (capacity slack: overrun reads stay in the chunk, the | ||
| // post-parse limit check throws) | ||
| ByteBuf pooled = PooledByteBufAllocator.DEFAULT.directBuffer(data.length); | ||
| try { | ||
| pooled.writeBytes(data); | ||
| assertThrows(IndexOutOfBoundsException.class, | ||
| () -> new Proto3Message().parseFrom(pooled, pooled.readableBytes())); | ||
| } finally { | ||
| pooled.release(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testTruncatedVarint() { | ||
| assertThrowsOnAllBufferTypes(TRUNCATED_VARINT); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTruncatedVarint64() { | ||
| assertThrowsOnAllBufferTypes(TRUNCATED_VARINT64); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTruncatedFixed64() { | ||
| assertThrowsOnAllBufferTypes(TRUNCATED_FIXED64); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTruncatedLengthDelimited() { | ||
| // string_field (tag 0x32) claims 5 bytes but only 2 are present: | ||
| // the checked skipBytes() catches this | ||
| byte[] data = {0x32, 0x05, 'a', 'b'}; | ||
| assertThrowsOnAllBufferTypes(data); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTruncatedVarint64InMapValue() { | ||
| // MapInt64Message has no top-level 64-bit field: only the map value type | ||
| // routes through readVarInt64(), so this exercises the check emission | ||
| // driven by map key/value types. counters entry: key "k", value varint64 | ||
| // truncated at the end of the buffer. | ||
| byte[] data = {0x0A, 0x06, 0x0A, 0x01, 'k', 0x10, (byte) 0xFF, (byte) 0xFF}; | ||
|
|
||
| assertThrows(IndexOutOfBoundsException.class, | ||
| () -> new MapInt64Message().parseFrom(data)); | ||
|
|
||
| ByteBuf pooled = PooledByteBufAllocator.DEFAULT.directBuffer(data.length); | ||
| try { | ||
| pooled.writeBytes(data); | ||
| assertThrows(IndexOutOfBoundsException.class, | ||
| () -> new MapInt64Message().parseFrom(pooled, pooled.readableBytes())); | ||
| } finally { | ||
| pooled.release(); | ||
| } | ||
|
|
||
| // Sanity: the same entry with a complete varint64 parses | ||
| byte[] valid = {0x0A, 0x06, 0x0A, 0x01, 'k', 0x10, (byte) 0xB9, 0x60}; | ||
| MapInt64Message parsed = new MapInt64Message(); | ||
| parsed.parseFrom(valid); | ||
| assertEquals(1, parsed.getCountersCount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidMessageOnTightCapacityBuffer() { | ||
| // A valid message on a zero-slack buffer never overruns | ||
| Proto3Message src = new Proto3Message(); | ||
| src.setIntField(42); | ||
| src.setLongField(123456789L); | ||
| src.setStringField("tight-capacity"); | ||
| byte[] data = src.toByteArray(); | ||
|
|
||
| ByteBuf tight = Unpooled.directBuffer(data.length, data.length); | ||
| try { | ||
| tight.writeBytes(data); | ||
| Proto3Message parsed = new Proto3Message(); | ||
| parsed.parseFrom(tight, tight.readableBytes()); | ||
| assertEquals(42, parsed.getIntField()); | ||
| assertEquals(123456789L, parsed.getLongField()); | ||
| assertEquals("tight-capacity", parsed.getStringField()); | ||
| assertEquals(0, tight.readableBytes()); | ||
| } finally { | ||
| tight.release(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.