Skip to content

Commit

Permalink
Remove Translator from ModifyData in wasm-mutate (#1795)
Browse files Browse the repository at this point in the history
Same as #1794 except for another use of the `Translator` trait.
  • Loading branch information
alexcrichton authored Sep 17, 2024
1 parent 4db76b8 commit 23aceed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
15 changes: 12 additions & 3 deletions crates/wasm-mutate/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,25 @@ impl From<wasmparser::BinaryReaderError> for Error {
}
}

impl From<wasm_encoder::reencode::Error<Error>> for Error {
fn from(e: wasm_encoder::reencode::Error<Error>) -> Self {
impl<E> From<wasm_encoder::reencode::Error<E>> for Error
where
E: Into<Error> + std::fmt::Display,
{
fn from(e: wasm_encoder::reencode::Error<E>) -> 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<std::convert::Infallible> for Error {
fn from(i: std::convert::Infallible) -> Error {
match i {}
}
}

/// The kind of error.
#[derive(thiserror::Error, Debug)]
pub enum ErrorKind {
Expand Down
42 changes: 19 additions & 23 deletions crates/wasm-mutate/src/mutators/modify_data.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion crates/wasm-mutate/src/mutators/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub enum ConstExprKind {
Global,
ElementOffset,
ElementFunction,
DataOffset,
}

pub trait Translator {
Expand Down

0 comments on commit 23aceed

Please sign in to comment.