@@ -24,6 +24,54 @@ use crate::InstantiationStrategy;
24
24
25
25
type HostFunctions = sp_io:: SubstrateHostFunctions ;
26
26
27
+ #[ macro_export]
28
+ macro_rules! test_wasm_execution {
29
+ ( @no_legacy_instance_reuse $method_name: ident) => {
30
+ paste:: item! {
31
+ #[ test]
32
+ fn [ <$method_name _recreate_instance_cow>] ( ) {
33
+ $method_name(
34
+ InstantiationStrategy :: RecreateInstanceCopyOnWrite
35
+ ) ;
36
+ }
37
+
38
+ #[ test]
39
+ fn [ <$method_name _recreate_instance_vanilla>] ( ) {
40
+ $method_name(
41
+ InstantiationStrategy :: RecreateInstance
42
+ ) ;
43
+ }
44
+
45
+ #[ test]
46
+ fn [ <$method_name _pooling_cow>] ( ) {
47
+ $method_name(
48
+ InstantiationStrategy :: PoolingCopyOnWrite
49
+ ) ;
50
+ }
51
+
52
+ #[ test]
53
+ fn [ <$method_name _pooling_vanilla>] ( ) {
54
+ $method_name(
55
+ InstantiationStrategy :: Pooling
56
+ ) ;
57
+ }
58
+ }
59
+ } ;
60
+
61
+ ( $method_name: ident) => {
62
+ test_wasm_execution!( @no_legacy_instance_reuse $method_name) ;
63
+
64
+ paste:: item! {
65
+ #[ test]
66
+ fn [ <$method_name _legacy_instance_reuse>] ( ) {
67
+ $method_name(
68
+ InstantiationStrategy :: LegacyInstanceReuse
69
+ ) ;
70
+ }
71
+ }
72
+ } ;
73
+ }
74
+
27
75
struct RuntimeBuilder {
28
76
code : Option < String > ,
29
77
instantiation_strategy : InstantiationStrategy ,
@@ -36,12 +84,10 @@ struct RuntimeBuilder {
36
84
}
37
85
38
86
impl RuntimeBuilder {
39
- /// Returns a new builder that won't use the fast instance reuse mechanism, but instead will
40
- /// create a new runtime instance each time.
41
- fn new_on_demand ( ) -> Self {
87
+ fn new ( instantiation_strategy : InstantiationStrategy ) -> Self {
42
88
Self {
43
89
code : None ,
44
- instantiation_strategy : InstantiationStrategy :: RecreateInstance ,
90
+ instantiation_strategy,
45
91
canonicalize_nans : false ,
46
92
deterministic_stack : false ,
47
93
extra_heap_pages : 1024 ,
@@ -128,9 +174,9 @@ impl RuntimeBuilder {
128
174
}
129
175
}
130
176
131
- # [ test ]
132
- fn test_nan_canonicalization ( ) {
133
- let mut builder = RuntimeBuilder :: new_on_demand ( ) . canonicalize_nans ( true ) ;
177
+ test_wasm_execution ! ( test_nan_canonicalization ) ;
178
+ fn test_nan_canonicalization ( instantiation_strategy : InstantiationStrategy ) {
179
+ let mut builder = RuntimeBuilder :: new ( instantiation_strategy ) . canonicalize_nans ( true ) ;
134
180
let runtime = builder. build ( ) ;
135
181
136
182
let mut instance = runtime. new_instance ( ) . expect ( "failed to instantiate a runtime" ) ;
@@ -166,11 +212,11 @@ fn test_nan_canonicalization() {
166
212
assert_eq ! ( res, CANONICAL_NAN_BITS ) ;
167
213
}
168
214
169
- # [ test ]
170
- fn test_stack_depth_reaching ( ) {
215
+ test_wasm_execution ! ( test_stack_depth_reaching ) ;
216
+ fn test_stack_depth_reaching ( instantiation_strategy : InstantiationStrategy ) {
171
217
const TEST_GUARD_PAGE_SKIP : & str = include_str ! ( "test-guard-page-skip.wat" ) ;
172
218
173
- let mut builder = RuntimeBuilder :: new_on_demand ( )
219
+ let mut builder = RuntimeBuilder :: new ( instantiation_strategy )
174
220
. use_wat ( TEST_GUARD_PAGE_SKIP . to_string ( ) )
175
221
. deterministic_stack ( true ) ;
176
222
@@ -186,33 +232,46 @@ fn test_stack_depth_reaching() {
186
232
}
187
233
}
188
234
189
- #[ test]
190
- fn test_max_memory_pages_imported_memory_without_precompilation ( ) {
191
- test_max_memory_pages ( true , false ) ;
235
+ test_wasm_execution ! ( test_max_memory_pages_imported_memory_without_precompilation) ;
236
+ fn test_max_memory_pages_imported_memory_without_precompilation (
237
+ instantiation_strategy : InstantiationStrategy ,
238
+ ) {
239
+ test_max_memory_pages ( instantiation_strategy, true , false ) ;
192
240
}
193
241
194
- #[ test]
195
- fn test_max_memory_pages_exported_memory_without_precompilation ( ) {
196
- test_max_memory_pages ( false , false ) ;
242
+ test_wasm_execution ! ( test_max_memory_pages_exported_memory_without_precompilation) ;
243
+ fn test_max_memory_pages_exported_memory_without_precompilation (
244
+ instantiation_strategy : InstantiationStrategy ,
245
+ ) {
246
+ test_max_memory_pages ( instantiation_strategy, false , false ) ;
197
247
}
198
248
199
- #[ test]
200
- fn test_max_memory_pages_imported_memory_with_precompilation ( ) {
201
- test_max_memory_pages ( true , true ) ;
249
+ test_wasm_execution ! ( @no_legacy_instance_reuse test_max_memory_pages_imported_memory_with_precompilation) ;
250
+ fn test_max_memory_pages_imported_memory_with_precompilation (
251
+ instantiation_strategy : InstantiationStrategy ,
252
+ ) {
253
+ test_max_memory_pages ( instantiation_strategy, true , true ) ;
202
254
}
203
255
204
- #[ test]
205
- fn test_max_memory_pages_exported_memory_with_precompilation ( ) {
206
- test_max_memory_pages ( false , true ) ;
256
+ test_wasm_execution ! ( @no_legacy_instance_reuse test_max_memory_pages_exported_memory_with_precompilation) ;
257
+ fn test_max_memory_pages_exported_memory_with_precompilation (
258
+ instantiation_strategy : InstantiationStrategy ,
259
+ ) {
260
+ test_max_memory_pages ( instantiation_strategy, false , true ) ;
207
261
}
208
262
209
- fn test_max_memory_pages ( import_memory : bool , precompile_runtime : bool ) {
263
+ fn test_max_memory_pages (
264
+ instantiation_strategy : InstantiationStrategy ,
265
+ import_memory : bool ,
266
+ precompile_runtime : bool ,
267
+ ) {
210
268
fn try_instantiate (
211
269
max_memory_size : Option < usize > ,
212
270
wat : String ,
271
+ instantiation_strategy : InstantiationStrategy ,
213
272
precompile_runtime : bool ,
214
273
) -> Result < ( ) , Box < dyn std:: error:: Error > > {
215
- let mut builder = RuntimeBuilder :: new_on_demand ( )
274
+ let mut builder = RuntimeBuilder :: new ( instantiation_strategy )
216
275
. use_wat ( wat)
217
276
. max_memory_size ( max_memory_size)
218
277
. precompile_runtime ( precompile_runtime) ;
@@ -265,6 +324,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
265
324
*/
266
325
memory( 64511 , None , import_memory)
267
326
) ,
327
+ instantiation_strategy,
268
328
precompile_runtime,
269
329
)
270
330
. unwrap ( ) ;
@@ -288,6 +348,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
288
348
// 1 initial, max is not specified.
289
349
memory( 1 , None , import_memory)
290
350
) ,
351
+ instantiation_strategy,
291
352
precompile_runtime,
292
353
)
293
354
. unwrap ( ) ;
@@ -309,6 +370,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
309
370
// Max is 2048.
310
371
memory( 1 , Some ( 2048 ) , import_memory)
311
372
) ,
373
+ instantiation_strategy,
312
374
precompile_runtime,
313
375
)
314
376
. unwrap ( ) ;
@@ -342,6 +404,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
342
404
// Zero starting pages.
343
405
memory( 0 , None , import_memory)
344
406
) ,
407
+ instantiation_strategy,
345
408
precompile_runtime,
346
409
)
347
410
. unwrap ( ) ;
@@ -375,6 +438,7 @@ fn test_max_memory_pages(import_memory: bool, precompile_runtime: bool) {
375
438
// Initial=1, meaning after heap pages mount the total will be already 1025.
376
439
memory( 1 , None , import_memory)
377
440
) ,
441
+ instantiation_strategy,
378
442
precompile_runtime,
379
443
)
380
444
. unwrap ( ) ;
0 commit comments