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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::emit::{Emit, EmitContext, Section};
use crate::ir::Value;
use crate::parse::IndicesToIds;
use crate::passes::Used;
use crate::tombstone_arena::{Id, Tombstone, TombstoneArena};
use crate::{InitExpr, Module, Result, ValType};
use failure::{bail, ResultExt};
pub type DataId = Id<Data>;
#[derive(Debug)]
pub struct Data {
id: DataId,
pub(crate) passive: bool,
pub value: Vec<u8>,
}
impl Tombstone for Data {
fn on_delete(&mut self) {
self.value = Vec::new();
}
}
impl Data {
pub fn id(&self) -> DataId {
self.id
}
}
#[derive(Debug, Default)]
pub struct ModuleData {
arena: TombstoneArena<Data>,
}
impl ModuleData {
pub fn get(&self, id: DataId) -> &Data {
&self.arena[id]
}
pub fn get_mut(&mut self, id: DataId) -> &mut Data {
&mut self.arena[id]
}
pub fn delete(&mut self, id: DataId) {
self.arena.delete(id);
}
pub fn iter(&self) -> impl Iterator<Item = &Data> {
self.arena.iter().map(|(_, f)| f)
}
pub fn add(&mut self, value: Vec<u8>) -> DataId {
self.arena.alloc_with_id(|id| Data {
id,
value,
passive: true,
})
}
pub(crate) fn iter_used<'a>(&'a self, used: &'a Used) -> impl Iterator<Item = &'a Data> + 'a {
self.iter().filter(move |data| used.data.contains(&data.id))
}
pub(crate) fn emit_data_count(&self, cx: &mut EmitContext) {
let mut count = 0;
for mem in cx.module.memories.iter_used(cx.used) {
count += mem.emit_data().count();
}
let mut any_passive = false;
for data in self.iter_used(cx.used) {
cx.indices.set_data_index(data.id(), count as u32);
count += 1;
any_passive = true;
}
if any_passive {
cx.start_section(Section::DataCount).encoder.usize(count);
}
}
}
impl Module {
pub(crate) fn reserve_data(&mut self, count: u32, ids: &mut IndicesToIds) {
for _ in 0..count {
ids.push_data(self.data.arena.alloc_with_id(|id| Data {
id,
passive: false,
value: Vec::new(),
}));
}
}
pub(crate) fn parse_data(
&mut self,
section: wasmparser::DataSectionReader,
ids: &IndicesToIds,
data_count: Option<u32>,
) -> Result<()> {
log::debug!("parse data section");
if let Some(count) = data_count {
if count != section.get_count() {
bail!("data count section mismatches actual data section");
}
}
for (i, segment) in section.into_iter().enumerate() {
let segment = segment?;
match segment.kind {
wasmparser::DataKind::Passive => {
let id = ids.get_data(i as u32)?;
let data = self.data.get_mut(id);
data.value = segment.data.to_vec();
data.passive = true;
}
wasmparser::DataKind::Active {
memory_index,
init_expr,
} => {
let memory = ids.get_memory(memory_index)?;
let value = segment.data.to_vec();
let memory = self.memories.get_mut(memory);
let offset = InitExpr::eval(&init_expr, ids)
.with_context(|_e| format!("in segment {}", i))?;
match offset {
InitExpr::Value(Value::I32(n)) => {
memory.data.add_absolute(n as u32, value);
}
InitExpr::Global(global) if self.globals.get(global).ty == ValType::I32 => {
memory.data.add_relative(global, value);
}
_ => bail!("non-i32 constant in segment {}", i),
}
}
}
}
Ok(())
}
}
impl Emit for ModuleData {
fn emit(&self, cx: &mut EmitContext) {
log::debug!("emit data section");
let mut active = cx
.module
.memories
.iter_used(cx.used)
.flat_map(|memory| memory.emit_data().map(move |data| (memory.id(), data)))
.collect::<Vec<_>>();
active.sort_by_key(|pair| pair.0);
let passive = self.iter_used(cx.used).count();
if active.len() == 0 && passive == 0 {
return;
}
let mut cx = cx.start_section(Section::Data);
cx.encoder.usize(active.len() + passive);
for (id, (offset, data)) in active {
let index = cx.indices.get_memory_index(id);
if index == 0 {
cx.encoder.byte(0x00);
} else {
cx.encoder.byte(0x02);
cx.encoder.u32(index);
}
offset.emit(&mut cx);
cx.encoder.bytes(data);
}
for data in self.iter_used(cx.used) {
cx.encoder.byte(0x01);
cx.encoder.bytes(&data.value);
}
}
}