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

temp1.0.2 #58

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
update
  • Loading branch information
zjg555543 committed Jan 15, 2024
commit 7594feb0c2ce0b9148e64839a1583cb06ad2d2f3
53 changes: 53 additions & 0 deletions packages/vm/src/compatibility.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use parity_wasm::elements::Type;
use parity_wasm::elements::{External, ImportEntry, Module};
use std::collections::BTreeSet;
use std::collections::HashSet;
@@ -50,6 +51,12 @@ const SUPPORTED_INTERFACE_VERSIONS: &[&str] = &[

const MEMORY_LIMIT: u32 = 512; // in pages

const MAX_FUNCTIONS: usize = 10000;

const MAX_FUNCTION_PARAMS: usize = 50;

const MAX_FUNCTION_RESULTS: usize = 1;

/// Checks if the data is valid wasm and compatibility with the CosmWasm API (imports and exports)
pub fn check_wasm(wasm_code: &[u8], supported_features: &HashSet<String>) -> VmResult<()> {
let module = deserialize_wasm(wasm_code)?;
@@ -58,6 +65,52 @@ pub fn check_wasm(wasm_code: &[u8], supported_features: &HashSet<String>) -> VmR
check_wasm_exports(&module)?;
check_wasm_imports(&module, SUPPORTED_IMPORTS)?;
check_wasm_features(&module, supported_features)?;
check_wasm_functions(&module)?;
Ok(())
}

fn check_wasm_functions(module: &Module) -> VmResult<()> {
let functions = module
.function_section()
.map(|fs| fs.entries())
.unwrap_or_default();

if functions.len() > MAX_FUNCTIONS {
return Err(VmError::static_validation_err(format!(
"Wasm contract contains more than {MAX_FUNCTIONS} functions"
)));
}

let types = module
.type_section()
.map(|ts| ts.types())
.unwrap_or_default();

let max_func_params = types
.iter()
.map(|t| match t {
Type::Function(f) => f.params().len(),
})
.max()
.unwrap_or_default();
let max_func_results = types
.iter()
.map(|t| match t {
Type::Function(f) => f.results().len(),
})
.max()
.unwrap_or_default();

if max_func_params > MAX_FUNCTION_PARAMS {
return Err(VmError::static_validation_err(format!(
"Wasm contract contains function with more than {MAX_FUNCTION_PARAMS} parameters"
)));
}
if max_func_results > MAX_FUNCTION_RESULTS {
return Err(VmError::static_validation_err(format!(
"Wasm contract contains function with more than {MAX_FUNCTION_RESULTS} results"
)));
}
Ok(())
}

11 changes: 8 additions & 3 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fs;
use std::io;
use std::panic::catch_unwind;
use std::panic::AssertUnwindSafe;
use std::path::PathBuf;

use wasmer::{DeserializeError, Module, Store};
@@ -119,9 +121,12 @@ impl FileSystemCache {
.map_err(|e| VmError::cache_err(format!("Error creating directory: {}", e)))?;
let filename = checksum.to_hex();
let path = modules_dir.join(filename);
module
.serialize_to_file(path)
.map_err(|e| VmError::cache_err(format!("Error writing module to disk: {}", e)))?;
catch_unwind(AssertUnwindSafe(|| {
module
.serialize_to_file(&path)
.map_err(|e| VmError::cache_err(format!("Error writing module to disk: {e}")))
}))
.map_err(|_| VmError::cache_err("Could not write module to disk"))??;
Ok(())
}