Skip to content

Commit 56565da

Browse files
authored
Merge pull request #87 from hugo-dc/trimstart
Module for removing start function (trimstartfunction)
2 parents a7ef630 + 02e31c3 commit 56565da

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ This comes with some presets:
2020
- `ewasm`: keeps `main` and exported memory
2121
- `pwasm`: keeps `_call`
2222

23+
### trimstartfunc
24+
25+
Remove start function.
26+
27+
This comes with the following preset:
28+
- `ewasm`: removes `start` function if present
29+
30+
2331
### verifyimports
2432

2533
Verifies that the module's imports are compliant with the provided import interface.

chisel/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::fs::{read, read_to_string};
1010
use std::process;
1111

1212
use libchisel::{
13-
checkstartfunc::*, deployer::*, remapimports::*, trimexports::*, verifyexports::*,
14-
verifyimports::*,
13+
checkstartfunc::*, deployer::*, remapimports::*, trimexports::*, trimstartfunc::*,
14+
verifyexports::*, verifyimports::*,
1515
};
1616

1717
use clap::{App, Arg, ArgMatches, SubCommand};
@@ -218,12 +218,21 @@ fn execute_module(context: &ModuleContext, module: &mut Module) -> bool {
218218
}
219219
"trimexports" => {
220220
is_translator = true;
221+
221222
if let Ok(chisel) = TrimExports::with_preset(&preset) {
222223
translate_module(module, chisel)
223224
} else {
224225
Err("trimexports: Invalid preset")
225226
}
226227
}
228+
"trimstartfunc" => {
229+
is_translator = true;
230+
if let Ok(chisel) = TrimStartFunc::with_preset(&preset) {
231+
translate_module(module, chisel)
232+
} else {
233+
Err("trimstartfunc: Invalid preset")
234+
}
235+
}
227236
"remapimports" => {
228237
is_translator = true;
229238
if let Ok(chisel) = RemapImports::with_preset(&preset) {

libchisel/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod checkstartfunc;
1010
pub mod deployer;
1111
pub mod remapimports;
1212
pub mod trimexports;
13+
pub mod trimstartfunc;
1314
pub mod verifyexports;
1415
pub mod verifyimports;
1516

libchisel/src/trimstartfunc.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use super::{ModuleError, ModulePreset, ModuleTranslator};
2+
use parity_wasm::elements::*;
3+
4+
pub struct TrimStartFunc;
5+
6+
impl TrimStartFunc {
7+
fn trim_startfunc(&self, module: &mut Module) -> bool {
8+
if let Some(start_section) = module.start_section() {
9+
module.clear_start_section();
10+
true
11+
} else {
12+
false
13+
}
14+
}
15+
}
16+
17+
impl ModulePreset for TrimStartFunc {
18+
fn with_preset(preset: &str) -> Result<Self, ()> {
19+
match preset {
20+
"ewasm" => Ok(TrimStartFunc {}),
21+
_ => Err(()),
22+
}
23+
}
24+
}
25+
26+
impl ModuleTranslator for TrimStartFunc {
27+
fn translate_inplace(&self, module: &mut Module) -> Result<bool, ModuleError> {
28+
Ok(self.trim_startfunc(module))
29+
}
30+
31+
fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError> {
32+
Err(ModuleError::NotSupported)
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
39+
use super::*;
40+
use parity_wasm::elements::deserialize_buffer;
41+
42+
#[test]
43+
fn start_removed() {
44+
let wasm: Vec<u8> = vec![
45+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
46+
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
47+
0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
48+
];
49+
50+
let mut module = deserialize_buffer::<Module>(&wasm).unwrap();
51+
52+
let trimmer = TrimStartFunc::with_preset("ewasm").unwrap();
53+
trimmer.translate_inplace(&mut module).unwrap();
54+
55+
let result = serialize::<Module>(module).unwrap();
56+
let expect: Vec<u8> = vec![
57+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
58+
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
59+
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
60+
];
61+
62+
assert_eq!(expect, result);
63+
}
64+
65+
#[test]
66+
fn start_not_removed() {
67+
let wasm: Vec<u8> = vec![
68+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
69+
0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
70+
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
71+
];
72+
73+
let mut module = deserialize_buffer::<Module>(&wasm).unwrap();
74+
75+
let trimmer = TrimStartFunc::with_preset("ewasm").unwrap();
76+
trimmer.translate_inplace(&mut module).unwrap();
77+
78+
let result = serialize::<Module>(module).unwrap();
79+
80+
// result is equal to initial wasm (not changed)
81+
assert_eq!(result, wasm);
82+
}
83+
}

0 commit comments

Comments
 (0)