-
Notifications
You must be signed in to change notification settings - Fork 824
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
Metering middleware. #1839
Metering middleware. #1839
Conversation
…make it work on compiler-llvm.
Thanks for bringing this over. Happy to test this work in an external project. Just ping me when you think this makes sense. |
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.
Looks pretty reasonable to me!
I'd like to see an example using metering too as the place where we explain how to use it, but that can be done separately after this merges
Cargo.toml
Outdated
@@ -23,6 +23,7 @@ wasmer-wasi = { version = "1.0.0-alpha5", path = "lib/wasi", optional = true } | |||
wasmer-wast = { version = "1.0.0-alpha5", path = "tests/lib/wast", optional = true } | |||
wasmer-cache = { version = "1.0.0-alpha5", path = "lib/cache", optional = true } | |||
wasmer-types = { version = "1.0.0-alpha5", path = "lib/wasmer-types" } | |||
wasmer-middlewares = { version = "1.0.0-alpha.5", path = "lib/middlewares", optional = true } |
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.
Slight typo in the version, should drop 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.
Done, thanks.
let mut module = (*compile_info.module).clone(); | ||
self.config.middlewares.apply_on_module_info(&mut module); | ||
compile_info.module = Arc::new(module); |
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 will leave all the old Arc<Module>
s unaffected, this should be looked at closely for bugs
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.
Indeed that was a source of the bug fixed in 363a28c of this PR. I'm trusting that the tests will catch any real problems here. (Also, there are no old Arc's? At least not in this function?)
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.
compile_info.module
before it gets overwritten is pointing to the old Arc, anything that it's shared with won't get the update
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.
Yeah I'm not sure about its correctness in context, but it's possible that this could cause problems later
lib/middlewares/src/metering.rs
Outdated
state.push_operator(Operator::GlobalGet { global_index: self.remaining_points_index.as_u32() }); | ||
state.push_operator(Operator::I64Const { value: self.accumulated_cost as i64 }); | ||
state.push_operator(Operator::I64LtU); | ||
state.push_operator(Operator::If { ty: WpTypeOrFuncType::Type(WpType::EmptyBlockType) }); | ||
state.push_operator(Operator::Unreachable); // FIXME: Signal the error properly. | ||
state.push_operator(Operator::End); | ||
|
||
// globals[remaining_points_index] -= self.accumulated_cost; | ||
state.push_operator(Operator::GlobalGet { global_index: self.remaining_points_index.as_u32() }); | ||
state.push_operator(Operator::I64Const { value: self.accumulated_cost as i64 }); | ||
state.push_operator(Operator::I64Sub); | ||
state.push_operator(Operator::GlobalSet { global_index: self.remaining_points_index.as_u32() }); |
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.
Might be ergonomic to implement https://doc.rust-lang.org/std/iter/trait.Extend.html for this, so you can put a bunch of operators in a collection like &[]
and push them all in
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.
Done. Two new Extend
s.
/// An instance of `Metering` should not be shared among different modules, since it tracks | ||
/// module-specific information like the global index to store metering state. Attempts to use | ||
/// a `Metering` instance from multiple modules will result in a panic. |
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.
If an instance of Metering
can be used for one module only, and it is installed via CompilerConfig::push_middleware
, does this mean you need a new compiler (and engine and store) for each metered module?
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.
That's right. The current structure makes easier reusing the modules in headless engines.
bors r+ |
1839: Metering middleware. r=nlewycky a=nlewycky Based on wasmerio/wasmer-reborn#129 . Original description: - [x] Necessary APIs for modifying module info. - [x] Global transformation. - [x] Per-function transformation. - [x] Testing and examples. Co-authored-by: losfair <[email protected]> Co-authored-by: Syrus <[email protected]> Co-authored-by: Nick Lewycky <[email protected]> Co-authored-by: nlewycky <[email protected]>
Build failed: |
bors r+ |
1839: Metering middleware. r=syrusakbary a=nlewycky Based on wasmerio/wasmer-reborn#129 . Original description: - [x] Necessary APIs for modifying module info. - [x] Global transformation. - [x] Per-function transformation. - [x] Testing and examples. Co-authored-by: losfair <[email protected]> Co-authored-by: Syrus <[email protected]> Co-authored-by: Nick Lewycky <[email protected]> Co-authored-by: nlewycky <[email protected]> Co-authored-by: Syrus Akbary <[email protected]>
bors r- |
Canceled. |
bors r+ |
Based on https://github.com/wasmerio/wasmer-reborn/pull/129 . Original description: