Repository navigation
Description
Is your feature request related to a problem? Please describe.
Every constructed generated C struct embeds an asn_struct_ctx_t _asn_ctx member (24 bytes on LP64) as its last field, purely so decoders can pause and resume across buffer boundaries (RC_WMORE). For a small NetMsg ::= SEQUENCE { id INTEGER, body CHOICE { ... } } schema, the generated NetMsg_t measures sizeof = 104, of which 72 bytes are _asn_ctx members; without them it is 32. Consequences:
- Generated structs are not plain data: they cannot be safely
memcpy'd, memset, serialized, or stored in bulk (SEQUENCE OF) without dragging parser scratch along. - The hidden mutable state is a thread-safety footgun and complicates debugging (parse state is invisible at the call site).
Describe the solution you'd like
An opt-in codegen flag -fno-ctx-members plus sibling decode entry points:
- With
-fno-ctx-members, generated constructed structs omit_asn_ctx; the type descriptor'sctx_offsetbecomes a sentinel ((unsigned)-1) andstruct_sizeshrinks automatically. Default output stays byte-identical to today. - New sibling entry points
ber_decode_ctx(opt_codec_ctx, td, struct_ptr, asn_struct_external_ctx_t *ext_ctx, buf, size)and per-codec variants (xer_/jer_/oer_/uper_/aper_/cbor_decode_ctx, plus anasn_decode_ctxdispatcher). The caller owns one arena (asn_struct_external_ctx_t ext = {0};) with 8 inline frames plus heap overflow, reuses it acrossRC_WMOREresumes, and resets it before a fresh decode. One arena type is shared by all generated types; frames are keyed by struct instance address. Encoders are unchanged. - The arena rides in
asn_codec_ctx_t(already threaded through every decoder), so no decoder vtable/signature changes are needed; the ~21st + specs->ctx_offsetfetch sites become dual-path, free routines and size guards become sentinel-aware, and newASN_STRUCT_RESET_CTX/ASN_STRUCT_FREE_CTXmacros use type-agnostic helpers (no newasn_TYPE_operation_sfield). - Contract: plain
ber_decode()cannot resume-fno-ctx-membersoutput; the flag is used together with the*_decode_ctxentry points.
Full design (with the coupling-surface map, prototype evidence, decisions log, rollout and testing plan): docs/external-ctx-design.md, delivered as design-doc PR #14.
Prototype validation (sandbox, real generated code): full-buffer, 1-byte-chunked and 3-byte-chunked BER decodes all pass identically with embedded and external ctx; sizeof(NetMsg_t) 104 -> 32; AddressSanitizer clean.
Describe alternatives you've considered
- Changing the
ber_decodesignature in place — rejected (ABI break); sibling functions keep existing callers working. - Replacing the member with a pointer
_asn_ctx_ptr— rejected: still a member, structs remain non-plain, and it adds a heap allocation per struct. - Adding a
free_struct_ctxmethod toasn_TYPE_operation_s— considered, deferred: all decoder scratch is genericctx->ptr, so type-agnostic arena helpers suffice; can be added later if a type needs custom cleanup. - Making external ctx the default — rejected: upstream-compatible default stays; opt-in flag only.
- The Rust backend already uses this model (plain value structs;
Decoded<T>{value,consumed};Limits; decoder-ownedRecordContext/cursors inrust/asn1c-runtime), so this brings the C backend to parity rather than inventing a new pattern.
Additional context
- Rollout phases (each its own PR off
vlm_master): Phase 0 BER/CER (shared machinery + arena +ber_decode_ctx), Phase 1 XER/JER, Phase 2 OER, Phase 3 UPER/APER (SET/SET-OF sites), Phase 4 CBOR.converter-example.c/PDU-collection helpers stay on the plain entry points until all codecs have_ctxversions. - Acceptance criteria per phase: both generation modes pass
make check;-Pexpected-output tests for the flag (no_asn_ctxmember, sentinel offset); chunked-decode regression driver (full / 1-byte / 3-byte chunks) per codec; ASAN/UBSan clean. - Scope note:
BIT_STRING_t/OCTET_STRING_tare fixed skeleton structs and keep their embedded member; the dual-path decoders make the mixed mode work.
Activity
Merge pull request #21 from sleevetosleeve/vibe/ext-ctx-converter-doc…
sleevetosleeve commentedon Nov 27, 2026
Amendment request for the external decode context: route the arena's own allocations through the allocator macros.
In
skeletons/constr_TYPE.c, the arena's heap-overflow array is managed with raw libc calls, while everything else in the skeletons uses the macros:constr_TYPE.c:45:realloc(ext->heap_frames, ncap * sizeof(*nf));constr_TYPE.c:70:free(ext->heap_frames);The per-frame parser scratch already goes through the macros (
FREEMEM(...ctx.ptr)atconstr_TYPE.c:66,69), and dynamic growth elsewhere usesREALLOC(e.g.asn_SET_OF.c:27,constr_SET_OF.c:103,429).Please amend the two raw calls so all memory management in this feature uses the macros:
Why it matters: consumers can redefine these macros. The aerodale Godot module (which generates its messaging layer with this fork) patches
asn_internal.hto routeCALLOC/MALLOC/REALLOC/FREEMEMto Godot's allocators; with the raw calls, the arena's overflow array currently bypasses that routing. The pairing is self-consistent (raw realloc/free), so this is not a bug — but the array escapes the consumer's allocator and memory accounting, and it is the only allocation in the feature that does not go through the macros. The 8 inline frames already keep the common case allocation-free; the overflow path is hit by deeply nested or many-element decodes.Testing suggestion:
tests/tests-skeletons/check-ext-ctx.c(heap-overflow case) should pass unchanged; reruntests-skeletonsplus thecheck-610pair under ASan/LSan to confirm the macro pairing stays balanced.Found while integrating
-fno-ctx-membersdownstream: generated code now decodes viaber_decode_ctxwith a caller-ownedasn_struct_external_ctx_tarena.Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe vibe@mistral.ai
sleevetosleeve commentedon Nov 27, 2026
Amendment implemented in PR #22 (branch
vibe/ext-ctx-arena-macros-d2e85d): the arena heap-overflow array inskeletons/constr_TYPE.cnow usesREALLOC/FREEMEMinstead of rawrealloc/free, so redefined allocator macros capture it too. The 8 inline frames stay allocation-free and the per-frame scratch already usedFREEMEM, so alloc/free pairing remains balanced.Verified locally:
tests-skeletons71/71 (incl.check-ext-ctxunder ASan/LSan),tests-asn1c-compiler17/17, check-610 pair 2/2,check-ext-ctx.sh1/1 — plus a consumer-style simulation with a patchedasn_internal.hrouting all four macros to counting allocators: 16 frames + scratch route 17 allocations through the macros andasn_struct_external_ctx_reset()frees all 17, LSan-clean. The pre-fix code failed that simulation (overflowreallocbypassed the macros), confirming the test detects the issue.Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe vibe@mistral.ai
Merge pull request #22 from sleevetosleeve/vibe/ext-ctx-arena-macros-…
fix: literal \n in -fno-ctx-members usage line; add issue #15 post-mo…
Merge pull request #26 from sleevetosleeve/vibe/ext-ctx-arena-sentine…
fix: literal \n in -fno-ctx-members usage line; add issue #15 post-mo…
Merge pull request #25 from sleevetosleeve/vibe/help-text-and-postmor…
sleevetosleeve commentedon Nov 29, 2026
Closing as done — the external decode context feature is complete on
vlm_masterand verified downstream (Godot integration working with-fno-ctx-members).Shipped across:
docs/external-ctx-design.md)-fno-ctx-memberscodegen,ASN_CTX_OFFSET_EXTERNALsentinel,asn_struct_external_ctx_tarena,ber_decode_ctx/asn_decode_ctx, dual-path decoders, 610 regression modulexer/jer/oer/uper/aper/cbor_decode_ctxREALLOC/FREEMEM), per the amendment comment above-husage text fix anddocs/issue-15-s2s-postmortem.mdDocs for end users: README section "External decode context (
-fno-ctx-members)", theasn1c(1)man page entry,docs/external-ctx-design.md(design + decision log),docs/issue-15-s2s-postmortem.md(post-mortem).Tests:
tests-skeletons/check-ext-ctx.c,tests-asn1c-smoke/check-ext-ctx.sh, check-610 in both modes,Wrapaliasing regression in the 610 module; full local suites green (skeletons 71/71, c-compiler 94/94, smoke 43/43, compiler 19/19) plus the PKIX1 examplemake checkin both codegen modes.Note: the flag remains fork-specific and is rejected as rust-incompatible by the Rust backend, as designed.
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe vibe@mistral.ai