@@ -19,34 +19,57 @@ limitations under the License.
19
19
use std:: sync:: { Mutex , OnceLock } ;
20
20
21
21
use hyperlight_host:: func:: { ParameterValue , ReturnType } ;
22
+ use hyperlight_host:: sandbox:: SandboxConfiguration ;
23
+ use hyperlight_host:: sandbox:: snapshot:: Snapshot ;
22
24
use hyperlight_host:: sandbox:: uninitialized:: GuestBinary ;
23
25
use hyperlight_host:: { HyperlightError , MultiUseSandbox , UninitializedSandbox } ;
24
26
use hyperlight_testing:: simple_guest_for_fuzzing_as_string;
25
27
use 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 ( ) ;
26
32
static SANDBOX : OnceLock < Mutex < MultiUseSandbox > > = OnceLock :: new ( ) ;
27
33
28
34
// This fuzz target tests all combinations of ReturnType and Parameters for `call_guest_function_by_name`.
29
35
// For fuzzing efficiency, we create one Sandbox and reuse it for all fuzzing iterations.
30
36
fuzz_target ! (
31
37
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
32
41
let u_sbox = UninitializedSandbox :: new(
33
42
GuestBinary :: FilePath ( simple_guest_for_fuzzing_as_string( ) . expect( "Guest Binary Missing" ) ) ,
34
- None
43
+ Some ( cfg )
35
44
)
36
45
. unwrap( ) ;
37
46
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( ) ;
39
49
SANDBOX . set( Mutex :: new( mu_sbox) ) . unwrap( ) ;
50
+ SNAPSHOT . set( Mutex :: new( snapshot) ) . map_err( |_| "Snapshot already set" ) . unwrap( ) ;
40
51
} ,
41
52
42
53
|data: ( String , ReturnType , Vec <ParameterValue >) | {
43
54
let ( host_func_name, host_func_return, mut host_func_params) = data;
44
55
let mut sandbox = SANDBOX . get( ) . unwrap( ) . lock( ) . unwrap( ) ;
56
+ let snapshot = SNAPSHOT . get( ) . unwrap( ) . lock( ) . unwrap( ) ;
57
+ sandbox. restore( & snapshot) . unwrap( ) ;
58
+
45
59
host_func_params. insert( 0 , ParameterValue :: String ( host_func_name) ) ;
46
60
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
+ }
50
73
}
51
74
_ => { }
52
75
}
0 commit comments