Skip to content

Allow initializer expressions to reference defined immutable globals - #2806

Open
ANAMASGARD wants to merge 3 commits into
WebAssembly:mainfrom
ANAMASGARD:fix/element-segment-global-get-validation
Open

Allow initializer expressions to reference defined immutable globals#2806
ANAMASGARD wants to merge 3 commits into
WebAssembly:mainfrom
ANAMASGARD:fix/element-segment-global-get-validation

Conversation

@ANAMASGARD

Copy link
Copy Markdown

Fixes #2526

Problem

WABT currently rejects global.get of any non-imported global in initializer expressions. This prevents element/data segment offsets and global initializers from referencing immutable globals defined earlier in the same module.

The current WebAssembly validation rules allow global.get to reference a previously defined immutable global. The referenced global must still be immutable and previously defined; self-references, forward references, invalid indexes, and wrong types remain invalid.

Fix

Remove the imported-global restriction from SharedValidator::OnGlobalGet while preserving the mutable-global check.

Split global handling into OnGlobalType (declaration validation via CheckReferenceType) and OnGlobal (publication into globals_). Validate each initializer before publishing the global so it is visible to later globals but not to itself. Failed type checks do not publish the global.

Apply the same ordering semantics to both Validator::CheckModule and BinaryReaderInterp.

Remove the now-unused num_imported_globals_ bookkeeping.

Tests

Add regression coverage in test/regress/issue-2526.txt for:

  • element segment offsets using a defined immutable global (runtime-observable via call_indirect → 42)
  • data segment offsets using a defined immutable global (runtime-observable via i32.load8_u → 97)
  • global initializers referencing an earlier immutable global
  • global chaining ($a$b$c)
  • self-references, forward references, mutable globals, and invalid offset types

Update affected spec-test expectations using minimal patched snapshots under test/old-spec/ (core global/elem/data plus proposal suites) and extend test/update-spec-tests.py to preserve those overrides during regeneration.

Test plan

  • ./test/run-tests.py issue-2526
  • ./test/run-tests.py spec/global spec/elem spec/data
  • ./test/run-tests.py wasm2c/spec/global wasm2c/spec/elem wasm2c/spec/data
  • cmake --build out --target run-unittests
  • cmake --build out --target run-c-api-tests
  • cmake --build out --target run-tests
  • Feature flags: default, --enable-all, --enable-extended-const on valid repro
  • Self-reference rejected by wasm-validate / wasm-interp paths

ANAMASGARD and others added 3 commits August 6, 2026 15:12
The validator rejected global.get of any non-imported global inside
initializer expressions (elem/data offsets and global initializers),
matching an old spec restriction. The spec now allows referencing
defined immutable globals; only mutable globals remain forbidden.

Remove the imported-only check in SharedValidator::OnGlobalGet and
publish each defined global to the validator only after its initializer
has been validated, so a global is visible to later globals but not to
itself (self- and forward references stay rejected). Apply the same
ordering fix in both validator.cc and BinaryReaderInterp.

Update spec goldens via vendored copies of elem/data/global under
test/old-spec (the pinned testsuite predates the spec change) and add
test/regress/issue-2526.txt.

Signed-off-by: Gaurav Chaudhary <chaudharygaurav2004@gmail.com>
Fixes WebAssembly#2526 by removing the obsolete imported-global restriction while
preserving mutable-global rejection and publish-after-init ordering in
both Validator and BinaryReaderInterp.

Split OnGlobalType from OnGlobal so declaration validation runs before
init-expr validation without delaying CheckReferenceType. Gate publication
on a successful type check.

Add runtime-observable regressions (call_indirect, memory load, global
chain), minimal old-spec patches for core/proposal suites, and teach
update-spec-tests.py about old-spec overrides.
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.

Incorrect Validation for Element Segment Initialization

1 participant