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
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Open Ethereum.

// Open Ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Open Ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Open Ethereum.  If not, see <http://www.gnu.org/licenses/>.

use crate::instructions::{self, Instruction};
use bit_set::BitSet;
use cfx_types::H256;
use keccak_hash::KECCAK_EMPTY;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use memory_cache::MemoryLruCache;
use parking_lot::Mutex;
use std::sync::Arc;

#[cfg(test)]
use rustc_hex::FromHex;

const DEFAULT_CACHE_SIZE: usize = 4 * 1024 * 1024;

/// Stub for a sharing `BitSet` data in cache (reference counted)
/// and implementing MallocSizeOf on it.
#[derive(Clone)]
struct Bits(Arc<BitSet>);

impl MallocSizeOf for Bits {
    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
        // dealing in bits here
        self.0.capacity() * 8
    }
}

#[derive(Clone)]
struct CacheItem {
    jump_destination: Bits,
    sub_entrypoint: Bits,
}

impl MallocSizeOf for CacheItem {
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        self.jump_destination.size_of(ops) + self.sub_entrypoint.size_of(ops)
    }
}

/// Global cache for EVM interpreter
pub struct SharedCache<const CANCUN: bool> {
    jump_destinations: Mutex<MemoryLruCache<H256, CacheItem>>,
}

impl<const CANCUN: bool> SharedCache<CANCUN> {
    /// Create a jump destinations cache with a maximum size in bytes
    /// to cache.
    pub fn new(max_size: usize) -> Self {
        SharedCache {
            jump_destinations: Mutex::new(MemoryLruCache::new(max_size)),
        }
    }

    /// Get jump destinations bitmap for a contract.
    pub fn jump_and_sub_destinations(
        &self, code_hash: &H256, code: &[u8],
    ) -> (Arc<BitSet>, Arc<BitSet>) {
        if code_hash == &KECCAK_EMPTY {
            let cache_item = Self::find_jump_and_sub_destinations(code);
            return (
                cache_item.jump_destination.0,
                cache_item.sub_entrypoint.0,
            );
        }

        if let Some(d) = self.jump_destinations.lock().get_mut(code_hash) {
            return (d.jump_destination.0.clone(), d.sub_entrypoint.0.clone());
        }

        let d = Self::find_jump_and_sub_destinations(code);
        self.jump_destinations.lock().insert(*code_hash, d.clone());

        (d.jump_destination.0, d.sub_entrypoint.0)
    }

    fn find_jump_and_sub_destinations(code: &[u8]) -> CacheItem {
        let mut jump_dests = BitSet::with_capacity(code.len());
        let mut sub_entrypoints = BitSet::with_capacity(code.len());
        let mut position = 0;

        while position < code.len() {
            let instruction = Instruction::from_u8(code[position]);

            if let Some(instruction) = instruction {
                match instruction {
                    instructions::JUMPDEST => {
                        jump_dests.insert(position);
                    }
                    instructions::BEGINSUB_TLOAD if !CANCUN => {
                        sub_entrypoints.insert(position);
                    }
                    _ => {
                        if let Some(push_bytes) = instruction.push_bytes() {
                            position += push_bytes;
                        }
                    }
                }
            }
            position += 1;
        }

        jump_dests.shrink_to_fit();
        CacheItem {
            jump_destination: Bits(Arc::new(jump_dests)),
            sub_entrypoint: Bits(Arc::new(sub_entrypoints)),
        }
    }
}

impl<const CANCUN: bool> Default for SharedCache<CANCUN> {
    fn default() -> Self { SharedCache::new(DEFAULT_CACHE_SIZE) }
}

#[test]
fn test_find_jump_destinations() {
    // given

    // 0000 7F   PUSH32
    // 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    // 0021 7F   PUSH32
    // 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    // 0042 5B   JUMPDEST
    // 0043 01   ADD
    // 0044 60   PUSH1 0x00
    // 0046 55   SSTORE
    let code: Vec<u8> = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b01600055".from_hex().unwrap();

    // when
    let cache_item =
        SharedCache::<false>::find_jump_and_sub_destinations(&code);

    // then
    assert!(cache_item
        .jump_destination
        .0
        .iter()
        .eq(vec![66].into_iter()));
    assert!(cache_item.sub_entrypoint.0.is_empty());
}

#[test]
fn test_find_jump_destinations_not_in_data_segments() {
    // given

    // 0000 60 06   PUSH1 06
    // 0002 56      JUMP
    // 0003 50 5B   PUSH1 0x5B
    // 0005 56      STOP
    // 0006 5B      JUMPDEST
    // 0007 60 04   PUSH1 04
    // 0009 56      JUMP
    let code: Vec<u8> = "600656605B565B6004".from_hex().unwrap();

    // when
    let cache_item =
        SharedCache::<false>::find_jump_and_sub_destinations(&code);

    // then
    assert!(cache_item.jump_destination.0.iter().eq(vec![6].into_iter()));
    assert!(cache_item.sub_entrypoint.0.is_empty());
}

#[test]
fn test_find_sub_entrypoints() {
    // given

    // see https://eips.ethereum.org/EIPS/eip-2315 for disassembly
    let code: Vec<u8> =
        "6800000000000000000c5e005c60115e5d5c5d".from_hex().unwrap();

    // when
    let cache_item =
        SharedCache::<false>::find_jump_and_sub_destinations(&code);

    // then
    assert!(cache_item.jump_destination.0.is_empty());
    assert!(cache_item
        .sub_entrypoint
        .0
        .iter()
        .eq(vec![12, 17].into_iter()));
}

#[test]
fn test_find_jump_and_sub_allowing_unknown_opcodes() {
    // precondition
    assert!(Instruction::from_u8(0xcc) == None);

    // given

    // 0000 5B   JUMPDEST
    // 0001 CC   ???
    // 0002 5C   BEGINSUB
    let code: Vec<u8> = "5BCC5C".from_hex().unwrap();

    // when
    let cache_item =
        SharedCache::<false>::find_jump_and_sub_destinations(&code);

    // then
    assert!(cache_item.jump_destination.0.iter().eq(vec![0].into_iter()));
    assert!(cache_item.sub_entrypoint.0.iter().eq(vec![2].into_iter()));
}