Skip to content

Commit

Permalink
update wamser; mem.grow compile time limits checks
Browse files Browse the repository at this point in the history
  • Loading branch information
laurci committed Aug 21, 2024
1 parent 95acf80 commit bff685a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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

0 comments on commit bff685a

Please sign in to comment.