Merge remote-tracking branch 'origin/main' into pch/wasi_common_cap_std

This commit is contained in:
Pat Hickey
2021-02-01 13:21:25 -08:00
12 changed files with 178 additions and 59 deletions

View File

@@ -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);
//! }
//! }

View File

@@ -410,6 +410,17 @@ pub enum UnaryRmROpcode {
Popcnt,
}
impl UnaryRmROpcode {
pub(crate) fn available_from(&self) -> Option<InstructionSet> {
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.

View File

@@ -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()),
}
}

View File

@@ -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, .. }

View File

@@ -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

View File

@@ -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");
}
}
}

View File

@@ -59,6 +59,7 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntr
let bufused =
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
assert!(bufused <= BUF_LEN);
let sl = slice::from_raw_parts(buf.as_ptr(), bufused);
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
let eof = bufused < BUF_LEN;

View File

@@ -156,7 +156,7 @@ impl TryFrom<std::io::Error> 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),

View File

@@ -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<Func> for Extern {

View File

@@ -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

View File

@@ -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::<Vec<_>>();
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::<Trap>().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::<Vec<_>>();
// 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::<Trap>().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.

View File

@@ -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<Cell<bool>>);
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<Cell<bool>>);
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(())
}