unc-vm-runner 0.12.2

This crate implements the specification of the interface that unc blockchain exposes to the smart contracts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Host function interface for smart contracts.
//!
//! Besides native WASM operations, smart contracts can call into runtime to
//! gain access to extra functionality, like operations with store. Such
//! "extras" are called "Host function", and play a role similar to syscalls. In
//! this module, we integrate host functions with various wasm runtimes we
//! support. The actual definitions of host functions live in the `vm-logic`
//! crate.
//!
//! Basically, what the following code does is (in pseudo-code):
//!
//! ```ignore
//! for host_fn in all_host_functions {
//!    wasm_imports.define("env", host_fn.name, |args| host_fn(args))
//! }
//! ```
//!
//! The actual implementation is a bit more complicated, for two reasons. First,
//! host functions have different signatures, so there isn't a trivial single
//! type one can use to hold a host function. Second, we want to use direct
//! calls in the compiled WASM, so we need to avoid dynamic dispatch and hand
//! functions as ZSTs to the WASM runtimes. This basically means that we need to
//! code the above for-loop as a macro.
//!
//! So, the `imports!` macro invocation is the main "public" API -- it just list
//! all host functions with their signatures. `imports! { foo, bar, baz }`
//! expands to roughly
//!
//! ```ignore
//! macro_rules! for_each_available_import {
//!    $($M:ident) => {
//!        $M!(foo);
//!        $M!(bar);
//!        $M!(baz);
//!    }
//! }
//! ```
//!
//! That is, `for_each_available_import` is a high-order macro which takes macro
//! `M` as a parameter, and calls `M!` with each import. Each supported WASM
//! runtime (see submodules of this module) then calls
//! `for_each_available_import` with its own import definition logic.
//!
//! The real `for_each_available_import` takes one more argument --
//! `VMConfig`. We can add new imports, but we must make sure that they
//! are only available to contracts at a specific protocol version -- we can't
//! make imports retroactively available to old transactions. So
//! `for_each_available_import` takes care to invoke `M!` only for currently
//! available imports.
#[allow(unused_macros)]
macro_rules! call_with_name {
    ( $M:ident => @in $mod:ident : $func:ident < [ $( $arg_name:ident : $arg_type:ident ),* ] -> [ $( $returns:ident ),* ] > ) => {
        $M!($mod / $func : $func < [ $( $arg_name : $arg_type ),* ] -> [ $( $returns ),* ] >)
    };
    ( $M:ident => @as $name:ident : $func:ident < [ $( $arg_name:ident : $arg_type:ident ),* ] -> [ $( $returns:ident ),* ] > ) => {
        $M!(env / $name : $func < [ $( $arg_name : $arg_type ),* ] -> [ $( $returns ),* ] >)
    };
    ( $M:ident => $func:ident < [ $( $arg_name:ident : $arg_type:ident ),* ] -> [ $( $returns:ident ),* ] > ) => {
        $M!(env / $func : $func < [ $( $arg_name : $arg_type ),* ] -> [ $( $returns ),* ] >)
    };
}

macro_rules! imports {
    (
      $($(#[$config_field:ident])? $(##[$feature_name:literal])?
        $( @in $mod:ident : )?
        $( @as $name:ident : )?
        $func:ident < [ $( $arg_name:ident : $arg_type:ident ),* ] -> [ $( $returns:ident ),* ] >,)*
    ) => {
        #[allow(unused_macros)]
        macro_rules! for_each_available_import {
            ($config:expr, $M:ident) => {$(
                $(#[cfg(feature = $feature_name)])?
                if true $(&& ($config).$config_field)? {
                    call_with_name!($M => $( @in $mod : )? $( @as $name : )? $func < [ $( $arg_name : $arg_type ),* ] -> [ $( $returns ),* ] >);
                }
            )*}
        }
    }
}

imports! {
    // #########################
    // # Finite-wasm internals #
    // #########################
    @in internal: finite_wasm_gas<[gas: u64] -> []>,
    @in internal: finite_wasm_stack<[operand_size: u64, frame_size: u64] -> []>,
    @in internal: finite_wasm_unstack<[operand_size: u64, frame_size: u64] -> []>,
    // #############
    // # Registers #
    // #############
    read_register<[register_id: u64, ptr: u64] -> []>,
    register_len<[register_id: u64] -> [u64]>,
    write_register<[register_id: u64, data_len: u64, data_ptr: u64] -> []>,
    // ###############
    // # Context API #
    // ###############
    current_account_id<[register_id: u64] -> []>,
    signer_account_id<[register_id: u64] -> []>,
    signer_account_pk<[register_id: u64] -> []>,
    predecessor_account_id<[register_id: u64] -> []>,
    input<[register_id: u64] -> []>,
    block_index<[] -> [u64]>,
    block_timestamp<[] -> [u64]>,
    epoch_height<[] -> [u64]>,
    storage_usage<[] -> [u64]>,
    // #################
    // # Economics API #
    // #################
    account_balance<[balance_ptr: u64] -> []>,
    account_locked_balance<[balance_ptr: u64] -> []>,
    attached_deposit<[balance_ptr: u64] -> []>,
    prepaid_gas<[] -> [u64]>,
    used_gas<[] -> [u64]>,
    // ############
    // # Math API #
    // ############
    random_seed<[register_id: u64] -> []>,
    sha256<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
    keccak256<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
    keccak512<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
    #[ed25519_verify] ed25519_verify<[sig_len: u64,
        sig_ptr: u64,
        msg_len: u64,
        msg_ptr: u64,
        pub_key_len: u64,
        pub_key_ptr: u64
    ] -> [u64]>,
    #[math_extension] ripemd160<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
    #[math_extension] ecrecover<[hash_len: u64, hash_ptr: u64, sign_len: u64, sig_ptr: u64, v: u64, malleability_flag: u64, register_id: u64] -> [u64]>,
    // #####################
    // # Miscellaneous API #
    // #####################
    value_return<[value_len: u64, value_ptr: u64] -> []>,
    panic<[] -> []>,
    panic_utf8<[len: u64, ptr: u64] -> []>,
    log_utf8<[len: u64, ptr: u64] -> []>,
    log_utf16<[len: u64, ptr: u64] -> []>,
    abort<[msg_ptr: u32, filename_ptr: u32, line: u32, col: u32] -> []>,
    // ################
    // # Promises API #
    // ################
    promise_create<[
        account_id_len: u64,
        account_id_ptr: u64,
        method_name_len: u64,
        method_name_ptr: u64,
        arguments_len: u64,
        arguments_ptr: u64,
        amount_ptr: u64,
        gas: u64
    ] -> [u64]>,
    promise_then<[
        promise_index: u64,
        account_id_len: u64,
        account_id_ptr: u64,
        method_name_len: u64,
        method_name_ptr: u64,
        arguments_len: u64,
        arguments_ptr: u64,
        amount_ptr: u64,
        gas: u64
    ] -> [u64]>,
    promise_and<[promise_idx_ptr: u64, promise_idx_count: u64] -> [u64]>,
    promise_batch_create<[account_id_len: u64, account_id_ptr: u64] -> [u64]>,
    promise_batch_then<[promise_index: u64, account_id_len: u64, account_id_ptr: u64] -> [u64]>,
    // #######################
    // # Promise API actions #
    // #######################
    promise_batch_action_create_account<[promise_index: u64] -> []>,
    promise_batch_action_deploy_contract<[promise_index: u64, code_len: u64, code_ptr: u64] -> []>,
    promise_batch_action_function_call<[
        promise_index: u64,
        method_name_len: u64,
        method_name_ptr: u64,
        arguments_len: u64,
        arguments_ptr: u64,
        amount_ptr: u64,
        gas: u64
    ] -> []>,
    #[function_call_weight] promise_batch_action_function_call_weight<[
        promise_index: u64,
        method_name_len: u64,
        method_name_ptr: u64,
        arguments_len: u64,
        arguments_ptr: u64,
        amount_ptr: u64,
        gas: u64,
        gas_weight: u64
    ] -> []>,
    promise_batch_action_transfer<[promise_index: u64, amount_ptr: u64] -> []>,
    promise_batch_action_stake<[
        promise_index: u64,
        amount_ptr: u64,
        public_key_len: u64,
        public_key_ptr: u64
    ] -> []>,
    promise_batch_action_add_key_with_full_access<[
        promise_index: u64,
        public_key_len: u64,
        public_key_ptr: u64,
        nonce: u64
    ] -> []>,
    promise_batch_action_add_key_with_function_call<[
        promise_index: u64,
        public_key_len: u64,
        public_key_ptr: u64,
        nonce: u64,
        allowance_ptr: u64,
        receiver_id_len: u64,
        receiver_id_ptr: u64,
        method_names_len: u64,
        method_names_ptr: u64
    ] -> []>,
    promise_batch_action_delete_key<[
        promise_index: u64,
        public_key_len: u64,
        public_key_ptr: u64
    ] -> []>,
    promise_batch_action_delete_account<[
        promise_index: u64,
        beneficiary_id_len: u64,
        beneficiary_id_ptr: u64
    ] -> []>,
    // #######################
    // # Promise API results #
    // #######################
    promise_results_count<[] -> [u64]>,
    promise_result<[result_idx: u64, register_id: u64] -> [u64]>,
    promise_return<[promise_idx: u64] -> []>,
    // ###############
    // # Storage API #
    // ###############
    storage_write<[key_len: u64, key_ptr: u64, value_len: u64, value_ptr: u64, register_id: u64] -> [u64]>,
    storage_read<[key_len: u64, key_ptr: u64, register_id: u64] -> [u64]>,
    storage_remove<[key_len: u64, key_ptr: u64, register_id: u64] -> [u64]>,
    storage_has_key<[key_len: u64, key_ptr: u64] -> [u64]>,
    storage_iter_prefix<[prefix_len: u64, prefix_ptr: u64] -> [u64]>,
    storage_iter_range<[start_len: u64, start_ptr: u64, end_len: u64, end_ptr: u64] -> [u64]>,
    storage_iter_next<[iterator_id: u64, key_register_id: u64, value_register_id: u64] -> [u64]>,
    // Function for the injected gas counter. Automatically called by the gas meter.
    @as gas: gas_seen_from_wasm<[gas_amount: u32] -> []>,
    // ###############
    // # Validator API #
    // ###############
    validator_stake<[account_id_len: u64, account_id_ptr: u64, stake_ptr: u64] -> []>,
    validator_total_stake<[stake_ptr: u64] -> []>,
    validator_power<[account_id_len: u64, account_id_ptr: u64, power_ptr: u64] -> []>,
    validator_total_power<[power_ptr: u64] -> []>,
    // #############
    // # Alt BN128 #
    // #############
    #[alt_bn128] alt_bn128_g1_multiexp<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
    #[alt_bn128] alt_bn128_g1_sum<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
    #[alt_bn128] alt_bn128_pairing_check<[value_len: u64, value_ptr: u64] -> [u64]>,
    // #############
    // #  Sandbox  #
    // #############
    ##["sandbox"] sandbox_debug_log<[len: u64, ptr: u64] -> []>,
}

#[cfg(all(feature = "wasmer0_vm", target_arch = "x86_64"))]
pub(crate) mod wasmer {
    use super::str_eq;
    use crate::logic::{VMLogic, VMLogicError};
    use std::ffi::c_void;

    #[derive(Clone, Copy)]
    struct ImportReference(pub *mut c_void);
    unsafe impl Send for ImportReference {}
    unsafe impl Sync for ImportReference {}

    pub(crate) fn build(
        memory: wasmer_runtime::memory::Memory,
        logic: &mut VMLogic<'_>,
    ) -> wasmer_runtime::ImportObject {
        let raw_ptr = logic as *mut _ as *mut c_void;
        let import_reference = ImportReference(raw_ptr);
        let mut import_object = wasmer_runtime::ImportObject::new_with_data(move || {
            let dtor = (|_: *mut c_void| {}) as fn(*mut c_void);
            ({ import_reference }.0, dtor)
        });

        let mut ns_internal = wasmer_runtime_core::import::Namespace::new();
        let mut ns_env = wasmer_runtime_core::import::Namespace::new();
        ns_env.insert("memory", memory);

        macro_rules! add_import {
            (
              $mod:ident / $name:ident : $func:ident < [ $( $arg_name:ident : $arg_type:ident ),* ] -> [ $( $returns:ident ),* ] >
            ) => {
                #[allow(unused_parens)]
                fn $name( ctx: &mut wasmer_runtime::Ctx, $( $arg_name: $arg_type ),* ) -> Result<($( $returns ),*), VMLogicError> {
                    const IS_GAS: bool = str_eq(stringify!($name), "gas") || str_eq(stringify!($name), "finite_wasm_gas");
                    let _span = if IS_GAS {
                        None
                    } else {
                        Some(tracing::trace_span!(target: "host-function", stringify!($name)).entered())
                    };
                    let logic: &mut VMLogic<'_> = unsafe { &mut *(ctx.data as *mut VMLogic<'_>) };
                    logic.$func( $( $arg_name, )* )
                }

                match stringify!($mod) {
                    "env" => ns_env.insert(stringify!($name), wasmer_runtime::func!($name)),
                    "internal" => ns_internal.insert(stringify!($name), wasmer_runtime::func!($name)),
                    _ => unimplemented!(),
                }
            };
        }
        for_each_available_import!(logic.config, add_import);

        import_object.register("env", ns_env);
        import_object.register("internal", ns_internal);
        import_object
    }
}

#[cfg(all(feature = "wasmer2_vm", target_arch = "x86_64"))]
pub(crate) mod wasmer2 {
    use std::sync::Arc;

    use super::str_eq;
    use crate::logic::VMLogic;
    use wasmer_engine::Engine;
    use wasmer_engine_universal::UniversalEngine;
    use wasmer_vm::{
        ExportFunction, ExportFunctionMetadata, Resolver, VMFunction, VMFunctionKind, VMMemory,
    };

    pub(crate) struct Wasmer2Imports<'engine, 'vmlogic, 'vmlogic_refs> {
        pub(crate) memory: VMMemory,
        // Note: this same object is also referenced by the `metadata` field!
        pub(crate) vmlogic: &'vmlogic mut VMLogic<'vmlogic_refs>,
        pub(crate) metadata: Arc<ExportFunctionMetadata>,
        pub(crate) engine: &'engine UniversalEngine,
    }

    trait Wasmer2Type {
        type Wasmer;
        fn to_wasmer(self) -> Self::Wasmer;
        fn ty() -> wasmer_types::Type;
    }
    macro_rules! wasmer_types {
        ($($native:ty as $wasmer:ty => $type_expr:expr;)*) => {
            $(impl Wasmer2Type for $native {
                type Wasmer = $wasmer;
                fn to_wasmer(self) -> $wasmer {
                    self as _
                }
                fn ty() -> wasmer_types::Type {
                    $type_expr
                }
            })*
        }
    }
    wasmer_types! {
        u32 as i32 => wasmer_types::Type::I32;
        u64 as i64 => wasmer_types::Type::I64;
    }

    macro_rules! return_ty {
        ($return_type: ident = [ ]) => {
            type $return_type = ();
            fn make_ret() -> () {}
        };
        ($return_type: ident = [ $($returns: ident),* ]) => {
            #[repr(C)]
            struct $return_type($(<$returns as Wasmer2Type>::Wasmer),*);
            fn make_ret($($returns: $returns),*) -> Ret { Ret($($returns.to_wasmer()),*) }
        }
    }

    impl<'e, 'l, 'lr> Resolver for Wasmer2Imports<'e, 'l, 'lr> {
        fn resolve(&self, _index: u32, module: &str, field: &str) -> Option<wasmer_vm::Export> {
            if module == "env" && field == "memory" {
                return Some(wasmer_vm::Export::Memory(self.memory.clone()));
            }

            macro_rules! add_import {
                (
                  $mod:ident / $name:ident : $func:ident <
                    [ $( $arg_name:ident : $arg_type:ident ),* ]
                    -> [ $( $returns:ident ),* ]
                  >
                ) => {
                    return_ty!(Ret = [ $($returns),* ]);

                    extern "C" fn $name(env: *mut VMLogic<'_>, $( $arg_name: $arg_type ),* )
                    -> Ret {
                        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                            const IS_GAS: bool = str_eq(stringify!($name), "gas") || str_eq(stringify!($name), "finite_wasm_gas");
                            let _span = if IS_GAS {
                                None
                            } else {
                                Some(tracing::trace_span!(
                                    target: "host-function",
                                    stringify!($name)
                                ).entered())
                            };

                            // SAFETY: This code should only be executable within `'vmlogic`
                            // lifetime and so it is safe to dereference the `env` pointer which is
                            // known to be derived from a valid `&'vmlogic mut VMLogic<'_>` in the
                            // first place.
                            unsafe { (*env).$func( $( $arg_name, )* ) }
                        }));
                        // We want to ensure that the only kind of error that host function calls
                        // return are VMLogicError. This is important because we later attempt to
                        // downcast the `RuntimeError`s into `VMLogicError`.
                        let result: Result<Result<_, crate::logic::VMLogicError>, _>  = result;
                        #[allow(unused_parens)]
                        match result {
                            Ok(Ok(($($returns),*))) => make_ret($($returns),*),
                            Ok(Err(trap)) => unsafe {
                                // SAFETY: this can only be called by a WASM contract, so all the
                                // necessary hooks are known to be in place.
                                wasmer_vm::raise_user_trap(Box::new(trap))
                            },
                            Err(e) => unsafe {
                                // SAFETY: this can only be called by a WASM contract, so all the
                                // necessary hooks are known to be in place.
                                wasmer_vm::resume_panic(e)
                            },
                        }
                    }
                    // TODO: a phf hashmap would probably work better here.
                    if module == stringify!($mod) && field == stringify!($name) {
                        let args = [$(<$arg_type as Wasmer2Type>::ty()),*];
                        let rets = [$(<$returns as Wasmer2Type>::ty()),*];
                        let signature = wasmer_types::FunctionTypeRef::new(&args[..], &rets[..]);
                        let signature = self.engine.register_signature(signature);
                        return Some(wasmer_vm::Export::Function(ExportFunction {
                            vm_function: VMFunction {
                                address: $name as *const _,
                                // SAFETY: here we erase the lifetime of the `vmlogic` reference,
                                // but we believe that the lifetimes on `Wasmer2Imports` enforce
                                // sufficiently that it isn't possible to call this exported
                                // function when vmlogic is no loger live.
                                vmctx: wasmer_vm::VMFunctionEnvironment {
                                    host_env: self.vmlogic as *const _ as *mut _
                                },
                                signature,
                                kind: VMFunctionKind::Static,
                                call_trampoline: None,
                                instance_ref: None,
                            },
                            metadata: Some(Arc::clone(&self.metadata)),
                        }));
                    }
                };
            }
            for_each_available_import!(self.vmlogic.config, add_import);
            return None;
        }
    }

    pub(crate) fn build<'e, 'a, 'b>(
        memory: VMMemory,
        logic: &'a mut VMLogic<'b>,
        engine: &'e UniversalEngine,
    ) -> Wasmer2Imports<'e, 'a, 'b> {
        let metadata = unsafe {
            // SAFETY: the functions here are thread-safe. We ensure that the lifetime of `VMLogic`
            // is sufficiently long by tying the lifetime of VMLogic to the return type which
            // contains this metadata.
            ExportFunctionMetadata::new(logic as *mut _ as *mut _, None, |ptr| ptr, |_| {})
        };
        Wasmer2Imports { memory, vmlogic: logic, metadata: Arc::new(metadata), engine }
    }
}

#[cfg(all(feature = "unc_vm", target_arch = "x86_64"))]
pub(crate) mod unc_vm {
    use std::sync::Arc;

    use super::str_eq;
    use crate::logic::VMLogic;
    use unc_vm_engine::universal::UniversalEngine;
    use unc_vm_vm::{
        ExportFunction, ExportFunctionMetadata, Resolver, VMFunction, VMFunctionKind, VMMemory,
    };

    pub(crate) struct UncVmImports<'engine, 'vmlogic, 'vmlogic_refs> {
        pub(crate) memory: VMMemory,
        // Note: this same object is also referenced by the `metadata` field!
        pub(crate) vmlogic: &'vmlogic mut VMLogic<'vmlogic_refs>,
        pub(crate) metadata: Arc<ExportFunctionMetadata>,
        pub(crate) engine: &'engine UniversalEngine,
    }

    trait UncVmType {
        type UncVm;
        fn to_unc_vm(self) -> Self::UncVm;
        fn ty() -> unc_vm_types::Type;
    }
    macro_rules! unc_vm_types {
        ($($native:ty as $unc_vm:ty => $type_expr:expr;)*) => {
            $(impl UncVmType for $native {
                type UncVm = $unc_vm;
                fn to_unc_vm(self) -> $unc_vm {
                    self as _
                }
                fn ty() -> unc_vm_types::Type {
                    $type_expr
                }
            })*
        }
    }
    unc_vm_types! {
        u32 as i32 => unc_vm_types::Type::I32;
        u64 as i64 => unc_vm_types::Type::I64;
    }

    macro_rules! return_ty {
        ($return_type: ident = [ ]) => {
            type $return_type = ();
            fn make_ret() -> () {}
        };
        ($return_type: ident = [ $($returns: ident),* ]) => {
            #[repr(C)]
            struct $return_type($(<$returns as UncVmType>::UncVm),*);
            fn make_ret($($returns: $returns),*) -> Ret { Ret($($returns.to_unc_vm()),*) }
        }
    }

    impl<'e, 'l, 'lr> Resolver for UncVmImports<'e, 'l, 'lr> {
        fn resolve(&self, _index: u32, module: &str, field: &str) -> Option<unc_vm_vm::Export> {
            if module == "env" && field == "memory" {
                return Some(unc_vm_vm::Export::Memory(self.memory.clone()));
            }

            macro_rules! add_import {
                (
                  $mod:ident / $name:ident : $func:ident <
                    [ $( $arg_name:ident : $arg_type:ident ),* ]
                    -> [ $( $returns:ident ),* ]
                  >
                ) => {
                    return_ty!(Ret = [ $($returns),* ]);

                    extern "C" fn $name(env: *mut VMLogic<'_>, $( $arg_name: $arg_type ),* )
                    -> Ret {
                        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                            const IS_GAS: bool = str_eq(stringify!($name), "gas") || str_eq(stringify!($name), "finite_wasm_gas");
                            let _span = if IS_GAS {
                                None
                            } else {
                                Some(tracing::trace_span!(
                                    target: "host-function",
                                    stringify!($name)
                                ).entered())
                            };

                            // SAFETY: This code should only be executable within `'vmlogic`
                            // lifetime and so it is safe to dereference the `env` pointer which is
                            // known to be derived from a valid `&'vmlogic mut VMLogic<'_>` in the
                            // first place.
                            unsafe { (*env).$func( $( $arg_name, )* ) }
                        }));
                        // We want to ensure that the only kind of error that host function calls
                        // return are VMLogicError. This is important because we later attempt to
                        // downcast the `RuntimeError`s into `VMLogicError`.
                        let result: Result<Result<_, crate::logic::VMLogicError>, _>  = result;
                        #[allow(unused_parens)]
                        match result {
                            Ok(Ok(($($returns),*))) => make_ret($($returns),*),
                            Ok(Err(trap)) => unsafe {
                                // SAFETY: this can only be called by a WASM contract, so all the
                                // necessary hooks are known to be in place.
                                unc_vm_vm::raise_user_trap(Box::new(trap))
                            },
                            Err(e) => unsafe {
                                // SAFETY: this can only be called by a WASM contract, so all the
                                // necessary hooks are known to be in place.
                                unc_vm_vm::resume_panic(e)
                            },
                        }
                    }
                    // TODO: a phf hashmap would probably work better here.
                    if module == stringify!($mod) && field == stringify!($name) {
                        let args = [$(<$arg_type as UncVmType>::ty()),*];
                        let rets = [$(<$returns as UncVmType>::ty()),*];
                        let signature = unc_vm_types::FunctionType::new(&args[..], &rets[..]);
                        let signature = self.engine.register_signature(signature);
                        return Some(unc_vm_vm::Export::Function(ExportFunction {
                            vm_function: VMFunction {
                                address: $name as *const _,
                                // SAFETY: here we erase the lifetime of the `vmlogic` reference,
                                // but we believe that the lifetimes on `UncVmImports` enforce
                                // sufficiently that it isn't possible to call this exported
                                // function when vmlogic is no loger live.
                                vmctx: unc_vm_vm::VMFunctionEnvironment {
                                    host_env: self.vmlogic as *const _ as *mut _
                                },
                                signature,
                                kind: VMFunctionKind::Static,
                                call_trampoline: None,
                                instance_ref: None,
                            },
                            metadata: Some(Arc::clone(&self.metadata)),
                        }));
                    }
                };
            }
            for_each_available_import!(self.vmlogic.config, add_import);
            return None;
        }
    }

    pub(crate) fn build<'e, 'a, 'b>(
        memory: VMMemory,
        logic: &'a mut VMLogic<'b>,
        engine: &'e UniversalEngine,
    ) -> UncVmImports<'e, 'a, 'b> {
        let metadata = unsafe {
            // SAFETY: the functions here are thread-safe. We ensure that the lifetime of `VMLogic`
            // is sufficiently long by tying the lifetime of VMLogic to the return type which
            // contains this metadata.
            ExportFunctionMetadata::new(logic as *mut _ as *mut _, None, |ptr| ptr, |_| {})
        };
        UncVmImports { memory, vmlogic: logic, metadata: Arc::new(metadata), engine }
    }
}

#[cfg(feature = "wasmtime_vm")]
pub(crate) mod wasmtime {
    use super::str_eq;
    use crate::logic::{VMLogic, VMLogicError};
    use std::cell::UnsafeCell;
    use std::ffi::c_void;

    /// This is a container from which an error can be taken out by value. This is necessary as
    /// `anyhow` does not really give any opportunity to grab causes by value and the VM Logic
    /// errors end up a couple layers deep in a causal chain.
    #[derive(Debug)]
    pub(crate) struct ErrorContainer(std::sync::Mutex<Option<VMLogicError>>);
    impl ErrorContainer {
        pub(crate) fn take(&self) -> Option<VMLogicError> {
            let mut guard = self.0.lock().unwrap_or_else(|e| e.into_inner());
            guard.take()
        }
    }
    impl std::error::Error for ErrorContainer {}
    impl std::fmt::Display for ErrorContainer {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("VMLogic error occurred and is now stored in an opaque storage container")
        }
    }

    thread_local! {
        static CALLER_CONTEXT: UnsafeCell<*mut c_void> = const { UnsafeCell::new(core::ptr::null_mut()) };
    }

    pub(crate) fn link<'a, 'b>(
        linker: &mut wasmtime::Linker<()>,
        memory: wasmtime::Memory,
        store: &wasmtime::Store<()>,
        logic: &'a mut VMLogic<'b>,
    ) {
        // Unfortunately, due to the Wasmtime implementation we have to do tricks with the
        // lifetimes of the logic instance and pass raw pointers here.
        // FIXME(nagisa): I believe this is no longer required, we just need to look at this code
        // again.
        let raw_logic = logic as *mut _ as *mut c_void;
        CALLER_CONTEXT.with(|caller_context| unsafe { *caller_context.get() = raw_logic });
        linker.define(store, "env", "memory", memory).expect("cannot define memory");

        macro_rules! add_import {
            (
              $mod:ident / $name:ident : $func:ident < [ $( $arg_name:ident : $arg_type:ident ),* ] -> [ $( $returns:ident ),* ] >
            ) => {
                #[allow(unused_parens)]
                fn $name(caller: wasmtime::Caller<'_, ()>, $( $arg_name: $arg_type ),* ) -> anyhow::Result<($( $returns ),*)> {
                    const IS_GAS: bool = str_eq(stringify!($name), "gas") || str_eq(stringify!($name), "finite_wasm_gas");
                    let _span = if IS_GAS {
                        None
                    } else {
                        Some(tracing::trace_span!(target: "host-function", stringify!($name)).entered())
                    };
                    // the below is bad. don't do this at home. it probably works thanks to the exact way the system is setup.
                    // Thanksfully, this doesn't run in production, and hopefully should be possible to remove before we even
                    // consider doing so.
                    let data = CALLER_CONTEXT.with(|caller_context| {
                        unsafe {
                            *caller_context.get()
                        }
                    });
                    unsafe {
                        // Transmute the lifetime of caller so it's possible to put it in a thread-local.
                        crate::wasmtime_runner::CALLER.with(|runner_caller| *runner_caller.borrow_mut() = std::mem::transmute(caller));
                    }
                    let logic: &mut VMLogic<'_> = unsafe { &mut *(data as *mut VMLogic<'_>) };
                    match logic.$func( $( $arg_name as $arg_type, )* ) {
                        Ok(result) => Ok(result as ($( $returns ),* ) ),
                        Err(err) => {
                            Err(ErrorContainer(std::sync::Mutex::new(Some(err))).into())
                        }
                    }
                }

                linker.func_wrap(stringify!($mod), stringify!($name), $name).expect("cannot link external");
            };
        }
        for_each_available_import!(logic.config, add_import);
    }
}

/// Constant-time string equality, work-around for `"foo" == "bar"` not working
/// in const context yet.
#[allow(dead_code)]
const fn str_eq(s1: &str, s2: &str) -> bool {
    let s1 = s1.as_bytes();
    let s2 = s2.as_bytes();
    if s1.len() != s2.len() {
        return false;
    }
    let mut i = 0;
    while i < s1.len() {
        if s1[i] != s2[i] {
            return false;
        }
        i += 1;
    }
    true
}