Repository navigation
feat: encode bound refusal hooks and the static storage clamp contract - #194
Merged
Merged
Conversation
…clamps at a UTF-8 boundary Generated serialize() now refuses a value past a bound only the schema knows (a string or blob longer than its maxlen, an array with more elements than its count). The wrapper holds no bound: - OStreamImpl::rejectArgument() latches InvalidArgument (first failure wins), so ok()/error() report the refusal and no byte of the message is returned as a success. - sofab::ENCODE_BOUNDS (SOFAB_CPP_HAVE_ENCODE_BOUNDS) is true unless SOFAB_DISABLE_ENCODE_BOUNDS is defined. Generated code writes each check as `if (sofab::ENCODE_BOUNDS && ...)`, so the switch compiles all of them out for a footprint build that guarantees its values fit. Decode bound checks are unaffected. The static storage keeps clamping at assignment, as a documented contract, with two fixes: - FixedString::assign never cuts inside a UTF-8 character (detail::utf8Prefix, one copy for every capacity). - InlineVector::push_back past N drops the new value and keeps the held ones instead of overwriting the last element; emplace_back keeps its last-slot reuse for the decode binders. Tests: test/cpp/test_encode_bounds.cpp, the push_back case in test_istream.cpp, and encode_bounds_off.cpp (compile-only object that pins the opt-out). Refs sofa-buffers/generator#656
Andste82
force-pushed
the
feat/encode-bounds-656
branch
from
October 9, 2026 10:47
8076891 to
e87d758
Compare
…_BOUNDS sofab_object_encode read a STRING member with strlen, so a char[maxlen + 1] filled without a terminator was read past the member and emitted whatever followed it. The member is now read bounded by its own storage (field->size, data the descriptor already carries; the corelib knows no schema bound), and a value that fills it without a terminator -- longer than maxlen -- is refused with SOFAB_RET_E_ARGUMENT before any byte of it is written. The length is a plain byte loop instead of strlen: no libc call. SOFAB_DISABLE_ENCODE_BOUNDS (CMake option, applied PUBLIC; the C++ wrapper reads the same macro as sofab::ENCODE_BOUNDS) removes the refusal for a caller that guarantees its values fit; the read stays bounded and such a value is emitted as the whole buffer. A sized blob's length and a sized array's or wrapper holder's count past the capacity keep being clamped to it: that is the documented clamp contract, now pinned by tests. Tests (test/c/test_object.c): a string at its bound, an unterminated one (both builds), an unterminated array<string> element, and the blob/array/holder clamp. CI: a no-encode-bounds leg runs the unit suite and the vectors with the switch. Coverage (gcovr, the CI filter): lines 87.9% (903/1027) -> 88.0% (908/1032), branches 59.1% (528/894) -> 59.6% (536/900). Refs sofa-buffers/generator#656
Member
Author
|
Coverage correction for the C commit 3636e52: the CI coverage job prints lines 97.2% (904/930) -> 97.2% (909/935) and branches 83.5% (525/629) -> 83.9% (533/635) from main b031f3b to 3636e52 (functions 100% both). The figures in the commit message came from a local run with a different configuration; the PR body now states the CI tool's numbers. |
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
The C target encodes through descriptors inside corelib-c-cpp, so the string check lives there (corelib-c-cpp#194): sofab_object_encode reads a string member bounded by its own char[maxlen + 1] storage, which the descriptor already carries as the field size, and refuses a value that fills it without a terminator with SOFAB_RET_E_ARGUMENT. Before, the member was read with strlen, past the field. The generated code carries no guard and no bound; nothing schema-specific is emitted for this. - Blob length and array/holder count companions past the capacity keep being clamped by the corelib at encode: the documented clamp contract. The driver's CLAMP_CONTRACT line for c lists those six cells; KNOWN_GAP[c] is empty. The scalar cells stay STORAGE_BLOCKED, now with the true reason: the fixed-width C type guarantees the declared width. - The member notes say what encode does: a string is refused, a companion past its capacity is clamped. They are rendered by the C backend instead of the shared BoundDoc, whose "never truncated" is true of the decoder only. - The project harness hands encode what a caller's over-bound value would look like: an over-long JSON string is stored as its first cap bytes with no terminator (encode refuses it; terminating it at maxlen would hide the over-long value and could cut inside a UTF-8 character), and a blob or array stores at most its capacity but sets the length companion to the JSON length unclamped (saturated only at the companion's C type). So the clamp the conformance cells observe is the encoder's, not the harness's. - docs/generator/c.md: "Bounds at encode", with the SOFAB_DISABLE_ENCODE_BOUNDS opt-out (a corelib switch; the project Makefile's CFLAGS reach the corelib sources). Tests: encodebounds_test.go pins that the encode path emits no check, the notes, the harness's unterminated string store and unclamped length companions, and builds the project against the corelib with and without -DSOFAB_DISABLE_ENCODE_BOUNDS: the string refusal, and the encoder's clamp of a blob length and of array, array<string> and nested array counts. Negative controls on a scratch corelib copy: - refusal removed from object.c: the driver fails exactly s_over_1, s_over_60, s_over_utf8_80, s_over_midchar and as_elem_over_maxlen; the opt-out test fails its checks-on case. - blob clamp removed: the driver fails b_over_1 and b_over_60; the opt-out test fails its blob check in both builds. - array/holder count clamps removed: the driver fails au_count_plus_1, au_count_plus_2, as_count_plus_1 and n_arr_count_plus_1; the opt-out test fails its three count checks in both builds. Measurements (tests/bench/run.sh --rows c, corelib main b031f3b vs corelib-c-cpp#194), .text of generated code + corelib, SofaBuffers symbols: ARMv6-m 5594 -> 5606 (+12), opt-out 5606 ARMv7-m+fp.dp 5588 -> 5592 (+4), opt-out 5592 RV32IMC 6134 -> 6158 (+24), opt-out 6156 .data/.bss 0/0 unchanged. The growth is the bounded loop replacing the strlen call, which the bench excludes as libc; the image no longer links strlen (92 B in newlib-nano for ARMv6-m). Ir/op encode 25974 -> 26491 (+2.0%), decode 54510 unchanged. Needs sofa-buffers/corelib-c-cpp#194. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
The C target encodes through descriptors inside corelib-c-cpp, so the string check lives there (corelib-c-cpp#194): sofab_object_encode reads a string member bounded by its own char[maxlen + 1] storage, which the descriptor already carries as the field size, and refuses a value that fills it without a terminator with SOFAB_RET_E_ARGUMENT. Before, the member was read with strlen, past the field. The generated code carries no guard and no bound; nothing schema-specific is emitted for this. - Blob length and array/holder count companions past the capacity keep being clamped by the corelib at encode: the documented clamp contract. The driver's CLAMP_CONTRACT line for c lists those six cells; KNOWN_GAP[c] is empty. The scalar cells stay STORAGE_BLOCKED, now with the true reason: the fixed-width C type guarantees the declared width. - The member notes say what encode does: a string is refused, a companion past its capacity is clamped. They are rendered by the C backend instead of the shared BoundDoc, whose "never truncated" is true of the decoder only. - The project harness hands encode what a caller's over-bound value would look like: an over-long JSON string is stored as its first cap bytes with no terminator (encode refuses it; terminating it at maxlen would hide the over-long value and could cut inside a UTF-8 character), and a blob or array stores at most its capacity but sets the length companion to the JSON length unclamped (saturated only at the companion's C type). So the clamp the conformance cells observe is the encoder's, not the harness's. - docs/generator/c.md: "Bounds at encode", with the SOFAB_DISABLE_ENCODE_BOUNDS opt-out (a corelib switch; the project Makefile's CFLAGS reach the corelib sources). Tests: encodebounds_test.go pins that the encode path emits no check, the notes, the harness's unterminated string store and unclamped length companions, and builds the project against the corelib with and without -DSOFAB_DISABLE_ENCODE_BOUNDS: the string refusal, and the encoder's clamp of a blob length and of array, array<string> and nested array counts. Negative controls on a scratch corelib copy: - refusal removed from object.c: the driver fails exactly s_over_1, s_over_60, s_over_utf8_80, s_over_midchar and as_elem_over_maxlen; the opt-out test fails its checks-on case. - blob clamp removed: the driver fails b_over_1 and b_over_60; the opt-out test fails its blob check in both builds. - array/holder count clamps removed: the driver fails au_count_plus_1, au_count_plus_2, as_count_plus_1 and n_arr_count_plus_1; the opt-out test fails its three count checks in both builds. Measurements (tests/bench/run.sh --rows c, corelib main b031f3b vs corelib-c-cpp#194), .text of generated code + corelib, SofaBuffers symbols: ARMv6-m 5594 -> 5606 (+12), opt-out 5606 ARMv7-m+fp.dp 5588 -> 5592 (+4), opt-out 5592 RV32IMC 6134 -> 6158 (+24), opt-out 6156 .data/.bss 0/0 unchanged. The growth is the bounded loop replacing the strlen call, which the bench excludes as libc; the image no longer links strlen (92 B in newlib-nano for ARMv6-m). Ir/op encode 25974 -> 26491 (+2.0%), decode 54510 unchanged. Needs sofa-buffers/corelib-c-cpp#194. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
The C target encodes through descriptors inside corelib-c-cpp, so the string check lives there (corelib-c-cpp#194): sofab_object_encode reads a string member bounded by its own char[maxlen + 1] storage, which the descriptor already carries as the field size, and refuses a value that fills it without a terminator with SOFAB_RET_E_ARGUMENT. Before, the member was read with strlen, past the field. The generated code carries no guard and no bound; nothing schema-specific is emitted for this. - Blob length and array/holder count companions past the capacity keep being clamped by the corelib at encode: the documented clamp contract. The driver's CLAMP_CONTRACT line for c lists those six cells; KNOWN_GAP[c] is empty. The scalar cells stay STORAGE_BLOCKED, now with the true reason: the fixed-width C type guarantees the declared width. - The member notes say what encode does: a string is refused, a companion past its capacity is clamped. They are rendered by the C backend instead of the shared BoundDoc, whose "never truncated" is true of the decoder only. - The project harness hands encode what a caller's over-bound value would look like: an over-long JSON string is stored as its first cap bytes with no terminator (encode refuses it; terminating it at maxlen would hide the over-long value and could cut inside a UTF-8 character), and a blob or array stores at most its capacity but sets the length companion to the JSON length unclamped (saturated only at the companion's C type). So the clamp the conformance cells observe is the encoder's, not the harness's. - docs/generator/c.md: "Bounds at encode", with the SOFAB_DISABLE_ENCODE_BOUNDS opt-out (a corelib switch; the project Makefile's CFLAGS reach the corelib sources). Tests: encodebounds_test.go pins that the encode path emits no check, the notes, the harness's unterminated string store and unclamped length companions, and builds the project against the corelib with and without -DSOFAB_DISABLE_ENCODE_BOUNDS: the string refusal, and the encoder's clamp of a blob length and of array, array<string> and nested array counts. Negative controls on a scratch corelib copy: - refusal removed from object.c: the driver fails exactly s_over_1, s_over_60, s_over_utf8_80, s_over_midchar and as_elem_over_maxlen; the opt-out test fails its checks-on case. - blob clamp removed: the driver fails b_over_1 and b_over_60; the opt-out test fails its blob check in both builds. - array/holder count clamps removed: the driver fails au_count_plus_1, au_count_plus_2, as_count_plus_1 and n_arr_count_plus_1; the opt-out test fails its three count checks in both builds. Measurements (tests/bench/run.sh --rows c, corelib main b031f3b vs corelib-c-cpp#194), .text of generated code + corelib, SofaBuffers symbols: ARMv6-m 5594 -> 5606 (+12), opt-out 5606 ARMv7-m+fp.dp 5588 -> 5592 (+4), opt-out 5592 RV32IMC 6134 -> 6158 (+24), opt-out 6156 .data/.bss 0/0 unchanged. The growth is the bounded loop replacing the strlen call, which the bench excludes as libc; the image no longer links strlen (92 B in newlib-nano for ARMv6-m). Ir/op encode 25974 -> 26491 (+2.0%), decode 54510 unchanged. Needs sofa-buffers/corelib-c-cpp#194. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
The C target encodes through descriptors inside corelib-c-cpp, so the string check lives there (corelib-c-cpp#194): sofab_object_encode reads a string member bounded by its own char[maxlen + 1] storage, which the descriptor already carries as the field size, and refuses a value that fills it without a terminator with SOFAB_RET_E_ARGUMENT. Before, the member was read with strlen, past the field. The generated code carries no guard and no bound; nothing schema-specific is emitted for this. - Blob length and array/holder count companions past the capacity keep being clamped by the corelib at encode: the documented clamp contract. The driver's CLAMP_CONTRACT line for c lists those six cells; KNOWN_GAP[c] is empty. The scalar cells stay STORAGE_BLOCKED, now with the true reason: the fixed-width C type guarantees the declared width. - The member notes say what encode does: a string is refused, a companion past its capacity is clamped. They are rendered by the C backend instead of the shared BoundDoc, whose "never truncated" is true of the decoder only. - The project harness hands encode what a caller's over-bound value would look like: an over-long JSON string is stored as its first cap bytes with no terminator (encode refuses it; terminating it at maxlen would hide the over-long value and could cut inside a UTF-8 character), and a blob or array stores at most its capacity but sets the length companion to the JSON length unclamped (saturated only at the companion's C type). So the clamp the conformance cells observe is the encoder's, not the harness's. - docs/generator/c.md: "Bounds at encode", with the SOFAB_DISABLE_ENCODE_BOUNDS opt-out (a corelib switch; the project Makefile's CFLAGS reach the corelib sources). Tests: encodebounds_test.go pins that the encode path emits no check, the notes, the harness's unterminated string store and unclamped length companions, and builds the project against the corelib with and without -DSOFAB_DISABLE_ENCODE_BOUNDS: the string refusal, and the encoder's clamp of a blob length and of array, array<string> and nested array counts. Negative controls on a scratch corelib copy: - refusal removed from object.c: the driver fails exactly s_over_1, s_over_60, s_over_utf8_80, s_over_midchar and as_elem_over_maxlen; the opt-out test fails its checks-on case. - blob clamp removed: the driver fails b_over_1 and b_over_60; the opt-out test fails its blob check in both builds. - array/holder count clamps removed: the driver fails au_count_plus_1, au_count_plus_2, as_count_plus_1 and n_arr_count_plus_1; the opt-out test fails its three count checks in both builds. Measurements (tests/bench/run.sh --rows c, corelib main b031f3b vs corelib-c-cpp#194), .text of generated code + corelib, SofaBuffers symbols: ARMv6-m 5594 -> 5606 (+12), opt-out 5606 ARMv7-m+fp.dp 5588 -> 5592 (+4), opt-out 5592 RV32IMC 6134 -> 6158 (+24), opt-out 6156 .data/.bss 0/0 unchanged. The growth is the bounded loop replacing the strlen call, which the bench excludes as libc; the image no longer links strlen (92 B in newlib-nano for ARMv6-m). Ir/op encode 25974 -> 26491 (+2.0%), decode 54510 unchanged. Needs sofa-buffers/corelib-c-cpp#194. Refs #656
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.
Refs sofa-buffers/generator#656
Shared branch: the C++ wrapper part (this commit) comes first; the C backend's part of #656 follows on the same branch.
C++ wrapper
OStreamImpl::rejectArgument()([[gnu::cold, gnu::noinline]]): latchesError::InvalidArgument(first failure wins) and returns aResultcarrying it. Generatedserialize()calls it when a value is past a bound only the schema knows (string/blobmaxlen, arraycount) and the member is a growablestd::string/std::vector(allow_dynamic: true). The wrapper is handed no bound.sofab::ENCODE_BOUNDS/SOFAB_CPP_HAVE_ENCODE_BOUNDS: true unlessSOFAB_DISABLE_ENCODE_BOUNDSis defined. Generated checks are writtenif (sofab::ENCODE_BOUNDS && ...), so the switch removes them all from the image. Decode bound checks are unaffected.FixedString::assignclamps at a UTF-8 character boundary (detail::utf8Prefix, one non-template copy for everyN). Before:"xxxé"intoFixedString<4>kept"xxx\xC3", which went on the wire.InlineVector::push_backpastNdrops the new value instead of overwriting the last element.emplace_backkeeps its last-slot reuse for the decode binders.The static storage (
allow_dynamic: false, the default) keeps clamping at assignment as a documented contract (generator#656 decision of 2026-10-07).Tests
test/cpp/test_encode_bounds.cpp(Catch2): therejectArgumentverdict and first-failure-wins, a refusingserialize(), theFixedStringclamp at 2-, 3- and 4-byte characters,InlineVector/FixedBytespast capacity.test/cpp/test_istream.cpp: the existing push_back-at-capacity case now pins the drop;emplace_backkeeps its last-slot reuse.test/cpp/encode_bounds_off.cpp: compile-only object,static_asserts thatSOFAB_DISABLE_ENCODE_BOUNDSturnssofab::ENCODE_BOUNDSoff.Coverage (gcovr, Debug + coverage + strict UTF-8, as the CI job):
src/*.c|h(the CI filter): unchanged, lines 87.9% (903/1027), branches 59.1% (528/894).src/include/sofab/*.hpp(for information): main lines 81.4% (2324/2854), branches 52.6% (511/972); this branch lines 82.0% (2413/2941), branches 53.6% (538/1004).Footprint
.textof generated code + corelib after--gc-sections(tests/bench/run.sh; generator main 3f85fbf + corelib main b031f3b vs. the #656 generator change + this branch):-DSOFAB_DISABLE_ENCODE_BOUNDS.data/.bssunchanged (0 / 180 and 0 / 300). The static row carries no guard (its storage clamps at assignment); the UTF-8 clamp keeps the fits-case inline inFixedString::assign, so a literal default still folds and the static row is byte-identical. On the dynamic row the 18 guards of the bench schema cost 202 B withrejectArgumentout of line (300 B with it inlined). Ir/op: cpp-c-cpp unchanged (32983 / 40862), cpp-c-cpp-dyn encode 33335 -> 33437 (+0.3%), decode 52167 -> 52164.C object API (second commit)
sofab_object_encoderead aSTRINGmember withstrlen, so achar[maxlen + 1]filled without a terminator was read past the member and emitted whatever followed it. It is now read bounded byfield->size(descriptor data; the corelib knows no schema bound), and a value that fills the member without a terminator, i.e. longer thanmaxlen, is refused withSOFAB_RET_E_ARGUMENTbefore any of its bytes are written. The length is a plain byte loop: no libc call.SOFAB_DISABLE_ENCODE_BOUNDSis now also a CMake option (appliedPUBLIC) and documented insofab.hand the README switch table. In C it removes the refusal; the read stays bounded and such a value is emitted as the whole buffer.Tests (
test/c/test_object.c): a string at its bound, an unterminated one (both builds,#if-guarded), an unterminatedarray<string>element, and the blob/array/holder clamp. Negative control: with the refusal removed fromobject.c, the two refusal tests fail. CI: ano-encode-boundsleg builds and runs the unit suite and the vectors with the switch.Coverage (gcovr as printed by the CI
coveragejob): main b031f3b lines 97.2% (904/930), functions 100.0% (97/97), branches 83.5% (525/629); C++ commit e87d758 the same; head 3636e52 lines 97.2% (909/935), functions 100.0% (97/97), branches 83.9% (533/635). The figures in the 3636e52 commit message came from a local run with a different configuration and are superseded by these.Footprint,
crow of the generator bench (generated code + corelib after--gc-sections, SofaBuffers symbols only; libc floors excluded by name):SOFAB_DISABLE_ENCODE_BOUNDSThe growth is the bounded loop that replaces the
strlencall; the compare itself costs 0-2 B.object.cno longer callsstrlen, which the bench counts as excluded libc: a real image drops it (16 B for the plainest loop, 92 B for newlib-nano's ARMv6-mstrlen). Two other loop shapes (pointer, do-while) measured 8-12 B larger. Ir/op (host, -O3): encode 25974 -> 26491 (+2.0%, the byte loop vs glibc's vectorisedstrlen), decode unchanged at 54510.