@@ -23,6 +23,52 @@ use super::{ParameterTuple, ResultType, SupportedReturnType};
2323use crate :: sandbox:: { ExtraAllowedSyscall , UninitializedSandbox } ;
2424use crate :: { log_then_return, new_error, Result } ;
2525
26+ /// A sandbox on which (primitive) host functions can be registered
27+ ///
28+ pub trait Registerable {
29+ /// Register a primitive host function
30+ fn register_host_function < Args : ParameterTuple , Output : SupportedReturnType > (
31+ & mut self ,
32+ name : & str ,
33+ hf : impl Into < HostFunction < Output , Args > > ,
34+ ) -> Result < ( ) > ;
35+ /// Register a primitive host function whose worker thread has
36+ /// extra permissive seccomp filters installed
37+ #[ cfg( all( feature = "seccomp" , target_os = "linux" ) ) ]
38+ fn register_host_function_with_syscalls < Args : ParameterTuple , Output : SupportedReturnType > (
39+ & mut self ,
40+ name : & str ,
41+ hf : impl Into < HostFunction < Output , Args > > ,
42+ eas : Vec < ExtraAllowedSyscall > ,
43+ ) -> Result < ( ) > ;
44+ }
45+ impl Registerable for UninitializedSandbox {
46+ fn register_host_function < Args : ParameterTuple , Output : SupportedReturnType > (
47+ & mut self ,
48+ name : & str ,
49+ hf : impl Into < HostFunction < Output , Args > > ,
50+ ) -> Result < ( ) > {
51+ let mut hfs = self
52+ . host_funcs
53+ . try_lock ( )
54+ . map_err ( |e| new_error ! ( "Error locking at {}:{}: {}" , file!( ) , line!( ) , e) ) ?;
55+ ( * hfs) . register_host_function ( name. to_string ( ) , hf. into ( ) . into ( ) )
56+ }
57+ #[ cfg( all( feature = "seccomp" , target_os = "linux" ) ) ]
58+ fn register_host_function_with_syscalls < Args : ParameterTuple , Output : SupportedReturnType > (
59+ & mut self ,
60+ name : & str ,
61+ hf : impl Into < HostFunction < Output , Args > > ,
62+ eas : Vec < ExtraAllowedSyscall > ,
63+ ) -> Result < ( ) > {
64+ let mut hfs = self
65+ . host_funcs
66+ . try_lock ( )
67+ . map_err ( |e| new_error ! ( "Error locking at {}:{}: {}" , file!( ) , line!( ) , e) ) ?;
68+ ( * hfs) . register_host_function_with_syscalls ( name. to_string ( ) , hf. into ( ) . into ( ) , eas)
69+ }
70+ }
71+
2672/// A representation of a host function.
2773/// This is a thin wrapper around a `Fn(Args) -> Result<Output>`.
2874#[ derive( Clone ) ]
0 commit comments