@@ -14,8 +14,7 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- use std:: cmp:: { max, min} ;
18- use std:: time:: Duration ;
17+ use std:: cmp:: max;
1918
2019use tracing:: { instrument, Span } ;
2120
@@ -56,31 +55,6 @@ pub struct SandboxConfiguration {
5655 /// field should be represented as an `Option`, that type is not
5756 /// FFI-safe, so it cannot be.
5857 heap_size_override : u64 ,
59- /// The max_execution_time of a guest execution in milliseconds. If set to 0, the max_execution_time
60- /// will be set to the default value of 1000ms if the guest execution does not complete within the time specified
61- /// then the execution will be cancelled, the minimum value is 1ms
62- ///
63- /// Note: this is a C-compatible struct, so even though this optional
64- /// field should be represented as an `Option`, that type is not
65- /// FFI-safe, so it cannot be.
66- ///
67- max_execution_time : u16 ,
68- /// The max_wait_for_cancellation represents the maximum time the host should wait for a guest execution to be cancelled
69- /// If set to 0, the max_wait_for_cancellation will be set to the default value of 10ms.
70- /// The minimum value is 1ms.
71- ///
72- /// Note: this is a C-compatible struct, so even though this optional
73- /// field should be represented as an `Option`, that type is not
74- /// FFI-safe, so it cannot be.
75- max_wait_for_cancellation : u8 ,
76- // The max_initialization_time represents the maximum time the host should wait for a guest to initialize
77- // If set to 0, the max_initialization_time will be set to the default value of 2000ms.
78- // The minimum value is 1ms.
79- //
80- // Note: this is a C-compatible struct, so even though this optional
81- // field should be represented as an `Option`, that type is not
82- // FFI-safe, so it cannot be.
83- max_initialization_time : u16 ,
8458}
8559
8660impl SandboxConfiguration {
@@ -92,24 +66,6 @@ impl SandboxConfiguration {
9266 pub const DEFAULT_OUTPUT_SIZE : usize = 0x4000 ;
9367 /// The minimum size of output data
9468 pub const MIN_OUTPUT_SIZE : usize = 0x2000 ;
95- /// The default value for max initialization time (in milliseconds)
96- pub const DEFAULT_MAX_INITIALIZATION_TIME : u16 = 2000 ;
97- /// The minimum value for max initialization time (in milliseconds)
98- pub const MIN_MAX_INITIALIZATION_TIME : u16 = 1 ;
99- /// The maximum value for max initialization time (in milliseconds)
100- pub const MAX_MAX_INITIALIZATION_TIME : u16 = u16:: MAX ;
101- /// The default and minimum values for max execution time (in milliseconds)
102- pub const DEFAULT_MAX_EXECUTION_TIME : u16 = 1000 ;
103- /// The minimum value for max execution time (in milliseconds)
104- pub const MIN_MAX_EXECUTION_TIME : u16 = 1 ;
105- /// The maximum value for max execution time (in milliseconds)
106- pub const MAX_MAX_EXECUTION_TIME : u16 = u16:: MAX ;
107- /// The default and minimum values for max wait for cancellation (in milliseconds)
108- pub const DEFAULT_MAX_WAIT_FOR_CANCELLATION : u8 = 100 ;
109- /// The minimum value for max wait for cancellation (in milliseconds)
110- pub const MIN_MAX_WAIT_FOR_CANCELLATION : u8 = 10 ;
111- /// The maximum value for max wait for cancellation (in milliseconds)
112- pub const MAX_MAX_WAIT_FOR_CANCELLATION : u8 = u8:: MAX ;
11369
11470 #[ allow( clippy:: too_many_arguments) ]
11571 /// Create a new configuration for a sandbox with the given sizes.
@@ -119,63 +75,14 @@ impl SandboxConfiguration {
11975 output_data_size : usize ,
12076 stack_size_override : Option < u64 > ,
12177 heap_size_override : Option < u64 > ,
122- max_execution_time : Option < Duration > ,
123- max_initialization_time : Option < Duration > ,
124- max_wait_for_cancellation : Option < Duration > ,
12578 #[ cfg( gdb) ] guest_debug_info : Option < DebugInfo > ,
12679 ) -> Self {
12780 Self {
12881 input_data_size : max ( input_data_size, Self :: MIN_INPUT_SIZE ) ,
12982 output_data_size : max ( output_data_size, Self :: MIN_OUTPUT_SIZE ) ,
13083 stack_size_override : stack_size_override. unwrap_or ( 0 ) ,
13184 heap_size_override : heap_size_override. unwrap_or ( 0 ) ,
132- max_execution_time : {
133- match max_execution_time {
134- Some ( max_execution_time) => match max_execution_time. as_millis ( ) {
135- 0 => Self :: DEFAULT_MAX_EXECUTION_TIME ,
136- 1 .. => min (
137- Self :: MAX_MAX_EXECUTION_TIME . into ( ) ,
138- max (
139- max_execution_time. as_millis ( ) ,
140- Self :: MIN_MAX_EXECUTION_TIME . into ( ) ,
141- ) ,
142- ) as u16 ,
143- } ,
144- None => Self :: DEFAULT_MAX_EXECUTION_TIME ,
145- }
146- } ,
147- max_wait_for_cancellation : {
148- match max_wait_for_cancellation {
149- Some ( max_wait_for_cancellation) => {
150- match max_wait_for_cancellation. as_millis ( ) {
151- 0 => Self :: DEFAULT_MAX_WAIT_FOR_CANCELLATION ,
152- 1 .. => min (
153- Self :: MAX_MAX_WAIT_FOR_CANCELLATION . into ( ) ,
154- max (
155- max_wait_for_cancellation. as_millis ( ) ,
156- Self :: MIN_MAX_WAIT_FOR_CANCELLATION . into ( ) ,
157- ) ,
158- ) as u8 ,
159- }
160- }
161- None => Self :: DEFAULT_MAX_WAIT_FOR_CANCELLATION ,
162- }
163- } ,
164- max_initialization_time : {
165- match max_initialization_time {
166- Some ( max_initialization_time) => match max_initialization_time. as_millis ( ) {
167- 0 => Self :: DEFAULT_MAX_INITIALIZATION_TIME ,
168- 1 .. => min (
169- Self :: MAX_MAX_INITIALIZATION_TIME . into ( ) ,
170- max (
171- max_initialization_time. as_millis ( ) ,
172- Self :: MIN_MAX_INITIALIZATION_TIME . into ( ) ,
173- ) ,
174- ) as u16 ,
175- } ,
176- None => Self :: DEFAULT_MAX_INITIALIZATION_TIME ,
177- }
178- } ,
85+
17986 #[ cfg( gdb) ]
18087 guest_debug_info,
18188 }
@@ -207,62 +114,6 @@ impl SandboxConfiguration {
207114 self . heap_size_override = heap_size;
208115 }
209116
210- /// Set the maximum execution time of a guest function execution. If set to 0, the max_execution_time
211- /// will be set to the default value of DEFAULT_MAX_EXECUTION_TIME if the guest execution does not complete within the time specified
212- /// then the execution will be cancelled, the minimum value is MIN_MAX_EXECUTION_TIME
213- #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
214- pub fn set_max_execution_time ( & mut self , max_execution_time : Duration ) {
215- match max_execution_time. as_millis ( ) {
216- 0 => self . max_execution_time = Self :: DEFAULT_MAX_EXECUTION_TIME ,
217- 1 .. => {
218- self . max_execution_time = min (
219- Self :: MAX_MAX_EXECUTION_TIME . into ( ) ,
220- max (
221- max_execution_time. as_millis ( ) ,
222- Self :: MIN_MAX_EXECUTION_TIME . into ( ) ,
223- ) ,
224- ) as u16
225- }
226- }
227- }
228-
229- /// Set the maximum time to wait for guest execution calculation. If set to 0, the maximum cancellation time
230- /// will be set to the default value of DEFAULT_MAX_WAIT_FOR_CANCELLATION if the guest execution cancellation does not complete within the time specified
231- /// then an error will be returned, the minimum value is MIN_MAX_WAIT_FOR_CANCELLATION
232- #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
233- pub fn set_max_execution_cancel_wait_time ( & mut self , max_wait_for_cancellation : Duration ) {
234- match max_wait_for_cancellation. as_millis ( ) {
235- 0 => self . max_wait_for_cancellation = Self :: DEFAULT_MAX_WAIT_FOR_CANCELLATION ,
236- 1 .. => {
237- self . max_wait_for_cancellation = min (
238- Self :: MAX_MAX_WAIT_FOR_CANCELLATION . into ( ) ,
239- max (
240- max_wait_for_cancellation. as_millis ( ) ,
241- Self :: MIN_MAX_WAIT_FOR_CANCELLATION . into ( ) ,
242- ) ,
243- ) as u8
244- }
245- }
246- }
247-
248- /// Set the maximum time to wait for guest initialization. If set to 0, the maximum initialization time
249- /// will be set to the default value of DEFAULT_MAX_INITIALIZATION_TIME if the guest initialization does not complete within the time specified
250- /// then an error will be returned, the minimum value is MIN_MAX_INITIALIZATION_TIME
251- pub fn set_max_initialization_time ( & mut self , max_initialization_time : Duration ) {
252- match max_initialization_time. as_millis ( ) {
253- 0 => self . max_initialization_time = Self :: DEFAULT_MAX_INITIALIZATION_TIME ,
254- 1 .. => {
255- self . max_initialization_time = min (
256- Self :: MAX_MAX_INITIALIZATION_TIME . into ( ) ,
257- max (
258- max_initialization_time. as_millis ( ) ,
259- Self :: MIN_MAX_INITIALIZATION_TIME . into ( ) ,
260- ) ,
261- ) as u16
262- }
263- }
264- }
265-
266117 /// Sets the configuration for the guest debug
267118 #[ cfg( gdb) ]
268119 #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
@@ -280,20 +131,6 @@ impl SandboxConfiguration {
280131 self . output_data_size
281132 }
282133
283- #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
284- pub ( crate ) fn get_max_execution_time ( & self ) -> u16 {
285- self . max_execution_time
286- }
287-
288- #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
289- pub ( crate ) fn get_max_wait_for_cancellation ( & self ) -> u8 {
290- self . max_wait_for_cancellation
291- }
292-
293- pub ( crate ) fn get_max_initialization_time ( & self ) -> u16 {
294- self . max_initialization_time
295- }
296-
297134 #[ cfg( gdb) ]
298135 #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
299136 pub ( crate ) fn get_guest_debug_info ( & self ) -> Option < DebugInfo > {
@@ -335,9 +172,6 @@ impl Default for SandboxConfiguration {
335172 Self :: DEFAULT_OUTPUT_SIZE ,
336173 None ,
337174 None ,
338- None ,
339- None ,
340- None ,
341175 #[ cfg( gdb) ]
342176 None ,
343177 )
@@ -346,8 +180,6 @@ impl Default for SandboxConfiguration {
346180
347181#[ cfg( test) ]
348182mod tests {
349- use std:: time:: Duration ;
350-
351183 use super :: SandboxConfiguration ;
352184 use crate :: testing:: simple_guest_exe_info;
353185
@@ -357,21 +189,11 @@ mod tests {
357189 const HEAP_SIZE_OVERRIDE : u64 = 0x50000 ;
358190 const INPUT_DATA_SIZE_OVERRIDE : usize = 0x4000 ;
359191 const OUTPUT_DATA_SIZE_OVERRIDE : usize = 0x4001 ;
360- const MAX_EXECUTION_TIME_OVERRIDE : u16 = 1010 ;
361- const MAX_WAIT_FOR_CANCELLATION_OVERRIDE : u8 = 200 ;
362- const MAX_INITIALIZATION_TIME_OVERRIDE : u16 = 2000 ;
363192 let mut cfg = SandboxConfiguration :: new (
364193 INPUT_DATA_SIZE_OVERRIDE ,
365194 OUTPUT_DATA_SIZE_OVERRIDE ,
366195 Some ( STACK_SIZE_OVERRIDE ) ,
367196 Some ( HEAP_SIZE_OVERRIDE ) ,
368- Some ( Duration :: from_millis ( MAX_EXECUTION_TIME_OVERRIDE as u64 ) ) ,
369- Some ( Duration :: from_millis (
370- MAX_INITIALIZATION_TIME_OVERRIDE as u64 ,
371- ) ) ,
372- Some ( Duration :: from_millis (
373- MAX_WAIT_FOR_CANCELLATION_OVERRIDE as u64 ,
374- ) ) ,
375197 #[ cfg( gdb) ]
376198 None ,
377199 ) ;
@@ -388,15 +210,6 @@ mod tests {
388210 assert_eq ! ( 2048 , cfg. heap_size_override) ;
389211 assert_eq ! ( INPUT_DATA_SIZE_OVERRIDE , cfg. input_data_size) ;
390212 assert_eq ! ( OUTPUT_DATA_SIZE_OVERRIDE , cfg. output_data_size) ;
391- assert_eq ! ( MAX_EXECUTION_TIME_OVERRIDE , cfg. max_execution_time) ;
392- assert_eq ! (
393- MAX_WAIT_FOR_CANCELLATION_OVERRIDE ,
394- cfg. max_wait_for_cancellation
395- ) ;
396- assert_eq ! (
397- MAX_WAIT_FOR_CANCELLATION_OVERRIDE ,
398- cfg. max_wait_for_cancellation
399- ) ;
400213 }
401214
402215 #[ test]
@@ -406,57 +219,19 @@ mod tests {
406219 SandboxConfiguration :: MIN_OUTPUT_SIZE - 1 ,
407220 None ,
408221 None ,
409- Some ( Duration :: from_millis (
410- SandboxConfiguration :: MIN_MAX_EXECUTION_TIME as u64 ,
411- ) ) ,
412- Some ( Duration :: from_millis (
413- SandboxConfiguration :: MIN_MAX_INITIALIZATION_TIME as u64 ,
414- ) ) ,
415- Some ( Duration :: from_millis (
416- SandboxConfiguration :: MIN_MAX_WAIT_FOR_CANCELLATION as u64 - 1 ,
417- ) ) ,
418222 #[ cfg( gdb) ]
419223 None ,
420224 ) ;
421225 assert_eq ! ( SandboxConfiguration :: MIN_INPUT_SIZE , cfg. input_data_size) ;
422226 assert_eq ! ( SandboxConfiguration :: MIN_OUTPUT_SIZE , cfg. output_data_size) ;
423227 assert_eq ! ( 0 , cfg. stack_size_override) ;
424228 assert_eq ! ( 0 , cfg. heap_size_override) ;
425- assert_eq ! (
426- SandboxConfiguration :: MIN_MAX_EXECUTION_TIME ,
427- cfg. max_execution_time
428- ) ;
429- assert_eq ! (
430- SandboxConfiguration :: MIN_MAX_WAIT_FOR_CANCELLATION ,
431- cfg. max_wait_for_cancellation
432- ) ;
433- assert_eq ! (
434- SandboxConfiguration :: MIN_MAX_EXECUTION_TIME ,
435- cfg. max_initialization_time
436- ) ;
437229
438230 cfg. set_input_data_size ( SandboxConfiguration :: MIN_INPUT_SIZE - 1 ) ;
439231 cfg. set_output_data_size ( SandboxConfiguration :: MIN_OUTPUT_SIZE - 1 ) ;
440- cfg. set_max_execution_time ( Duration :: from_millis (
441- SandboxConfiguration :: MIN_MAX_EXECUTION_TIME as u64 ,
442- ) ) ;
443- cfg. set_max_initialization_time ( Duration :: from_millis (
444- SandboxConfiguration :: MIN_MAX_INITIALIZATION_TIME as u64 - 1 ,
445- ) ) ;
446- cfg. set_max_execution_cancel_wait_time ( Duration :: from_millis (
447- SandboxConfiguration :: MIN_MAX_WAIT_FOR_CANCELLATION as u64 - 1 ,
448- ) ) ;
449232
450233 assert_eq ! ( SandboxConfiguration :: MIN_INPUT_SIZE , cfg. input_data_size) ;
451234 assert_eq ! ( SandboxConfiguration :: MIN_OUTPUT_SIZE , cfg. output_data_size) ;
452- assert_eq ! (
453- SandboxConfiguration :: MIN_MAX_EXECUTION_TIME ,
454- cfg. max_execution_time
455- ) ;
456- assert_eq ! (
457- SandboxConfiguration :: MIN_MAX_WAIT_FOR_CANCELLATION ,
458- cfg. max_wait_for_cancellation
459- ) ;
460235 }
461236
462237 mod proptests {
@@ -481,27 +256,6 @@ mod tests {
481256 prop_assert_eq!( size, cfg. get_output_data_size( ) ) ;
482257 }
483258
484- #[ test]
485- fn max_execution_time( time in SandboxConfiguration :: MIN_MAX_EXECUTION_TIME ..=SandboxConfiguration :: MIN_MAX_EXECUTION_TIME * 10 ) {
486- let mut cfg = SandboxConfiguration :: default ( ) ;
487- cfg. set_max_execution_time( std:: time:: Duration :: from_millis( time. into( ) ) ) ;
488- prop_assert_eq!( time, cfg. get_max_execution_time( ) ) ;
489- }
490-
491- #[ test]
492- fn max_wait_for_cancellation( time in SandboxConfiguration :: MIN_MAX_WAIT_FOR_CANCELLATION ..=SandboxConfiguration :: MIN_MAX_WAIT_FOR_CANCELLATION * 10 ) {
493- let mut cfg = SandboxConfiguration :: default ( ) ;
494- cfg. set_max_execution_cancel_wait_time( std:: time:: Duration :: from_millis( time. into( ) ) ) ;
495- prop_assert_eq!( time, cfg. get_max_wait_for_cancellation( ) ) ;
496- }
497-
498- #[ test]
499- fn max_initialization_time( time in SandboxConfiguration :: MIN_MAX_INITIALIZATION_TIME ..=SandboxConfiguration :: MIN_MAX_INITIALIZATION_TIME * 10 ) {
500- let mut cfg = SandboxConfiguration :: default ( ) ;
501- cfg. set_max_initialization_time( std:: time:: Duration :: from_millis( time. into( ) ) ) ;
502- prop_assert_eq!( time, cfg. get_max_initialization_time( ) ) ;
503- }
504-
505259 #[ test]
506260 fn stack_size_override( size in 0x1000 ..=0x10000u64 ) {
507261 let mut cfg = SandboxConfiguration :: default ( ) ;
0 commit comments