Skip to content

Commit ae473e3

Browse files
authored
Remove custom translation framework from wasm-mutate (#1796)
* Treat inputs uniformly in `mutate` fuzzer Don't use cargo features to reinterpret inputs to ensure that test cases can reproduce more easily when toggling features. * Remove a panic in wasm-mutate Let this fall through to the "unimplemented opcodes" section below instead of panicking. * Remove usage of `Translator` from peephole pass Only used in a minor capacity, easy to transfer over. * Share the `ReencodeResult` type in wasm-mutate * Refactor `Elements` in `wasm-encoder` * Add another hook in `Reencode` to process just elements * Change the internal slices to `Cow` instead of `&[T]` to be a bit more flexible in terms of ownership * Migrate const expression mutator to wasm-mutate Mostly just updating various bits and pieces here and there. * Remove translation framework of wasm-mutate Now no-longer-needed. * Reject `TryTable` in `wasm-mutate` Returns a first-class error instead of creating an invalid module. * Fix another panic in wasm-mutate Fall back on empty type info instead of panicking to get the error to crop up elsewhere for unsupported instructions. * Fix test on historical rust * More test fixes for older rust
1 parent 23aceed commit ae473e3

18 files changed

Lines changed: 165 additions & 489 deletions

File tree

crates/wasm-encoder/src/core/code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3736,7 +3736,7 @@ impl Encode for Catch {
37363736
/// A constant expression.
37373737
///
37383738
/// Usable in contexts such as offsets or initializers.
3739-
#[derive(Debug)]
3739+
#[derive(Clone, Debug)]
37403740
pub struct ConstExpr {
37413741
bytes: Vec<u8>,
37423742
}

crates/wasm-encoder/src/core/elements.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId};
2+
use std::borrow::Cow;
23

34
/// An encoder for the element section.
45
///
@@ -7,6 +8,7 @@ use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId};
78
/// # Example
89
///
910
/// ```
11+
/// use std::borrow::Cow;
1012
/// use wasm_encoder::{
1113
/// Elements, ElementSection, Module, TableSection, TableType,
1214
/// RefType, ConstExpr
@@ -24,9 +26,9 @@ use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId};
2426
/// let mut elements = ElementSection::new();
2527
/// let table_index = 0;
2628
/// let offset = ConstExpr::i32_const(42);
27-
/// let functions = Elements::Functions(&[
29+
/// let functions = Elements::Functions(Cow::Borrowed(&[
2830
/// // Function indices...
29-
/// ]);
31+
/// ]));
3032
/// elements.active(Some(table_index), &offset, functions);
3133
///
3234
/// let mut module = Module::new();
@@ -43,12 +45,12 @@ pub struct ElementSection {
4345
}
4446

4547
/// A sequence of elements in a segment in the element section.
46-
#[derive(Clone, Copy, Debug)]
48+
#[derive(Clone, Debug)]
4749
pub enum Elements<'a> {
4850
/// A sequences of references to functions by their indices.
49-
Functions(&'a [u32]),
51+
Functions(Cow<'a, [u32]>),
5052
/// A sequence of reference expressions.
51-
Expressions(RefType, &'a [ConstExpr]),
53+
Expressions(RefType, Cow<'a, [ConstExpr]>),
5254
}
5355

5456
/// An element segment's mode.
@@ -151,7 +153,7 @@ impl ElementSection {
151153
ty.encode(&mut self.bytes);
152154
}
153155
e.len().encode(&mut self.bytes);
154-
for expr in e {
156+
for expr in e.iter() {
155157
expr.encode(&mut self.bytes);
156158
}
157159
}

crates/wasm-encoder/src/reencode.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ pub trait Reencode {
304304
utils::parse_element(self, elements, element)
305305
}
306306

307+
fn element_items<'a>(
308+
&mut self,
309+
items: wasmparser::ElementItems<'a>,
310+
) -> Result<crate::Elements<'a>, Error<Self::Error>> {
311+
utils::element_items(self, items)
312+
}
313+
307314
/// Parses the input `section` given from the `wasmparser` crate and adds
308315
/// all the exports to the `exports` section.
309316
fn parse_export_section(
@@ -1390,24 +1397,7 @@ pub mod utils {
13901397
elements: &mut crate::ElementSection,
13911398
element: wasmparser::Element<'_>,
13921399
) -> Result<(), Error<T::Error>> {
1393-
let mut funcs;
1394-
let mut exprs;
1395-
let elems = match element.items {
1396-
wasmparser::ElementItems::Functions(f) => {
1397-
funcs = Vec::new();
1398-
for func in f {
1399-
funcs.push(reencoder.function_index(func?));
1400-
}
1401-
crate::Elements::Functions(&funcs)
1402-
}
1403-
wasmparser::ElementItems::Expressions(ty, e) => {
1404-
exprs = Vec::new();
1405-
for expr in e {
1406-
exprs.push(reencoder.const_expr(expr?)?);
1407-
}
1408-
crate::Elements::Expressions(reencoder.ref_type(ty)?, &exprs)
1409-
}
1410-
};
1400+
let elems = reencoder.element_items(element.items)?;
14111401
match element.kind {
14121402
wasmparser::ElementKind::Active {
14131403
table_index,
@@ -1436,6 +1426,28 @@ pub mod utils {
14361426
Ok(())
14371427
}
14381428

1429+
pub fn element_items<'a, T: ?Sized + Reencode>(
1430+
reencoder: &mut T,
1431+
items: wasmparser::ElementItems<'a>,
1432+
) -> Result<crate::Elements<'a>, Error<T::Error>> {
1433+
Ok(match items {
1434+
wasmparser::ElementItems::Functions(f) => {
1435+
let mut funcs = Vec::new();
1436+
for func in f {
1437+
funcs.push(reencoder.function_index(func?));
1438+
}
1439+
crate::Elements::Functions(funcs.into())
1440+
}
1441+
wasmparser::ElementItems::Expressions(ty, e) => {
1442+
let mut exprs = Vec::new();
1443+
for expr in e {
1444+
exprs.push(reencoder.const_expr(expr?)?);
1445+
}
1446+
crate::Elements::Expressions(reencoder.ref_type(ty)?, exprs.into())
1447+
}
1448+
})
1449+
}
1450+
14391451
pub fn table_index<T: ?Sized + Reencode>(_reencoder: &mut T, table: u32) -> u32 {
14401452
table
14411453
}

crates/wasm-mutate/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,6 @@ pub enum ErrorKind {
101101

102102
/// A `Result` type that is either `Ok(T)` or `Err(wasm_mutate::Error)`.
103103
pub type Result<T, E = Error> = std::result::Result<T, E>;
104+
105+
/// A `Result` type for use with `wasm_encoder::reencode`
106+
pub type ReencodeResult<T, E = Error> = Result<T, wasm_encoder::reencode::Error<E>>;

crates/wasm-mutate/src/mutators.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub mod start;
3939

4040
mod translate;
4141
pub use self::translate::Item;
42-
use self::translate::{DefaultTranslator, Translator};
4342

4443
use std::borrow::Cow;
4544

crates/wasm-mutate/src/mutators/codemotion/ir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use crate::{
33
module::map_block_type,
44
mutators::{codemotion::ir::parse_context::ParseContext, OperatorAndByteOffset},
5+
Error,
56
};
67
use std::ops::Range;
78
use wasm_encoder::{Function, Instruction};
@@ -287,6 +288,7 @@ impl AstBuilder {
287288
parse_context.push_state();
288289
parse_context.push_frame(State::Loop, Some(*blockty), idx);
289290
}
291+
Operator::TryTable { .. } => return Err(Error::no_mutations_applicable()),
290292
Operator::End => {
291293
if !parse_context.current_code_is_empty() {
292294
parse_context.push_current_code_as_node();

crates/wasm-mutate/src/mutators/codemotion/loop_unrolling.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,10 @@ impl LoopUnrollWriter {
7070
newfunc.instruction(&Instruction::Block(map_block_type(*ty)?));
7171
for (idx, (op, _)) in chunk.iter().enumerate() {
7272
match op {
73-
Operator::Block { .. } => {
74-
current_depth += 1;
75-
}
76-
Operator::Loop { .. } => {
77-
current_depth += 1;
78-
}
79-
Operator::If { .. } => {
73+
Operator::Block { .. }
74+
| Operator::Loop { .. }
75+
| Operator::If { .. }
76+
| Operator::TryTable { .. } => {
8077
current_depth += 1;
8178
}
8279
Operator::End { .. } => {

0 commit comments

Comments
 (0)