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 diff --git a/src/include/sofab/object.h b/src/include/sofab/object.h index 67110ee..4ca257c 100644 --- a/src/include/sofab/object.h +++ b/src/include/sofab/object.h @@ -278,6 +278,90 @@ 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 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 +/*! @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 + * @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) <= SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) ? 1 : -1; })) + +/*! + * @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_prefix_default_image_may_not_exceed_255_bytes \ + : ((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 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 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_DESCR_COUNT_WORD(field_count, extent) \ + ((uint16_t)(((field_count) + SOFAB_OBJECT_ASSERT_FIELD_COUNT(field_count)) \ + | (uint16_t)(((extent) <= SOFAB_OBJECT_DESCR_EXTENT_MAX ? (extent) : 0u) \ + << SOFAB_OBJECT_DESCR_EXTENT_SHIFT))) + /*! * @brief Build an object descriptor (@ref sofab_object_descr_t) without defaults. * @@ -290,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, (field_count), (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. @@ -303,10 +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 a fully-populated object holding the field defaults. + * @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), (field_count), (nested_count), 0 } + { (field_list), (nested_list), (default_struct), \ + SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_struct))), (nested_count), 0 } + +/*! + * @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_prefix Pointer to a prefix of the object holding the field defaults. + */ +#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) @@ -362,8 +484,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, (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. @@ -428,7 +551,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, (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 \ @@ -542,7 +666,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_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; }) \ @@ -570,7 +695,26 @@ 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 + * (@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. + */ + 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..cf601d0 100644 --- a/src/object.c +++ b/src/object.c @@ -199,6 +199,62 @@ 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 (@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 & SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX); +} + +/*! + * @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 >> SOFAB_OBJECT_DESCR_EXTENT_SHIFT); +} + +/*! + * @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": what every descriptor written before the extent existed + * 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) +{ + 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 +330,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 +368,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 +643,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 +654,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 +749,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,11 +788,16 @@ extern sofab_ret_t sofab_object_init ( else #endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */ { - if (info->default_values != NULL) + /* 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 *, info->default_values, field->offset), + CAST_TO(const void *, fdefault, field->offset), field->size); } else @@ -747,8 +815,8 @@ 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, + 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 +871,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 +881,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 +1109,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 +1352,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..a9a9a8e 100644 --- a/test/c/test_object.c +++ b/test/c/test_object.c @@ -4270,6 +4270,229 @@ 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 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_DESCR_COUNT_WORD(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_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. 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 +}; + +/* + * 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) +{ + 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"); + +} + +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_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)); +} + +/* + * 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 +4607,10 @@ 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_whole_image_longer_than_the_extent_byte); + RUN_TEST(test_object_holder_keeps_a_16_bit_slot_count); + return UNITY_END(); }