Skip to content

Truly independent web browser

LibJS/JIT: Make the tests of the compiler work on Windows

Aliaksandr Kalenik1de1f38263ce
diff --git a/Cargo.lock b/Cargo.lock
index a93c642deee..ab8eb6c9159 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1819,6 +1819,7 @@ dependencies = [
"flapc",
"iced-x86",
"libc",
+ "libjs_runtime_macros",
]
[[package]]
diff --git a/Libraries/LibJS/JIT/Rust/Cargo.toml b/Libraries/LibJS/JIT/Rust/Cargo.toml
index 4a826b1f057..eceb259edeb 100644
--- a/Libraries/LibJS/JIT/Rust/Cargo.toml
+++ b/Libraries/LibJS/JIT/Rust/Cargo.toml
@@ -23,6 +23,7 @@ ak = { path = "../../../../AK/Rust" }
farm64 = { package = "fARM64", version = "0.0.2", features = ["alloc"] }
iced-x86 = { version = "1.21", default-features = false, features = ["decoder", "intel", "std"] }
libc = "0.2"
+libjs_runtime_macros = { path = "../../Rust/macros" }
[lints]
workspace = true
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/executable_code.rs b/Libraries/LibJS/JIT/Rust/src/asm/executable_code.rs
index 64595b4d710..4e78534d27c 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/executable_code.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/executable_code.rs
@@ -10,7 +10,8 @@
/// Machine code copied into its own executable mapping (never writable and
/// executable at the same time).
pub(crate) struct ExecutableCode {
- address: *mut libc::c_void,
+ address: *mut core::ffi::c_void,
+ #[cfg_attr(windows, allow(dead_code))]
size: usize,
}
@@ -20,7 +21,7 @@ unsafe extern "C" {
fn sys_icache_invalidate(start: *mut libc::c_void, length: libc::size_t);
}
-#[cfg(not(target_os = "macos"))]
+#[cfg(not(any(target_os = "macos", windows)))]
unsafe extern "C" {
/// The compiler runtime's instruction cache flush (a no-op on x86-64).
fn __clear_cache(start: *mut libc::c_char, end: *mut libc::c_char);
@@ -33,6 +34,19 @@ impl ExecutableCode {
pub(crate) fn new(code: &[u8]) -> Self {
let size = code.len().max(1).next_multiple_of(16384);
+ #[cfg(windows)]
+ // SAFETY: A fresh reservation, written before it is made executable.
+ unsafe {
+ let address = ak::windows_code_memory::reserve(size);
+ assert!(!address.is_null());
+ assert!(ak::windows_code_memory::make_writable(address, size));
+ std::ptr::copy_nonoverlapping(code.as_ptr(), address, code.len());
+ assert!(ak::windows_code_memory::make_executable(address, size));
+ Self {
+ address: address.cast(),
+ size,
+ }
+ }
#[cfg(target_os = "macos")]
// SAFETY: A fresh private anonymous mapping for JIT code, written
// while the thread's write protection of it is off.
@@ -52,7 +66,7 @@ impl ExecutableCode {
sys_icache_invalidate(address, code.len());
Self { address, size }
}
- #[cfg(not(target_os = "macos"))]
+ #[cfg(not(any(target_os = "macos", windows)))]
// SAFETY: A fresh private anonymous mapping, written before it is
// made executable.
unsafe {
@@ -75,9 +89,9 @@ impl ExecutableCode {
/// Reinterprets the code as a function pointer of type `F`.
///
/// # Safety
- /// `F` must be an `extern "C" fn` type matching what the code implements.
+ /// `F` must be an `extern "sysv64" fn` type matching what the code implements.
pub(crate) unsafe fn function<F: Copy>(&self) -> F {
- assert_eq!(size_of::<F>(), size_of::<*mut libc::c_void>());
+ assert_eq!(size_of::<F>(), size_of::<*mut core::ffi::c_void>());
// SAFETY: The caller guarantees `F` is a matching function pointer type.
unsafe { std::mem::transmute_copy(&self.address) }
}
@@ -86,8 +100,14 @@ impl ExecutableCode {
impl Drop for ExecutableCode {
fn drop(&mut self) {
// SAFETY: The mapping was created in `new` with this size.
+ #[cfg(not(windows))]
unsafe {
libc::munmap(self.address, self.size);
}
+ // SAFETY: The reservation was created in `new`.
+ #[cfg(windows)]
+ unsafe {
+ ak::windows_code_memory::release(self.address.cast());
+ }
}
}
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs b/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs
index 9e307441ad8..71abddb6405 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs
@@ -35,8 +35,8 @@ struct Pair {
status: u64,
}
-type Binary = extern "C" fn(u64, u64) -> u64;
-type BinaryPair = extern "C" fn(u64, u64) -> Pair;
+type Binary = extern "sysv64" fn(u64, u64) -> u64;
+type BinaryPair = extern "sysv64" fn(u64, u64) -> Pair;
const VALUES: [u64; 16] = [
0,
@@ -558,7 +558,7 @@ fn compares_and_branches() {
#[test]
fn memory_compares() {
- type Memory = extern "C" fn(*const u64, u64) -> u64;
+ type Memory = extern "sysv64" fn(*const u64, u64) -> u64;
for condition in [
Condition::Equal,
Condition::NotEqual,
@@ -631,7 +631,7 @@ fn memory_compares() {
#[test]
fn additions_to_memory() {
- type Add = extern "C" fn(*mut u64);
+ type Add = extern "sysv64" fn(*mut u64);
for imm in IMMEDIATES.map(|imm| imm as i32) {
let code = assemble(|masm| {
masm.add32_to_memory_imm(&Address::new(RDI, 8), imm);
@@ -657,7 +657,7 @@ fn additions_to_memory() {
#[test]
fn loads_and_stores_of_128_bit_constants() {
- type Store = extern "C" fn(*mut u64);
+ type Store = extern "sysv64" fn(*mut u64);
let first = 0x0123_4567_89ab_cdef_fedc_ba98_7654_3210u128;
let second = u128::MAX - 7;
let code = assemble(|masm| {
@@ -684,7 +684,7 @@ fn loads_and_stores_of_128_bit_constants() {
#[test]
fn stack_pointer_comparisons() {
- type Compare = extern "C" fn(*const u64) -> u64;
+ type Compare = extern "sysv64" fn(*const u64) -> u64;
let code = assemble(|masm| {
let below = masm.new_label();
masm.branch_if_stack_pointer_below(&Address::new(RDI, 8), below);
@@ -765,7 +765,7 @@ fn bit_tests_and_select() {
}
}
- type Select = extern "C" fn(u64, u64, u64, u64) -> u64;
+ type Select = extern "sysv64" fn(u64, u64, u64, u64) -> u64;
let registers = [RDI, RSI, RDX, RCX];
for condition in [Condition::LessThan, Condition::Equal, Condition::Above] {
for dst in [RAX, RDI, RSI, RDX, RCX] {
@@ -822,7 +822,7 @@ fn bit_tests_and_select() {
#[test]
fn loads_and_stores() {
- type Access = extern "C" fn(*mut u8, u64) -> u64;
+ type Access = extern "sysv64" fn(*mut u8, u64) -> u64;
let mut memory = [0u8; 64];
for (i, byte) in memory.iter_mut().enumerate() {
*byte = (i as u8).wrapping_mul(37).wrapping_add(0x81);
@@ -983,7 +983,7 @@ fn same_double(a: f64, b: f64) -> bool {
#[test]
fn double_arithmetic() {
- type DoubleBinary = extern "C" fn(f64, f64) -> f64;
+ type DoubleBinary = extern "sysv64" fn(f64, f64) -> f64;
type Emit = fn(&mut MacroAssembler, Fpr, Fpr, Fpr);
type Reference = fn(f64, f64) -> f64;
let operations: [(&str, Emit, Reference); 12] = [
@@ -1049,9 +1049,9 @@ fn double_arithmetic() {
#[test]
fn double_conversions() {
- type ToDouble = extern "C" fn(u64) -> f64;
- type FromDouble = extern "C" fn(f64) -> u64;
- type FromDoublePair = extern "C" fn(f64) -> Pair;
+ type ToDouble = extern "sysv64" fn(u64) -> f64;
+ type FromDouble = extern "sysv64" fn(f64) -> u64;
+ type FromDoublePair = extern "sysv64" fn(f64) -> Pair;
let int32 = assemble(|masm| {
masm.convert_int32_to_double(XMM0, RDI);
@@ -1141,7 +1141,7 @@ const ALL_DOUBLE_CONDITIONS: [DoubleCondition; 14] = [
#[test]
fn double_compares() {
- type Compare = extern "C" fn(f64, f64) -> u64;
+ type Compare = extern "sysv64" fn(f64, f64) -> u64;
for condition in ALL_DOUBLE_CONDITIONS {
assert_eq!(condition.invert().invert(), condition);
let branch = assemble(|masm| {
@@ -1171,7 +1171,7 @@ fn double_compares() {
}
}
-extern "C" fn helper(a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) -> u64 {
+extern "sysv64" fn helper(a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) -> u64 {
#[repr(align(16))]
struct Aligned(u8);
let local = Aligned(0);
@@ -1184,7 +1184,7 @@ extern "C" fn helper(a: u64, b: u64, c: u64, d: u64, e: u64, f: u64) -> u64 {
#[test]
fn calls_with_frames() {
- type Call = extern "C" fn(u64) -> Pair;
+ type Call = extern "sysv64" fn(u64) -> Pair;
let frame = MacroAssembler::frame(GprSet::of(&[RBX, R12]), FprSet::EMPTY, 24);
assert_eq!(frame.locals_size % 8, 0);
let code = assemble(|masm| {
@@ -1214,7 +1214,7 @@ fn calls_with_frames() {
);
// A tail jump to the helper returns straight to our caller.
- type Six = extern "C" fn(u64, u64, u64, u64, u64, u64) -> u64;
+ type Six = extern "sysv64" fn(u64, u64, u64, u64, u64, u64) -> u64;
let code = assemble(|masm| {
masm.add64_imm(RDI, RDI, 100);
masm.jump_absolute(helper as *const () as u64);
@@ -1246,7 +1246,7 @@ fn calls_with_frames() {
#[test]
fn prologue_and_epilogue_preserve_callee_saved_registers() {
- type Run = extern "C" fn() -> u64;
+ type Run = extern "sysv64" fn() -> u64;
let callee_saved = <MacroAssembler as PortableMacroAssembler>::CALLEE_SAVED_GPRS;
let checked: Vec<Gpr> = callee_saved.without(RSP).iter().collect();
assert_eq!(checked, [RBX, RBP, R12, R13, R14, R15]);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
index 9f137a3f2ca..bf31f7c1b07 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
@@ -23,6 +23,7 @@ use crate::snapshot::FunctionFrameFields;
use crate::snapshot::RuntimeLayout;
use crate::snapshot::RuntimeOffsets;
use crate::snapshot::Snapshot;
+use libjs_runtime_macros::jit_abi;
use std::cell::RefCell;
/// Words of the fake `ExecutionContext` before its slots.
@@ -238,6 +239,7 @@ struct SlowPathResult {
value: u64,
}
+#[jit_abi]
extern "C" fn add_values(vm: *mut u64, pc: u32, dst: *mut u64, lhs: u64, rhs: u64) -> i64 {
let control = continuation_after(vm, pc);
if control >= 0 && control as u64 & CONTINUATION_BIT != 0 {
@@ -247,6 +249,7 @@ extern "C" fn add_values(vm: *mut u64, pc: u32, dst: *mut u64, lhs: u64, rhs: u6
control
}
+#[jit_abi]
extern "C" fn jump_less_than(vm: *mut u64, pc: u32, lhs: u64, rhs: u64, if_true: u32, if_false: u32) -> i64 {
if let Some((fail_pc, control)) = test_state(vm).borrow().fail
&& fail_pc == pc
@@ -256,23 +259,66 @@ extern "C" fn jump_less_than(vm: *mut u64, pc: u32, lhs: u64, rhs: u64, if_true:
i64::from(if int(lhs) < int(rhs) { if_true } else { if_false })
}
+/// Whether the slow path of the instruction at `instruction` takes every operand in a record, which has a word per
+/// field, as slow paths on Windows do, like its interpreter.
+///
+/// # Safety
+///
+/// `instruction` must point at an instruction.
+unsafe fn slow_path_takes_record(instruction: *const u8) -> bool {
+ // SAFETY: The caller keeps the contract.
+ let opcode = OpCode::from_u8(unsafe { *instruction }).expect("compiled code passes an instruction");
+ slow_path_layout(opcode).abi == SlowPathAbi::Record
+}
+
+/// An input of a slow path with scalar inputs and outputs in the record: the scalar `input` it got, or else, if the
+/// slow path of the instruction at `instruction` takes every operand in the record, the word of the record with that
+/// field.
+///
+/// # Safety
+///
+/// `instruction` must point at an instruction whose slow path's record has such a field at `index`, and `record` at
+/// the record.
+unsafe fn scalar_or_record_input(instruction: *const u8, record: *const u64, index: usize, input: u64) -> u64 {
+ // SAFETY: The caller keeps the contract.
+ unsafe {
+ if slow_path_takes_record(instruction) {
+ *record.add(index)
+ } else {
+ input
+ }
+ }
+}
+
/// A fake `ToInt32` or `ToLength` slow path (scalar convention) that
-/// doubles its input.
+/// doubles its input. With records, `value` is the record, which has the
+/// destination and then the input.
+#[jit_abi]
extern "C" fn to_int32(vm: *mut u64, pc: u32, instruction: *const u8, value: u64) -> SlowPathResult {
// SAFETY: Compiled code passes a pointer to the instruction in the snapshot's bytecode.
let opcode = unsafe { *instruction };
assert!(opcode == OpCode::ToInt32 as u8 || opcode == OpCode::ToLength as u8);
+ let control = continuation_after(vm, pc);
+ // SAFETY: As above.
+ if unsafe { slow_path_takes_record(instruction) } {
+ let record = value as *mut u64;
+ // SAFETY: Compiled code passes the record in the JIT frame.
+ unsafe { *record = value::int32(int(*record.add(1)).wrapping_mul(2)) };
+ return SlowPathResult { control, value: 0 };
+ }
SlowPathResult {
- control: continuation_after(vm, pc),
+ control,
value: value::int32(int(value).wrapping_mul(2)),
}
}
/// A fake `Increment` slow path (scalar inputs, outputs in the record).
+#[jit_abi]
extern "C" fn increment(vm: *mut u64, pc: u32, instruction: *const u8, outputs: *mut u64, dst: u64) -> i64 {
// SAFETY: See `to_int32`; `outputs` is the record in the JIT frame.
unsafe {
assert_eq!(*instruction, OpCode::Increment as u8);
+ let dst = scalar_or_record_input(instruction, outputs, 0, dst);
*outputs = value::int32(int(dst).wrapping_add(1));
}
continuation_after(vm, pc)
@@ -280,6 +326,7 @@ extern "C" fn increment(vm: *mut u64, pc: u32, instruction: *const u8, outputs:
/// A fake `ConcatString` slow path (scalar inputs, outputs in the record)
/// that adds its operands.
+#[jit_abi]
extern "C" fn concat_string(
vm: *mut u64,
pc: u32,
@@ -291,12 +338,15 @@ extern "C" fn concat_string(
// SAFETY: See `to_int32`; `outputs` is the record in the JIT frame.
unsafe {
assert_eq!(*instruction, OpCode::ConcatString as u8);
+ let dst = scalar_or_record_input(instruction, outputs, 0, dst);
+ let src = scalar_or_record_input(instruction, outputs, 1, src);
*outputs = value::int32(int(dst).wrapping_add(int(src)));
}
continuation_after(vm, pc)
}
/// A fake `NewArray` slow path (record convention) that sums its elements.
+#[jit_abi]
extern "C" fn new_array(vm: *mut u64, pc: u32, instruction: *const u8, values: *mut u64) -> i64 {
let Instruction::NewArray { elements, .. } = decoded(vm, pc) else {
panic!("not a NewArray");
@@ -311,6 +361,7 @@ extern "C" fn new_array(vm: *mut u64, pc: u32, instruction: *const u8, values: *
}
/// A fake `libjs_jit_call` that "calls" an int32 callee by adding the arguments to it.
+#[jit_abi]
extern "C" fn jit_call(vm: *mut u64, frame: *mut u64, pc: u32) -> i64 {
let Instruction::Call {
dst, callee, arguments, ..
@@ -343,6 +394,7 @@ fn fake_arguments_object(mapped: bool) -> u64 {
}
/// A fake `libjs_jit_create_arguments`.
+#[jit_abi]
extern "C" fn create_arguments(vm: *mut u64, _frame: *mut u64, mapped: u32) -> u64 {
test_state(vm).borrow_mut().arguments_created += 1;
fake_arguments_object(mapped != 0)
@@ -350,6 +402,7 @@ extern "C" fn create_arguments(vm: *mut u64, _frame: *mut u64, mapped: u32) -> u
/// A fake `libjs_jit_call_forwarding_arguments` that "calls" its function, an
/// int32, by adding the frame's passed arguments to it.
+#[jit_abi]
extern "C" fn call_forwarding_arguments(vm: *mut u64, frame: *mut u64, pc: u32) -> i64 {
let Instruction::Call { dst, this_value, .. } = decoded(vm, pc) else {
panic!("not a Call");
@@ -371,6 +424,7 @@ extern "C" fn call_forwarding_arguments(vm: *mut u64, frame: *mut u64, pc: u32)
/// A fake `asm_helper_to_boolean`, for values that are no strings, symbols
/// or bigints: doubles by their value, and other values truthy unless they
/// are undefined or null.
+#[jit_abi]
extern "C" fn to_boolean(value: u64) -> u64 {
const NAN_BASE_TAG: u64 = 0x7FF8;
if (value >> 48) & NAN_BASE_TAG != NAN_BASE_TAG {
@@ -381,6 +435,7 @@ extern "C" fn to_boolean(value: u64) -> u64 {
/// A fake `libjs_jit_exit` that writes the exit's values into the frame,
/// like the runtime does. Leaves neither count as exits nor move the pc.
+#[jit_abi]
extern "C" fn jit_exit(vm: *mut u64, frame: *mut u64, exit_index: u32, dump: *const u64) {
if test_state(vm).borrow().exits[exit_index as usize].kind == SiteKind::Publish {
publish_frames(vm, exit_index);
@@ -609,6 +664,22 @@ pub(super) mod fake_heap {
fn region() -> &'static Region {
static REGION: OnceLock<Region> = OnceLock::new();
REGION.get_or_init(|| {
+ #[cfg(windows)]
+ // SAFETY: A fresh reservation that nothing else uses.
+ unsafe {
+ let reservation = ak::windows_code_memory::reserve(2 * REGION_SIZE);
+ assert!(!reservation.is_null());
+ let base = reservation.expose_provenance().next_multiple_of(REGION_SIZE);
+ assert!(ak::windows_code_memory::make_writable(
+ core::ptr::with_exposed_provenance_mut(base),
+ COMMITTED_SIZE,
+ ));
+ Region {
+ base,
+ next: Mutex::new(base + FIRST_CELL_OFFSET),
+ }
+ }
+ #[cfg(not(windows))]
// SAFETY: A fresh reservation that nothing else uses.
unsafe {
let reservation = libc::mmap(
@@ -684,31 +755,64 @@ pub(super) mod fake_heap {
let mut hint = LOWEST_ADDRESS;
loop {
assert!(hint + COMMITTED_SIZE <= REGION_SIZE, "no room for the low fake heap");
- // NB: The address is only a hint, so this never replaces an
- // existing mapping, which MAP_FIXED_NOREPLACE would need
- // and only Linux has. The kernel places the mapping at the
- // hint if that is free, and anywhere else otherwise.
- // SAFETY: A fresh private anonymous mapping.
- let mapping = unsafe {
- libc::mmap(
- core::ptr::with_exposed_provenance_mut(hint),
- COMMITTED_SIZE,
- libc::PROT_READ | libc::PROT_WRITE,
- libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE,
- -1,
- 0,
- )
- };
- assert_ne!(mapping, libc::MAP_FAILED);
- let address = mapping.expose_provenance();
- if address >= LOWEST_ADDRESS && address + COMMITTED_SIZE <= REGION_SIZE {
- return Region {
- base: address,
- next: Mutex::new(address),
+ #[cfg(windows)]
+ {
+ const MEM_COMMIT_AND_RESERVE: u32 = 0x3000;
+ const PAGE_READWRITE: u32 = 0x04;
+ unsafe extern "system" {
+ fn VirtualAlloc(
+ address: *mut core::ffi::c_void,
+ size: usize,
+ allocation_type: u32,
+ protect: u32,
+ ) -> *mut core::ffi::c_void;
+ }
+ // NB: This fails if the address is taken.
+ // SAFETY: Allocating memory at a given address replaces nothing.
+ let mapping = unsafe {
+ VirtualAlloc(
+ core::ptr::with_exposed_provenance_mut(hint),
+ COMMITTED_SIZE,
+ MEM_COMMIT_AND_RESERVE,
+ PAGE_READWRITE,
+ )
};
+ if !mapping.is_null() {
+ let address = mapping.expose_provenance();
+ return Region {
+ base: address,
+ next: Mutex::new(address),
+ };
+ }
+ }
+ #[cfg(not(windows))]
+ {
+ // NB: The address is only a hint, so this never replaces an
+ // existing mapping, which MAP_FIXED_NOREPLACE would need
+ // and only Linux has. The kernel places the mapping at the
+ // hint if that is free, and anywhere else otherwise.
+ // SAFETY: A fresh private anonymous mapping.
+ let mapping = unsafe {
+ libc::mmap(
+ core::ptr::with_exposed_provenance_mut(hint),
+ COMMITTED_SIZE,
+ libc::PROT_READ | libc::PROT_WRITE,
+ libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE,
+ -1,
+ 0,
+ )
+ };
+ assert_ne!(mapping, libc::MAP_FAILED);
+ let address = mapping.expose_provenance();
+ if address >= LOWEST_ADDRESS && address + COMMITTED_SIZE <= REGION_SIZE {
+ return Region {
+ base: address,
+ next: Mutex::new(address),
+ };
+ }
+ // SAFETY: The mapping was just made, and nothing uses it.
+ assert_eq!(unsafe { libc::munmap(mapping, COMMITTED_SIZE) }, 0);
}
- // SAFETY: The mapping was just made, and nothing uses it.
- assert_eq!(unsafe { libc::munmap(mapping, COMMITTED_SIZE) }, 0);
hint += 1 << 30;
}
})
@@ -832,7 +936,7 @@ impl Machine {
let trampoline = crate::asm::executable_code::ExecutableCode::new(
&generate_entry_trampoline::<crate::asm::MacroAssembler>().expect("the trampoline assembles"),
);
- type Trampoline = extern "C" fn(*mut u64, *mut u64, u64) -> JitResult;
+ type Trampoline = extern "sysv64" fn(*mut u64, *mut u64, u64) -> JitResult;
// SAFETY: The trampoline implements `Trampoline`, and compiled code
// implements the JIT entry ABI.
let enter: Trampoline = unsafe { trampoline.function() };
@@ -2507,7 +2611,7 @@ mod property_access {
/// The fake growing helper, which leaves the value in the array's
/// unused named property storage word.
- extern "C" fn grow(array: *mut u64, value: u64) -> u64 {
+ extern "sysv64" fn grow(array: *mut u64, value: u64) -> u64 {
// SAFETY: Compiled code passes a fake array of six words.
unsafe { *array.add(2) = value };
value::int32(99)
@@ -3257,6 +3361,7 @@ mod direct_calls {
/// The JIT entry of a fake callee: records its frame and returns. Like
/// compiled code, it publishes the frame (which direct calls do not)
/// unless it returns.
+ #[jit_abi]
extern "C" fn callee_entry(vm: *mut u64, frame: *mut u64) -> JitResult {
// SAFETY: The callee frame is on the fake interpreter stack, with its
// slot count in the header.
@@ -3276,6 +3381,7 @@ mod direct_calls {
/// A fake `libjs_jit_finish_direct_call` that "runs" the callee to
/// completion like the interpreter: it returns into the caller's
/// destination and pops its frame.
+ #[jit_abi]
pub(super) extern "C" fn finish_direct_call(vm: *mut u64, frame: *mut u64, pc: u32, status: u64) -> i64 {
// SAFETY: The fake VM and frames are live.
unsafe {
@@ -3519,7 +3625,7 @@ mod direct_calls {
/// The call entry of executables without JIT code, like the
/// runtime's: publishes the frame and has the interpreter run it.
- extern "C" fn not_compiled_call_entry(vm: *mut u64, frame: *mut u64) -> JitResult {
+ extern "sysv64" fn not_compiled_call_entry(vm: *mut u64, frame: *mut u64) -> JitResult {
// SAFETY: The fake VM is live.
unsafe { *vm = frame as u64 };
JitResult {
@@ -3974,7 +4080,7 @@ mod direct_calls {
}
/// A fake `libjs_jit_prepare_call_environment`.
- extern "C" fn prepare_call_environment(vm: *mut u64, function: u64, this_argument: u64) -> JitResult {
+ extern "sysv64" fn prepare_call_environment(vm: *mut u64, function: u64, this_argument: u64) -> JitResult {
let mut state = test_state(vm).borrow_mut();
state.direct.prepared.push((function, this_argument));
let (value, status) = state.direct.prepared_environment;
@@ -4235,7 +4341,7 @@ mod direct_calls {
/// A fake raw native function: returns the sum of its `this` value
/// and its arguments (as int32s), and records its frame.
- extern "C" fn fake_native(vm: *mut u64) -> JitResult {
+ extern "sysv64" fn fake_native(vm: *mut u64) -> JitResult {
// SAFETY: The running frame is the native frame on the fake
// interpreter stack.
unsafe {
@@ -4324,7 +4430,7 @@ mod direct_calls {
let code = crate::asm::executable_code::ExecutableCode::new(&callee_code.code);
let mut callee = Callee::new();
// SAFETY: Compiled code implements the JIT entry ABI.
- let entry: extern "C" fn(*mut u64, *mut u64) -> JitResult = unsafe { code.function() };
+ let entry: extern "sysv64" fn(*mut u64, *mut u64) -> JitResult = unsafe { code.function() };
callee.set_entry(entry as *const () as u64);
let mut target = callee.target(true, false);
target.function.formal_parameter_count = callee_layout.number_of_arguments;
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index b64e532b270..8f9e9ca40ed 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -17,6 +17,7 @@ use crate::ir::Float64Function;
use crate::snapshot::KeyedFeedbackSnapshot;
use crate::snapshot::keyed_feedback_bits;
use ak::math;
+use libjs_runtime_macros::jit_abi;
/// What the fake slow paths here produce.
const MARKER: u64 = (value::UNDEFINED_TAG as u64) << 48 | 0xDEAD_BEEF;
@@ -39,13 +40,29 @@ const INT32_RESULTS_FEEDBACK: u8 = 1 | 1 << 1 | 1 << 3;
/// What `run_instruction_with()` results in for code that exited.
const EXITED: u64 = (value::UNDEFINED_TAG as u64) << 48 | 0xE817;
+#[jit_abi]
extern "C" fn marker_values(vm: *mut u64, pc: u32, dst: *mut u64, _lhs: u64, _rhs: u64) -> i64 {
// SAFETY: Compiled code passes the address of a frame slot.
unsafe { *dst = MARKER };
continuation_after(vm, pc)
}
-extern "C" fn marker_scalar(vm: *mut u64, pc: u32, _instruction: *const u8, _value: u64) -> SlowPathResult {
+/// With records, `value` is the record, whose outputs get the marker.
+#[jit_abi]
+extern "C" fn marker_scalar(vm: *mut u64, pc: u32, instruction: *const u8, value: u64) -> SlowPathResult {
+ // SAFETY: Compiled code passes a pointer to its instruction.
+ if unsafe { slow_path_takes_record(instruction) } {
+ let record = value as *mut u64;
+ // SAFETY: As above, and the record is in the JIT frame.
+ unsafe {
+ let opcode = OpCode::from_u8(*instruction).expect("compiled code passes an instruction");
+ for (index, field) in slow_path_layout(opcode).fields.iter().enumerate() {
+ if field.role == OperandRole::Out {
+ *record.add(index) = MARKER;
+ }
+ }
+ }
+ }
SlowPathResult {
control: continuation_after(vm, pc),
value: MARKER,
@@ -54,6 +71,7 @@ extern "C" fn marker_scalar(vm: *mut u64, pc: u32, _instruction: *const u8, _val
/// A fake `libjs_jit_call` that writes `MARKER` into the destination of the
/// `Call`.
+#[jit_abi]
extern "C" fn marker_call(vm: *mut u64, frame: *mut u64, pc: u32) -> i64 {
let Instruction::Call { dst, .. } = decoded(vm, pc) else {
panic!("not a Call");
@@ -64,6 +82,7 @@ extern "C" fn marker_call(vm: *mut u64, frame: *mut u64, pc: u32) -> i64 {
}
/// Counts its calls, and writes `MARKER`.
+#[jit_abi]
extern "C" fn marker_comparison(vm: *mut u64, pc: u32, dst: *mut u64, _lhs: u64, _rhs: u64) -> i64 {
// SAFETY: The fake VM has a word for this, and compiled code passes the
// address of a frame slot.
@@ -75,6 +94,7 @@ extern "C" fn marker_comparison(vm: *mut u64, pc: u32, dst: *mut u64, _lhs: u64,
}
/// Counts its calls, and jumps to the true target.
+#[jit_abi]
extern "C" fn marker_jump(vm: *mut u64, _pc: u32, _lhs: u64, _rhs: u64, if_true: u32, _if_false: u32) -> i64 {
// SAFETY: The fake VM has a word for this.
unsafe { *vm.add(VM_SLOW_JUMPS_WORD) += 1 };
@@ -85,6 +105,7 @@ extern "C" fn marker_jump(vm: *mut u64, _pc: u32, _lhs: u64, _rhs: u64, if_true:
/// calls in.
const VM_SLOW_PUTS_WORD: usize = 9;
+#[jit_abi]
extern "C" fn marker_put(
vm: *mut u64,
pc: u32,
@@ -103,6 +124,7 @@ extern "C" fn marker_put(
/// Counts its calls like `marker_put`, and throws: the interpreter is to
/// take over.
+#[jit_abi]
extern "C" fn marker_throw(
vm: *mut u64,
_pc: u32,
@@ -118,11 +140,13 @@ extern "C" fn marker_throw(
/// For reads of bindings, which only reach their slow paths where a binding
/// is uninitialized: throws, and the interpreter is to take over.
+#[jit_abi]
extern "C" fn throwing_binding_read(_vm: *mut u64, _pc: u32) -> SlowPathResult {
SlowPathResult { control: -1, value: 0 }
}
/// For Increment and Decrement (one output) and their postfix forms (two).
+#[jit_abi]
extern "C" fn marker_record(vm: *mut u64, pc: u32, instruction: *const u8, outputs: *mut u64, _input: u64) -> i64 {
// SAFETY: `outputs` is the record in the JIT frame, with a word per field.
unsafe {
@@ -151,6 +175,7 @@ const STRING_PROTOTYPE_CLASS: u64 = 0x7300;
/// What the fake cache probes find: objects whose word 0 is 1 have the
/// property 42 in every cache, and accept every store into their word 1.
+#[jit_abi]
extern "C" fn fake_try_get_by_id_cache(base: u64, _cache: *const u64) -> u64 {
let object = (base & ((1 << 48) - 1)) as *const u64;
// SAFETY: The tests only probe with objects.
@@ -161,6 +186,7 @@ extern "C" fn fake_try_get_by_id_cache(base: u64, _cache: *const u64) -> u64 {
}
}
+#[jit_abi]
extern "C" fn fake_try_put_by_id_cache(_vm: *mut u64, _pc: u32, _instruction: *const u8, values: *const u64) -> i64 {
// SAFETY: The record has the base, then the value; the tests only probe
// with objects.
@@ -3076,6 +3102,7 @@ struct FakeNativeResult {
const NATIVE_GETTER_RESULT: u64 = (value::UNDEFINED_TAG as u64) << 48 | 0xFEED;
+#[jit_abi]
extern "C" fn fake_native_getter(_vm: u64) -> FakeNativeResult {
FakeNativeResult {
value: NATIVE_GETTER_RESULT,
@@ -3192,6 +3219,7 @@ fn get_by_id_reads_direct_getter_wrappers_inline_from(compressed_field: bool) {
/// argument it was called with.
const VM_NATIVE_SETTER_CALL_WORDS: usize = 10;
+#[jit_abi]
extern "C" fn fake_native_setter(vm: *mut u64) -> FakeNativeResult {
// SAFETY: The VM's word 0 is the running frame, the setter's own, and the
// fake VM has room for the words the call is recorded in.
@@ -3700,26 +3728,32 @@ fn math_builtin_calls() {
}
}
+#[jit_abi]
extern "C" fn fake_exp(number: f64) -> f64 {
math::exp(number)
}
+#[jit_abi]
extern "C" fn fake_log(number: f64) -> f64 {
math::log(number)
}
+#[jit_abi]
extern "C" fn fake_sin(number: f64) -> f64 {
math::sin(number)
}
+#[jit_abi]
extern "C" fn fake_cos(number: f64) -> f64 {
math::cos(number)
}
+#[jit_abi]
extern "C" fn fake_tan(number: f64) -> f64 {
math::tan(number)
}
+#[jit_abi]
extern "C" fn fake_pow(base: f64, exponent: f64) -> f64 {
math::pow(base, exponent)
}
@@ -4475,6 +4509,7 @@ const VM_ROPE_STRINGS_WORDS: [usize; 2] = [10, 11];
/// The fake `RopeAllocationInfo::slow_path`: records its strings, and
/// returns `ROPE_FROM_RUNTIME`.
+#[jit_abi]
extern "C" fn fake_create_rope_string(vm: *mut u64, lhs: u64, rhs: u64) -> u64 {
// SAFETY: The fake VM has room for the words the fakes here use.
unsafe {
diff --git a/Libraries/LibJS/JIT/Rust/src/lib.rs b/Libraries/LibJS/JIT/Rust/src/lib.rs
index 99e58ea9f54..11e869f104c 100644
--- a/Libraries/LibJS/JIT/Rust/src/lib.rs
+++ b/Libraries/LibJS/JIT/Rust/src/lib.rs
@@ -28,6 +28,10 @@ pub mod regalloc;
pub mod snapshot;
pub mod sparse_bitset;
+// NB: The tests get their memory on Windows from `ak::windows_code_memory`, which is for x86-64.
+#[cfg(all(test, windows, not(target_arch = "x86_64")))]
+compile_error!("the tests of the JIT run on Windows on x86-64 only");
+
use bytecode::OpCode;
use code::CompiledCode;
use snapshot::Snapshot;