Skip to content

Emit declarative element segments for ref.func references in WasmGC#13156

Open
zacharywhitley wants to merge 1 commit intooracle:masterfrom
tegmentum:fix/declarative-elem-segments
Open

Emit declarative element segments for ref.func references in WasmGC#13156
zacharywhitley wants to merge 1 commit intooracle:masterfrom
tegmentum:fix/declarative-elem-segments

Conversation

@zacharywhitley
Copy link

Summary

The WebAssembly spec requires that functions referenced by ref.func outside of active/passive element segments must be declared in a declarative element segment (elem declare func ...). The WasmGC backend uses ref.func in heap initialization code (DynamicHub newInstance and clone function pointers) but never emits the required declarations.

This causes validation failures with strict validators:

  • wasm-tools validate reports "undeclared function reference"
  • wasmtime rejects modules assembled by wasm-tools parse
  • Binaryen's wasm-as silently adds declarations, masking the issue

Changes

  • Add declarativeFuncRefs set to WasmModule to track functions needing declarative element segments
  • Add RefFuncCollector visitor in WasmPrinter that scans all function bodies, globals, and table initializers for ref.func instructions
  • Emit (elem declare func ...) in the WAT output with all collected references

The fix is general — it handles any current or future ref.func usage automatically, not just the heap initialization paths.

Test plan

  • Existing WasmGC tests pass (no behavioral change — Binaryen already handled this implicitly)
  • wasm-tools validate --features all passes on WasmGC output
  • wasm-tools parse successfully assembles WasmGC WAT to binary

@oracle-contributor-agreement
Copy link

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label Mar 17, 2026
The WebAssembly spec requires that functions referenced by ref.func
outside of active/passive element segments must be declared in a
declarative element segment (elem declare func ...).

Add a RefFuncCollector visitor that scans all function bodies and
global initializers for ref.func instructions, then emits a single
declarative element segment in the WAT output.

This fixes validation errors from wasm-tools validate and wasmtime
("undeclared function reference") that occurred because the WasmGC
heap initialization code uses ref.func for DynamicHub newInstance
and clone function pointers without declaring them.
@zacharywhitley zacharywhitley force-pushed the fix/declarative-elem-segments branch from cf05b8d to 91ac152 Compare March 17, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Required At least one contributor does not have an approved Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant