Skip to content
This repository was archived by the owner on Dec 6, 2025. It is now read-only.
Open
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
12 changes: 6 additions & 6 deletions crates/ezbpf-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ edition = "2021"
authors = ["Dean Little <@deanmlittle>"]

[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.4", features = ["derive"] }
clap_derive = { version = "4.5.5" }
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
ezbpf-core = { path = "../ezbpf-core" }
anyhow = "1.0"
clap = { version = "4.5", features = ["derive"] }
clap_derive = { version = "4.5" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
ezbpf-core = { path = "../ezbpf-core" }
19 changes: 16 additions & 3 deletions crates/ezbpf-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Result;
use clap::Parser;
use ezbpf_core::errors::EZBpfError;
use ezbpf_core::program::Program;
use std::fs::File;
use std::io::Read;
Expand All @@ -22,8 +21,22 @@ fn main() -> Result<()> {
file.read_to_end(&mut b)?;
let program = Program::from_bytes(b.as_ref())?;
match args.asm {
Some(_) => println!("{}", program.section_header_entries.iter().map(|h| h.ixs.clone()).filter(|ixs| !ixs.is_empty()).map(|ixs| ixs.iter().map(|i| i.to_asm().unwrap()).collect::<Vec<String>>().join("\n")).collect::<Vec<String>>().join("\n")),
None => println!("{}", serde_json::to_string_pretty(&program)?)
Some(_) => println!(
"{}",
program
.section_header_entries
.iter()
.map(|h| h.ixs.clone())
.filter(|ixs| !ixs.is_empty())
.map(|ixs| ixs
.iter()
.map(|i| i.to_asm().unwrap())
.collect::<Vec<String>>()
.join("\n"))
.collect::<Vec<String>>()
.join("\n")
),
None => println!("{}", serde_json::to_string_pretty(&program)?),
}
Ok(())
}
12 changes: 6 additions & 6 deletions crates/ezbpf-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ authors = ["Dean Little <@deanmlittle>"]
name = "ezbpf_core"

[dependencies]
anyhow = "1.0.86"
hex = "0.4.3"
thiserror = "1.0.61"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
anyhow = "1.0"
hex = "0.4"
thiserror = "2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dev-dependencies]
hex-literal = "0.4.1"
hex-literal = "1.0"
3 changes: 2 additions & 1 deletion crates/ezbpf-core/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::io::{Cursor, Read, Seek, SeekFrom};

use crate::{
elf_header::{
ELFHeader, EI_ABIVERSION, EI_CLASS, EI_DATA, EI_MAGIC, EI_OSABI, EI_PAD, EI_VERSION, E_MACHINE, E_MACHINE_SBPF, E_TYPE, E_VERSION
ELFHeader, EI_ABIVERSION, EI_CLASS, EI_DATA, EI_MAGIC, EI_OSABI, EI_PAD, EI_VERSION,
E_MACHINE, E_MACHINE_SBPF, E_TYPE, E_VERSION,
},
errors::EZBpfError,
instructions::Ix,
Expand Down
4 changes: 2 additions & 2 deletions crates/ezbpf-core/src/elf_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ mod tests {
let h = ELFHeader::from_bytes(&b).unwrap();
assert_eq!(h.to_bytes(), &b)
}

#[test]
fn serialize_sbpf_machine_e2e() {
let b = hex!("7F454C46020101000000000000000000030007010100000020010000000000004000000000000000680200000000000000000000400038000300400006000500");
let h = ELFHeader::from_bytes(&b).unwrap();
assert_eq!(h.to_bytes(), &b)
}
}
}
2 changes: 1 addition & 1 deletion crates/ezbpf-core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pub enum EZBpfError {
#[error("Invalid data length")]
InvalidDataLength,
#[error("Invalid string")]
InvalidString
InvalidString,
}
29 changes: 16 additions & 13 deletions crates/ezbpf-core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,23 @@ impl Program {
let mut indices: Vec<u32> = section_headers.iter().map(|h| h.sh_name).collect();
indices.push(shstrndx.sh_size as u32);
indices.sort_unstable();

let section_header_entries = section_headers.iter().map(|s| {
let current_offset = s.sh_name as usize;
let next_index = indices.binary_search(&s.sh_name).unwrap() + 1 as usize;
let next_offset = *indices.get(next_index).ok_or(EZBpfError::InvalidString)? as usize;

let label = String::from_utf8(
shstrndx_value[current_offset..next_offset].to_vec(),
).unwrap_or("default".to_string());
let data = b[s.sh_offset as usize..s.sh_offset as usize + s.sh_size as usize].to_vec();
let section_header_entries = section_headers
.iter()
.map(|s| {
let current_offset = s.sh_name as usize;
let next_index = indices.binary_search(&s.sh_name).unwrap() + 1_usize;
let next_offset =
*indices.get(next_index).ok_or(EZBpfError::InvalidString)? as usize;

SectionHeaderEntry::new(label, s.sh_offset as usize, data)
}).collect::<Result<Vec<_>, _>>()?;
let label = String::from_utf8(shstrndx_value[current_offset..next_offset].to_vec())
.unwrap_or("default".to_string());
let data =
b[s.sh_offset as usize..s.sh_offset as usize + s.sh_size as usize].to_vec();

SectionHeaderEntry::new(label, s.sh_offset as usize, data)
})
.collect::<Result<Vec<_>, _>>()?;

Ok(Self {
elf_header,
Expand All @@ -61,7 +65,6 @@ impl Program {
}
}


#[cfg(test)]
mod tests {
use hex_literal::hex;
Expand All @@ -73,4 +76,4 @@ mod tests {
let program = Program::from_bytes(&hex!("7F454C460201010000000000000000000300F700010000002001000000000000400000000000000028020000000000000000000040003800030040000600050001000000050000002001000000000000200100000000000020010000000000003000000000000000300000000000000000100000000000000100000004000000C001000000000000C001000000000000C0010000000000003C000000000000003C000000000000000010000000000000020000000600000050010000000000005001000000000000500100000000000070000000000000007000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007912A000000000007911182900000000B7000000010000002D21010000000000B70000000000000095000000000000001E0000000000000004000000000000000600000000000000C0010000000000000B0000000000000018000000000000000500000000000000F0010000000000000A000000000000000C00000000000000160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000120001002001000000000000300000000000000000656E747279706F696E7400002E74657874002E64796E737472002E64796E73796D002E64796E616D6963002E73687374727461620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010000000600000000000000200100000000000020010000000000003000000000000000000000000000000008000000000000000000000000000000170000000600000003000000000000005001000000000000500100000000000070000000000000000400000000000000080000000000000010000000000000000F0000000B0000000200000000000000C001000000000000C001000000000000300000000000000004000000010000000800000000000000180000000000000007000000030000000200000000000000F001000000000000F0010000000000000C00000000000000000000000000000001000000000000000000000000000000200000000300000000000000000000000000000000000000FC010000000000002A00000000000000000000000000000001000000000000000000000000000000")).unwrap();
println!("{:?}", program.section_header_entries);
}
}
}
15 changes: 5 additions & 10 deletions crates/ezbpf-core/src/section_header_entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{fmt::Debug, io::Cursor};

use serde::{ser::Error, Deserialize, Serialize, Serializer};
use serde_json::{error, Map, Value};
use serde::{Deserialize, Serialize};

use crate::{cursor::ELFCursor, errors::EZBpfError, instructions::Ix};

Expand All @@ -13,17 +12,17 @@ pub struct SectionHeaderEntry {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub ixs: Vec<Ix>,
#[serde(skip_serializing_if = "String::is_empty")]
pub utf8: String
pub utf8: String,
}

impl SectionHeaderEntry {
pub fn new(label: String, offset: usize, data: Vec<u8>) -> Result<Self, EZBpfError> {
let mut h = SectionHeaderEntry {
label,
offset: offset,
offset,
data,
ixs: vec![],
utf8: String::new()
utf8: String::new(),
};

if &h.label == ".text\0" {
Expand Down Expand Up @@ -70,11 +69,7 @@ mod test {
0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let h = SectionHeaderEntry::new(
".text\0".to_string(),
128,
data.clone()
).unwrap();
let h = SectionHeaderEntry::new(".text\0".to_string(), 128, data.clone()).unwrap();

let ixs = vec![
Ix {
Expand Down
16 changes: 8 additions & 8 deletions crates/ezbpf-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ name = "ezbpf_wasm"
crate-type = ["cdylib", "rlib"]

[dependencies]
anyhow = "1.0.86"
wasm-bindgen = "0.2.92"
hex = "0.4.3"
anyhow = "1.0"
wasm-bindgen = "0.2"
hex = "0.4"
ezbpf-core = { path = "../ezbpf-core" }
thiserror = "1.0.61"
serde = { version = "1.0.203", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.117"
thiserror = "2.0"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.6"
serde_json = "1.0"

[dev-dependencies]
hex-literal = "0.4.1"
hex-literal = "1.0"
2 changes: 1 addition & 1 deletion crates/ezbpf-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ impl Program {
pub fn to_json(&self) -> Result<JsValue, JsValue> {
to_value(&self.inner).map_err(|e| JsValue::from_str(&e.to_string()))
}
}
}