Skip to content

Commit 0003ca4

Browse files
committed
Add function number checks
1 parent 14ef098 commit 0003ca4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/vm/src/compatibility.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ const TABLE_SIZE_LIMIT: u32 = 2500; // entries
7777
/// when a user accidentally includes wasm-bindgen, they get a bunch of unsupported imports.
7878
const MAX_IMPORTS: usize = 100;
7979

80+
const MAX_FUNCTIONS: usize = 10000;
81+
82+
const MAX_FUNCTION_PARAMS: usize = 50;
83+
84+
const MAX_FUNCTION_RESULTS: usize = 1;
85+
8086
/// Checks if the data is valid wasm and compatibility with the CosmWasm API (imports and exports)
8187
pub fn check_wasm(wasm_code: &[u8], available_capabilities: &HashSet<String>) -> VmResult<()> {
8288
let module = ParsedWasm::parse(wasm_code)?;
@@ -87,6 +93,7 @@ pub fn check_wasm(wasm_code: &[u8], available_capabilities: &HashSet<String>) ->
8793
check_wasm_exports(&module)?;
8894
check_wasm_imports(&module, SUPPORTED_IMPORTS)?;
8995
check_wasm_capabilities(&module, available_capabilities)?;
96+
check_wasm_functions(&module)?;
9097

9198
Ok(())
9299
}
@@ -234,6 +241,25 @@ fn check_wasm_capabilities(
234241
Ok(())
235242
}
236243

244+
fn check_wasm_functions(module: &ParsedWasm) -> VmResult<()> {
245+
if module.function_count > MAX_FUNCTIONS {
246+
return Err(VmError::static_validation_err(format!(
247+
"Wasm contract contains more than {MAX_FUNCTIONS} functions"
248+
)));
249+
}
250+
if module.max_func_params > MAX_FUNCTION_PARAMS {
251+
return Err(VmError::static_validation_err(format!(
252+
"Wasm contract contains function with more than {MAX_FUNCTION_PARAMS} parameters"
253+
)));
254+
}
255+
if module.max_func_results > MAX_FUNCTION_RESULTS {
256+
return Err(VmError::static_validation_err(format!(
257+
"Wasm contract contains function with more than {MAX_FUNCTION_RESULTS} results"
258+
)));
259+
}
260+
Ok(())
261+
}
262+
237263
#[cfg(test)]
238264
mod tests {
239265
use super::*;

0 commit comments

Comments
 (0)