-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
#![no_std]
support to the wasmparser
crate
This commit adds a `std` feature to the `wasmparser` crate, enables it by default, and then enables building the crate when the feature is disabled. This works by switching imports of `wasmparser` from `std` to either the `core` or `alloc` crates which are available in the `no_std` context. The main change made in this PR is that maps used by `wasmparser`, e.g. `HashMap` and `IndexMap`, are now abstracted with type aliases in `wasmparser` which unconditionally customize a hash algorithm parameter. When the `std` feature is enabled this parameter is implemented using the standard library's default hashing algorithm. When `std` is disabled then this uses the `ahash` crate internally. This commit additionally updates CI to both check that wasmparser builds when the `std` feature is disabled and that it builds on a target without `std`.
- Loading branch information
1 parent
7003a7f
commit fe2637f
Showing
38 changed files
with
307 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
//! Type aliases for maps used by `wasmparser` | ||
//! | ||
//! This module contains type aliases used for [`HashMap`], [`HashSet`], | ||
//! [`IndexMap`], and [`IndexSet`]. Note that these differ from upstream types | ||
//! in the `indexmap` crate and the standard library due to customization of the | ||
//! hash algorithm type parameter. | ||
use core::hash::{BuildHasher, Hasher}; | ||
|
||
/// Wasmparser-specific type alias for an ordered map. | ||
pub type IndexMap<K, V> = indexmap::IndexMap<K, V, RandomState>; | ||
|
||
/// Wasmparser-specific type alias for an ordered set. | ||
pub type IndexSet<K> = indexmap::IndexSet<K, RandomState>; | ||
|
||
/// Wasmparser-specific type alias for hash map. | ||
pub type HashMap<K, V> = hashbrown::HashMap<K, V, RandomState>; | ||
|
||
/// Wasmparser-specific type alias for hash set. | ||
pub type HashSet<K> = hashbrown::HashSet<K, RandomState>; | ||
|
||
/// Wasmparser's hashing state stored per-map. | ||
/// | ||
/// This is DoS-resistant when the `std` feature is activated and still somewhat | ||
/// resistant when it's not active but not as secure. | ||
#[derive(Clone, Debug)] | ||
pub struct RandomState(RandomStateImpl); | ||
|
||
impl Default for RandomState { | ||
#[inline] | ||
fn default() -> RandomState { | ||
RandomState(RandomStateImpl::default()) | ||
} | ||
} | ||
|
||
impl BuildHasher for RandomState { | ||
type Hasher = RandomStateHasher; | ||
|
||
#[inline] | ||
fn build_hasher(&self) -> RandomStateHasher { | ||
RandomStateHasher(self.0.build_hasher()) | ||
} | ||
} | ||
|
||
/// Wasmparser's hasher type used with [`RandomState`]. | ||
pub struct RandomStateHasher(<RandomStateImpl as BuildHasher>::Hasher); | ||
|
||
impl Hasher for RandomStateHasher { | ||
#[inline] | ||
fn finish(&self) -> u64 { | ||
self.0.finish() | ||
} | ||
#[inline] | ||
fn write(&mut self, bytes: &[u8]) { | ||
self.0.write(bytes) | ||
} | ||
#[inline] | ||
fn write_u8(&mut self, i: u8) { | ||
self.0.write_u8(i) | ||
} | ||
#[inline] | ||
fn write_u16(&mut self, i: u16) { | ||
self.0.write_u16(i) | ||
} | ||
#[inline] | ||
fn write_u32(&mut self, i: u32) { | ||
self.0.write_u32(i) | ||
} | ||
#[inline] | ||
fn write_u64(&mut self, i: u64) { | ||
self.0.write_u64(i) | ||
} | ||
#[inline] | ||
fn write_u128(&mut self, i: u128) { | ||
self.0.write_u128(i) | ||
} | ||
#[inline] | ||
fn write_usize(&mut self, i: usize) { | ||
self.0.write_usize(i) | ||
} | ||
#[inline] | ||
fn write_i8(&mut self, i: i8) { | ||
self.0.write_i8(i) | ||
} | ||
#[inline] | ||
fn write_i16(&mut self, i: i16) { | ||
self.0.write_i16(i) | ||
} | ||
#[inline] | ||
fn write_i32(&mut self, i: i32) { | ||
self.0.write_i32(i) | ||
} | ||
#[inline] | ||
fn write_i64(&mut self, i: i64) { | ||
self.0.write_i64(i) | ||
} | ||
#[inline] | ||
fn write_i128(&mut self, i: i128) { | ||
self.0.write_i128(i) | ||
} | ||
#[inline] | ||
fn write_isize(&mut self, i: isize) { | ||
self.0.write_isize(i) | ||
} | ||
} | ||
|
||
// When the `std` feature is active reuse the standard library's implementation | ||
// of hash state and hasher. | ||
#[cfg(feature = "std")] | ||
use std::collections::hash_map::RandomState as RandomStateImpl; | ||
|
||
// When the `std` feature is NOT active then rely on `ahash::RandomState`. That | ||
// relies on ASLR by default for randomness. | ||
#[derive(Default, Clone, Debug)] | ||
#[cfg(not(feature = "std"))] | ||
struct RandomStateImpl; | ||
|
||
#[cfg(not(feature = "std"))] | ||
impl BuildHasher for RandomStateImpl { | ||
type Hasher = ahash::AHasher; | ||
|
||
#[inline] | ||
fn build_hasher(&self) -> ahash::AHasher { | ||
ahash::RandomState::new().build_hasher() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.