-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecutor.rs
More file actions
417 lines (359 loc) · 13.1 KB
/
executor.rs
File metadata and controls
417 lines (359 loc) · 13.1 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::sync::Arc;
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::events::*;
use crate::instruction::Instruction;
use crate::opcode::Opcode;
use crate::program::Program;
use crate::record::{ExecutionRecord, MemoryAccessRecord};
use crate::state::ExecutionState;
/// The default increment for the program counter. Is used for all instructions except
/// for branches and jumps.
pub const DEFAULT_PC_INC: u32 = 1;
/// An executor for the zkVM.
///
/// The executor is responsible for executing a user program and tracing important events which
/// occur during execution (i.e., memory reads, alu operations, etc).
#[derive(Default)]
pub struct Executor {
/// The program.
pub program: Arc<Program>,
/// The state of the execution.
pub state: ExecutionState,
/// The current trace of the execution that is being collected.
pub record: ExecutionRecord,
/// The memory accesses for the current cycle.
pub memory_accesses: MemoryAccessRecord,
/// Memory access events.
pub memory_events: HashMap<u32, MemoryEvent>,
}
/// Errors that the [`Executor`] can throw.
#[derive(Error, Debug, Serialize, Deserialize)]
pub enum ExecutionError {
/// An error occurred while executing the program.
#[error("Execution error: {0}")]
ExecutionError(String),
/// An error occurred while reading from memory.
#[error("Memory read error: {0}")]
MemoryReadError(String),
/// An error occurred while writing to memory.
#[error("Memory write error: {0}")]
MemoryWriteError(String),
}
impl Executor {
/// Create a new [`Executor`] from a program and options.
#[must_use]
pub fn new(program: Program, input: Vec<u8>) -> Self {
// Create a shared reference to the program.
let program = Arc::new(program);
// Create a default record with the program.
let record = ExecutionRecord::new(program.clone());
Self { program, record, state: ExecutionState::new(input), ..Default::default() }
}
/// Executes the program.
/// This function will return an error if the program execution fails.
pub fn run(&mut self) -> Result<(), ExecutionError> {
while !self.execute_cycle()? {}
for (_, event) in self.memory_events.drain() {
self.record.cpu_memory_access.push(event);
}
Ok(())
}
/// Executes one cycle of the program, returning whether the program has finished.
#[inline]
#[allow(clippy::too_many_lines)]
fn execute_cycle(&mut self) -> Result<bool, ExecutionError> {
// Fetch the instruction at the current program counter.
let instruction = self.fetch();
// Execute the instruction.
self.execute_instruction(&instruction)?;
// Increment the clock.
self.state.global_clk += 1;
let done = self.state.pc == self.program.instructions.len() as u32;
Ok(done)
}
/// Fetch the instruction at the current program counter.
#[inline]
fn fetch(&self) -> Instruction {
self.program.fetch(self.state.pc)
}
/// Execute the given instruction over the current state of the runtime.
#[allow(clippy::too_many_lines)]
fn execute_instruction(&mut self, instruction: &Instruction) -> Result<(), ExecutionError> {
let mut next_pc = self.state.pc.wrapping_add(1);
let mut jmp_dst: u32 = 0;
let mut next_mv: u8 = 0;
let mut mv: u8 = 0;
let mp = self.state.mem_ptr;
// Execute the instruction.
match instruction.opcode {
Opcode::MemStepForward | Opcode::MemStepBackward => self.execute_memory(instruction),
Opcode::Add | Opcode::Sub => (next_mv, mv) = self.execute_alu(instruction),
Opcode::LoopStart | Opcode::LoopEnd => {
(mv, next_pc) = self.execute_jump(instruction);
jmp_dst = next_pc;
}
Opcode::Input | Opcode::Output => mv = self.execute_io(instruction),
}
self.emit_events(next_pc, instruction, jmp_dst, mp, next_mv, mv);
// Update the program counter.
self.state.pc = next_pc;
// Update the clk to the next cycle.
self.state.clk += 2;
Ok(())
}
/// Execute a memory instruction.
fn execute_memory(&mut self, instruction: &Instruction) {
let mp = match instruction.opcode {
Opcode::MemStepForward => self.state.mem_ptr.wrapping_add(1),
Opcode::MemStepBackward => self.state.mem_ptr.wrapping_sub(1),
_ => unreachable!(),
};
self.state.mem_ptr = mp;
}
/// Execute an ALU instruction.
fn execute_alu(&mut self, instruction: &Instruction) -> (u8, u8) {
let mv = self.rr_cpu(self.state.mem_ptr, self.state.clk + 1);
let next_mv = match instruction.opcode {
Opcode::Add => mv.wrapping_add(1),
Opcode::Sub => mv.wrapping_sub(1),
_ => unreachable!(),
};
self.rw_cpu(self.state.mem_ptr, next_mv, self.state.clk + 2, true);
(next_mv, mv)
}
/// Execute a jump instruction.
fn execute_jump(&mut self, instruction: &Instruction) -> (u8, u32) {
let mv = self.rr_cpu(self.state.mem_ptr, self.state.clk + 1);
let next_pc = match instruction.opcode {
Opcode::LoopStart => {
if mv == 0 {
instruction.op_a
} else {
self.state.pc.wrapping_add(1)
}
}
Opcode::LoopEnd => {
if mv != 0 {
instruction.op_a
} else {
self.state.pc.wrapping_add(1)
}
}
_ => unreachable!(),
};
(mv, next_pc)
}
/// Execute an IO instruction.
fn execute_io(&mut self, instruction: &Instruction) -> u8 {
match instruction.opcode {
Opcode::Input => {
let input = self.state.input_stream[self.state.input_stream_ptr];
self.rw_cpu(self.state.mem_ptr, input, self.state.clk + 1, false);
input
}
Opcode::Output => {
let output = self.rr_cpu(self.state.mem_ptr, self.state.clk + 1);
self.state.output_stream.push(output);
output
}
_ => unreachable!(),
}
}
/// Emit events for this cycle.
#[allow(clippy::too_many_arguments)]
fn emit_events(
&mut self,
next_pc: u32,
instruction: &Instruction,
jmp_dst: u32,
mp: u32,
next_mv: u8,
mv: u8,
) {
self.record.cpu_events.push(CpuEvent {
clk: self.state.clk,
pc: self.state.pc,
next_pc,
mp,
next_mp: self.state.mem_ptr,
next_mv,
mv,
next_mv_access: self.memory_accesses.next_mv,
mv_access: self.memory_accesses.mv,
});
if instruction.is_alu_instruction() {
self.record.add_events.push(AluEvent::new(
self.state.pc,
instruction.opcode,
next_mv,
mv,
));
}
if instruction.is_jump_instruction() {
self.record.jump_events.push(JumpEvent::new(
self.state.pc,
next_pc,
instruction.opcode,
jmp_dst,
mv,
));
}
if instruction.is_memory_instruction() {
self.record.memory_instr_events.push(MemInstrEvent::new(
self.state.clk,
self.state.pc,
instruction.opcode,
mp,
self.state.mem_ptr,
));
}
if instruction.is_io_instruction() {
self.record.io_events.push(IoEvent::new(self.state.pc, instruction.opcode, mp, mv));
}
self.memory_accesses.mv = None;
self.memory_accesses.next_mv = None;
}
/// Read the memory register.
#[inline]
pub fn rr_cpu(&mut self, addr: u32, timestamp: u32) -> u8 {
// Read the address from memory and create a memory read record if in trace mode.
let record = self.rr_traced(addr, timestamp);
self.memory_accesses.mv = Some(record.into());
record.value
}
/// Write to a register.
pub fn rw_cpu(&mut self, register: u32, value: u8, timestamp: u32, is_alu: bool) {
// Read the address from memory and create a memory read record.
let record = self.rw_traced(register, value, timestamp);
if is_alu {
self.memory_accesses.next_mv = Some(record.into());
} else {
self.memory_accesses.mv = Some(record.into());
}
}
/// Read a register and create an access record.
pub fn rr_traced(&mut self, addr: u32, timestamp: u32) -> MemoryReadRecord {
let record: &mut MemoryRecord =
self.state.memory_access.entry(addr).or_insert(MemoryRecord { value: 0, timestamp: 0 });
let prev_record = *record;
record.timestamp = timestamp;
self.memory_events
.entry(addr)
.and_modify(|e| {
e.final_mem_access = *record;
})
.or_insert(MemoryEvent {
addr,
initial_mem_access: prev_record,
final_mem_access: *record,
});
// Construct the memory read record.
MemoryReadRecord {
value: record.value,
timestamp: record.timestamp,
prev_timestamp: prev_record.timestamp,
}
}
/// Write a word to a register and create an access record.
pub fn rw_traced(&mut self, addr: u32, value: u8, timestamp: u32) -> MemoryWriteRecord {
let record: &mut MemoryRecord =
self.state.memory_access.entry(addr).or_insert(MemoryRecord { value: 0, timestamp: 0 });
let prev_record = *record;
record.value = value;
record.timestamp = timestamp;
self.memory_events
.entry(addr)
.and_modify(|e| {
e.final_mem_access = *record;
})
.or_insert(MemoryEvent {
addr,
initial_mem_access: prev_record,
final_mem_access: *record,
});
// Construct the memory write record.
MemoryWriteRecord {
value: record.value,
timestamp: record.timestamp,
prev_value: prev_record.value,
prev_timestamp: prev_record.timestamp,
}
}
}
#[cfg(test)]
mod tests {
use test_artifacts::{FIBO_BF, HELLO_BF, LOOP_BF, MOVE_BF, PRINTA_BF};
use crate::executor::Executor;
use crate::program::Program;
#[test]
fn test_add_sub_run() {
let program = Program::from("++-.").unwrap();
let mut runtime = Executor::new(program, vec![]);
runtime.run().unwrap();
assert_eq!(1, runtime.state.output_stream[0]);
}
#[test]
fn test_mem_run() {
let program = Program::from(">><").unwrap();
let mut runtime = Executor::new(program, vec![]);
runtime.run().unwrap();
assert_eq!(1, runtime.state.mem_ptr);
}
#[test]
fn test_jmp_run() {
let program = Program::from("[----]").unwrap();
let mut runtime = Executor::new(program, vec![1]);
runtime.run().unwrap();
assert_eq!(2, runtime.state.global_clk);
}
#[test]
fn test_io_run() {
let program = Program::from(",.").unwrap();
let mut runtime = Executor::new(program, vec![1]);
runtime.run().unwrap();
assert_eq!(1, runtime.state.output_stream[0]);
}
#[test]
fn test_printa_run() {
let program = Program::from(PRINTA_BF).unwrap();
let mut runtime = Executor::new(program, vec![]);
runtime.run().unwrap();
assert_eq!(b'A', runtime.state.output_stream[0]);
}
#[test]
fn test_move_run() {
let program = Program::from(MOVE_BF).unwrap();
let mut runtime = Executor::new(program, vec![]);
runtime.run().unwrap();
assert_eq!(2, runtime.state.output_stream[0]);
assert_eq!(0, runtime.state.output_stream[1]);
}
#[test]
fn test_loop_run() {
let program = Program::from(LOOP_BF).unwrap();
let mut runtime = Executor::new(program, vec![]);
runtime.run().unwrap();
assert_eq!(9, runtime.state.pc);
assert_eq!(0_u8, runtime.state.output_stream[0]);
}
#[test]
fn test_hello_run() {
let program = Program::from(HELLO_BF).unwrap();
let mut runtime = Executor::new(program, vec![]);
runtime.run().unwrap();
assert_eq!(b'H', runtime.state.output_stream[0]);
assert_eq!(b'e', runtime.state.output_stream[1]);
assert_eq!(b'l', runtime.state.output_stream[2]);
assert_eq!(b'l', runtime.state.output_stream[3]);
assert_eq!(b'o', runtime.state.output_stream[4]);
}
#[test]
fn test_fibo_run() {
let program = Program::from(FIBO_BF).unwrap();
let mut runtime = Executor::new(program, vec![17]);
runtime.run().unwrap();
assert_eq!(85, runtime.state.output_stream[0]);
}
}