Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hashbrown and use BTree{Map,Set} from the alloc crate #187

Merged
merged 2 commits into from Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ exclude = [ "/res/*", "/tests/*", "/fuzz/*", "/benches/*" ]
[dependencies]
wasmi-validation = { version = "0.1", path = "validation", default-features = false }
parity-wasm = { version = "0.31", default-features = false }
hashbrown = { version = "0.1.8", optional = true }
memory_units = "0.3.0"
libm = { version = "0.1.2", optional = true }

Expand All @@ -30,10 +29,8 @@ std = [
"wasmi-validation/std",
]
# Enable for no_std support
# hashbrown only works on no_std
core = [
"wasmi-validation/core",
"hashbrown/nightly",
"libm"
]

Expand Down
9 changes: 3 additions & 6 deletions src/imports.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#[allow(unused_imports)]
use alloc::prelude::v1::*;

#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
use alloc::collections::BTreeMap;

use func::FuncRef;
use global::GlobalRef;
Expand Down Expand Up @@ -106,7 +103,7 @@ pub trait ImportResolver {
/// [`ImportResolver`]: trait.ImportResolver.html
/// [`ModuleImportResolver`]: trait.ModuleImportResolver.html
pub struct ImportsBuilder<'a> {
modules: HashMap<String, &'a ModuleImportResolver>,
modules: BTreeMap<String, &'a ModuleImportResolver>,
}

impl<'a> Default for ImportsBuilder<'a> {
Expand All @@ -119,7 +116,7 @@ impl<'a> ImportsBuilder<'a> {
/// Create an empty `ImportsBuilder`.
pub fn new() -> ImportsBuilder<'a> {
ImportsBuilder {
modules: HashMap::new(),
modules: BTreeMap::new(),
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ extern crate assert_matches;
#[cfg(test)]
extern crate wabt;

#[cfg(not(feature = "std"))]
extern crate hashbrown;
extern crate memory_units as memory_units_crate;
extern crate parity_wasm;

Expand Down
9 changes: 3 additions & 6 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use core::cell::RefCell;
use core::fmt;
use Trap;

#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
use alloc::collections::BTreeMap;

use core::cell::Ref;
use func::{FuncBody, FuncInstance, FuncRef};
Expand Down Expand Up @@ -161,7 +158,7 @@ pub struct ModuleInstance {
funcs: RefCell<Vec<FuncRef>>,
memories: RefCell<Vec<MemoryRef>>,
globals: RefCell<Vec<GlobalRef>>,
exports: RefCell<HashMap<String, ExternVal>>,
exports: RefCell<BTreeMap<String, ExternVal>>,
}

impl ModuleInstance {
Expand All @@ -172,7 +169,7 @@ impl ModuleInstance {
tables: RefCell::new(Vec::new()),
memories: RefCell::new(Vec::new()),
globals: RefCell::new(Vec::new()),
exports: RefCell::new(HashMap::new()),
exports: RefCell::new(BTreeMap::new()),
}
}

Expand Down
5 changes: 1 addition & 4 deletions validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ description = "Wasm code validator"

[dependencies]
parity-wasm = { version = "0.31", default-features = false }
hashbrown = { version = "0.1.8", optional = true }

[dev-dependencies]
assert_matches = "1.1"

[features]
default = ["std"]
std = ["parity-wasm/std"]
core = [
"hashbrown/nightly"
]
core = []
29 changes: 16 additions & 13 deletions validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ use core::fmt;
#[cfg(feature = "std")]
use std::error;

#[cfg(not(feature = "std"))]
use hashbrown::HashSet;
#[cfg(feature = "std")]
use std::collections::HashSet;

use self::context::ModuleContextBuilder;
use parity_wasm::elements::{
BlockType, External, FuncBody, GlobalEntry, GlobalType, InitExpr, Instruction, Internal,
MemoryType, Module, ResizableLimits, TableType, Type, ValueType,
BlockType, ExportEntry, External, FuncBody, GlobalEntry, GlobalType, InitExpr, Instruction,
Internal, MemoryType, Module, ResizableLimits, TableType, Type, ValueType,
};

pub mod context;
Expand Down Expand Up @@ -250,13 +245,21 @@ pub fn validate_module<V: Validator>(module: &Module) -> Result<V::Output, Error

// validate export section
if let Some(export_section) = module.export_section() {
let mut export_names = HashSet::with_capacity(export_section.entries().len());
for export in export_section.entries() {
// HashSet::insert returns false if item already in set.
let duplicate = export_names.insert(export.field()) == false;
if duplicate {
return Err(Error(format!("duplicate export {}", export.field())));
let mut export_names = export_section
.entries()
.iter()
.map(ExportEntry::field)
.collect::<Vec<_>>();

export_names.sort_unstable();

for (fst, snd) in export_names.iter().zip(export_names.iter().skip(1)) {
if fst == snd {
return Err(Error(format!("duplicate export {}", fst)));
}
}

for export in export_section.entries() {
match *export.internal() {
Internal::Function(function_index) => {
context.require_function(function_index)?;
Expand Down