interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / json / README.md

143 lines · 9545 bytes Raw Blame History Source

Bounded JSON

json.om is an Omega-only, global-free JSON parser and encoder for the daemon's raw TKA requests. It has no dependencies on another unit. Concatenate it before callers. Parsing is iterative, not recursive, and retains exact number lexemes rather than converting through floating point.

Limits and policy

Bound violations return errors, never attacker-triggered traps. Callers must provide the fixed-size arrays named in the signatures, use distinct input/arena/token/output arrays, and discard partial output on failure. Token accessors and jsonencode require an unmodified table and arena from a successful jsonparse; a forged token table is a caller contract violation. Only tok[0] is valid following a failed parse (it is zero). Other data is scratch and may contain partial results.

Parsing API

`jsonparse(raw view uchar 65536, n ulong, tok edit ulong 32768, dec edit uchar 65536) -> int`

Returns zero on success, otherwise: 1 syntax, 2 input/decoded-byte bound, 3 token bound, 4 depth bound, 5 duplicate key, 6 object-member bound. Empty input is rejected. No I/O or global state is used.

tok[0] is the token count. Root id is 1; zero is the absent-id sentinel. Header slots 1..7 are reserved. Token id starts at id * 8, with these fields:

OffsetMeaning
0Kind: 1 object, 2 array, 3 string, 4 number, 5 true, 6 false, 7 null
1Offset into dec for decoded string or raw number lexeme
2Byte length; zero for containers, booleans and null
3Id immediately after the entire subtree
4Parent container id; zero at root
5Direct-child count; includes both keys and values for objects
61 for an object key, otherwise 0
7Internal parser phase; callers must not inspect it

Children are preorder. An object's children alternate key, value; a key is always a string token and its value starts at key_id + 1. Use a value's subtree-next field to advance to the next key.

No number precision is lost: numbers beyond 64-bit range remain valid JSON tokens but fail the integer helpers. There is no implicit float conversion or arbitrary exponent computation.

Encoding API

Every writer returns the next output offset, or jsonbad (65,537). That sentinel is sticky through further writer calls. No writer terminates output with a NUL. Capacity is checked before access; an error can leave a partial prefix, which must not be sent. Writer helpers do not maintain a JSON syntax state: punctuation/field sequencing is the caller's responsibility.

String output matches Go json.Marshal(string): standard short control escapes, lowercase \u hex, HTML escapes for <, >, and &, and escapes for U+2028/U+2029. Invalid UTF-8 becomes the six-character escape \ufffd, matching the Go 1.26 encoder the test corpus is generated with; a valid U+FFFD scalar is written as its raw UTF-8 bytes. U+FFFD scalar is written the same way, matching Go. Internal helpers all have the json prefix; callers should use the APIs above.

Focused tests

GO=/path/to/go1.26.8/bin/go sh om/json/test/run.sh
# Or pass the compiler already verified by tools/omc.sh:
GO=/path/to/go1.26.8/bin/go sh om/json/test/run.sh /path/to/pinned/omc

The suite builds with the repository-pinned Omega compiler. Go is a test oracle only and is required for this suite; the module pins tailscale.com v1.102.0. Deterministic generated cases compare complete token metadata/decoded data (a 64-bit rolling fingerprint), exact output bytes, accept/reject behavior and exact signed/unsigned integer results with Go encoding/json. The oracle uses Decoder.UseNumber, independently walks the token stream, and applies only the explicit resource bounds and duplicate-key policy above.

Coverage includes all eight actual tailcfg request types, their responses, map TKAInfo, NodeID values above 2^53 and at the signed limit, nil/empty byte values, arrays, maps, case-insensitive field decoding, escaped field names, ignored unknown fields, all 256 byte values, malformed and boundary UTF-8, surrogate pairs, exact integer limits, malformed grammar, and message/token/depth/ member/writer bounds. Tests explicitly distinguish valid generic JSON from a typed tailcfg rejection. The driver also checks lookup, token copying, offset/length boundaries and sticky writer errors. test/vectors.json supplies independently authored committed golden cases. Generated Go records and compiled test artifacts live in build/test/json, not the production unit. The member boundary includes 256 accepted and 257 rejected members, both at root and nested. A measured smoke sends ten full 65,536-byte messages, each with 256 distinct 250-byte keys sharing a 247-byte prefix. It prints actual wall time for parsing, token hashing and encoding together, including process startup and pipe I/O; this is not an isolated parser benchmark or a latency SLA.

Six mutants must finish the entire differential run normally and produce at least one wrong answer: duplicate acceptance, wrong surrogate replacement, depth and member off-by-ones, integer bound, and HTML escaping. A separate trapping mutant must be classified as abnormal, not counted as a successful detection. A short output, compiler failure, process signal, trap or timeout fails. These are host-only tests and make no stock-client deployment claim.

Third-party material and notices

No upstream implementation or vector text is copied or translated into this unit. Tests import the upstream Go packages through their pinned modules, which retain their own licenses. The vectors are independently authored. Format references:

References checked against the local pinned sources on 2026-10-25.