evmil/analysis/storage.rs
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12use std::fmt;
13use std::marker::PhantomData;
14use crate::util::Top;
15use super::{EvmWord};
16
17/// Abstraction of peristent storage within an EVM. This provides the
18/// minimal set of operations required to implement the semantics of a
19/// given bytecode instruction. For example, reading/writing from
20/// storage.
21pub trait EvmStorage : fmt::Debug {
22 /// Defines what constitutes a word in this EVM. For example, a
23 /// concrete evm will use a `w256` here whilst an abstract evm
24 /// will use something that can, for example, describe unknown
25 /// values.
26 type Word : EvmWord;
27
28 /// Get the word at a given location in storage.
29 fn get(&mut self, address: Self::Word) -> Self::Word;
30
31 /// Put a given value at a given location in storage.
32 fn put(&mut self, address: Self::Word, item: Self::Word);
33}
34
35// ===================================================================
36// Unknown Storage
37// ===================================================================
38
39/// The simplest possible implementation of `EvmStorage` which simply
40/// returns "unknown" for every location. In other words, it doesn't
41/// actually analyse storage at all.
42#[derive(Clone,Eq,Ord,PartialEq,PartialOrd)]
43pub struct UnknownStorage<T:EvmWord+Top> {
44 dummy: PhantomData<T>
45}
46
47impl<T:EvmWord+Top> UnknownStorage<T> {
48 pub fn new() -> Self { Self{dummy: PhantomData} }
49}
50
51impl<T:EvmWord+Top> EvmStorage for UnknownStorage<T> {
52 type Word = T;
53
54 fn get(&mut self, _address: Self::Word) -> Self::Word {
55 T::TOP
56 }
57
58 fn put(&mut self, _address: Self::Word, _item: Self::Word) {
59 // no op (for now)
60 }
61}
62
63impl<T:EvmWord+Top> Default for UnknownStorage<T> {
64 fn default() -> Self {
65 Self::new()
66 }
67}
68
69
70impl<T:EvmWord+Top> fmt::Display for UnknownStorage<T>
71{
72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73 write!(f,"???")?;
74 Ok(())
75 }
76}
77
78impl<T:EvmWord+Top> fmt::Debug for UnknownStorage<T>
79{
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 write!(f,"???")?;
82 Ok(())
83 }
84}