@@ -15,8 +15,8 @@ pub enum Injection {
1515}
1616
1717impl Injection {
18- pub fn inject ( self , module : & mut Module ) -> Result < ( ) , String > {
19- get_injection ( self ) ( module)
18+ pub fn inject ( self , module : & mut Module , function : & str ) -> Result < ( ) , String > {
19+ get_injection ( self ) ( module, function )
2020 }
2121}
2222
@@ -31,7 +31,7 @@ impl Display for Injection {
3131 }
3232 }
3333}
34- type InjectionFn = dyn FnMut ( & mut Module ) -> Result < ( ) , String > ;
34+ type InjectionFn = dyn FnMut ( & mut Module , & str ) -> Result < ( ) , String > ;
3535
3636pub fn get_injection ( injection : Injection ) -> Box < InjectionFn > {
3737 Box :: new ( match injection {
@@ -43,8 +43,8 @@ pub fn get_injection(injection: Injection) -> Box<InjectionFn> {
4343 } )
4444}
4545
46- pub fn inject_infinite_loop ( module : & mut Module ) -> Result < ( ) , String > {
47- module. map_function ( "validate_block" , |func_body : & mut FuncBody , _ | {
46+ pub fn inject_infinite_loop ( module : & mut Module , function_name : & str ) -> Result < ( ) , String > {
47+ module. map_function ( function_name , |func_body : & mut FuncBody | {
4848 let code = func_body. code_mut ( ) ;
4949
5050 let mut code_with_loop = vec ! [
@@ -60,8 +60,8 @@ pub fn inject_infinite_loop(module: &mut Module) -> Result<(), String> {
6060 } )
6161}
6262
63- fn inject_bad_return_value ( module : & mut Module ) -> Result < ( ) , String > {
64- module. map_function ( "validate_block" , |func_body : & mut FuncBody , _ | {
63+ fn inject_bad_return_value ( module : & mut Module , function_name : & str ) -> Result < ( ) , String > {
64+ module. map_function ( function_name , |func_body : & mut FuncBody | {
6565 * func_body. code_mut ( ) = Instructions :: new ( vec ! [
6666 // Last value on the stack gets returned
6767 Instruction :: I64Const ( 123456789 ) ,
@@ -70,8 +70,8 @@ fn inject_bad_return_value(module: &mut Module) -> Result<(), String> {
7070 } )
7171}
7272
73- fn inject_stack_overflow ( module : & mut Module ) -> Result < ( ) , String > {
74- module. map_function ( "validate_block" , |func_body : & mut FuncBody , _ | {
73+ fn inject_stack_overflow ( module : & mut Module , function_name : & str ) -> Result < ( ) , String > {
74+ module. map_function ( function_name , |func_body : & mut FuncBody | {
7575 let code = func_body. code_mut ( ) ;
7676
7777 let mut code_with_allocation = vec ! [
@@ -86,8 +86,8 @@ fn inject_stack_overflow(module: &mut Module) -> Result<(), String> {
8686 } )
8787}
8888
89- fn inject_noops ( module : & mut Module ) -> Result < ( ) , String > {
90- module. map_function ( "validate_block" , |func_body : & mut FuncBody , _ | {
89+ fn inject_noops ( module : & mut Module , function_name : & str ) -> Result < ( ) , String > {
90+ module. map_function ( function_name , |func_body : & mut FuncBody | {
9191 // Add half a billion NoOperations to (hopefully) slow down interpretation-time
9292 let code = func_body. code_mut ( ) ;
9393
@@ -98,24 +98,27 @@ fn inject_noops(module: &mut Module) -> Result<(), String> {
9898 } )
9999}
100100
101- fn inject_heap_overflow ( module : & mut Module ) -> Result < ( ) , String > {
102- module. map_function (
103- "validate_block" ,
104- |func_body : & mut FuncBody , malloc_index| {
105- let code = func_body. code_mut ( ) ;
106- let index: u32 = malloc_index as u32 ;
101+ fn inject_heap_overflow ( module : & mut Module , function_name : & str ) -> Result < ( ) , String > {
102+ let malloc_index = & module. get_malloc_index ( ) . expect ( "No malloc function" ) ;
107103
108- let mut code_with_allocation =
109- vec ! [ [ Instruction :: I32Const ( 33_554_431 ) , Instruction :: Call ( index) ] ; 8 ]
110- . into_iter ( )
111- . flatten ( )
112- . collect :: < Vec < Instruction > > ( ) ;
104+ module. map_function ( function_name, |func_body : & mut FuncBody | {
105+ let code = func_body. code_mut ( ) ;
106+
107+ let mut code_with_allocation = vec ! [
108+ [
109+ Instruction :: I32Const ( 33_554_431 ) ,
110+ Instruction :: Call ( * malloc_index as u32 )
111+ ] ;
112+ 8
113+ ]
114+ . into_iter ( )
115+ . flatten ( )
116+ . collect :: < Vec < Instruction > > ( ) ;
113117
114- code_with_allocation. append ( code. elements_mut ( ) ) ;
118+ code_with_allocation. append ( code. elements_mut ( ) ) ;
115119
116- * code. elements_mut ( ) = code_with_allocation;
117- } ,
118- )
120+ * code. elements_mut ( ) = code_with_allocation;
121+ } )
119122}
120123
121124#[ cfg( test) ]
@@ -124,6 +127,7 @@ mod injections_tests {
124127 use crate :: util:: load_module_from_wasm;
125128 use std:: path:: Path ;
126129
130+ const FUNCTION_NAME : & ' static str = "validate_block" ;
127131 const WASM_PATH : & ' static str = concat ! ( env!( "CARGO_MANIFEST_DIR" ) , "/test-wasm/test.wasm" ) ;
128132
129133 fn get_function_body ( module : & mut Module ) -> & mut FuncBody {
@@ -149,7 +153,7 @@ mod injections_tests {
149153 let mut module = load_module ( ) ;
150154
151155 let injection = Injection :: InfiniteLoop ;
152- assert ! ( injection. inject( & mut module) . is_ok( ) ) ;
156+ assert ! ( injection. inject( & mut module, FUNCTION_NAME ) . is_ok( ) ) ;
153157
154158 let function_body = get_function_body ( & mut module) ;
155159
@@ -167,7 +171,7 @@ mod injections_tests {
167171 let mut module = load_module ( ) ;
168172 let injection = Injection :: BadReturnValue ;
169173
170- assert ! ( injection. inject( & mut module) . is_ok( ) ) ;
174+ assert ! ( injection. inject( & mut module, FUNCTION_NAME ) . is_ok( ) ) ;
171175
172176 let function_body = get_function_body ( & mut module) ;
173177
@@ -180,7 +184,7 @@ mod injections_tests {
180184 let mut module = load_module ( ) ;
181185
182186 let injection = Injection :: StackOverflow ;
183- assert ! ( injection. inject( & mut module) . is_ok( ) ) ;
187+ assert ! ( injection. inject( & mut module, FUNCTION_NAME ) . is_ok( ) ) ;
184188
185189 let function_body = get_function_body ( & mut module) ;
186190
@@ -197,7 +201,7 @@ mod injections_tests {
197201 let mut module = load_module ( ) ;
198202
199203 let injection = Injection :: Noops ;
200- assert ! ( injection. inject( & mut module) . is_ok( ) ) ;
204+ assert ! ( injection. inject( & mut module, FUNCTION_NAME ) . is_ok( ) ) ;
201205
202206 let function_body = get_function_body ( & mut module) ;
203207
@@ -210,7 +214,7 @@ mod injections_tests {
210214 let mut module = load_module ( ) ;
211215
212216 let injection = Injection :: HeapOverflow ;
213- assert ! ( injection. inject( & mut module) . is_ok( ) ) ;
217+ assert ! ( injection. inject( & mut module, FUNCTION_NAME ) . is_ok( ) ) ;
214218
215219 let index = module. get_malloc_index ( ) . unwrap ( ) as u32 ;
216220 let function_body = get_function_body ( & mut module) ;
0 commit comments