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

Replace wasmparser-nostd fork with upstream wasmparser #1141

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
222 changes: 121 additions & 101 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions crates/wasmi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exclude = [
]

[dependencies]
wasmparser = { version = "0.100.2", package = "wasmparser-nostd", default-features = false }
wasmparser = { version = "0.219.0", default-features = false, features = ["validate", "features"] }
wasmi_core = { workspace = true }
wasmi_collections = { workspace = true }
wasmi_ir = { workspace = true }
Expand Down Expand Up @@ -55,7 +55,10 @@ std = [
# malious actors that control their inputs.
#
# An example of such an environment is `wasm32-unknown-unknown`.
no-hash-maps = ["wasmi_collections/no-hash-maps"]
no-hash-maps = [
"wasmi_collections/no-hash-maps",
"wasmparser/no-hash-maps",
]

# Enables extra checks performed during Wasmi bytecode execution.
#
Expand Down
92 changes: 33 additions & 59 deletions crates/wasmi/src/engine/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,8 @@
stack_limits: StackLimits,
/// The amount of Wasm stacks to keep in cache at most.
cached_stacks: usize,
/// Is `true` if the `mutable-global` Wasm proposal is enabled.
mutable_global: bool,
/// Is `true` if the `sign-extension` Wasm proposal is enabled.
sign_extension: bool,
/// Is `true` if the `saturating-float-to-int` Wasm proposal is enabled.
saturating_float_to_int: bool,
/// Is `true` if the [`multi-value`] Wasm proposal is enabled.
multi_value: bool,
/// Is `true` if the [`multi-memory`] Wasm proposal is enabled.
multi_memory: bool,
/// Is `true` if the [`bulk-memory`] Wasm proposal is enabled.
bulk_memory: bool,
/// Is `true` if the [`reference-types`] Wasm proposal is enabled.
reference_types: bool,
/// Is `true` if the [`tail-call`] Wasm proposal is enabled.
tail_call: bool,
/// Is `true` if the [`extended-const`] Wasm proposal is enabled.
extended_const: bool,
/// Is `true` if Wasm instructions on `f32` and `f64` types are allowed.
floats: bool,
/// The Wasm features used when validating or translating functions.
features: WasmFeatures,
/// Is `true` if Wasmi executions shall consume fuel.
consume_fuel: bool,
/// Is `true` if Wasmi shall ignore Wasm custom sections when parsing Wasm modules.
Expand Down Expand Up @@ -174,16 +156,7 @@
Self {
stack_limits: StackLimits::default(),
cached_stacks: DEFAULT_CACHED_STACKS,
mutable_global: true,
sign_extension: true,
saturating_float_to_int: true,
multi_value: true,
multi_memory: true,
bulk_memory: true,
reference_types: true,
tail_call: true,
extended_const: true,
floats: true,
features: Self::default_features(),
consume_fuel: false,
ignore_custom_sections: false,
fuel_costs: FuelCosts::default(),
Expand All @@ -194,6 +167,23 @@
}

impl Config {
/// Returns the default [`WasmFeatures`].
fn default_features() -> WasmFeatures {
let mut features = WasmFeatures::empty();
features.set(WasmFeatures::MUTABLE_GLOBAL, true);
features.set(WasmFeatures::MULTI_VALUE, true);
features.set(WasmFeatures::MULTI_MEMORY, true);
features.set(WasmFeatures::SATURATING_FLOAT_TO_INT, true);
features.set(WasmFeatures::SIGN_EXTENSION, true);
features.set(WasmFeatures::BULK_MEMORY, true);
features.set(WasmFeatures::REFERENCE_TYPES, true);
features.set(WasmFeatures::GC_TYPES, true); // required by reference-types
features.set(WasmFeatures::TAIL_CALL, true);
features.set(WasmFeatures::EXTENDED_CONST, true);
features.set(WasmFeatures::FLOATS, true);
features
}

/// Sets the [`StackLimits`] for the [`Config`].
pub fn set_stack_limits(&mut self, stack_limits: StackLimits) -> &mut Self {
self.stack_limits = stack_limits;
Expand Down Expand Up @@ -228,7 +218,7 @@
///
/// [`mutable-global`]: https://github.com/WebAssembly/mutable-global
pub fn wasm_mutable_global(&mut self, enable: bool) -> &mut Self {
self.mutable_global = enable;
self.features.set(WasmFeatures::MUTABLE_GLOBAL, enable);
self
}

Expand All @@ -240,7 +230,7 @@
///
/// [`sign-extension`]: https://github.com/WebAssembly/sign-extension-ops
pub fn wasm_sign_extension(&mut self, enable: bool) -> &mut Self {
self.sign_extension = enable;
self.features.set(WasmFeatures::SIGN_EXTENSION, enable);
self
}

Expand All @@ -253,7 +243,8 @@
/// [`saturating-float-to-int`]:
/// https://github.com/WebAssembly/nontrapping-float-to-int-conversions
pub fn wasm_saturating_float_to_int(&mut self, enable: bool) -> &mut Self {
self.saturating_float_to_int = enable;
self.features
.set(WasmFeatures::SATURATING_FLOAT_TO_INT, enable);

Check warning on line 247 in crates/wasmi/src/engine/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/config.rs#L247

Added line #L247 was not covered by tests
self
}

Expand All @@ -265,7 +256,7 @@
///
/// [`multi-value`]: https://github.com/WebAssembly/multi-value
pub fn wasm_multi_value(&mut self, enable: bool) -> &mut Self {
self.multi_value = enable;
self.features.set(WasmFeatures::MULTI_VALUE, enable);
self
}

Expand All @@ -277,7 +268,7 @@
///
/// [`multi-memory`]: https://github.com/WebAssembly/multi-memory
pub fn wasm_multi_memory(&mut self, enable: bool) -> &mut Self {
self.multi_memory = enable;
self.features.set(WasmFeatures::MULTI_MEMORY, enable);
self
}

Expand All @@ -289,7 +280,7 @@
///
/// [`bulk-memory`]: https://github.com/WebAssembly/bulk-memory-operations
pub fn wasm_bulk_memory(&mut self, enable: bool) -> &mut Self {
self.bulk_memory = enable;
self.features.set(WasmFeatures::BULK_MEMORY, enable);
self
}

Expand All @@ -301,7 +292,8 @@
///
/// [`reference-types`]: https://github.com/WebAssembly/reference-types
pub fn wasm_reference_types(&mut self, enable: bool) -> &mut Self {
self.reference_types = enable;
self.features.set(WasmFeatures::REFERENCE_TYPES, enable);
self.features.set(WasmFeatures::GC_TYPES, enable);
self
}

Expand All @@ -313,7 +305,7 @@
///
/// [`tail-call`]: https://github.com/WebAssembly/tail-call
pub fn wasm_tail_call(&mut self, enable: bool) -> &mut Self {
self.tail_call = enable;
self.features.set(WasmFeatures::TAIL_CALL, enable);
self
}

Expand All @@ -325,15 +317,15 @@
///
/// [`extended-const`]: https://github.com/WebAssembly/extended-const
pub fn wasm_extended_const(&mut self, enable: bool) -> &mut Self {
self.extended_const = enable;
self.features.set(WasmFeatures::EXTENDED_CONST, enable);
self
}

/// Enable or disable Wasm floating point (`f32` and `f64`) instructions and types.
///
/// Enabled by default.
pub fn floats(&mut self, enable: bool) -> &mut Self {
self.floats = enable;
self.features.set(WasmFeatures::FLOATS, enable);

Check warning on line 328 in crates/wasmi/src/engine/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/config.rs#L328

Added line #L328 was not covered by tests
self
}

Expand Down Expand Up @@ -420,24 +412,6 @@

/// Returns the [`WasmFeatures`] represented by the [`Config`].
pub(crate) fn wasm_features(&self) -> WasmFeatures {
WasmFeatures {
multi_value: self.multi_value,
mutable_global: self.mutable_global,
saturating_float_to_int: self.saturating_float_to_int,
sign_extension: self.sign_extension,
bulk_memory: self.bulk_memory,
reference_types: self.reference_types,
tail_call: self.tail_call,
extended_const: self.extended_const,
floats: self.floats,
component_model: false,
simd: false,
relaxed_simd: false,
threads: false,
multi_memory: self.multi_memory,
exceptions: false,
memory64: false,
memory_control: false,
}
self.features
}
}
14 changes: 11 additions & 3 deletions crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@
module: ModuleHeader,
func_to_validate: Option<FuncToValidate<ValidatorResources>>,
) -> Result<(), Error> {
let features = self.config().wasm_features();
match (self.config.get_compilation_mode(), func_to_validate) {
(CompilationMode::Eager, Some(func_to_validate)) => {
let (translation_allocs, validation_allocs) = self.get_allocs();
Expand All @@ -580,16 +581,23 @@
}
(CompilationMode::LazyTranslation, Some(func_to_validate)) => {
let allocs = self.get_validation_allocs();
let translator = LazyFuncTranslator::new(func_index, engine_func, module, None);
let translator =
LazyFuncTranslator::new_unchecked(func_index, engine_func, module, features);

Check warning on line 585 in crates/wasmi/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/mod.rs#L584-L585

Added lines #L584 - L585 were not covered by tests
let validator = func_to_validate.into_validator(allocs);
let translator = ValidatingFuncTranslator::new(validator, translator)?;
let allocs = FuncTranslationDriver::new(offset, bytes, translator)?
.translate(|func_entity| self.init_func(engine_func, func_entity))?;
self.recycle_validation_allocs(allocs.validation);
}
(CompilationMode::Lazy | CompilationMode::LazyTranslation, func_to_validate) => {
let translator =
LazyFuncTranslator::new(func_index, engine_func, module, func_to_validate);
let translator = match func_to_validate {
Some(func_to_validate) => {
LazyFuncTranslator::new(func_index, engine_func, module, func_to_validate)

Check warning on line 595 in crates/wasmi/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/mod.rs#L593-L595

Added lines #L593 - L595 were not covered by tests
}
None => {
LazyFuncTranslator::new_unchecked(func_index, engine_func, module, features)

Check warning on line 598 in crates/wasmi/src/engine/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/mod.rs#L597-L598

Added lines #L597 - L598 were not covered by tests
}
};
FuncTranslationDriver::new(offset, bytes, translator)?
.translate(|func_entity| self.init_func(engine_func, func_entity))?;
}
Expand Down
16 changes: 8 additions & 8 deletions crates/wasmi/src/engine/translator/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
engine::{code_map::CompiledFuncEntity, WasmTranslator},
Error,
};
use wasmparser::FunctionBody;
use wasmparser::{BinaryReader, FunctionBody};

/// Translates Wasm bytecode into Wasmi bytecode for a single Wasm function.
pub struct FuncTranslationDriver<'parser, T> {
Expand All @@ -14,27 +14,27 @@ pub struct FuncTranslationDriver<'parser, T> {
translator: T,
}

impl<'parser, T> FuncTranslationDriver<'parser, T> {
impl<'parser, T> FuncTranslationDriver<'parser, T>
where
T: WasmTranslator<'parser>,
{
/// Creates a new Wasm to Wasmi bytecode function translator.
pub fn new(
offset: impl Into<Option<usize>>,
bytes: &'parser [u8],
translator: T,
) -> Result<Self, Error> {
let offset = offset.into().unwrap_or(0);
let func_body = FunctionBody::new(offset, bytes);
let features = translator.features();
let reader = BinaryReader::new_features(bytes, offset, features);
let func_body = FunctionBody::new(reader);
Ok(Self {
func_body,
bytes,
translator,
})
}
}

impl<'parser, T> FuncTranslationDriver<'parser, T>
where
T: WasmTranslator<'parser>,
{
/// Starts translation of the Wasm stream into Wasmi bytecode.
pub fn translate(
mut self,
Expand Down
Loading
Loading