forked from anza-xyz/sbpf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjit.rs
More file actions
175 lines (167 loc) · 6.79 KB
/
Copy pathjit.rs
File metadata and controls
175 lines (167 loc) · 6.79 KB
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
#![allow(clippy::literal_string_with_formatting_args)]
#![cfg(all(test, target_arch = "x86_64", not(target_os = "windows")))]
use byteorder::{ByteOrder, LittleEndian};
use solana_sbpf::{
disassembler::disassemble_instruction,
ebpf,
elf::Executable,
error::EbpfError,
jit::{
MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT, MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH,
MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION,
},
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
static_analysis::CfgNode,
vm::Config,
};
use std::{collections::BTreeMap, sync::Arc};
use test_utils::{syscalls, TestContextObject};
fn create_mockup_executable(config: Config, program: &[u8]) -> Executable<TestContextObject> {
let sbpf_version = *config.enabled_sbpf_versions.end();
let mut loader = BuiltinProgram::new_loader(config);
loader
.register_function("gather_bytes", syscalls::SyscallGatherBytes::vm)
.unwrap();
let mut function_registry = FunctionRegistry::default();
function_registry
.register_function(8, *b"function_foo", 8)
.unwrap();
Executable::<TestContextObject>::from_text_bytes(
program,
Arc::new(loader),
sbpf_version,
function_registry,
)
.unwrap()
}
#[test]
fn test_code_length_estimate() {
const INSTRUCTION_COUNT: usize = 256;
let mut prog = vec![0; ebpf::INSN_SIZE * INSTRUCTION_COUNT];
for pc in 0..INSTRUCTION_COUNT {
prog[pc * ebpf::INSN_SIZE] = ebpf::ADD64_IMM;
}
let mut empty_program_machine_code_length_per_version = [0; 4];
for sbpf_version in [SBPFVersion::V0, SBPFVersion::V3] {
let empty_program_machine_code_length = {
let config = Config {
noop_instruction_rate: 0,
enabled_sbpf_versions: sbpf_version..=sbpf_version,
..Config::default()
};
let mut executable = create_mockup_executable(config, &prog[0..0]);
Executable::<TestContextObject>::jit_compile(&mut executable).unwrap();
executable
.get_compiled_program()
.unwrap()
.machine_code_length()
};
assert!(empty_program_machine_code_length <= MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH);
empty_program_machine_code_length_per_version[sbpf_version as usize] =
empty_program_machine_code_length;
}
let mut instruction_meter_checkpoint_machine_code_length = [0; 2];
for (index, machine_code_length) in instruction_meter_checkpoint_machine_code_length
.iter_mut()
.enumerate()
{
let config = Config {
instruction_meter_checkpoint_distance: index * INSTRUCTION_COUNT * 2,
noop_instruction_rate: 0,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0,
..Config::default()
};
let mut executable = create_mockup_executable(config, &prog);
Executable::<TestContextObject>::jit_compile(&mut executable).unwrap();
*machine_code_length = (executable
.get_compiled_program()
.unwrap()
.machine_code_length()
- empty_program_machine_code_length_per_version[0])
/ INSTRUCTION_COUNT;
}
let instruction_meter_checkpoint_machine_code_length =
instruction_meter_checkpoint_machine_code_length[0]
- instruction_meter_checkpoint_machine_code_length[1];
assert!(
instruction_meter_checkpoint_machine_code_length
<= MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT
);
let mut cfg_nodes = BTreeMap::new();
cfg_nodes.insert(
8,
CfgNode {
label: std::string::String::from("label"),
..CfgNode::default()
},
);
for sbpf_version in [SBPFVersion::V0, SBPFVersion::V3] {
println!("opcode;machine_code_length_per_instruction;assembly");
let empty_program_machine_code_length =
empty_program_machine_code_length_per_version[sbpf_version as usize];
for mut opcode in 0x00..=0xFF {
let (registers, immediate) = match opcode {
0x85 => (0x00, Some(8)),
0x8D => (0x88, Some(0)),
0xE5 => {
// Put external function calls on a separate loop iteration
opcode = 0x85;
(0x00, Some(0x91020CDD))
}
0xF5 => {
// Put invalid function calls on a separate loop iteration
opcode = 0x85;
(0x00, Some(0x91020CD0))
}
0xD4 | 0xDC => (0x88, Some(16)),
_ => (0x88, Some(0x11223344)),
};
for pc in 0..INSTRUCTION_COUNT {
prog[pc * ebpf::INSN_SIZE] = opcode;
prog[pc * ebpf::INSN_SIZE + 1] = registers;
let offset = 7_u16.wrapping_sub(pc as u16);
LittleEndian::write_u16(&mut prog[pc * ebpf::INSN_SIZE + 2..], offset);
let immediate = immediate.unwrap_or_else(|| 7_u32.wrapping_sub(pc as u32));
LittleEndian::write_u32(&mut prog[pc * ebpf::INSN_SIZE + 4..], immediate);
}
let config = Config {
noop_instruction_rate: 0,
enabled_sbpf_versions: sbpf_version..=sbpf_version,
..Config::default()
};
let mut executable = create_mockup_executable(config, &prog);
let result = Executable::<TestContextObject>::jit_compile(&mut executable);
if let Err(err) = result {
assert!(matches!(err, EbpfError::UnsupportedInstruction));
continue;
}
let machine_code_length = executable
.get_compiled_program()
.unwrap()
.machine_code_length()
- empty_program_machine_code_length;
let instruction_count = if opcode == 0x18 {
// LDDW takes two slots
INSTRUCTION_COUNT / 2
} else {
INSTRUCTION_COUNT
};
let machine_code_length_per_instruction =
machine_code_length as f64 / instruction_count as f64;
assert!(
f64::ceil(machine_code_length_per_instruction) as usize
<= MAX_MACHINE_CODE_LENGTH_PER_INSTRUCTION
);
let insn = ebpf::get_insn_unchecked(&prog, 0);
let assembly = disassemble_instruction(
&insn,
0,
&cfg_nodes,
executable.get_function_registry(),
executable.get_loader(),
executable.get_sbpf_version(),
);
println!("{opcode:02X};{machine_code_length_per_instruction:>7.3};{assembly}");
}
}
}