@@ -19,34 +19,57 @@ limitations under the License.
1919use  std:: sync:: { Mutex ,  OnceLock } ; 
2020
2121use  hyperlight_host:: func:: { ParameterValue ,  ReturnType } ; 
22+ use  hyperlight_host:: sandbox:: SandboxConfiguration ; 
23+ use  hyperlight_host:: sandbox:: snapshot:: Snapshot ; 
2224use  hyperlight_host:: sandbox:: uninitialized:: GuestBinary ; 
2325use  hyperlight_host:: { HyperlightError ,  MultiUseSandbox ,  UninitializedSandbox } ; 
2426use  hyperlight_testing:: simple_guest_for_fuzzing_as_string; 
2527use  libfuzzer_sys:: fuzz_target; 
28+ 
29+ // TODO: this SNAPSHOT is needed because of the memory leak in: https://github.com/hyperlight-dev/hyperlight/issues/826 
30+ // This should be removed once the leak is fixed 
31+ static  SNAPSHOT :  OnceLock < Mutex < Snapshot > >  = OnceLock :: new ( ) ; 
2632static  SANDBOX :  OnceLock < Mutex < MultiUseSandbox > >  = OnceLock :: new ( ) ; 
2733
2834// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`. 
2935// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations. 
3036fuzz_target ! ( 
3137    init:  { 
38+         let  mut  cfg = SandboxConfiguration :: default ( ) ; 
39+         cfg. set_output_data_size( 64  *  1024 ) ;  // 64 KB output buffer 
40+         cfg. set_input_data_size( 64  *  1024 ) ;  // 64 KB input buffer 
3241        let  u_sbox = UninitializedSandbox :: new( 
3342            GuestBinary :: FilePath ( simple_guest_for_fuzzing_as_string( ) . expect( "Guest Binary Missing" ) ) , 
34-             None 
43+             Some ( cfg ) 
3544        ) 
3645        . unwrap( ) ; 
3746
38-         let  mu_sbox:  MultiUseSandbox  = u_sbox. evolve( ) . unwrap( ) ; 
47+         let  mut  mu_sbox:  MultiUseSandbox  = u_sbox. evolve( ) . unwrap( ) ; 
48+         let  snapshot = mu_sbox. snapshot( ) . unwrap( ) ; 
3949        SANDBOX . set( Mutex :: new( mu_sbox) ) . unwrap( ) ; 
50+         SNAPSHOT . set( Mutex :: new( snapshot) ) . map_err( |_| "Snapshot already set" ) . unwrap( ) ; 
4051    } , 
4152
4253    |data:  ( String ,  ReturnType ,  Vec <ParameterValue >) | { 
4354        let  ( host_func_name,  host_func_return,  mut  host_func_params)  = data; 
4455        let  mut  sandbox = SANDBOX . get( ) . unwrap( ) . lock( ) . unwrap( ) ; 
56+         let  snapshot = SNAPSHOT . get( ) . unwrap( ) . lock( ) . unwrap( ) ; 
57+         sandbox. restore( & snapshot) . unwrap( ) ; 
58+ 
4559        host_func_params. insert( 0 ,  ParameterValue :: String ( host_func_name) ) ; 
4660        match  sandbox. call_type_erased_guest_function_by_name( "FuzzHostFunc" ,  host_func_return,  host_func_params)  { 
47-             Err ( HyperlightError :: GuestAborted ( _,  message) )  if  !message. contains( "Host Function Not Found" )  => { 
48-                 // We don't allow GuestAborted errors, except for the "Host Function Not Found" case 
49-                 panic!( "Guest Aborted: {}" ,  message) ; 
61+             Err ( e)  => { 
62+                 match  e { 
63+                     // the following are expected errors and occur frequently since 
64+                     // we are randomly generating the function name and parameters 
65+                     // to call with. 
66+                     HyperlightError :: HostFunctionNotFound ( _)  => { } 
67+                     HyperlightError :: UnexpectedNoOfArguments ( _,  _)  => { } , 
68+                     HyperlightError :: ParameterValueConversionFailure ( _,  _)  => { } , 
69+ 
70+                     // any other error should be reported 
71+                     _ => panic!( "Guest Aborted with Unexpected Error: {:?}" ,  e) , 
72+                 } 
5073            } 
5174            _ => { } 
5275        } 
0 commit comments