wasmtime_internal_cranelift/
lib.rs1#![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
79fn 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 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
95fn 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
113fn 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
134fn 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
146fn array_call_signature(isa: &dyn TargetIsa) -> ir::Signature {
162 let mut sig = blank_sig(isa, CallConv::triple_default(isa.triple()));
163 sig.params.push(ir::AbiParam::new(isa.pointer_type()));
167 sig.params.push(ir::AbiParam::new(isa.pointer_type()));
168 sig.returns.push(ir::AbiParam::new(ir::types::I8));
170 sig
171}
172
173fn wasm_call_conv(isa: &dyn TargetIsa, tunables: &Tunables) -> CallConv {
175 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
198fn 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
212fn 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 ir::Type::int((2 * pointer_type.bits()).try_into().unwrap()).unwrap()
220 }
221 }
222}
223
224#[derive(Debug, Clone, PartialEq, Eq)]
228pub struct Relocation {
229 pub reloc: binemit::Reloc,
231 pub reloc_target: FuncKey,
233 pub offset: binemit::CodeOffset,
235 pub addend: binemit::Addend,
237}
238
239pub 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
258pub 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
294fn 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 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
326struct 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 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
438const I31_REF_DISCRIMINANT: u32 = 1;
444
445#[derive(PartialEq, Eq)]
451#[must_use]
452enum Reachability<T> {
453 Reachable(T),
455 Unreachable,
459}