Skip to content

feat: encode bound refusal hooks and the static storage clamp contract - #194

Merged
Andste82 merged 2 commits into
mainfrom
feat/encode-bounds-656
Oct 9, 2026
Merged

Andste82 merged 2 commits into
mainfrom
feat/encode-bounds-656

Conversation

@Andste82

@Andste82 Andste82 commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

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]]): latches Error::InvalidArgument (first failure wins) and returns a Result carrying it. Generated serialize() calls it when a value is past a bound only the schema knows (string/blob maxlen, array count) and the member is a growable std::string/std::vector (allow_dynamic: true). The wrapper is handed no bound.
  • sofab::ENCODE_BOUNDS / SOFAB_CPP_HAVE_ENCODE_BOUNDS: true unless SOFAB_DISABLE_ENCODE_BOUNDS is defined. Generated checks are written if (sofab::ENCODE_BOUNDS && ...), so the switch removes them all from the image. Decode bound checks are unaffected.
  • FixedString::assign clamps at a UTF-8 character boundary (detail::utf8Prefix, one non-template copy for every N). Before: "xxxé" into FixedString<4> kept "xxx\xC3", which went on the wire.
  • InlineVector::push_back past N drops the new value instead of overwriting the last element. emplace_back keeps 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): the rejectArgument verdict and first-failure-wins, a refusing serialize(), the FixedString clamp at 2-, 3- and 4-byte characters, InlineVector/FixedBytes past capacity.
  • test/cpp/test_istream.cpp: the existing push_back-at-capacity case now pins the drop; emplace_back keeps its last-slot reuse.
  • test/cpp/encode_bounds_off.cpp: compile-only object, static_asserts that SOFAB_DISABLE_ENCODE_BOUNDS turns sofab::ENCODE_BOUNDS off.

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

.text of generated code + corelib after --gc-sections (tests/bench/run.sh; generator main 3f85fbf + corelib main b031f3b vs. the #656 generator change + this branch):

row arch before after (checks on) after, -DSOFAB_DISABLE_ENCODE_BOUNDS
cpp-c-cpp (static) ARMv6-m 8694 8694 8694
cpp-c-cpp (static) ARMv7-m 8296 8296 8296
cpp-c-cpp-dyn ARMv6-m 15632 15834 (+202) 15632
cpp-c-cpp-dyn ARMv7-m 14370 14602 (+232) 14370

.data/.bss unchanged (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 in FixedString::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 with rejectArgument out 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_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. It is now read bounded by field->size (descriptor data; the corelib knows no schema bound), and a value that fills the member without a terminator, i.e. longer than maxlen, is refused with SOFAB_RET_E_ARGUMENT before any of its bytes are written. The length is a plain byte loop: no libc call.
  • SOFAB_DISABLE_ENCODE_BOUNDS is now also a CMake option (applied PUBLIC) and documented in sofab.h and the README switch table. In C it removes the refusal; 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 stay clamped to it: the documented clamp contract, now pinned by tests.

Tests (test/c/test_object.c): a string at its bound, an unterminated one (both builds, #if-guarded), an unterminated array<string> element, and the blob/array/holder clamp. Negative control: with the refusal removed from object.c, the two refusal tests fail. CI: a no-encode-bounds leg builds and runs the unit suite and the vectors with the switch.

Coverage (gcovr as printed by the CI coverage job): 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, c row of the generator bench (generated code + corelib after --gc-sections, SofaBuffers symbols only; libc floors excluded by name):

arch before after (checks on) after, SOFAB_DISABLE_ENCODE_BOUNDS
ARMv6-m 5594 5606 (+12) 5606 (+12)
ARMv7-m+fp.dp 5588 5592 (+4) 5592 (+4)
RV32IMC 6134 6158 (+24) 6156 (+22)

The growth is the bounded loop that replaces the strlen call; the compare itself costs 0-2 B. object.c no longer calls strlen, 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-m strlen). 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 vectorised strlen), decode unchanged at 54510.

…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
Andste82 force-pushed the feat/encode-bounds-656 branch from 8076891 to e87d758 Compare October 9, 2026 10:47
…_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
@Andste82

Andste82 commented Oct 9, 2026

Copy link
Copy Markdown
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
Andste82 merged commit 0ba9be9 into main Oct 9, 2026
26 checks passed
@Andste82
Andste82 deleted the feat/encode-bounds-656 branch October 9, 2026 19:33
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant