diff --git a/cranelift/codegen/src/isa/mod.rs b/cranelift/codegen/src/isa/mod.rs index bfc4e0d0d0..94895b0b6e 100644 --- a/cranelift/codegen/src/isa/mod.rs +++ b/cranelift/codegen/src/isa/mod.rs @@ -20,7 +20,6 @@ //! appropriate for the requested ISA: //! //! ``` -//! # extern crate cranelift_codegen; //! # #[macro_use] extern crate target_lexicon; //! use cranelift_codegen::isa; //! use cranelift_codegen::settings::{self, Configurable}; @@ -30,12 +29,12 @@ //! let shared_builder = settings::builder(); //! let shared_flags = settings::Flags::new(shared_builder); //! -//! match isa::lookup(triple!("riscv32")) { +//! match isa::lookup(triple!("x86_64")) { //! Err(_) => { -//! // The RISC-V target ISA is not available. +//! // The x86_64 target ISA is not available. //! } //! Ok(mut isa_builder) => { -//! isa_builder.set("supports_m", "on"); +//! isa_builder.set("use_popcnt", "on"); //! let isa = isa_builder.finish(shared_flags); //! } //! } diff --git a/cranelift/codegen/src/isa/x64/inst/args.rs b/cranelift/codegen/src/isa/x64/inst/args.rs index 0e99ff94fa..00f581218c 100644 --- a/cranelift/codegen/src/isa/x64/inst/args.rs +++ b/cranelift/codegen/src/isa/x64/inst/args.rs @@ -410,6 +410,17 @@ pub enum UnaryRmROpcode { Popcnt, } +impl UnaryRmROpcode { + pub(crate) fn available_from(&self) -> Option { + match self { + UnaryRmROpcode::Bsr | UnaryRmROpcode::Bsf => None, + UnaryRmROpcode::Lzcnt => Some(InstructionSet::Lzcnt), + UnaryRmROpcode::Tzcnt => Some(InstructionSet::BMI1), + UnaryRmROpcode::Popcnt => Some(InstructionSet::Popcnt), + } + } +} + impl fmt::Debug for UnaryRmROpcode { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self { @@ -442,6 +453,10 @@ pub(crate) enum InstructionSet { SSSE3, SSE41, SSE42, + Popcnt, + Lzcnt, + BMI1, + BMI2, } /// Some SSE operations requiring 2 operands r/m and r. diff --git a/cranelift/codegen/src/isa/x64/inst/emit.rs b/cranelift/codegen/src/isa/x64/inst/emit.rs index 4d6ae596eb..bce655a4ab 100644 --- a/cranelift/codegen/src/isa/x64/inst/emit.rs +++ b/cranelift/codegen/src/isa/x64/inst/emit.rs @@ -529,9 +529,13 @@ pub(crate) fn emit( match iset_requirement { // Cranelift assumes SSE2 at least. InstructionSet::SSE | InstructionSet::SSE2 => {} - InstructionSet::SSSE3 => assert!(info.isa_flags.has_ssse3()), - InstructionSet::SSE41 => assert!(info.isa_flags.has_sse41()), - InstructionSet::SSE42 => assert!(info.isa_flags.has_sse42()), + InstructionSet::SSSE3 => assert!(info.isa_flags.use_ssse3()), + InstructionSet::SSE41 => assert!(info.isa_flags.use_sse41()), + InstructionSet::SSE42 => assert!(info.isa_flags.use_sse42()), + InstructionSet::Popcnt => assert!(info.isa_flags.use_popcnt()), + InstructionSet::Lzcnt => assert!(info.isa_flags.use_lzcnt()), + InstructionSet::BMI1 => assert!(info.isa_flags.use_bmi1()), + InstructionSet::BMI2 => assert!(info.isa_flags.has_bmi2()), } } diff --git a/cranelift/codegen/src/isa/x64/inst/mod.rs b/cranelift/codegen/src/isa/x64/inst/mod.rs index e6a6ec9486..40cbdc92bd 100644 --- a/cranelift/codegen/src/isa/x64/inst/mod.rs +++ b/cranelift/codegen/src/isa/x64/inst/mod.rs @@ -539,7 +539,6 @@ impl Inst { | Inst::SignExtendData { .. } | Inst::TrapIf { .. } | Inst::Ud2 { .. } - | Inst::UnaryRmR { .. } | Inst::VirtualSPOffsetAdj { .. } | Inst::XmmCmove { .. } | Inst::XmmCmpRmR { .. } @@ -550,6 +549,8 @@ impl Inst { | Inst::MachOTlsGetAddr { .. } | Inst::ValueLabelMarker { .. } => None, + Inst::UnaryRmR { op, .. } => op.available_from(), + // These use dynamic SSE opcodes. Inst::GprToXmm { op, .. } | Inst::XmmMovRM { op, .. } diff --git a/cranelift/frontend/src/frontend.rs b/cranelift/frontend/src/frontend.rs index 07d8d7819e..365e5aeffb 100644 --- a/cranelift/frontend/src/frontend.rs +++ b/cranelift/frontend/src/frontend.rs @@ -975,12 +975,12 @@ mod tests { let shared_flags = settings::Flags::new(shared_builder); let triple = - ::target_lexicon::Triple::from_str("riscv32").expect("Couldn't create riscv32 triple"); + ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple"); let target = isa::lookup(triple) .ok() .map(|b| b.finish(shared_flags)) - .expect("This test requires riscv32 support."); + .expect("This test requires x86_64 support."); let mut sig = Signature::new(target.default_call_conv()); sig.returns.push(AbiParam::new(I32)); @@ -1013,13 +1013,13 @@ mod tests { assert_eq!( func.display(None).to_string(), "function %sample() -> i32 system_v { - sig0 = (i32, i32, i32) system_v + sig0 = (i64, i64, i64) system_v fn0 = %Memcpy sig0 block0: - v3 = iconst.i32 0 + v3 = iconst.i64 0 v1 -> v3 - v2 = iconst.i32 0 + v2 = iconst.i64 0 v0 -> v2 call fn0(v1, v0, v1) return v1 @@ -1037,12 +1037,12 @@ block0: let shared_flags = settings::Flags::new(shared_builder); let triple = - ::target_lexicon::Triple::from_str("riscv32").expect("Couldn't create riscv32 triple"); + ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple"); let target = isa::lookup(triple) .ok() .map(|b| b.finish(shared_flags)) - .expect("This test requires riscv32 support."); + .expect("This test requires x86_64 support."); let mut sig = Signature::new(target.default_call_conv()); sig.returns.push(AbiParam::new(I32)); @@ -1074,9 +1074,9 @@ block0: func.display(None).to_string(), "function %sample() -> i32 system_v { block0: - v4 = iconst.i32 0 + v4 = iconst.i64 0 v1 -> v4 - v3 = iconst.i32 0 + v3 = iconst.i64 0 v0 -> v3 v2 = load.i64 aligned v0 store aligned v2, v1 @@ -1095,12 +1095,12 @@ block0: let shared_flags = settings::Flags::new(shared_builder); let triple = - ::target_lexicon::Triple::from_str("riscv32").expect("Couldn't create riscv32 triple"); + ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple"); let target = isa::lookup(triple) .ok() .map(|b| b.finish(shared_flags)) - .expect("This test requires riscv32 support."); + .expect("This test requires x86_64 support."); let mut sig = Signature::new(target.default_call_conv()); sig.returns.push(AbiParam::new(I32)); @@ -1131,15 +1131,15 @@ block0: assert_eq!( func.display(None).to_string(), "function %sample() -> i32 system_v { - sig0 = (i32, i32, i32) system_v + sig0 = (i64, i64, i64) system_v fn0 = %Memcpy sig0 block0: - v4 = iconst.i32 0 + v4 = iconst.i64 0 v1 -> v4 - v3 = iconst.i32 0 + v3 = iconst.i64 0 v0 -> v3 - v2 = iconst.i32 8192 + v2 = iconst.i64 8192 call fn0(v1, v0, v2) return v1 } @@ -1156,12 +1156,12 @@ block0: let shared_flags = settings::Flags::new(shared_builder); let triple = - ::target_lexicon::Triple::from_str("riscv32").expect("Couldn't create riscv32 triple"); + ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple"); let target = isa::lookup(triple) .ok() .map(|b| b.finish(shared_flags)) - .expect("This test requires riscv32 support."); + .expect("This test requires x86_64 support."); let mut sig = Signature::new(target.default_call_conv()); sig.returns.push(AbiParam::new(I32)); @@ -1190,7 +1190,7 @@ block0: func.display(None).to_string(), "function %sample() -> i32 system_v { block0: - v2 = iconst.i32 0 + v2 = iconst.i64 0 v0 -> v2 v1 = iconst.i64 0x0001_0001_0101 store aligned v1, v0 @@ -1209,12 +1209,12 @@ block0: let shared_flags = settings::Flags::new(shared_builder); let triple = - ::target_lexicon::Triple::from_str("riscv32").expect("Couldn't create riscv32 triple"); + ::target_lexicon::Triple::from_str("x86_64").expect("Couldn't create x86_64 triple"); let target = isa::lookup(triple) .ok() .map(|b| b.finish(shared_flags)) - .expect("This test requires riscv32 support."); + .expect("This test requires x86_64 support."); let mut sig = Signature::new(target.default_call_conv()); sig.returns.push(AbiParam::new(I32)); @@ -1242,14 +1242,14 @@ block0: assert_eq!( func.display(None).to_string(), "function %sample() -> i32 system_v { - sig0 = (i32, i32, i32) system_v + sig0 = (i64, i32, i64) system_v fn0 = %Memset sig0 block0: - v4 = iconst.i32 0 + v4 = iconst.i64 0 v0 -> v4 v1 = iconst.i8 1 - v2 = iconst.i32 8192 + v2 = iconst.i64 8192 v3 = uextend.i32 v1 call fn0(v0, v3, v2) return v0 diff --git a/cranelift/reader/src/parser.rs b/cranelift/reader/src/parser.rs index 33b18da6b1..93036b3419 100644 --- a/cranelift/reader/src/parser.rs +++ b/cranelift/reader/src/parser.rs @@ -3678,7 +3678,6 @@ mod tests { } #[test] - #[cfg(feature = "riscv")] fn isa_spec() { assert!(parse_test( "target @@ -3688,7 +3687,7 @@ mod tests { .is_err()); assert!(parse_test( - "target riscv32 + "target x86_64 set enable_float=false function %foo() system_v {}", ParseOptions::default() @@ -3697,7 +3696,7 @@ mod tests { match parse_test( "set enable_float=false - isa riscv + target x86_64 function %foo() system_v {}", ParseOptions::default(), ) @@ -3707,7 +3706,10 @@ mod tests { IsaSpec::None(_) => panic!("Expected some ISA"), IsaSpec::Some(v) => { assert_eq!(v.len(), 1); - assert_eq!(v[0].name(), "riscv"); + #[cfg(not(feature = "experimental_x64"))] + assert_eq!(v[0].name(), "x86"); + #[cfg(feature = "experimental_x64")] + assert_eq!(v[0].name(), "x64"); } } } diff --git a/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs b/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs index f3594f939c..c22a742b3d 100644 --- a/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs +++ b/crates/test-programs/wasi-tests/src/bin/fd_readdir.rs @@ -59,6 +59,7 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec = ReadDir::from_slice(sl).collect(); let eof = bufused < BUF_LEN; diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs index dc8a6f5311..2d5c67c9b5 100644 --- a/crates/wasi-common/src/snapshots/preview_1.rs +++ b/crates/wasi-common/src/snapshots/preview_1.rs @@ -156,7 +156,7 @@ impl TryFrom for types::Errno { winerror::ERROR_TOO_MANY_OPEN_FILES => Some(types::Errno::Nfile), winerror::ERROR_ACCESS_DENIED => Some(types::Errno::Acces), winerror::ERROR_SHARING_VIOLATION => Some(types::Errno::Acces), - winerror::ERROR_PRIVILEGE_NOT_HELD => Some(types::Errno::Notcapable), + winerror::ERROR_PRIVILEGE_NOT_HELD => Some(types::Errno::Perm), winerror::ERROR_INVALID_HANDLE => Some(types::Errno::Badf), winerror::ERROR_INVALID_NAME => Some(types::Errno::Noent), winerror::ERROR_NOT_ENOUGH_MEMORY => Some(types::Errno::Nomem), diff --git a/crates/wasmtime/src/externals.rs b/crates/wasmtime/src/externals.rs index 00bcaa499b..e71fb7d75c 100644 --- a/crates/wasmtime/src/externals.rs +++ b/crates/wasmtime/src/externals.rs @@ -160,6 +160,17 @@ impl Extern { Extern::Module(_) => "module", } } + + pub(crate) fn wasmtime_export(&self) -> wasmtime_runtime::Export { + match self { + Extern::Func(f) => f.wasmtime_export().clone().into(), + Extern::Global(f) => f.wasmtime_export().clone().into(), + Extern::Table(f) => f.wasmtime_export().clone().into(), + Extern::Memory(f) => f.wasmtime_export().clone().into(), + Extern::Instance(f) => wasmtime_runtime::Export::Instance(f.wasmtime_export().clone()), + Extern::Module(f) => wasmtime_runtime::Export::Module(Box::new(f.clone())), + } + } } impl From for Extern { diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 47115d90ae..8bc6085c11 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -121,6 +121,10 @@ impl Instance { ty } + pub(crate) fn wasmtime_export(&self) -> &RuntimeInstance { + &self.items + } + /// Returns the associated [`Store`] that this `Instance` is compiled into. /// /// This is the [`Store`] that generally serves as a sort of global cache diff --git a/crates/wasmtime/src/linker.rs b/crates/wasmtime/src/linker.rs index 396327b5d5..5875cf8891 100644 --- a/crates/wasmtime/src/linker.rs +++ b/crates/wasmtime/src/linker.rs @@ -402,34 +402,53 @@ impl Linker { fn command(&mut self, module_name: &str, module: &Module) -> Result<&mut Self> { for export in module.exports() { if let Some(func_ty) = export.ty().func() { - let imports = self.compute_imports(module)?; - let store = self.store.clone(); + let imports = self + .compute_imports(module)? + .into_iter() + .map(|e| e.wasmtime_export()) + .collect::>(); let module = module.clone(); let export_name = export.name().to_owned(); - let func = Func::new(&self.store, func_ty.clone(), move |_, params, results| { - // Create a new instance for this command execution. - let instance = Instance::new(&store, &module, &imports)?; + let func = Func::new( + &self.store, + func_ty.clone(), + move |caller, params, results| { + let store = caller.store(); - // `unwrap()` everything here because we know the instance contains a - // function export with the given name and signature because we're - // iterating over the module it was instantiated from. - let command_results = instance - .get_export(&export_name) - .unwrap() - .into_func() - .unwrap() - .call(params) - .map_err(|error| error.downcast::().unwrap())?; + // Note that the unsafety here is due to the validity of + // `i` and the validity of `i` within `store`. For our + // case though these items all come from `imports` above + // so they're all valid. They're also all kept alive by + // the store itself used here so this should be safe. + let imports = imports + .iter() + .map(|i| unsafe { Extern::from_wasmtime_export(&i, &store) }) + .collect::>(); - // Copy the return values into the output slice. - for (result, command_result) in - results.iter_mut().zip(command_results.into_vec()) - { - *result = command_result; - } + // Create a new instance for this command execution. + let instance = Instance::new(&store, &module, &imports)?; - Ok(()) - }); + // `unwrap()` everything here because we know the instance contains a + // function export with the given name and signature because we're + // iterating over the module it was instantiated from. + let command_results = instance + .get_export(&export_name) + .unwrap() + .into_func() + .unwrap() + .call(params) + .map_err(|error| error.downcast::().unwrap())?; + + // Copy the return values into the output slice. + for (result, command_result) in + results.iter_mut().zip(command_results.into_vec()) + { + *result = command_result; + } + + Ok(()) + }, + ); self.insert(module_name, export.name(), func.into())?; } else if export.name() == "memory" && export.ty().memory().is_some() { // Allow an exported "memory" memory for now. diff --git a/tests/all/linker.rs b/tests/all/linker.rs index 675c109cf1..5312af31f5 100644 --- a/tests/all/linker.rs +++ b/tests/all/linker.rs @@ -1,4 +1,6 @@ use anyhow::Result; +use std::cell::Cell; +use std::rc::Rc; use wasmtime::*; #[test] @@ -160,3 +162,64 @@ fn module_interposition() -> Result<()> { assert_eq!(func()?, 112); Ok(()) } + +#[test] +fn no_leak() -> Result<()> { + struct DropMe(Rc>); + + impl Drop for DropMe { + fn drop(&mut self) { + self.0.set(true); + } + } + + let flag = Rc::new(Cell::new(false)); + { + let store = Store::default(); + let mut linker = Linker::new(&store); + let drop_me = DropMe(flag.clone()); + linker.func("", "", move || drop(&drop_me))?; + let module = Module::new( + store.engine(), + r#" + (module + (func (export "_start")) + ) + "#, + )?; + linker.module("a", &module)?; + } + assert!(flag.get(), "store was leaked"); + Ok(()) +} + +#[test] +fn no_leak_with_imports() -> Result<()> { + struct DropMe(Rc>); + + impl Drop for DropMe { + fn drop(&mut self) { + self.0.set(true); + } + } + + let flag = Rc::new(Cell::new(false)); + { + let store = Store::default(); + let mut linker = Linker::new(&store); + let drop_me = DropMe(flag.clone()); + linker.func("", "", move || drop(&drop_me))?; + let module = Module::new( + store.engine(), + r#" + (module + (import "" "" (func)) + (func (export "_start")) + ) + "#, + )?; + linker.module("a", &module)?; + } + assert!(flag.get(), "store was leaked"); + Ok(()) +}