From dc0cbb8045d5653f3757de534f5705fa89ff8fd3 Mon Sep 17 00:00:00 2001 From: Andreas Steinbart Date: Tue, 13 Oct 2026 05:52:27 +0000 Subject: [PATCH 1/5] perf: a default image may be the PREFIX of an object, not a copy of it SOFAB_OBJECT_DESCR_WITH_DEFAULTS documented its argument as "a fully-populated object", and every read addresses the image through the FIELD's own offset (object.c: init's memcpy and its sized-length seed, and the three compares in _field_is_default). So the only buffer a generator could hand over was a copy of the whole struct -- for sofa-buffers/generator's own realworld schema, 824 bytes of .rodata to carry 16 bytes of actual defaults, 14.6 % of that target's entire flash image. The descriptor now records how many bytes of the object the image describes, so it may stop after the members that have defaults: const uint16_t count_extent; /* was: const uint16_t field_count */ A wrapper-array HOLDER keeps all 16 bits for its element slot count, so an `array` still has its 300 slots. Every other object spends the low byte on the field count -- 255 max, asserted by the macros -- and the high byte on the image's extent. The descriptor therefore does NOT grow: still 16 bytes on a 32-bit target, where a separate extent field would have made it 20. Read through _field_count() and _default_extent(), never directly. A field at or past the extent has no declared default and is compared against (and seeded with) zero, which is what "no declared default" has always meant -- so _field_default() returning NULL past the extent lands on the existing zero-compare path rather than a new one. An extent of 0 reads as "the whole object": what every descriptor written before this field carries (an omitted initialiser is 0), what a union image uses, and what an image longer than 255 bytes falls back to. Old descriptors and old generated code therefore keep working unchanged -- verified by building generator-emitted code from before this change against this corelib with -Wall -Wextra -Werror. Measured with sofa-buffers/generator's `tests/bench --rows c` (vehicle_telemetry, cortex-m0/m7/rv32imc, -Os), generator emitting prefix images: .text ARMv6-m 5628 -> 5016 (-612) ARMv7-m 5616 -> 4982 (-634) RV32IMC 6170 -> 5560 (-610) Ir/op encode 26491 -> 26567 (+0.3 %) decode 54510 -> 54568 (+0.1 %) The field count is hoisted out of the three loops that had it in their condition: at -Os the load+mask was re-evaluated per iteration, which cost 2035 Ir/op on decode (+3.8 %) before. Tests: a prefix image seeds the fields it covers and leaves a field past the extent at zero -- with 0xDEADBEEF sitting in the image buffer exactly where that field would be read from, so the boundary shows up as a wrong VALUE and not only under a sanitizer; extent 0 still means the whole object; and a 300-slot holder reaches its last element, which a byte-wide slot count would have cut at 255. Each is mutation-checked: ignoring the extent fails the first, reading the slot count byte-wide fails the third. --- src/include/sofab/object.h | 67 ++++++++++++-- src/object.c | 107 +++++++++++++++++----- test/c/test_object.c | 179 +++++++++++++++++++++++++++++++++++++ 3 files changed, 326 insertions(+), 27 deletions(-) diff --git a/src/include/sofab/object.h b/src/include/sofab/object.h index 67110ee..1fca94e 100644 --- a/src/include/sofab/object.h +++ b/src/include/sofab/object.h @@ -278,6 +278,31 @@ extern "C" { + SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)), \ type, (sizeof(((obj *)0)->dfield[0]) & 0xF) } +/*! + * @brief Compile-time limit on an OBJECT's field count (message, struct, union). + * + * The low byte of @ref sofab_object_descr_t::count_extent holds it, so 255 is + * the ceiling and a 256-field object fails to compile here instead of wrapping + * silently. A wrapper-array HOLDER is not affected: its slot count uses all 16 + * bits (see @ref SOFAB_OBJECT_DESCR_SEQ_SIZED). + */ +#define SOFAB_OBJECT_ASSERT_FIELD_COUNT(n) \ + (0u * sizeof(struct { int an_object_may_not_declare_more_than_255_fields \ + : ((n) <= 255) ? 1 : -1; })) + +/*! + * @brief The @ref sofab_object_descr_t::count_extent word of an object: the + * field count in the low byte, the default image's byte extent in the + * high byte. + * + * An image longer than 255 bytes stores an extent of 0, which reads as "the + * image covers the whole object" -- the conservative meaning, and the one every + * descriptor written before the extent existed carries. + */ +#define SOFAB_OBJECT_COUNT_EXTENT(field_count, extent) \ + ((uint16_t)(((field_count) + SOFAB_OBJECT_ASSERT_FIELD_COUNT(field_count)) \ + | (uint16_t)(((extent) <= 255u ? (extent) : 0u) << 8))) + /*! * @brief Build an object descriptor (@ref sofab_object_descr_t) without defaults. * @@ -290,7 +315,7 @@ extern "C" { * @param nested_count Number of entries in @p nested_list. */ #define SOFAB_OBJECT_DESCR(field_list, field_count, nested_list, nested_count) \ - { (field_list), (nested_list), NULL, (field_count), (nested_count), 0 } + { (field_list), (nested_list), NULL, SOFAB_OBJECT_COUNT_EXTENT(field_count, 0u), (nested_count), 0 } /*! * @brief Build an object descriptor with a default-values reference. @@ -303,10 +328,20 @@ extern "C" { * @param field_count Number of entries in @p field_list. * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL). * @param nested_count Number of entries in @p nested_list. - * @param default_struct Pointer to a fully-populated object holding the field defaults. + * @param default_struct Pointer to an object holding the field defaults. It need + * NOT be a whole object: any PREFIX of one will do, as long + * as every field it describes sits at the same offset as in + * the object itself, because that is how the fields are + * addressed. @c sizeof(*default_struct) is recorded as the + * extent and a field at or past it is compared against zero + * -- which is what "no declared default" means. A prefix is + * what the SofaBuffers generator emits: a message that + * defaults one byte then costs one byte of @c .rodata + * instead of a copy of the whole object. */ #define SOFAB_OBJECT_DESCR_WITH_DEFAULTS(field_list, field_count,nested_list, nested_count, default_struct) \ - { (field_list), (nested_list), (default_struct), (field_count), (nested_count), 0 } + { (field_list), (nested_list), (default_struct), \ + SOFAB_OBJECT_COUNT_EXTENT(field_count, sizeof(*(default_struct))), (nested_count), 0 } /*! * @name Wrapper-array holder marker (@ref sofab_object_descr_t::fixed_seq) @@ -362,7 +397,7 @@ extern "C" { * @param nested_count Number of entries in @p nested_list. */ #define SOFAB_OBJECT_DESCR_SEQ(field_list, field_count, nested_list, nested_count) \ - { (field_list), (nested_list), NULL, (field_count), (nested_count), \ + { (field_list), (nested_list), NULL, (uint16_t)(field_count), (nested_count), \ SOFAB_OBJECT_SEQ_HOLDER } /*! @@ -428,7 +463,7 @@ extern "C" { * @p obj (at offset 0), ahead of the element slots. */ #define SOFAB_OBJECT_DESCR_SEQ_SIZED(field_list, field_count, nested_list, nested_count, obj, lfield) \ - { (field_list), (nested_list), NULL, (field_count), (nested_count), \ + { (field_list), (nested_list), NULL, (uint16_t)(field_count), (nested_count), \ (uint8_t)(SOFAB_OBJECT_SEQ_HOLDER \ | (sizeof(((obj *)0)->lfield) << SOFAB_OBJECT_SEQ_LEN_SHIFT) \ | 0u * sizeof(struct { int length_member_is_too_wide_to_share_fixed_seq_with_the_union_bit \ @@ -542,7 +577,8 @@ extern "C" { #endif /*! @endcond */ #define SOFAB_OBJECT_DESCR_UNION(field_list, field_count, nested_list, nested_count, default_image, obj, tfield) \ - { (field_list), (nested_list), (const void *)(default_image), (field_count), (nested_count), \ + { (field_list), (nested_list), (const void *)(default_image), \ + SOFAB_OBJECT_COUNT_EXTENT(field_count, 0u), (nested_count), \ (uint8_t)(_SOFAB_OBJECT_UNION_BIT \ | 0u * sizeof(struct { int tag_member_must_have_the_width_of_sofab_object_descr_id_t \ : sizeof(((obj *)0)->tfield) == sizeof(sofab_object_descr_id_t) ? 1 : -1; }) \ @@ -570,7 +606,24 @@ typedef struct sofab_object_descr const sofab_object_descr_field_t *const field_list; /*!< Pointer to list of field descriptors */ const struct sofab_object_descr *const *nested_list; /*!< Pointer to list of nested object descriptors */ const void *const default_values; /*!< Pointer to default values for fields (optional, may be NULL) */ - const uint16_t field_count; /*!< Number of fields in the object */ + /*! + * Field count and, for an object that is not a wrapper-array holder, the + * byte extent of @ref default_values -- two things in one 16-bit slot, + * which is what keeps this descriptor 16 bytes wide on a 32-bit target. + * + * - **Holder** (@ref SOFAB_OBJECT_SEQ_HOLDER): all 16 bits are the element + * slot count, so a wrapper array keeps its full capacity (a + * 300-element `array` is 300 slots). A holder never carries a + * default image, so there is no extent to store for it. + * - **Object** (message, struct, union): the low 8 bits are the field count + * -- at most 255, asserted by the descriptor macros -- and the high 8 bits + * are how many bytes of the object @ref default_values describes, counted + * from offset 0. + * + * Read it through @c _field_count() and @c _default_extent(), never + * directly. + */ + const uint16_t count_extent; const uint8_t nested_count; /*!< Number of nested objects */ const uint8_t fixed_seq; /*!< Wrapper-array holder marker: bit 0 (@ref SOFAB_OBJECT_SEQ_HOLDER) flags the holder — reject an unmatched (over-index) element id instead of skipping it, unless §7.3 already skips it on a contradicting wire type/subtype; the bits above @ref SOFAB_OBJECT_SEQ_LEN_SHIFT carry the byte width of the companion element-count member (0 = un-sized, see @ref SOFAB_OBJECT_DESCR_SEQ_SIZED) */ } sofab_object_descr_t; diff --git a/src/object.c b/src/object.c index 4dd9591..3fc264c 100644 --- a/src/object.c +++ b/src/object.c @@ -199,6 +199,59 @@ static sofab_object_descr_id_t _default_tag(const sofab_object_descr_t *info) } #endif +/*! + * @brief Field count of this descriptor. + * + * A wrapper-array HOLDER spends all 16 bits of + * @ref sofab_object_descr_t::count_extent on its element slot count, so a + * 300-element `array` keeps its 300 slots. Every other object spends + * the low byte on the field count (255 max, asserted by the macros) and the + * high byte on the default image's extent, which is what keeps the descriptor + * 16 bytes wide instead of 20. + */ +static size_t _field_count(const sofab_object_descr_t *info) +{ +#if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) + if (_IS_HOLDER(info)) return (size_t)info->count_extent; +#endif + return (size_t)(info->count_extent & 0xFFu); +} + +/*! + * @brief Bytes of the object that @ref sofab_object_descr_t::default_values + * covers, 0 for "the whole object". + * + * Only read where an image exists, and a holder never carries one -- so the + * high byte of a holder's slot count is never mistaken for an extent. + */ +static size_t _default_extent(const sofab_object_descr_t *info) +{ + return (size_t)(info->count_extent >> 8); +} + +/*! + * @brief The default image to compare or seed @p field from, or NULL when the + * field has no declared default. + * + * The image covers @c [0, extent) of the object, so a field at or past the + * extent has no default of its own and the callers fall back to zero -- which is + * exactly what "no declared default" means. That is what lets the image be the + * PREFIX of the object that carries defaults instead of a copy of all of it: a + * message that defaults one byte costs one byte of .rodata. An extent of 0 reads + * as "the whole object", the conservative meaning every descriptor written + * before the extent existed carries, and what a union image uses. + */ +static const void *_field_default(const sofab_object_descr_t *info, + const sofab_object_descr_field_t *field) +{ + size_t extent; + + if (info->default_values == NULL) return NULL; + extent = _default_extent(info); + if (extent != 0 && (size_t)field->offset + (size_t)field->size > extent) return NULL; + return info->default_values; +} + #if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) /*! * @brief Byte width of a wrapper-array holder's element-count member (0 = none). @@ -274,7 +327,7 @@ typedef struct { static size_t _seq_len (const sofab_object_descr_t *info, const void *obj) { uint8_t width = _seq_len_width(info); - size_t n = info->field_count; + size_t n = _field_count(info); if (width != 0) { @@ -312,7 +365,10 @@ static void _seq_len_observe (const sofab_object_descr_t *info, off = _SEQ_LEN_OFFSET; len = (size_t)id + 1u; - if (len > (size_t)info->field_count) len = (size_t)info->field_count; + { + size_t slots = _field_count(info); + if (len > slots) len = slots; + } if ((uint64_t)len > _load_uint(dst + off, width)) { @@ -584,7 +640,9 @@ static int _field_is_default ( return _seq_len(ninfo, nsrc) == 0; } - for (size_t i = 0; i < ninfo->field_count; i++) + const size_t nfields = _field_count(ninfo); + + for (size_t i = 0; i < nfields; i++) { if (!_field_is_default(ninfo, &ninfo->field_list[i], nsrc)) return 0; @@ -593,7 +651,7 @@ static int _field_is_default ( } #endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */ - const void *defaults = info->default_values; + const void *defaults = _field_default(info, field); const void *val = CAST_TO(const void *, src, field->offset); #if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) @@ -688,7 +746,9 @@ extern sofab_ret_t sofab_object_init ( _TAG_W(obj) = _default_tag(info); #endif - for (size_t i = 0; i < info->field_count; i++) + const size_t nfields = _field_count(info); + + for (size_t i = 0; i < nfields; i++) { const sofab_object_descr_field_t *field = &info->field_list[i]; @@ -725,16 +785,20 @@ extern sofab_ret_t sofab_object_init ( else #endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */ { - if (info->default_values != NULL) { - memcpy( - CAST_TO(void *, obj, field->offset), - CAST_TO(const void *, info->default_values, field->offset), - field->size); - } - else - { - memset(CAST_TO(void *, obj, field->offset), 0, field->size); + const void *fdefault = _field_default(info, field); + + if (fdefault != NULL) + { + memcpy( + CAST_TO(void *, obj, field->offset), + CAST_TO(const void *, fdefault, field->offset), + field->size); + } + else + { + memset(CAST_TO(void *, obj, field->offset), 0, field->size); + } } #if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) || !defined(SOFAB_DISABLE_ARRAY_SUPPORT) @@ -747,8 +811,9 @@ extern sofab_ret_t sofab_object_init ( uint8_t width = _sized_width(field); if (width != 0) { - uint64_t dlen = info->default_values != NULL - ? _load_uint(CAST_TO(const void *, info->default_values, + const void *fdefault = _field_default(info, field); + uint64_t dlen = fdefault != NULL + ? _load_uint(CAST_TO(const void *, fdefault, field->offset - width), width) : 0; _store_uint(CAST_TO(void *, obj, field->offset - width), @@ -803,7 +868,7 @@ extern sofab_ret_t sofab_object_encode ( * shortens the value instead of compacting it, and it is gone.) */ #if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) - count = info->field_count; + count = _field_count(info); last = (size_t)-1; if (_IS_HOLDER(info)) { @@ -813,7 +878,7 @@ extern sofab_ret_t sofab_object_encode ( # define _SOFAB_FIELD_COUNT count # define _SOFAB_ELEMENT_HELD(i) ((i) == last) #else -# define _SOFAB_FIELD_COUNT (info->field_count) +# define _SOFAB_FIELD_COUNT (_field_count(info)) # define _SOFAB_ELEMENT_HELD(i) 0 #endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */ @@ -1041,7 +1106,9 @@ extern void sofab_object_field_cb (sofab_istream_t *ctx, sofab_id_t id, size_t s const uint8_t wire_opt = ctx->target_opt; #endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */ - for (size_t i = 0; i < info->field_count; i++) + const size_t nfields = _field_count(info); + + for (size_t i = 0; i < nfields; i++) { const sofab_object_descr_field_t *field = &info->field_list[i]; if (field->id != id) @@ -1282,7 +1349,7 @@ extern void sofab_object_field_cb (sofab_istream_t *ctx, sofab_id_t id, size_t s // in the istream, before or independently of this callback. §7.3 subordinates // the SCHEMA bound only (CORELIB_PLAN §4.8: "the format ceiling still fires on // the count word whatever the subtype turns out to be"). - if (_IS_HOLDER(info) && info->field_count != 0) + if (_IS_HOLDER(info) && _field_count(info) != 0) { /* field_count == 0 above: a holder with no slot has no element type to * contradict, so §7.3 cannot be settled -- skip, never reject. diff --git a/test/c/test_object.c b/test/c/test_object.c index 9f3ded5..c4aa0aa 100644 --- a/test/c/test_object.c +++ b/test/c/test_object.c @@ -4270,6 +4270,181 @@ static void test_object_encode_array_len_clamped (void) TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, buf, used); } + +/* ---- the default image may be a PREFIX of the object ------------------- */ + +/* + * SOFAB_OBJECT_DESCR_WITH_DEFAULTS used to require "a fully-populated object": + * every read addresses the image at the field's own offset, so the only buffer a + * generator could hand over was a copy of the whole struct -- 824 bytes of + * .rodata on one real schema to carry 16 bytes of actual defaults. + * + * It now records sizeof(*image) as the extent, so the image may stop after the + * members that have defaults. A field at or past the extent has no declared + * default and is compared against (and seeded with) zero, which is what "no + * declared default" has always meant. + */ +typedef struct +{ + uint32_t seeded; /* has a default, first in the struct on purpose */ + uint32_t plain; /* no default, past the image */ +} prefix_obj_t; + +/* + * The image buffer deliberately holds MORE than the extent describes: `poison` + * sits exactly where `plain` would be read from. An implementation that ignored + * the extent would seed `plain` with it, so the boundary is observable in a + * value rather than only in a sanitizer -- which is the difference between a + * test that fails and a test that happens to pass on zeroed .rodata. + */ +typedef struct +{ + uint32_t seeded; /* the prefix that carries defaults */ + uint32_t poison; /* past the extent: must never reach the object */ +} prefix_img_t; + +static const sofab_object_descr_field_t _prefix_fields[] = +{ + SOFAB_OBJECT_FIELD(0, prefix_obj_t, seeded, SOFAB_OBJECT_FIELDTYPE_UNSIGNED), + SOFAB_OBJECT_FIELD(1, prefix_obj_t, plain, SOFAB_OBJECT_FIELDTYPE_UNSIGNED), +}; + +static const prefix_img_t _prefix_img = { .seeded = 4242, .poison = 0xDEADBEEFu }; + +/* Hand-built so the extent can be smaller than the buffer: the macro derives it + * from sizeof(*image), which is what a generator wants (its image stops where + * the defaults stop) but cannot express "describe only the first 4 bytes". */ +static const sofab_object_descr_t _prefix_descr = +{ + _prefix_fields, NULL, &_prefix_img, + SOFAB_OBJECT_COUNT_EXTENT(2, sizeof(uint32_t)), 0, 0 +}; + +/* ...and the same thing through the macro, where the image IS its own extent. */ +typedef struct { uint32_t seeded; } prefix_exact_img_t; + +static const prefix_exact_img_t _prefix_exact_img = { .seeded = 4242 }; + +static const sofab_object_descr_t _prefix_exact_descr = + SOFAB_OBJECT_DESCR_WITH_DEFAULTS(_prefix_fields, 2, NULL, 0, &_prefix_exact_img); + +/* The same two fields, but the image is a whole object and the extent is 0 -- + * what every descriptor written before the extent existed looks like, and what a + * union image still uses. Hand-built: the macro would fill the extent in. */ +static const prefix_obj_t _whole_img = { .seeded = 4242, .plain = 77 }; + +static const sofab_object_descr_t _whole_descr = +{ + _prefix_fields, NULL, &_whole_img, 2 /* count, extent 0 */, 0, 0 +}; + +static void test_object_default_image_may_be_a_prefix (void) +{ + prefix_obj_t o; + + memset(&o, 0xAA, sizeof o); + sofab_object_init(&_prefix_descr, &o); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(4242, o.seeded, + "a field inside the image is seeded from it"); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(0, o.plain, + "a field past the extent has no declared default: zero, never the " + "image bytes that happen to sit at its offset"); + + /* the macro form: the image stops where the defaults stop */ + memset(&o, 0xAA, sizeof o); + sofab_object_init(&_prefix_exact_descr, &o); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(4242, o.seeded, "seeded through the macro form"); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(0, o.plain, + "and the field past sizeof(*image) is zero"); + + /* The omission test follows the same line: the seeded field is at its + * default, and the field past the image is at its default when it is 0. */ + uint8_t buf[32]; + sofab_ostream_t os; + size_t used; + + sofab_ostream_init(&os, buf, sizeof buf, 0, NULL, NULL); + TEST_ASSERT_EQUAL(SOFAB_RET_OK, sofab_object_encode(&os, &_prefix_descr, &o)); + used = sofab_ostream_flush(&os); + TEST_ASSERT_EQUAL_size_t_MESSAGE(0, used, + "a fresh object is all-default and encodes to nothing"); + + o.plain = 1; + sofab_ostream_init(&os, buf, sizeof buf, 0, NULL, NULL); + TEST_ASSERT_EQUAL(SOFAB_RET_OK, sofab_object_encode(&os, &_prefix_descr, &o)); + used = sofab_ostream_flush(&os); + TEST_ASSERT_EQUAL_size_t_MESSAGE(2, used, + "the field past the image is written once it differs from zero"); + + o.plain = 0; + o.seeded = 0; + sofab_ostream_init(&os, buf, sizeof buf, 0, NULL, NULL); + TEST_ASSERT_EQUAL(SOFAB_RET_OK, sofab_object_encode(&os, &_prefix_descr, &o)); + used = sofab_ostream_flush(&os); + TEST_ASSERT_EQUAL_size_t_MESSAGE(2, used, + "and the seeded field is written once it differs from ITS default"); +} + +static void test_object_extent_zero_means_the_whole_object (void) +{ + prefix_obj_t o; + + memset(&o, 0xAA, sizeof o); + sofab_object_init(&_whole_descr, &o); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(4242, o.seeded, "seeded from the image"); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(77, o.plain, + "extent 0 reads as 'the whole object', so this field is seeded too"); +} + +/* + * A wrapper-array holder spends all 16 bits of count_extent on its slot count, + * so an `array` keeps its 300 slots -- the packing that buys + * the extent must not cap an array's capacity at 255. The slots are expanded by + * macro: 300 field descriptors say the same thing in 300 lines. + */ +enum { BIG_SLOTS = 300 }; + +typedef struct { uint16_t len; uint8_t items[BIG_SLOTS]; } big_holder_t; + +#define _SLOT1(i) SOFAB_OBJECT_FIELD(i, big_holder_t, items[i], SOFAB_OBJECT_FIELDTYPE_UNSIGNED), +#define _SLOT10(b) _SLOT1(b+0) _SLOT1(b+1) _SLOT1(b+2) _SLOT1(b+3) _SLOT1(b+4) \ + _SLOT1(b+5) _SLOT1(b+6) _SLOT1(b+7) _SLOT1(b+8) _SLOT1(b+9) +#define _SLOT100(b) _SLOT10(b+0) _SLOT10(b+10) _SLOT10(b+20) _SLOT10(b+30) _SLOT10(b+40) \ + _SLOT10(b+50) _SLOT10(b+60) _SLOT10(b+70) _SLOT10(b+80) _SLOT10(b+90) + +static const sofab_object_descr_field_t _big_holder_fields[] = +{ + _SLOT100(0) _SLOT100(100) _SLOT100(200) +}; + +static const sofab_object_descr_t _big_holder_descr = + SOFAB_OBJECT_DESCR_SEQ_SIZED(_big_holder_fields, BIG_SLOTS, NULL, 0, big_holder_t, len); + +static void test_object_holder_keeps_a_16_bit_slot_count (void) +{ + static big_holder_t holder; + uint8_t buf[2048]; + sofab_ostream_t os; + size_t used; + + TEST_ASSERT_EQUAL_size_t_MESSAGE(BIG_SLOTS, + sizeof _big_holder_fields / sizeof _big_holder_fields[0], + "the macro expansion must produce one field per slot"); + + memset(&holder, 0, sizeof holder); + holder.len = BIG_SLOTS; + holder.items[BIG_SLOTS - 1] = 7; /* the LAST slot, past any 8-bit count */ + + sofab_ostream_init(&os, buf, sizeof buf, 0, NULL, NULL); + TEST_ASSERT_EQUAL(SOFAB_RET_OK, sofab_object_encode(&os, &_big_holder_descr, &holder)); + used = sofab_ostream_flush(&os); + TEST_ASSERT_TRUE_MESSAGE(used > 0, + "a 300-slot holder must reach its last element; a byte-wide slot count " + "would have stopped at 255 and written nothing"); + TEST_ASSERT_EQUAL_HEX8_MESSAGE(7, buf[used - 1], + "and the value written last is the one in the last slot"); +} + int test_object_main (void) { UNITY_BEGIN(); @@ -4384,5 +4559,9 @@ int test_object_main (void) RUN_TEST(test_object_encode_blob_len_clamped); RUN_TEST(test_object_encode_array_len_clamped); + RUN_TEST(test_object_default_image_may_be_a_prefix); + RUN_TEST(test_object_extent_zero_means_the_whole_object); + RUN_TEST(test_object_holder_keeps_a_16_bit_slot_count); + return UNITY_END(); } From bfd624dc6f4c2622402ead45b83d849a266d4810 Mon Sep 17 00:00:00 2001 From: Andste82 Date: Tue, 13 Oct 2026 09:09:42 +0000 Subject: [PATCH 2/5] fix(object): refuse a default image the extent cannot describe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SOFAB_OBJECT_COUNT_EXTENT clamped an image longer than 255 bytes to extent 0 -- and 0 reads as "the image covers the whole object". That is the conservative reading for a whole-object image and the wrong one for a PREFIX: every field past the prefix is then seeded and compared through the image, reading past its end. A generator-emitted message defaulting a `string` or an `array` lands there, and nothing says so -- under MEDIUM and BIG the walk reads .rodata it does not own (AddressSanitizer: global-buffer-overflow in sofab_object_init); SMALL is immune only because the field behind such a prefix has an offset past its own ceiling and is already a build error. So the clamp is gone and the macros name every ceiling they cannot describe, the way SOFAB_OBJECT_ASSERT_FITS_PROFILE already does for an offset: - SOFAB_OBJECT_ASSERT_EXTENT refuses an image over SOFAB_OBJECT_DESCR_EXTENT_MAX. An image that really is a copy of the whole object says so with the new SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS, which records extent 0 on purpose -- the form every descriptor written before the extent existed already has, and the only form left for an image the extent cannot reach. - SOFAB_OBJECT_ASSERT_SLOT_COUNT replaces the (uint16_t) cast that SOFAB_OBJECT_DESCR_SEQ / _SEQ_SIZED had grown around their slot count. The cast silenced -Woverflow, which was the only thing standing between a 70000-slot holder under BIG and a descriptor that silently walks 4464 of them and rejects the rest as over-index elements (MESSAGE_SPEC §7.1). - SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX / _EXTENT_MAX / _SLOT_COUNT_MAX are exported so generated code can hold a schema against them at compile time the way it already does with SOFAB_ID_MAX, instead of discovering the ceiling as an assertion failure inside this header. object.c reads its mask and shift from the same two macros. All of it is compile-time: .text and .rodata are byte-identical on ARMv6-m, ARMv7-m+fp.dp, RV32IMC and atmega8. The extent-0 test now also builds its descriptor through WITH_WHOLE_DEFAULTS and pins its count word against the hand-rolled legacy one, so the macro cannot drift from the shape it stands for. Co-Authored-By: Claude Opus 5 (1M context) --- src/include/sofab/object.h | 119 ++++++++++++++++++++++++++++++++----- src/object.c | 16 ++--- test/c/test_object.c | 22 ++++++- 3 files changed, 133 insertions(+), 24 deletions(-) diff --git a/src/include/sofab/object.h b/src/include/sofab/object.h index 1fca94e..6368d81 100644 --- a/src/include/sofab/object.h +++ b/src/include/sofab/object.h @@ -278,30 +278,84 @@ extern "C" { + SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)), \ type, (sizeof(((obj *)0)->dfield[0]) & 0xF) } +/*! + * @name Count-word ceilings (@ref sofab_object_descr_t::count_extent) + * + * What the 16-bit count word can describe. Generated code can hold a schema + * against these at compile time the way it holds a field id against + * @ref SOFAB_ID_MAX, instead of discovering the ceiling as an assertion failure + * inside this header -- which is why they are macros and not just prose. + * @{ + */ +/*! @brief Fields an object (message, struct, union) may declare. */ +#define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX 255u +/*! @brief Bytes a default image may describe (@ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS). */ +#define SOFAB_OBJECT_DESCR_EXTENT_MAX 255u +/*! @brief Element slots a wrapper-array holder may declare (@ref SOFAB_OBJECT_DESCR_SEQ). */ +#define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX 65535u +/*! @brief Position of the extent inside the count word. */ +#define SOFAB_OBJECT_DESCR_EXTENT_SHIFT 8 +/*! @} */ + /*! * @brief Compile-time limit on an OBJECT's field count (message, struct, union). * - * The low byte of @ref sofab_object_descr_t::count_extent holds it, so 255 is - * the ceiling and a 256-field object fails to compile here instead of wrapping - * silently. A wrapper-array HOLDER is not affected: its slot count uses all 16 - * bits (see @ref SOFAB_OBJECT_DESCR_SEQ_SIZED). + * The low byte of @ref sofab_object_descr_t::count_extent holds it, so + * @ref SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX is the ceiling and a 256-field object + * fails to compile here instead of wrapping silently. A wrapper-array HOLDER is + * not affected: its slot count uses all 16 bits + * (@ref SOFAB_OBJECT_ASSERT_SLOT_COUNT). */ #define SOFAB_OBJECT_ASSERT_FIELD_COUNT(n) \ (0u * sizeof(struct { int an_object_may_not_declare_more_than_255_fields \ - : ((n) <= 255) ? 1 : -1; })) + : ((n) <= SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) ? 1 : -1; })) + +/*! + * @brief Compile-time limit on a default image's byte extent. + * + * The high byte of @ref sofab_object_descr_t::count_extent holds it, and an + * extent of 0 reads as "the image covers the whole object" -- so an image longer + * than @ref SOFAB_OBJECT_DESCR_EXTENT_MAX cannot be described as the PREFIX it + * is. Clamping such an image to 0 would not be the conservative reading it looks + * like: every field past the prefix would then be seeded and compared through the + * image, reading past its end. So it is a build error here, and an image that + * really is a copy of the whole object says so with + * @ref SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS. + */ +#define SOFAB_OBJECT_ASSERT_EXTENT(n) \ + (0u * sizeof(struct { int a_default_image_over_255_bytes_must_use_WITH_WHOLE_DEFAULTS \ + : ((n) <= SOFAB_OBJECT_DESCR_EXTENT_MAX) ? 1 : -1; })) + +/*! + * @brief Compile-time limit on a wrapper-array holder's element slot count. + * + * A holder spends all 16 bits of @ref sofab_object_descr_t::count_extent on its + * slot count, so @ref SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX is the ceiling. Named + * here rather than left to the narrowing in the initialiser: @c -Woverflow + * reports that only where it is enabled, and a holder whose count wrapped walks + * a fraction of its slots and rejects every element id above them as an + * over-index (MESSAGE_SPEC §7.1) -- a silent change of meaning, not a build + * failure. + */ +#define SOFAB_OBJECT_ASSERT_SLOT_COUNT(n) \ + (0u * sizeof(struct { int a_wrapper_array_may_not_declare_more_than_65535_slots \ + : ((n) <= SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX) ? 1 : -1; })) /*! * @brief The @ref sofab_object_descr_t::count_extent word of an object: the * field count in the low byte, the default image's byte extent in the * high byte. * - * An image longer than 255 bytes stores an extent of 0, which reads as "the - * image covers the whole object" -- the conservative meaning, and the one every - * descriptor written before the extent existed carries. + * An extent of 0 reads as "the image covers the whole object": what every + * descriptor written before the extent existed carries, what a union image uses, + * and what @ref SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS states on purpose. An + * image too long for the high byte is therefore refused rather than clamped to + * 0 (@ref SOFAB_OBJECT_ASSERT_EXTENT). */ #define SOFAB_OBJECT_COUNT_EXTENT(field_count, extent) \ ((uint16_t)(((field_count) + SOFAB_OBJECT_ASSERT_FIELD_COUNT(field_count)) \ - | (uint16_t)(((extent) <= 255u ? (extent) : 0u) << 8))) + | (uint16_t)(((extent) + SOFAB_OBJECT_ASSERT_EXTENT(extent)) \ + << SOFAB_OBJECT_DESCR_EXTENT_SHIFT))) /*! * @brief Build an object descriptor (@ref sofab_object_descr_t) without defaults. @@ -337,12 +391,41 @@ extern "C" { * -- which is what "no declared default" means. A prefix is * what the SofaBuffers generator emits: a message that * defaults one byte then costs one byte of @c .rodata - * instead of a copy of the whole object. + * instead of a copy of the whole object. At most + * @ref SOFAB_OBJECT_DESCR_EXTENT_MAX bytes: a longer image + * is a build error, because the extent has nowhere to say + * how far it reaches -- use + * @ref SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS for an image + * that is a copy of the whole object. */ #define SOFAB_OBJECT_DESCR_WITH_DEFAULTS(field_list, field_count,nested_list, nested_count, default_struct) \ { (field_list), (nested_list), (default_struct), \ SOFAB_OBJECT_COUNT_EXTENT(field_count, sizeof(*(default_struct))), (nested_count), 0 } +/*! + * @brief Build an object descriptor whose default image covers the WHOLE object. + * + * As @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS, but the image is a copy of the + * entire object instead of a prefix of it, and the descriptor says so by + * recording an extent of 0. Two callers want that: an image longer than + * @ref SOFAB_OBJECT_DESCR_EXTENT_MAX, which no prefix extent can describe, and + * code generated before the extent existed -- an omitted initialiser is 0, so it + * already reads this way. + * + * @warning @p default_struct must be as long as the object. Every field is + * seeded and compared through it, with no extent to stop the walk, so an image + * shorter than @c sizeof the object is read past its end. + * + * @param field_list Array of @ref sofab_object_descr_field_t for this object. + * @param field_count Number of entries in @p field_list. + * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL). + * @param nested_count Number of entries in @p nested_list. + * @param default_struct Pointer to a fully-populated object holding the field defaults. + */ +#define SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS(field_list, field_count, nested_list, nested_count, default_struct) \ + { (field_list), (nested_list), (default_struct), \ + SOFAB_OBJECT_COUNT_EXTENT(field_count, 0u), (nested_count), 0 } + /*! * @name Wrapper-array holder marker (@ref sofab_object_descr_t::fixed_seq) * @@ -397,8 +480,9 @@ extern "C" { * @param nested_count Number of entries in @p nested_list. */ #define SOFAB_OBJECT_DESCR_SEQ(field_list, field_count, nested_list, nested_count) \ - { (field_list), (nested_list), NULL, (uint16_t)(field_count), (nested_count), \ - SOFAB_OBJECT_SEQ_HOLDER } + { (field_list), (nested_list), NULL, \ + (uint16_t)((field_count) + SOFAB_OBJECT_ASSERT_SLOT_COUNT(field_count)), \ + (nested_count), SOFAB_OBJECT_SEQ_HOLDER } /*! * @brief Build a length-carrying (sized) sequence-holder object descriptor. @@ -463,7 +547,8 @@ extern "C" { * @p obj (at offset 0), ahead of the element slots. */ #define SOFAB_OBJECT_DESCR_SEQ_SIZED(field_list, field_count, nested_list, nested_count, obj, lfield) \ - { (field_list), (nested_list), NULL, (uint16_t)(field_count), (nested_count), \ + { (field_list), (nested_list), NULL, \ + (uint16_t)((field_count) + SOFAB_OBJECT_ASSERT_SLOT_COUNT(field_count)), (nested_count), \ (uint8_t)(SOFAB_OBJECT_SEQ_HOLDER \ | (sizeof(((obj *)0)->lfield) << SOFAB_OBJECT_SEQ_LEN_SHIFT) \ | 0u * sizeof(struct { int length_member_is_too_wide_to_share_fixed_seq_with_the_union_bit \ @@ -616,9 +701,11 @@ typedef struct sofab_object_descr * 300-element `array` is 300 slots). A holder never carries a * default image, so there is no extent to store for it. * - **Object** (message, struct, union): the low 8 bits are the field count - * -- at most 255, asserted by the descriptor macros -- and the high 8 bits - * are how many bytes of the object @ref default_values describes, counted - * from offset 0. + * (@ref SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) and the high 8 bits are how + * many bytes of the object @ref default_values describes, counted from + * offset 0 (@ref SOFAB_OBJECT_DESCR_EXTENT_MAX, 0 = the whole object). + * Both ceilings are asserted by the descriptor macros, as is a holder's + * slot count (@ref SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX). * * Read it through @c _field_count() and @c _default_extent(), never * directly. diff --git a/src/object.c b/src/object.c index 3fc264c..f35cd50 100644 --- a/src/object.c +++ b/src/object.c @@ -205,16 +205,16 @@ static sofab_object_descr_id_t _default_tag(const sofab_object_descr_t *info) * A wrapper-array HOLDER spends all 16 bits of * @ref sofab_object_descr_t::count_extent on its element slot count, so a * 300-element `array` keeps its 300 slots. Every other object spends - * the low byte on the field count (255 max, asserted by the macros) and the - * high byte on the default image's extent, which is what keeps the descriptor - * 16 bytes wide instead of 20. + * the low byte on the field count (@ref SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX, + * asserted by the macros) and the high byte on the default image's extent, which + * is what keeps the descriptor 16 bytes wide instead of 20. */ static size_t _field_count(const sofab_object_descr_t *info) { #if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) if (_IS_HOLDER(info)) return (size_t)info->count_extent; #endif - return (size_t)(info->count_extent & 0xFFu); + return (size_t)(info->count_extent & SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX); } /*! @@ -226,7 +226,7 @@ static size_t _field_count(const sofab_object_descr_t *info) */ static size_t _default_extent(const sofab_object_descr_t *info) { - return (size_t)(info->count_extent >> 8); + return (size_t)(info->count_extent >> SOFAB_OBJECT_DESCR_EXTENT_SHIFT); } /*! @@ -238,8 +238,10 @@ static size_t _default_extent(const sofab_object_descr_t *info) * exactly what "no declared default" means. That is what lets the image be the * PREFIX of the object that carries defaults instead of a copy of all of it: a * message that defaults one byte costs one byte of .rodata. An extent of 0 reads - * as "the whole object", the conservative meaning every descriptor written - * before the extent existed carries, and what a union image uses. + * as "the whole object": what every descriptor written before the extent existed + * carries, what a union image uses, and what SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS + * states on purpose -- never a clamped prefix, which the macros refuse + * (SOFAB_OBJECT_ASSERT_EXTENT) because it would read past the image. */ static const void *_field_default(const sofab_object_descr_t *info, const sofab_object_descr_field_t *field) diff --git a/test/c/test_object.c b/test/c/test_object.c index c4aa0aa..08622a9 100644 --- a/test/c/test_object.c +++ b/test/c/test_object.c @@ -4330,7 +4330,11 @@ static const sofab_object_descr_t _prefix_exact_descr = /* The same two fields, but the image is a whole object and the extent is 0 -- * what every descriptor written before the extent existed looks like, and what a - * union image still uses. Hand-built: the macro would fill the extent in. */ + * union image still uses. Built twice on purpose: hand-rolled the way an old + * descriptor looks (an omitted initialiser is 0), and through the macro that + * states it, which is the only form left for an image the extent cannot + * describe -- one over SOFAB_OBJECT_DESCR_EXTENT_MAX is a build error now, not a + * silently clamped prefix. The two must read identically. */ static const prefix_obj_t _whole_img = { .seeded = 4242, .plain = 77 }; static const sofab_object_descr_t _whole_descr = @@ -4338,6 +4342,9 @@ static const sofab_object_descr_t _whole_descr = _prefix_fields, NULL, &_whole_img, 2 /* count, extent 0 */, 0, 0 }; +static const sofab_object_descr_t _whole_macro_descr = + SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS(_prefix_fields, 2, NULL, 0, &_whole_img); + static void test_object_default_image_may_be_a_prefix (void) { prefix_obj_t o; @@ -4394,6 +4401,19 @@ static void test_object_extent_zero_means_the_whole_object (void) TEST_ASSERT_EQUAL_UINT32_MESSAGE(4242, o.seeded, "seeded from the image"); TEST_ASSERT_EQUAL_UINT32_MESSAGE(77, o.plain, "extent 0 reads as 'the whole object', so this field is seeded too"); + + /* ...and the macro that states it reads the same, member for member: the + * extent is the only thing it sets, so a difference could only be a wrong + * extent. */ + TEST_ASSERT_EQUAL_UINT16_MESSAGE(_whole_descr.count_extent, + _whole_macro_descr.count_extent, + "WITH_WHOLE_DEFAULTS must build the same count word as an old descriptor"); + + memset(&o, 0xAA, sizeof o); + sofab_object_init(&_whole_macro_descr, &o); + TEST_ASSERT_EQUAL_UINT32(4242, o.seeded); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(77, o.plain, + "the whole image is read through the macro form too"); } /* From e7ea976acd5666dc37e386cdd75708e0e7c67812 Mon Sep 17 00:00:00 2001 From: Andste82 Date: Tue, 13 Oct 2026 09:10:22 +0000 Subject: [PATCH 3/5] perf(object): one default-image lookup per field in sofab_object_init The init walk asked _field_default twice for every field -- once for the value, once for a sized buffer's companion length -- and at -Os neither compiler folded the second call away, although the function is pure and both calls are in the same block. Asking once: | target | .text before | after | |---------------|-------------:|------:| | ARMv6-m | 4 104 | 4 066 | | ARMv7-m+fp.dp | 4 064 | 4 046 | | RV32IMC | 5 264 | 5 236 | | atmega8 | 7 664 | 7 702 | atmega8 is the one that pays: the pointer now lives across the memcpy, whose clobbers spill it, where two short live ranges needed no stack at all. 38 B on an 8-bit target against 38/18/28 B on the three 32-bit ones is the trade this commit makes, and it is its own commit so it can be dropped if an AVR budget disagrees. Two other shapes measured worse everywhere and are not here: taking the companion length first (ARMv6-m 4 070, atmega8 7 718), which would also have matched the order _field_is_default reads the two in; and hoisting the image pointer and its extent out of the field loop as the object-invariants they are, with a three-argument core (ARMv6-m 4 086, atmega8 7 712) -- the per-field compare that remains is not what the loop was spending. Whole C suite green, and green again under -fsanitize=address,undefined. Co-Authored-By: Claude Opus 5 (1M context) --- src/object.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/object.c b/src/object.c index f35cd50..c463964 100644 --- a/src/object.c +++ b/src/object.c @@ -787,20 +787,21 @@ extern sofab_ret_t sofab_object_init ( else #endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */ { - { - const void *fdefault = _field_default(info, field); + /* One lookup for both halves of the field: the value and, below, a + * sized buffer's companion length. _field_default is pure, and at -Os + * the second call was not folded -- 38 B of .text for asking twice. */ + const void *fdefault = _field_default(info, field); - if (fdefault != NULL) - { - memcpy( - CAST_TO(void *, obj, field->offset), - CAST_TO(const void *, fdefault, field->offset), - field->size); - } - else - { - memset(CAST_TO(void *, obj, field->offset), 0, field->size); - } + if (fdefault != NULL) + { + memcpy( + CAST_TO(void *, obj, field->offset), + CAST_TO(const void *, fdefault, field->offset), + field->size); + } + else + { + memset(CAST_TO(void *, obj, field->offset), 0, field->size); } #if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) || !defined(SOFAB_DISABLE_ARRAY_SUPPORT) @@ -813,7 +814,6 @@ extern sofab_ret_t sofab_object_init ( uint8_t width = _sized_width(field); if (width != 0) { - const void *fdefault = _field_default(info, field); uint64_t dlen = fdefault != NULL ? _load_uint(CAST_TO(const void *, fdefault, field->offset - width), width) From 8e90b10873bf2b2ad3dda9e65d2b3a9759a10282 Mon Sep 17 00:00:00 2001 From: Andste82 Date: Tue, 13 Oct 2026 17:27:14 +0000 Subject: [PATCH 4/5] fix(object): a whole-object image of any size keeps building; a prefix says so bfd624d made SOFAB_OBJECT_DESCR_WITH_DEFAULTS refuse an image over 255 bytes, to stop a PREFIX that long from being clamped to extent 0 and read past its end. The macro cannot tell a prefix from a whole object, though, and every file the generator emits today hands over the WHOLE object: its own realworld example (vehicle_telemetry, 824 B) stopped compiling against this branch, while it builds against main -- the opposite of the "old generated code keeps working" this PR promises, and a broken lang-c job in sofa-buffers/generator the moment this merges. So the two images get two spellings: - SOFAB_OBJECT_DESCR_WITH_DEFAULTS keeps its contract: a fully-populated object, of any size. It records sizeof(*image), or 0 when that is past the extent byte -- for a whole image the two mean the same. - SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS is the prefix form. It records sizeof(*prefix) and refuses one over SOFAB_OBJECT_DESCR_EXTENT_MAX (a_prefix_default_image_may_not_exceed_255_bytes), because no extent value says how far such a prefix reaches. WITH_WHOLE_DEFAULTS goes again: WITH_DEFAULTS already is that macro. SOFAB_OBJECT_COUNT_EXTENT becomes SOFAB_OBJECT_DESCR_COUNT_WORD on purpose. Generator PR #743 still emits its prefix images through WITH_DEFAULTS and guards on `defined(SOFAB_OBJECT_COUNT_EXTENT)`; paired with this corelib that output would read a prefix over 255 B as the whole object (ASan: global-buffer-overflow in sofab_object_init, reproduced). With the old name gone, its own guard stops the build instead, until it emits WITH_PREFIX_DEFAULTS and guards on that. Verified against generated code, not hand-written descriptors -- current generator main, #743 as it stands, and #743 switched to the prefix macro, against this branch under SMALL/MEDIUM/BIG: - generator main, realworld: builds, seeds, round-trips; wire identical to the prefix build (10 B probe), clean under ASan+UBSan - #743 as it stands: refused by its own guard, never silently paired - #743 + WITH_PREFIX_DEFAULTS: builds; a 256 B prefix is the named error test_object_whole_image_longer_than_the_extent_byte pins the case that broke: a whole image of 304 B whose last member sits at offset 300. .text is byte-identical to the previous commit on all four targets. Co-Authored-By: Claude Opus 5.5 --- src/include/sofab/object.h | 106 +++++++++++++++++++------------------ src/object.c | 7 +-- test/c/test_object.c | 73 +++++++++++++++++-------- 3 files changed, 110 insertions(+), 76 deletions(-) diff --git a/src/include/sofab/object.h b/src/include/sofab/object.h index 6368d81..4ca257c 100644 --- a/src/include/sofab/object.h +++ b/src/include/sofab/object.h @@ -289,7 +289,7 @@ extern "C" { */ /*! @brief Fields an object (message, struct, union) may declare. */ #define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX 255u -/*! @brief Bytes a default image may describe (@ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS). */ +/*! @brief Bytes a PREFIX default image may describe (@ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS). */ #define SOFAB_OBJECT_DESCR_EXTENT_MAX 255u /*! @brief Element slots a wrapper-array holder may declare (@ref SOFAB_OBJECT_DESCR_SEQ). */ #define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX 65535u @@ -311,19 +311,18 @@ extern "C" { : ((n) <= SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) ? 1 : -1; })) /*! - * @brief Compile-time limit on a default image's byte extent. - * - * The high byte of @ref sofab_object_descr_t::count_extent holds it, and an - * extent of 0 reads as "the image covers the whole object" -- so an image longer - * than @ref SOFAB_OBJECT_DESCR_EXTENT_MAX cannot be described as the PREFIX it - * is. Clamping such an image to 0 would not be the conservative reading it looks - * like: every field past the prefix would then be seeded and compared through the - * image, reading past its end. So it is a build error here, and an image that - * really is a copy of the whole object says so with - * @ref SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS. + * @brief Compile-time limit on a PREFIX default image's byte extent. + * + * The high byte of @ref sofab_object_descr_t::count_extent holds the extent, and + * an extent of 0 reads as "the image covers the whole object". A prefix longer + * than @ref SOFAB_OBJECT_DESCR_EXTENT_MAX therefore has no extent that describes + * it: recorded as 0, every field past the prefix would be seeded and compared + * through the image, reading past its end. @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS + * refuses it here instead. A whole-object image of any length is not affected -- + * @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS records 0 for it, which is exactly right. */ #define SOFAB_OBJECT_ASSERT_EXTENT(n) \ - (0u * sizeof(struct { int a_default_image_over_255_bytes_must_use_WITH_WHOLE_DEFAULTS \ + (0u * sizeof(struct { int a_prefix_default_image_may_not_exceed_255_bytes \ : ((n) <= SOFAB_OBJECT_DESCR_EXTENT_MAX) ? 1 : -1; })) /*! @@ -348,13 +347,19 @@ extern "C" { * * An extent of 0 reads as "the image covers the whole object": what every * descriptor written before the extent existed carries, what a union image uses, - * and what @ref SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS states on purpose. An - * image too long for the high byte is therefore refused rather than clamped to - * 0 (@ref SOFAB_OBJECT_ASSERT_EXTENT). + * and what an image too long for the high byte is recorded as. That last one is + * right for a whole-object image -- the form every generated file before the + * extent hands over, at any size -- and wrong for a prefix, which is why a + * prefix goes through @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS and is + * refused there (@ref SOFAB_OBJECT_ASSERT_EXTENT) rather than clamped here. + * + * Generated code tests for @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS, the + * macro it calls, to tell a corelib that reads prefix images from one that does + * not -- never for this helper. */ -#define SOFAB_OBJECT_COUNT_EXTENT(field_count, extent) \ +#define SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, extent) \ ((uint16_t)(((field_count) + SOFAB_OBJECT_ASSERT_FIELD_COUNT(field_count)) \ - | (uint16_t)(((extent) + SOFAB_OBJECT_ASSERT_EXTENT(extent)) \ + | (uint16_t)(((extent) <= SOFAB_OBJECT_DESCR_EXTENT_MAX ? (extent) : 0u) \ << SOFAB_OBJECT_DESCR_EXTENT_SHIFT))) /*! @@ -369,7 +374,7 @@ extern "C" { * @param nested_count Number of entries in @p nested_list. */ #define SOFAB_OBJECT_DESCR(field_list, field_count, nested_list, nested_count) \ - { (field_list), (nested_list), NULL, SOFAB_OBJECT_COUNT_EXTENT(field_count, 0u), (nested_count), 0 } + { (field_list), (nested_list), NULL, SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), 0 } /*! * @brief Build an object descriptor with a default-values reference. @@ -382,49 +387,48 @@ extern "C" { * @param field_count Number of entries in @p field_list. * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL). * @param nested_count Number of entries in @p nested_list. - * @param default_struct Pointer to an object holding the field defaults. It need - * NOT be a whole object: any PREFIX of one will do, as long - * as every field it describes sits at the same offset as in - * the object itself, because that is how the fields are - * addressed. @c sizeof(*default_struct) is recorded as the - * extent and a field at or past it is compared against zero - * -- which is what "no declared default" means. A prefix is - * what the SofaBuffers generator emits: a message that - * defaults one byte then costs one byte of @c .rodata - * instead of a copy of the whole object. At most - * @ref SOFAB_OBJECT_DESCR_EXTENT_MAX bytes: a longer image - * is a build error, because the extent has nowhere to say - * how far it reaches -- use - * @ref SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS for an image - * that is a copy of the whole object. + * @param default_struct Pointer to a fully-populated object holding the field + * defaults. @c sizeof(*default_struct) is recorded as the + * extent, or 0 ("the whole object") when it is longer than + * @ref SOFAB_OBJECT_DESCR_EXTENT_MAX -- both mean the same + * for a whole-object image, so this form takes one of any + * size, as it always has. A PREFIX of the object goes + * through @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS: here, + * one longer than the extent byte would be read as the whole + * object and past its own end. */ #define SOFAB_OBJECT_DESCR_WITH_DEFAULTS(field_list, field_count,nested_list, nested_count, default_struct) \ { (field_list), (nested_list), (default_struct), \ - SOFAB_OBJECT_COUNT_EXTENT(field_count, sizeof(*(default_struct))), (nested_count), 0 } + SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_struct))), (nested_count), 0 } /*! - * @brief Build an object descriptor whose default image covers the WHOLE object. - * - * As @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS, but the image is a copy of the - * entire object instead of a prefix of it, and the descriptor says so by - * recording an extent of 0. Two callers want that: an image longer than - * @ref SOFAB_OBJECT_DESCR_EXTENT_MAX, which no prefix extent can describe, and - * code generated before the extent existed -- an omitted initialiser is 0, so it - * already reads this way. - * - * @warning @p default_struct must be as long as the object. Every field is - * seeded and compared through it, with no extent to stop the walk, so an image - * shorter than @c sizeof the object is read past its end. + * @brief Build an object descriptor whose default image is a PREFIX of the object. + * + * As @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS, but @p default_prefix need not be a + * whole object: any prefix of one will do, as long as every field it describes + * sits at the same offset as in the object itself, because that is how the + * fields are addressed. @c sizeof(*default_prefix) is recorded as the extent, + * and a field at or past it is seeded with and compared against zero -- which is + * what "no declared default" means. This is what the SofaBuffers generator emits + * (the defaulted members first): a message that defaults one byte costs one byte + * of @c .rodata instead of a copy of the whole object. + * + * The prefix may be at most @ref SOFAB_OBJECT_DESCR_EXTENT_MAX bytes; a longer + * one is a build error (@ref SOFAB_OBJECT_ASSERT_EXTENT), because the extent has + * no value that says how far it reaches. Hand over the whole object through + * @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS instead. * * @param field_list Array of @ref sofab_object_descr_field_t for this object. * @param field_count Number of entries in @p field_list. * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL). * @param nested_count Number of entries in @p nested_list. - * @param default_struct Pointer to a fully-populated object holding the field defaults. + * @param default_prefix Pointer to a prefix of the object holding the field defaults. */ -#define SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS(field_list, field_count, nested_list, nested_count, default_struct) \ - { (field_list), (nested_list), (default_struct), \ - SOFAB_OBJECT_COUNT_EXTENT(field_count, 0u), (nested_count), 0 } +#define SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS(field_list, field_count, nested_list, nested_count, default_prefix) \ + { (field_list), (nested_list), (default_prefix), \ + SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_prefix)) \ + + SOFAB_OBJECT_ASSERT_EXTENT(sizeof(*(default_prefix)))), \ + (nested_count), 0 } /*! * @name Wrapper-array holder marker (@ref sofab_object_descr_t::fixed_seq) @@ -663,7 +667,7 @@ extern "C" { /*! @endcond */ #define SOFAB_OBJECT_DESCR_UNION(field_list, field_count, nested_list, nested_count, default_image, obj, tfield) \ { (field_list), (nested_list), (const void *)(default_image), \ - SOFAB_OBJECT_COUNT_EXTENT(field_count, 0u), (nested_count), \ + SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), \ (uint8_t)(_SOFAB_OBJECT_UNION_BIT \ | 0u * sizeof(struct { int tag_member_must_have_the_width_of_sofab_object_descr_id_t \ : sizeof(((obj *)0)->tfield) == sizeof(sofab_object_descr_id_t) ? 1 : -1; }) \ diff --git a/src/object.c b/src/object.c index c463964..cf601d0 100644 --- a/src/object.c +++ b/src/object.c @@ -239,9 +239,10 @@ static size_t _default_extent(const sofab_object_descr_t *info) * PREFIX of the object that carries defaults instead of a copy of all of it: a * message that defaults one byte costs one byte of .rodata. An extent of 0 reads * as "the whole object": what every descriptor written before the extent existed - * carries, what a union image uses, and what SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS - * states on purpose -- never a clamped prefix, which the macros refuse - * (SOFAB_OBJECT_ASSERT_EXTENT) because it would read past the image. + * carries, what a union image uses, and what SOFAB_OBJECT_DESCR_WITH_DEFAULTS + * records for a whole-object image too long for the extent byte. A prefix that + * long is refused by SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS instead + * (SOFAB_OBJECT_ASSERT_EXTENT), because read as 0 it would run past the image. */ static const void *_field_default(const sofab_object_descr_t *info, const sofab_object_descr_field_t *field) diff --git a/test/c/test_object.c b/test/c/test_object.c index 08622a9..a9a9a8e 100644 --- a/test/c/test_object.c +++ b/test/c/test_object.c @@ -4311,13 +4311,14 @@ static const sofab_object_descr_field_t _prefix_fields[] = static const prefix_img_t _prefix_img = { .seeded = 4242, .poison = 0xDEADBEEFu }; -/* Hand-built so the extent can be smaller than the buffer: the macro derives it - * from sizeof(*image), which is what a generator wants (its image stops where - * the defaults stop) but cannot express "describe only the first 4 bytes". */ +/* Hand-built so the extent can be smaller than the buffer: the prefix macro + * derives it from sizeof(*image), which is what a generator wants (its image + * stops where the defaults stop) but cannot express "describe only the first 4 + * bytes". */ static const sofab_object_descr_t _prefix_descr = { _prefix_fields, NULL, &_prefix_img, - SOFAB_OBJECT_COUNT_EXTENT(2, sizeof(uint32_t)), 0, 0 + SOFAB_OBJECT_DESCR_COUNT_WORD(2, sizeof(uint32_t)), 0, 0 }; /* ...and the same thing through the macro, where the image IS its own extent. */ @@ -4326,15 +4327,11 @@ typedef struct { uint32_t seeded; } prefix_exact_img_t; static const prefix_exact_img_t _prefix_exact_img = { .seeded = 4242 }; static const sofab_object_descr_t _prefix_exact_descr = - SOFAB_OBJECT_DESCR_WITH_DEFAULTS(_prefix_fields, 2, NULL, 0, &_prefix_exact_img); + SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS(_prefix_fields, 2, NULL, 0, &_prefix_exact_img); /* The same two fields, but the image is a whole object and the extent is 0 -- * what every descriptor written before the extent existed looks like, and what a - * union image still uses. Built twice on purpose: hand-rolled the way an old - * descriptor looks (an omitted initialiser is 0), and through the macro that - * states it, which is the only form left for an image the extent cannot - * describe -- one over SOFAB_OBJECT_DESCR_EXTENT_MAX is a build error now, not a - * silently clamped prefix. The two must read identically. */ + * union image still uses. Hand-built: the macro would fill the extent in. */ static const prefix_obj_t _whole_img = { .seeded = 4242, .plain = 77 }; static const sofab_object_descr_t _whole_descr = @@ -4342,8 +4339,30 @@ static const sofab_object_descr_t _whole_descr = _prefix_fields, NULL, &_whole_img, 2 /* count, extent 0 */, 0, 0 }; -static const sofab_object_descr_t _whole_macro_descr = - SOFAB_OBJECT_DESCR_WITH_WHOLE_DEFAULTS(_prefix_fields, 2, NULL, 0, &_whole_img); +/* + * A WHOLE-object image longer than the extent byte, through the macro every + * generated file before the extent uses. vehicle_telemetry is 824 bytes; its + * descriptor must keep building and keep seeding the members past byte 255, or + * every existing generated header breaks on the corelib update. The macro has + * to record extent 0 for it -- the one value that says "all of it" -- and the + * prefix form, which cannot say that, is the one that refuses an image this long. + */ +typedef struct +{ + char big[300]; /* pushes the next member past the extent byte */ + uint32_t tail; /* at offset 300: only an extent of 0 reaches it */ +} whole_big_obj_t; + +static const sofab_object_descr_field_t _whole_big_fields[] = +{ + SOFAB_OBJECT_FIELD(0, whole_big_obj_t, big, SOFAB_OBJECT_FIELDTYPE_STRING), + SOFAB_OBJECT_FIELD(1, whole_big_obj_t, tail, SOFAB_OBJECT_FIELDTYPE_UNSIGNED), +}; + +static const whole_big_obj_t _whole_big_img = { .big = "hi", .tail = 77 }; + +static const sofab_object_descr_t _whole_big_descr = + SOFAB_OBJECT_DESCR_WITH_DEFAULTS(_whole_big_fields, 2, NULL, 0, &_whole_big_img); static void test_object_default_image_may_be_a_prefix (void) { @@ -4402,18 +4421,27 @@ static void test_object_extent_zero_means_the_whole_object (void) TEST_ASSERT_EQUAL_UINT32_MESSAGE(77, o.plain, "extent 0 reads as 'the whole object', so this field is seeded too"); - /* ...and the macro that states it reads the same, member for member: the - * extent is the only thing it sets, so a difference could only be a wrong - * extent. */ - TEST_ASSERT_EQUAL_UINT16_MESSAGE(_whole_descr.count_extent, - _whole_macro_descr.count_extent, - "WITH_WHOLE_DEFAULTS must build the same count word as an old descriptor"); +} + +static void test_object_whole_image_longer_than_the_extent_byte (void) +{ + whole_big_obj_t o; + + TEST_ASSERT_EQUAL_UINT_MESSAGE(0, _whole_big_descr.count_extent >> 8, + "a whole image past the extent byte must read as 'the whole object'"); memset(&o, 0xAA, sizeof o); - sofab_object_init(&_whole_macro_descr, &o); - TEST_ASSERT_EQUAL_UINT32(4242, o.seeded); - TEST_ASSERT_EQUAL_UINT32_MESSAGE(77, o.plain, - "the whole image is read through the macro form too"); + sofab_object_init(&_whole_big_descr, &o); + TEST_ASSERT_EQUAL_STRING("hi", o.big); + TEST_ASSERT_EQUAL_UINT32_MESSAGE(77, o.tail, + "the member past byte 255 is seeded from the whole image"); + + /* and it is at its default, so a fresh object still encodes to nothing */ + uint8_t buf[16]; + sofab_ostream_t os; + sofab_ostream_init(&os, buf, sizeof buf, 0, NULL, NULL); + TEST_ASSERT_EQUAL(SOFAB_RET_OK, sofab_object_encode(&os, &_whole_big_descr, &o)); + TEST_ASSERT_EQUAL_size_t(0, sofab_ostream_flush(&os)); } /* @@ -4581,6 +4609,7 @@ int test_object_main (void) RUN_TEST(test_object_default_image_may_be_a_prefix); RUN_TEST(test_object_extent_zero_means_the_whole_object); + RUN_TEST(test_object_whole_image_longer_than_the_extent_byte); RUN_TEST(test_object_holder_keeps_a_16_bit_slot_count); return UNITY_END(); From 77a52b581057447c6feb41adaf121470ae854202 Mon Sep 17 00:00:00 2001 From: Andste82 Date: Tue, 13 Oct 2026 17:29:55 +0000 Subject: [PATCH 5/5] docs(readme): what each descriptor profile can describe, verified A new "Descriptor limits" section, linked from the SOFAB_OBJECT_DESCR_PROFILE row: what the descriptor can hold per profile, what that leaves for each schema type, the name of the compile error at every limit, and why a wrapper array runs out long before its element count does (it is one member of its parent, so its whole byte size meets the profile's `size` ceiling -- 10 elements of `string` under SMALL, 2 621 under MEDIUM). Every number without a footnote was found the same way: a schema at the limit and one past it, generated with sofabgen (#743 emitting WITH_PREFIX_DEFAULTS) and compiled against SMALL, MEDIUM and BIG -- N builds, N+1 fails with the named error, in every row and every profile. The footnoted ones are the schema's and the wire's own bounds, or follow from the 32-bit width where the struct at the boundary would be gigabytes. Co-Authored-By: Claude Opus 5.5 --- README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 180926b..7e3ff56 100644 --- a/README.md +++ b/README.md @@ -345,7 +345,7 @@ Two switches **tune** rather than remove: | Switch | Set with | Default | Effect | | - | - | - | - | | `SOFAB_LAZY_SEQ_DEPTH` | **macro only** | `8` | How many nested sequence headers can be held back at once — this profile's **documented hold-back bound**, see [Sequence framing](#sequence-framing-and-the-hold-back-window). Costs 4 B of RAM per output stream per level; must be **1…255** (the run counter is a `uint8_t`, and a build outside that range is rejected with an `#error`) | -| `SOFAB_OBJECT_DESCR_PROFILE` | CMake cache variable | `SOFAB_OBJECT_DESCR_MEDIUM` | Integer width of the object descriptor's members: `SOFAB_OBJECT_DESCR_SMALL` / `_MEDIUM` / `_BIG` = `uint8_t` / `uint16_t` / `uint32_t`. It sizes the **descriptor tables in your code**, not the library — the library's own `.text` barely moves and `SMALL` even costs a few bytes there (see [Footprint](#footprint)). Also in a public header, hence `PUBLIC` | +| `SOFAB_OBJECT_DESCR_PROFILE` | CMake cache variable | `SOFAB_OBJECT_DESCR_MEDIUM` | Integer width of the object descriptor's members: `SOFAB_OBJECT_DESCR_SMALL` / `_MEDIUM` / `_BIG` = `uint8_t` / `uint16_t` / `uint32_t`. It sizes the **descriptor tables in your code**, not the library — the library's own `.text` barely moves and `SMALL` even costs a few bytes there (see [Footprint](#footprint)). What each profile can describe is under [Descriptor limits](#descriptor-limits). Also in a public header, hence `PUBLIC` | Two knobs are **opt-IN** (off by default in this footprint corelib — see below): @@ -397,6 +397,85 @@ the C++ surface and are rejected outright with a `#error`. > - **Field ids:** `SOFAB_ID_MAX` shrinks to `UINT32_MAX >> 3`. > - **Conformance:** the shipped vectors carry 64-bit values and will not decode. +### Descriptor limits + +The object API describes a message with constant tables, and the integers in +those tables have fixed widths. `SOFAB_OBJECT_DESCR_PROFILE` sets three of them +— a field's `id`, its `offset` in the struct and its `size` in bytes — and +everything else is fixed. **Every limit below is a compile error that names its +cause**, in hand-written descriptors and in code generated by `sofabgen` alike; +none of them truncates a value silently. + +What the descriptor can hold: + +| Limit | `_SMALL` | `_MEDIUM` (default) | `_BIG` | Over it, the build fails with | +| - | -: | -: | -: | - | +| Field or element id | 255 | 65 535 | 2 147 483 647¹ | `field_exceeds_the_SOFAB_OBJECT_DESCR_PROFILE_id_offset_or_size_ceiling` | +| Bytes in one member (`offset`, `size`) | 255 | 65 535 | 4 294 967 295⁵ | same | +| Fields of one message, struct or union | 255 | 255 | 255 | `an_object_may_not_declare_more_than_255_fields` | +| Prefix default image | 255 B³ | 255 B | 255 B | `a_prefix_default_image_may_not_exceed_255_bytes` | +| Elements of a wrapper array | ² | ² | 65 535 | `a_wrapper_array_may_not_declare_more_than_65535_slots` | +| Sequence nesting depth | 255 | 255 | 255 | rejected by `sofabgen` (`MAX_DEPTH`) | +| `sizeof(sofab_object_descr_field_t)`, 32-bit target | 5 B | 8 B | 16 B | | +| `sizeof(sofab_object_descr_t)`, 32-bit target | 16 B | 16 B | 16 B | | + +¹ the descriptor holds 4 294 967 295; the wire stops at `SOFAB_ID_MAX` +(2 147 483 647, or 536 870 911 with `SOFAB_DISABLE_INT64_SUPPORT`), which +the generated header checks with its own `#error` naming the message. +² never reached: the array's bytes run into the `size` ceiling first (next table). +³ under `_SMALL` the member behind a longer prefix is already past the profile, +which is the error that fires. + +"Bytes in one member" is the limit a schema meets most often, because every +member counts whole: a `string` is `char[maxlen + 1]`, and a **wrapper array** — +an array of `string`, `blob`, `struct`, `union` or arrays — is one member of its +parent, however many elements it holds. What that leaves, per schema type: + +| Schema field | `_SMALL` | `_MEDIUM` | `_BIG` | +| - | -: | -: | -: | +| `string`, `maxlen` | 254 | 65 534 | 2 147 483 647⁴ | +| `blob`, `maxlen` | 255 | 65 535 | 2 147 483 647⁴ | +| array of `u8` / `i8` / `boolean`, `count` | 255 | 65 535 | 2 147 483 647⁴ | +| array of `u16` / `i16`, `count` | 127 | 32 767 | 2 147 483 647⁴ | +| array of `u32` / `i32` / `fp32`, `count` | 63 | 16 383 | 1 073 741 823⁵ | +| array of `u64` / `i64` / `fp64`, `count` | 31 | 8 191 | 536 870 911⁵ | +| array of `string`, `maxlen: 24`, `count` | 10 | 2 621 | 65 535 | +| array of `blob`, `maxlen: 1`, `count` (smallest element) | 127 | 32 766 | 65 535 | +| union options | 255 | 255 | 255 | + +⁴ the schema itself stops `maxlen` and `count` at 2 147 483 647, and so does the +wire (`SOFAB_FIXLEN_MAX`, `SOFAB_ARRAY_MAX`). +⁵ follows from the 32-bit width; not compiled at the boundary, the struct would be +gigabytes. + +A compact array — numbers, booleans — is one descriptor however long it is, so its +`count` is the byte ceiling divided by the element width. A wrapper array is one +descriptor **per element**, and the generator lays it out as a leading count — +as wide as the count needs (1 B up to 255, 2 B up to 65 535), and at least +as wide as the element's alignment — followed by the elements, so its `count` is +`(byte ceiling − count width) / element size`: a `string` of `maxlen` 24 is +25 B, which is where 10 and 2 621 come from. Its descriptor table also +grows with `count` — 300 `string` elements are 2 400 B of `.rodata` under +`_MEDIUM` — and decoding one element searches that table, so a full array costs +time quadratic in `count`. + +**A prefix default image** is what `sofabgen` emits when a struct declares +defaults: the defaulted members first, the image only as long as they are, read +through `SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS`. The descriptor records how far +it reaches in one byte, so a prefix stops at 255 B; past that the image has to +be the whole object, through `SOFAB_OBJECT_DESCR_WITH_DEFAULTS`, which takes one +of any size. + +The generated header adds its own guards on top: a message that uses a construct +the corelib was built without (`SOFAB_DISABLE_FIXLEN_SUPPORT`, `_ARRAY_`, +`_SEQUENCE_`, `_FP64_`, `_INT64_`), an id past `SOFAB_ID_MAX`, or a prefix image +on a corelib that cannot read one stops at an `#error` naming the message and the +switch. + +Every value without a footnote was verified by generating a schema at the limit +and one past it with `sofabgen` and compiling both against each profile; ¹ and +⁴ are bounds of the schema and the wire, ⁵ follows from the width. + ### Sequence framing and the hold-back window A nested structure is a **sequence**: a start marker, the child fields, an end