Skip to content

Conversation

@stevenfontanella
Copy link
Member

@stevenfontanella stevenfontanella commented Dec 11, 2025

ctor-eval doesn't have access to imports and previously didn't have any special handling for this. Globals would fail to import, which would prevent further evaluation. This is why global-get-init.wast didn't optimize away even though it does nothing.

We already stubbed out imports to "env". Change the code to create a stub module for all modules named in imports, so that instantiation is valid and evaluation can continue. This also unblocks #8086 which checks imports and requires them to exist during instantiation.

@stevenfontanella stevenfontanella marked this pull request as ready for review December 11, 2025 18:40

ModuleUtils::iterImports(
wasm,
[&modules](std::variant<Memory*, Table*, Global*, Function*, Tag*> import) {
Copy link
Member

Choose a reason for hiding this comment

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

What is the advantage of this variant-based visiting, replacing iterImported* functions with one iterImports? (I'm not opposed to it, necessarily, I just don't see an obvious reason so I think I may be missing something.)

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not a big difference, I mainly did this because there is some work in common for all imports (the first few lines of this function). If we were to use the iterImported* functions, we'd need to introduce a helper function and call it in the body of each one. Also this way forces us to deal with all types of imports which makes it clear to the reader which ones are handled (e.g. Tags are not handled which was implicit before but is explicit now which I think is a small plus).

Copy link
Member

Choose a reason for hiding this comment

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

Why don't we want to handle tags? That seems like it was probably an accidental omission before. (Or do we not have a hook for aborting the execution if an imported tag is used? In that case maybe aborting early in the presence of imported tags is intentional.)

Copy link
Member

Choose a reason for hiding this comment

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

Good question about tags @tlively , but @stevenfontanella otherwise that all sounds good, thanks for the explanation.

Copy link
Member Author

Choose a reason for hiding this comment

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

My impression is that ctor-eval won't work at all on any ctor that references an imported tag: https://github.com/WebAssembly/binaryen/blob/main/src/tools/wasm-ctor-eval.cpp#L327. I guess this is the best we can do unless the tag is somehow referenced in a way that doesn't matter at all? Or as you mentioned, if the same tag is used to throw and catch, then it doesn't matter what the imported value was.


ModuleUtils::iterImports(
wasm,
[&modules](std::variant<Memory*, Table*, Global*, Function*, Tag*> import) {
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we want to handle tags? That seems like it was probably an accidental omission before. (Or do we not have a hook for aborting the execution if an imported tag is used? In that case maybe aborting early in the presence of imported tags is intentional.)

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

lgtm with test suggestions

@stevenfontanella stevenfontanella enabled auto-merge (squash) December 14, 2025 02:18
@stevenfontanella stevenfontanella merged commit bc7632d into main Dec 14, 2025
17 checks passed
@stevenfontanella stevenfontanella deleted the ctor-import branch December 14, 2025 02:49
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.

4 participants