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
#![deny(warnings)]
#![forbid(unsafe_code, unused_variables, unused_imports)]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate core;
extern crate alloc;
mod memory;
mod stack;
mod valids;
mod opcode;
mod error;
mod eval;
mod utils;
pub use crate::memory::Memory;
pub use crate::stack::Stack;
pub use crate::valids::Valids;
pub use crate::opcode::Opcode;
pub use crate::error::{Trap, Capture, ExitReason, ExitSucceed, ExitError, ExitRevert, ExitFatal};
use core::ops::Range;
use alloc::vec::Vec;
use alloc::rc::Rc;
use primitive_types::U256;
use crate::eval::{eval, Control};
pub struct Machine {
data: Rc<Vec<u8>>,
code: Rc<Vec<u8>>,
position: Result<usize, ExitReason>,
return_range: Range<U256>,
valids: Valids,
memory: Memory,
stack: Stack,
}
impl Machine {
pub fn stack(&self) -> &Stack { &self.stack }
pub fn stack_mut(&mut self) -> &mut Stack { &mut self.stack }
pub fn memory(&self) -> &Memory { &self.memory }
pub fn memory_mut(&mut self) -> &mut Memory { &mut self.memory }
pub fn new(
code: Rc<Vec<u8>>,
data: Rc<Vec<u8>>,
stack_limit: usize,
memory_limit: usize
) -> Self {
let valids = Valids::new(&code[..]);
Self {
data,
code,
position: Ok(0),
return_range: U256::zero()..U256::zero(),
valids,
memory: Memory::new(memory_limit),
stack: Stack::new(stack_limit),
}
}
pub fn exit(&mut self, reason: ExitReason) {
self.position = Err(reason);
}
pub fn inspect(&self) -> Option<(Opcode, &Stack)> {
let position = match self.position {
Ok(position) => position,
Err(_) => return None,
};
self.code.get(position).map(|v| (Opcode(*v), &self.stack))
}
pub fn return_value(&self) -> Vec<u8> {
if self.return_range.start > U256::from(usize::max_value()) {
let mut ret = Vec::new();
ret.resize((self.return_range.end - self.return_range.start).as_usize(), 0);
ret
} else if self.return_range.end > U256::from(usize::max_value()) {
let mut ret = self.memory.get(
self.return_range.start.as_usize(),
usize::max_value() - self.return_range.start.as_usize(),
);
while ret.len() < (self.return_range.end - self.return_range.start).as_usize() {
ret.push(0);
}
ret
} else {
self.memory.get(
self.return_range.start.as_usize(),
(self.return_range.end - self.return_range.start).as_usize(),
)
}
}
pub fn run(&mut self) -> Capture<ExitReason, Trap> {
loop {
match self.step() {
Ok(()) => (),
Err(res) => return res,
}
}
}
#[inline]
pub fn step(&mut self) -> Result<(), Capture<ExitReason, Trap>> {
let position = *self.position.as_ref().map_err(|reason| Capture::Exit(reason.clone()))?;
match self.code.get(position).map(|v| Opcode(*v)) {
Some(opcode) => {
match eval(self, opcode, position) {
Control::Continue(p) => {
self.position = Ok(position + p);
Ok(())
},
Control::Exit(e) => {
self.position = Err(e.clone());
Err(Capture::Exit(e))
},
Control::Jump(p) => {
self.position = Ok(p);
Ok(())
},
Control::Trap(opcode) => {
self.position = Ok(position + 1);
Err(Capture::Trap(opcode))
},
}
},
None => {
self.position = Err(ExitSucceed::Stopped.into());
Err(Capture::Exit(ExitSucceed::Stopped.into()))
},
}
}
}