Skip to content
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

wasm-split: Add fuzzer support #7014

Merged
merged 11 commits into from
Oct 18, 2024
Merged

Conversation

kripken
Copy link
Member

@kripken kripken commented Oct 16, 2024

The support is added but not enabled as this is still finding bugs, but I'd
like to land it now as it will conflict with other work I am doing.

The first part here is to add Split testcase handler to the fuzzer,
which runs a wasm, then runs it again after splitting it and then
linking it at runtime, and checking for different results.

The second part is support for linking two modules at runtime
in the fuzzer's JS code, that works in tandem with the first part.
New options are added to load and link a second wasm, and to
pick which exports to run.

@kripken kripken requested a review from tlively October 16, 2024 22:41
# get the list of function names, some of which we will decide to split
# out
wat = run([in_bin('wasm-dis'), wasm] + FEATURE_OPTS)
all_funcs = re.findall(r'\n [(]func [$](\S+)', wat)
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, I haven't seen square brackets used as a form of escape before. I think backslashes would work, too, and might be less surprising. It also might be good to stash this regex in compiled form in a global.

Copy link
Member Author

Choose a reason for hiding this comment

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

Compiling the regex sgtm. About clarity, though, I feel that [(] is better than \( because the slash is similar to \S etc. where it is not escaping anything. The list of things that need escaping differs between regex impls AFAIK, but doing [X] always works.

Comment on lines +1408 to +1412
# find the names of the exports. we need this because when we split the
# module then new exports appear to connect the two halves of the
# original module. we do not want to call all the exports on the new
# primary module, but only the original ones.
exports = []
Copy link
Member

Choose a reason for hiding this comment

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

It might be simpler to use the --export-prefix option to add a unique prefix to the new exports that we can filter out directly in the JS wrapper.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, then we'd need to hardcode some special prefix in the JS wrapper, but I'm not sure there is a fixed prefix we can use: we don't want to overlap with existing export names.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW Emscripten uses % as the prefix. Maybe we can just choose an arbitrary one like that and it would be good enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the difference is that Emscripten has full control over the export names. In the fuzzer we do want to be able to fuzz initial content from anywhere. I suppose we could sanitize that content before fuzzing it, but that seems more complicated to me.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, the currently solution LGTM, then.

opts = ['-O3']
new_name = name + '.opt.wasm'
run([in_bin('wasm-opt'), name, '-o', new_name, '-all'] + opts + split_feature_opts)
nonlocal optimized
Copy link
Member

Choose a reason for hiding this comment

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

Neat, I've never seen nonlocal before.

Comment on lines 169 to 176
if (secondBinary) {
imports['placeholder'] = new Proxy({}, {
get(target, prop, receiver) {
// Return a function that does an indirect call using the exported table.
return (...args) => exports['table'].get(+prop)(...args);
}
});
}
Copy link
Member

Choose a reason for hiding this comment

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

This proxy can just throw an error if it is ever called. Since the secondary module is eagerly loaded, it should be impossible to end up in a placeholder function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point!

Comment on lines 198 to 202
var combinedImports = Object.assign({}, imports);
combinedImports['primary'] = {};
for (var e in exports) {
combinedImports['primary'][e] = exports[e];
}
Copy link
Member

Choose a reason for hiding this comment

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

This can just be something like var combinedImports = {'primary': exports};. See for example https://github.com/emscripten-core/emscripten/blob/bdde3cd337ed4ac72a641f862b5371048e049946/src/preamble.js#L686.

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, thanks, I didn't realize wasm-split routes all secondary module imports through the primary module... does that not add any inefficiency? I guess not as the primary module just imports and exports them, unchanged.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I don't think it should have any overhead.

Copy link
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

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

LGTM if we decide not to use --export-prefix.

@kripken kripken merged commit 679c26f into WebAssembly:main Oct 18, 2024
13 checks passed
@kripken kripken deleted the fuzz.split.TRUE branch October 18, 2024 19:36
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.

2 participants