Repository navigation
rust: bounds-check scalar access (panic instead of UB) and fix VTable::get_field OOB read - #9304
Open
just-s1lence wants to merge 1 commit into
Open
just-s1lence wants to merge 1 commit into
just-s1lence 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. |
just-s1lence
force-pushed
the
rust-bounds-safety-hardening
branch
from
October 5, 2026 13:06
f05b781 to
d563c4a
Compare
This branch has not been deployed
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.
Summary
The Rust runtime's byte-level scalar access layer (
read_scalar,read_scalar_at,emplace_scalar,emplace_scalar_array) used rawcopy_nonoverlappingcalls withno length checks. Out-of-range accesses were undefined behavior in release builds
(the only guard was a
debug_assert!, stripped with debug assertions; two of the fourfunctions had no guard at all).
This PR makes the whole scalar access layer bounds-checked by construction:
indexing/
copy_from_slicebehind a single sealed trait(
endian_scalar::private::TriviallyTransmutable::load_ne/store_ne), which isnow the only place in the runtime where raw bytes become scalar values.
unsafe fnsignatures are kept as-is so that existing callers — includingflatc-generated code that wraps these calls inunsafe {}— remain sourcecompatible; their
# Safetycontracts were updated to document the new behavior.the remaining 3 are genuinely unavoidable (
follow_cast_ref,Array::as_ptr,array_init'sMaybeUninitloop) and each carries a safety comment.Additionally, two bugs in
VTable:VTable::get_fieldout-of-bounds read. The guard wasidx > num_fields()but field indices are zero-based, soidx == num_fields()read two bytes past the end of the vtable — reachable from safe code via
Table::vtable().get_field(n)(e.g. past the end of the allocation). Fixed toidx >= num_fields().num_fieldsunderflow.(num_bytes / SIZE_VOFFSET) - 2underflowed formalformed (0- or 2-byte) vtables — a debug panic and a release-mode wrap to
usize::MAX. Now saturates. (The verifier can currently accept such vtables.)The
/facepalmcomment onVector::default— which exists solely because the oldread_scalar_atwas UB on a zero-length buffer — was updated accordingly.Evidence: this is UB today
New test file
tests/rust_usage_test/tests/safety_hardening_test.rs(11 tests) failson current
masterand passes with this PR. Under Miri (-C debug-assertions=off,i.e. release-like) the pristine tree reports undefined behavior for every vector:
read_scalar_on_short_slice_panics_instead_of_ubUndefined Behavior: attempting to access 4 bytes, ... only 3 bytes from the endread_scalar_at_truncated_tail_panics_instead_of_ubread_scalar_past_end_of_buffer_panics_instead_of_ubemplace_scalar_on_short_destination_panics_instead_of_ubemplace_scalar_array_past_end_of_buffer_panics_instead_of_ubemplace_scalar_array_with_out_of_range_offset_panics_instead_of_ubvtable_get_field_on_exact_sized_buffer_is_not_ub0vtable_num_fields_does_not_underflow_on_short_vtableattempt to subtract with overflow0scalar_roundtrip_little_endian,vtable_get_field_never_reads_past_the_vtable,default_vector_stays_usableThe
emplace_scalar_arraycase is notable: the OOB write is silent even in debugbuilds on master (no assert, no panic — the test fails with "did not panic as
expected").
Testing (all run locally)
tests/rust_usage_test:cargo test— all green (276 integration + 21 + 13 + 11 + …)cargo test --no-default-features(no_std runtime) — all greencargo clippyinrust_usage_test— no warnings; runtime crate: 14 → 13 warnings(this PR removes one, adds none)
MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri test(the CI recipe) — all green, including the new teststests/rust_serialize_test(cargo run),tests/rust_reflection_test(112 tests) — greencargo +nightly buildforthumbv7m-none-eabi(rust_no_std_compilation_test) — greenflatbuffers_alloc_check/flexbuffers_alloc_checkbins — greencargo test -p flatbuffers: 6/7 green;builder::tests::with_internal_capacity_preallocates_vecsfails identically on pristine
master(b8431fb) and is unrelated: the stdstrings_poolis aHashMap<String, …>(since feat: use HashMap for create_shared_string to fix O(N²) performance #8958), and insertingStringkeysallocates by design, so the "0 allocations" expectation no longer holds. Not touched here.
Performance
cargo benchbefore/after (same machine, debug off):read_monstersserialize_monsterstraverse_canonical_buffercreate_many_tablesNo regression; the bounds checks are on
size_of-derived constant lengths and areelided by the optimizer in the hot paths.
Compatibility notes
read_scalar/read_scalar_at/emplace_scalar/emplace_scalar_arrayremainunsafe fn(their bodies are now safe), so existingflatc-generated code and downstreamunsafe {}call sites compile unchanged, andno
unused_unsafewarnings are introduced.debug_assert!panic (debug) now consistently panic. This only affects inputs thatviolated the documented (old) contracts, i.e. buggy callers or malformed-data
handling that previously "worked" by reading adjacent memory.
no_stdis preserved (onlycoreslice ops are used).