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

update wasmer; mem.grow compile time limits checks #45

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions vm-executor-wasmer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ version = "0.2.0"
path = "../vm-executor"

[dependencies]
wasmer = { git = "https://github.com/multiversx/wasmer", rev = "cdd1550", default-features = false, features = [
wasmer = { git = "https://github.com/multiversx/wasmer", rev = "090ad17927fd1cbecb253a7b123d21e453fa13df", default-features = false, features = [
"singlepass",
"sys",
"universal",
"wat",
] }

wasmer-vm = { git = "https://github.com/multiversx/wasmer", rev = "cdd1550" }
wasmer-types = { git = "https://github.com/multiversx/wasmer", rev = "cdd1550" }
wasmer-vm = { git = "https://github.com/multiversx/wasmer", rev = "090ad17927fd1cbecb253a7b123d21e453fa13df" }
wasmer-types = { git = "https://github.com/multiversx/wasmer", rev = "090ad17927fd1cbecb253a7b123d21e453fa13df" }

chrono = "0.4.23"
log = "0.4.17"
Expand Down
1 change: 1 addition & 0 deletions vm-executor-wasmer/src/wasmer_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ fn push_middlewares(

// Create opcode_control middleware
let opcode_control_middleware = Arc::new(OpcodeControl::new(
100, // TODO: should be compilation_options.max_memory_grow_count,
compilation_options.max_memory_grow,
compilation_options.max_memory_grow_delta,
breakpoints_middleware.clone(),
Expand Down
15 changes: 15 additions & 0 deletions vm-executor-wasmer/src/wasmer_opcode_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct OpcodeControlGlobalIndexes {

#[derive(Debug)]
pub(crate) struct OpcodeControl {
total_memory_grow_count: Arc<Mutex<usize>>,
max_memory_grow_count: usize,
max_memory_grow: usize,
max_memory_grow_delta: usize,
breakpoints_middleware: Arc<Breakpoints>,
Expand All @@ -34,11 +36,14 @@ pub(crate) struct OpcodeControl {

impl OpcodeControl {
pub(crate) fn new(
max_memory_grow_count: usize,
max_memory_grow: usize,
max_memory_grow_delta: usize,
breakpoints_middleware: Arc<Breakpoints>,
) -> Self {
Self {
total_memory_grow_count: Arc::new(Mutex::new(0)),
max_memory_grow_count,
max_memory_grow,
max_memory_grow_delta,
breakpoints_middleware,
Expand Down Expand Up @@ -81,6 +86,8 @@ impl ModuleMiddleware for OpcodeControl {
_local_function_index: LocalFunctionIndex,
) -> Box<dyn FunctionMiddleware> {
Box::new(FunctionOpcodeControl {
total_memory_grow_count: self.total_memory_grow_count.clone(),
max_memory_grow_count: self.max_memory_grow_count,
max_memory_grow: self.max_memory_grow,
max_memory_grow_delta: self.max_memory_grow_delta,
breakpoints_middleware: self.breakpoints_middleware.clone(),
Expand Down Expand Up @@ -117,6 +124,8 @@ impl MiddlewareWithProtectedGlobals for OpcodeControl {

#[derive(Debug)]
struct FunctionOpcodeControl {
total_memory_grow_count: Arc<Mutex<usize>>,
max_memory_grow_count: usize,
max_memory_grow: usize,
max_memory_grow_delta: usize,
breakpoints_middleware: Arc<Breakpoints>,
Expand Down Expand Up @@ -193,6 +202,12 @@ impl FunctionMiddleware for FunctionOpcodeControl {
state: &mut MiddlewareReaderState<'b>,
) -> Result<(), MiddlewareError> {
if matches!(operator, Operator::MemoryGrow { .. }) {
let mut grow_count = self.total_memory_grow_count.lock().map_err(|_| MiddlewareError::new("MemoryGrowLimit", "failed to lock counter"))?;
*grow_count += 1;
if *grow_count > self.max_memory_grow_count {
return Err(MiddlewareError::new("MemoryGrowLimit", "memory.grow limit exceeded"));
}

self.inject_memory_grow_check(state);
}

Expand Down
Loading