Skip to content

feat(encode): rejectArgument hook; static storage clamps at a UTF-8 boundary - #153

Merged
Andste82 merged 1 commit into
mainfrom
feat/encode-bounds-656
Oct 9, 2026
Merged

Andste82 merged 1 commit into
mainfrom
feat/encode-bounds-656

Conversation

@Andste82

@Andste82 Andste82 commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Refs sofa-buffers/generator#656

What

  • 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). The corelib is handed no bound; it only records the refusal, so ok()/error() report it and encode() returns no bytes as a success. Encode-side counterpart of IStreamImpl::invalidate().
  • 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".
  • InlineVector::push_back past N drops the new value and keeps the held ones (it used to overwrite the last element via emplace_back). emplace_back keeps its last-slot reuse, which the decode binders rely on.

The assignment-time clamp of the static storage (allow_dynamic: false) is a documented contract (generator#656 decision of 2026-10-07); these two fixes make it a correct one.

Tests

encodeBoundRefusalAndAssignmentClamp in test/test_roundtrip.cpp: the Result and stream verdict, first-failure-wins against BufferFull, a refusing serialize(), and the clamp of FixedString (2-, 3-, 4-byte characters, nothing fits, cut exactly at a boundary), InlineVector (copy and move push_back, initializer list) and FixedBytes.

Coverage (gcovr, include/sofab/*.hpp, Debug + coverage, as the CI job):

  • main 65755b0: lines 93.6% (7768/8296), functions 95.7% (1437/1501), branches 70.8% (3295/4657)
  • this branch: lines 93.7% (7853/8382), functions 95.8% (1470/1534), branches 70.8% (3326/4695; 70.75% -> 70.84%)

Generator impact

Instructions per operation (tests/bench/run.sh, callgrind, generator main 3f85fbf + corelib main 65755b0 vs. the #656 generator change + this branch):

row encode Ir/op before after decode Ir/op before after
cpp-cpp 11904 11546 (-3.0%) 28116 28254 (+0.5%)
cpp-cpp-static 10588 10588 21322 21325
cpp-cpp-unbounded 8496 8490 21663 21663

rejectArgument is cold and out of line on purpose: inlined at the 18 guard sites of the bench schema it pushed serialize() past GCC's inlining budget for writeIntArray<uint16_t> (12423 Ir/op encode, +4.4%); out of line the encode is cheaper than before the guards (11546). The decode +0.5% is outside the changed code (same TU, inlining noise).

…oundary

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 corelib holds no bound: the new
OStreamImpl::rejectArgument() only latches InvalidArgument, so ok() and
error() report the refusal like the corelib's own §6.2/§6.4 refusals and
no byte of the message is returned as a success. It is the encode-side
counterpart of IStreamImpl::invalidate().

The static storage keeps clamping at assignment, now as a documented
contract, with two fixes:

- FixedString::assign never cuts inside a UTF-8 character: the cut moves
  back to the start of the character that would not fit whole
  (detail::utf8Prefix, one copy for every capacity). Before, "xxxé"
  into FixedString<4> kept "xxx\xC3", which the strict-UTF-8 encoder then
  refused.
- InlineVector::push_back past N drops the new value and keeps the held
  ones. It used to overwrite the last element (it went through
  emplace_back, whose last-slot reuse is the decode binder's OOB guard and
  stays as it is).

Tests: encodeBoundRefusalAndAssignmentClamp in test_roundtrip.

Refs sofa-buffers/generator#656
@Andste82
Andste82 force-pushed the feat/encode-bounds-656 branch from 42ff32f to e96b937 Compare October 9, 2026 10:47
@Andste82
Andste82 merged commit 1f6ffe3 into main Oct 9, 2026
9 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
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
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
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
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