Skip to content

Preserve passive and declarative segments at their original index - #28

Merged
cfallin merged 4 commits into
bytecodealliance:mainfrom
tschneidereit:preserve-passive-segments
Nov 16, 2026
Merged

cfallin merged 4 commits into
bytecodealliance:mainfrom
tschneidereit:preserve-passive-segments

Conversation

@tschneidereit

Copy link
Copy Markdown
Member

Waffle currently drops passive and declarative segments. That's fine for many WASIp2 components, but not for WASIp3 ones, which contain at least the __wasm_init_tls and __wasm_init_memory passive segments.

This set of patches makes Waffle retain these segments with their original index, inserting padding where needed. That way, code referencing the segments doesn't have to be updated.

The items of an element segment are parsed into a list of `Func`s
inline in the active-segment case. Move that code into
`parse_element_items` so that passive and declarative segments can
use it as well. No functional change.
An element segment item that is a constant expression other than
`ref.func` or `ref.null`, such as `global.get`, made the frontend
panic. Return `FrontendError::UnsupportedFeature` instead, as the
frontend does for other constructs it does not support.
The frontend dropped passive data segments and passive and declarative
element segments, and the backend never emitted a DataCount section.
Function bodies that are not expanded are copied byte for byte, so a
`memory.init`, `data.drop`, `table.init` or `elem.drop` in such a body
then referred to a different segment or to one that no longer existed,
a module using `memory.init` or `data.drop` failed validation because
DataCount was missing, and a `ref.func` of a function that only a
declarative segment declared failed validation as well.

Record these segments in the module with their original segment
index: `Module::passive_data_segments` holds passive data segments and
`Module::element_segments` holds passive and declarative element
segments. `Module::has_data_count` records whether the input had a
DataCount section, and `Module::orig_data_segment_count` and
`Module::orig_element_segment_count` record how many segments of each
kind the input had.

The backend lays out each segment index space so that every recorded
segment lands at its original index. Active segments fill the
remaining indices in order. Instantiation drops each active segment
after applying it, so code that refers to an active data segment's
index behaves the same whichever active data segment occupies it, or
if an empty data segment occupies it. The same holds for element
segments of the same element type. When there are fewer active segments
than free indices below the highest recorded index or below the input's
segment count, empty segments fill the rest, so every index the input
had stays valid. Active segments beyond that are appended. Data
segments are padded to the input's count only when the input had a
DataCount section. Validation requires that section for every
instruction that names a data segment, so code in an input without one
names no data segment.

Empty data segments are passive. Empty element segments are active
segments of no entries at offset 0 of table 0, in the 1.0 encoding,
when table 0 is a `funcref` table, and passive segments otherwise.
Instantiation applies an empty active segment without writing an entry
or trapping, for any table size, and then drops it. Code that names its
index therefore behaves as it does for an empty passive segment. For
every input that validates under WebAssembly 1.0 or 2.0, the output
requires bulk memory only when the input did. A module that uses
reference types without bulk memory and has a table 0 that is not a
`funcref` table still gets passive padding.

Empty element segments are `funcref` segments. A `table.init` into a
typed function reference table that names such an index, or an index
now holding an active segment of another element type, fails
validation.

A DataCount section is emitted when the module has a passive data
segment or the input had a DataCount section.
`ModuleDisplay` printed table contents and active data segments only.
Print `Module::passive_data_segments` and `Module::element_segments`
as well, in the same format.
@tschneidereit
tschneidereit force-pushed the preserve-passive-segments branch from 0b3d72b to 1518cec Compare November 11, 2026 23:23

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@cfallin
cfallin merged commit ee293f6 into bytecodealliance:main Nov 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants