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 #43

Merged
merged 5 commits 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
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 @@ -137,7 +137,7 @@
memories
}

fn validate_memories(memories: &Vec<(&String, &wasmer::Memory)>) -> Result<(), ExecutorError> {

Check warning on line 140 in vm-executor-wasmer/src/wasmer_instance.rs

View workflow job for this annotation

GitHub Actions / clippy

writing `&Vec` instead of `&[_]` involves a new object where a slice will do

warning: writing `&Vec` instead of `&[_]` involves a new object where a slice will do --> vm-executor-wasmer/src/wasmer_instance.rs:140:32 | 140 | fn validate_memories(memories: &Vec<(&String, &wasmer::Memory)>) -> Result<(), ExecutorError> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `&[(&String, &wasmer::Memory)]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg = note: `#[warn(clippy::ptr_arg)]` on by default
if memories.is_empty() {
return Err(Box::new(ServiceError::new(
"no memory declared in smart contract",
Expand Down Expand Up @@ -180,6 +180,7 @@

// 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