Skip to content

Support duplicate imports by default in wasm-tools component new #2187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

alexcrichton
Copy link
Member

This commit is a refinement to the support added in #2145 to support duplicate imports by default in wasm-tools, namely by avoiding changing any binary offsets in a module except for the import section. This implements a strategy of renaming imports in the module that goes into a component to be unique. Classification of what an import maps to is then ensured to use the original names, not the current names, in the module.

This commit is a refinement to the support added in bytecodealliance#2145 to support
duplicate imports by default in wasm-tools, namely by avoiding changing
any binary offsets in a module except for the import section. This
implements a strategy of renaming imports in the module that goes into a
component to be unique. Classification of what an import maps to is then
ensured to use the original names, not the current names, in the module.
@alexcrichton
Copy link
Member Author

cc @erikrose I had a long plane ride and figured I could give this a crack and I think it worked out pretty well but would be good to get your eyes on it too

Copy link
Contributor

@erikrose erikrose left a comment

Choose a reason for hiding this comment

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

So very nice to be able to do this unconditionally, without a flag. Thanks for taking a swing, Alex! I just noted a few things.

let mut new_name = import.name.to_string();
for i in 2.. {
new_name.truncate(import.name.len());
write!(new_name, " [v{i}]").unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do something more graceful about the (admittedly extreme) corner case of exceeding the spec'd 256-char import name? It seems like everything returns Results all the way up, so it should be just as easy to toss a descriptive error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh? Where did you see a 256-length limit? Wasmparser should have a limit of 100,000 as opposed to 256 to match web implementations. (or so we thought matched...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, excuse me: the max is 2^32. So, even more extreme a corner case.


;; define `f2` before the second `f` import shows up
(import "cm32p2" "f" (func $f_v1))
(import "cm32p2" "f2" (func $f2 (result i32)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should test somewhere whether we gracefully compensate for collisions of new names with existing imports. For example, what if we import "f", then "f [v2]", then another "f"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Heh this test was actually originally for that where I appended "N" to the end of the import instead of [v2]. The previous syntax, as seen here, can clash with actual names that show up in the component model. I then had the idea of using a syntax which is impossible to show up in the component model (e.g. a space and brackets) so there's actually no worry of collisions any more. I figured I'd leave the test though as it might as well serve for future-proofing purposes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I dig it.:-D Hadn't seen the more limited import/export names for components before.


;; Call both copies of the import. They should both end up pointed to the same one.
(func
call $f_v1
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there something specific we're testing by doing both a series of fs and a series of gs? I could just be too dopey to figure it out. :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah just wanted to make sure they were all referenced. In the original implementation of "just add N" to fully test the collision handling I needed to have different imports listed in different orders, but it's not really necessary any more but I figured I'd leave things.

@erikrose
Copy link
Contributor

LGTM!

@alexcrichton
Copy link
Member Author

@erikrose w.r.t. #2205 will it be problematic to remove the CLI option here? I was originally hoping to get this in before a release was made because then it's not a breaking change for the CLI, but it's something I want to consider if you're planning on integrating this elsewhere already.

@erikrose
Copy link
Contributor

Boy, I'd love to get this in. Is Right Now good for you? I juuuust submitted an internal PR to pin to that wit-component release, because we need its functionality for a goal we're chasing. If you merge this now and cut a release, I'll pin to that instead, and we can forget the CLI flag ever existed!

@alexcrichton
Copy link
Member Author

Yeah I'm happy to merge whenever but repo settings require 1+ approval from someone with write access, and 3/4 maintainers were on vacation last week so it may take a moment to work through the backlog to get to this.

No urgency from my part though, I'm happy to just leave the argument there but hide it from --help and/or make it a noop. Just wanted to confirm whether such logic was necessary or not.

@alexcrichton alexcrichton added this pull request to the merge queue May 27, 2025
Merged via the queue into bytecodealliance:main with commit f36c4b5 May 27, 2025
32 checks passed
@alexcrichton alexcrichton deleted the component-duplicate-imports branch May 27, 2025 21:05
@alexcrichton
Copy link
Member Author

@erikrose ok I've landed/published this as-is with dropping the CLI argument. If that causes issues though let me know and I can add it back in.

@erikrose
Copy link
Contributor

erikrose commented May 27, 2025 via email

@alexcrichton
Copy link
Member Author

Oh already done!

@erikrose
Copy link
Contributor

erikrose commented May 27, 2025 via email

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.

3 participants