-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix(wasmtime):Config
methods should be idempotent
#4252
fix(wasmtime):Config
methods should be idempotent
#4252
Conversation
7db2afb
to
4227ed2
Compare
Sorry for this late fix. |
4227ed2
to
e212d86
Compare
Subscribe to Label Actioncc @peterhuene
This issue or pull request has been labeled: "wasmtime:api", "wasmtime:config", "wasmtime:docs"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
Label Messager: wasmtime:configIt looks like you are changing Wasmtime's configuration options. Make sure to
To modify this label's message, edit the To add new label messages or remove existing label messages, edit the |
e212d86
to
3f57e76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a bunch for this -- I think it moves the Config
API in a clearer direction and eliminates some potential confusion!
I've added some thoughts below on the doc-comments, mostly, because getting these right is important for the public config interface; but I think the overall strategy is good (modulo one thought on the reftypes/bulk-memory options below).
cc @alexcrichton for thoughts on this as well, want to make sure you're also happy with this change...
pub unsafe fn cranelift_flag_enable(&mut self, flag: &str) -> Result<&mut Self> { | ||
self.compiler.enable(flag)?; | ||
Ok(self) | ||
pub unsafe fn cranelift_flag_enable(&mut self, flag: &str) -> &mut Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In place of the "Errors" section in the old doc comment, we should note here that flags are validated when the engine is built, and so any errors will be deferred, I think. Likewise elsewhere where an "Errors" section is removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, except for the strategy
method, because it will not return error in any situation (previously and now).
/// # Errors | ||
/// | ||
/// This method can fail if the `config` is invalid or some | ||
/// configurations are incompatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we say a bit more here? Something about how some Wasm features require certain compiler features, and they will turn on the relevant compiler features by default, but explicitly disabling some compiler features or setting them in an incompatible way may cause errors. I think we should also note a guarantee/promise that default compiler options (i.e., if the user does not add any specific setting overrides) will always work if the underlying hardware supports what is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed about expanding the error documentation a bit here, but I also think it would be best to keep documentation on the original config methods as well. For example Config::cranelift_flag_set
could retain some documentation indicating that if an invalid setting is specified that the error will pop out during Engine::new
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we say a bit more here? Something about how some Wasm features require certain compiler features, and they will turn on the relevant compiler features by default, but explicitly disabling some compiler features or setting them in an incompatible way may cause errors.
Fixed.
I think we should also note a guarantee/promise that default compiler options (i.e., if the user does not add any specific setting overrides) will always work if the underlying hardware supports what is needed.
I don't think it necessary to add this note explicitly, because the default options should work is intuition for users, and we have enough information in the errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On https://docs.wasmtime.dev/c-api/config_8h.html#ada735dd95cbee6854fc6bb27dc9591c6 it says:
If the compilation strategy selected could not be enabled then an error is returned.
But the signature is:
void wasmtime_config_strategy_set(wasm_config_t *, wasmtime_strategy_t)
I've been looking everywhere in the docs to try and find what happened to config errors, I don't think it is very clear today without looking into this PR.
Also, we use wasm_engine_new_with_config
to instantiate our engine with wasm.h
; where is the error returned from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for your remind.
I changed the doc and the signature of the rust code, but I forgot the doc for c.
I've submitted #4550 to fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PureWhiteWu That's great! Very prompt, thank you!
A question still remains on my end but I am not sure if here's the right place for it, I'll shoot it again though:
Since wasm.h
's wasm_engine_new_with_config
does not return any error, where are the engine initialization errors reported when using the C API? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not familiar with the c api, @alexcrichton do you know about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can take a look at the implementation and right now it looks like no error handling happens and if something otherwise would result in an error it would cause the process to abort.
This is probably something that would be good to fix, returning a null pointer there and perhaps adding a Wasmtime-specific API for the same operation but one that returns an error message.
3f57e76
to
9ea619a
Compare
Subscribe to Label Actioncc @fitzgen, @peterhuene
This issue or pull request has been labeled: "fuzzing", "wasmtime:c-api"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also looks great to me, thanks for working on this!
I personally really like the idea of deferring work from Config
to Engine::new
as much as possible. I think there's a number of other options we could apply this to as well, but I'm happy to follow up with these in a later PR or tracking issue, there's no need to tackle them here right now unelss you're motivated:
Config::profiler
could defer creation of the profiling agent toEngine
-construction. This would actually also remove theArc
around it since there's no more need to clone it.Config::cache_config_load
could defer its I/O toEngine::new
.- Size normalization in methods like
Config::{static,dynamic}_memory_guard_size
could instead happen inEngine::new
instead of as a side effect of calling the configuration method.
Finally, if you're feeling even more ambitious (sorry I keep seeing these things and figure I should write them down, but don't feel like you're required to do any of them) the CompilerConfig
type might actually be good to move to wasmtime_environ
. The current CompilerBuilder
trait could be removed entirely in favor of a construction function in the wasmtime_cranelift
crate along the lines of fn build_compiler(settings: &wasmtime_environ::CompilerConfig) -> Result<Box<dyn wasmtime_environ::Compiler>>
or something like that. Basically instead of mirroring hash maps in Config
into hash maps stored in cranelift we'd just pass cranelift all the hash maps directly to get processed. Again though this is an orthogonal refactoring that's totally ok to leave to later.
/// # Errors | ||
/// | ||
/// This method can fail if the `config` is invalid or some | ||
/// configurations are incompatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed about expanding the error documentation a bit here, but I also think it would be best to keep documentation on the original config methods as well. For example Config::cranelift_flag_set
could retain some documentation indicating that if an invalid setting is specified that the error will pop out during Engine::new
here.
9ea619a
to
fb584d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review!
pub unsafe fn cranelift_flag_enable(&mut self, flag: &str) -> Result<&mut Self> { | ||
self.compiler.enable(flag)?; | ||
Ok(self) | ||
pub unsafe fn cranelift_flag_enable(&mut self, flag: &str) -> &mut Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, except for the strategy
method, because it will not return error in any situation (previously and now).
/// # Errors | ||
/// | ||
/// This method can fail if the `config` is invalid or some | ||
/// configurations are incompatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we say a bit more here? Something about how some Wasm features require certain compiler features, and they will turn on the relevant compiler features by default, but explicitly disabling some compiler features or setting them in an incompatible way may cause errors.
Fixed.
I think we should also note a guarantee/promise that default compiler options (i.e., if the user does not add any specific setting overrides) will always work if the underlying hardware supports what is needed.
I don't think it necessary to add this note explicitly, because the default options should work is intuition for users, and we have enough information in the errors.
7d83e3c
to
0ba39e2
Compare
@alexcrichton Thanks for your review and suggestions! |
This commit refactored `Config` to use a seperate `CompilerConfig` field instead of operating on `CompilerBuilder` directly to make all its methods idempotent. Fixes bytecodealliance#4189
0ba39e2
to
a7b00f7
Compare
This has been done.
For this part, I think the entire This may be not be a minor change, so I will think this may need another pr. I've also submitted a tracking issue #4257 for this.
I don't think defer these things to And for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me, thanks again @PureWhiteWu!
Thank you for your patient instructions and review! |
This commit fixes the .NET `Config` API to conform to the latest upstream Wasmtime's C API signatures. See bytecodealliance/wasmtime#4252 for the upstream change.
In bytecodealliance#4252 I changed the signature and doc of the rust code, but forgot to change the doc for c. Set strategy no longer returns error. This commit fixes that.
In #4252 I changed the signature and doc of the rust code, but forgot to change the doc for c. Set strategy no longer returns error. This commit fixes that.
This commit refactored
Config
to use a seperateCompilerConfig
field instead of operating onCompilerBuilder
directly to make all its methods idempotent.Fixes #4189