2021-12-03 09:00:31 -08:00
|
|
|
use {
|
2022-03-07 09:30:47 -08:00
|
|
|
clap::{crate_version, Arg, Command},
|
2021-12-03 09:00:31 -08:00
|
|
|
serde::{Deserialize, Serialize},
|
|
|
|
serde_json::Result,
|
|
|
|
solana_bpf_loader_program::{
|
|
|
|
create_vm, serialization::serialize_parameters, syscalls::register_syscalls, BpfError,
|
|
|
|
ThisInstructionMeter,
|
|
|
|
},
|
|
|
|
solana_program_runtime::invoke_context::{prepare_mock_invoke_context, InvokeContext},
|
|
|
|
solana_rbpf::{
|
|
|
|
assembler::assemble,
|
|
|
|
elf::Executable,
|
|
|
|
static_analysis::Analysis,
|
2022-06-07 04:45:07 -07:00
|
|
|
verifier::RequisiteVerifier,
|
|
|
|
vm::{Config, DynamicAnalysis, VerifiedExecutable},
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
2021-12-17 05:01:12 -08:00
|
|
|
solana_sdk::{
|
|
|
|
account::AccountSharedData, bpf_loader, instruction::AccountMeta, pubkey::Pubkey,
|
2022-07-15 00:31:34 -07:00
|
|
|
sysvar::rent::Rent, transaction_context::TransactionContext,
|
2021-12-17 05:01:12 -08:00
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
std::{
|
2022-02-13 09:50:22 -08:00
|
|
|
fmt::{Debug, Formatter},
|
2021-12-03 09:00:31 -08:00
|
|
|
fs::File,
|
|
|
|
io::{Read, Seek, SeekFrom},
|
|
|
|
path::Path,
|
2022-02-13 09:50:22 -08:00
|
|
|
time::{Duration, Instant},
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
2021-05-28 04:28:30 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct Account {
|
2021-11-04 13:47:32 -07:00
|
|
|
key: Pubkey,
|
|
|
|
owner: Pubkey,
|
|
|
|
is_signer: bool,
|
|
|
|
is_writable: bool,
|
2021-05-28 04:28:30 -07:00
|
|
|
lamports: u64,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Input {
|
|
|
|
accounts: Vec<Account>,
|
2021-11-04 13:47:32 -07:00
|
|
|
instruction_data: Vec<u8>,
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
|
|
|
fn load_accounts(path: &Path) -> Result<Input> {
|
|
|
|
let file = File::open(path).unwrap();
|
|
|
|
let input: Input = serde_json::from_reader(file)?;
|
2022-05-20 20:29:10 -07:00
|
|
|
eprintln!("Program input:");
|
|
|
|
eprintln!("accounts {:?}", &input.accounts);
|
|
|
|
eprintln!("instruction_data {:?}", &input.instruction_data);
|
|
|
|
eprintln!("----------------------------------------");
|
2021-05-28 04:28:30 -07:00
|
|
|
Ok(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
solana_logger::setup();
|
2022-03-07 09:30:47 -08:00
|
|
|
let matches = Command::new("Solana BPF CLI")
|
2021-05-28 04:28:30 -07:00
|
|
|
.version(crate_version!())
|
|
|
|
.author("Solana Maintainers <maintainers@solana.foundation>")
|
|
|
|
.about(
|
|
|
|
r##"CLI to test and analyze eBPF programs.
|
|
|
|
|
|
|
|
The tool executes eBPF programs in a mocked environment.
|
|
|
|
Some features, such as sysvars syscall and CPI, are not
|
|
|
|
available for the programs executed by the CLI tool.
|
|
|
|
|
|
|
|
The input data for a program execution have to be in JSON format
|
|
|
|
and the following fields are required
|
|
|
|
{
|
|
|
|
"accounts": [
|
|
|
|
{
|
2021-11-04 13:47:32 -07:00
|
|
|
"key": [
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
],
|
2021-05-28 04:28:30 -07:00
|
|
|
"owner": [
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
2021-11-04 13:47:32 -07:00
|
|
|
],
|
|
|
|
"is_signer": false,
|
|
|
|
"is_writable": true,
|
|
|
|
"lamports": 1000,
|
|
|
|
"data": [0, 0, 0, 3]
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
|
|
|
],
|
2021-11-04 13:47:32 -07:00
|
|
|
"instruction_data": []
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
|
|
|
"##,
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("PROGRAM")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help(
|
2021-05-28 04:28:30 -07:00
|
|
|
"Program file to use. This is either an ELF shared-object file to be executed, \
|
|
|
|
or an assembly file to be assembled and executed.",
|
|
|
|
)
|
|
|
|
.required(true)
|
|
|
|
.index(1)
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("input")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help(
|
2021-05-28 04:28:30 -07:00
|
|
|
"Input for the program to run on, where FILE is a name of a JSON file \
|
|
|
|
with input data, or BYTES is the number of 0-valued bytes to allocate for program parameters",
|
|
|
|
)
|
|
|
|
.short('i')
|
|
|
|
.long("input")
|
|
|
|
.value_name("FILE / BYTES")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("0"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("memory")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help("Heap memory for the program to run on")
|
2021-05-28 04:28:30 -07:00
|
|
|
.short('m')
|
|
|
|
.long("memory")
|
|
|
|
.value_name("BYTES")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("0"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("use")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help(
|
2021-05-28 04:28:30 -07:00
|
|
|
"Method of execution to use, where 'cfg' generates Control Flow Graph \
|
|
|
|
of the program, 'disassembler' dumps disassembled code of the program, 'interpreter' runs \
|
|
|
|
the program in the virtual machine's interpreter, and 'jit' precompiles the program to \
|
|
|
|
native machine code before execting it in the virtual machine.",
|
|
|
|
)
|
|
|
|
.short('u')
|
|
|
|
.long("use")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("VALUE")
|
|
|
|
.possible_values(&["cfg", "disassembler", "interpreter", "jit"])
|
2021-07-14 13:30:38 -07:00
|
|
|
.default_value("jit"),
|
2021-05-28 04:28:30 -07:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("instruction limit")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help("Limit the number of instructions to execute")
|
2021-05-28 04:28:30 -07:00
|
|
|
.short('l')
|
|
|
|
.long("limit")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("COUNT")
|
|
|
|
.default_value(&std::i64::MAX.to_string()),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("trace")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help("Output trace to 'trace.out' file using tracing instrumentation")
|
2021-05-28 04:28:30 -07:00
|
|
|
.short('t')
|
|
|
|
.long("trace"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("profile")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help("Output profile to 'profile.dot' file using tracing instrumentation")
|
2021-05-28 04:28:30 -07:00
|
|
|
.short('p')
|
|
|
|
.long("profile"),
|
|
|
|
)
|
2022-02-13 09:50:22 -08:00
|
|
|
.arg(
|
|
|
|
Arg::new("output_format")
|
2022-03-07 09:30:47 -08:00
|
|
|
.help("Return information in specified output format")
|
2022-02-13 09:50:22 -08:00
|
|
|
.long("output")
|
|
|
|
.value_name("FORMAT")
|
|
|
|
.global(true)
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_values(&["json", "json-compact"]),
|
|
|
|
)
|
2021-05-28 04:28:30 -07:00
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let config = Config {
|
2022-05-20 20:29:10 -07:00
|
|
|
enable_instruction_tracing: matches.is_present("trace") || matches.is_present("profile"),
|
2021-12-03 06:45:25 -08:00
|
|
|
enable_symbol_and_section_labels: true,
|
2021-05-28 04:28:30 -07:00
|
|
|
..Config::default()
|
|
|
|
};
|
2021-11-04 13:47:32 -07:00
|
|
|
let loader_id = bpf_loader::id();
|
2021-12-17 05:01:12 -08:00
|
|
|
let mut transaction_accounts = vec![
|
2021-11-04 13:47:32 -07:00
|
|
|
(
|
|
|
|
loader_id,
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::new(0, 0, &solana_sdk::native_loader::id()),
|
2021-11-04 13:47:32 -07:00
|
|
|
),
|
|
|
|
(
|
|
|
|
Pubkey::new_unique(),
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::new(0, 0, &loader_id),
|
2021-11-04 13:47:32 -07:00
|
|
|
),
|
|
|
|
];
|
2021-12-17 05:01:12 -08:00
|
|
|
let mut instruction_accounts = Vec::new();
|
2021-11-04 13:47:32 -07:00
|
|
|
let instruction_data = match matches.value_of("input").unwrap().parse::<usize>() {
|
|
|
|
Ok(allocation_size) => {
|
2021-12-17 05:01:12 -08:00
|
|
|
let pubkey = Pubkey::new_unique();
|
|
|
|
transaction_accounts.push((
|
|
|
|
pubkey,
|
|
|
|
AccountSharedData::new(0, allocation_size, &Pubkey::new_unique()),
|
2021-11-04 13:47:32 -07:00
|
|
|
));
|
2021-12-17 05:01:12 -08:00
|
|
|
instruction_accounts.push(AccountMeta {
|
|
|
|
pubkey,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: true,
|
|
|
|
});
|
2021-11-04 13:47:32 -07:00
|
|
|
vec![]
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
|
|
|
Err(_) => {
|
2021-06-18 06:34:46 -07:00
|
|
|
let input = load_accounts(Path::new(matches.value_of("input").unwrap())).unwrap();
|
2021-12-17 05:01:12 -08:00
|
|
|
for account_info in input.accounts {
|
|
|
|
let mut account = AccountSharedData::new(
|
|
|
|
account_info.lamports,
|
|
|
|
account_info.data.len(),
|
|
|
|
&account_info.owner,
|
2021-11-04 13:47:32 -07:00
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
account.set_data(account_info.data);
|
|
|
|
instruction_accounts.push(AccountMeta {
|
|
|
|
pubkey: account_info.key,
|
|
|
|
is_signer: account_info.is_signer,
|
|
|
|
is_writable: account_info.is_writable,
|
|
|
|
});
|
|
|
|
transaction_accounts.push((account_info.key, account));
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
2021-11-04 13:47:32 -07:00
|
|
|
input.instruction_data
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
|
|
|
};
|
2021-11-04 13:47:32 -07:00
|
|
|
let program_indices = [0, 1];
|
2021-12-30 06:46:36 -08:00
|
|
|
let preparation =
|
|
|
|
prepare_mock_invoke_context(transaction_accounts, instruction_accounts, &program_indices);
|
2022-07-15 00:31:34 -07:00
|
|
|
let mut transaction_context = TransactionContext::new(
|
|
|
|
preparation.transaction_accounts,
|
|
|
|
Some(Rent::default()),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
);
|
2022-01-03 14:30:56 -08:00
|
|
|
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
|
2021-11-04 13:47:32 -07:00
|
|
|
invoke_context
|
2022-01-03 14:30:56 -08:00
|
|
|
.push(
|
|
|
|
&preparation.instruction_accounts,
|
|
|
|
&program_indices,
|
|
|
|
&instruction_data,
|
|
|
|
)
|
2021-11-04 13:47:32 -07:00
|
|
|
.unwrap();
|
|
|
|
let (mut parameter_bytes, account_lengths) = serialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
invoke_context.transaction_context,
|
|
|
|
invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_current_instruction_context()
|
|
|
|
.unwrap(),
|
2022-07-20 05:12:43 -07:00
|
|
|
true, // should_cap_ix_accounts
|
2021-11-04 13:47:32 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-05-28 04:28:30 -07:00
|
|
|
let compute_meter = invoke_context.get_compute_meter();
|
|
|
|
let mut instruction_meter = ThisInstructionMeter { compute_meter };
|
|
|
|
|
|
|
|
let program = matches.value_of("PROGRAM").unwrap();
|
|
|
|
let mut file = File::open(&Path::new(program)).unwrap();
|
|
|
|
let mut magic = [0u8; 4];
|
|
|
|
file.read_exact(&mut magic).unwrap();
|
|
|
|
file.seek(SeekFrom::Start(0)).unwrap();
|
|
|
|
let mut contents = Vec::new();
|
|
|
|
file.read_to_end(&mut contents).unwrap();
|
2022-05-20 13:19:41 -07:00
|
|
|
let syscall_registry = register_syscalls(&mut invoke_context, true).unwrap();
|
2022-06-07 04:45:07 -07:00
|
|
|
let executable = if magic == [0x7f, 0x45, 0x4c, 0x46] {
|
|
|
|
Executable::<BpfError, ThisInstructionMeter>::from_elf(&contents, config, syscall_registry)
|
|
|
|
.map_err(|err| format!("Executable constructor failed: {:?}", err))
|
2021-05-28 04:28:30 -07:00
|
|
|
} else {
|
|
|
|
assemble::<BpfError, ThisInstructionMeter>(
|
|
|
|
std::str::from_utf8(contents.as_slice()).unwrap(),
|
|
|
|
config,
|
2021-07-07 00:50:11 -07:00
|
|
|
syscall_registry,
|
2021-05-28 04:28:30 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
.unwrap();
|
|
|
|
|
2022-06-07 04:45:07 -07:00
|
|
|
let mut verified_executable =
|
|
|
|
VerifiedExecutable::<RequisiteVerifier, BpfError, ThisInstructionMeter>::from_executable(
|
|
|
|
executable,
|
|
|
|
)
|
|
|
|
.map_err(|err| format!("Executable verifier failed: {:?}", err))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
verified_executable.jit_compile().unwrap();
|
|
|
|
let mut analysis = LazyAnalysis::new(verified_executable.get_executable());
|
2021-05-28 04:28:30 -07:00
|
|
|
|
|
|
|
match matches.value_of("use") {
|
|
|
|
Some("cfg") => {
|
|
|
|
let mut file = File::create("cfg.dot").unwrap();
|
2022-02-15 18:35:05 -08:00
|
|
|
analysis
|
|
|
|
.analyze()
|
|
|
|
.visualize_graphically(&mut file, None)
|
|
|
|
.unwrap();
|
2021-05-28 04:28:30 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Some("disassembler") => {
|
|
|
|
let stdout = std::io::stdout();
|
2022-02-15 18:35:05 -08:00
|
|
|
analysis.analyze().disassemble(&mut stdout.lock()).unwrap();
|
2021-05-28 04:28:30 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2021-09-28 01:13:03 -07:00
|
|
|
let mut vm = create_vm(
|
2022-06-07 04:45:07 -07:00
|
|
|
&verified_executable,
|
2021-11-04 13:47:32 -07:00
|
|
|
parameter_bytes.as_slice_mut(),
|
2022-05-19 15:14:28 -07:00
|
|
|
account_lengths,
|
2021-09-28 01:13:03 -07:00
|
|
|
&mut invoke_context,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-13 19:00:56 -07:00
|
|
|
let start_time = Instant::now();
|
2021-05-28 04:28:30 -07:00
|
|
|
let result = if matches.value_of("use").unwrap() == "interpreter" {
|
|
|
|
vm.execute_program_interpreted(&mut instruction_meter)
|
|
|
|
} else {
|
|
|
|
vm.execute_program_jit(&mut instruction_meter)
|
|
|
|
};
|
2021-07-13 19:00:56 -07:00
|
|
|
let duration = Instant::now() - start_time;
|
2022-02-13 09:50:22 -08:00
|
|
|
|
|
|
|
let output = Output {
|
|
|
|
result: format!("{:?}", result),
|
|
|
|
instruction_count: vm.get_total_instruction_count(),
|
|
|
|
execution_time: duration,
|
|
|
|
};
|
|
|
|
match matches.value_of("output_format") {
|
|
|
|
Some("json") => {
|
|
|
|
println!("{}", serde_json::to_string_pretty(&output).unwrap());
|
|
|
|
}
|
|
|
|
Some("json-compact") => {
|
|
|
|
println!("{}", serde_json::to_string(&output).unwrap());
|
|
|
|
}
|
|
|
|
_ => {
|
2022-05-20 20:29:10 -07:00
|
|
|
println!("Program output:");
|
|
|
|
println!("{:?}", output);
|
2022-02-13 09:50:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 04:28:30 -07:00
|
|
|
if matches.is_present("trace") {
|
2022-05-20 20:29:10 -07:00
|
|
|
eprintln!("Trace is saved in trace.out");
|
2021-05-28 04:28:30 -07:00
|
|
|
let mut file = File::create("trace.out").unwrap();
|
2022-02-15 18:35:05 -08:00
|
|
|
vm.get_tracer()
|
|
|
|
.write(&mut file, analysis.analyze())
|
|
|
|
.unwrap();
|
2021-05-28 04:28:30 -07:00
|
|
|
}
|
|
|
|
if matches.is_present("profile") {
|
2022-05-20 20:29:10 -07:00
|
|
|
eprintln!("Profile is saved in profile.dot");
|
|
|
|
let tracer = &vm.get_tracer();
|
2022-02-15 18:35:05 -08:00
|
|
|
let analysis = analysis.analyze();
|
|
|
|
let dynamic_analysis = DynamicAnalysis::new(tracer, analysis);
|
2021-05-28 04:28:30 -07:00
|
|
|
let mut file = File::create("profile.dot").unwrap();
|
|
|
|
analysis
|
|
|
|
.visualize_graphically(&mut file, Some(&dynamic_analysis))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 09:50:22 -08:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Output {
|
|
|
|
result: String,
|
|
|
|
instruction_count: u64,
|
|
|
|
execution_time: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Output {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
writeln!(f, "Result: {}", self.result)?;
|
|
|
|
writeln!(f, "Instruction Count: {}", self.instruction_count)?;
|
|
|
|
writeln!(f, "Execution time: {} us", self.execution_time.as_micros())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 18:35:05 -08:00
|
|
|
|
|
|
|
// Replace with std::lazy::Lazy when stabilized.
|
|
|
|
// https://github.com/rust-lang/rust/issues/74465
|
|
|
|
struct LazyAnalysis<'a> {
|
|
|
|
analysis: Option<Analysis<'a, BpfError, ThisInstructionMeter>>,
|
|
|
|
executable: &'a Executable<BpfError, ThisInstructionMeter>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LazyAnalysis<'a> {
|
|
|
|
fn new(executable: &'a Executable<BpfError, ThisInstructionMeter>) -> Self {
|
|
|
|
Self {
|
|
|
|
analysis: None,
|
|
|
|
executable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn analyze(&mut self) -> &Analysis<BpfError, ThisInstructionMeter> {
|
|
|
|
if let Some(ref analysis) = self.analysis {
|
|
|
|
return analysis;
|
|
|
|
}
|
|
|
|
self.analysis
|
2022-04-30 01:58:12 -07:00
|
|
|
.insert(Analysis::from_executable(self.executable).unwrap())
|
2022-02-15 18:35:05 -08:00
|
|
|
}
|
|
|
|
}
|