libchisel/
trimstartfunc.rs

1use parity_wasm::elements::Module;
2
3use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
4
5pub struct TrimStartFunc;
6
7impl TrimStartFunc {
8    fn trim_startfunc(&self, module: &mut Module) -> bool {
9        if let Some(_start_section) = module.start_section() {
10            module.clear_start_section();
11            true
12        } else {
13            false
14        }
15    }
16}
17
18impl<'a> ChiselModule<'a> for TrimStartFunc {
19    type ObjectReference = &'a dyn ModuleTranslator;
20
21    fn id(&'a self) -> String {
22        "trimstartfunc".to_string()
23    }
24
25    fn kind(&'a self) -> ModuleKind {
26        ModuleKind::Translator
27    }
28
29    fn as_abstract(&'a self) -> Self::ObjectReference {
30        self as Self::ObjectReference
31    }
32}
33
34impl ModulePreset for TrimStartFunc {
35    fn with_preset(preset: &str) -> Result<Self, ModuleError> {
36        match preset {
37            "ewasm" => Ok(TrimStartFunc {}),
38            _ => Err(ModuleError::NotSupported),
39        }
40    }
41}
42
43impl ModuleTranslator for TrimStartFunc {
44    fn translate_inplace(&self, module: &mut Module) -> Result<bool, ModuleError> {
45        Ok(self.trim_startfunc(module))
46    }
47
48    fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError> {
49        let mut ret = module.clone();
50        let modified = self.trim_startfunc(&mut ret);
51        if modified {
52            return Ok(Some(ret));
53        }
54        Ok(None)
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn start_removed() {
64        let wasm: Vec<u8> = vec![
65            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
66            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
67            0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
68        ];
69
70        let mut module = Module::from_bytes(&wasm).unwrap();
71
72        let trimmer = TrimStartFunc::with_preset("ewasm").unwrap();
73        trimmer.translate_inplace(&mut module).unwrap();
74
75        let result = module.to_bytes().unwrap();
76        let expect: Vec<u8> = vec![
77            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
78            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
79            0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
80        ];
81
82        assert_eq!(expect, result);
83    }
84
85    #[test]
86    fn start_not_removed() {
87        let wasm: Vec<u8> = vec![
88            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
89            0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
90            0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
91        ];
92
93        let mut module = Module::from_bytes(&wasm).unwrap();
94
95        let trimmer = TrimStartFunc::with_preset("ewasm").unwrap();
96        trimmer.translate_inplace(&mut module).unwrap();
97
98        let result = module.to_bytes().unwrap();
99
100        // result is equal to initial wasm (not changed)
101        assert_eq!(result, wasm);
102    }
103}