Skip to content

Commit f58b04b

Browse files
committed
Fix test failure by validating functions
1 parent bb9dfc3 commit f58b04b

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/vm/src/parsed_wasm.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use wasmer::wasmparser::{
88

99
use crate::{VmError, VmResult};
1010

11+
#[derive(Default)]
1112
pub struct OpaqueDebug<T>(pub T);
1213

1314
impl<T> fmt::Debug for OpaqueDebug<T> {
@@ -19,18 +20,18 @@ impl<T> fmt::Debug for OpaqueDebug<T> {
1920

2021
#[derive(Debug)]
2122
pub enum FunctionValidator<'a> {
22-
Pending(Vec<OpaqueDebug<(FuncToValidate<ValidatorResources>, FunctionBody<'a>)>>),
23+
Pending(OpaqueDebug<Vec<(FuncToValidate<ValidatorResources>, FunctionBody<'a>)>>),
2324
Success,
2425
Error(BinaryReaderError),
2526
}
2627

2728
impl<'a> FunctionValidator<'a> {
2829
fn push(&mut self, item: (FuncToValidate<ValidatorResources>, FunctionBody<'a>)) {
29-
let Self::Pending(ref mut funcs) = self else {
30+
let Self::Pending(OpaqueDebug(ref mut funcs)) = self else {
3031
panic!("attempted to push function into non-pending validator");
3132
};
3233

33-
funcs.push(OpaqueDebug(item));
34+
funcs.push(item);
3435
}
3536
}
3637

@@ -97,7 +98,7 @@ impl<'a> ParsedWasm<'a> {
9798
max_func_params: 0,
9899
max_func_results: 0,
99100
total_func_params: 0,
100-
func_validator: FunctionValidator::Pending(Vec::new()),
101+
func_validator: FunctionValidator::Pending(OpaqueDebug::default()),
101102
};
102103

103104
for p in Parser::new(0).parse_all(wasm) {
@@ -181,10 +182,10 @@ impl<'a> ParsedWasm<'a> {
181182
/// Note: This function caches the output of this function into the field `funcs_to_validate` so repeated invocations are cheap.
182183
pub fn validate_funcs(&mut self) -> VmResult<()> {
183184
match self.func_validator {
184-
FunctionValidator::Pending(ref mut funcs) => {
185+
FunctionValidator::Pending(OpaqueDebug(ref mut funcs)) => {
185186
let result = mem::take(funcs) // This is fine since `Vec::default()` doesn't allocate
186187
.into_par_iter()
187-
.try_for_each(|OpaqueDebug((func, body))| {
188+
.try_for_each(|(func, body)| {
188189
// Reusing the allocations between validations only results in an ~6% performance improvement
189190
// The parallelization is blowing this out of the water by a magnitude of 5x
190191
// Especially when combining this with a high-performance allocator, the missing buffer reuse will be pretty much irrelevant

packages/vm/src/static_analysis.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ mod tests {
135135

136136
#[test]
137137
fn deserialize_wasm_corrupted_data() {
138-
match ParsedWasm::parse(CORRUPTED).unwrap_err() {
138+
match ParsedWasm::parse(CORRUPTED)
139+
.and_then(|mut parsed| parsed.validate_funcs())
140+
.unwrap_err()
141+
{
139142
VmError::StaticValidationErr { msg, .. } => {
140143
assert!(msg.starts_with("Wasm bytecode could not be deserialized."))
141144
}

0 commit comments

Comments
 (0)