Make more code work with no_std. (#407)

* Make more code work with no_std.

no_std support is still incomplete, but this patch takes care of the
bulk of the straightforward parts.
This commit is contained in:
Dan Gohman
2019-10-08 16:53:32 -07:00
committed by GitHub
parent c0b37bb713
commit 8e1b44b29c
100 changed files with 380 additions and 270 deletions

View File

@@ -1,8 +1,8 @@
use crate::instance::InstanceHandle;
use crate::vmcontext::{VMFunctionImport, VMGlobalImport, VMMemoryImport, VMTableImport};
use crate::HashSet;
use cranelift_entity::{BoxedSlice, PrimaryMap};
use cranelift_wasm::{FuncIndex, GlobalIndex, MemoryIndex, TableIndex};
use std::collections::HashSet;
/// Resolved import pointers.
#[derive(Clone)]

View File

@@ -15,9 +15,15 @@ use crate::vmcontext::{
VMGlobalDefinition, VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex,
VMTableDefinition, VMTableImport,
};
use crate::{HashMap, HashSet};
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use core::any::Any;
use core::borrow::Borrow;
use core::cell::RefCell;
use core::convert::TryFrom;
use core::slice;
use core::{mem, ptr};
use cranelift_entity::EntityRef;
@@ -27,12 +33,6 @@ use cranelift_wasm::{
GlobalIndex, GlobalInit, MemoryIndex, SignatureIndex, TableIndex,
};
use indexmap;
use std::borrow::ToOwned;
use std::boxed::Box;
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::rc::Rc;
use std::string::{String, ToString};
use wasmtime_environ::{DataInitializer, Module, TableElements, VMOffsets};
fn signature_id(
@@ -613,9 +613,9 @@ impl Instance {
}
pub(crate) fn lookup_global_export(&self, field: &str) -> Option<Export> {
let cell: &RefCell<HashMap<std::string::String, core::option::Option<Export>>> =
let cell: &RefCell<HashMap<alloc::string::String, core::option::Option<Export>>> =
self.global_exports.borrow();
let map: &mut HashMap<std::string::String, core::option::Option<Export>> =
let map: &mut HashMap<alloc::string::String, core::option::Option<Export>> =
&mut cell.borrow_mut();
if let Some(Some(export)) = map.get(field) {
return Some(export.clone());
@@ -790,10 +790,10 @@ impl InstanceHandle {
// Collect the exports for the global export map.
for (field, decl) in &instance.module.exports {
use std::collections::hash_map::Entry::*;
let cell: &RefCell<HashMap<std::string::String, core::option::Option<Export>>> =
use crate::hash_map::Entry::*;
let cell: &RefCell<HashMap<alloc::string::String, core::option::Option<Export>>> =
instance.global_exports.borrow();
let map: &mut HashMap<std::string::String, core::option::Option<Export>> =
let map: &mut HashMap<alloc::string::String, core::option::Option<Export>> =
&mut cell.borrow_mut();
match map.entry(field.to_string()) {
Vacant(entry) => {

View File

@@ -2,8 +2,9 @@
//! the __jit_debug_register_code() and __jit_debug_descriptor to register
//! or unregister generated object images with debuggers.
use std::boxed::Box;
use std::ptr;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::ptr;
#[repr(C)]
struct JITCodeEntry {
@@ -40,7 +41,7 @@ extern "C" fn __jit_debug_register_code() {
// Hack to not allow inlining even when Rust wants to do it in release mode.
let x = 3;
unsafe {
std::ptr::read_volatile(&x);
core::ptr::read_volatile(&x);
}
}

View File

@@ -21,6 +21,7 @@
clippy::use_self
)
)]
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use]
extern crate lazy_static;
@@ -28,6 +29,7 @@ extern crate lazy_static;
extern crate memoffset;
#[macro_use]
extern crate failure_derive;
extern crate alloc;
mod export;
mod imports;
@@ -59,5 +61,15 @@ pub use crate::vmcontext::{
VMTableDefinition, VMTableImport,
};
#[cfg(not(feature = "std"))]
use hashbrown::{hash_map, HashMap, HashSet};
#[cfg(feature = "std")]
use std::collections::{hash_map, HashMap, HashSet};
#[cfg(not(feature = "std"))]
use spin::{RwLock, RwLockReadGuard, RwLockWriteGuard};
#[cfg(feature = "std")]
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@@ -4,8 +4,8 @@
use crate::mmap::Mmap;
use crate::vmcontext::VMMemoryDefinition;
use std::convert::TryFrom;
use std::string::String;
use alloc::string::String;
use core::convert::TryFrom;
use wasmtime_environ::{MemoryPlan, MemoryStyle, WASM_MAX_PAGES, WASM_PAGE_SIZE};
/// A linear memory instance.

View File

@@ -1,14 +1,14 @@
//! Low-level abstraction for allocating and managing zero-filled pages
//! of memory.
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::ptr;
use core::slice;
use errno;
#[cfg(not(target_os = "windows"))]
use libc;
use region;
use std::string::{String, ToString};
use std::vec::Vec;
/// Round `size` up to the nearest multiple of `page_size`.
fn round_up_to_page_size(size: usize, page_size: usize) -> usize {

View File

@@ -2,9 +2,9 @@
//! signature checking.
use crate::vmcontext::VMSharedSignatureIndex;
use crate::{hash_map, HashMap};
use core::convert::TryFrom;
use cranelift_codegen::ir;
use std::collections::{hash_map, HashMap};
use std::convert::TryFrom;
/// WebAssembly requires that the caller and callee signatures in an indirect
/// call must match. To implement this efficiently, keep a registry of all
@@ -29,11 +29,11 @@ impl SignatureRegistry {
match self.signature_hash.entry(sig.clone()) {
hash_map::Entry::Occupied(entry) => *entry.get(),
hash_map::Entry::Vacant(entry) => {
// Keep `signature_hash` len under 2**32 -- VMSharedSignatureIndex::new(std::u32::MAX)
// Keep `signature_hash` len under 2**32 -- VMSharedSignatureIndex::new(core::u32::MAX)
// is reserved for VMSharedSignatureIndex::default().
debug_assert!(
len < std::u32::MAX as usize,
"Invariant check: signature_hash.len() < std::u32::MAX"
len < core::u32::MAX as usize,
"Invariant check: signature_hash.len() < core::u32::MAX"
);
let sig_id = VMSharedSignatureIndex::new(u32::try_from(len).unwrap());
entry.insert(sig_id);

View File

@@ -5,9 +5,9 @@
#![allow(non_snake_case)]
use crate::vmcontext::VMContext;
use crate::RwLock;
use core::borrow::{Borrow, BorrowMut};
use core::cell::Cell;
use std::sync::RwLock;
#[derive(Default)]
struct TrapContext {

View File

@@ -3,9 +3,9 @@
//! `Table` is to WebAssembly tables what `LinearMemory` is to WebAssembly linear memories.
use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition};
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};
use cranelift_wasm::TableElementType;
use std::convert::{TryFrom, TryInto};
use std::vec::Vec;
use wasmtime_environ::{TablePlan, TableStyle};
/// A table instance.
@@ -25,7 +25,7 @@ impl Table {
}
};
assert!(
plan.table.minimum <= std::u32::MAX,
plan.table.minimum <= core::u32::MAX,
"Invariant check: vec.len() <= u32::MAX"
);
match plan.style {
@@ -60,7 +60,7 @@ impl Table {
}
};
assert!(
new_len <= std::u32::MAX,
new_len <= core::u32::MAX,
"Invariant check: vec.len() <= u32::MAX"
);
self.vec.resize(

View File

@@ -1,7 +1,7 @@
use crate::HashMap;
use crate::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use cranelift_codegen::ir;
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
lazy_static! {
static ref REGISTRY: RwLock<TrapRegistry> = RwLock::new(TrapRegistry::default());

View File

@@ -4,10 +4,10 @@
use crate::trap_registry::get_trap_registry;
use crate::trap_registry::TrapDescription;
use crate::vmcontext::{VMContext, VMFunctionBody};
use alloc::string::{String, ToString};
use core::cell::Cell;
use core::ptr;
use cranelift_codegen::ir;
use std::string::String;
extern "C" {
fn WasmtimeCallTrampoline(