/
allocation.rs
341 lines (277 loc) · 10 KB
/
allocation.rs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::engine::import_resolver::{Import, ImportResolver};
use crate::engine::memory::MemoryInstance;
use crate::engine::store::Store;
use crate::engine::*;
use crate::value::Value;
use wasm_parser::core::*;
use wasm_parser::Module;
use crate::engine::module::ModuleInstance;
use crate::engine::table::TableInstance;
use anyhow::{anyhow, Context, Result};
pub fn allocate(
m: &Module,
mod_instance: &mut ModuleInstance,
store: &mut Store,
imports: &[Import],
) -> Result<()> {
debug!("allocate");
// Step 1
let imports_entries = get_extern_values_in_imports(m);
let imports = create_import_resolver(&imports_entries, imports)?;
// Step 2a and 6
allocate_functions(m, mod_instance, store).context("Allocating function instances failed")?;
//TODO host functions
// Step 3a and 7
allocate_tables(m, mod_instance, store, &imports_entries, &imports)
.context("Allocating table instances failed")?;
// Step 4a and 8
allocate_memories(m, mod_instance, store)?;
// Step 5a and 9
allocate_globals(m, mod_instance, store, &imports)
.context("Allocating global instances failed")?;
// ... Step 13
// Step 14.
allocate_exports(m, mod_instance, store)?;
// Step 15.
Ok(())
}
fn get_extern_values_in_imports(m: &Module) -> Vec<&ImportEntry> {
m.sections
.iter()
.filter_map(|ref w| match w {
Section::Import(t) => Some(&t.entries),
_ => None,
})
.flatten()
.collect()
}
fn create_import_resolver(_entries: &[&ImportEntry], imports: &[Import]) -> Result<ImportResolver> {
debug!("match imports");
let mut resolver = ImportResolver::new();
for entry in imports.iter() {
//TODO add more types
match entry {
Import::Global(module, name, instance) => {
debug!("=> Injecting global import");
resolver
.inject_global(module.clone(), name.clone(), instance)
.with_context(|| format!("Injecting global failed {} {}", module, name))?;
}
Import::Table(module, name, instance) => {
debug!("=> Injecting table import");
resolver
.inject_table(module.clone(), name.clone(), instance)
.with_context(|| format!("Injecting table failed {} {}", module, name))?;
}
}
}
Ok(resolver)
}
fn allocate_functions(
m: &Module,
mod_instance: &mut ModuleInstance,
store: &mut Store,
) -> Result<()> {
debug!("allocate function");
// Gets all functions and imports
let ty = validation::extract::get_funcs(&m);
let num_imports = validation::extract::get_imports(&m).len();
debug!("functions extracted {:#?}", ty);
for (code_index, t) in ty.iter().enumerate() {
debug!("Function {} with ty {:#?}", code_index, t);
// Allocate function
let borrow = &mod_instance;
let fn_sig = match borrow.lookup_func_types(t) {
Some(sig) => sig,
None => {
return Err(anyhow!("{} function type is not defined", t));
}
};
let code_index = code_index as isize - num_imports as isize;
let code = {
if code_index < 0 {
None
} else {
borrow.lookup_code(code_index as usize)
}
};
let fcode = match code {
Some(fcode) => fcode.clone(),
None => {
// This was added for the `ifds` implementation,
// that it can handle partial defined module.
let mut params: Vec<_> = Vec::new();
let mut returns_const: Vec<_> = Vec::new();
for param_ty in fn_sig.param_types.iter() {
params.push(match param_ty {
ValueType::I32 => LocalEntry {
count: 1,
ty: ValueType::I32,
},
ValueType::I64 => LocalEntry {
count: 1,
ty: ValueType::I64,
},
ValueType::F32 => LocalEntry {
count: 1,
ty: ValueType::F32,
},
ValueType::F64 => LocalEntry {
count: 1,
ty: ValueType::F64,
},
});
}
for ret_ty in &fn_sig.return_types {
returns_const.push(match ret_ty {
ValueType::I32 => Instruction::OP_I32_CONST(0),
ValueType::I64 => Instruction::OP_I64_CONST(0),
ValueType::F32 => Instruction::OP_F32_CONST(0.0),
ValueType::F64 => Instruction::OP_F64_CONST(0.0),
});
}
let mut counter = Counter::default();
FunctionBody {
locals: params,
code: InstructionWrapper::wrap_instructions(&mut counter, returns_const),
}
}
};
store.allocate_func_instance(fn_sig.clone(), fcode);
let addr = FuncAddr::new(store.count_functions() - 1);
mod_instance.store_func_addr(addr)?;
}
Ok(())
}
fn allocate_tables(
m: &Module,
mod_instance: &mut ModuleInstance,
store: &mut Store,
imports: &[&ImportEntry],
import_resolver: &ImportResolver,
) -> Result<()> {
debug!("allocate tables");
// Gets all tables and imports
//let ty = validation::extract::get_tables(&m);
let ty = validation::extract::get_defined_tables(&m);
for t in ty.iter() {
debug!("table {:#?}", t);
let instance = match t.limits {
Limits::Zero(n) => TableInstance::new(n, None),
Limits::One(n, m) => TableInstance::new(n, Some(m)),
};
let addr = TableAddr::new(store.tables.len());
mod_instance.store_table_addr(addr)?;
store.tables.push(instance);
}
for entry in imports {
if matches!(entry.desc, ImportDesc::Table { .. }) {
let instance = import_resolver.resolve_table(&entry.module_name, &entry.name)?;
debug!("table {:#?}", instance);
let addr = TableAddr::new(store.tables.len());
mod_instance.store_table_addr(addr)?;
store.tables.push(instance.clone());
}
}
debug!("Tables in store {:#?}", store.tables);
Ok(())
}
fn allocate_memories(m: &Module, mod_instance: &mut ModuleInstance, store: &mut Store) -> Result<()> {
debug!("allocate memories");
// Gets all memories and imports
let ty = validation::extract::get_mems(&m);
for mem_type in ty.iter() {
debug!("mem_type {:#?}", mem_type);
let instance = match mem_type.limits {
Limits::Zero(n) => MemoryInstance {
data: vec![0u8; (n * 1024 * 64) as usize],
max: None,
},
Limits::One(n, m) => MemoryInstance {
data: vec![0u8; (n * 1024 * 64) as usize],
max: Some(m),
},
};
let addr = MemoryAddr::new(store.memory.len());
mod_instance.store_memory_addr(addr)?;
store.memory.push(instance);
}
debug!("Memories in store {:#?}", store.memory);
Ok(())
}
fn allocate_globals(
m: &Module,
mod_instance: &mut ModuleInstance,
store: &mut Store,
imports: &ImportResolver,
) -> Result<()> {
debug!("allocate globals");
// Gets all globals and imports
let defined_globals = validation::extract::get_defined_globals(&m);
let imported_globals = validation::extract::get_imported_globals(&m);
debug!("defined globals {:?}", defined_globals);
debug!("imported globals {:?}", imported_globals);
debug!("imports {:?}", imports);
for gl in defined_globals.iter() {
debug!("global {:#?}", gl);
//TODO move to `allocate_global` in store
let instance = Variable {
mutable: matches!(gl.ty.mu, Mu::Var),
val: get_expr_const_ty_global(&gl.init, &mod_instance, store)?,
};
let addr = GlobalAddr::new(store.globals.len());
mod_instance.store_global_addr(addr)?;
store.globals.push(instance);
}
for gl in imported_globals.iter() {
debug!("global {:#?}", gl);
let addr = GlobalAddr::new(store.globals.len());
mod_instance.store_global_addr(addr)?;
store
.globals
.push(imports.resolve_global(&gl.module_name, &gl.name)?);
}
debug!("Globals in store {:#?}", store.globals);
Ok(())
}
fn allocate_exports(m: &Module, mod_instance: &mut ModuleInstance, _store: &mut Store) -> Result<()> {
debug!("allocate exports");
// Gets all exports
let ty = validation::extract::get_exports(&m);
for export in ty.into_iter() {
debug!("Export {:?}", export);
mod_instance.store_export(export.into())?;
}
Ok(())
}
pub(crate) fn get_expr_const_ty_global(
init: &[InstructionWrapper],
mod_instance: &ModuleInstance,
store: &mut Store,
) -> Result<Value> {
use wasm_parser::core::Instruction::*;
if init.is_empty() {
error!("No expr to evaluate");
return Err(anyhow!("No expr to evaluate"));
}
match init
.get(0)
.context("Cannot access instruction")?
.get_instruction()
{
OP_I32_CONST(v) => Ok(Value::I32(*v)),
OP_I64_CONST(v) => Ok(Value::I64(*v)),
OP_F32_CONST(v) => Ok(Value::F32(*v)),
OP_F64_CONST(v) => Ok(Value::F64(*v)),
OP_GLOBAL_GET(idx) => {
let addr = mod_instance.lookup_global_addr(idx)
.context("Cannot find global addr by index")?;
let global_instance = store.get_global_instance(addr)?;
Ok(global_instance.val)
}
_ => {
error!("Wrong expression");
Err(anyhow!("Wrong expression"))
}
}
}