Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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
Expand Down
160 changes: 152 additions & 8 deletions src/include/sofab/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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; }) \
Expand Down Expand Up @@ -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<string>` 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;
Expand Down
Loading
Loading