From b8240493756ee309cf6a062e513105da3b15e991 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 00:35:02 -0800 Subject: [PATCH 01/14] Improved errors --- lib/vm/src/trap/traphandlers.rs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index 452e6ed61d9..bc576926aff 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -16,6 +16,17 @@ use std::mem; use std::ptr; use std::sync::Once; +cfg_if::cfg_if! { + if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { + // In Apple Silicon, the page size is 16KiB + const PAGE_SIZE: usize = 16384; + } + else { + // Otherwise, page size is 4KiB + const PAGE_SIZE: usize = 4096; + } +} + extern "C" { fn RegisterSetjmp( jmp_buf: *mut *const u8, @@ -117,8 +128,8 @@ cfg_if::cfg_if! { let (stackaddr, stacksize) = thread_stack(); // The stack and its guard page covers the // range [stackaddr - guard pages .. stackaddr + stacksize). - // We assume the guard page is 1 page, and pages are 4KiB. - if stackaddr - 4096 <= addr && addr < stackaddr + stacksize { + // We assume the guard page is 1 page, and pages are 4KiB (or 16KiB in Apple Silicon) + if stackaddr - PAGE_SIZE <= addr && addr < stackaddr + stacksize { Some(TrapCode::StackOverflow) } else { Some(TrapCode::HeapAccessOutOfBounds) @@ -202,9 +213,16 @@ cfg_if::cfg_if! { } else if #[cfg(all(target_os = "linux", target_arch = "aarch64"))] { let cx = &*(cx as *const libc::ucontext_t); cx.uc_mcontext.pc as *const u8 - } else if #[cfg(target_os = "macos")] { + } else if #[cfg(all(target_os = "macos", target_arch = "x86_64"))] { let cx = &*(cx as *const libc::ucontext_t); (*cx.uc_mcontext).__ss.__rip as *const u8 + } else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { + // println!("GET PC"); + let cx = &*(cx as *const libc::ucontext_t); + (*cx.uc_mcontext).__ss.__r15 as *const u8 // it holds the program counter - https://interrupt.memfault.com/blog/cortex-m-rtos-context-switching + // (*cx.uc_mcontext).__ss.__rip as *const u8 + // let cx = &*(cx as *const libc::ucontext_t); + // (*cx.uc_mcontext) as *const u8 } else { compile_error!("unsupported platform"); } @@ -602,6 +620,7 @@ impl CallThreadState { signal_trap: Option, call_handler: impl Fn(&SignalHandler) -> bool, ) -> *const u8 { + println!("HANDLING TRAP"); // If we hit a fault while handling a previous trap, that's quite bad, // so bail out and let the system handle this recursive segfault. // @@ -640,7 +659,9 @@ impl CallThreadState { self.handling_trap.set(false); return ptr::null(); } + println!("New unresolved 1"); let backtrace = Backtrace::new_unresolved(); + println!("New unresolved 2"); self.reset_guard_page.set(reset_guard_page); self.unwind.replace(UnwindReason::RuntimeTrap { backtrace, @@ -720,7 +741,7 @@ fn setup_unix_sigaltstack() -> Result<(), Trap> { /// The size of the sigaltstack (not including the guard, which will be /// added). Make this large enough to run our signal handlers. - const MIN_STACK_SIZE: usize = 16 * 4096; + const MIN_STACK_SIZE: usize = 16 * PAGE_SIZE; enum Tls { None, @@ -738,7 +759,7 @@ fn setup_unix_sigaltstack() -> Result<(), Trap> { // already checked _ => return Ok(()), } - + println!("TLS WITH"); // Check to see if the existing sigaltstack, if it exists, is big // enough. If so we don't need to allocate our own. let mut old_stack = mem::zeroed(); @@ -751,7 +772,7 @@ fn setup_unix_sigaltstack() -> Result<(), Trap> { // ... but failing that we need to allocate our own, so do all that // here. - let page_size: usize = libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap(); + let page_size: usize = dbg!(libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap()); let guard_size = page_size; let alloc_size = guard_size + MIN_STACK_SIZE; From 7d550ad1edb31151c6008518c0bb6f06876e139b Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 12:47:40 -0800 Subject: [PATCH 02/14] Wasm WAST tests are now working in Apple Silicon (ARM M1 chip) --- lib/vm/src/trap/traphandlers.rs | 39 ++++++++++++++------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index bc576926aff..16107c385d0 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -16,17 +16,6 @@ use std::mem; use std::ptr; use std::sync::Once; -cfg_if::cfg_if! { - if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { - // In Apple Silicon, the page size is 16KiB - const PAGE_SIZE: usize = 16384; - } - else { - // Otherwise, page size is 4KiB - const PAGE_SIZE: usize = 4096; - } -} - extern "C" { fn RegisterSetjmp( jmp_buf: *mut *const u8, @@ -129,7 +118,7 @@ cfg_if::cfg_if! { // The stack and its guard page covers the // range [stackaddr - guard pages .. stackaddr + stacksize). // We assume the guard page is 1 page, and pages are 4KiB (or 16KiB in Apple Silicon) - if stackaddr - PAGE_SIZE <= addr && addr < stackaddr + stacksize { + if stackaddr - region::page::size() <= addr && addr < stackaddr + stacksize { Some(TrapCode::StackOverflow) } else { Some(TrapCode::HeapAccessOutOfBounds) @@ -217,12 +206,20 @@ cfg_if::cfg_if! { let cx = &*(cx as *const libc::ucontext_t); (*cx.uc_mcontext).__ss.__rip as *const u8 } else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { - // println!("GET PC"); + // We need to submit this upfront in rust/libc + pub struct __darwin_arm_thread_state64 { + pub __x: [u64; 29], /* General purpose registers x0-x28 */ + pub __fp: u64, /* Frame pointer x29 */ + pub __lr: u64, /* Link register x30 */ + pub __sp: u64, /* Stack pointer x31 */ + pub __pc: u64, /* Program counter */ + pub __cpsr: u32, /* Current program status register */ + pub __pad: u32, /* Same size for 32-bit or 64-bit clients */ + }; + let cx = &*(cx as *const libc::ucontext_t); - (*cx.uc_mcontext).__ss.__r15 as *const u8 // it holds the program counter - https://interrupt.memfault.com/blog/cortex-m-rtos-context-switching - // (*cx.uc_mcontext).__ss.__rip as *const u8 - // let cx = &*(cx as *const libc::ucontext_t); - // (*cx.uc_mcontext) as *const u8 + let uc_mcontext = unsafe { std::mem::transmute::<_, *const __darwin_arm_thread_state64>(&(*cx.uc_mcontext).__ss) }; + (*uc_mcontext).__pc as *const u8 } else { compile_error!("unsupported platform"); } @@ -620,7 +617,6 @@ impl CallThreadState { signal_trap: Option, call_handler: impl Fn(&SignalHandler) -> bool, ) -> *const u8 { - println!("HANDLING TRAP"); // If we hit a fault while handling a previous trap, that's quite bad, // so bail out and let the system handle this recursive segfault. // @@ -659,9 +655,7 @@ impl CallThreadState { self.handling_trap.set(false); return ptr::null(); } - println!("New unresolved 1"); let backtrace = Backtrace::new_unresolved(); - println!("New unresolved 2"); self.reset_guard_page.set(reset_guard_page); self.unwind.replace(UnwindReason::RuntimeTrap { backtrace, @@ -741,7 +735,7 @@ fn setup_unix_sigaltstack() -> Result<(), Trap> { /// The size of the sigaltstack (not including the guard, which will be /// added). Make this large enough to run our signal handlers. - const MIN_STACK_SIZE: usize = 16 * PAGE_SIZE; + const MIN_STACK_SIZE: usize = 16 * 4096; enum Tls { None, @@ -759,7 +753,6 @@ fn setup_unix_sigaltstack() -> Result<(), Trap> { // already checked _ => return Ok(()), } - println!("TLS WITH"); // Check to see if the existing sigaltstack, if it exists, is big // enough. If so we don't need to allocate our own. let mut old_stack = mem::zeroed(); @@ -772,7 +765,7 @@ fn setup_unix_sigaltstack() -> Result<(), Trap> { // ... but failing that we need to allocate our own, so do all that // here. - let page_size: usize = dbg!(libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap()); + let page_size: usize = region::page::size(); let guard_size = page_size; let alloc_size = guard_size + MIN_STACK_SIZE; From c161f01393d4263c64227bcf5b6a7f690d875051 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 13:05:09 -0800 Subject: [PATCH 03/14] Added reference to rust/libc issue --- lib/vm/src/trap/traphandlers.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index 16107c385d0..9c45259e559 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -206,7 +206,8 @@ cfg_if::cfg_if! { let cx = &*(cx as *const libc::ucontext_t); (*cx.uc_mcontext).__ss.__rip as *const u8 } else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { - // We need to submit this upfront in rust/libc + // This should be integrated into rust/libc + // Related issue: https://github.com/rust-lang/libc/issues/1977 pub struct __darwin_arm_thread_state64 { pub __x: [u64; 29], /* General purpose registers x0-x28 */ pub __fp: u64, /* Frame pointer x29 */ @@ -216,7 +217,7 @@ cfg_if::cfg_if! { pub __cpsr: u32, /* Current program status register */ pub __pad: u32, /* Same size for 32-bit or 64-bit clients */ }; - + let cx = &*(cx as *const libc::ucontext_t); let uc_mcontext = unsafe { std::mem::transmute::<_, *const __darwin_arm_thread_state64>(&(*cx.uc_mcontext).__ss) }; (*uc_mcontext).__pc as *const u8 From eba54b78efba87a5dacf89a058f6b81cfbe47309 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 18:31:44 -0800 Subject: [PATCH 04/14] Improved Native function tests --- tests/compilers/native_functions.rs | 108 ++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tests/compilers/native_functions.rs b/tests/compilers/native_functions.rs index 66b3fada8f1..0e570286c5e 100644 --- a/tests/compilers/native_functions.rs +++ b/tests/compilers/native_functions.rs @@ -6,6 +6,15 @@ use std::rc::Rc; use wasmer::*; + +fn long_f(a:u32, b:u32, c:u32, d:u32, e:u32, f:u16, g:u64, h:u64, i:u16, j:u32) -> u64 { + j as u64 + i as u64*10 + h*100 + g*1000 + f as u64 *10000 + e as u64 *100000 + d as u64 *1000000 + c as u64 *10000000 + b as u64 * 100000000 + a as u64 * 1000000000 +} + +fn long_f_dynamic(values: &[Value]) -> Result, RuntimeError> { + Ok(vec![Value::I64(values[9].unwrap_i32() as i64 + values[8].unwrap_i32() as i64*10 + values[7].unwrap_i64()*100 + values[6].unwrap_i64()*1000 + values[5].unwrap_i32() as i64 *10000 + values[4].unwrap_i32() as i64 *100000 + values[3].unwrap_i32() as i64 *1000000 + values[2].unwrap_i32() as i64 *10000000 + values[1].unwrap_i32() as i64 * 100000000 + values[0].unwrap_i32() as i64 * 1000000000)]) +} + #[test] fn native_function_works_for_wasm() -> Result<()> { let store = get_store(false); @@ -50,6 +59,93 @@ fn native_function_works_for_wasm() -> Result<()> { Ok(()) } + +// The native ABI for functions fails when defining a function natively in +// macos (Darwin) with the Apple Silicon ARM chip +// TODO: Cranelift should have a good ABI for the ABI +#[test] +#[cfg_attr( + any( + feature = "test-cranelift", + target_os = "macos", + target_arch = "aarch64", + ), + ignore +)] +fn native_function_works_for_wasm_function_manyparams() -> Result<()> { + let store = get_store(false); + let wat = r#"(module + (func $longf (import "env" "longf") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64)) + (func (export "longf_pure") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64) + (call $longf (local.get 0) (local.get 1) (local.get 2) (local.get 3) (local.get 4) (local.get 5) (local.get 6) (local.get 7) (local.get 8) (local.get 9))) + (func (export "longf") (result i64) + (call $longf (i32.const 1) (i32.const 2) (i32.const 3) (i32.const 4) (i32.const 5) (i32.const 6) (i64.const 7) (i64.const 8) (i32.const 9) (i32.const 0))) +)"#; + let module = Module::new(&store, wat).unwrap(); + + let import_object = imports! { + "env" => { + "longf" => Function::new_native(&store, long_f), + }, + }; + + let instance = Instance::new(&module, &import_object)?; + + { + let dyn_f: &Function = instance.exports.get("longf")?; + let f: NativeFunc<(), i64> = dyn_f.native().unwrap(); + let result = f.call()?; + assert_eq!(result, 1234567890); + } + + { + let dyn_f: &Function = instance.exports.get("longf_pure")?; + let f: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = dyn_f.native().unwrap(); + let result = f.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; + assert_eq!(result, 1234567890); + } + + Ok(()) +} + + +#[test] +fn native_function_works_for_wasm_function_manyparams_dynamic() -> Result<()> { + let store = get_store(false); + let wat = r#"(module + (func $longf (import "env" "longf") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64)) + (func (export "longf_pure") (param i32 i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i64) + (call $longf (local.get 0) (local.get 1) (local.get 2) (local.get 3) (local.get 4) (local.get 5) (local.get 6) (local.get 7) (local.get 8) (local.get 9))) + (func (export "longf") (result i64) + (call $longf (i32.const 1) (i32.const 2) (i32.const 3) (i32.const 4) (i32.const 5) (i32.const 6) (i64.const 7) (i64.const 8) (i32.const 9) (i32.const 0))) +)"#; + let module = Module::new(&store, wat).unwrap(); + + let import_object = imports! { + "env" => { + "longf" => Function::new(&store, &FunctionType::new(vec![ValType::I32, ValType::I32, ValType::I32, ValType::I32, ValType::I32, ValType::I32, ValType::I64 , ValType::I64 ,ValType::I32, ValType::I32], vec![ValType::I64]), long_f_dynamic), + }, + }; + + let instance = Instance::new(&module, &import_object)?; + + { + let dyn_f: &Function = instance.exports.get("longf")?; + let f: NativeFunc<(), i64> = dyn_f.native().unwrap(); + let result = f.call()?; + assert_eq!(result, 1234567890); + } + + { + let dyn_f: &Function = instance.exports.get("longf_pure")?; + let f: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = dyn_f.native().unwrap(); + let result = f.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; + assert_eq!(result, 1234567890); + } + + Ok(()) +} + #[test] fn static_host_function_without_env() -> anyhow::Result<()> { let store = get_store(false); @@ -62,6 +158,10 @@ fn static_host_function_without_env() -> anyhow::Result<()> { Ok((d * 4.0, c * 3.0, b * 2, a * 1)) } + fn long_f(a:u32, b:u32, c:u32, d:u32, e:u32, f:u16, g:u64, h:u64, i:u16, j:u32) -> (u32, u64, u32) { + (a+b*10+c*100+d*1000+e*10000+f as u32*100000, g+h*10, i as u32+j*10) + } + // Native static host function that returns a tuple. { let f = Function::new_native(&store, f); @@ -70,6 +170,14 @@ fn static_host_function_without_env() -> anyhow::Result<()> { assert_eq!(result, (28.0, 15.0, 6, 1)); } + // Native static host function that returns a tuple. + { + let long_f = Function::new_native(&store, long_f); + let long_f_native: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), (u32, u64, u32)> = long_f.native().unwrap(); + let result = long_f_native.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; + assert_eq!(result, (654321, 87, 09)); + } + // Native static host function that returns a result of a tuple. { let f = Function::new_native(&store, f_ok); From b9f0d55f2e8a49e715ad207db0c7f2a917f09f81 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 18:33:04 -0800 Subject: [PATCH 05/14] Improved traphandlers --- lib/vm/src/trap/traphandlers.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index 9c45259e559..de6582cf4b6 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -206,8 +206,10 @@ cfg_if::cfg_if! { let cx = &*(cx as *const libc::ucontext_t); (*cx.uc_mcontext).__ss.__rip as *const u8 } else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { - // This should be integrated into rust/libc + use std::mem; + // TODO: This should be integrated into rust/libc // Related issue: https://github.com/rust-lang/libc/issues/1977 + #[allow(non_camel_case_types)] pub struct __darwin_arm_thread_state64 { pub __x: [u64; 29], /* General purpose registers x0-x28 */ pub __fp: u64, /* Frame pointer x29 */ @@ -219,7 +221,7 @@ cfg_if::cfg_if! { }; let cx = &*(cx as *const libc::ucontext_t); - let uc_mcontext = unsafe { std::mem::transmute::<_, *const __darwin_arm_thread_state64>(&(*cx.uc_mcontext).__ss) }; + let uc_mcontext = mem::transmute::<_, *const __darwin_arm_thread_state64>(&(*cx.uc_mcontext).__ss); (*uc_mcontext).__pc as *const u8 } else { compile_error!("unsupported platform"); @@ -725,7 +727,6 @@ mod tls { #[cfg(unix)] fn setup_unix_sigaltstack() -> Result<(), Trap> { use std::cell::RefCell; - use std::convert::TryInto; use std::ptr::null_mut; thread_local! { From 517fac2c96477857330b201238af5ca95a4e23e3 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 18:35:36 -0800 Subject: [PATCH 06/14] Disallow Native Functions with more than 9 params in Apple Silicon --- lib/engine/src/resolver.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/engine/src/resolver.rs b/lib/engine/src/resolver.rs index 4c36dab3141..e0389e83bed 100644 --- a/lib/engine/src/resolver.rs +++ b/lib/engine/src/resolver.rs @@ -163,7 +163,17 @@ pub fn resolve_imports( // TODO: We should check that the f.vmctx actually matches // the shape of `VMDynamicFunctionImportContext` } - VMFunctionKind::Static => f.address, + VMFunctionKind::Static => { + // The native ABI for functions fails when defining a function natively in + // macos (Darwin) with the Apple Silicon ARM chip, for functions with more than 10 args + // TODO: Cranelift should have a good ABI for the ABI + if cfg!(all(target_os="macos", target_arch="aarch64")) { + let num_params = f.signature.params().len(); + assert!(num_params < 9, "Only native functions with less than 9 arguments are allowed in Apple Silicon (for now). Received {} in the import {}.{}", num_params, module_name, field); + } + + f.address + }, }; function_imports.push(VMFunctionImport { body: address, From 1f66798bed859e5335001d3ba948e4f0528042b1 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 19:25:57 -0800 Subject: [PATCH 07/14] All WASI tests now pass in Apple Silicon --- lib/cli/src/commands/compile.rs | 2 +- lib/engine/src/resolver.rs | 4 +- lib/wasi/src/lib.rs | 34 ++++++++++++++- lib/wasi/src/ptr.rs | 6 +++ lib/wasi/src/syscalls/mod.rs | 29 ++++++++++++- tests/compilers/native_functions.rs | 65 ++++++++++++++++++++++------- 6 files changed, 120 insertions(+), 20 deletions(-) diff --git a/lib/cli/src/commands/compile.rs b/lib/cli/src/commands/compile.rs index 2d4e0c4065a..7e85bce2e74 100644 --- a/lib/cli/src/commands/compile.rs +++ b/lib/cli/src/commands/compile.rs @@ -87,7 +87,7 @@ impl Compile { if ext != recommended_extension { warning!("the output file has a wrong extension. We recommend using `{}.{}` for the chosen target", &output_filename, &recommended_extension) } - }, + } None => { warning!("the output file has no extension. We recommend using `{}.{}` for the chosen target", &output_filename, &recommended_extension) } diff --git a/lib/engine/src/resolver.rs b/lib/engine/src/resolver.rs index e0389e83bed..be3e03e5849 100644 --- a/lib/engine/src/resolver.rs +++ b/lib/engine/src/resolver.rs @@ -167,13 +167,13 @@ pub fn resolve_imports( // The native ABI for functions fails when defining a function natively in // macos (Darwin) with the Apple Silicon ARM chip, for functions with more than 10 args // TODO: Cranelift should have a good ABI for the ABI - if cfg!(all(target_os="macos", target_arch="aarch64")) { + if cfg!(all(target_os = "macos", target_arch = "aarch64")) { let num_params = f.signature.params().len(); assert!(num_params < 9, "Only native functions with less than 9 arguments are allowed in Apple Silicon (for now). Received {} in the import {}.{}", num_params, module_name, field); } f.address - }, + } }; function_imports.push(VMFunctionImport { body: address, diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index cdca70e6ed3..389b0c5deea 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -30,6 +30,8 @@ pub use crate::utils::{get_wasi_version, is_wasi_module, WasiVersion}; use thiserror::Error; use wasmer::{imports, Function, ImportObject, Memory, Module, Store}; +#[cfg(all(target_os = "macos", target_arch = "aarch64",))] +use wasmer::{FunctionType, ValType}; use std::cell::UnsafeCell; use std::fmt; @@ -201,6 +203,34 @@ pub fn generate_import_object_from_env( } } +// Note: we use this wrapper because native functions with more than 9 params +// fail on Apple Silicon (with Cranelift). +fn get_path_open_for_store(store: &Store, env: WasiEnv) -> Function { + #[cfg(not(all(target_os = "macos", target_arch = "aarch64",)))] + let path_open = Function::new_native_with_env(store, env.clone(), path_open); + #[cfg(all(target_os = "macos", target_arch = "aarch64",))] + let path_open = Function::new_with_env( + store, + &FunctionType::new( + vec![ + ValType::I32, + ValType::I32, + ValType::I32, + ValType::I32, + ValType::I32, + ValType::I64, + ValType::I64, + ValType::I32, + ValType::I32, + ], + vec![ValType::I32], + ), + env.clone(), + path_open_dynamic, + ); + path_open +} + /// Combines a state generating function with the import list for legacy WASI fn generate_import_object_snapshot0(store: &Store, env: WasiEnv) -> ImportObject { imports! { @@ -236,7 +266,7 @@ fn generate_import_object_snapshot0(store: &Store, env: WasiEnv) -> ImportObject "path_filestat_get" => Function::new_native_with_env(store, env.clone(), legacy::snapshot0::path_filestat_get), "path_filestat_set_times" => Function::new_native_with_env(store, env.clone(), path_filestat_set_times), "path_link" => Function::new_native_with_env(store, env.clone(), path_link), - "path_open" => Function::new_native_with_env(store, env.clone(), path_open), + "path_open" => get_path_open_for_store(store, env.clone()), "path_readlink" => Function::new_native_with_env(store, env.clone(), path_readlink), "path_remove_directory" => Function::new_native_with_env(store, env.clone(), path_remove_directory), "path_rename" => Function::new_native_with_env(store, env.clone(), path_rename), @@ -289,7 +319,7 @@ fn generate_import_object_snapshot1(store: &Store, env: WasiEnv) -> ImportObject "path_filestat_get" => Function::new_native_with_env(store, env.clone(), path_filestat_get), "path_filestat_set_times" => Function::new_native_with_env(store, env.clone(), path_filestat_set_times), "path_link" => Function::new_native_with_env(store, env.clone(), path_link), - "path_open" => Function::new_native_with_env(store, env.clone(), path_open), + "path_open" => get_path_open_for_store(store, env.clone()), "path_readlink" => Function::new_native_with_env(store, env.clone(), path_readlink), "path_remove_directory" => Function::new_native_with_env(store, env.clone(), path_remove_directory), "path_rename" => Function::new_native_with_env(store, env.clone(), path_rename), diff --git a/lib/wasi/src/ptr.rs b/lib/wasi/src/ptr.rs index a4c76e3e39e..430c2b6ec38 100644 --- a/lib/wasi/src/ptr.rs +++ b/lib/wasi/src/ptr.rs @@ -24,6 +24,12 @@ impl fmt::Debug for WasmPtr { } } +impl From for WasmPtr { + fn from(offset: i32) -> Self { + Self::new(offset as _) + } +} + unsafe impl FromToNativeWasmType for WasmPtr { type Native = as FromToNativeWasmType>::Native; diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 23ecd7b58b4..516115319e6 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -28,7 +28,7 @@ use std::cell::Cell; use std::convert::{Infallible, TryInto}; use std::io::{self, Read, Seek, Write}; use tracing::{debug, trace}; -use wasmer::{Memory, RuntimeError}; +use wasmer::{Memory, RuntimeError, Value}; #[cfg(any( target_os = "freebsd", @@ -1884,6 +1884,33 @@ pub fn path_open( __WASI_ESUCCESS } +// Note: we define path_open_dynamic because native functions with more than 9 params +// fail on Apple Silicon (with Cranelift). +pub fn path_open_dynamic(env: &mut WasiEnv, params: &[Value]) -> Result, RuntimeError> { + let dirfd: __wasi_fd_t = params[0].unwrap_i32() as _; + let dirflags: __wasi_lookupflags_t = params[1].unwrap_i32() as _; + let path: WasmPtr = params[2].unwrap_i32().into(); + let path_len: u32 = params[3].unwrap_i32() as _; + let o_flags: __wasi_oflags_t = params[4].unwrap_i32() as _; + let fs_rights_base: __wasi_rights_t = params[5].unwrap_i64() as _; + let fs_rights_inheriting: __wasi_rights_t = params[6].unwrap_i64() as _; + let fs_flags: __wasi_fdflags_t = params[7].unwrap_i32() as _; + let fd: WasmPtr<__wasi_fd_t> = params[8].unwrap_i32().into(); + + Ok(vec![Value::I32(path_open( + env, + dirfd, + dirflags, + path, + path_len, + o_flags, + fs_rights_base, + fs_rights_inheriting, + fs_flags, + fd, + ) as i32)]) +} + /// ### `path_readlink()` /// Read the value of a symlink /// Inputs: diff --git a/tests/compilers/native_functions.rs b/tests/compilers/native_functions.rs index 0e570286c5e..795c9a7fc74 100644 --- a/tests/compilers/native_functions.rs +++ b/tests/compilers/native_functions.rs @@ -6,13 +6,32 @@ use std::rc::Rc; use wasmer::*; - -fn long_f(a:u32, b:u32, c:u32, d:u32, e:u32, f:u16, g:u64, h:u64, i:u16, j:u32) -> u64 { - j as u64 + i as u64*10 + h*100 + g*1000 + f as u64 *10000 + e as u64 *100000 + d as u64 *1000000 + c as u64 *10000000 + b as u64 * 100000000 + a as u64 * 1000000000 +fn long_f(a: u32, b: u32, c: u32, d: u32, e: u32, f: u16, g: u64, h: u64, i: u16, j: u32) -> u64 { + j as u64 + + i as u64 * 10 + + h * 100 + + g * 1000 + + f as u64 * 10000 + + e as u64 * 100000 + + d as u64 * 1000000 + + c as u64 * 10000000 + + b as u64 * 100000000 + + a as u64 * 1000000000 } fn long_f_dynamic(values: &[Value]) -> Result, RuntimeError> { - Ok(vec![Value::I64(values[9].unwrap_i32() as i64 + values[8].unwrap_i32() as i64*10 + values[7].unwrap_i64()*100 + values[6].unwrap_i64()*1000 + values[5].unwrap_i32() as i64 *10000 + values[4].unwrap_i32() as i64 *100000 + values[3].unwrap_i32() as i64 *1000000 + values[2].unwrap_i32() as i64 *10000000 + values[1].unwrap_i32() as i64 * 100000000 + values[0].unwrap_i32() as i64 * 1000000000)]) + Ok(vec![Value::I64( + values[9].unwrap_i32() as i64 + + values[8].unwrap_i32() as i64 * 10 + + values[7].unwrap_i64() * 100 + + values[6].unwrap_i64() * 1000 + + values[5].unwrap_i32() as i64 * 10000 + + values[4].unwrap_i32() as i64 * 100000 + + values[3].unwrap_i32() as i64 * 1000000 + + values[2].unwrap_i32() as i64 * 10000000 + + values[1].unwrap_i32() as i64 * 100000000 + + values[0].unwrap_i32() as i64 * 1000000000, + )]) } #[test] @@ -59,13 +78,12 @@ fn native_function_works_for_wasm() -> Result<()> { Ok(()) } - // The native ABI for functions fails when defining a function natively in // macos (Darwin) with the Apple Silicon ARM chip -// TODO: Cranelift should have a good ABI for the ABI +// TODO: Cranelift should have a good ABI for the ABI #[test] #[cfg_attr( - any( + all( feature = "test-cranelift", target_os = "macos", target_arch = "aarch64", @@ -100,7 +118,8 @@ fn native_function_works_for_wasm_function_manyparams() -> Result<()> { { let dyn_f: &Function = instance.exports.get("longf_pure")?; - let f: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = dyn_f.native().unwrap(); + let f: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = + dyn_f.native().unwrap(); let result = f.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; assert_eq!(result, 1234567890); } @@ -108,7 +127,6 @@ fn native_function_works_for_wasm_function_manyparams() -> Result<()> { Ok(()) } - #[test] fn native_function_works_for_wasm_function_manyparams_dynamic() -> Result<()> { let store = get_store(false); @@ -138,7 +156,8 @@ fn native_function_works_for_wasm_function_manyparams_dynamic() -> Result<()> { { let dyn_f: &Function = instance.exports.get("longf_pure")?; - let f: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = dyn_f.native().unwrap(); + let f: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), i64> = + dyn_f.native().unwrap(); let result = f.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; assert_eq!(result, 1234567890); } @@ -158,10 +177,25 @@ fn static_host_function_without_env() -> anyhow::Result<()> { Ok((d * 4.0, c * 3.0, b * 2, a * 1)) } - fn long_f(a:u32, b:u32, c:u32, d:u32, e:u32, f:u16, g:u64, h:u64, i:u16, j:u32) -> (u32, u64, u32) { - (a+b*10+c*100+d*1000+e*10000+f as u32*100000, g+h*10, i as u32+j*10) + fn long_f( + a: u32, + b: u32, + c: u32, + d: u32, + e: u32, + f: u16, + g: u64, + h: u64, + i: u16, + j: u32, + ) -> (u32, u64, u32) { + ( + a + b * 10 + c * 100 + d * 1000 + e * 10000 + f as u32 * 100000, + g + h * 10, + i as u32 + j * 10, + ) } - + // Native static host function that returns a tuple. { let f = Function::new_native(&store, f); @@ -173,7 +207,10 @@ fn static_host_function_without_env() -> anyhow::Result<()> { // Native static host function that returns a tuple. { let long_f = Function::new_native(&store, long_f); - let long_f_native: NativeFunc<(u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), (u32, u64, u32)> = long_f.native().unwrap(); + let long_f_native: NativeFunc< + (u32, u32, u32, u32, u32, u16, u64, u64, u16, u32), + (u32, u64, u32), + > = long_f.native().unwrap(); let result = long_f_native.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)?; assert_eq!(result, (654321, 87, 09)); } From f1d0840987bd4331d29984e4b8c92dc021c0df0c Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 20:29:12 -0800 Subject: [PATCH 08/14] Improved Makefile --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 3a564329ac3..724d5448675 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,10 @@ ifeq ($(ARCH), aarch64) endif endif +ifeq ($(ARCH), arm64) + test_compilers_engines += cranelift-jit +endif + compilers := $(filter-out ,$(compilers)) test_compilers_engines := $(filter-out ,$(test_compilers_engines)) From debd76babec15566315839038eb8ac6c190198f1 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 20:29:46 -0800 Subject: [PATCH 09/14] Updated Cargo.lock in runtime-core,runtime --- lib/deprecated/runtime-core/Cargo.lock | 145 ++++++++++++------------- lib/deprecated/runtime/Cargo.lock | 145 ++++++++++++------------- 2 files changed, 144 insertions(+), 146 deletions(-) diff --git a/lib/deprecated/runtime-core/Cargo.lock b/lib/deprecated/runtime-core/Cargo.lock index 8b2331c5743..d971b374ec1 100644 --- a/lib/deprecated/runtime-core/Cargo.lock +++ b/lib/deprecated/runtime-core/Cargo.lock @@ -120,6 +120,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "const_fn" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -128,18 +134,18 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "cranelift-bforest" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9413a2c6bdb01ab8acc867421bd7343ddea491d015453f4e56f4f60c816d120" +checksum = "0f065f6889758f817f61a230220d1811ba99a9762af2fb69ae23048314f75ff2" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28d389588c2375bb95292e0bc6cbf010e7f30fb4e9734738b576521b737826a" +checksum = "510aa2ab4307644100682b94e449940a0ea15c5887f1d4b9678b8dd5ef31e736" dependencies = [ "byteorder", "cranelift-bforest", @@ -149,7 +155,6 @@ dependencies = [ "gimli 0.21.0", "log", "regalloc", - "serde", "smallvec", "target-lexicon", "thiserror", @@ -157,9 +162,9 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74dd3cf6f107c1df4c2b8aab91ec4181aee7ff17289673fcbec63325e7e40a83" +checksum = "b4cb0c7e87c60d63b35f9670c15479ee4a5e557dd127efab88b2f9b2ca83c9a0" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -167,24 +172,24 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efaf050fab2dbf324544489443ff3cc8c67c9420c8902ec6628bd906bd7393e9" +checksum = "60636227098693e06de8d6d88beea2a7d32ecf8a8030dacdb57c68e06f381826" [[package]] name = "cranelift-entity" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f07eb8aa0a5da94b56339e4e3052c496a3df4354357cd5da8c7b02c6e8f1dc1d" +checksum = "6156db73e0c9f65f80c512988d63ec736be0dee3dd66bf951e3e28aed9dc02d3" dependencies = [ "serde", ] [[package]] name = "cranelift-frontend" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f6fe1d3e968576f4b60d23f40ee90281f8de2cdf23d2110f3b0296ff420555e" +checksum = "e09cd158c9a820a4cc14a34076811da225cce1d31dc6d03c5ef85b91aef560b9" dependencies = [ "cranelift-codegen", "log", @@ -202,50 +207,49 @@ dependencies = [ ] [[package]] -name = "crossbeam-deque" -version = "0.7.3" +name = "crossbeam-channel" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" dependencies = [ - "crossbeam-epoch", + "cfg-if 1.0.0", "crossbeam-utils", - "maybe-uninit", ] [[package]] -name = "crossbeam-epoch" -version = "0.8.2" +name = "crossbeam-deque" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" dependencies = [ - "autocfg", - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "crossbeam-epoch", "crossbeam-utils", - "lazy_static", - "maybe-uninit", - "memoffset", - "scopeguard", ] [[package]] -name = "crossbeam-queue" -version = "0.2.3" +name = "crossbeam-epoch" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" +checksum = "ec0f606a85340376eef0d6d8fec399e6d4a544d648386c6645eb6d0653b27d9f" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "const_fn", "crossbeam-utils", - "maybe-uninit", + "lazy_static", + "memoffset", + "scopeguard", ] [[package]] name = "crossbeam-utils" -version = "0.7.2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5" dependencies = [ "autocfg", - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "const_fn", "lazy_static", ] @@ -570,12 +574,6 @@ dependencies = [ "libc", ] -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "memchr" version = "2.3.3" @@ -637,19 +635,19 @@ dependencies = [ [[package]] name = "object" -version = "0.19.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cbca9424c482ee628fa549d9c812e2cd22f1180b9222c9200fdfa6eb31aecb2" -dependencies = [ - "crc32fast", - "indexmap", -] +checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" [[package]] name = "object" -version = "0.20.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" +checksum = "37fd5004feb2ce328a52b0b3d01dbf4ffff72583493900ed15f22d4111c51693" +dependencies = [ + "crc32fast", + "indexmap", +] [[package]] name = "once_cell" @@ -793,9 +791,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.3.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f02856753d04e03e26929f820d0a0a337ebe71f849801eea335d464b349080" +checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" dependencies = [ "autocfg", "crossbeam-deque", @@ -805,12 +803,12 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.7.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e92e15d89083484e11353891f1af602cc661426deb9564c298b270c726973280" +checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" dependencies = [ + "crossbeam-channel", "crossbeam-deque", - "crossbeam-queue", "crossbeam-utils", "lazy_static", "num_cpus", @@ -824,9 +822,9 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "regalloc" -version = "0.0.26" +version = "0.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c03092d79e0fd610932d89ed53895a38c0dd3bcd317a0046e69940de32f1d95" +checksum = "2041c2d34f6ff346d6f428974f03d8bf12679b0c816bb640dc5eb1d48848d8d1" dependencies = [ "log", "rustc-hash", @@ -1011,9 +1009,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.10.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab0e7238dcc7b40a7be719a25365910f6807bd864f4cce6b2e6b873658e2b19d" +checksum = "4ee5a98e506fb7231a304c3a1bd7c132a55016cf65001e0282480665870dfcb9" [[package]] name = "tempfile" @@ -1115,7 +1113,7 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasmer" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "cfg-if 0.1.10", "indexmap", @@ -1137,7 +1135,7 @@ dependencies = [ [[package]] name = "wasmer-cache" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "blake3", "hex", @@ -1148,7 +1146,7 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "enumset", "raw-cpuid", @@ -1164,7 +1162,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-cranelift" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "cranelift-codegen", "cranelift-frontend", @@ -1172,6 +1170,7 @@ dependencies = [ "more-asserts", "rayon", "serde", + "smallvec", "tracing", "wasmer-compiler", "wasmer-types", @@ -1180,7 +1179,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-llvm" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "byteorder", "cc", @@ -1202,7 +1201,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-singlepass" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "byteorder", "dynasm", @@ -1219,7 +1218,7 @@ dependencies = [ [[package]] name = "wasmer-engine" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "backtrace", "bincode", @@ -1237,7 +1236,7 @@ dependencies = [ [[package]] name = "wasmer-engine-jit" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "bincode", "cfg-if 0.1.10", @@ -1253,7 +1252,7 @@ dependencies = [ [[package]] name = "wasmer-engine-native" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "bincode", "cfg-if 0.1.10", @@ -1272,9 +1271,9 @@ dependencies = [ [[package]] name = "wasmer-object" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ - "object 0.19.0", + "object 0.21.1", "thiserror", "wasmer-compiler", "wasmer-types", @@ -1300,7 +1299,7 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "cranelift-entity", "serde", @@ -1308,7 +1307,7 @@ dependencies = [ [[package]] name = "wasmer-vm" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "backtrace", "cc", @@ -1326,9 +1325,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.57.0" +version = "0.65.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6" +checksum = "87cc2fe6350834b4e528ba0901e7aa405d78b89dc1fa3145359eb4de0e323fcf" [[package]] name = "wast" diff --git a/lib/deprecated/runtime/Cargo.lock b/lib/deprecated/runtime/Cargo.lock index e4cfe71133e..a3584c5886e 100644 --- a/lib/deprecated/runtime/Cargo.lock +++ b/lib/deprecated/runtime/Cargo.lock @@ -120,6 +120,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "const_fn" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab" + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -128,18 +134,18 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "cranelift-bforest" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9413a2c6bdb01ab8acc867421bd7343ddea491d015453f4e56f4f60c816d120" +checksum = "0f065f6889758f817f61a230220d1811ba99a9762af2fb69ae23048314f75ff2" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28d389588c2375bb95292e0bc6cbf010e7f30fb4e9734738b576521b737826a" +checksum = "510aa2ab4307644100682b94e449940a0ea15c5887f1d4b9678b8dd5ef31e736" dependencies = [ "byteorder", "cranelift-bforest", @@ -149,7 +155,6 @@ dependencies = [ "gimli 0.21.0", "log", "regalloc", - "serde", "smallvec", "target-lexicon", "thiserror", @@ -157,9 +162,9 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74dd3cf6f107c1df4c2b8aab91ec4181aee7ff17289673fcbec63325e7e40a83" +checksum = "b4cb0c7e87c60d63b35f9670c15479ee4a5e557dd127efab88b2f9b2ca83c9a0" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -167,24 +172,24 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efaf050fab2dbf324544489443ff3cc8c67c9420c8902ec6628bd906bd7393e9" +checksum = "60636227098693e06de8d6d88beea2a7d32ecf8a8030dacdb57c68e06f381826" [[package]] name = "cranelift-entity" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f07eb8aa0a5da94b56339e4e3052c496a3df4354357cd5da8c7b02c6e8f1dc1d" +checksum = "6156db73e0c9f65f80c512988d63ec736be0dee3dd66bf951e3e28aed9dc02d3" dependencies = [ "serde", ] [[package]] name = "cranelift-frontend" -version = "0.65.0" +version = "0.67.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f6fe1d3e968576f4b60d23f40ee90281f8de2cdf23d2110f3b0296ff420555e" +checksum = "e09cd158c9a820a4cc14a34076811da225cce1d31dc6d03c5ef85b91aef560b9" dependencies = [ "cranelift-codegen", "log", @@ -202,50 +207,49 @@ dependencies = [ ] [[package]] -name = "crossbeam-deque" -version = "0.7.3" +name = "crossbeam-channel" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" dependencies = [ - "crossbeam-epoch", + "cfg-if 1.0.0", "crossbeam-utils", - "maybe-uninit", ] [[package]] -name = "crossbeam-epoch" -version = "0.8.2" +name = "crossbeam-deque" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" dependencies = [ - "autocfg", - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "crossbeam-epoch", "crossbeam-utils", - "lazy_static", - "maybe-uninit", - "memoffset", - "scopeguard", ] [[package]] -name = "crossbeam-queue" -version = "0.2.3" +name = "crossbeam-epoch" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" +checksum = "ec0f606a85340376eef0d6d8fec399e6d4a544d648386c6645eb6d0653b27d9f" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "const_fn", "crossbeam-utils", - "maybe-uninit", + "lazy_static", + "memoffset", + "scopeguard", ] [[package]] name = "crossbeam-utils" -version = "0.7.2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5" dependencies = [ "autocfg", - "cfg-if 0.1.10", + "cfg-if 1.0.0", + "const_fn", "lazy_static", ] @@ -570,12 +574,6 @@ dependencies = [ "libc", ] -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "memchr" version = "2.3.3" @@ -637,19 +635,19 @@ dependencies = [ [[package]] name = "object" -version = "0.19.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cbca9424c482ee628fa549d9c812e2cd22f1180b9222c9200fdfa6eb31aecb2" -dependencies = [ - "crc32fast", - "indexmap", -] +checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" [[package]] name = "object" -version = "0.20.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" +checksum = "37fd5004feb2ce328a52b0b3d01dbf4ffff72583493900ed15f22d4111c51693" +dependencies = [ + "crc32fast", + "indexmap", +] [[package]] name = "once_cell" @@ -793,9 +791,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.3.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f02856753d04e03e26929f820d0a0a337ebe71f849801eea335d464b349080" +checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" dependencies = [ "autocfg", "crossbeam-deque", @@ -805,12 +803,12 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.7.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e92e15d89083484e11353891f1af602cc661426deb9564c298b270c726973280" +checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" dependencies = [ + "crossbeam-channel", "crossbeam-deque", - "crossbeam-queue", "crossbeam-utils", "lazy_static", "num_cpus", @@ -824,9 +822,9 @@ checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" [[package]] name = "regalloc" -version = "0.0.26" +version = "0.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c03092d79e0fd610932d89ed53895a38c0dd3bcd317a0046e69940de32f1d95" +checksum = "2041c2d34f6ff346d6f428974f03d8bf12679b0c816bb640dc5eb1d48848d8d1" dependencies = [ "log", "rustc-hash", @@ -1011,9 +1009,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.10.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab0e7238dcc7b40a7be719a25365910f6807bd864f4cce6b2e6b873658e2b19d" +checksum = "4ee5a98e506fb7231a304c3a1bd7c132a55016cf65001e0282480665870dfcb9" [[package]] name = "tempfile" @@ -1115,7 +1113,7 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasmer" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "cfg-if 0.1.10", "indexmap", @@ -1137,7 +1135,7 @@ dependencies = [ [[package]] name = "wasmer-cache" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "blake3", "hex", @@ -1148,7 +1146,7 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "enumset", "raw-cpuid", @@ -1164,7 +1162,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-cranelift" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "cranelift-codegen", "cranelift-frontend", @@ -1172,6 +1170,7 @@ dependencies = [ "more-asserts", "rayon", "serde", + "smallvec", "tracing", "wasmer-compiler", "wasmer-types", @@ -1180,7 +1179,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-llvm" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "byteorder", "cc", @@ -1202,7 +1201,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-singlepass" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "byteorder", "dynasm", @@ -1219,7 +1218,7 @@ dependencies = [ [[package]] name = "wasmer-engine" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "backtrace", "bincode", @@ -1237,7 +1236,7 @@ dependencies = [ [[package]] name = "wasmer-engine-jit" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "bincode", "cfg-if 0.1.10", @@ -1253,7 +1252,7 @@ dependencies = [ [[package]] name = "wasmer-engine-native" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "bincode", "cfg-if 0.1.10", @@ -1272,9 +1271,9 @@ dependencies = [ [[package]] name = "wasmer-object" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ - "object 0.19.0", + "object 0.21.1", "thiserror", "wasmer-compiler", "wasmer-types", @@ -1307,7 +1306,7 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "cranelift-entity", "serde", @@ -1315,7 +1314,7 @@ dependencies = [ [[package]] name = "wasmer-vm" -version = "1.0.0-alpha4" +version = "1.0.0-alpha5" dependencies = [ "backtrace", "cc", @@ -1333,9 +1332,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.57.0" +version = "0.65.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6" +checksum = "87cc2fe6350834b4e528ba0901e7aa405d78b89dc1fa3145359eb4de0e323fcf" [[package]] name = "wast" From 47c958dc4399554a8500dc2c864e9e32cea98edc Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 20:31:26 -0800 Subject: [PATCH 10/14] Improved Native support --- examples/engine_cross_compilation.rs | 6 +++++- lib/engine-native/src/artifact.rs | 17 ++++++++++++++--- lib/object/src/module.rs | 6 ++++++ tests/compilers/traps.rs | 3 ++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/examples/engine_cross_compilation.rs b/examples/engine_cross_compilation.rs index 272da2cfcee..7396bf0f9d1 100644 --- a/examples/engine_cross_compilation.rs +++ b/examples/engine_cross_compilation.rs @@ -99,7 +99,11 @@ fn main() -> Result<(), Box> { } #[test] -#[cfg(not(windows))] +#[cfg(not(any( + windows, + // We don't support yet crosscompilation in macOS with Apple Silicon + all(target_os = "macos", target_arch = "aarch64") +)))] fn test_cross_compilation() -> Result<(), Box> { main() } diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index 80971347441..277e702a53e 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -254,15 +254,26 @@ impl NativeArtifact { }; let is_cross_compiling = engine_inner.is_cross_compiling(); + let target_triple_str = { + let into_str = target_triple.to_string(); + // We have to adapt the target triple string, because otherwise + // Apple's clang will not recognize it. + if into_str == "aarch64-apple-darwin" { + "arm64-apple-darwin".to_string() + } else { + into_str + } + }; + let cross_compiling_args: Vec = if is_cross_compiling { vec![ - format!("--target={}", target_triple), + format!("--target={}", target_triple_str), "-fuse-ld=lld".to_string(), "-nodefaultlibs".to_string(), "-nostdlib".to_string(), ] } else { - vec![] + vec![format!("--target={}", target_triple_str)] }; let target_args = match (target_triple.operating_system, is_cross_compiling) { (OperatingSystem::Windows, true) => vec!["-Wl,/force:unresolved,/noentry"], @@ -271,7 +282,7 @@ impl NativeArtifact { }; trace!( "Compiling for target {} from host {}", - target_triple.to_string(), + target_triple_str, Triple::host().to_string(), ); diff --git a/lib/object/src/module.rs b/lib/object/src/module.rs index fa7c3c53f21..5b1f84391b3 100644 --- a/lib/object/src/module.rs +++ b/lib/object/src/module.rs @@ -209,6 +209,12 @@ pub fn emit_compilation( RelocationKind::PltRelative, RelocationEncoding::X86Branch, ), + // Object doesn't fully support it yet + // Architecture::Aarch64(_) => ( + // 32, + // RelocationKind::PltRelative, + // RelocationEncoding::Generic, + // ), architecture => { return Err(ObjectError::UnsupportedArchitecture( architecture.to_string(), diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index e84d98a9aea..1b7674abe37 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -421,7 +421,8 @@ fn mismatched_arguments() -> Result<()> { any( feature = "test-singlepass", feature = "test-llvm", - feature = "test-native" + feature = "test-native", + all(target_os = "macos", target_arch = "aarch64") ), ignore )] From 99737abec4bf5a80c85e6e34c9e51b7d86e72a40 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 21:12:41 -0800 Subject: [PATCH 11/14] Build wasmer release for Apple silicon --- .github/workflows/main.yaml | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index ad894739228..4030abdc50c 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -55,6 +55,11 @@ jobs: artifact_name: 'wasmer-macos-amd64' cross_compilation_artifact_name: 'cross_compiled_from_mac' run_integration_tests: true + - build: macos + os: macos-latest + rust: nightly + target: aarch64-apple-darwin + artifact_name: 'wasmer-macos-arm64' - build: windows os: windows-latest rust: 1.46.0 @@ -83,7 +88,12 @@ jobs: uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust }} + target: ${{ matrix.target }} override: true + - name: Setup Rust target + run: + echo "\n[build]\ntarget = \"aarch64-apple-darwin\"" >> .cargo/config.toml + if: matrix.target != 'aarch64-apple-darwin' # we just build - name: Configure cargo data directory # After this point, all cargo registry and crate data is stored in # $GITHUB_WORKSPACE/.cargo_home. This allows us to cache only the files @@ -131,7 +141,7 @@ jobs: # echo "LLVM_SYS_100_PREFIX=C:/llvm-10" >> $GITHUB_ENV # echo "LIBCLANG_PATH=C:/llvm-10/bin/libclang.dll" >> $GITHUB_ENV - name: Install LLVM (Unix) - if: matrix.os != 'windows-latest' + if: matrix.os != 'windows-latest' && matrix.target != 'aarch64-apple-darwin' run: | curl --proto '=https' --tlsv1.2 -sSf ${{ matrix.llvm_url }} -L -o llvm.tar.xz mkdir -p ${{ env.LLVM_DIR }} @@ -141,12 +151,19 @@ jobs: env: LLVM_DIR: ${{ github.workspace }}/llvm-10 - name: Set up dependencies for Mac OS - run: brew install automake + run: | + brew install automake if: matrix.os == 'macos-latest' - - run: make test - - name: Build and Test C API + - name: Test + run: | + make test + if: matrix.target != 'aarch64-apple-darwin' + - name: Test C API run: | make test-capi + if: matrix.os != 'windows-latest' && matrix.target != 'aarch64-apple-darwin' # we can't test yet on Apple Silicon or Windows + - name: Build C API + run: | make build-capi if: matrix.os != 'windows-latest' - name: Build C API on Windows @@ -313,7 +330,7 @@ jobs: asset_path: artifacts/wasmer-linux-amd64/wasmer-linux-amd64.tar.gz asset_name: wasmer-linux-amd64.tar.gz asset_content_type: application/gzip - - name: Upload Release Asset Mac + - name: Upload Release Asset Mac amd64 id: upload-release-asset-mac uses: actions/upload-release-asset@v1 env: @@ -323,6 +340,16 @@ jobs: asset_path: artifacts/wasmer-macos-amd64/wasmer-darwin-amd64.tar.gz asset_name: wasmer-darwin-amd64.tar.gz asset_content_type: application/gzip + - name: Upload Release Asset Mac arm64 + id: upload-release-asset-mac + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: artifacts/wasmer-macos-amd64/wasmer-darwin-arm64.tar.gz + asset_name: wasmer-darwin-arm64.tar.gz + asset_content_type: application/gzip - name: Upload Release Asset Linux aarch64 id: upload-release-asset-linux-aarch64 uses: actions/upload-release-asset@v1 From a6492ed806e89cce27e4f35d891dbc74387fbe2b Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 21:30:19 -0800 Subject: [PATCH 12/14] Fixed Github Actions workflow --- .github/workflows/main.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 4030abdc50c..ec238cfb726 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -331,7 +331,7 @@ jobs: asset_name: wasmer-linux-amd64.tar.gz asset_content_type: application/gzip - name: Upload Release Asset Mac amd64 - id: upload-release-asset-mac + id: upload-release-asset-mac-amd64 uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -341,7 +341,7 @@ jobs: asset_name: wasmer-darwin-amd64.tar.gz asset_content_type: application/gzip - name: Upload Release Asset Mac arm64 - id: upload-release-asset-mac + id: upload-release-asset-mac-arm64 uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 95314cf6453244446522c555c7113aac01797e5a Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 21:38:30 -0800 Subject: [PATCH 13/14] Fixing Github Actions workflow --- .github/workflows/main.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index ec238cfb726..b8e80ff5f35 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -38,7 +38,7 @@ jobs: strategy: fail-fast: false matrix: - build: [linux, macos, windows, linux-aarch64] + build: [linux, macos, macos-arm64, windows, linux-aarch64] include: - build: linux os: ubuntu-18.04 @@ -55,7 +55,7 @@ jobs: artifact_name: 'wasmer-macos-amd64' cross_compilation_artifact_name: 'cross_compiled_from_mac' run_integration_tests: true - - build: macos + - build: macos-arm64 os: macos-latest rust: nightly target: aarch64-apple-darwin @@ -93,7 +93,7 @@ jobs: - name: Setup Rust target run: echo "\n[build]\ntarget = \"aarch64-apple-darwin\"" >> .cargo/config.toml - if: matrix.target != 'aarch64-apple-darwin' # we just build + if: matrix.target == 'aarch64-apple-darwin' # we just build - name: Configure cargo data directory # After this point, all cargo registry and crate data is stored in # $GITHUB_WORKSPACE/.cargo_home. This allows us to cache only the files From 8b956dcb8682ea694e0a4dfe03518e2699d6da21 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 19 Nov 2020 21:42:41 -0800 Subject: [PATCH 14/14] Fix new line in GA --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index b8e80ff5f35..2268bac2235 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -92,7 +92,7 @@ jobs: override: true - name: Setup Rust target run: - echo "\n[build]\ntarget = \"aarch64-apple-darwin\"" >> .cargo/config.toml + echo "[build]\\ntarget = \"aarch64-apple-darwin\"" >> .cargo/config.toml if: matrix.target == 'aarch64-apple-darwin' # we just build - name: Configure cargo data directory # After this point, all cargo registry and crate data is stored in