-
Notifications
You must be signed in to change notification settings - Fork 839
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
Add metered compile and check functions to c api #736
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
use crate::{ | ||
error::{update_last_error, CApiError}, | ||
instance::wasmer_instance_t, | ||
module::wasmer_module_t, | ||
wasmer_result_t, | ||
}; | ||
|
||
use std::slice; | ||
|
||
#[cfg(feature = "metering")] | ||
use wasmer_runtime_core::backend::Compiler; | ||
|
||
/// Creates a new Module with gas limit from the given wasm bytes. | ||
/// | ||
/// Returns `wasmer_result_t::WASMER_OK` upon success. | ||
/// | ||
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` | ||
/// and `wasmer_last_error_message` to get an error message. | ||
#[allow(clippy::cast_ptr_alignment)] | ||
#[cfg(feature = "metering")] | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_compile_with_gas_metering( | ||
module: *mut *mut wasmer_module_t, | ||
wasm_bytes: *mut u8, | ||
wasm_bytes_len: u32, | ||
gas_limit: u64, | ||
) -> wasmer_result_t { | ||
if module.is_null() { | ||
update_last_error(CApiError { | ||
msg: "module is null".to_string(), | ||
}); | ||
return wasmer_result_t::WASMER_ERROR; | ||
} | ||
if wasm_bytes.is_null() { | ||
update_last_error(CApiError { | ||
msg: "wasm bytes is null".to_string(), | ||
}); | ||
return wasmer_result_t::WASMER_ERROR; | ||
} | ||
|
||
let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize); | ||
ethanfrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let result = wasmer_runtime_core::compile_with(bytes, &get_metered_compiler(gas_limit)); | ||
let new_module = match result { | ||
Ok(instance) => instance, | ||
Err(_) => { | ||
update_last_error(CApiError { | ||
msg: "compile error".to_string(), | ||
}); | ||
return wasmer_result_t::WASMER_ERROR; | ||
} | ||
}; | ||
*module = Box::into_raw(Box::new(new_module)) as *mut wasmer_module_t; | ||
wasmer_result_t::WASMER_OK | ||
} | ||
|
||
#[cfg(feature = "metering")] | ||
fn get_metered_compiler(limit: u64) -> impl Compiler { | ||
use wasmer_middleware_common::metering; | ||
use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler}; | ||
|
||
#[cfg(feature = "llvm-backend")] | ||
use wasmer_llvm_backend::ModuleCodeGenerator as MeteredMCG; | ||
|
||
#[cfg(feature = "singlepass-backend")] | ||
use wasmer_singlepass_backend::ModuleCodeGenerator as MeteredMCG; | ||
|
||
#[cfg(feature = "cranelift-backend")] | ||
use wasmer_clif_backend::CraneliftModuleCodeGenerator as MeteredMCG; | ||
|
||
let c: StreamingCompiler<MeteredMCG, _, _, _, _> = StreamingCompiler::new(move || { | ||
let mut chain = MiddlewareChain::new(); | ||
chain.push(metering::Metering::new(limit)); | ||
chain | ||
}); | ||
c | ||
} | ||
|
||
// returns gas used | ||
#[allow(clippy::cast_ptr_alignment)] | ||
#[no_mangle] | ||
#[cfg(feature = "metering")] | ||
pub unsafe extern "C" fn wasmer_instance_get_points_used(instance: *mut wasmer_instance_t) -> u64 { | ||
if instance.is_null() { | ||
return 0; | ||
} | ||
use wasmer_middleware_common::metering; | ||
let instance = &*(instance as *const wasmer_runtime::Instance); | ||
ethanfrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let points = metering::get_points_used(instance); | ||
points | ||
} | ||
|
||
// sets gas used | ||
#[allow(clippy::cast_ptr_alignment)] | ||
#[no_mangle] | ||
#[cfg(feature = "metering")] | ||
pub unsafe extern "C" fn wasmer_instance_set_points_used( | ||
instance: *mut wasmer_instance_t, | ||
new_gas: u64, | ||
) { | ||
if instance.is_null() { | ||
return; | ||
} | ||
use wasmer_middleware_common::metering; | ||
let instance = &mut *(instance as *mut wasmer_runtime::Instance); | ||
ethanfrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metering::set_points_used(instance, new_gas) | ||
} | ||
|
||
/*** placeholder implementation if metering feature off ***/ | ||
|
||
// Without metering, wasmer_compile_with_gas_metering is a copy of wasmer_compile | ||
#[cfg(not(feature = "metering"))] | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_compile_with_gas_metering( | ||
module: *mut *mut wasmer_module_t, | ||
wasm_bytes: *mut u8, | ||
wasm_bytes_len: u32, | ||
_: u64, | ||
) -> wasmer_result_t { | ||
if module.is_null() { | ||
update_last_error(CApiError { | ||
msg: "module is null".to_string(), | ||
}); | ||
return wasmer_result_t::WASMER_ERROR; | ||
} | ||
if wasm_bytes.is_null() { | ||
update_last_error(CApiError { | ||
msg: "wasm bytes is null".to_string(), | ||
}); | ||
return wasmer_result_t::WASMER_ERROR; | ||
} | ||
|
||
let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize); | ||
// TODO: this implicitly uses default_compiler() is that proper? maybe we override default_compiler | ||
let result = wasmer_runtime::compile(bytes); | ||
ethanfrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let new_module = match result { | ||
Ok(instance) => instance, | ||
Err(error) => { | ||
update_last_error(error); | ||
return wasmer_result_t::WASMER_ERROR; | ||
} | ||
}; | ||
*module = Box::into_raw(Box::new(new_module)) as *mut wasmer_module_t; | ||
wasmer_result_t::WASMER_OK | ||
} | ||
|
||
// returns gas used | ||
#[allow(clippy::cast_ptr_alignment)] | ||
#[no_mangle] | ||
#[cfg(not(feature = "metering"))] | ||
pub unsafe extern "C" fn wasmer_instance_get_points_used(_: *mut wasmer_instance_t) -> u64 { | ||
0 | ||
} | ||
|
||
// sets gas used | ||
#[allow(clippy::cast_ptr_alignment)] | ||
#[no_mangle] | ||
#[cfg(not(feature = "metering"))] | ||
pub unsafe extern "C" fn wasmer_instance_set_points_used(_: *mut wasmer_instance_t, _: u64) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 add the attribute
#[cfg(feature = "metering")]
here, and remove all the same attributes inmetering.rs
. I believe it will simplify the code :-).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.
The problem is that it would expose a different C ABI then, and the go wrapper would fail to compile. Since go doesn't support conditional compilation feature tags as rust does, I think it would break the go bindings to have different ABIs (okay, there is some stuff for architecture, but nothing like rust's feature switches).
I can change that out if this is not a problem, but wanted the least breaking change possible (especially since the abi is committed to the go-ext-wasmer repo)
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.
Golang build tags can be used to select the proper C ABI object to link against.