Truly independent web browser
LibWasm/JIT: Generate code that works on Windows
Aliaksandr Kalenikdaf2dba5e063
diff --git a/Cargo.lock b/Cargo.lockindex ab8eb6c9159..377fb975879 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1945,6 +1945,7 @@ dependencies = ["fARM64","iced-x86","libc",+ "libjs_runtime_macros","libwasm_engine","libwasm_module","serde_json",diff --git a/Libraries/LibWasm/JIT/Cargo.toml b/Libraries/LibWasm/JIT/Cargo.tomlindex 6d224c8f5a8..8ebb39b5236 100644--- a/Libraries/LibWasm/JIT/Cargo.toml+++ b/Libraries/LibWasm/JIT/Cargo.toml@@ -9,6 +9,7 @@ crate-type = ["rlib"]# After changing dependencies, regenerate the Flatpak sources:# python3 Meta/CMake/flatpak/generate-cargo-sources.py[dependencies]+libjs_runtime_macros = { path = "../../LibJS/Rust/macros" }libwasm_module = { path = "../Module" }# The host's disassembler, for `dump-asm`. Tests use both.diff --git a/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs b/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rsindex 57d95902872..bbaa008da98 100644--- a/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs+++ b/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs@@ -222,6 +222,11 @@ impl<'a> CodeGenerator<'a> {);let overflow = self.trap_label(libwasm_module::abi::TRAP_STACK_EXHAUSTED);self.asm.jcc(Cond::Below, overflow);+ if crate::asm::x86_64::needs_stack_probes(below) {+ // NB: The frame's pages are touched before anything goes in them (see `Assembler::probe_stack()`).+ self.asm.mov_rr(Width::W64, context, SP);+ self.asm.probe_stack(context, SCRATCH, Some(below));+ }self.asm.mov_rr(Width::W64, SP, SCRATCH);// Values arriving in registers that have slots.let data = func.blocks[0];diff --git a/Libraries/LibWasm/JIT/src/runtime.rs b/Libraries/LibWasm/JIT/src/runtime.rsindex ee7e611076f..12876ef38a9 100644--- a/Libraries/LibWasm/JIT/src/runtime.rs+++ b/Libraries/LibWasm/JIT/src/runtime.rs@@ -11,6 +11,7 @@use crate::RuntimeInfo;use crate::ir::Helper;+use libjs_runtime_macros::jit_abi;use libwasm_module::abi::{ExecutionContext, MemoryData, OBJECT_DATA_OFFSET, OBJECT_LENGTH_OFFSET, Rtt, STORAGE_32, STORAGE_64, STORAGE_I8,STORAGE_I16, STORAGE_V128, Segment, TRAP_ARRAY_OUT_OF_BOUNDS, TRAP_MEMORY_OUT_OF_BOUNDS, TRAP_NULL_REFERENCE,@@ -54,9 +55,15 @@ pub fn flush_instruction_cache(start: usize, length: usize) {let _ = (start, length);}+/// A helper, with the calling convention compiled code calls it with.+#[cfg(not(all(target_arch = "x86_64", windows)))]+type HelperFunction = unsafe extern "C" fn(*mut ExecutionContext) -> u64;+#[cfg(all(target_arch = "x86_64", windows))]+type HelperFunction = unsafe extern "sysv64" fn(*mut ExecutionContext) -> u64;+/// The address of the function `helper` calls.pub fn helper_address(helper: Helper, runtime: &RuntimeInfo) -> u64 {- let function: unsafe extern "C" fn(*mut ExecutionContext) -> u64 = match helper {+ let function: HelperFunction = match helper {Helper::MemoryGrow => return runtime.memory_grow,Helper::TableGrow => return runtime.table_grow,Helper::AllocateException => return runtime.allocate_exception,@@ -88,6 +95,7 @@ unsafe fn arguments<'a>(context: *mut ExecutionContext) -> &'a [u64; 6] {unsafe { &(*context).arguments }}+#[jit_abi]unsafe extern "C" fn memory_fill(context: *mut ExecutionContext) -> u64 {unsafe {let &[memory, destination, value, count, ..] = arguments(context);@@ -103,6 +111,7 @@ unsafe extern "C" fn memory_fill(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn memory_copy(context: *mut ExecutionContext) -> u64 {unsafe {let &[destination_memory, source_memory, destination, source, count, _] = arguments(context);@@ -123,6 +132,7 @@ unsafe extern "C" fn memory_copy(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn memory_init(context: *mut ExecutionContext) -> u64 {unsafe {let &[memory, segment, destination, source, count, _] = arguments(context);@@ -143,6 +153,7 @@ unsafe extern "C" fn memory_init(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn table_fill(context: *mut ExecutionContext) -> u64 {unsafe {let &[table, destination, value, count, ..] = arguments(context);@@ -157,6 +168,7 @@ unsafe extern "C" fn table_fill(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn table_copy(context: *mut ExecutionContext) -> u64 {unsafe {let &[destination_table, source_table, destination, source, count, _] = arguments(context);@@ -177,6 +189,7 @@ unsafe extern "C" fn table_copy(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn table_init(context: *mut ExecutionContext) -> u64 {unsafe {let &[table, segment, destination, source, count, _] = arguments(context);@@ -226,6 +239,7 @@ fn data_size(kind: u8) -> usize {}}+#[jit_abi]unsafe extern "C" fn array_fill(context: *mut ExecutionContext) -> u64 {unsafe {let &[array, start, low, high, count, _] = arguments(context);@@ -253,6 +267,7 @@ unsafe extern "C" fn array_fill(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn array_copy(context: *mut ExecutionContext) -> u64 {unsafe {let &[destination, destination_start, source, source_start, count, _] = arguments(context);@@ -277,6 +292,7 @@ unsafe extern "C" fn array_copy(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn array_init_data(context: *mut ExecutionContext) -> u64 {unsafe {let &[array, segment, destination, source, count, _] = arguments(context);@@ -315,6 +331,7 @@ unsafe extern "C" fn array_init_data(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn array_init_elem(context: *mut ExecutionContext) -> u64 {unsafe {let &[array, segment, destination, source, count, _] = arguments(context);@@ -341,6 +358,7 @@ unsafe extern "C" fn array_init_elem(context: *mut ExecutionContext) -> u64 {}}+#[jit_abi]unsafe extern "C" fn segment_range(context: *mut ExecutionContext) -> u64 {unsafe {let &[segment, start, count, unit, trap, _] = arguments(context);diff --git a/Libraries/LibWasm/JIT/src/stubs.rs b/Libraries/LibWasm/JIT/src/stubs.rsindex 8accce70322..946d8bbe077 100644--- a/Libraries/LibWasm/JIT/src/stubs.rs+++ b/Libraries/LibWasm/JIT/src/stubs.rs@@ -8,8 +8,8 @@//!//! - The trap exit, which compiled code jumps to with a TRAP_* code in edi. It leaves the code through the//! interpreter's TrapFromNative handler, which returns from the interpreter's innermost entry with the trap.-//! - The entry trampoline, through which the runtime calls compiled code: an `extern "C"` function of a-//! `NativeCall`, whose registers and home slot image it loads.+//! - The entry trampoline, through which the runtime calls compiled code: a function of a `NativeCall`, with the System+//! V calling convention on every x86-64 target, whose registers and home slot image it loads.//! - The runtime adapter, the native entry of functions that run in the runtime (interpreted and host functions)://! it saves the argument registers and calls a function of the runtime with them, the callee's header and the//! frame, which puts the results where they go or returns a trap code.@@ -274,7 +274,7 @@ fn copy_words(asm: &mut Assembler, from: Gpr, to: Gpr, count: Gpr, temp: Gpr) {asm.bind(done);}-/// The entry trampoline, `extern "C" fn(*mut NativeCall)`, and the offset of the address its call of compiled code+/// The entry trampoline, a System V `fn(*mut NativeCall)`, and the offset of the address its call of compiled code/// returns to, where an exception the compiled code does not handle continues (see `RuntimeInfo::throw`).pub fn entry_trampoline() -> (Vec<u8>, usize) {let mut asm = Assembler::for_host();@@ -287,17 +287,22 @@ pub fn entry_trampoline() -> (Vec<u8>, usize) {gpr(convention::RDI),);let call_field = |offset: usize| Address::new(record, field(offset));- asm.push(FP);- asm.mov_rr(Width::W64, FP, SP);- for register in [+ let saved_registers = [convention::RBX,convention::R12,convention::R13,convention::R14,convention::R15,- ] {+ ];+ // NB: The frame is set up like that of compiled code, so that a stack walk that starts in compiled code before its+ // frame is set up, and takes this frame for its frame, stays on track.+ asm.push(FP);+ asm.lea(Width::W64, FP, &Address::new(SP, 16));+ for register in saved_registers {asm.push(gpr(register));}+ let lowest_saved_register = convention::FRAME_SAVED_FP - 8 * saved_registers.len() as i32;+ let record_slot = lowest_saved_register - 8;// NB: Six pushes after the return address leave rsp 16-byte aligned. The record is the sixth.asm.push(rdi);asm.mov_rr(Width::W64, record, rdi);@@ -306,7 +311,17 @@ pub fn entry_trampoline() -> (Vec<u8>, usize) {asm.shl_ri(Width::W64, rax, 3);asm.add_ri(Width::W64, rax, 15);asm.and_ri(Width::W64, rax, -16);- asm.sub_rr(Width::W64, SP, rax);+ if crate::asm::x86_64::PROBES_STACK {+ // NB: The image's pages are touched before anything goes in them (see `Assembler::probe_stack()`).+ let (top, bottom) = (rdx, rsi);+ asm.mov_rr(Width::W64, top, SP);+ asm.mov_rr(Width::W64, bottom, SP);+ asm.sub_rr(Width::W64, bottom, rax);+ asm.probe_stack(top, bottom, None);+ asm.mov_rr(Width::W64, SP, bottom);+ } else {+ asm.sub_rr(Width::W64, SP, rax);+ }asm.mov_mr(Width::W64, &call_field(offset_of!(NativeCall, saved_sp)), SP);asm.mov_rm(Width::W64, rsi, &call_field(offset_of!(NativeCall, image)));asm.mov_rr(Width::W64, rdi, SP);@@ -342,7 +357,7 @@ pub fn entry_trampoline() -> (Vec<u8>, usize) {asm.call_m(&call_field(offset_of!(NativeCall, entry)));let return_offset = asm.offset();// NB: Tail calls may have moved rsp; the record has it.- asm.mov_rm(Width::W64, record, &Address::new(FP, -48));+ asm.mov_rm(Width::W64, record, &Address::new(FP, record_slot));asm.mov_rm(Width::W64, SP, &call_field(offset_of!(NativeCall, saved_sp)));asm.mov_mr(Width::W64, &call_field(offset_of!(NativeCall, result_gprs)), rax);asm.mov_mr(Width::W64, &call_field(offset_of!(NativeCall, result_gprs) + 8), rdx);@@ -354,14 +369,8 @@ pub fn entry_trampoline() -> (Vec<u8>, usize) {asm.mov_rm(Width::W64, rdi, &call_field(offset_of!(NativeCall, image)));asm.mov_rr(Width::W64, rsi, SP);copy_words(&mut asm, rsi, rdi, rcx, rdx);- asm.lea(Width::W64, SP, &Address::new(FP, -40));- for register in [- convention::R15,- convention::R14,- convention::R13,- convention::R12,- convention::RBX,- ] {+ asm.lea(Width::W64, SP, &Address::new(FP, lowest_saved_register));+ for register in saved_registers.into_iter().rev() {asm.pop(gpr(register));}asm.pop(FP);@@ -373,9 +382,9 @@ pub fn entry_trampoline() -> (Vec<u8>, usize) {/// adapter continues where the context's `resume` says.pub const ADAPTER_RESUME: u64 = u64::MAX;-/// The runtime adapter. `function` is the runtime's `extern "C" fn(*mut ExecutionContext) -> u64`, which it calls-/// with the callee's header, the save area and fp as the context's first three arguments. A nonzero result is a-/// trap code, or `ADAPTER_RESUME`.+/// The runtime adapter. `function` is the runtime's System V `fn(*mut ExecutionContext) -> u64`, which it calls with+/// the callee's header, the save area and fp as the context's first three arguments. A nonzero result is a trap code,+/// or `ADAPTER_RESUME`.pub fn runtime_adapter(function: u64, runtime: &RuntimeInfo) -> Vec<u8> {let mut asm = Assembler::for_host();let instance = gpr(convention::INSTANCE_REGISTER);