wasmer_compiler_cranelift/trampoline/
dynamic_function.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/main/docs/ATTRIBUTIONS.md
3
4//! A trampoline generator for calling dynamic host functions from Wasm.
5
6use crate::{
7    CraneliftCallbacks,
8    translator::{compiled_function_unwind_info, signature_to_cranelift_ir},
9};
10use cranelift_codegen::{
11    Context,
12    ir::{self, Function, InstBuilder, MemFlags, StackSlotData, StackSlotKind, UserFuncName},
13    isa::TargetIsa,
14};
15use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
16use std::{cmp, mem};
17use target_lexicon::Architecture;
18use wasmer_compiler::{misc::CompiledKind, types::function::FunctionBody};
19use wasmer_types::{CompileError, FunctionType, VMOffsets};
20
21/// Create a trampoline for invoking a WebAssembly function.
22pub fn make_trampoline_dynamic_function(
23    callbacks: &Option<CraneliftCallbacks>,
24    isa: &dyn TargetIsa,
25    arch: Architecture,
26    offsets: &VMOffsets,
27    fn_builder_ctx: &mut FunctionBuilderContext,
28    func_type: &FunctionType,
29) -> Result<FunctionBody, CompileError> {
30    let pointer_type = isa.pointer_type();
31    let frontend_config = isa.frontend_config();
32    let signature = signature_to_cranelift_ir(func_type, frontend_config);
33    let mut stub_sig = ir::Signature::new(frontend_config.default_call_conv);
34    // Add the caller `vmctx` parameter.
35    stub_sig.params.push(ir::AbiParam::special(
36        pointer_type,
37        ir::ArgumentPurpose::VMContext,
38    ));
39
40    // Add the `values_vec` parameter.
41    stub_sig.params.push(ir::AbiParam::new(pointer_type));
42
43    // Compute the size of the values vector. The vmctx and caller vmctx are passed separately.
44    let value_size = mem::size_of::<u128>();
45    let values_vec_len =
46        (value_size * cmp::max(signature.params.len() - 1, signature.returns.len())) as u32;
47
48    let mut context = Context::new();
49    context.func = Function::with_name_signature(UserFuncName::user(0, 0), signature.clone());
50
51    let ss = context.func.create_sized_stack_slot(StackSlotData::new(
52        StackSlotKind::ExplicitSlot,
53        values_vec_len,
54        0,
55    ));
56
57    {
58        let mut builder = FunctionBuilder::new(&mut context.func, fn_builder_ctx);
59        let block0 = builder.create_block();
60
61        builder.append_block_params_for_function_params(block0);
62        builder.switch_to_block(block0);
63        builder.seal_block(block0);
64
65        let values_vec_ptr_val = builder.ins().stack_addr(pointer_type, ss, 0);
66        let mflags = MemFlags::trusted();
67        // We only get the non-vmctx arguments
68        for i in 1..signature.params.len() {
69            let val = builder.func.dfg.block_params(block0)[i];
70            builder.ins().store(
71                mflags,
72                val,
73                values_vec_ptr_val,
74                ((i - 1) * value_size) as i32,
75            );
76        }
77
78        let block_params = builder.func.dfg.block_params(block0);
79        let vmctx_ptr_val = block_params[0];
80        let callee_args = vec![vmctx_ptr_val, values_vec_ptr_val];
81
82        let new_sig = builder.import_signature(stub_sig);
83
84        let mem_flags = ir::MemFlags::trusted();
85        let callee_value = builder.ins().load(
86            pointer_type,
87            mem_flags,
88            vmctx_ptr_val,
89            offsets.vmdynamicfunction_import_context_address() as i32,
90        );
91
92        builder
93            .ins()
94            .call_indirect(new_sig, callee_value, &callee_args);
95
96        let mflags = MemFlags::trusted();
97        let mut results = Vec::new();
98        for (i, r) in signature.returns.iter().enumerate() {
99            let load = builder.ins().load(
100                r.value_type,
101                mflags,
102                values_vec_ptr_val,
103                (i * value_size) as i32,
104            );
105            results.push(load);
106        }
107        builder.ins().return_(&results);
108        builder.finalize()
109    }
110
111    if let Some(callbacks) = callbacks.as_ref() {
112        callbacks.preopt_ir(
113            &CompiledKind::DynamicFunctionTrampoline(func_type.clone()),
114            context.func.display().to_string().as_bytes(),
115        );
116    }
117
118    let mut code_buf = Vec::new();
119    let mut ctrl_plane = Default::default();
120    let compiled = context
121        .compile(isa, &mut ctrl_plane)
122        .map_err(|error| CompileError::Codegen(error.inner.to_string()))?;
123    code_buf.extend_from_slice(compiled.code_buffer());
124
125    if let Some(callbacks) = callbacks.as_ref() {
126        callbacks.obj_memory_buffer(
127            &CompiledKind::DynamicFunctionTrampoline(func_type.clone()),
128            &code_buf,
129        );
130        callbacks.asm_memory_buffer(
131            &CompiledKind::DynamicFunctionTrampoline(func_type.clone()),
132            arch,
133            &code_buf,
134        )?;
135    }
136
137    let unwind_info = compiled_function_unwind_info(isa, &context)?.maybe_into_to_windows_unwind();
138
139    Ok(FunctionBody {
140        body: code_buf,
141        unwind_info,
142    })
143}