@@ -20,7 +20,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
20
20
use tracing:: { instrument, Span } ;
21
21
22
22
use super :: guest_dispatch:: call_function_on_guest;
23
- use crate :: { MultiUseSandbox , Result , SingleUseSandbox } ;
23
+ use crate :: { MultiUseSandbox , Result } ;
24
24
/// A context for calling guest functions.
25
25
///
26
26
/// Takes ownership of an existing `MultiUseSandbox`.
@@ -99,100 +99,6 @@ impl MultiUseGuestCallContext {
99
99
}
100
100
}
101
101
102
- /// A context for calling guest functions. Can only be created from an existing
103
- /// `SingleUseSandbox`, and once created, guest functions against that sandbox
104
- /// can be made from this until it is dropped.
105
- #[ derive( Debug ) ]
106
- pub struct SingleUseGuestCallContext {
107
- sbox : SingleUseSandbox ,
108
- }
109
-
110
- impl SingleUseGuestCallContext {
111
- /// Take ownership of a `SingleUseSandbox` and
112
- /// return a new `SingleUseGuestCallContext` instance.
113
- ///
114
- #[ instrument( skip_all, parent = Span :: current( ) ) ]
115
- pub ( crate ) fn start ( sbox : SingleUseSandbox ) -> Self {
116
- Self { sbox }
117
- }
118
-
119
- /// Call the guest function called `func_name` with the given arguments
120
- /// `args`, and expect the return value have the same type as
121
- /// `func_ret_type`.
122
- ///
123
- /// Once the call is complete, the 'SingleUseSandbox' will no longer be useable and a new one will need to be created.
124
- ///
125
- /// Rather than call this method directly, consider using the `call_guest_function_by_name` method on the `SingleUseSandbox`
126
-
127
- #[ instrument( err( Debug ) , skip( self , args) , parent = Span :: current( ) ) ]
128
- pub ( crate ) fn call (
129
- mut self ,
130
- func_name : & str ,
131
- func_ret_type : ReturnType ,
132
- args : Option < Vec < ParameterValue > > ,
133
- ) -> Result < ReturnValue > {
134
- self . call_internal ( func_name, func_ret_type, args)
135
- }
136
-
137
- // Internal call function that takes a mutable reference to self
138
- // This function allows a SingleUseMultiGuestCallContext to be used to make multiple calls to guest functions
139
- // before it is no longer usable.
140
- #[ instrument( skip_all, parent = Span :: current( ) ) ]
141
- fn call_internal (
142
- & mut self ,
143
- func_name : & str ,
144
- func_ret_type : ReturnType ,
145
- args : Option < Vec < ParameterValue > > ,
146
- ) -> Result < ReturnValue > {
147
- // We are guaranteed to be holding a lock now, since `self` can't
148
- // exist without doing so. since GuestCallContext is effectively
149
- // !Send (and !Sync), we also don't need to worry about
150
- // synchronization
151
-
152
- call_function_on_guest ( & mut self . sbox , func_name, func_ret_type, args)
153
- }
154
-
155
- /// This function allows for a `SingleUseSandbox` to be used to make multiple calls to guest functions before it is dropped.
156
- ///
157
- /// The function is passed a callback function that it then callsd with a reference to a 'SingleUseMultiGuestCallContext'
158
- /// that can be used to make multiple calls to guest functions.
159
- ///
160
- pub fn call_from_func <
161
- Fn : FnOnce ( & mut SingleUseMultiGuestCallContext ) -> Result < ReturnValue > ,
162
- > (
163
- self ,
164
- f : Fn ,
165
- ) -> Result < ReturnValue > {
166
- let mut ctx = SingleUseMultiGuestCallContext :: new ( self ) ;
167
- f ( & mut ctx)
168
- }
169
- }
170
-
171
- /// A context for making multiple calls to guest functions in a SingleUseSandbox. Can only be created
172
- /// from an existing SingleUseGuestCallContext using the `call_using_closure` method.
173
- /// Once created, calls to guest functions may be made through this context until it is dropped.
174
- /// Once dropped the underlying `SingleUseGuestCallContext` and associated `SingleUseSandbox` will be dropped
175
- pub struct SingleUseMultiGuestCallContext {
176
- call_context : SingleUseGuestCallContext ,
177
- }
178
-
179
- impl SingleUseMultiGuestCallContext {
180
- fn new ( call_context : SingleUseGuestCallContext ) -> Self {
181
- Self { call_context }
182
- }
183
-
184
- /// Call the guest function called `func_name` with the given arguments
185
- pub fn call (
186
- & mut self ,
187
- func_name : & str ,
188
- func_ret_type : ReturnType ,
189
- args : Option < Vec < ParameterValue > > ,
190
- ) -> Result < ReturnValue > {
191
- self . call_context
192
- . call_internal ( func_name, func_ret_type, args)
193
- }
194
- }
195
-
196
102
#[ cfg( test) ]
197
103
mod tests {
198
104
use std:: sync:: mpsc:: sync_channel;
@@ -203,13 +109,9 @@ mod tests {
203
109
} ;
204
110
use hyperlight_testing:: simple_guest_as_string;
205
111
206
- use crate :: func:: call_ctx:: SingleUseMultiGuestCallContext ;
207
112
use crate :: sandbox_state:: sandbox:: EvolvableSandbox ;
208
113
use crate :: sandbox_state:: transition:: Noop ;
209
- use crate :: {
210
- GuestBinary , HyperlightError , MultiUseSandbox , Result , SingleUseSandbox ,
211
- UninitializedSandbox ,
212
- } ;
114
+ use crate :: { GuestBinary , HyperlightError , MultiUseSandbox , Result , UninitializedSandbox } ;
213
115
214
116
fn new_uninit ( ) -> Result < UninitializedSandbox > {
215
117
let path = simple_guest_as_string ( ) . map_err ( |e| {
@@ -218,68 +120,6 @@ mod tests {
218
120
UninitializedSandbox :: new ( GuestBinary :: FilePath ( path) , None , None , None )
219
121
}
220
122
221
- /// Test to create a `SingleUseSandbox`, then call several guest
222
- /// functions sequentially.
223
- #[ test]
224
- fn singleusesandbox_single_call ( ) {
225
- let calls = [
226
- (
227
- "StackAllocate" ,
228
- ReturnType :: Int ,
229
- Some ( vec ! [ ParameterValue :: Int ( 1 ) ] ) ,
230
- ReturnValue :: Int ( 1 ) ,
231
- ) ,
232
- (
233
- "CallMalloc" ,
234
- ReturnType :: Int ,
235
- Some ( vec ! [ ParameterValue :: Int ( 200 ) ] ) ,
236
- ReturnValue :: Int ( 200 ) ,
237
- ) ,
238
- ] ;
239
-
240
- for call in calls. iter ( ) {
241
- let sbox: SingleUseSandbox = new_uninit ( ) . unwrap ( ) . evolve ( Noop :: default ( ) ) . unwrap ( ) ;
242
- let ctx = sbox. new_call_context ( ) ;
243
- let res = ctx. call ( call. 0 , call. 1 , call. 2 . clone ( ) ) . unwrap ( ) ;
244
- assert_eq ! ( call. 3 , res) ;
245
- }
246
- }
247
-
248
- #[ test]
249
- fn singleusesandbox_multi_call ( ) {
250
- let calls = [
251
- (
252
- "StackAllocate" ,
253
- ReturnType :: Int ,
254
- Some ( vec ! [ ParameterValue :: Int ( 1 ) ] ) ,
255
- ReturnValue :: Int ( 1 ) ,
256
- ) ,
257
- (
258
- "CallMalloc" ,
259
- ReturnType :: Int ,
260
- Some ( vec ! [ ParameterValue :: Int ( 200 ) ] ) ,
261
- ReturnValue :: Int ( 200 ) ,
262
- ) ,
263
- ] ;
264
-
265
- let sbox: SingleUseSandbox = new_uninit ( ) . unwrap ( ) . evolve ( Noop :: default ( ) ) . unwrap ( ) ;
266
- let ctx = sbox. new_call_context ( ) ;
267
-
268
- let callback_closure = |ctx : & mut SingleUseMultiGuestCallContext | {
269
- let mut res: ReturnValue = ReturnValue :: Int ( 0 ) ;
270
- for call in calls. iter ( ) {
271
- res = ctx
272
- . call ( call. 0 , call. 1 , call. 2 . clone ( ) )
273
- . expect ( "failed to call guest function" ) ;
274
- assert_eq ! ( call. 3 , res) ;
275
- }
276
- Ok ( res)
277
- } ;
278
-
279
- let res = ctx. call_from_func ( callback_closure) . unwrap ( ) ;
280
- assert_eq ! ( calls. last( ) . unwrap( ) . 3 , res) ;
281
- }
282
-
283
123
/// Test to create a `MultiUseSandbox`, then call several guest functions
284
124
/// on it across different threads.
285
125
///
0 commit comments