[Swift] Read vtable field slots as unsigned VOffset to match the verifier - #9202
Open
bianyeyu wants to merge 1 commit into
Open
[Swift] Read vtable field slots as unsigned VOffset to match the verifier#9202bianyeyu wants to merge 1 commit into
bianyeyu wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
@googlebot I signed it! |
…fier TableVerifier.dereference validates vtable field slots as VOffset (UInt16), but the accessors re-read the same slots as Int16. A slot of 0x8000 is +32768 for the verifier (accepted when in bounds) but -32768 for the accessors, steering reads and writes before the start of a verified buffer. Read the slot as VOffset in Table.offset and the two static Table.offset(_:vOffset:fbb:) overloads used by generated lookupByKey/sortVectorOf code, so accessors observe the same displacement the verifier validated. Slots >= 0x8000 are format-legal for tables with more than 32KB of inline field data and are handled correctly after this change; slots <= 0x7FFF are bit-identical. Adds crafted-buffer regression tests for the read, mutate, and lookupByKey paths.
bianyeyu
force-pushed
the
swift-read-vtable-slots-unsigned
branch
from
August 20, 2026 06:02
8e692a8 to
052de38
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
TableVerifier.dereferencevalidates vtable field slots asVOffset(UInt16) and bounds-checkstable position + slot. The accessors re-read the same slots asInt16. A buffer accepted bygetCheckedRootverification can therefore carry a slot such as0x8000: the verifier treats it as +32768 (in bounds), whileTable.offsetinterprets it as -32768, steering field accessors before the start of the verified buffer. Becauseassert()is compiled out in release builds andByteBufferreads/writes are unchecked, this is a genuine out-of-bounds access on a fully verified buffer - both reads through field accessors and writes throughmutate.The same divergence exists in the two static
Table.offset(_:vOffset:fbb:)overloads used by generatedlookupByKey/sortVectorOf*code; theByteBufferoverload is reachable on externally supplied, verified buffers through public APIs such asscalarKeySortedTablesBy(key:).Fix
Read the slot as
VOffsetin all three places so the accessors observe exactly the displacement the verifier validated. No per-access bounds checks are added (consistent with #6209); this is the accessor-side counterpart of theVOffset-typed verifier introduced in #8577.Slots >= 0x8000 are format-legal (tables with more than 32KB of inline field data); they verify today but are misread by accessors, and this change makes them read correctly. Slots <= 0x7FFF are bit-identical between
Int16andVOffset, so existing buffers behave exactly as before.Tests
Three crafted-buffer regression tests in
FlatbuffersVerifierTestsverify a buffer first, then assert the accessors reach the verified field positions:mutateon the same crafted slotlookupByKeypath: sorted-table vector onMonsterWithout this change all three crash with out-of-bounds accesses; with it they pass, and the full Swift suite is green (97 tests).
Related: the analogous Kotlin sign-extension issue is tracked in #8990 (C-08). This PR covers the Swift runtime only; the verifier is unchanged.