Skip to content

Commit 1dacd13

Browse files
committed
wasm-mutate: Add a mutator to remove items in a module
This commit adds a new mutator to `wasm-mutate` which can remove any of these items in a module: * Functions * Tables * Memories * Tags * Globals * Types * Data * Elements This pass is not currently a "gc" pass where a set of used references is generated, but rather the mutator considers itself applicable if a wasm module contains any of the above items. If, during removal of an item, the item is actually used then the mutation aborts with "not applicable". It's expected that drivers like `wasm-shrink` will try again with different seeds which will try a different mutator or will otherwise try to remove a different item. The main complexity in this mutator is that almost the entire module (including the code section) is entirely rewritten. This is because item references via index are everywhere in a wasm module and all of them are candidate for being updated. Overall my original test case for when `wasm-shrink` was first added does indeed get successfully reduced down further from before, removing extraneous functions and globals. The function-in-question still isn't shrunk much but I believe that's because the peephole and code motion mutator don't understand simd yet so those mutations are not applicable. This commit also removes the recently added data segment and element segment mutators. Their functionality is folded into this new `RemoveItemMutator` mutator and the code section is now also updated to account for updating `{elem,data}.drop` references.
1 parent bb96b24 commit 1dacd13

30 files changed

Lines changed: 1565 additions & 311 deletions

File tree

crates/wasm-encoder/src/code.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::borrow::Cow;
3232
///
3333
/// let wasm_bytes = module.finish();
3434
/// ```
35-
#[derive(Clone, Debug)]
35+
#[derive(Clone, Default, Debug)]
3636
pub struct CodeSection {
3737
bytes: Vec<u8>,
3838
num_added: u32,
@@ -41,10 +41,7 @@ pub struct CodeSection {
4141
impl CodeSection {
4242
/// Create a new code section encoder.
4343
pub fn new() -> CodeSection {
44-
CodeSection {
45-
bytes: vec![],
46-
num_added: 0,
47-
}
44+
CodeSection::default()
4845
}
4946

5047
/// How many function bodies have been defined inside this section so far?
@@ -361,7 +358,7 @@ pub enum Instruction<'a> {
361358
F64Const(f64),
362359
I32Eqz,
363360
I32Eq,
364-
I32Neq,
361+
I32Ne,
365362
I32LtS,
366363
I32LtU,
367364
I32GtS,
@@ -372,7 +369,7 @@ pub enum Instruction<'a> {
372369
I32GeU,
373370
I64Eqz,
374371
I64Eq,
375-
I64Neq,
372+
I64Ne,
376373
I64LtS,
377374
I64LtU,
378375
I64GtS,
@@ -382,13 +379,13 @@ pub enum Instruction<'a> {
382379
I64GeS,
383380
I64GeU,
384381
F32Eq,
385-
F32Neq,
382+
F32Ne,
386383
F32Lt,
387384
F32Gt,
388385
F32Le,
389386
F32Ge,
390387
F64Eq,
391-
F64Neq,
388+
F64Ne,
392389
F64Lt,
393390
F64Gt,
394391
F64Le,
@@ -1022,7 +1019,7 @@ impl Instruction<'_> {
10221019
}
10231020
Instruction::I32Eqz => bytes.push(0x45),
10241021
Instruction::I32Eq => bytes.push(0x46),
1025-
Instruction::I32Neq => bytes.push(0x47),
1022+
Instruction::I32Ne => bytes.push(0x47),
10261023
Instruction::I32LtS => bytes.push(0x48),
10271024
Instruction::I32LtU => bytes.push(0x49),
10281025
Instruction::I32GtS => bytes.push(0x4A),
@@ -1033,7 +1030,7 @@ impl Instruction<'_> {
10331030
Instruction::I32GeU => bytes.push(0x4F),
10341031
Instruction::I64Eqz => bytes.push(0x50),
10351032
Instruction::I64Eq => bytes.push(0x51),
1036-
Instruction::I64Neq => bytes.push(0x52),
1033+
Instruction::I64Ne => bytes.push(0x52),
10371034
Instruction::I64LtS => bytes.push(0x53),
10381035
Instruction::I64LtU => bytes.push(0x54),
10391036
Instruction::I64GtS => bytes.push(0x55),
@@ -1043,13 +1040,13 @@ impl Instruction<'_> {
10431040
Instruction::I64GeS => bytes.push(0x59),
10441041
Instruction::I64GeU => bytes.push(0x5A),
10451042
Instruction::F32Eq => bytes.push(0x5B),
1046-
Instruction::F32Neq => bytes.push(0x5C),
1043+
Instruction::F32Ne => bytes.push(0x5C),
10471044
Instruction::F32Lt => bytes.push(0x5D),
10481045
Instruction::F32Gt => bytes.push(0x5E),
10491046
Instruction::F32Le => bytes.push(0x5F),
10501047
Instruction::F32Ge => bytes.push(0x60),
10511048
Instruction::F64Eq => bytes.push(0x61),
1052-
Instruction::F64Neq => bytes.push(0x62),
1049+
Instruction::F64Ne => bytes.push(0x62),
10531050
Instruction::F64Lt => bytes.push(0x63),
10541051
Instruction::F64Gt => bytes.push(0x64),
10551052
Instruction::F64Le => bytes.push(0x65),

crates/wasm-encoder/src/data.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use super::*;
3030
///
3131
/// let wasm_bytes = module.finish();
3232
/// ```
33-
#[derive(Clone, Debug)]
33+
#[derive(Clone, Default, Debug)]
3434
pub struct DataSection {
3535
bytes: Vec<u8>,
3636
num_added: u32,
@@ -64,10 +64,7 @@ pub enum DataSegmentMode<'a> {
6464
impl DataSection {
6565
/// Create a new data section encoder.
6666
pub fn new() -> DataSection {
67-
DataSection {
68-
bytes: vec![],
69-
num_added: 0,
70-
}
67+
DataSection::default()
7168
}
7269

7370
/// How many segments have been defined inside this section so far?

crates/wasm-encoder/src/elements.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use super::*;
3333
///
3434
/// let wasm_bytes = module.finish();
3535
/// ```
36-
#[derive(Clone, Debug)]
36+
#[derive(Clone, Default, Debug)]
3737
pub struct ElementSection {
3838
bytes: Vec<u8>,
3939
num_added: u32,
@@ -94,10 +94,7 @@ pub struct ElementSegment<'a> {
9494
impl ElementSection {
9595
/// Create a new element section encoder.
9696
pub fn new() -> ElementSection {
97-
ElementSection {
98-
bytes: vec![],
99-
num_added: 0,
100-
}
97+
ElementSection::default()
10198
}
10299

103100
/// How many segments have been defined inside this section so far?

crates/wasm-encoder/src/exports.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use super::*;
2626
///
2727
/// let wasm_bytes = module.finish();
2828
/// ```
29-
#[derive(Clone, Debug)]
29+
#[derive(Clone, Default, Debug)]
3030
pub struct ExportSection {
3131
bytes: Vec<u8>,
3232
num_added: u32,
@@ -35,10 +35,7 @@ pub struct ExportSection {
3535
impl ExportSection {
3636
/// Create a new export section encoder.
3737
pub fn new() -> ExportSection {
38-
ExportSection {
39-
bytes: vec![],
40-
num_added: 0,
41-
}
38+
ExportSection::default()
4239
}
4340

4441
/// How many exports have been defined inside this section so far?

crates/wasm-encoder/src/functions.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::*;
2020
///
2121
/// let wasm_bytes = module.finish();
2222
/// ```
23-
#[derive(Clone, Debug)]
23+
#[derive(Clone, Default, Debug)]
2424
pub struct FunctionSection {
2525
bytes: Vec<u8>,
2626
num_added: u32,
@@ -29,10 +29,7 @@ pub struct FunctionSection {
2929
impl FunctionSection {
3030
/// Construct a new function section encoder.
3131
pub fn new() -> FunctionSection {
32-
FunctionSection {
33-
bytes: vec![],
34-
num_added: 0,
35-
}
32+
FunctionSection::default()
3633
}
3734

3835
/// How many functions have been defined inside this section so far?

crates/wasm-encoder/src/globals.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::*;
2121
///
2222
/// let wasm_bytes = module.finish();
2323
/// ```
24-
#[derive(Clone, Debug)]
24+
#[derive(Clone, Default, Debug)]
2525
pub struct GlobalSection {
2626
bytes: Vec<u8>,
2727
num_added: u32,
@@ -30,10 +30,7 @@ pub struct GlobalSection {
3030
impl GlobalSection {
3131
/// Create a new global section encoder.
3232
pub fn new() -> GlobalSection {
33-
GlobalSection {
34-
bytes: vec![],
35-
num_added: 0,
36-
}
33+
GlobalSection::default()
3734
}
3835

3936
/// How many globals have been defined inside this section so far?

crates/wasm-encoder/src/imports.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::convert::TryFrom;
2424
///
2525
/// let wasm_bytes = module.finish();
2626
/// ```
27-
#[derive(Clone, Debug)]
27+
#[derive(Clone, Default, Debug)]
2828
pub struct ImportSection {
2929
bytes: Vec<u8>,
3030
num_added: u32,
@@ -33,10 +33,7 @@ pub struct ImportSection {
3333
impl ImportSection {
3434
/// Construct a new import section encoder.
3535
pub fn new() -> ImportSection {
36-
ImportSection {
37-
bytes: vec![],
38-
num_added: 0,
39-
}
36+
ImportSection::default()
4037
}
4138

4239
/// How many imports have been defined inside this section so far?

crates/wasm-encoder/src/memories.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::*;
1919
///
2020
/// let wasm_bytes = module.finish();
2121
/// ```
22-
#[derive(Clone, Debug)]
22+
#[derive(Clone, Default, Debug)]
2323
pub struct MemorySection {
2424
bytes: Vec<u8>,
2525
num_added: u32,
@@ -28,10 +28,7 @@ pub struct MemorySection {
2828
impl MemorySection {
2929
/// Create a new memory section encoder.
3030
pub fn new() -> MemorySection {
31-
MemorySection {
32-
bytes: vec![],
33-
num_added: 0,
34-
}
31+
MemorySection::default()
3532
}
3633

3734
/// How many memories have been defined inside this section so far?

crates/wasm-encoder/src/tables.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::*;
1919
///
2020
/// let wasm_bytes = module.finish();
2121
/// ```
22-
#[derive(Clone, Debug)]
22+
#[derive(Clone, Default, Debug)]
2323
pub struct TableSection {
2424
bytes: Vec<u8>,
2525
num_added: u32,
@@ -28,10 +28,7 @@ pub struct TableSection {
2828
impl TableSection {
2929
/// Construct a new table section encoder.
3030
pub fn new() -> TableSection {
31-
TableSection {
32-
bytes: vec![],
33-
num_added: 0,
34-
}
31+
TableSection::default()
3532
}
3633

3734
/// How many tables have been defined inside this section so far?

crates/wasm-encoder/src/tags.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::convert::TryFrom;
1919
///
2020
/// let wasm_bytes = module.finish();
2121
/// ```
22-
#[derive(Clone, Debug)]
22+
#[derive(Clone, Default, Debug)]
2323
pub struct TagSection {
2424
bytes: Vec<u8>,
2525
num_added: u32,
@@ -28,10 +28,7 @@ pub struct TagSection {
2828
impl TagSection {
2929
/// Create a new tag section encoder.
3030
pub fn new() -> TagSection {
31-
TagSection {
32-
bytes: vec![],
33-
num_added: 0,
34-
}
31+
TagSection::default()
3532
}
3633

3734
/// How many tags have been defined inside this section so far?

0 commit comments

Comments
 (0)