Repository navigation
fix(object): compare a sized blob against its default image, not the empty blob - #192
Conversation
…empty blob _field_is_default hard-coded a sized blob's default as used_len == 0 and never read the default image. A blob with a non-empty default therefore wrote the default when left untouched, and dropped an explicit empty blob as if it were the default (MESSAGE_SPEC section 2). Handle the blob in the sized-array branch (a blob is an array of one-byte elements there): length first, then the used prefix against the image; no image keeps the empty blob as the default. One branch instead of two, object.c is 22 B (armv6-m) / 34 B (armv7e-m) smaller. Fixes #191
Andste82
left a comment
There was a problem hiding this comment.
Reviewed against #191. The diagnosis and the direction are right -- folding the blob into the sized-array branch is the correct shape, the spec reading matches MESSAGE_SPEC §2, and I reproduced 83 Tests 0 Failures plus the 131-vector runner on this branch. One real finding on footprint, and three smaller ones; all inline, all addressed in a follow-up commit I pushed to this branch.
Footprint, the finding. The PR measures only the full configuration. With SOFAB_DISABLE_ARRAY_SUPPORT the whole width != 0 block used to be #if-ed out and the blob took the two-line branch; now the generic block compiles in, including a variable division that pulls __aeabi_uidiv on a core without one:
_field_is_default 208 -> 314 B (+106)
sofab_object_init 268 -> 272 B (+4)
That is the row README:778 publishes as the per-switch delta for SOFAB_DISABLE_ARRAY_SUPPORT, so the table needs regenerating either way.
Measured A/B, src/object.c .text, -Os -DNDEBUG (CMake Release -- what tools/footprint.sh measures), fixed being the follow-up commit:
| Config | Arch | main | this PR | fixed |
|---|---|---|---|---|
| full | ARMv6-m | 1664 | 1640 (-24) | 1640 |
| full | ARMv7e-m+fp.dp | 1680 | 1644 (-36) | 1644 |
DISABLE_ARRAY |
ARMv6-m | 1268 | 1372 (+104) | 1320 (+52) |
DISABLE_ARRAY |
ARMv7e-m+fp.dp | 1268 | 1356 (+88) | 1328 (+60) |
DISABLE_FIXLEN |
ARMv6-m | 1368 | 1372 (+4) | 1372 |
| minimal | ARMv6-m | 352 | 352 | 352 |
.data/.bss stay 0 in every row. The guard is free in the full configuration and takes back 52 B / 28 B in the array-free one; the remaining ~+52 B is the irreducible price of reading the default image at all, and belongs in the PR description rather than being left out.
Out of scope here, but worth not losing: the generator follow-up this PR's description promises (blob default image emits .bl without .bl__len, while arrays do get .x__len) has no issue filed in the generator repo. Until it does, a non-empty blob default stays silently empty in generated code. Related and unverified: the C++ leg omits defaults through per-field comparisons in generated code, not through _field_is_default, so the same bug class may well still be live for FixedBytes with a non-empty default -- the PR's generator verification covered the C leg only.
| * logical default is the empty value. The buffer past the used length | ||
| * is indeterminate and never influences the decision. */ | ||
| size_t esz = field->type == SOFAB_OBJECT_FIELDTYPE_BLOB | ||
| ? 1 : field->element_size; |
There was a problem hiding this comment.
The 1 is load-bearing in a way nothing here records, and the ternary leaves footprint on the table.
For a BLOB the descriptor macros put sizeof(buffer) & 0xF in element_size -- the capacity, not an element width (SOFAB_OBJECT_FIELD_BLOB_SIZED in object.h, same expression as SOFAB_OBJECT_FIELD). At a capacity of 16 that is 0, so field->size / esz on the next line would be a division by zero if a blob ever reached it with element_size. The ternary is what makes that unreachable, and a future "simplification" that drops it reintroduces UB silently. It deserves to say so.
Second, as a ternary this costs the array-free build: with SOFAB_DISABLE_ARRAY_SUPPORT the blob is the only kind that reaches this block, so an #if folds esz to a constant and GCC emits neither the division (__aeabi_uidiv, no hardware divide on Cortex-M0) nor the multiply below. 52 B on ARMv6-m, 28 B on ARMv7e-m, and provably 0 B in the full configuration. object.c:1069 already uses exactly this #if shape in the decode path for the same reason, so it is the house pattern:
size_t esz = 1;
#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT)
if (field->type != SOFAB_OBJECT_FIELDTYPE_BLOB)
esz = field->element_size;
#endifFixed in the follow-up commit, with the capacity-16 trap written into the comment and a test_object_blob_sized_default_capacity_16 regression test that would crash rather than quietly pass if the 1 ever goes away. (Verified by hand before the test existed: capacity 16, default {3, "abc"} -- at default omitted, explicit empty 02 03, full 16 bytes round-trip.)
| TEST_ASSERT_EQUAL_size_t(sizeof(exp_h), n); | ||
| TEST_ASSERT_EQUAL_UINT8_ARRAY(exp_h, buf, n); | ||
|
|
||
| /* 4. absent decodes to the default; a written value round-trips. */ |
There was a problem hiding this comment.
Step numbering jumps 2. -> 4.; the sibling test_object_blob_sized above does have a 3., so this reads as a dropped step rather than a style. The prefix case under 2. really is its own step (same bytes, shorter length -- it passes for a different reason than "same length, other bytes"), so the follow-up commit splits it into 2. and 3. instead of renumbering this line down.
| &_info_defaults_blobsized); | ||
|
|
||
| static size_t blobsized_def_encode (const sofab_object_descr_t *info, | ||
| const blobsized_t *in, uint8_t *buf, size_t n) |
There was a problem hiding this comment.
Minor, and the follow-up commit changes it: blobsized_def_encode/_decode are typed to const blobsized_t *, but they are entirely descriptor-driven -- sofab_object_encode itself takes the object as const void *. Typing them void * lets a second blob layout (the capacity-16 case) share them instead of needing a duplicate pair.
…6 trap Review of #192. The sized-blob/array merge left two things on the table. esz was a ternary, so an array-free build still emitted the capacity division -- __aeabi_uidiv on a core without a divide instruction -- for the one field kind that can reach the block there. Behind #if the constant folds and neither the division nor the multiply is generated: object.c .text goes 1372 -> 1320 B (armv6-m) and 1356 -> 1328 B (armv7e-m) with SOFAB_DISABLE_ARRAY_SUPPORT, and is unchanged in the full configuration. Same #if shape the decode path at object.c:1069 already uses. The 1 is also load-bearing against a division by zero: for a BLOB the descriptor macros record sizeof(buffer) & 0xF in element_size -- the capacity, not an element width -- which is 0 at a capacity of 16. Nothing said so and no test covered it, so a later "simplification" could reach for element_size and reintroduce UB silently. Now the comment states it and test_object_blob_sized_default_capacity_16 exercises it: at default omitted, explicit empty written as 02 03, full 16 bytes round-tripped. Also: split the prefix case in test_object_blob_sized_default_nonempty into its own step (the numbering jumped 2. -> 4.), and type the two blobsized_def_* helpers on void * -- they are descriptor-driven, like sofab_object_encode itself -- so both blob layouts share them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
object.c moved, so the published numbers did. Regenerated with tools/footprint.sh against origin/main as the baseline, and reproduced byte-identically by a second run. Full configuration .text: ARMv6-m 3960 -> 3938 B, ARMv7e-m+fp.dp 3960 -> 3926, RV32IMC 5080 -> 5038, atmega8 7550 -> 7546. The two Minimal rows do not move -- the branch this fix touches is already compiled out there. .data/.bss stay 0 everywhere. In the per-switch table SOFAB_DISABLE_ARRAY_SUPPORT goes the other way, 2918 -> 2972 B: the blob now reads its default image in that configuration too, which is the point of the fix and is not free. SOFAB_OBJECT_DESCR_SMALL (+16 B) now sorts below SOFAB_ENABLE_SKIP_COUNTER (+18 B), so the two rows swap. The prose absolute for SOFAB_DISABLE_LAZY_SEQ_SUPPORT follows its row to 3662 B; every delta the hold-back section quotes is a difference between two builds that both carry this change, so those are unchanged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Pushed as 95b7e22 (code + test) and a7a4f0c (re-measured tables). One correction to my review above. I first reported the Final library-wide A/B,
Per-switch, ARMv6-m: Verified on the pushed head: Still open, and not for this PR: the generator-side |
…6 trap Review of #192. The sized-blob/array merge left two things on the table. esz was a ternary, so an array-free build still emitted the capacity division -- __aeabi_uidiv on a core without a divide instruction -- for the one field kind that can reach the block there. Behind #if the constant folds and neither the division nor the multiply is generated: object.c .text goes 1372 -> 1320 B (armv6-m) and 1356 -> 1328 B (armv7e-m) with SOFAB_DISABLE_ARRAY_SUPPORT, and is unchanged in the full configuration. Same #if shape the decode path at object.c:1069 already uses. The 1 is also load-bearing against a division by zero: for a BLOB the descriptor macros record sizeof(buffer) & 0xF in element_size -- the capacity, not an element width -- which is 0 at a capacity of 16. Nothing said so and no test covered it, so a later "simplification" could reach for element_size and reintroduce UB silently. Now the comment states it and test_object_blob_sized_default_capacity_16 exercises it: at default omitted, explicit empty written as 02 03, full 16 bytes round-tripped. Also: split the prefix case in test_object_blob_sized_default_nonempty into its own step (the numbering jumped 2. -> 4.), and type the two blobsized_def_* helpers on void * -- they are descriptor-driven, like sofab_object_encode itself -- so both blob layouts share them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Fixes #191
Problem
_field_is_default(src/object.c) hard-coded the default of a sized blob as "used length == 0" and ignored the default image. With a non-empty declared default (example:{2, "Hi"}):02 13 48 69instead of nothing;02 03, so it did not round-trip.MESSAGE_SPEC.md§2: "The encoder MUST emit a field iff its value ≠ its default"; and "absent → reconstructed as the declared default (which may be non-empty [...]); explicit empty → the empty collection, overriding that default."Fix
FIXLEN || ARRAYsupport.Tests
New in
test/c/test_object.c:test_object_blob_sized_default_nonempty: default omitted; same-length different bytes and a prefix are written; absent decodes to the default; written values round-trip.test_object_blob_sized_default_explicit_empty: explicit empty written as02 03, decodes back as empty.test_object_blob_sized_no_default_image_unchanged: no image / empty image, empty is omitted.Before the fix (main + tests):
83 Tests 2 FailuresAfter:
83 Tests 0 Failures; full ctest (full config, C++ on) 5/5 passed; ASan+UBSan build (SOFAB_ENABLE_FUZZ=ON, GCC, Debug) 5/5 passed. Theminimalconfiguration and builds withSOFAB_DISABLE_FIXLEN_SUPPORT/SOFAB_DISABLE_ARRAY_SUPPORTcompile.Footprint
object.c.text, full configuration, -Os: armv6-m 1678 -> 1656 B (-22), armv7e-m+fp.dp 1694 -> 1660 B (-34)..data/.bssunchanged (0).Sibling kinds
Checked, no change needed: sized strings/char arrays compare with
strncmpagainst the default image; sized arrays of every element kind (also inside structs, which recurse through the same function) already used the length-first branch; sized wrapper holders carry no default image by design (default is the empty array).Generator verification
Using the generator's shared driver
tests/conformance/lib/check_empty_vs_absent.py(generator#645 branch, C leg only): with an unpatched corelib it fails ({}writes7a 13 48 69, explicit emptyblwrites nothing). The generated default image currently sets.bl = {0x48, 0x69}but not.bl__len(arrays do get.x__len), so the image's blob length is 0. With only this fix the driver still reports{}as non-default; with.bl__len = 2added to the generated image by hand, the driver passes on bothdecodeandstreamdecodewith this fix and fails identically on main. The generator therefore needs a one-line follow-up (emit.bl__lenin the blob default image); this PR does not regress the current output.