Add BPF support & C-based BPF tic-tac-toe (#1422)

Add initial support for BPF and a C port of tictactoe
This commit is contained in:
jackcmay
2018-10-04 09:44:44 -07:00
committed by GitHub
parent 74b63c12a0
commit 13d4443d4d
27 changed files with 1321 additions and 124 deletions

314
src/bpf_verifier.rs Normal file
View File

@@ -0,0 +1,314 @@
use rbpf::ebpf;
// This “verifier” performs simple checks when the eBPF program is loaded into the VM (before it is
// interpreted or JIT-compiled).
fn verify_prog_len(prog: &[u8]) {
if prog.len() % ebpf::INSN_SIZE != 0 {
panic!(
"[Verifier] Error: eBPF program length must be a multiple of {:?} octets",
ebpf::INSN_SIZE
);
}
if prog.len() > ebpf::PROG_MAX_SIZE {
panic!(
"[Verifier] Error: eBPF program length limited to {:?}, here {:?}",
ebpf::PROG_MAX_INSNS,
prog.len() / ebpf::INSN_SIZE
);
}
if prog.is_empty() {
panic!("[Verifier] Error: program is empty");
}
// TODO BPF program may deterministically exit even if the last
// instruction in the block is not an exit (might be earlier and jumped to)
// TODO need to validate more intelligently
// let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);
// if last_insn.opc != ebpf::EXIT {
// panic!("[Verifier] Error: program does not end with “EXIT” instruction");
// }
}
fn verify_imm_nonzero(insn: &ebpf::Insn, insn_ptr: usize) {
if insn.imm == 0 {
panic!("[Verifier] Error: division by 0 (insn #{:?})", insn_ptr);
}
}
fn verify_imm_endian(insn: &ebpf::Insn, insn_ptr: usize) {
match insn.imm {
16 | 32 | 64 => return,
_ => panic!(
"[Verifier] Error: unsupported argument for LE/BE (insn #{:?})",
insn_ptr
),
}
}
fn verify_load_dw(prog: &[u8], insn_ptr: usize) {
// We know we can reach next insn since we enforce an EXIT insn at the end of program, while
// this function should be called only for LD_DW insn, that cannot be last in program.
let next_insn = ebpf::get_insn(prog, insn_ptr + 1);
if next_insn.opc != 0 {
panic!(
"[Verifier] Error: incomplete LD_DW instruction (insn #{:?})",
insn_ptr
);
}
}
fn verify_jmp_offset(prog: &[u8], insn_ptr: usize) {
let insn = ebpf::get_insn(prog, insn_ptr);
if insn.off == -1 {
panic!("[Verifier] Error: infinite loop (insn #{:?})", insn_ptr);
}
let dst_insn_ptr = insn_ptr as isize + 1 + insn.off as isize;
if dst_insn_ptr < 0 || dst_insn_ptr as usize >= (prog.len() / ebpf::INSN_SIZE) {
panic!(
"[Verifier] Error: jump out of code to #{:?} (insn #{:?})",
dst_insn_ptr, insn_ptr
);
}
let dst_insn = ebpf::get_insn(prog, dst_insn_ptr as usize);
if dst_insn.opc == 0 {
panic!(
"[Verifier] Error: jump to middle of LD_DW at #{:?} (insn #{:?})",
dst_insn_ptr, insn_ptr
);
}
}
fn verify_registers(insn: &ebpf::Insn, store: bool, insn_ptr: usize) {
if insn.src > 10 {
panic!(
"[Verifier] Error: invalid source register (insn #{:?})",
insn_ptr
);
}
match (insn.dst, store) {
(0...9, _) | (10, true) => {}
(10, false) => panic!(
"[Verifier] Error: cannot write into register r10 (insn #{:?})",
insn_ptr
),
(_, _) => panic!(
"[Verifier] Error: invalid destination register (insn #{:?})",
insn_ptr
),
}
}
pub fn verifier(prog: &[u8]) -> bool {
verify_prog_len(prog);
let mut insn_ptr: usize = 0;
while insn_ptr * ebpf::INSN_SIZE < prog.len() {
let insn = ebpf::get_insn(prog, insn_ptr);
let mut store = false;
match insn.opc {
// BPF_LD class
ebpf::LD_ABS_B => {}
ebpf::LD_ABS_H => {}
ebpf::LD_ABS_W => {}
ebpf::LD_ABS_DW => {}
ebpf::LD_IND_B => {}
ebpf::LD_IND_H => {}
ebpf::LD_IND_W => {}
ebpf::LD_IND_DW => {}
ebpf::LD_DW_IMM => {
store = true;
verify_load_dw(prog, insn_ptr);
insn_ptr += 1;
}
// BPF_LDX class
ebpf::LD_B_REG => {}
ebpf::LD_H_REG => {}
ebpf::LD_W_REG => {}
ebpf::LD_DW_REG => {}
// BPF_ST class
ebpf::ST_B_IMM => store = true,
ebpf::ST_H_IMM => store = true,
ebpf::ST_W_IMM => store = true,
ebpf::ST_DW_IMM => store = true,
// BPF_STX class
ebpf::ST_B_REG => store = true,
ebpf::ST_H_REG => store = true,
ebpf::ST_W_REG => store = true,
ebpf::ST_DW_REG => store = true,
ebpf::ST_W_XADD => {
unimplemented!();
}
ebpf::ST_DW_XADD => {
unimplemented!();
}
// BPF_ALU class
ebpf::ADD32_IMM => {}
ebpf::ADD32_REG => {}
ebpf::SUB32_IMM => {}
ebpf::SUB32_REG => {}
ebpf::MUL32_IMM => {}
ebpf::MUL32_REG => {}
ebpf::DIV32_IMM => {
verify_imm_nonzero(&insn, insn_ptr);
}
ebpf::DIV32_REG => {}
ebpf::OR32_IMM => {}
ebpf::OR32_REG => {}
ebpf::AND32_IMM => {}
ebpf::AND32_REG => {}
ebpf::LSH32_IMM => {}
ebpf::LSH32_REG => {}
ebpf::RSH32_IMM => {}
ebpf::RSH32_REG => {}
ebpf::NEG32 => {}
ebpf::MOD32_IMM => {
verify_imm_nonzero(&insn, insn_ptr);
}
ebpf::MOD32_REG => {}
ebpf::XOR32_IMM => {}
ebpf::XOR32_REG => {}
ebpf::MOV32_IMM => {}
ebpf::MOV32_REG => {}
ebpf::ARSH32_IMM => {}
ebpf::ARSH32_REG => {}
ebpf::LE => {
verify_imm_endian(&insn, insn_ptr);
}
ebpf::BE => {
verify_imm_endian(&insn, insn_ptr);
}
// BPF_ALU64 class
ebpf::ADD64_IMM => {}
ebpf::ADD64_REG => {}
ebpf::SUB64_IMM => {}
ebpf::SUB64_REG => {}
ebpf::MUL64_IMM => {
verify_imm_nonzero(&insn, insn_ptr);
}
ebpf::MUL64_REG => {}
ebpf::DIV64_IMM => {
verify_imm_nonzero(&insn, insn_ptr);
}
ebpf::DIV64_REG => {}
ebpf::OR64_IMM => {}
ebpf::OR64_REG => {}
ebpf::AND64_IMM => {}
ebpf::AND64_REG => {}
ebpf::LSH64_IMM => {}
ebpf::LSH64_REG => {}
ebpf::RSH64_IMM => {}
ebpf::RSH64_REG => {}
ebpf::NEG64 => {}
ebpf::MOD64_IMM => {}
ebpf::MOD64_REG => {}
ebpf::XOR64_IMM => {}
ebpf::XOR64_REG => {}
ebpf::MOV64_IMM => {}
ebpf::MOV64_REG => {}
ebpf::ARSH64_IMM => {}
ebpf::ARSH64_REG => {}
// BPF_JMP class
ebpf::JA => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JEQ_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JEQ_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JGT_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JGT_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JGE_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JGE_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JLT_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JLT_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JLE_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JLE_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSET_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSET_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JNE_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JNE_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSGT_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSGT_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSGE_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSGE_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSLT_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSLT_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSLE_IMM => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::JSLE_REG => {
verify_jmp_offset(prog, insn_ptr);
}
ebpf::CALL => {}
ebpf::TAIL_CALL => unimplemented!(),
ebpf::EXIT => {}
_ => {
panic!(
"[Verifier] Error: unknown eBPF opcode {:#2x} (insn #{:?})",
insn.opc, insn_ptr
);
}
}
verify_registers(&insn, store, insn_ptr);
insn_ptr += 1;
}
// insn_ptr should now be equal to number of instructions.
if insn_ptr != prog.len() / ebpf::INSN_SIZE {
panic!("[Verifier] Error: jumped out of code to #{:?}", insn_ptr);
}
true
}

View File

@@ -1,37 +1,74 @@
extern crate bincode;
extern crate generic_array;
extern crate elf;
extern crate rbpf;
use libc;
use libloading;
use solana_program_interface::account::KeyedAccount;
use std::io::prelude::*;
use std::mem;
use std::path::PathBuf;
/// Dynamic link library prefix
use bpf_verifier;
use byteorder::{LittleEndian, WriteBytesExt};
use libc;
#[cfg(unix)]
const PLATFORM_FILE_PREFIX: &str = "lib";
/// Dynamic link library prefix
use libloading::os::unix::*;
#[cfg(windows)]
const PLATFORM_FILE_PREFIX: &str = "";
use libloading::os::windows::*;
use solana_program_interface::account::KeyedAccount;
use solana_program_interface::pubkey::Pubkey;
/// Dynamic link library prefixs
const PLATFORM_FILE_PREFIX_BPF: &str = "";
#[cfg(unix)]
const PLATFORM_FILE_PREFIX_NATIVE: &str = "lib";
#[cfg(windows)]
const PLATFORM_FILE_PREFIX_NATIVE: &str = "";
/// Dynamic link library file extension specific to the platform
const PLATFORM_FILE_EXTENSION_BPF: &str = "o";
#[cfg(any(target_os = "macos", target_os = "ios"))]
const PLATFORM_FILE_EXTENSION: &str = "dylib";
const PLATFORM_FILE_EXTENSION_NATIVE: &str = "dylib";
/// Dynamic link library file extension specific to the platform
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
const PLATFORM_FILE_EXTENSION: &str = "so";
const PLATFORM_FILE_EXTENSION_NATIVE: &str = "so";
/// Dynamic link library file extension specific to the platform
#[cfg(windows)]
const PLATFORM_FILE_EXTENSION: &str = "dll";
const PLATFORM_FILE_EXTENSION_NATIVE: &str = "dll";
/// Creates a platform-specific file path
fn create_library_path(name: &str) -> PathBuf {
let mut path = PathBuf::from(env!("OUT_DIR"));
path.pop();
path.pop();
path.pop();
path.push("deps");
path.push(PLATFORM_FILE_PREFIX.to_string() + name);
path.set_extension(PLATFORM_FILE_EXTENSION);
path
/// Section name
const PLATFORM_SECTION_RS: &str = ".text,entrypoint";
const PLATFORM_SECTION_C: &str = ".text.entrypoint";
pub enum ProgramPath {
Bpf,
Native,
}
impl ProgramPath {
/// Creates a platform-specific file path
pub fn create(&self, name: &str) -> PathBuf {
let mut path = PathBuf::from(env!("OUT_DIR"));
match self {
ProgramPath::Bpf => {
//println!("Bpf");
path.pop();
path.pop();
path.pop();
path.push(PLATFORM_FILE_PREFIX_BPF.to_string() + name);
path.set_extension(PLATFORM_FILE_EXTENSION_BPF);
}
ProgramPath::Native => {
//println!("Native");
path.pop();
path.pop();
path.pop();
path.push("deps");
path.push(PLATFORM_FILE_PREFIX_NATIVE.to_string() + name);
path.set_extension(PLATFORM_FILE_EXTENSION_NATIVE);
}
}
//println!("Path: {:?}", path);
path
}
}
// All programs export a symbol named process()
@@ -44,47 +81,129 @@ pub enum DynamicProgram {
/// * Transaction::keys[0..] - program dependent
/// * name - name of the program, translated to a file path of the program module
/// * userdata - program specific user data
Native {
name: String,
library: libloading::Library,
},
Native { name: String, library: Library },
/// Bpf program
/// * Transaction::keys[0..] - program dependent
/// * TODO BPF specific stuff
/// * userdata - program specific user data
Bpf { userdata: Vec<u8> },
Bpf { name: String, prog: Vec<u8> },
}
impl DynamicProgram {
pub fn new(name: String) -> Self {
// TODO determine what kind of module to load
pub fn new_native(name: String) -> Self {
// create native program
let path = create_library_path(&name);
let path = ProgramPath::Native {}.create(&name);
// TODO linux tls bug can cause crash on dlclose, workaround by never unloading
let os_lib =
libloading::os::unix::Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)
.unwrap();
let library = libloading::Library::from(os_lib);
let library = Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW).unwrap();
DynamicProgram::Native { name, library }
}
pub fn new_bpf_from_file(name: String) -> Self {
// create native program
let path = ProgramPath::Bpf {}.create(&name);
let file = match elf::File::open_path(&path) {
Ok(f) => f,
Err(e) => panic!("Error opening ELF {:?}: {:?}", path, e),
};
let text_section = match file.get_section(PLATFORM_SECTION_RS) {
Some(s) => s,
None => match file.get_section(PLATFORM_SECTION_C) {
Some(s) => s,
None => panic!("Failed to find text section"),
},
};
let prog = text_section.data.clone();
DynamicProgram::Bpf { name, prog }
}
pub fn new_bpf_from_buffer(prog: Vec<u8>) -> Self {
DynamicProgram::Bpf {
name: "from_buffer".to_string(),
prog,
}
}
#[allow(dead_code)]
fn dump_prog(name: &str, prog: &[u8]) {
let mut eight_bytes: Vec<u8> = Vec::new();
println!("BPF Program: {}", name);
for i in prog.iter() {
if eight_bytes.len() >= 7 {
println!("{:02X?}", eight_bytes);
eight_bytes.clear();
} else {
eight_bytes.push(i.clone());
}
}
}
fn serialize(infos: &mut Vec<KeyedAccount>, data: &[u8]) -> Vec<u8> {
assert_eq!(32, mem::size_of::<Pubkey>());
let mut v: Vec<u8> = Vec::new();
v.write_u64::<LittleEndian>(infos.len() as u64).unwrap();
for info in infos.iter_mut() {
v.write_all(info.key.as_ref()).unwrap();
v.write_i64::<LittleEndian>(info.account.tokens).unwrap();
v.write_u64::<LittleEndian>(info.account.userdata.len() as u64)
.unwrap();
v.write_all(&info.account.userdata).unwrap();
v.write_all(info.account.program_id.as_ref()).unwrap();
//println!("userdata: {:?}", infos[i].account.userdata);
}
v.write_u64::<LittleEndian>(data.len() as u64).unwrap();
v.write_all(data).unwrap();
v
}
fn deserialize(infos: &mut Vec<KeyedAccount>, buffer: &[u8]) {
assert_eq!(32, mem::size_of::<Pubkey>());
let mut start = mem::size_of::<u64>();
for info in infos.iter_mut() {
start += mem::size_of::<Pubkey>() // pubkey
+ mem::size_of::<u64>() // tokens
+ mem::size_of::<u64>(); // length tag
let end = start + info.account.userdata.len();
info.account.userdata.clone_from_slice(&buffer[start..end]);
start += info.account.userdata.len() // userdata
+ mem::size_of::<Pubkey>(); // program_id
//println!("userdata: {:?}", infos[i].account.userdata);
}
}
pub fn call(&self, infos: &mut Vec<KeyedAccount>, data: &[u8]) {
match self {
DynamicProgram::Native { name, library } => unsafe {
let entrypoint: libloading::Symbol<Entrypoint> =
match library.get(ENTRYPOINT.as_bytes()) {
Ok(s) => s,
Err(e) => panic!(
"{:?} Unable to find {:?} in program {}",
e, ENTRYPOINT, name
),
};
let entrypoint: Symbol<Entrypoint> = match library.get(ENTRYPOINT.as_bytes()) {
Ok(s) => s,
Err(e) => panic!(
"Unable to find {:?} in program {}: {:?} ",
e, ENTRYPOINT, name
),
};
entrypoint(infos, data);
},
DynamicProgram::Bpf { .. } => {
// TODO BPF
println!{"Bpf program not supported"}
DynamicProgram::Bpf { prog, .. } => {
println!("Instructions: {}", prog.len() / 8);
//DynamicProgram::dump_prog(name, prog);
let mut vm = rbpf::EbpfVmRaw::new(prog, Some(bpf_verifier::verifier));
// TODO register more handlers (memcpy for example)
vm.register_helper(
rbpf::helpers::BPF_TRACE_PRINTK_IDX,
rbpf::helpers::bpf_trace_printf,
);
let mut v = DynamicProgram::serialize(infos, data);
vm.prog_exec(v.as_mut_slice());
DynamicProgram::deserialize(infos, &v);
}
}
}
@@ -95,17 +214,71 @@ mod tests {
use super::*;
use std::path::Path;
use solana_program_interface::account::Account;
use solana_program_interface::pubkey::Pubkey;
#[test]
fn test_create_library_path() {
let path = create_library_path("noop");
fn test_path_create_native() {
let path = ProgramPath::Native {}.create("noop");
assert_eq!(true, Path::new(&path).exists());
let path = create_library_path("print");
assert_eq!(true, Path::new(&path).exists());
let path = create_library_path("move_funds");
let path = ProgramPath::Native {}.create("move_funds");
assert_eq!(true, Path::new(&path).exists());
}
#[test]
fn test_bpf_buf_noop() {
let prog = vec![
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
let data: Vec<u8> = vec![0];
let keys = vec![Pubkey::default(); 2];
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].tokens = 100;
accounts[1].tokens = 1;
{
let mut infos: Vec<_> = (&keys)
.into_iter()
.zip(&mut accounts)
.map(|(key, account)| KeyedAccount { key, account })
.collect();
let dp = DynamicProgram::new_bpf_from_buffer(prog);
dp.call(&mut infos, &data);
}
}
#[test]
fn test_bpf_buf_print() {
let prog = vec![
0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = 0
0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r2 = 0
0xb7, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // r3 = 1
0xb7, 0x04, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // r4 = 2
0xb7, 0x05, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // r5 = 3
0x85, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, // call 6
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
];
let data: Vec<u8> = vec![0];
let keys = vec![Pubkey::default(); 2];
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].tokens = 100;
accounts[1].tokens = 1;
{
let mut infos: Vec<_> = (&keys)
.into_iter()
.zip(&mut accounts)
.map(|(key, account)| KeyedAccount { key, account })
.collect();
let dp = DynamicProgram::new_bpf_from_buffer(prog);
dp.call(&mut infos, &data);
}
}
// TODO add more tests to validate the Userdata and Account data is
// moving across the boundary correctly
}

View File

@@ -22,6 +22,7 @@ pub mod choose_gossip_peer_strategy;
pub mod client;
#[macro_use]
pub mod crdt;
pub mod bpf_verifier;
pub mod budget_program;
pub mod drone;
pub mod dynamic_program;
@@ -91,6 +92,7 @@ extern crate log;
extern crate nix;
extern crate pnet_datalink;
extern crate rayon;
extern crate rbpf;
extern crate reqwest;
extern crate ring;
extern crate serde;

View File

@@ -88,7 +88,7 @@ impl SystemProgram {
}
SystemProgram::Load { program_id, name } => {
let mut hashmap = loaded_programs.write().unwrap();
hashmap.insert(program_id, DynamicProgram::new(name));
hashmap.insert(program_id, DynamicProgram::new_native(name));
}
}
} else {

View File

@@ -55,6 +55,7 @@ impl Default for State {
}
}
#[repr(C)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Game {
player_x: Pubkey,
@@ -173,7 +174,8 @@ impl Game {
}
#[derive(Debug, Serialize, Deserialize)]
enum Command {
#[repr(C)]
pub enum Command {
Init, // player X initializes a new game
Join(i64), // player O wants to join (seconds since UNIX epoch)
KeepAlive(i64), // player X/O keep alive (seconds since UNIX epoch)