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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt;
use super::{EvmWord,EvmMemory,EvmStack,EvmStorage};
// ===================================================================
// State
// ===================================================================
/// Describes the state of an EVM at a given point (which could be
/// _running_ or _terminated_). In essence, this simply packages all
/// the key components (e.g. stack, memory, storage) of the EVM state
/// together.
///
/// An `EvmState` can be _concrete_ or _abstract_. For example, a
/// physically executing EVM operates over concrete states which are
/// updated after each executed instruction. In contrast, a static
/// analysis over a sequence of EVM bytecodes produces abstract states
/// at each point which summarise the _set of all possible states_ at
/// that point.
pub trait EvmState : fmt::Debug {
/// Defines what constitutes a word in this EVM. For example, a
/// concrete evm will use a `w256` here whilst an abstract evm
/// will use something that can, for example, describe unknown
/// values.
type Word : EvmWord;
/// Defines the stack implementation used in this EVM.
type Stack : EvmStack<Word=Self::Word>;
/// Defines the memory implementation used in this EVM.
type Memory : EvmMemory<Word=Self::Word>;
/// Defines the memory implementation used in this EVM.
type Storage : EvmStorage<Word=Self::Word>;
/// Get the program counter. Every `EvmState` has a statically
/// known `pc`.
fn pc(&self) -> usize;
/// Get read access to the operand stack contained within this
/// state.
fn stack(&self) -> &Self::Stack;
/// Get write access to the operand stack contained within this
/// state.
fn stack_mut(&mut self) -> &mut Self::Stack;
/// Get read access to the scratch memory contained within this
/// state.
fn memory(&self) -> &Self::Memory;
/// Get write access to the scratch memory contained within this
/// state.
fn memory_mut(&mut self) -> &mut Self::Memory;
/// Get read access to the persistent storage contained within
/// this state.
fn storage(&self) -> &Self::Storage;
/// Get write access to the persistent storage contained within
/// this state.
fn storage_mut(&mut self) -> &mut Self::Storage;
/// Move _program counter_ over `n` bytes in the next instruction.
fn skip(&mut self, n: usize);
/// Move _program counter_ to a given (byte) offset within the
/// code section.
fn goto(&mut self, pc: usize);
}
// ===================================================================
// Concrete State
// ===================================================================
/// An `EvmState` composed from three distinct (and potentially
/// abstract) components: _stack_, _memory_ and _storage_.
#[derive(Clone,Debug,PartialEq)]
pub struct ConcreteState<S,M,T>
where S:EvmStack,
M:EvmMemory<Word=S::Word>,
T:EvmStorage<Word=S::Word>
{
pc: usize,
stack: S,
memory: M,
storage: T
}
impl<S,M,T> ConcreteState<S,M,T>
where S:EvmStack+Default,
M:EvmMemory<Word=S::Word>+Default,
T:EvmStorage<Word=S::Word>+Default
{
pub fn new() -> Self {
let stack = S::default();
let memory = M::default();
let storage = T::default();
Self{pc:0,stack,memory,storage}
}
}
impl<S,M,T> EvmState for ConcreteState<S,M,T>
where S:EvmStack,
M:EvmMemory<Word=S::Word>,
T:EvmStorage<Word=S::Word>
{
type Word = S::Word;
type Stack = S;
type Memory = M;
type Storage = T;
fn pc(&self) -> usize {
self.pc
}
fn stack(&self) -> &Self::Stack {
&self.stack
}
fn memory(&self) -> &Self::Memory {
&self.memory
}
fn storage(&self) -> &Self::Storage {
&self.storage
}
fn stack_mut(&mut self) -> &mut Self::Stack {
&mut self.stack
}
fn memory_mut(&mut self) -> &mut Self::Memory {
&mut self.memory
}
fn storage_mut(&mut self) -> &mut Self::Storage {
&mut self.storage
}
fn skip(&mut self, n: usize) {
self.pc += n;
self.stack.goto(self.pc);
}
/// Move _program counter_ to a given (byte) offset within the
/// code section.
fn goto(&mut self, pc: usize) {
self.pc = pc;
self.stack.goto(pc);
}
}
impl<S,M,T> fmt::Display for ConcreteState<S,M,T>
where S:EvmStack+Default+fmt::Display,
M:EvmMemory<Word=S::Word>+Default+fmt::Display,
T:EvmStorage<Word=S::Word>+Default
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,"|{}|{}|",self.stack,self.memory)?;
Ok(())
}
}