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
use super::super::instructions::{self, Instruction};
use bit_set::BitSet;
use ethereum_types::H256;
use hash::KECCAK_EMPTY;
use memory_cache::MemoryLruCache;
use parity_util_mem::{MallocSizeOf, MallocSizeOfOps};
use parking_lot::Mutex;
use std::sync::Arc;
const DEFAULT_CACHE_SIZE: usize = 4 * 1024 * 1024;
#[derive(Clone)]
struct Bits(Arc<BitSet>);
impl MallocSizeOf for Bits {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
self.0.capacity() * 8
}
}
#[derive(MallocSizeOf, Clone)]
struct CacheItem {
jump_destination: Bits,
sub_entrypoint: Bits,
}
pub struct SharedCache {
jump_destinations: Mutex<MemoryLruCache<H256, CacheItem>>,
}
impl SharedCache {
pub fn new(max_size: usize) -> Self {
SharedCache {
jump_destinations: Mutex::new(MemoryLruCache::new(max_size)),
}
}
pub fn jump_and_sub_destinations(
&self,
code_hash: &Option<H256>,
code: &[u8],
) -> (Arc<BitSet>, Arc<BitSet>) {
if let Some(ref code_hash) = code_hash {
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);
if let Some(ref code_hash) = code_hash {
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 => {
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 Default for SharedCache {
fn default() -> Self {
SharedCache::new(DEFAULT_CACHE_SIZE)
}
}
#[cfg(test)]
mod test {
use super::*;
use hex_literal::hex;
#[test]
fn test_find_jump_destinations() {
let code = hex!("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b01600055");
let cache_item = SharedCache::find_jump_and_sub_destinations(&code);
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() {
let code = hex!("600656605B565B6004");
let cache_item = SharedCache::find_jump_and_sub_destinations(&code);
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() {
let code = hex!("6800000000000000000c5e005c60115e5d5c5d");
let cache_item = SharedCache::find_jump_and_sub_destinations(&code);
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() {
assert!(Instruction::from_u8(0xcc) == None);
let code = hex!("5BCC5C");
let cache_item = SharedCache::find_jump_and_sub_destinations(&code);
assert!(cache_item.jump_destination.0.iter().eq(vec![0].into_iter()));
assert!(cache_item.sub_entrypoint.0.iter().eq(vec![2].into_iter()));
}
}