@@ -78,12 +78,23 @@ const TABLE_SIZE_LIMIT: u32 = 2500; // entries
78
78
/// when a user accidentally includes wasm-bindgen, they get a bunch of unsupported imports.
79
79
const MAX_IMPORTS : usize = 100 ;
80
80
81
+ /// The maximum number of functions a contract can have.
82
+ /// Any contract with more functions than this will be rejected during static validation.
81
83
const MAX_FUNCTIONS : usize = 20_000 ;
82
84
85
+ /// The maximum number of parameters a wasm function can have.
86
+ /// Any contract with a function type with more parameters than this will be rejected
87
+ /// during static validation.
83
88
const MAX_FUNCTION_PARAMS : usize = 100 ;
84
89
90
+ /// The maximum total number of parameters of all functions in the wasm.
91
+ /// For each function in the wasm, take the number of parameters and sum all of these up.
92
+ /// If that sum exceeds this limit, the wasm will be rejected during static validation.
85
93
const MAX_TOTAL_FUNCTION_PARAMS : usize = 10_000 ;
86
94
95
+ /// The maximum number of results a wasm function can have.
96
+ /// Any contract with a function type with more results than this will be rejected
97
+ /// during static validation.
87
98
const MAX_FUNCTION_RESULTS : usize = 1 ;
88
99
89
100
/// Checks if the data is valid wasm and compatibility with the CosmWasm API (imports and exports)
@@ -935,4 +946,63 @@ mod tests {
935
946
_ => panic ! ( "Got unexpected error" ) ,
936
947
}
937
948
}
949
+
950
+ #[ test]
951
+ fn check_wasm_fails_for_big_functions ( ) {
952
+ // too many arguments
953
+ let args = " i32" . repeat ( MAX_FUNCTION_PARAMS + 1 ) ;
954
+ let wasm = wat:: parse_str ( format ! (
955
+ r#"(module
956
+ (type (func (param {args})))
957
+ (func (type 0) nop)
958
+ )"#
959
+ ) )
960
+ . unwrap ( ) ;
961
+ let module = ParsedWasm :: parse ( & wasm) . unwrap ( ) ;
962
+
963
+ match check_wasm_functions ( & module) . unwrap_err ( ) {
964
+ VmError :: StaticValidationErr { msg, .. } => assert_eq ! (
965
+ msg,
966
+ "Wasm contract contains function with more than 100 parameters"
967
+ ) ,
968
+ _ => panic ! ( "Got unexpected error" ) ,
969
+ }
970
+
971
+ // too many returns
972
+ let return_types = " i32" . repeat ( MAX_FUNCTION_RESULTS + 1 ) ;
973
+ let returns = " i32.const 42" . repeat ( MAX_FUNCTION_RESULTS + 1 ) ;
974
+ let wasm = wat:: parse_str ( format ! (
975
+ r#"(module
976
+ (type (func (result {return_types})))
977
+ (func (type 0) {returns})
978
+ )"#
979
+ ) )
980
+ . unwrap ( ) ;
981
+ let module = ParsedWasm :: parse ( & wasm) . unwrap ( ) ;
982
+ match check_wasm_functions ( & module) . unwrap_err ( ) {
983
+ VmError :: StaticValidationErr { msg, .. } => assert_eq ! (
984
+ msg,
985
+ "Wasm contract contains function with more than 1 results"
986
+ ) ,
987
+ _ => panic ! ( "Got unexpected error" ) ,
988
+ }
989
+
990
+ // too many functions
991
+ let functions = [ "(func (type 0) nop)" ; MAX_FUNCTIONS + 1 ] ;
992
+ let functions = functions. join ( "\n " ) ;
993
+ let wasm = wat:: parse_str ( format ! (
994
+ r#"(module
995
+ (type (func))
996
+ {functions}
997
+ )"#
998
+ ) )
999
+ . unwrap ( ) ;
1000
+ let module = ParsedWasm :: parse ( & wasm) . unwrap ( ) ;
1001
+ match check_wasm_functions ( & module) . unwrap_err ( ) {
1002
+ VmError :: StaticValidationErr { msg, .. } => {
1003
+ assert_eq ! ( msg, "Wasm contract contains more than 20000 functions" )
1004
+ }
1005
+ _ => panic ! ( "Got unexpected error" ) ,
1006
+ }
1007
+ }
938
1008
}
0 commit comments