1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#[macro_use]
extern crate failure;
extern crate parity_wasm;
use parity_wasm::elements::{self, Deserialize};
use std::collections::hash_map::{Entry, HashMap};
use std::path;
#[derive(Clone, Debug, Default)]
pub struct Options {
pub input: path::PathBuf,
pub functions: Vec<String>,
}
const FUNCTION_NAMES: u8 = 1;
fn decode_name_map<'a>(mut bytes: &'a [u8]) -> Result<HashMap<String, usize>, failure::Error> {
while !bytes.is_empty() {
let name_type = u8::from(elements::VarUint7::deserialize(&mut bytes)?);
let name_payload_len = u32::from(elements::VarUint32::deserialize(&mut bytes)?);
let (these_bytes, rest) = bytes.split_at(name_payload_len as usize);
if name_type == FUNCTION_NAMES {
bytes = these_bytes;
} else {
bytes = rest;
continue;
}
let count = u32::from(elements::VarUint32::deserialize(&mut bytes)?);
let mut names = HashMap::with_capacity(count as usize);
for _ in 0..count {
let index = usize::from(elements::VarUint32::deserialize(&mut bytes)?);
let name_len = usize::from(elements::VarUint32::deserialize(&mut bytes)?);
let (name, rest) = bytes.split_at(name_len);
let name = String::from_utf8(name.to_vec())?;
match names.entry(name) {
Entry::Occupied(entry) => {
bail!(format!("duplicate name entries for '{}'", entry.key()));
}
Entry::Vacant(entry) => {
entry.insert(index);
}
}
bytes = rest;
}
return Ok(names);
}
return Ok(Default::default());
}
pub fn snip(options: Options) -> Result<elements::Module, failure::Error> {
let mut module = elements::deserialize_file(&options.input)?;
let names = module
.sections()
.iter()
.filter_map(|section| match *section {
elements::Section::Custom(ref custom) if custom.name() == "name" => Some(custom),
_ => None,
})
.next()
.and_then(|name_section| decode_name_map(name_section.payload()).ok())
.ok_or(failure::err_msg("missing \"name\" section"))?;
{
let num_imports = module
.import_section()
.map_or(0, |imports| imports.entries().len());
let code = module
.sections_mut()
.iter_mut()
.filter_map(|section| match *section {
elements::Section::Code(ref mut code) => Some(code),
_ => None,
})
.next()
.ok_or(failure::err_msg("missing code section"))?;
for function in &options.functions {
let idx = names.get(function).ok_or(failure::err_msg(format!(
"'{}' is not in the name section",
function
)))?;
let mut body = code.bodies_mut()
.get_mut(*idx - num_imports)
.ok_or(failure::err_msg(format!(
"index for '{}' is out of bounds of the code section",
function
)))?;
*body.code_mut().elements_mut() =
vec![elements::Opcode::Unreachable, elements::Opcode::End];
}
}
Ok(module)
}