Repository navigation
Bugfixes in handling of table initializers and element declarations - #30
Merged
cfallin merged 8 commits intoNov 16, 2026
Merged
Conversation
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.
The backend emitted one active element segment for each non-null table entry, so a module whose table was initialized by one segment of n entries came back with n single-entry segments. The table contents are the same, but each segment repeats the segment header in the binary and is applied separately at instantiation. Emit one active segment for each run of consecutive non-null entries instead. Null entries are still not emitted. The runs go through `segment_layout`, which pads the element segment index space to the input's element segment count, so an unexpanded `elem.drop` or `table.init` that names an active segment's index still validates when merging lowers the number of active segments.
…nt overwrites them. A `ref.func` in a function body validates only if the module declares the function, for example as an element segment item or an export. The frontend applied each active element segment to `TableData::func_elements` and kept only the final contents of each table, and the backend emits active segments only from those contents. A function whose only declaration was a table entry that a later active segment overwrote, with another function or with `ref.null`, was then declared nowhere in the output, and a `ref.func` of it failed validation with "undeclared function reference". This happened for unexpanded and expanded function bodies. Record each overwritten entry in the new `Module::overwritten_elements`. The backend writes each recorded function that no table's final contents, passive or declarative segment or function export contains into its entry, in active segments placed before the segments for the final contents. Entries with consecutive indices share a segment. An entry whose final content is null is written with `ref.null` after them, so the tables' final contents do not change. Records whose entry is not within the table's `func_elements` are ignored. These are active segments, so they fill the element segment indices that passive and declarative segments leave free, like the segments for the final contents.
The frontend ignored a table's initializer expression, such as `(table 1 funcref (ref.func 0))`, and the backend emitted the table without one. The output's entries started as null instead of the initializer's value, a table of a non-nullable type failed validation with "non-defaultable element type", and a function declared only by the initializer failed validation with "undeclared function reference" when code used `ref.func` of it. Record the initializer in the new `TableData::initializer` field and emit tables that have one with `table_with_init`. `ref.func` and `ref.null` initializers are supported. Every other initializer expression returns `FrontendError::UnsupportedFeature`. For a `ref.func` initializer, the frontend fills `func_elements` with the function for every index below the table's initial size before element segments are applied, so `func_elements` holds the table's initial contents. The backend emits an element segment entry only for entries that differ from their value before element segments are applied, and emits `ref.null` for a null entry that replaces an initializer's function. The segments for overwritten functions use the same rule. An entry that a recorded function is written into is restored to its final content when that content equals the entry's value before element segments are applied, because the final-content segments skip it. Before, only null entries were restored, so an entry that went from the initializer's function to another function and back read as the other function. The initializer's function counts as declared, so overwriting it produces no record segment. Tables without an initializer produce the same output as before.
…nt index. Unexpanded function bodies name element segments by their original index in `table.init`, and validation requires the segment's element type to be a subtype of the table's element type. The backend filled the indices of the input's active segments with the segments for the tables' contents in table order, regardless of element type, and padded the remaining ones with empty `funcref` segments. A `table.init` into a typed function reference table, or into an `externref` table once waffle keeps that type, then named a `funcref` segment and failed validation with "type mismatch". Record the index, table and element type of each active segment of the input in the new `Module::active_element_segments`. The backend places a segment for table contents only at a recorded index of its table's element type. A recorded index that no such segment takes gets an empty active segment at offset 0 of the recorded table, with the recorded element type. Segments that no index takes follow the original segment count. The segments of one table keep their order. Free indices without a record keep the previous padding. For modules whose active segments are all `funcref` segments, segments are placed as before. An empty segment at the index of an input segment on a table other than table 0 now writes to that table instead of table 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a set of three commits (stacked on #29) that fix a bunch of issues with the common theme of "Waffle is dropping something it shouldn't":
These were found with a short fuzzing campaign after fixing the fuzzing setup (with those fixes coming separately in a bit.)