forked from paritytech/polkavm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
381 lines (333 loc) · 10.1 KB
/
lib.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#![allow(non_snake_case)]
use std::sync::Mutex;
use polkavm::{ArcBytes, Engine, InterruptKind, Module, ModuleConfig, ProgramBlob, ProgramCounter, RawInstance, Reg};
use polkavm_common::program::ProgramParts;
use wasm_bindgen::prelude::wasm_bindgen;
#[repr(C)]
#[wasm_bindgen]
#[derive(Copy, Clone, Debug)]
pub enum Status {
Ok = 255,
Halt = 0,
Panic = 1,
Fault = 2,
Host = 3,
OutOfGas = 4,
}
static PVM: Mutex<Option<RawInstance>> = Mutex::new(None);
static STATUS: Mutex<Status> = Mutex::new(Status::Ok);
static EXIT_ARG: Mutex<u32> = Mutex::new(0);
const NO_OF_REGISTERS: usize = 13;
const BYTES_PER_REG: usize = 8;
const PAGE_SIZE: usize = 4_096;
fn with_pvm<F, R>(f: F, default: R) -> R where F: FnMut(&mut RawInstance) -> R {
let pvm_l = PVM.lock();
if let Ok(mut pvm_l) = pvm_l {
pvm_l.as_mut().map(f).unwrap_or(default)
} else {
default
}
}
#[deprecated = "Use setGasLeft / setNextProgramCounter instead."]
#[wasm_bindgen]
pub fn resume(pc: u32, gas: i64) {
with_pvm(|pvm| {
pvm.set_gas(gas);
pvm.set_next_program_counter(ProgramCounter(pc));
}, ());
}
#[deprecated = "Use resetGeneric instead"]
#[wasm_bindgen]
pub fn reset(program: Vec<u8>, registers: Vec<u8>, gas: i64) {
resetGeneric(
program,
registers,
gas,
)
}
#[wasm_bindgen]
pub fn resetGeneric(
program: Vec<u8>,
registers: Vec<u8>,
gas: i64,
) {
resetGenericWithMemory(program, registers, vec![], vec![], gas);
}
#[wasm_bindgen]
pub fn resetGenericWithMemory(
program: Vec<u8>,
registers: Vec<u8>,
page_map: Vec<u8>,
chunks: Vec<u8>,
gas: i64,
) {
let mut config = polkavm::Config::new();
config.set_backend(Some(polkavm::BackendKind::Interpreter));
let engine = Engine::new(&config).unwrap();
let mut module_config = ModuleConfig::default();
module_config.set_strict(true);
module_config.set_gas_metering(Some(polkavm::GasMeteringKind::Sync));
module_config.set_step_tracing(true);
let mut parts = ProgramParts::default();
parts.is_64_bit = true;
parts.code_and_jump_table = program.into();
setup_memory(&mut parts, page_map, chunks);
let blob = ProgramBlob::from_parts(parts).unwrap();
let module = Module::from_blob(&engine, &module_config, blob).unwrap();
let mut instance = module.instantiate().unwrap();
instance.set_gas(gas);
instance.set_next_program_counter(ProgramCounter(0));
for (i, reg) in (0..NO_OF_REGISTERS).zip(Reg::ALL) {
let start_bytes = i * BYTES_PER_REG;
let reg_value = read_u64(®isters, start_bytes);
instance.set_reg(reg, reg_value);
}
*PVM.lock().unwrap() = Some(instance);
nextStep();
}
#[wasm_bindgen]
pub fn nextStep() -> bool {
let (can_continue, status) = with_pvm(|pvm| {
match pvm.run() {
Ok(InterruptKind::Finished) => {
(false, Status::Halt)
},
Ok(InterruptKind::Trap) => {
(false, Status::Panic)
},
Ok(InterruptKind::Ecalli(call)) => {
*EXIT_ARG.lock().unwrap() = call;
(false, Status::Host)
},
Ok(InterruptKind::Segfault(page)) => {
*EXIT_ARG.lock().unwrap() = page.page_address;
(false, Status::Fault)
},
Ok(InterruptKind::NotEnoughGas) => {
(false, Status::OutOfGas)
},
Ok(InterruptKind::Step) => {
(true, Status::Ok)
},
Err(e) => {
eprintln!("Error: {:?}", e);
(false, Status::Panic)
},
}
}, (false, Status::Panic));
*STATUS.lock().unwrap() = status;
can_continue
}
#[wasm_bindgen]
pub fn nSteps(steps: u32) -> bool {
for _ in 0..steps {
if !nextStep() {
return false;
}
}
return true;
}
#[wasm_bindgen]
pub fn getProgramCounter() -> u32 {
with_pvm(|pvm| pvm.program_counter().map(|x| x.0).unwrap_or(0), 0)
}
#[wasm_bindgen]
pub fn setNextProgramCounter(pc: u32) {
with_pvm(|pvm| pvm.set_next_program_counter(ProgramCounter(pc)), ());
}
#[wasm_bindgen]
pub fn getStatus() -> u8 {
let status = *STATUS.lock().unwrap();
status as u8
}
#[wasm_bindgen]
pub fn getExitArg() -> u32 {
*EXIT_ARG.lock().unwrap()
}
#[wasm_bindgen]
pub fn getGasLeft() -> i64 {
with_pvm(|pvm| pvm.gas(), 0)
}
#[wasm_bindgen]
pub fn setGasLeft(gas: i64) {
with_pvm(|pvm| pvm.set_gas(gas), ());
}
#[wasm_bindgen]
pub fn getRegisters() -> Vec<u8> {
let mut registers = vec![0u8; NO_OF_REGISTERS * BYTES_PER_REG];
with_pvm(|pvm| {
for (i, reg) in (0..NO_OF_REGISTERS).zip(Reg::ALL) {
let start_byte = i * BYTES_PER_REG;
let val_le_bytes = pvm.reg(reg).to_le_bytes();
registers[start_byte..start_byte +BYTES_PER_REG].copy_from_slice(&val_le_bytes);
}
}, ());
registers
}
#[wasm_bindgen]
pub fn setRegisters(registers: Vec<u8>) {
with_pvm(|pvm| {
for (i, reg) in (0..NO_OF_REGISTERS).zip(Reg::ALL) {
let start_bytes = i * BYTES_PER_REG;
let reg_value = read_u64(®isters, start_bytes);
pvm.set_reg(reg, reg_value);
}
}, ());
}
#[wasm_bindgen]
pub fn getPageDump(index: u32) -> Vec<u8> {
with_pvm(|pvm| {
let address = index * PAGE_SIZE as u32;
let page = pvm
.read_memory(address, PAGE_SIZE as u32)
.unwrap_or_else(|_| vec![0; PAGE_SIZE]);
page
}, vec![0; PAGE_SIZE])
}
#[wasm_bindgen]
pub fn setMemory(address: u32, data: Vec<u8>) {
with_pvm(|pvm| {
let _ = pvm.write_memory(address, &data);
}, ());
}
pub fn setup_memory(
parts: &mut ProgramParts,
page_map: Vec<u8>,
chunks: Vec<u8>,
) {
let pages = read_pages(page_map);
let chunks = read_chunks(chunks);
let mut ro_start = None;
let mut rw_start = None;
let mut stack_start = None;
for page in pages {
if page.is_writable {
if rw_start.is_some() {
if stack_start.is_some() { panic!("Can't set STACK/RW memory twice"); }
parts.stack_size = page.length;
stack_start = Some(page.address);
} else {
parts.rw_data_size = page.length;
rw_start = Some(page.address);
}
} else {
if ro_start.is_some() { panic!("Can't set RO memory twice"); }
parts.ro_data_size = page.length;
ro_start = Some(page.address);
}
}
let mut ro_data = vec![0; parts.ro_data_size as usize];
let mut rw_data = vec![0; parts.rw_data_size as usize];
let copy_chunk = |chunk: &Chunk, start, size, into: &mut Vec<u8>| {
if let Some(start) = start {
if chunk.address >= start {
let rel_address = chunk.address - start;
if rel_address < size {
let rel_address = rel_address as usize;
let rel_end = rel_address + chunk.data.len();
into[rel_address .. rel_end].copy_from_slice(&chunk.data);
return true;
}
}
}
false
};
if let Some(ro_start) = ro_start {
if ro_start != 0x10000 {
panic!("Unsupported address of RO data.");
}
}
for chunk in chunks {
let is_in_ro = copy_chunk(&chunk, ro_start, parts.ro_data_size, &mut ro_data);
let is_in_rw = copy_chunk(&chunk, rw_start, parts.rw_data_size, &mut rw_data);
if !is_in_ro && !is_in_rw {
panic!("Invalid chunk!");
}
}
parts.ro_data = ArcBytes::from(ro_data);
parts.rw_data = ArcBytes::from(rw_data);
}
fn read_u32(source: &[u8], index: usize) -> u32 {
let mut val = [0u8; 4];
val.copy_from_slice(&source[index .. index + 4]);
u32::from_le_bytes(val)
}
fn read_u64(source: &[u8], index: usize) -> u64 {
let mut val = [0u8; 8];
val.copy_from_slice(&source[index .. index + 8]);
u64::from_le_bytes(val)
}
/// Page Map is defined in JAM codec lingo as: `sequence(tuple(u32, u32, bool))`
fn read_pages(page_map: Vec<u8>) -> Vec<Page> {
let mut pages = vec![];
let mut index = 0;
while index < page_map.len() {
let address = read_u32(&page_map, index);
index += 4;
let length = read_u32(&page_map, index);
index += 4;
let is_writable = page_map[index] > 0;
index += 1;
pages.push(Page {
address, length, is_writable
});
}
pages
}
/// Chunks is defined in JAM codec lingo as: `sequence(tuple(u32, u32, bytes))`
fn read_chunks(chunks: Vec<u8>) -> Vec<Chunk> {
let mut res = vec![];
let mut index = 0;
while index < chunks.len() {
let address = read_u32(&chunks, index);
index += 4;
let length = read_u32(&chunks, index) as usize;
index += 4;
let data = chunks[index .. index + length].to_vec();
res.push(Chunk {
address,
data,
});
index += length;
}
res
}
struct Page {
address: u32,
length: u32,
is_writable: bool,
}
struct Chunk {
address: u32,
data: Vec<u8>,
}
#[cfg(test)]
mod tests {
use super::*;
const FIB: &[u8] = &[
0,0,33,4,8,1,4,9,1,5,3,0,2,119,255,7,7,12,82,138,8,152,8,82,169,5,243,82,135,4,8,4,9,17,19,0,73,147,82,213,0];
#[test]
fn run_simple_program() {
let program = FIB.to_vec();
let mut registers = vec![0u8; 13 * 4];
registers[7] = 9;
resetGeneric(program, registers, 10_000);
loop {
let can_continue = nextStep();
println!("Status: {:?}, PC: {}", getStatus(), getProgramCounter());
if !can_continue {
break;
}
}
}
#[test]
fn should_change_pc_after_first_step() {
let program = FIB.to_vec();
let mut registers = vec![0u8; 13 * 4];
registers[7] = 9;
resetGeneric(program, registers, 10_000);
assert_eq!(getProgramCounter(), 0);
nextStep();
assert_eq!(getProgramCounter(), 3);
}
}