From bfe5ce53072865ac5633c2d83576a629b9315fa9 Mon Sep 17 00:00:00 2001 From: Andreas Steinbart Date: Sun, 9 Aug 2026 09:58:30 +0000 Subject: [PATCH] perf: take std::function, a re-tested word skip and two mask rounds off the hot paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Five measured changes on the maxspeed profile, plus the cleanup they made possible. Every figure is instructions/op under Callgrind (bench/run_callgrind.sh), which is what this repo measures with because it is deterministic and machine-independent. Workload before after delta encode: u64 array (1000) 38 046 35 046 -7.9 % encode: typical message 266 224 -15.8 % decode: u64 array (1000) 44 839 43 830 -2.2 % decode: typical message 1 329 1 199 -9.8 % 1. dispatchLevel took its per-field callback as `const std::function&`, so every `read()` of a sub-message built one, invoked through it twice and destroyed it through its manager pointer; the std::function machinery was ~13 % of the `decode: typical message` profile. It is now a template on the callable. The two call sites pass a stateless no-op and a two-pointer lambda. 2. utf8Valid re-tested its eight-byte ASCII-skip condition once per character, so a short ASCII payload paid four branches a byte -- validating the five-byte string of the shared `typical message` workload cost ~62 instructions. The word skip and the byte scan now each run to completion before the other is considered. 3. spread7's last two rounds are spelled as an add. Once the 7-bit groups are laid out, each round's two halves partition the live bits, so `(w & keep) | ((w & move) << k)` is exactly `w + (2^k - 1) * (w & move)` -- one mask and one OR less per round, three x86 instructions off every varint the encoder writes. 4. getVarintWindowed took its single-byte fast path off a separate byte load of p[0] before loading the eight-byte word it needs anyway; the test now comes off that word. The tenth-byte overflow test `(b & 0x7f) > 1 || (b & 0x80)` folds to `b > 1`, which is the same predicate. 5. The float scalar/array paths built their four or eight wire bytes with a shift loop that GCC did not fold into a store. New detail::storeLittleBits / loadLittleBits keep the byte-at-a-time form for big-endian hosts only. Cleanup in the same pass: - The field-header parser existed twice, at the top level and inside a sequence, and the copies had already drifted once: the nested one shipped without the §6.2 ARRAY_MAX ceiling on a fixlen array's count word until #103. They are now one parser (parseFieldTag + parseFieldMeta, composed by parseFieldHeader for the top level, which owns the dangling-SequenceEnd rule). 70 lines gone. - headerParsersEnforceCeilings() can now assert the invariant at full strength -- exactly one site decodes a field header -- instead of checking that each of however many sites happens to carry the ceilings. - Dropped a doc block for a function that no longer exists, and moved the wire() documentation back onto wire() (it sat on consumed()). Behaviour is unchanged, and the two rewrites that could plausibly change it are pinned by differential tests against independent references: - utf8ValidatorSweep() (new) compares utf8_valid against a direct transcription of Unicode Table 3-7 over ~45 000 cases: every byte value and every interesting multi-byte sequence at every offset 0..17 inside an ASCII run, so each atom in turn crosses, starts on and ends on an eight-byte boundary. It passes unmodified against the pre-change header. - varintWidthSweep() already pins spread7/gather7 byte-exactly against a reference varint over every width, both boundaries of every width and a pseudo-random spread, windowed and byte-at-a-time. Full suite green in Debug and Release under GCC 15 and Clang, under ASan+UBSan, and against all 81 shared vectors plus the 11 invalid_utf8 ones. --- README.md | 30 +++- include/sofab/sofab.hpp | 373 +++++++++++++++++++++------------------- test/test_roundtrip.cpp | 98 +++++++++++ 3 files changed, 320 insertions(+), 181 deletions(-) diff --git a/README.md b/README.md index 0da2056..9e42e11 100644 --- a/README.md +++ b/README.md @@ -833,13 +833,15 @@ is better). All three are compiled at `-O3` so the comparison is like-for-like: | Workload | C | C++ wrapper | this (pure C++20) | |---|--:|--:|--:| -| encode: u64 array (1000) | 124 992 | 125 021 | **38 046** (−70 %) | -| encode: typical message | 908 | 1 008 | **270** (−70 %) | -| decode: u64 array (1000) | 289 941 | 289 942 | **44 835** (−85 %) | -| decode: typical message | 2 057 | 2 056 | **1 321** (−36 %) | - -All four columns were re-measured together on one machine; the percentages are -against the C column. Reproduce with `bash bench/run_callgrind.sh` here and the +| encode: u64 array (1000) | 124 992 | 125 021 | **35 046** (−72 %) | +| encode: typical message | 908 | 1 008 | **224** (−75 %) | +| decode: u64 array (1000) | 289 941 | 289 942 | **43 830** (−85 %) | +| decode: typical message | 2 057 | 2 056 | **1 199** (−42 %) | + +The four columns were measured together on one machine and the percentages are +against the C column. The last column has since been re-measured on its own — +only this library's code moved — and is the current reading; the two C columns +are the joint run's. Reproduce with `bash bench/run_callgrind.sh` here and the same script in `corelib-c-cpp`. The encode figures include the §2 sequence hold-back — the price of never @@ -855,7 +857,19 @@ place without the C port's per-field bookkeeping. The array workloads pull furthest ahead because their varint runs establish the ten-byte window once and then move whole 64-bit words — eight varint bytes per store on encode, one load plus a terminator scan on decode — instead of testing bounds and continuation a -byte at a time. +byte at a time. Inside that word step the bit-spread is written as an add rather +than a mask-and-recombine: once the 7-bit groups are laid out, each round's two +halves partition the live bits, so `(w & keep) | ((w & move) << k)` is +`w + (2^k - 1) * (w & move)` and drops a mask and an OR per round. + +On the message-shaped workloads the cost is dominated instead by what happens +*around* the bytes, so that is what the same figures made worth removing: the +nested-sequence dispatcher took its per-field callback as a `std::function`, +which cost a construction, a manager-driven destruction and two indirect hops on +every sub-message read; and the UTF-8 validator re-tested its word-skip +condition once per character, so a short ASCII payload paid four branches a byte. +Neither is visible on an array workload, and together they were a fifth of +`decode: typical message` and a quarter of `encode: typical message`. In the multi-language arena it lands at a 434-byte wire size (vs protobuf's 494 bytes) and roughly **1.3× the throughput of protobuf** for a comparable C++ diff --git a/include/sofab/sofab.hpp b/include/sofab/sofab.hpp index 76a5151..0b71b4d 100644 --- a/include/sofab/sofab.hpp +++ b/include/sofab/sofab.hpp @@ -285,8 +285,18 @@ namespace sofab { uint64_t w = v & 0x00FFFFFFFFFFFFFFull; /* 56 bits */ w = (w & 0x000000000FFFFFFFull) | ((w & 0x00FFFFFFF0000000ull) << 4); /* 2 x 28 */ - w = (w & 0x00003FFF00003FFFull) | ((w & 0x0FFFC0000FFFC000ull) << 2); /* 4 x 14 */ - w = (w & 0x007F007F007F007Full) | ((w & 0x3F803F803F803F80ull) << 1); /* 8 x 7 */ + /* The last two rounds are spelled as an add rather than the + * split-mask-and-recombine of the first. Once the groups are laid + * out, the two halves of a round *partition* the live bits — `w` + * equals `(w & keep) + (w & move)` exactly — so + * `(w & keep) | ((w & move) << k)` is `w + (2^k - 1) * (w & move)`, + * which drops one mask and one OR per round. That is three x86 + * instructions off every varint the encoder writes; on the shared + * `u64 array` workload it is ~8 % of the whole encode. The first + * round cannot use it: it would need `w` pre-masked to 56 bits in a + * register of its own, which costs back more than it saves. */ + w += 3u * (w & 0x0FFFC0000FFFC000ull); /* 4 x 14 */ + w += (w & 0x3F803F803F803F80ull); /* 8 x 7 */ return w; } @@ -306,6 +316,45 @@ namespace sofab return w; } + /** + * @brief Store the object representation of a 4- or 8-byte value as + * little-endian bytes. + * + * The wire is little-endian (§4), so on a little-endian host this is one + * unaligned store; elsewhere the bytes are laid down individually. The + * hand-rolled shift loop this replaces did not fold into a store on GCC + * and cost ten instructions per `fp32` field. + * + * @tparam U Unsigned type holding the bits (`uint32_t` or `uint64_t`). + * @param out Destination, with at least `sizeof(U)` writable bytes. + * @param bits Value to lay down. + */ + template + inline void storeLittleBits(uint8_t *out, U bits) noexcept + { + if constexpr (std::endian::native == std::endian::little) + std::memcpy(out, &bits, sizeof bits); + else + for (size_t i = 0; i < sizeof(U); ++i) + out[i] = static_cast((bits >> (8 * i)) & 0xffu); + } + + /** @brief Inverse of @ref storeLittleBits. */ + template + inline U loadLittleBits(const uint8_t *in) noexcept + { + U bits; + if constexpr (std::endian::native == std::endian::little) + std::memcpy(&bits, in, sizeof bits); + else + { + bits = 0; + for (size_t i = 0; i < sizeof(U); ++i) + bits |= static_cast(in[i]) << (8 * i); + } + return bits; + } + /** @brief Load eight little-endian bytes as a 64-bit word (see @ref storeLittle64). */ inline uint64_t loadLittle64(const uint8_t *in) noexcept { @@ -411,14 +460,25 @@ namespace sofab [[nodiscard]] constexpr bool utf8Valid(const char *data, size_t len) noexcept { size_t i = 0; - uint32_t state = 0; while (i < len) { - /* SWAR: skip runs of ASCII 8 bytes at a time. Payloads in - * practice are overwhelmingly ASCII, and this is where the - * time goes -- a per-byte DFA over the same run is slower. */ + /* The ASCII run, which is where the time goes: payloads in + * practice are overwhelmingly ASCII, and a per-byte DFA over such + * a run is slower than skipping it. It is walked in two steps, + * word-at-a-time while a whole word is left and byte-at-a-time + * over what remains, and — this is the point — each step runs to + * completion before the other is considered again. Re-testing the + * word condition once per byte, as this loop used to, cost a + * short ASCII payload four branches per character: validating the + * five-byte string of the shared `typical message` workload took + * ~62 instructions, a quarter of the whole encode. */ if (!std::is_constant_evaluated()) { + /* Spelled `i + 8 <= len`, not `len - i >= 8`: the two are + * equal for `i <= len`, but only the first lets GCC's + * value-range pass see that eight bytes at `data + i` are + * in bounds — the subtraction reads as a possible unsigned + * wrap and costs a bogus -Warray-bounds under -Werror. */ while (i + 8 <= len) { uint64_t w; @@ -426,27 +486,22 @@ namespace sofab if (w & 0x8080808080808080ULL) break; i += 8; } - if (i >= len) break; } - /* Stray ASCII tail: a run shorter than 8 bytes never enters the - * SWAR skip, so a mostly-ASCII short payload would otherwise pay - * the DFA's two-lookups-per-byte for plain ASCII. Advance ASCII - * with one branch, exactly as the pre-DFA scalar loop did; only a - * genuine multi-byte lead falls through to the DFA below. */ - if (static_cast(data[i]) < 0x80) { ++i; continue; } - /* multi-byte: step the DFA until it is back at ACCEPT, then hand - * control to the SWAR loop again. */ - state = utf8Dfa[256u + state + utf8Dfa[static_cast(data[i])]]; - if (state == 12) return false; - ++i; - while (state != 0 && i < len) + while (i < len && static_cast(data[i]) < 0x80) ++i; + if (i >= len) return true; + /* multi-byte: step the DFA until it is back at ACCEPT (state 0), + * then hand control back to the ASCII scan. REJECT is state 12. */ + uint32_t state = 0; + do { state = utf8Dfa[256u + state + utf8Dfa[static_cast(data[i])]]; if (state == 12) return false; ++i; - } + } while (state != 0 && i < len); + /* Input ended inside a multi-byte sequence: truncated is INVALID. */ + if (state != 0) return false; } - return state == 0; + return true; } /** @@ -1601,14 +1656,15 @@ namespace sofab uint8_t *o = cursor_; o += encodeVarintPadded(o, headerWord(fieldId, Wire::Fixlen)); o += encodeVarintPadded(o, fixlenWord(sizeof(F), ft)); - for (size_t i = 0; i < sizeof(F); ++i) o[i] = static_cast((bits >> (8 * i)) & 0xff); + detail::storeLittleBits(o, bits); cursor_ = o + sizeof(F); return Error::None; } uint8_t tmp[20]; size_t n = encodeVarint(tmp, headerWord(fieldId, Wire::Fixlen)); n += encodeVarint(tmp + n, fixlenWord(sizeof(F), ft)); - for (size_t i = 0; i < sizeof(F); ++i) tmp[n++] = static_cast((bits >> (8 * i)) & 0xff); + detail::storeLittleBits(tmp + n, bits); + n += sizeof(F); return pushBytes(tmp, n); } @@ -1735,9 +1791,8 @@ namespace sofab { for (F v : elems) { - auto bits = detail::floatBits(v); uint8_t tmp[sizeof(F)]; - for (size_t i = 0; i < sizeof(F); ++i) tmp[i] = static_cast((bits >> (8 * i)) & 0xff); + detail::storeLittleBits(tmp, detail::floatBits(v)); if (Error e = pushBytes(tmp, sizeof(F)); e != Error::None) return e; } return Error::None; @@ -2673,18 +2728,20 @@ namespace sofab */ static bool getVarintWindowed(const uint8_t *&p, uint64_t &out, bool *overflow) noexcept { - /* A single byte still wins on its own: it is one load and one compare - * against the word machinery's load, mask and gather. */ - if (p[0] < 0x80) - { - out = p[0]; - ++p; - return true; - } /* Read the first eight bytes as one word and locate the terminator by * its clear continuation bit. Everything past it belongs to whatever * follows this varint, so it is masked away before the gather. */ const uint64_t w = detail::loadLittle64(p); + /* A single byte still wins on its own: one test against the gather's + * dozen ALU ops. It is taken off the word already loaded rather than + * from a second byte-sized load of p[0] — the load is on the critical + * path either way, and one of the two was pure overhead. */ + if (!(w & 0x80u)) + { + out = w & 0xffu; + ++p; + return true; + } const uint64_t term = ~w & 0x8080808080808080ull; if (term) [[likely]] { @@ -2711,7 +2768,10 @@ namespace sofab * so callers observe the same position either way. */ const uint8_t b9 = p[9]; p += 10; - if ((b9 & 0x7f) > 1 || (b9 & 0x80)) + /* `b9 > 1` is exactly "a payload bit above bit 0, or a continuation + * into an eleventh byte": the legal tenth bytes are 0x00 and 0x01 and + * nothing else, so the two tests fold into one compare. */ + if (b9 > 1) { if (overflow) *overflow = true; return false; @@ -2738,7 +2798,7 @@ namespace sofab } const uint8_t b9 = p[9]; p += 10; - if ((b9 & 0x7f) > 1 || (b9 & 0x80)) + if (b9 > 1) /* see @ref getVarintWindowed */ { if (overflow) *overflow = true; return false; @@ -2901,33 +2961,10 @@ namespace sofab template static F loadFloat(const uint8_t *p) noexcept { - if constexpr (sizeof(F) == 4) - { - uint32_t b = 0; - for (int i = 0; i < 4; ++i) b |= static_cast(p[i]) << (8 * i); - return detail::bitsFloat(b); - } - else - { - uint64_t b = 0; - for (int i = 0; i < 8; ++i) b |= static_cast(p[i]) << (8 * i); - return detail::bitsFloat(b); - } + using Bits = std::conditional_t; + return detail::bitsFloat(detail::loadLittleBits(p)); } - /** - * @brief Advance over one complete field (including nested sequences) without firing callbacks. - * - * Used to confirm a whole top-level field is present before delivering it. - * - * @param[in,out] p Cursor; advanced past the field on success. - * @param end One past the last readable byte. - * @param depth Current nesting depth; nesting past @ref MAX_DEPTH sets the - * error flag (§4.9) so the caller reports @ref Error::InvalidMessage. - * @return `true` if a full field was spanned, `false` if the buffer ends - * mid-field or the error flag was set (check @ref error_ to tell them apart). - */ - /** * @brief The §7.3 seam: does the delivered field's wire tag match the one * the caller's read declares? @@ -3000,11 +3037,21 @@ namespace sofab * @ref count_, ...) are set before @p cb runs; a field whose value the * callback does not @ref read is skipped automatically. * + * @tparam Cb The callable's own type, so the call is direct. A + * `std::function` parameter here cost a construction, a + * manager-driven destruction and two indirect hops **per + * sub-message read**, and the `std::function` machinery around + * it accounted for ~13 % of the `decode: typical message` + * profile. The two call sites pass a stateless no-op + * (@ref skipPayload) and a two-pointer lambda (@ref read), so + * the instantiation count is bounded and the body inlines into + * both. * @param cb Callback invoked as `(fieldId, size, count)` per field. * @param stopAtEnd If `true`, return at a @ref Wire::SequenceEnd * marker (nested level); if `false`, such a marker is a decode error. */ - void dispatchLevel(const std::function &cb, bool stopAtEnd) noexcept + template + void dispatchLevel(Cb &&cb, bool stopAtEnd) noexcept { while (p_ < end_ && !error_ && !incomplete_) { @@ -3015,20 +3062,8 @@ namespace sofab exceedsBuffer(static_cast(p_ - fieldStart_), 0)) { limitExceeded_ = true; return; } const uint8_t *fieldStart = p_; - uint64_t header; - bool ovf = false; - if (!getVarint(p_, end_, header, &ovf)) - { - (ovf ? error_ : incomplete_) = true; - return; - } - if ((header >> 3) > ID_MAX) - { - error_ = true; - return; - } - auto fieldId = static_cast(header >> 3); - type_ = static_cast(header & 0x7); + if (!parseFieldTag()) return; /* error_ or incomplete_ set */ + const sofab::id fieldId = fieldId_; /* §5.1/§7: an element index at or past the declared count is * INVALID -- but §7.3 decides first. An element header whose wire * type (or, for a fixlen element type, whose fixlen subtype) @@ -3067,77 +3102,12 @@ namespace sofab error_ = true; return; } - /* parse the metadata that precedes the payload */ - fixLen_ = 0; count_ = 0; - if (type_ == Wire::Fixlen) - { - uint64_t sub; - if (!getVarint(p_, end_, sub, &ovf)) - { - (ovf ? error_ : incomplete_) = true; - return; - } - if (!fixlenWordValid(sub)) /* §4.6/§7 */ - { - error_ = true; - return; - } - fixLen_ = static_cast(sub >> 3); - fixType_ = static_cast(sub & 0x7); - } - else if (type_ == Wire::ArrayUnsigned || type_ == Wire::ArraySigned) - { - uint64_t n; - if (!getVarint(p_, end_, n, &ovf)) - { - (ovf ? error_ : incomplete_) = true; - return; - } - if (n > ARRAY_MAX) /* §6.2/§7 */ - { - error_ = true; - return; - } - count_ = static_cast(n); - } - else if (type_ == Wire::ArrayFixlen) - { - uint64_t n; - if (!getVarint(p_, end_, n, &ovf)) - { - (ovf ? error_ : incomplete_) = true; - return; - } - /* §4.8 step 1: the FORMAT ceiling fires on the count word - * whatever the subtype turns out to be, so an absurd count is - * rejected before anything is sized from it. Without this the - * `count_ * fixLen_` byte-span below wraps size_t — a count of - * 2^62 with 4-byte elements wraps to zero and the array is - * skipped as if it were empty, so a message that must be - * INVALID decodes COMPLETE. The top-level path - * (@ref parseFieldHeader) has always checked this; this - * nested one did not. */ - if (n > ARRAY_MAX) /* §6.2/§7 */ - { - error_ = true; - return; - } - count_ = static_cast(n); - /* §4.8: the fixlen_word is always present, even for an empty array. */ - uint64_t sub; - if (!getVarint(p_, end_, sub, &ovf)) - { - (ovf ? error_ : incomplete_) = true; - return; - } - if (!arrayFixlenWordValid(sub)) /* §4.8/§7 */ - { - error_ = true; - return; - } - fixLen_ = static_cast(sub >> 3); /* element size */ - fixType_ = static_cast(sub & 0x7); - } + /* The metadata that precedes the payload — one parser, shared + * with the top level (@ref parseFieldMeta). It used to be copied + * out here, and the copy silently lost the §6.2 count ceiling on + * a nested fixlen array until #103 put it back; a second copy of + * a validating parser is a hole waiting to reopen. */ + if (!parseFieldMeta()) return; /* the fixlen word is in: §7.3 first, then the §5.1/§7 bound. */ if (boundPending) @@ -3538,27 +3508,56 @@ namespace sofab } /** - * @brief Parse one field header plus the metadata that precedes its - * payload, into the current-field members. + * @brief Parse a field header word into @ref fieldId_ and @ref type_ (§4.3). * - * @return `true` when the header is complete and well-formed. On `false` - * either @ref error_ (malformed) or @ref incomplete_ (more bytes - * needed) is set. + * The **only** place a `(id << 3) | wire_type` word is taken apart. Both + * parse loops — @ref parseTopLevel through @ref parseFieldHeader, and + * @ref dispatchLevel inside a sequence — come through here, so the + * @ref ID_MAX ceiling (§6.2) cannot be present in one and missing from + * the other. + * + * @return `true` when a whole header word was read and its id is legal. + * On `false` either @ref error_ (a > 64-bit varint, or an id past + * @ref ID_MAX) or @ref incomplete_ (the word is cut short) is set. */ - bool parseFieldHeader() noexcept + bool parseFieldTag() noexcept { uint64_t header; bool ovf = false; if (!getVarint(p_, end_, header, &ovf)) - { if (ovf) error_ = true; else incomplete_ = true; return false; } - if ((header >> 3) > ID_MAX) + { + (ovf ? error_ : incomplete_) = true; + return false; + } + if ((header >> 3) > ID_MAX) /* §6.2/§7 */ { error_ = true; return false; } fieldId_ = static_cast(header >> 3); type_ = static_cast(header & 0x7); + return true; + } + + /** + * @brief Parse the word(s) between a field header and its payload — + * the fixlen word (§4.6), the array count (§4.7) or both (§4.8). + * + * Shared by the two parse loops for the same reason as @ref parseFieldTag: + * this is where §4.6's subtype/length pairing, §4.8's element-subtype rule + * and §6.2's @ref ARRAY_MAX ceiling are enforced, and a second copy of it + * is a place for one of them to go missing — which is exactly how the + * nested path lost the @ref ARRAY_MAX check on a fixlen array until #103. + * A varint or sequence header states no metadata, so it falls through. + * + * @return `true` when the metadata is complete and well-formed, with + * @ref fixLen_, @ref count_ and @ref fixType_ set. On `false` + * either @ref error_ or @ref incomplete_ is set. + */ + bool parseFieldMeta() noexcept + { fixLen_ = 0; count_ = 0; + bool ovf = false; switch (type_) { case Wire::Fixlen: @@ -3576,7 +3575,7 @@ namespace sofab } fixLen_ = static_cast(sub >> 3); fixType_ = static_cast(sub & 0x7); - break; + return true; } case Wire::ArrayUnsigned: case Wire::ArraySigned: @@ -3593,7 +3592,7 @@ namespace sofab return false; } count_ = static_cast(n); - break; + return true; } case Wire::ArrayFixlen: { @@ -3603,7 +3602,14 @@ namespace sofab (ovf ? error_ : incomplete_) = true; return false; } - if (n > ARRAY_MAX) + /* §4.8 step 1: the FORMAT ceiling fires on the count word + * whatever the subtype turns out to be, so an absurd count is + * rejected before anything is sized from it. Without it the + * `count_ * fixLen_` byte-span computed downstream wraps + * size_t — a count of 2^62 with 4-byte elements wraps to zero + * and the array is skipped as if it were empty, so a message + * that must be INVALID decodes COMPLETE (#103). */ + if (n > ARRAY_MAX) /* §6.2/§7 */ { error_ = true; return false; @@ -3621,16 +3627,38 @@ namespace sofab error_ = true; return false; } - fixLen_ = static_cast(sub >> 3); + fixLen_ = static_cast(sub >> 3); /* element size */ fixType_ = static_cast(sub & 0x7); - break; + return true; } - case Wire::SequenceEnd: - error_ = true; return false; /* §7: dangling end at the root */ default: - break; + return true; /* varint and sequence headers carry no metadata */ } - return true; + } + + /** + * @brief Parse one **top-level** field header plus the metadata that + * precedes its payload, into the current-field members. + * + * @ref parseFieldTag + @ref parseFieldMeta, with the one rule that is the + * top level's own: a @ref Wire::SequenceEnd here closes a sequence that + * was never opened, which is `INVALID` (§7). Inside a sequence the same + * marker is the close, which is why @ref dispatchLevel puts its own test + * between the two calls. + * + * @return `true` when the header is complete and well-formed. On `false` + * either @ref error_ (malformed) or @ref incomplete_ (more bytes + * needed) is set. + */ + bool parseFieldHeader() noexcept + { + if (!parseFieldTag()) return false; + if (type_ == Wire::SequenceEnd) /* §7: dangling end at the root */ + { + error_ = true; + return false; + } + return parseFieldMeta(); } /** @@ -3759,10 +3787,9 @@ namespace sofab for (size_t k = 0; k < fit; ++k, ++i) { uint64_t raw; - bool ovf = false; - if (!getVarintWindowed(p_, raw, &ovf)) + if (!getVarintWindowed(p_, raw, nullptr)) { - (ovf ? error_ : incomplete_) = true; + error_ = true; /* only a > 64-bit varint can fail here */ return false; } if (!store(i, raw)) return false; @@ -4325,6 +4352,13 @@ namespace sofab return n; } + /** + * @return `true` when a read in this delivery consumed the field. `false` + * means the field was declined (skipped) or its payload has not + * arrived yet, in which case it is delivered again later. + */ + [[nodiscard]] bool consumed() const noexcept { return consumed_; } + /** * @brief Wire type of the field currently being delivered. * @@ -4337,13 +4371,6 @@ namespace sofab * * @return The delivered field's @ref Wire. */ - /** - * @return `true` when a read in this delivery consumed the field. `false` - * means the field was declined (skipped) or its payload has not - * arrived yet, in which case it is delivered again later. - */ - [[nodiscard]] bool consumed() const noexcept { return consumed_; } - [[nodiscard]] Wire wire() const noexcept { return type_; } /** diff --git a/test/test_roundtrip.cpp b/test/test_roundtrip.cpp index 0ab2d8a..358aad5 100644 --- a/test/test_roundtrip.cpp +++ b/test/test_roundtrip.cpp @@ -2445,6 +2445,94 @@ static void schemaBoundOutranksCap() * itself is always available (utf8_valid); the SOFAB_STRICT_UTF8 gate only * decides whether encode/decode invoke it. --- */ +/* --- utf8_valid, differentially, against Unicode Table 3-7 ----------------- + * + * The validator is the one place in this header that takes a *security* + * decision on attacker-controlled bytes, and it is also a hot loop that gets + * rewritten for speed: it runs a Hoehrmann DFA over multi-byte sequences and + * skips ASCII runs a machine word at a time, so how far the word skip and the + * DFA each advance before handing over to the other is an implementation + * detail that moves. The hand-picked cases in strictUtf8() below pin the + * classic rejects; what they cannot pin is the *seams* — a lead byte landing + * exactly on a word boundary, a sequence straddling one, a truncation at every + * distance from the end. + * + * So this check compares the shipped validator against an independent + * reference: a direct transcription of Unicode Table 3-7 (the well-formed + * byte-sequence table), sharing no code, no table and no loop shape with it. + * The corpus places every single byte value and every interesting multi-byte + * sequence at every offset 0..17 inside an ASCII run, with trailing runs of up + * to 17 more — so every atom in turn crosses, starts on and ends on an + * eight-byte boundary, and every truncation distance is exercised. */ + +static bool refUtf8Valid(const char *d, size_t n) +{ + size_t i = 0; + while (i < n) + { + const unsigned c = static_cast(d[i]); + unsigned trail, lo, hi; /* continuation count, and the 2nd byte's range */ + if (c < 0x80) { ++i; continue; } + else if (c >= 0xC2 && c <= 0xDF) { trail = 1; lo = 0x80; hi = 0xBF; } + else if (c == 0xE0) { trail = 2; lo = 0xA0; hi = 0xBF; } + else if (c >= 0xE1 && c <= 0xEC) { trail = 2; lo = 0x80; hi = 0xBF; } + else if (c == 0xED) { trail = 2; lo = 0x80; hi = 0x9F; } + else if (c >= 0xEE && c <= 0xEF) { trail = 2; lo = 0x80; hi = 0xBF; } + else if (c == 0xF0) { trail = 3; lo = 0x90; hi = 0xBF; } + else if (c >= 0xF1 && c <= 0xF3) { trail = 3; lo = 0x80; hi = 0xBF; } + else if (c == 0xF4) { trail = 3; lo = 0x80; hi = 0x8F; } + else return false; /* C0/C1 (overlong), F5..FF (> U+10FFFF), 80..BF (bare) */ + if (n - i - 1 < trail) return false; /* truncated before the end */ + if (static_cast(d[i + 1]) < lo || + static_cast(d[i + 1]) > hi) return false; + for (unsigned k = 2; k <= trail; ++k) + { + const unsigned b = static_cast(d[i + k]); + if (b < 0x80 || b > 0xBF) return false; + } + i += trail + 1; + } + return true; +} + +static void utf8ValidatorSweep() +{ + std::vector atoms; + for (int b = 0; b < 256; ++b) atoms.push_back(std::string(1, static_cast(b))); + for (const char *a : {"\xC2\xA9", "\xDF\xBF", "\xE0\xA0\x80", "\xE2\x82\xAC", "\xED\x9F\xBF", + "\xEE\x80\x80", "\xEF\xBF\xBF", "\xF0\x90\x80\x80", "\xF0\x9F\x98\x80", + "\xF4\x8F\xBF\xBF", /* valid: both edges of every lead class */ + "\xC0\x80", "\xC1\xBF", "\xE0\x80\x80", "\xE0\x9F\xBF", "\xED\xA0\x80", + "\xED\xBF\xBF", "\xF0\x80\x80\x80", "\xF0\x8F\xBF\xBF", "\xF4\x90\x80\x80", + "\xF5\x80\x80\x80", /* overlong, surrogate, out of range */ + "\xC2", "\xE2\x82", "\xF0\x9F\x98", "\xE2\x28\xA1", "\xF0\x9F\x28\x80", + "\xC2\xC2\xA9"}) /* truncated and bad-continuation forms */ + atoms.push_back(std::string(a)); + + size_t cases = 0, mismatches = 0; + std::string s; + for (const std::string &atom : atoms) + for (size_t lead = 0; lead <= 17; ++lead) + for (size_t trail : {size_t{0}, size_t{1}, size_t{6}, size_t{7}, size_t{8}, + size_t{9}, size_t{16}, size_t{17}}) + { + s.assign(lead, 'a'); + s += atom; + s.append(trail, 'z'); + ++cases; + if (sofab::utf8_valid(std::string_view{s}) != refUtf8Valid(s.data(), s.size())) + ++mismatches; + } + CHECK(cases > 40000, "utf8 sweep: the corpus is the size it claims to be"); + CHECK(mismatches == 0, "utf8 sweep: utf8_valid agrees with Unicode Table 3-7 on every case"); + + /* Pure-ASCII runs of every length: the word skip's own boundaries. */ + bool asciiOk = true; + for (size_t n = 0; n <= 64; ++n) + if (!sofab::utf8_valid(std::string_view{std::string(n, 'x')})) asciiOk = false; + CHECK(asciiOk, "utf8 sweep: an ASCII run of every length 0..64 validates"); +} + /* Encode a single string/blob field (id 5) so it can be fed straight into ScalarMsg. */ static std::vector stringFieldWire(std::string_view payload, sofab::detail::Fix sub) { @@ -4221,6 +4309,15 @@ static void headerParsersEnforceCeilings() at = text.find(kDecodeSite, at + 1)) sites.push_back(at); CHECK(!sites.empty(), "header parsers: at least one field-header decode site is found"); + /* One parser, not two. The top level and the nested level used to carry a + * copy each, and the copies drifted: the nested one shipped without the + * §6.2 ARRAY_MAX check on a fixlen array's count word until #103, which is + * the whole reason this structural check exists. They are now one function + * (parseFieldTag + parseFieldMeta), so the invariant can be stated at full + * strength — a second site is a regression whether or not it happens to + * carry the ceilings today. */ + CHECK(sites.size() == 1, + "header parsers: the field header is taken apart in exactly one place"); for (size_t i = 0; i < sites.size(); ++i) { @@ -5041,6 +5138,7 @@ int main() maxDepth(); bufferLimits(); schemaBoundOutranksCap(); + utf8ValidatorSweep(); strictUtf8(); messageLayerFraming(); wrapperArrayCollectors();