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
19 changes: 19 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
Changes since v1.5.3:
* Add per-codec external-context decode entry points (issue #15, Phases
1-4): xer_decode_ctx(), jer_decode_ctx(), oer_decode_ctx(),
uper_decode_ctx(), aper_decode_ctx(), cbor_decode_ctx() complement
ber_decode_ctx()/asn_decode_ctx(); all stack-copy the codec context and
carry the caller-owned asn_struct_external_ctx_t arena in
asn_codec_ctx_t.ext_struct_ctx. Decoder headers take the arena as
struct asn_struct_external_ctx_s *. Plain entry points of the
resumable codecs (BER/CER, XER, JER, OER) fail with RC_FAIL on
-fno-ctx-members output; PER/CBOR plain entry points are unaffected
(they never fetch the embedded context at decode time).
* Add -fno-ctx-members (issue #15, Phase 0 BER/CER): generated constructed
structs omit the asn_struct_ctx_t _asn_ctx member and carry the
ASN_CTX_OFFSET_EXTERNAL sentinel in their specifics. New entry points
ber_decode_ctx() and asn_decode_ctx() decode with a caller-owned
asn_struct_external_ctx_t arena (8 inline frames + heap overflow)
carried in asn_codec_ctx_t; all decoder ctx fetches, free paths and
layout guards are dual-path / sentinel-aware. New macros
ASN_STRUCT_FREE_CTX / ASN_STRUCT_RESET_CTX reset the arena.
Design: docs/external-ctx-design.md.
* Stop emitting dead per-member type descriptors for SEQUENCE/SET members
that merely reference a float-mapped named REAL type under
-fcompound-names: the referenced type's descriptor already carries the
Expand Down
4 changes: 4 additions & 0 deletions asn1c/asn1c.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ asn1c_handle_simple_f_option(const char *name, asn1c_cli_options_t *options) {
options->compiler_flags |= A1C_NO_CONSTRAINTS;
else if(!strcmp(name, "no-include-deps"))
options->compiler_flags |= A1C_NO_INCLUDE_DEPS;
else if(!strcmp(name, "no-ctx-members"))
options->compiler_flags |= A1C_NO_CTX_MEMBERS;
else if(!strcmp(name, "includes-quoted"))
options->compiler_flags |= A1C_INCLUDES_QUOTED;
else if(!strcmp(name, "unnamed-unions"))
Expand Down Expand Up @@ -1416,6 +1418,8 @@ usage(const char *av0) {
" -fline-refs Include ASN.1 module's line numbers in comments\n"
" -fno-constraints Do not generate the constraint checking code\n"
" -fno-include-deps Do not generate the courtesy #includes for dependencies\n"
" -fno-ctx-members Do not embed asn_struct_ctx_t in generated structs;\\n"
" decode via the *_decode_ctx entry points with an external ctx\\n"
" -fprefer-import-source Resolve only names listed in IMPORTS (no whole-module fallback)\n"
" -funnamed-unions Enable unnamed unions in structures\n"
" -fwide-types Use INTEGER_t instead of \"long\" by default, etc.\n"
Expand Down
11 changes: 11 additions & 0 deletions doc/man/asn1c.1
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ Do not generate courtesy #include lines for non\-critical type
dependencies.
Helps prevent namespace collisions.
.TP
\-fno\-ctx\-members
Do not embed the \f[CR]asn_struct_ctx_t _asn_ctx\f[R] member into generated
structures; emit the \f[CR]ASN_CTX_OFFSET_EXTERNAL\f[R] sentinel in the type
specifics instead. Decode such code through \f[CR]ber_decode_ctx()\f[R] /
\f[CR]asn_decode_ctx()\f[R], passing a caller\-owned
\f[CR]asn_struct_external_ctx_t\f[R] arena (zero\-initialized once, reused
across \f[CR]RC_WMORE\f[R] resumes, reset with
\f[CR]asn_struct_external_ctx_reset()\f[R]); see \f[CR]constr_TYPE.h\f[R].
Plain \f[CR]ber_decode()\f[R] cannot resume \f[CR]\-fno\-ctx\-members\f[R]
output. Design: \f[CR]docs/external\-ctx\-design.md\f[R] (issue #15).
.TP
\-fprefer\-import\-source
Resolve only names that an IMPORTS group lists (\f[CR]xp_members\f[R]).
In both modes, a name listed in any IMPORTS group resolves to the module
Expand Down
10 changes: 10 additions & 0 deletions doc/man/asn1c.man.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ cargo check --offline --manifest-path generated/Cargo.toml
: Do not generate courtesy #include lines for non-critical type dependencies.
Helps prevent namespace collisions.

-fno-ctx-members
: Do not embed the `asn_struct_ctx_t _asn_ctx` member into generated
structures; emit the `ASN_CTX_OFFSET_EXTERNAL` sentinel in the type
specifics instead. Decode such code through `ber_decode_ctx()` /
`asn_decode_ctx()`, passing a caller-owned `asn_struct_external_ctx_t`
arena (zero-initialized once, reused across `RC_WMORE` resumes, reset
with `asn_struct_external_ctx_reset()`); see `constr_TYPE.h`.
Plain `ber_decode()` cannot resume `-fno-ctx-members` output.
Design: `docs/external-ctx-design.md` (issue #15).

-fprefer-import-source
: Resolve only names that an IMPORTS group lists (`xp_members`).
In both modes, a name listed in any IMPORTS group resolves to the module
Expand Down
45 changes: 36 additions & 9 deletions docs/external-ctx-design.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Proposal: external decode context (`-fno-ctx-members`)

Status: design proposal, 2026-11-26. No code is changed by this document;
a local prototype validated the mechanism (see "Experiment evidence") but is
not committed. Responsible party: asn1c maintainers (fork).
Status: design implemented, 2026-11-26 (issue #15); all phases landed:
Phase 0 (BER/CER, shared machinery: `-fno-ctx-members`, the arena, dual-path
fetches, sentinel-aware guards/free paths, tests) plus Phases 1-4, which add
the remaining per-codec `_ctx` entry points (`xer_decode_ctx`,
`jer_decode_ctx`, `oer_decode_ctx`, `uper_decode_ctx`, `aper_decode_ctx`,
`cbor_decode_ctx`) on top of the existing `ber_decode_ctx` / `asn_decode_ctx`.
A local prototype validated the mechanism earlier (see "Experiment
evidence"). Responsible party: asn1c maintainers (fork).

## Motivation

Expand Down Expand Up @@ -115,9 +120,11 @@ Defined in `skeletons/constr_TYPE.h` next to `asn_struct_ctx_t`:
optional heap overflow, so the common case needs no allocation.
- Helpers (type-agnostic, no vtable change):
`asn_ext_ctx_get(arena, st)` (find-or-create a zeroed frame),
`asn_struct_external_ctx_reset(arena)` (free heap overflow, zero the arena),
`asn_ext_ctx_release_frame(arena, st)` (free `ctx->ptr` scratch and zero the
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).
- 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 @@ -145,6 +152,13 @@ Compatibility contract: plain `ber_decode()` & co. cannot resume
`-fno-ctx-members` output (a temporary arena would die on `RC_WMORE`), so
constructed types decoded through them fail with `RC_FAIL`. This is
documented; the flag and the `_ctx` entry points are used together.
Refined after Phases 1-4: the `RC_FAIL` contract holds for the resumable
codecs (BER/CER, XER, JER, OER), whose decoders fetch the context at decode
time. UPER/APER/CBOR decoders never fetch the embedded context at decode
time (PER is single-shot by design; the CBOR/PER `ctx_offset` uses are
layout guards only), so their plain entry points keep decoding
`-fno-ctx-members` output successfully; the `_ctx` variants exist for API
symmetry and pass the arena through `asn_codec_ctx_t` unchanged.

### 4. Decoder and runtime changes

Expand All @@ -158,9 +172,11 @@ documented; the flag and the `_ctx` entry points are used together.
when the sentinel is set (the arena provides the room).
- New macros `ASN_STRUCT_RESET_CTX(asn_DEF, ptr, ext)` and
`ASN_STRUCT_FREE_CTX(asn_DEF, ptr, ext)`: existing `free_struct` (sentinel
aware) plus arena frame release. **Decision: no new field in
`asn_TYPE_operation_s` and no `free_struct_ctx` method** — all scratch is
generic `ctx->ptr`, so type-agnostic helpers suffice.
aware) plus `asn_struct_external_ctx_reset(ext)`. **Decision: no new field
in `asn_TYPE_operation_s` and no `free_struct_ctx` method** — all scratch
is generic `ctx->ptr`, so type-agnostic helpers suffice. Resetting the whole
arena (not one frame) is what makes re-decoding into the same struct work,
matching the embedded model where `ASN_STRUCT_RESET` zeroes nested ctxs too.
- Partial decoding: the arena's lifetime is caller-controlled; with
`preserve_partial_decoding` the frames stay alive after `RC_FAIL` until the
caller resets, so partially decoded structures remain inspectable.
Expand Down Expand Up @@ -202,6 +218,17 @@ pass the existing suites.
- 2026-11-26: converter-example/PDU helpers deferred until all decoders are
onboarded.
- 2026-11-26: sentinel value `(unsigned)-1` for `ctx_offset`.
- 2026-11-26: Phases 1-4 landed as one change: `xer_decode_ctx`,
`jer_decode_ctx`, `oer_decode_ctx`, `uper_decode_ctx`, `aper_decode_ctx`,
`cbor_decode_ctx` all follow the `ber_decode_ctx` pattern (always stack-copy
the codec context, set `ext_struct_ctx`, dispatch). `uper_decode` /
`aper_decode` were split into a static `*_impl` taking the arena so the
plain entry points keep their exact former behavior. Decoder headers
declare the arena parameter as `struct asn_struct_external_ctx_s *` (the
tag forward-declared in `asn_application.h`), because `uper_decoder.h` /
`aper_decoder.h` are included from `asn_internal.h` before the typedef in
`constr_TYPE.h` is visible. PER/CBOR plain entry points intentionally still
decode `-fno-ctx-members` output (they never fetch ctx at decode time).

## References

Expand Down
38 changes: 28 additions & 10 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,25 @@ static int emit_collection_constraint(arg_t *arg, asn1p_expr_t *expr);
#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))

#define PCTX_DEF INDENTED( \
OUT("\n"); \
OUT("/* Context for parsing across buffer boundaries */\n"); \
OUT("asn_struct_ctx_t _asn_ctx;\n"));
#define PCTX_DEF do { \
if(!(arg->flags & A1C_NO_CTX_MEMBERS)) { \
INDENTED( \
OUT("\n"); \
OUT("/* Context for parsing across buffer boundaries */\n"); \
OUT("asn_struct_ctx_t _asn_ctx;\n")); \
} \
} while(0)

/*
* Emit the ctx_offset field of a type's specifics: the embedded member's
* offset, or the external sentinel under -fno-ctx-members.
*/
#define OUT_CTX_OFFSET() do { \
if(arg->flags & A1C_NO_CTX_MEMBERS) \
OUT("ASN_CTX_OFFSET_EXTERNAL,\n"); \
else \
OUT("offsetof(%s, _asn_ctx),\n", c_name(arg).full_name); \
} while(0)


#define DEPENDENCIES do { \
Expand Down Expand Up @@ -735,7 +750,7 @@ asn1c_lang_C_type_SEQUENCE_def(arg_t *arg, asn1c_ioc_table_and_objset_t *opt_ioc
MKID(expr), expr->_type_unique_index);
INDENT(+1);
OUT("sizeof(%s),\n", c_name(arg).full_name);
OUT("offsetof(%s, _asn_ctx),\n", c_name(arg).full_name);
OUT_CTX_OFFSET();
emit_tag2member_reference(arg, expr, tag2el_count);
if(roms_count + aoms_count) {
OUT("asn_MAP_%s_oms_%d,\t/* Optional members */\n",
Expand Down Expand Up @@ -896,7 +911,7 @@ emit_set_specifics(arg_t *arg, asn1p_expr_t *expr, int tag2el_count,
MKID(expr), expr->_type_unique_index);
INDENTED(
OUT("sizeof(%s),\n", c_name(arg).full_name);
OUT("offsetof(%s, _asn_ctx),\n", c_name(arg).full_name);
OUT_CTX_OFFSET();
OUT("offsetof(%s, _presence_map),\n", c_name(arg).full_name);
emit_tag2member_reference(arg, expr, tag2el_count);
p = MKID(expr);
Expand Down Expand Up @@ -1269,8 +1284,11 @@ emit_set_extension_group_adapters(arg_t *arg, asn1p_expr_t *expr,
parent_id, unique_index, group_index);
INDENT(+1);
OUT("sizeof(%s),\t/* Group storage size */\n", group_names.full_name);
OUT("offsetof(%s, _asn_ctx),\t/* Group context offset */\n",
group_names.full_name);
if(arg->flags & A1C_NO_CTX_MEMBERS)
OUT("ASN_CTX_OFFSET_EXTERNAL,\t/* Group context offset */\n");
else
OUT("offsetof(%s, _asn_ctx),\t/* Group context offset */\n",
group_names.full_name);
OUT("0, 0,\t/* Tag map is not used by OER */\n");
if(optional_count) {
OUT("asn_MAP_%s_group_oms_%d_%d,\t/* Optional members */\n",
Expand Down Expand Up @@ -1707,7 +1725,7 @@ asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
MKID(expr), expr->_type_unique_index);
INDENTED(
OUT("sizeof(%s),\n", c_name(arg).full_name);
OUT("offsetof(%s, _asn_ctx),\n", c_name(arg).full_name);
OUT_CTX_OFFSET();
{
int as_xvl = expr_as_xmlvaluelist(arg, v);
OUT("%d,\t/* XER encoding is %s */\n",
Expand Down Expand Up @@ -2178,7 +2196,7 @@ asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
expr->_type_unique_index);
INDENTED(
OUT("sizeof(%s),\n", c_name(arg).full_name);
OUT("offsetof(%s, _asn_ctx),\n", c_name(arg).full_name);
OUT_CTX_OFFSET();
OUT("offsetof(%s, present),\n", c_name(arg).full_name);
OUT("sizeof(((%s *)0)->present),\n", c_name(arg).full_name);
emit_tag2member_reference(arg, expr, tag2el_count);
Expand Down
7 changes: 7 additions & 0 deletions libasn1compiler/asn1compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ enum asn1c_flags {
A1C_TOLERANT_UTC_TIME = 0x2000000000,
/* --strict-mode: standards policy shared by all codecs and targets. */
A1C_STRICT_MODE = 0x4000000000,
/*
* -fno-ctx-members
* Do not embed the asn_struct_ctx_t _asn_ctx member into generated
* structures; emit ASN_CTX_OFFSET_EXTERNAL instead. Decode such code
* via the *_decode_ctx() entry points with an external context.
*/
A1C_NO_CTX_MEMBERS = 0x8000000000,
};

/*
Expand Down
3 changes: 1 addition & 2 deletions skeletons/ANY.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ ANY_copy(const asn_TYPE_descriptor_t *td, void **aptr, const void *bptr) {
? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_ANY_specs;
if(specs->struct_size < sizeof(ANY_t)
|| specs->ctx_offset > specs->struct_size
|| sizeof(asn_struct_ctx_t) > specs->struct_size - specs->ctx_offset) {
|| !asn_ctx_offset_valid(specs->struct_size, specs->ctx_offset)) {
return -1;
}
destination = (ANY_t *)*aptr;
Expand Down
5 changes: 3 additions & 2 deletions skeletons/OCTET_STRING.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
specs = td->specifics
? (const asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_SPC_OCTET_STRING_specs;
ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
ctx = specs->ctx_offset != ASN_CTX_OFFSET_EXTERNAL
? (asn_struct_ctx_t *)((char *)st + specs->ctx_offset) : 0;

/*
* Remove decode-time stack.
*/
struct _stack *stck;
stck = (struct _stack *)ctx->ptr;
stck = ctx ? (struct _stack *)ctx->ptr : 0;
if(stck) {
while(stck->tail) {
struct _stack_el *sel = stck->tail;
Expand Down
7 changes: 4 additions & 3 deletions skeletons/OCTET_STRING_ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,8 @@ OCTET_STRING__valid_ber_descriptor(
: offsetof(OCTET_STRING_t, _asn_ctx);
minimum_size = context_offset + sizeof(asn_struct_ctx_t);
if(specs->struct_size < minimum_size
|| specs->ctx_offset < context_offset
|| specs->ctx_offset > specs->struct_size - sizeof(asn_struct_ctx_t))
|| (specs->ctx_offset != ASN_CTX_OFFSET_EXTERNAL && (specs->ctx_offset < context_offset
|| specs->ctx_offset > specs->struct_size - sizeof(asn_struct_ctx_t))))
return 0;
if(td->tags_count > BER_EXPLICIT_TAG_DEPTH_LIMIT
|| td->all_tags_count > BER_EXPLICIT_TAG_DEPTH_LIMIT
Expand Down Expand Up @@ -1079,7 +1079,8 @@ OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
state.st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!state.st) return octet_string_ber_result(&state, RC_FAIL);
}
state.ctx = (asn_struct_ctx_t *)((char *)state.st + specs->ctx_offset);
state.ctx = asn_get_struct_ctx(opt_codec_ctx, state.st, specs->ctx_offset);
if(!state.ctx) return octet_string_ber_result(&state, RC_FAIL);
return octet_string_ber_run(&state, opt_codec_ctx);
}

Expand Down
3 changes: 2 additions & 1 deletion skeletons/OCTET_STRING_jer.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,8 @@ OCTET_STRING__decode_jer(
}

/* Restore parsing context */
ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, *sptr, specs->ctx_offset);
if(!ctx) { asn_dec_rval_t _fr = {RC_FAIL, 0}; return _fr; }

return jer_decode_general(opt_codec_ctx, ctx, *sptr,
buf_ptr, size,
Expand Down
3 changes: 2 additions & 1 deletion skeletons/OCTET_STRING_xer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,8 @@ OCTET_STRING__decode_xer(
}

/* Restore parsing context */
ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, *sptr, specs->ctx_offset);
if(!ctx) { asn_dec_rval_t _fr = {RC_FAIL, 0}; return _fr; }

return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
buf_ptr, size,
Expand Down
35 changes: 31 additions & 4 deletions skeletons/aper_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ aper_decode_complete(const asn_codec_ctx_t *opt_codec_ctx,
return rval;
}

asn_dec_rval_t
aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
static asn_dec_rval_t
aper_decode_impl(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td, void **sptr,
asn_struct_external_ctx_t *ext_ctx,
const void *buffer,
size_t size, int skip_bits, int unused_bits) {
asn_codec_ctx_t s_codec_ctx;
asn_dec_rval_t rval;
Expand All @@ -74,7 +76,7 @@ aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
* must be allocated on the stack.
*/
if(opt_codec_ctx) {
if(opt_codec_ctx->max_stack_size) {
if(opt_codec_ctx->max_stack_size || ext_ctx) {
s_codec_ctx = *opt_codec_ctx;
opt_codec_ctx = &s_codec_ctx;
}
Expand All @@ -84,6 +86,8 @@ aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
opt_codec_ctx = &s_codec_ctx;
}
if(ext_ctx)
s_codec_ctx.ext_struct_ctx = ext_ctx;

/* Fill in the position indicator */
memset(&pd, 0, sizeof(pd));
Expand Down Expand Up @@ -121,3 +125,26 @@ aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
}
return ASN__STRICT_DECODE_RESULT(td, sptr, ATS_ALIGNED_BASIC_PER, rval);
}

asn_dec_rval_t
aper_decode(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td, void **sptr, const void *buffer,
size_t size, int skip_bits, int unused_bits) {
return aper_decode_impl(opt_codec_ctx, td, sptr, NULL,
buffer, size, skip_bits, unused_bits);
}

/*
* aper_decode() variant carrying an external parsing-context arena
* (code generated with -fno-ctx-members). See aper_decoder.h.
* WARNING: Like aper_decode(), this call returns the number of BITS read.
*/
asn_dec_rval_t
aper_decode_ctx(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td, void **sptr,
asn_struct_external_ctx_t *ext_ctx,
const void *buffer,
size_t size, int skip_bits, int unused_bits) {
return aper_decode_impl(opt_codec_ctx, td, sptr, ext_ctx,
buffer, size, skip_bits, unused_bits);
}
Loading