Skip to content

Commit

Permalink
[KLC-884] Potential code bloat in wasm smart contract deployment (#7)
Browse files Browse the repository at this point in the history
* Create tests to ensure function constraints

* Upgrade wasmer version
  • Loading branch information
nickgs1337 authored Jul 30, 2024
1 parent 8badd21 commit 1700d88
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 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.2"
path = "../vm-executor"

[dependencies]
wasmer = { git = "https://github.com/klever-io/wasmer", rev = "480f263", default-features = false, features = [
wasmer = { git = "https://github.com/klever-io/wasmer", rev = "3ab6ec4", default-features = false, features = [
"singlepass",
"sys",
"universal",
"wat",
] }

wasmer-vm = { git = "https://github.com/klever-io/wasmer", rev = "480f263" }
wasmer-types = { git = "https://github.com/klever-io/wasmer", rev = "480f263" }
wasmer-vm = { git = "https://github.com/klever-io/wasmer", rev = "3ab6ec4" }
wasmer-types = { git = "https://github.com/klever-io/wasmer", rev = "3ab6ec4" }

chrono = "0.4.23"
log = "0.4.17"
Expand Down
Binary file not shown.
Binary file added vm-executor-wasmer/tests/assets/web3-dns.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion vm-executor-wasmer/tests/common/test_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use klever_chain_vm_executor::{CompilationOptions, ExecutorService, Instance, VM
use klever_chain_vm_executor_wasmer::BasicExecutorService;
use wasmer::wat2wasm;

const DUMMY_COMPILATION_OPTIONS: CompilationOptions = CompilationOptions {
pub const DUMMY_COMPILATION_OPTIONS: CompilationOptions = CompilationOptions {
gas_limit: 0,
unmetered_locals: 0,
max_memory_grow: 0,
Expand Down
38 changes: 38 additions & 0 deletions vm-executor-wasmer/tests/endpoints_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use std::fs;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use klever_chain_vm_executor::{ExecutorService, VMHooksDefault};
use klever_chain_vm_executor_wasmer::BasicExecutorService;
use crate::common::DUMMY_COMPILATION_OPTIONS;

mod common;

#[test]
Expand Down Expand Up @@ -31,3 +39,33 @@ fn bad_init_result() {
let instance = common::test_instance(common::BAD_INIT_RESULT);
assert!(!instance.check_signatures());
}

#[test]
fn test_functions_constraints() {
// code bloat attack, should fail
let bytes = get_file_as_byte_vec("tests/assets/code_bloat_attack.wasm");
let service = BasicExecutorService::new();
let executor = service.new_executor(Box::new(VMHooksDefault)).unwrap();
executor.new_instance(&bytes, &DUMMY_COMPILATION_OPTIONS).err().expect("should fail");

// web3 dns, should pass
let bytes = get_file_as_byte_vec("tests/assets/web3-dns.wasm");
let service = BasicExecutorService::new();
let executor = service.new_executor(Box::new(VMHooksDefault)).unwrap();
executor.new_instance(&bytes, &DUMMY_COMPILATION_OPTIONS).expect("should pass");
}

fn get_file_as_byte_vec(contract_name: &str) -> Vec<u8> {
// relative path from cargo
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push(contract_name);
let filename = d.to_str().unwrap().to_string();

// load contents
let mut f = File::open(&filename).expect("no file found");
let metadata = fs::metadata(&filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");

buffer
}

0 comments on commit 1700d88

Please sign in to comment.