diff --git a/crates/wasm-mutate/src/error.rs b/crates/wasm-mutate/src/error.rs index c01a644024..0f0f1791b7 100644 --- a/crates/wasm-mutate/src/error.rs +++ b/crates/wasm-mutate/src/error.rs @@ -56,16 +56,25 @@ impl From for Error { } } -impl From> for Error { - fn from(e: wasm_encoder::reencode::Error) -> Self { +impl From> for Error +where + E: Into + std::fmt::Display, +{ + fn from(e: wasm_encoder::reencode::Error) -> Self { match e { wasm_encoder::reencode::Error::ParseError(e) => Error::parse(e), - wasm_encoder::reencode::Error::UserError(e) => e, + wasm_encoder::reencode::Error::UserError(e) => e.into(), other => Error::other(other.to_string()), } } } +impl From for Error { + fn from(i: std::convert::Infallible) -> Error { + match i {} + } +} + /// The kind of error. #[derive(thiserror::Error, Debug)] pub enum ErrorKind { diff --git a/crates/wasm-mutate/src/mutators/modify_data.rs b/crates/wasm-mutate/src/mutators/modify_data.rs index e7356012d1..e40317ca90 100644 --- a/crates/wasm-mutate/src/mutators/modify_data.rs +++ b/crates/wasm-mutate/src/mutators/modify_data.rs @@ -1,9 +1,8 @@ -use super::translate::ConstExprKind; use super::Mutator; -use crate::mutators::{DefaultTranslator, Translator}; use crate::{Result, WasmMutate}; use rand::Rng; -use wasm_encoder::{DataSection, DataSegment, DataSegmentMode, Module}; +use wasm_encoder::reencode::{Reencode, RoundtripReencoder}; +use wasm_encoder::{DataSection, Module}; use wasmparser::{DataKind, DataSectionReader}; /// Mutator that modifies a data segment, either adding or removing bytes. @@ -29,32 +28,29 @@ impl Mutator for ModifyDataMutator { // them to the `new_section` one-by-one. for (i, data) in reader.into_iter().enumerate() { let data = data?; - let offset; - // Preserve the mode of the data segment - let mode = match &data.kind { + // If this is the correct data segment apply the mutation, + // otherwise preserve the data. + let mut contents = data.data.to_vec(); + if i as u32 == data_to_modify { + config.raw_mutate(&mut contents, self.max_data_size)?; + } + + // Add the data segment to the section that we're building + match data.kind { DataKind::Active { memory_index, offset_expr, } => { - offset = DefaultTranslator.translate_const_expr( - offset_expr, - &wasmparser::ValType::I32, - ConstExprKind::DataOffset, - )?; - DataSegmentMode::Active { - memory_index: *memory_index, - offset: &offset, - } + new_section.active( + memory_index, + &RoundtripReencoder.const_expr(offset_expr)?, + contents, + ); + } + DataKind::Passive => { + new_section.passive(contents); } - DataKind::Passive => DataSegmentMode::Passive, }; - // If this is the correct data segment apply the mutation, - // otherwise preserve the data. - let mut data = data.data.to_vec(); - if i as u32 == data_to_modify { - config.raw_mutate(&mut data, self.max_data_size)?; - } - new_section.segment(DataSegment { mode, data }); } Ok(Box::new(std::iter::once(Ok(config diff --git a/crates/wasm-mutate/src/mutators/translate.rs b/crates/wasm-mutate/src/mutators/translate.rs index fe57fe6cef..8b07f1054c 100644 --- a/crates/wasm-mutate/src/mutators/translate.rs +++ b/crates/wasm-mutate/src/mutators/translate.rs @@ -19,7 +19,6 @@ pub enum ConstExprKind { Global, ElementOffset, ElementFunction, - DataOffset, } pub trait Translator {