Skip to content

Commit

Permalink
Merge pull request #43 from multiversx/update-wasmer-limits
Browse files Browse the repository at this point in the history
update wasmer; mem.grow compile time limits checks
  • Loading branch information
laurci authored Sep 26, 2024
2 parents 46c5fae + 54448cd commit 3750dc3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/libvmexeccapi-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ jobs:
- os: ubuntu-20.04
artifact_name: libvmexeccapi.so
make_target: capi-linux-amd64
- os: macos-11
- os: macos-13
artifact_name: libvmexeccapi.dylib
make_target: capi-osx-amd64
- os: macos-13-xlarge
artifact_name: libvmexeccapi_arm
make_target: capi-osx-arm
steps:
- name: Install rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: "1.77"

- name: Checkout
uses: actions/checkout@v4

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
default: true
toolchain: stable
toolchain: "1.77"
- name: Run rust tests
run: cargo test
clippy_check:
Expand Down
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 3750dc3

Please sign in to comment.