Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
V0.2 (#105)
Browse files Browse the repository at this point in the history
* Tunable stack depth and frame size

* Adds support to resize memory regions in memory mapping.

* Fixes bug in JIT: mul, div, mod

* Let the disassembler print the IP / PC instead of the line number.
As the line number can easily be calculated later, but the IP / PC is more important and harder to get otherwise.

* Fixes bugs related to using "MM_PROGRAM_START" instead of "elf.text_section_info.vaddr".

* Fixes upper bound of register relative jumps.

* Bump to v0.2.0
  • Loading branch information
Lichtso authored Oct 15, 2020
1 parent 3e3e37e commit 5e38d3d
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Project metadata
name = "solana_rbpf"
version = "0.1.31"
version = "0.2.0"
authors = ["Solana Maintainers <maintainers@solana.com>"]

# Additional metadata for packaging
Expand Down Expand Up @@ -39,4 +39,3 @@ time = "0.1"

elf = "0.0.10"
json = "0.11"

20 changes: 15 additions & 5 deletions examples/uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate solana_rbpf;
use solana_rbpf::{
syscalls,
user_error::UserError,
vm::{DefaultInstructionMeter, EbpfVm, Executable, Syscall},
vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, Syscall},
};

// The main objectives of this example is to show:
Expand Down Expand Up @@ -43,8 +43,13 @@ fn main() {

// Create a VM: this one takes no data. Load prog1 in it.
let executable = Executable::<UserError>::from_text_bytes(prog1, None).unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &[], &[]).unwrap();
let mut vm = EbpfVm::<UserError, DefaultInstructionMeter>::new(
executable.as_ref(),
Config::default(),
&[],
&[],
)
.unwrap();
// Execute prog1.
assert_eq!(
vm.execute_program_interpreted(&mut DefaultInstructionMeter {})
Expand All @@ -60,8 +65,13 @@ fn main() {
// reimplement uptime in eBPF, in Rust. Because why not.

let executable = Executable::<UserError>::from_text_bytes(prog2, None).unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &[], &[]).unwrap();
let mut vm = EbpfVm::<UserError, DefaultInstructionMeter>::new(
executable.as_ref(),
Config::default(),
&[],
&[],
)
.unwrap();
vm.register_syscall(
syscalls::BPF_KTIME_GETNS_IDX,
Syscall::Function(syscalls::bpf_time_getns),
Expand Down
10 changes: 0 additions & 10 deletions src/call_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ use crate::{
memory_region::MemoryRegion,
};

/// Stack for the eBPF stack, in bytes.
pub const CALL_FRAME_SIZE: usize = 4_096; // !! Warning: if you change stack size here also change warning in llvm (BPF_RegisterInfo.cpp)
/// Max BPF to BPF call depth
pub const MAX_CALL_DEPTH: usize = 20;

/// One call frame
#[derive(Clone, Debug)]
struct CallFrame {
Expand All @@ -29,11 +24,6 @@ pub struct CallFrames {
max_frame: usize,
frames: Vec<CallFrame>,
}
impl Default for CallFrames {
fn default() -> Self {
CallFrames::new(MAX_CALL_DEPTH, CALL_FRAME_SIZE)
}
}
impl CallFrames {
/// New call frame, depth indicates maximum call depth
pub fn new(depth: usize, size: usize) -> Self {
Expand Down
18 changes: 11 additions & 7 deletions src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ fn jmp_reg_str(name: &str, insn: &ebpf::Insn) -> String {
/// more concise version.
#[derive(Debug, PartialEq)]
pub struct HLInsn {
/// Instruction pointer.
pub ptr: usize,
/// Operation code.
pub opc: u8,
/// Name (mnemonic). This name is not canon.
Expand Down Expand Up @@ -132,6 +134,7 @@ pub struct HLInsn {
/// let v = disassembler::to_insn_vec(prog);
/// assert_eq!(v, vec![
/// disassembler::HLInsn {
/// ptr: 0,
/// opc: 0x18,
/// name: "lddw".to_string(),
/// desc: "lddw r0, 0x1122334455667788".to_string(),
Expand All @@ -141,6 +144,7 @@ pub struct HLInsn {
/// imm: 0x1122334455667788
/// },
/// disassembler::HLInsn {
/// ptr: 2,
/// opc: 0x95,
/// name: "exit".to_string(),
/// desc: "exit".to_string(),
Expand All @@ -162,10 +166,11 @@ pub fn to_insn_vec(prog: &[u8]) -> Vec<HLInsn> {
}

let mut res = vec![];
let mut insn_ptr:usize = 0;
let mut insn_ptr: usize = 0;

while insn_ptr * ebpf::INSN_SIZE < prog.len() {
let insn = ebpf::get_insn(prog, insn_ptr);
let ptr = insn_ptr;

let name;
let desc;
Expand Down Expand Up @@ -299,17 +304,16 @@ pub fn to_insn_vec(prog: &[u8]) -> Vec<HLInsn> {
},
};

let hl_insn = HLInsn {
res.push(HLInsn {
ptr,
opc: insn.opc,
name: name.to_string(),
desc,
dst: insn.dst,
src: insn.src,
off: insn.off,
imm,
};

res.push(hl_insn);
});

insn_ptr += 1;
};
Expand Down Expand Up @@ -347,7 +351,7 @@ pub fn to_insn_vec(prog: &[u8]) -> Vec<HLInsn> {
/// exit
/// ```
pub fn disassemble(prog: &[u8]) {
for (i, insn) in to_insn_vec(prog).iter().enumerate() {
println!("{:5} {}", i, insn.desc);
for insn in to_insn_vec(prog).iter() {
println!("{:5} {}", insn.ptr, insn.desc);
}
}
Loading

0 comments on commit 5e38d3d

Please sign in to comment.