Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/allowed_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Copyright 2024 Akenes SA <wouter.dullaert@exoscale.ch>

extern crate elf;
use elf::endian::AnyEndian;
use elf::ElfBytes;
use elf::endian::AnyEndian;
use std::path::PathBuf;
use std::{ptr::addr_of};
use std::ptr::addr_of;

extern crate rbpf;

Expand Down Expand Up @@ -46,9 +46,10 @@ fn get_prog_data(filename: &str) -> Vec<u8> {
Err(e) => panic!("Error while searching for classifier section: {}", e),
};

file
.section_data(&classifier_section_header)
.expect("Failed to get classifier section data").0.to_vec()
file.section_data(&classifier_section_header)
.expect("Failed to get classifier section data")
.0
.to_vec()
}

fn main() {
Expand Down
9 changes: 7 additions & 2 deletions examples/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ use std::time::{SystemTime, UNIX_EPOCH};
// Custom helper that returns the current Unix timestamp in seconds.
// This demonstrates how users can create and register their own helpers with rbpf.
#[allow(unused_variables)]
fn helper_get_time_sec(unused1: u64, unused2: u64, unused3: u64, unused4: u64, unused5: u64) -> u64 {
fn helper_get_time_sec(
unused1: u64,
unused2: u64,
unused3: u64,
unused4: u64,
unused5: u64,
) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand Down Expand Up @@ -139,4 +145,3 @@ fn print_date(timestamp: u64) {
println!("Current date and time (UTC): {year:04}-{month:02}-{day:02} {hours:02}:{minutes:02}:{seconds:02}");
println!("Unix timestamp: {timestamp}");
}

5 changes: 3 additions & 2 deletions examples/load_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>

extern crate elf;
use elf::endian::AnyEndian;
use elf::ElfBytes;
use elf::endian::AnyEndian;
use std::path::PathBuf;

extern crate rbpf;
Expand Down Expand Up @@ -63,7 +63,8 @@ fn main() {

let prog = file
.section_data(&classifier_section_header)
.expect("Failed to get .classifier section data").0;
.expect("Failed to get .classifier section data")
.0;

#[rustfmt::skip]
let packet1 = &mut [
Expand Down
5 changes: 3 additions & 2 deletions examples/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
extern crate json;

extern crate elf;
use elf::endian::AnyEndian;
use elf::ElfBytes;
use elf::endian::AnyEndian;
use std::path::PathBuf;

extern crate rbpf;
Expand Down Expand Up @@ -72,7 +72,8 @@ fn main() {

let prog = file
.section_data(&classifier_section_header)
.expect("Failed to get .classifier section data").0;
.expect("Failed to get .classifier section data")
.0;

println!("{}", to_json(prog));
}
8 changes: 4 additions & 4 deletions src/asm_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

//! This module parses eBPF assembly language source code.

use combine::parser::char::{alpha_num, char, digit, hex_digit, spaces, string};
use combine::stream::position::{self};
#[cfg(feature = "std")]
use combine::EasyParser;
use combine::parser::char::{alpha_num, char, digit, hex_digit, spaces, string};
use combine::stream::position::{self};
use combine::{
attempt, between, eof, many, many1, one_of, optional, sep_by, ParseError, Parser, Stream,
ParseError, Parser, Stream, attempt, between, eof, many, many1, one_of, optional, sep_by,
};

use crate::lib::*;
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn parse(input: &str) -> Result<Vec<Instruction>, String> {
#[cfg(test)]
mod tests {

use super::{ident, instruction, integer, operand, parse, register, Instruction, Operand};
use super::{Instruction, Operand, ident, instruction, integer, operand, parse, register};
use crate::lib::*;
use combine::Parser;

Expand Down
9 changes: 5 additions & 4 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use self::InstructionType::{
LoadInd, LoadReg, NoOperand, StoreImm, StoreReg,
};
use crate::asm_parser::Operand::{Integer, Memory, Nil, Register};
use crate::asm_parser::{parse, Instruction, Operand};
use crate::asm_parser::{Instruction, Operand, parse};
use crate::ebpf;
use crate::ebpf::Insn;
use crate::lib::*;
Expand Down Expand Up @@ -216,9 +216,10 @@ fn assemble_internal(parsed: &[Instruction]) -> Result<Vec<Insn>, String> {
}
// Special case for lddw.
if let LoadImm = inst_type
&& let Integer(imm) = instruction.operands[1] {
result.push(insn(0, 0, 0, 0, imm >> 32).unwrap());
}
&& let Integer(imm) = instruction.operands[1]
{
result.push(insn(0, 0, 0, 0, imm >> 32).unwrap());
}
}
None => return Err(format!("Invalid instruction {name:?}")),
}
Expand Down
8 changes: 4 additions & 4 deletions src/cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use cranelift_codegen::{
entity::EntityRef,
ir::{
condcodes::IntCC,
types::{I16, I32, I64, I8},
AbiParam, Block, Endianness, FuncRef, Function, InstBuilder, MemFlags, Signature,
SourceLoc, StackSlotData, StackSlotKind, TrapCode, Type, UserFuncName, Value,
condcodes::IntCC,
types::{I8, I16, I32, I64},
},
isa::OwnedTargetIsa,
settings::{self, Configurable},
Expand All @@ -16,8 +16,8 @@ use cranelift_jit::{JITBuilder, JITModule};
use cranelift_module::{FuncId, Linkage, Module};

use crate::ebpf::{
self, Insn, BPF_ALU_OP_MASK, BPF_IND, BPF_JEQ, BPF_JGE, BPF_JGT, BPF_JLE, BPF_JLT, BPF_JMP32,
BPF_JNE, BPF_JSET, BPF_JSGE, BPF_JSGT, BPF_JSLE, BPF_JSLT, BPF_X, STACK_SIZE,
self, BPF_ALU_OP_MASK, BPF_IND, BPF_JEQ, BPF_JGE, BPF_JGT, BPF_JLE, BPF_JLT, BPF_JMP32,
BPF_JNE, BPF_JSET, BPF_JSGE, BPF_JSGT, BPF_JSLE, BPF_JSLT, BPF_X, Insn, STACK_SIZE,
};
use crate::lib::*;

Expand Down
21 changes: 14 additions & 7 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ fn check_mem(

Err(Error::other(format!(
"Error: out of bounds memory {} (insn #{:?}), addr {:#x}, size {:?}\nmbuff: {:#x}/{:#x}, mem: {:#x}/{:#x}, stack: {:#x}/{:#x}",
access_type, insn_ptr, addr, len,
mbuff.as_ptr() as u64, mbuff.len(),
mem.as_ptr() as u64, mem.len(),
stack.as_ptr() as u64, stack.len()
access_type,
insn_ptr,
addr,
len,
mbuff.as_ptr() as u64,
mbuff.len(),
mem.as_ptr() as u64,
mem.len(),
stack.as_ptr() as u64,
stack.len()
)))
}

Expand Down Expand Up @@ -117,9 +123,10 @@ pub fn execute_program(
while insn_ptr * ebpf::INSN_SIZE < prog.len() {
let insn = ebpf::get_insn(prog, insn_ptr);
if stack_frame_idx < MAX_CALL_DEPTH
&& let Some(usage) = stack_usage.stack_usage_for_local_func(insn_ptr) {
stacks[stack_frame_idx].set_stack_usage(usage);
}
&& let Some(usage) = stack_usage.stack_usage_for_local_func(insn_ptr)
{
stacks[stack_frame_idx].set_stack_usage(usage);
}
insn_ptr += 1;
let _dst = insn.dst as usize;
let _src = insn.src as usize;
Expand Down
35 changes: 28 additions & 7 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

#![allow(clippy::single_match)]

use crate::{ebpf, format, vec, Error, HashMap, Vec};
#[cfg(not(feature = "std"))]
use crate::ErrorKind;
use crate::{Error, HashMap, Vec, ebpf, format, vec};
use core::fmt::Error as FormatterError;
use core::fmt::Formatter;
use core::mem;
use core::ops::{Index, IndexMut};
use core::ptr;

#[cfg(target_arch = "riscv64")]
mod jit_riscv64;

type MachineCode = unsafe fn(*mut u8, usize, *mut u8, usize, usize, usize) -> u64;

const PAGE_SIZE: usize = 4096;
Expand Down Expand Up @@ -1049,9 +1052,18 @@ impl<'a> JitMemory<'a> {
offset: 0,
};

let mut jit = JitCompiler::new();
jit.jit_compile(&mut mem, prog, use_mbuff, update_data_ptr, helpers)?;
jit.resolve_jumps(&mut mem)?;
#[cfg(target_arch = "x86_64")]
{
let mut jit = JitCompiler::new();
jit.jit_compile(&mut mem, prog, use_mbuff, update_data_ptr, helpers)?;
jit.resolve_jumps(&mut mem)?;
}
#[cfg(target_arch = "riscv64")]
{
let mut jit = jit_riscv64::RiscV64Compiler::new();
jit.jit_compile(&mut mem, prog, use_mbuff, update_data_ptr, helpers)?;
jit.resolve_jumps(&mut mem)?;
}

Ok(mem)
}
Expand Down Expand Up @@ -1083,9 +1095,18 @@ impl<'a> JitMemory<'a> {
offset: 0,
};

let mut jit = JitCompiler::new();
jit.jit_compile(&mut mem, prog, use_mbuff, update_data_ptr, helpers)?;
jit.resolve_jumps(&mut mem)?;
#[cfg(target_arch = "x86_64")]
{
let mut jit = JitCompiler::new();
jit.jit_compile(&mut mem, prog, use_mbuff, update_data_ptr, helpers)?;
jit.resolve_jumps(&mut mem)?;
}
#[cfg(target_arch = "riscv64")]
{
let mut jit = jit_riscv64::RiscV64Compiler::new();
jit.jit_compile(&mut mem, prog, use_mbuff, update_data_ptr, helpers)?;
jit.resolve_jumps(&mut mem)?;
}

Ok(mem)
}
Expand Down
Loading
Loading