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
17 changes: 17 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
Changes since v1.5.3:
* Make external-arena heap frames stable (issue #15): the overflow is now
an array of pointers to individually CALLOC'd frames instead of a
realloc'd frame array. Decoders hold asn_struct_ctx_t* across nested
decode calls, and growing a realloc'd frame array left those pointers
dangling (heap-use-after-free on the PKIX1 certificate in
-fno-ctx-members mode, which needs >8 frames; found under ASan).
check-ext-ctx now also asserts frame pointers survive array growth.
* Use the external decode-context arena only for the
ASN_CTX_OFFSET_EXTERNAL sentinel (issue #15): asn_get_struct_ctx() now
resolves a real ctx_offset to the embedded member even when the codec
context carries an arena. The previous "arena wins whenever present" rule
broke the PKIX1 example: ANY text/JER/XER adapters decode through
stack-temporary view structs whose address and specifics are reused
across sequential ANY values, so arena frames keyed on them collided
(found by the manual c-cpp.yml run on vlm_master, PKIX1 check-ber).
The (st, type_key) frame keying stays: -fno-ctx-members code is
all-sentinel, where offset-0 inline members still alias.
* Route the external decode-context arena's heap-overflow array through
the allocator macros (issue #15 amendment): REALLOC/FREEMEM instead of
raw realloc/free in constr_TYPE.c, so consumers that redefine the macros
Expand Down
32 changes: 27 additions & 5 deletions docs/external-ctx-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,22 @@ Defined in `skeletons/constr_TYPE.h` next to `asn_struct_ctx_t`:
fixture; fixed by keying on the specifics pointer, which is unique per type).
- `asn_struct_external_ctx_t` — the arena, one per top-level decode operation:
an inline array of 8 frames (`ASN_EXT_CTX_INLINE_FRAMES = 8`) plus an
optional heap overflow, so the common case needs no allocation.
optional heap overflow (an array of pointers to individually allocated
frames — decoders hold `asn_struct_ctx_t *` across nested calls, so frames
must never move when the array grows; found as heap-use-after-free on the
PKIX1 certificate in `-fno-ctx-members` mode, which needs >8 frames), so the
common case needs no allocation.
- Helpers (type-agnostic, no vtable change):
`asn_ext_ctx_get(arena, st, type_key)` (find-or-create a zeroed frame),
`asn_struct_external_ctx_reset(arena)` (free every frame's `ctx->ptr`
scratch, free the heap overflow, zero the arena),
`asn_get_struct_ctx(opt_codec_ctx, st, ctx_offset)` (dual-path fetch used by
all decoder sites), and `asn_ctx_offset_valid(struct_size, ctx_offset)`
(sentinel-aware layout guard).
`asn_get_struct_ctx(opt_codec_ctx, st, ctx_offset, type_key)` (dual-path
fetch used by all decoder sites: the arena is consulted **only** for the
`ASN_CTX_OFFSET_EXTERNAL` sentinel; a real offset always resolves to the
embedded member, because decoders that run through stack-temporary view
structs (the ANY text/JER/XER adapters) reuse those addresses and would
collide in an address-keyed arena), and `asn_ctx_offset_valid(struct_size,
ctx_offset)` (sentinel-aware layout guard).
- Carried inside `asn_codec_ctx_t` as a `void *ext_struct_ctx` field (a `void *`
avoids header cycles); set by the `*_decode_ctx` entry points on their stack
copy of the codec context, so it reaches every nested decoder through the
Expand Down Expand Up @@ -199,7 +207,8 @@ symmetry and pass the arena through `asn_codec_ctx_t` unchanged.
now decodes through `asn_decode_ctx` / `uper_decode_ctx` / `aper_decode_ctx`
with a file-scope arena and frees via `ASN_STRUCT_FREE_CTX`, so it works
unchanged with both default and `-fno-ctx-members` generated code (the arena
takes precedence over embedded members when present). Previously deferred
is consulted only for the sentinel offset; default code keeps using its
embedded members). Previously deferred
until all codecs had `_ctx` versions (decision recorded 2026-11-26, lifted
2026-11-27); covered by `tests/tests-asn1c-smoke/check-ext-ctx.sh`.

Expand Down Expand Up @@ -233,6 +242,19 @@ pass the existing suites.
(regression found by check-BER-retag's `Nested` fixture through the
onboarded converter-example; locked by `Wrap` in the 610 module, the
check-ext-ctx unit test, and the check-ext-ctx smoke test).
- 2026-11-28: heap overflow frames are individually allocated behind a pointer
array so their addresses (and the ctx pointers decoders hold across nested
decode calls) survive array growth — fixes a heap-use-after-free in SET-OF
BER decode of the PKIX1 certificate with >8 live frames under
-fno-ctx-members (ASan).
- 2026-11-28: fetch rule changed from "arena wins whenever present" to "arena
only for the sentinel offset" — the PKIX1 example (ANY members decoded via
stack view structs in ANY.c's ANY_text_prepare) collided in the arena because
the view address and its specifics are reused across sequential ANYs.
With a real offset the embedded member is authoritative again; the
(st, type_key) keying stays, since -fno-ctx-members code is all-sentinel and
offset-0 inline members still alias there. Found via the manual c-cpp.yml
run on vlm_master (PKIX1 example check-ber).
- 2026-11-27: `converter-example.c` onboarded onto the `_ctx` entry points
with a file-scope arena; the arena-wins-when-present fetch rule makes the
same binary correct for both codegen modes. Arena helpers unit-tested in
Expand Down
54 changes: 38 additions & 16 deletions skeletons/constr_TYPE.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ asn_struct_ctx_t *asn_ext_ctx_get(asn_struct_external_ctx_t *ext, void *st,
return &ext->inline_frames[i].ctx;
if(ext->frames_used > ASN_EXT_CTX_INLINE_FRAMES)
for(i = ASN_EXT_CTX_INLINE_FRAMES; i < ext->frames_used; i++)
if(ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].st == st
&& ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].type_key == type_key)
return &ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].ctx;
if(ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES]->st == st
&& ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES]->type_key == type_key)
return &ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES]->ctx;
if(ext->frames_used < ASN_EXT_CTX_INLINE_FRAMES) {
asn_struct_ctx_frame_t *f = &ext->inline_frames[ext->frames_used++];
f->st = st;
Expand All @@ -41,44 +41,66 @@ asn_struct_ctx_t *asn_ext_ctx_get(asn_struct_external_ctx_t *ext, void *st,
}
if(ext->frames_used - ASN_EXT_CTX_INLINE_FRAMES >= ext->heap_capacity) {
size_t ncap = ext->heap_capacity ? ext->heap_capacity * 2 : 8;
asn_struct_ctx_frame_t *nf = (asn_struct_ctx_frame_t *)
asn_struct_ctx_frame_t **nf = (asn_struct_ctx_frame_t **)
REALLOC(ext->heap_frames, ncap * sizeof(*nf));
if(!nf) return 0;
ext->heap_frames = nf;
ext->heap_capacity = ncap;
}
{
asn_struct_ctx_frame_t *f =
&ext->heap_frames[ext->frames_used - ASN_EXT_CTX_INLINE_FRAMES];
ext->frames_used++;
/*
* Allocate the frame itself individually: growing heap_frames above
* may realloc the pointer array, but existing frame addresses (and
* the asn_struct_ctx_t* decoders hold across nested calls) stay
* valid for the lifetime of the arena.
*/
asn_struct_ctx_frame_t *f = (asn_struct_ctx_frame_t *)
CALLOC(1, sizeof(*f));
if(!f) return 0;
f->st = st;
f->type_key = type_key;
memset(&f->ctx, 0, sizeof(f->ctx));
ext->heap_frames[ext->frames_used - ASN_EXT_CTX_INLINE_FRAMES] = f;
ext->frames_used++;
return &f->ctx;
}
}


void asn_struct_external_ctx_reset(asn_struct_external_ctx_t *ext) {
size_t i;
if(!ext) return;
for(i = 0; i < ext->frames_used && i < ASN_EXT_CTX_INLINE_FRAMES; i++)
FREEMEM(ext->inline_frames[i].ctx.ptr);
if(ext->frames_used > ASN_EXT_CTX_INLINE_FRAMES)
for(i = ASN_EXT_CTX_INLINE_FRAMES; i < ext->frames_used; i++)
FREEMEM(ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].ctx.ptr);
for(i = ASN_EXT_CTX_INLINE_FRAMES; i < ext->frames_used; i++) {
asn_struct_ctx_frame_t *f =
ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES];
FREEMEM(f->ctx.ptr);
FREEMEM(f);
}
FREEMEM(ext->heap_frames);
memset(ext, 0, sizeof(*ext));
}

asn_struct_ctx_t *asn_get_struct_ctx(const asn_codec_ctx_t *opt_codec_ctx,
void *st, unsigned ctx_offset,
const void *type_key) {
if(opt_codec_ctx && opt_codec_ctx->ext_struct_ctx)
return asn_ext_ctx_get(
(asn_struct_external_ctx_t *)opt_codec_ctx->ext_struct_ctx,
st, type_key);
if(!st || ctx_offset == ASN_CTX_OFFSET_EXTERNAL) return 0;
/*
* The arena is used only when there is no embedded member to use
* (ctx_offset == ASN_CTX_OFFSET_EXTERNAL, i.e. -fno-ctx-members code).
* With a real offset the embedded member is always authoritative: some
* decoders (ANY text/JER/XER paths) decode through stack-temporary view
* structs whose address and specifics are reused across sequential
* values, so an arena keyed on them would collide; the embedded member
* is shuttled by those paths instead.
*/
if(ctx_offset == ASN_CTX_OFFSET_EXTERNAL) {
if(opt_codec_ctx && opt_codec_ctx->ext_struct_ctx)
return asn_ext_ctx_get(
(asn_struct_external_ctx_t *)opt_codec_ctx->ext_struct_ctx,
st, type_key);
return 0;
}
if(!st) return 0;
return (asn_struct_ctx_t *)((char *)st + ctx_offset);
}

Expand Down
22 changes: 16 additions & 6 deletions skeletons/constr_TYPE.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ typedef struct asn_struct_ctx_frame_s {

typedef struct asn_struct_external_ctx_s {
asn_struct_ctx_frame_t inline_frames[ASN_EXT_CTX_INLINE_FRAMES];
asn_struct_ctx_frame_t *heap_frames; /* Overflow past the inline frames */
/*
* Overflow past the inline frames: an array of POINTERS to individually
* allocated frames. Decoders hold asn_struct_ctx_t* across nested decode
* calls, so frames must never move when the array grows (a plain
* realloc'd array of frames left dangling ctx pointers — use-after-free
* found on the PKIX1 certificate, which needs >8 frames).
*/
asn_struct_ctx_frame_t **heap_frames;
size_t frames_used;
size_t heap_capacity;
} asn_struct_external_ctx_t;
Expand All @@ -82,11 +89,14 @@ void asn_struct_external_ctx_reset(asn_struct_external_ctx_t *ext);
struct asn_codec_ctx_s; /* Forward declaration */

/*
* Fetch the parsing context of structure st: from the external arena when the
* codec context carries one, otherwise from the embedded member at ctx_offset.
* type_key (the type specifics pointer) discriminates arena frames of nested
* structures sharing the same address. Returns 0 when ctx_offset is
* ASN_CTX_OFFSET_EXTERNAL but no arena is set.
* Fetch the parsing context of structure st. When ctx_offset is
* ASN_CTX_OFFSET_EXTERNAL (-fno-ctx-members code, no embedded member), the
* frame comes from the external arena carried in the codec context, keyed by
* (st, type_key) — type_key (the type specifics pointer) discriminates nested
* structures sharing the same base address. With a real offset the embedded
* member at ctx_offset is always used (decoders that run through stack
* temporary views rely on this); returns 0 for the sentinel without an arena
* or for a null st.
*/
asn_struct_ctx_t *asn_get_struct_ctx(const struct asn_codec_ctx_s *opt_codec_ctx,
void *st, unsigned ctx_offset,
Expand Down
3 changes: 2 additions & 1 deletion skeletons/converter-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ static int opt_partial; /* -P (print partial decoding results on failure) */
/*
* External decode-context arena used for all decoding in this converter, so
* it works with code generated both with and without -fno-ctx-members (the
* arena takes precedence over embedded _asn_ctx members when present).
* arena is consulted for the ASN_CTX_OFFSET_EXTERNAL sentinel; with a real
* offset the embedded _asn_ctx member is used).
* Zero-initialized once; every ASN_STRUCT_FREE_CTX / ASN_STRUCT_RESET_CTX
* below resets it after releasing the structure.
*/
Expand Down
17 changes: 13 additions & 4 deletions tests/tests-skeletons/check-ext-ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ main(void) {
for(i = 0; i < 16; i++)
assert(asn_ext_ctx_get(&ext, storage[i], (const void *)(i + 1))
== frames[i]); /* reuse */
/* Heap frames are individually allocated: their addresses (and the
asn_struct_ctx_t* decoders hold across nested calls) must stay valid
when the overflow array grows. Dereferencing them here after two
growth steps catches a realloc-moved frame array (ASan would flag
use-after-free with the old layout). */
for(i = 0; i < 16; i++)
assert(frames[i]->phase == (short)(i + 1));

/* Aliasing regression: nested inline members share the base address of
their parent (single-member SEQUENCE wrapper at offset 0), so frames
Expand Down Expand Up @@ -75,12 +82,14 @@ main(void) {
/* Sentinel offset without an arena cannot be resolved. */
assert(asn_get_struct_ctx(&codec_ctx, &st, ASN_CTX_OFFSET_EXTERNAL, &st) == 0);
assert(asn_get_struct_ctx(0, &st, ASN_CTX_OFFSET_EXTERNAL, &st) == 0);
/* With an arena in the codec context, the arena wins, even for a
structure that still has an embedded member. */
/* With a real offset the embedded member is authoritative even when
the codec context carries an arena (decoders running through stack
temporary views rely on this). */
codec_ctx.ext_struct_ctx = &ext;
assert(asn_get_struct_ctx(&codec_ctx, &st,
offsetof(struct ext_ctx_probe_s, ctx), (const void *)1)
== asn_ext_ctx_get(&ext, &st, (const void *)1));
offsetof(struct ext_ctx_probe_s, ctx), (const void *)1) == &st.ctx);
assert(ext.frames_used == 1); /* only the aliasing probes above */
/* The sentinel offset resolves through the arena. */
assert(asn_get_struct_ctx(&codec_ctx, &st, ASN_CTX_OFFSET_EXTERNAL,
(const void *)2)
== asn_ext_ctx_get(&ext, &st, (const void *)2));
Expand Down