Skip to main content

wasmtime_internal_cranelift/
lib.rs

1//! Support for compiling with Cranelift.
2//!
3//! This crate provides an implementation of the `wasmtime_environ::Compiler`
4//! and `wasmtime_environ::CompilerBuilder` traits.
5//!
6//! > **⚠️ Warning ⚠️**: this crate is an internal-only crate for the Wasmtime
7//! > project and is not intended for general use. APIs are not strictly
8//! > reviewed for safety and usage outside of Wasmtime may have bugs. If
9//! > you're interested in using this feel free to file an issue on the
10//! > Wasmtime repository to start a discussion about doing so, but otherwise
11//! > be aware that your usage of this crate is not supported.
12
13// See documentation in crates/wasmtime/src/runtime.rs for why this is
14// selectively enabled here.
15#![warn(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
16
17use cranelift_codegen::{
18    FinalizedMachReloc, FinalizedRelocTarget, MachTrap, binemit,
19    cursor::FuncCursor,
20    ir::{self, AbiParam, ArgumentPurpose, ExternalName, InstBuilder, Signature, TrapCode},
21    isa::{CallConv, TargetIsa},
22    settings,
23};
24use cranelift_entity::PrimaryMap;
25
26use target_lexicon::Architecture;
27use wasmtime_environ::{
28    BuiltinFunctionIndex, CompiledTrap, FlagValue, FuncKey, Trap, TrapInformation, Tunables,
29    WasmFuncType, WasmHeapTopType, WasmHeapType, WasmValType,
30};
31
32pub use builder::builder;
33
34pub mod isa_builder;
35mod obj;
36pub use obj::*;
37mod compiled_function;
38pub use compiled_function::*;
39
40mod alias_region_key;
41mod bounds_checks;
42mod builder;
43mod compiler;
44mod debug;
45mod func_environ;
46mod translate;
47mod trap;
48
49use self::compiler::Compiler;
50
51const TRAP_INTERNAL_ASSERT: TrapCode = TrapCode::unwrap_user(1);
52const TRAP_GC_HEAP_CORRUPT: TrapCode = TrapCode::unwrap_user(2);
53const TRAP_OFFSET: u8 = 3;
54pub const TRAP_CANNOT_LEAVE_COMPONENT: TrapCode =
55    TrapCode::unwrap_user(Trap::CannotLeaveComponent as u8 + TRAP_OFFSET);
56pub const TRAP_INDIRECT_CALL_TO_NULL: TrapCode =
57    TrapCode::unwrap_user(Trap::IndirectCallToNull as u8 + TRAP_OFFSET);
58pub const TRAP_BAD_SIGNATURE: TrapCode =
59    TrapCode::unwrap_user(Trap::BadSignature as u8 + TRAP_OFFSET);
60pub const TRAP_NULL_REFERENCE: TrapCode =
61    TrapCode::unwrap_user(Trap::NullReference as u8 + TRAP_OFFSET);
62pub const TRAP_ALLOCATION_TOO_LARGE: TrapCode =
63    TrapCode::unwrap_user(Trap::AllocationTooLarge as u8 + TRAP_OFFSET);
64pub const TRAP_ARRAY_OUT_OF_BOUNDS: TrapCode =
65    TrapCode::unwrap_user(Trap::ArrayOutOfBounds as u8 + TRAP_OFFSET);
66pub const TRAP_UNREACHABLE: TrapCode =
67    TrapCode::unwrap_user(Trap::UnreachableCodeReached as u8 + TRAP_OFFSET);
68pub const TRAP_HEAP_MISALIGNED: TrapCode =
69    TrapCode::unwrap_user(Trap::HeapMisaligned as u8 + TRAP_OFFSET);
70pub const TRAP_TABLE_OUT_OF_BOUNDS: TrapCode =
71    TrapCode::unwrap_user(Trap::TableOutOfBounds as u8 + TRAP_OFFSET);
72pub const TRAP_UNHANDLED_TAG: TrapCode =
73    TrapCode::unwrap_user(Trap::UnhandledTag as u8 + TRAP_OFFSET);
74pub const TRAP_CONTINUATION_ALREADY_CONSUMED: TrapCode =
75    TrapCode::unwrap_user(Trap::ContinuationAlreadyConsumed as u8 + TRAP_OFFSET);
76pub const TRAP_CAST_FAILURE: TrapCode =
77    TrapCode::unwrap_user(Trap::CastFailure as u8 + TRAP_OFFSET);
78
79/// Creates a new cranelift `Signature` with no wasm params/results for the
80/// given calling convention.
81///
82/// This will add the default vmctx/etc parameters to the signature returned.
83fn blank_sig(isa: &dyn TargetIsa, call_conv: CallConv) -> ir::Signature {
84    let pointer_type = isa.pointer_type();
85    let mut sig = ir::Signature::new(call_conv);
86    // Add the caller/callee `vmctx` parameters.
87    sig.params.push(ir::AbiParam::special(
88        pointer_type,
89        ir::ArgumentPurpose::VMContext,
90    ));
91    sig.params.push(ir::AbiParam::new(pointer_type));
92    return sig;
93}
94
95/// Emit code for the following unbarriered memory write of the given type:
96///
97/// ```ignore
98/// *(base + offset) = value
99/// ```
100///
101/// This is intended to be used with things like `ValRaw` and the array calling
102/// convention.
103fn unbarriered_store_type_at_offset(
104    pos: &mut FuncCursor,
105    flags: ir::MemFlagsData,
106    base: ir::Value,
107    offset: i32,
108    value: ir::Value,
109) {
110    pos.ins().store(flags, value, base, offset);
111}
112
113/// Emit code to do the following unbarriered memory read of the given type and
114/// with the given flags:
115///
116/// ```ignore
117/// result = *(base + offset)
118/// ```
119///
120/// This is intended to be used with things like `ValRaw` and the array calling
121/// convention.
122fn unbarriered_load_type_at_offset(
123    isa: &dyn TargetIsa,
124    pos: &mut FuncCursor,
125    ty: WasmValType,
126    flags: ir::MemFlagsData,
127    base: ir::Value,
128    offset: i32,
129) -> ir::Value {
130    let ir_ty = value_type(isa, ty);
131    pos.ins().load(ir_ty, flags, base, offset)
132}
133
134/// Returns the corresponding cranelift type for the provided wasm type.
135fn value_type(isa: &dyn TargetIsa, ty: WasmValType) -> ir::types::Type {
136    match ty {
137        WasmValType::I32 => ir::types::I32,
138        WasmValType::I64 => ir::types::I64,
139        WasmValType::F32 => ir::types::F32,
140        WasmValType::F64 => ir::types::F64,
141        WasmValType::V128 => ir::types::I8X16,
142        WasmValType::Ref(rt) => reference_type(rt.heap_type, isa.pointer_type()),
143    }
144}
145
146/// Get the Cranelift signature for all array-call functions, that is:
147///
148/// ```ignore
149/// unsafe extern "C" fn(
150///     callee_vmctx: *mut VMOpaqueContext,
151///     caller_vmctx: *mut VMOpaqueContext,
152///     values_ptr: *mut ValRaw,
153///     values_len: usize,
154/// )
155/// ```
156///
157/// This signature uses the target's default calling convention.
158///
159/// Note that regardless of the Wasm function type, the array-call calling
160/// convention always uses that same signature.
161fn array_call_signature(isa: &dyn TargetIsa) -> ir::Signature {
162    let mut sig = blank_sig(isa, CallConv::triple_default(isa.triple()));
163    // The array-call signature has an added parameter for the `values_vec`
164    // input/output buffer in addition to the size of the buffer, in units
165    // of `ValRaw`.
166    sig.params.push(ir::AbiParam::new(isa.pointer_type()));
167    sig.params.push(ir::AbiParam::new(isa.pointer_type()));
168    // boolean return value of whether this function trapped
169    sig.returns.push(ir::AbiParam::new(ir::types::I8));
170    sig
171}
172
173/// Get the internal Wasm calling convention for the target/tunables combo
174fn wasm_call_conv(isa: &dyn TargetIsa, tunables: &Tunables) -> CallConv {
175    // The default calling convention is `CallConv::Tail` to enable the use of
176    // tail calls in modules when needed. Note that this is used even if the
177    // tail call proposal is disabled in wasm. This is not interacted with on
178    // the host so it's purely an internal detail of wasm itself.
179    //
180    // The Winch calling convention is used instead when generating trampolines
181    // which call Winch-generated functions. The winch calling convention is
182    // only implemented for x64 and aarch64, so assert that here and panic on
183    // other architectures.
184    if tunables.winch_callable {
185        assert!(
186            matches!(
187                isa.triple().architecture,
188                Architecture::X86_64 | Architecture::Aarch64(_)
189            ),
190            "The Winch calling convention is only implemented for x86_64 and aarch64"
191        );
192        CallConv::Winch
193    } else {
194        CallConv::Tail
195    }
196}
197
198/// Get the internal Wasm calling convention signature for the given type.
199fn wasm_call_signature(
200    isa: &dyn TargetIsa,
201    wasm_func_ty: &WasmFuncType,
202    tunables: &Tunables,
203) -> ir::Signature {
204    let call_conv = wasm_call_conv(isa, tunables);
205    let mut sig = blank_sig(isa, call_conv);
206    let cvt = |ty: &WasmValType| ir::AbiParam::new(value_type(isa, *ty));
207    sig.params.extend(wasm_func_ty.params().iter().map(&cvt));
208    sig.returns.extend(wasm_func_ty.results().iter().map(&cvt));
209    sig
210}
211
212/// Returns the reference type to use for the provided wasm type.
213fn reference_type(wasm_ht: WasmHeapType, pointer_type: ir::Type) -> ir::Type {
214    match wasm_ht.top() {
215        WasmHeapTopType::Func => pointer_type,
216        WasmHeapTopType::Any | WasmHeapTopType::Extern | WasmHeapTopType::Exn => ir::types::I32,
217        WasmHeapTopType::Cont => {
218            // VMContObj is 2 * pointer_size (pointer + usize revision)
219            ir::Type::int((2 * pointer_type.bits()).try_into().unwrap()).unwrap()
220        }
221    }
222}
223
224// List of namespaces which are processed in `mach_reloc_to_reloc` below.
225
226/// A record of a relocation to perform.
227#[derive(Debug, Clone, PartialEq, Eq)]
228pub struct Relocation {
229    /// The relocation code.
230    pub reloc: binemit::Reloc,
231    /// Relocation target.
232    pub reloc_target: FuncKey,
233    /// The offset where to apply the relocation.
234    pub offset: binemit::CodeOffset,
235    /// The addend to add to the relocation value.
236    pub addend: binemit::Addend,
237}
238
239/// Converts cranelift_codegen settings to the wasmtime_environ equivalent.
240pub fn clif_flags_to_wasmtime(
241    flags: impl IntoIterator<Item = settings::Value>,
242) -> Vec<(&'static str, FlagValue<'static>)> {
243    flags
244        .into_iter()
245        .map(|val| (val.name, to_flag_value(&val)))
246        .collect()
247}
248
249fn to_flag_value(v: &settings::Value) -> FlagValue<'static> {
250    match v.kind() {
251        settings::SettingKind::Enum => FlagValue::Enum(v.as_enum().unwrap()),
252        settings::SettingKind::Num => FlagValue::Num(v.as_num().unwrap()),
253        settings::SettingKind::Bool => FlagValue::Bool(v.as_bool().unwrap()),
254        settings::SettingKind::Preset => unreachable!(),
255    }
256}
257
258/// Converts machine traps to trap information.
259pub fn mach_trap_to_trap(trap: &MachTrap, tunables: &Tunables) -> Option<TrapInformation> {
260    let &MachTrap { offset, code } = trap;
261    Some(TrapInformation {
262        code_offset: offset,
263        trap_code: clif_trap_to_env_trap(code, tunables)?,
264    })
265}
266
267fn clif_trap_to_env_trap(trap: ir::TrapCode, tunables: &Tunables) -> Option<CompiledTrap> {
268    Some(CompiledTrap::Normal(match trap {
269        ir::TrapCode::STACK_OVERFLOW => Trap::StackOverflow,
270        ir::TrapCode::HEAP_OUT_OF_BOUNDS => Trap::MemoryOutOfBounds,
271        ir::TrapCode::INTEGER_OVERFLOW => Trap::IntegerOverflow,
272        ir::TrapCode::INTEGER_DIVISION_BY_ZERO => Trap::IntegerDivisionByZero,
273        ir::TrapCode::BAD_CONVERSION_TO_INTEGER => Trap::BadConversionToInteger,
274
275        TRAP_INTERNAL_ASSERT => {
276            return if tunables.metadata_for_internal_asserts {
277                Some(CompiledTrap::InternalAssert)
278            } else {
279                None
280            };
281        }
282        TRAP_GC_HEAP_CORRUPT => {
283            return if tunables.metadata_for_gc_heap_corruption {
284                Some(CompiledTrap::GcHeapCorrupt)
285            } else {
286                None
287            };
288        }
289
290        other => Trap::from_u8(other.as_raw().get() - TRAP_OFFSET).unwrap(),
291    }))
292}
293
294/// Converts machine relocations to relocation information
295/// to perform.
296fn mach_reloc_to_reloc(
297    reloc: &FinalizedMachReloc,
298    name_map: &PrimaryMap<ir::UserExternalNameRef, ir::UserExternalName>,
299) -> Relocation {
300    let &FinalizedMachReloc {
301        offset,
302        kind,
303        ref target,
304        addend,
305    } = reloc;
306    let reloc_target = match *target {
307        FinalizedRelocTarget::ExternalName(ExternalName::User(user_func_ref)) => {
308            let name = &name_map[user_func_ref];
309            FuncKey::from_raw_parts(name.namespace, name.index)
310        }
311        FinalizedRelocTarget::ExternalName(ExternalName::LibCall(libcall)) => {
312            // We should have avoided any code that needs this style of libcalls
313            // in the Wasm-to-Cranelift translator.
314            panic!("unexpected libcall {libcall:?}");
315        }
316        _ => panic!("unrecognized external name {target:?}"),
317    };
318    Relocation {
319        reloc: kind,
320        reloc_target,
321        offset,
322        addend,
323    }
324}
325
326/// Helper structure for creating a `Signature` for all builtins.
327struct BuiltinFunctionSignatures {
328    pointer_type: ir::Type,
329
330    host_call_conv: CallConv,
331    wasm_call_conv: CallConv,
332    argument_extension: ir::ArgumentExtension,
333}
334
335impl BuiltinFunctionSignatures {
336    fn new(compiler: &Compiler) -> Self {
337        Self {
338            pointer_type: compiler.isa().pointer_type(),
339            host_call_conv: CallConv::triple_default(compiler.isa().triple()),
340            wasm_call_conv: wasm_call_conv(compiler.isa(), compiler.tunables()),
341            argument_extension: compiler.isa().default_argument_extension(),
342        }
343    }
344
345    fn vmctx(&self) -> AbiParam {
346        AbiParam::special(self.pointer_type, ArgumentPurpose::VMContext)
347    }
348
349    fn pointer(&self) -> AbiParam {
350        AbiParam::new(self.pointer_type)
351    }
352
353    fn u32(&self) -> AbiParam {
354        AbiParam::new(ir::types::I32)
355    }
356
357    fn u64(&self) -> AbiParam {
358        AbiParam::new(ir::types::I64)
359    }
360
361    fn f32(&self) -> AbiParam {
362        AbiParam::new(ir::types::F32)
363    }
364
365    fn f64(&self) -> AbiParam {
366        AbiParam::new(ir::types::F64)
367    }
368
369    fn u8(&self) -> AbiParam {
370        AbiParam::new(ir::types::I8)
371    }
372
373    fn i8x16(&self) -> AbiParam {
374        AbiParam::new(ir::types::I8X16)
375    }
376
377    fn f32x4(&self) -> AbiParam {
378        AbiParam::new(ir::types::F32X4)
379    }
380
381    fn f64x2(&self) -> AbiParam {
382        AbiParam::new(ir::types::F64X2)
383    }
384
385    fn bool(&self) -> AbiParam {
386        AbiParam::new(ir::types::I8)
387    }
388
389    fn size(&self) -> AbiParam {
390        AbiParam::new(self.pointer_type)
391    }
392
393    fn wasm_signature(&self, builtin: BuiltinFunctionIndex) -> Signature {
394        let mut _cur = 0;
395        macro_rules! iter {
396            (
397                $(
398                    $( #[$attr:meta] )*
399                    $name:ident( $( $pname:ident: $param:ident ),* ) $( -> $result:ident )?;
400                )*
401            ) => {
402                $(
403                    $( #[$attr] )*
404                    if _cur == builtin.index() {
405                        return Signature {
406                            params: vec![ $( self.$param() ),* ],
407                            returns: vec![ $( self.$result() )? ],
408                            call_conv: self.wasm_call_conv,
409                        };
410                    }
411                    _cur += 1;
412                )*
413            };
414        }
415
416        wasmtime_environ::foreach_builtin_function!(iter);
417
418        unreachable!();
419    }
420
421    fn host_signature(&self, builtin: BuiltinFunctionIndex) -> Signature {
422        let mut sig = self.wasm_signature(builtin);
423        sig.call_conv = self.host_call_conv;
424
425        // Once we're declaring the signature of a host function we must
426        // respect the default ABI of the platform which is where argument
427        // extension of params/results may come into play.
428        for arg in sig.params.iter_mut().chain(sig.returns.iter_mut()) {
429            if arg.value_type.is_int() {
430                arg.extension = self.argument_extension;
431            }
432        }
433
434        sig
435    }
436}
437
438/// If this bit is set on a GC reference, then the GC reference is actually an
439/// unboxed `i31`.
440///
441/// Must be kept in sync with
442/// `crate::runtime::vm::gc::VMGcRef::I31_REF_DISCRIMINANT`.
443const I31_REF_DISCRIMINANT: u32 = 1;
444
445/// Like `Option<T>` but specifically for passing information about transitions
446/// from reachable to unreachable state and the like from callees to callers.
447///
448/// Marked `must_use` to force callers to update
449/// `FuncTranslationStacks::reachable` as necessary.
450#[derive(PartialEq, Eq)]
451#[must_use]
452enum Reachability<T> {
453    /// The Wasm execution state is reachable, here is a `T`.
454    Reachable(T),
455    /// The Wasm execution state has been determined to be statically
456    /// unreachable. It is the receiver of this value's responsibility to update
457    /// `FuncTranslationStacks::reachable` as necessary.
458    Unreachable,
459}