wasmer_compiler_cranelift/trampoline/
function_call.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 Wasm functions easily.
5//!
6//! That way, you can start calling Wasm functions doing things like:
7//! ```ignore
8//! let my_func = instance.exports.get("func");
9//! my_func.call([1, 2])
10//! ```
11use crate::{
12    CraneliftCallbacks,
13    translator::{compiled_function_unwind_info, signature_to_cranelift_ir},
14};
15use cranelift_codegen::{
16    Context,
17    ir::{self, InstBuilder},
18    isa::TargetIsa,
19};
20use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
21use std::mem;
22use target_lexicon::Architecture;
23use wasmer_compiler::{misc::CompiledKind, types::function::FunctionBody};
24use wasmer_types::{CompileError, FunctionType};
25
26/// Create a trampoline for invoking a WebAssembly function.
27pub fn make_trampoline_function_call(
28    callbacks: &Option<CraneliftCallbacks>,
29    isa: &dyn TargetIsa,
30    arch: Architecture,
31    fn_builder_ctx: &mut FunctionBuilderContext,
32    func_type: &FunctionType,
33    module_hash: &Option<String>,
34) -> Result<FunctionBody, CompileError> {
35    let pointer_type = isa.pointer_type();
36    let frontend_config = isa.frontend_config();
37    let signature = signature_to_cranelift_ir(func_type, frontend_config);
38    let mut wrapper_sig = ir::Signature::new(frontend_config.default_call_conv);
39
40    // Add the callee `vmctx` parameter.
41    wrapper_sig.params.push(ir::AbiParam::special(
42        pointer_type,
43        ir::ArgumentPurpose::VMContext,
44    ));
45
46    // Add the `callee_address` parameter.
47    wrapper_sig.params.push(ir::AbiParam::new(pointer_type));
48
49    // Add the `values_vec` parameter.
50    wrapper_sig.params.push(ir::AbiParam::new(pointer_type));
51
52    let mut context = Context::new();
53    context.func = ir::Function::with_name_signature(ir::UserFuncName::user(0, 0), wrapper_sig);
54
55    let value_size = mem::size_of::<u128>();
56    {
57        let mut builder = FunctionBuilder::new(&mut context.func, fn_builder_ctx);
58        let block0 = builder.create_block();
59
60        builder.append_block_params_for_function_params(block0);
61        builder.switch_to_block(block0);
62        builder.seal_block(block0);
63
64        let (vmctx_ptr_val, callee_value, values_vec_ptr_val) = {
65            let params = builder.func.dfg.block_params(block0);
66            (params[0], params[1], params[2])
67        };
68
69        // Load the argument values out of `values_vec`.
70        let mflags = ir::MemFlags::trusted();
71        let callee_args = signature
72            .params
73            .iter()
74            .enumerate()
75            .map(|(i, r)| {
76                match i {
77                    0 => vmctx_ptr_val,
78                    _ =>
79                    // i - 1 because vmctx is not passed through `values_vec`.
80                    {
81                        builder.ins().load(
82                            r.value_type,
83                            mflags,
84                            values_vec_ptr_val,
85                            ((i - 1) * value_size) as i32,
86                        )
87                    }
88                }
89            })
90            .collect::<Vec<_>>();
91
92        let new_sig = builder.import_signature(signature);
93
94        let call = builder
95            .ins()
96            .call_indirect(new_sig, callee_value, &callee_args);
97
98        let results = builder.func.dfg.inst_results(call).to_vec();
99
100        // Store the return values into `values_vec`.
101        let mflags = ir::MemFlags::trusted();
102        for (i, r) in results.iter().enumerate() {
103            builder
104                .ins()
105                .store(mflags, *r, values_vec_ptr_val, (i * value_size) as i32);
106        }
107
108        builder.ins().return_(&[]);
109        builder.finalize()
110    }
111
112    if let Some(callbacks) = callbacks.as_ref() {
113        callbacks.preopt_ir(
114            &CompiledKind::FunctionCallTrampoline(func_type.clone()),
115            module_hash,
116            context.func.display().to_string().as_bytes(),
117        );
118    }
119
120    let mut code_buf = Vec::new();
121    let mut ctrl_plane = Default::default();
122    let compiled = context
123        .compile(isa, &mut ctrl_plane)
124        .map_err(|error| CompileError::Codegen(error.inner.to_string()))?;
125    code_buf.extend_from_slice(compiled.code_buffer());
126
127    if let Some(callbacks) = callbacks.as_ref() {
128        callbacks.obj_memory_buffer(
129            &CompiledKind::FunctionCallTrampoline(func_type.clone()),
130            module_hash,
131            &code_buf,
132        );
133        callbacks.asm_memory_buffer(
134            &CompiledKind::FunctionCallTrampoline(func_type.clone()),
135            module_hash,
136            arch,
137            &code_buf,
138        )?;
139    }
140
141    let unwind_info = compiled_function_unwind_info(isa, &context)?.maybe_into_to_windows_unwind();
142
143    Ok(FunctionBody {
144        body: code_buf,
145        unwind_info,
146    })
147}