Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous fixes to allow running integration tests in bpf-linker #641

Merged
merged 7 commits into from
Jul 10, 2023
Merged
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
1 change: 1 addition & 0 deletions aya-log-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub fn write_record_header(
mod test {
use super::*;

#[test]
fn log_value_length_sufficient() {
assert!(
LOG_BUF_CAPACITY >= LogValueLength::MAX.into(),
Expand Down
2 changes: 1 addition & 1 deletion aya-obj/src/btf/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub enum BtfError {
#[source]
io_error: std::io::Error,
/// The error log produced by the kernel verifier.
verifier_log: String,
verifier_log: Cow<'static, str>,
},

/// offset not found for symbol
Expand Down
70 changes: 23 additions & 47 deletions aya-obj/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct Object {
/// Program license
pub license: CString,
/// Kernel version
pub kernel_version: KernelVersion,
pub kernel_version: Option<u32>,
/// Program BTF
pub btf: Option<Btf>,
/// Program BTF.ext
Expand All @@ -135,7 +135,7 @@ pub struct Program {
/// The license
pub license: CString,
/// The kernel version
pub kernel_version: KernelVersion,
pub kernel_version: Option<u32>,
/// The section containing the program
pub section: ProgramSection,
/// The section index of the program
Expand Down Expand Up @@ -579,7 +579,7 @@ impl Object {
let kernel_version = if let Some(section) = obj.section_by_name("version") {
parse_version(Section::try_from(&section)?.data, endianness)?
} else {
KernelVersion::Any
None
};

let mut bpf_obj = Object::new(endianness, license, kernel_version);
Expand Down Expand Up @@ -631,7 +631,7 @@ impl Object {
Ok(bpf_obj)
}

fn new(endianness: Endianness, license: CString, kernel_version: KernelVersion) -> Object {
fn new(endianness: Endianness, license: CString, kernel_version: Option<u32>) -> Object {
Object {
endianness,
license,
Expand Down Expand Up @@ -1256,7 +1256,7 @@ fn parse_license(data: &[u8]) -> Result<CString, ParseError> {
.to_owned())
}

fn parse_version(data: &[u8], endianness: object::Endianness) -> Result<KernelVersion, ParseError> {
fn parse_version(data: &[u8], endianness: object::Endianness) -> Result<Option<u32>, ParseError> {
let data = match data.len() {
4 => data.try_into().unwrap(),
_ => {
Expand All @@ -1271,9 +1271,10 @@ fn parse_version(data: &[u8], endianness: object::Endianness) -> Result<KernelVe
object::Endianness::Little => u32::from_le_bytes(data),
};

Ok(match v {
KERNEL_VERSION_ANY => KernelVersion::Any,
v => KernelVersion::Version(v),
Ok(if v == KERNEL_VERSION_ANY {
None
} else {
Some(v)
})
}

Expand Down Expand Up @@ -1301,24 +1302,6 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
Ok(arr.len)
}

/// The parsed kernel version
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum KernelVersion {
/// Specified version
Version(u32),
/// Any version
Any,
}

impl From<KernelVersion> for u32 {
fn from(version: KernelVersion) -> u32 {
match version {
KernelVersion::Any => KERNEL_VERSION_ANY,
KernelVersion::Version(v) => v,
}
}
}

// Parsed '.bss' '.data' and '.rodata' sections. These sections are arrays of
// bytes and are relocated based on their section index.
fn parse_data_map_section(section: &Section) -> Result<Map, ParseError> {
Expand Down Expand Up @@ -1592,23 +1575,20 @@ mod tests {
Err(ParseError::InvalidKernelVersion { .. })
));

assert_eq!(
parse_version(&0xFFFF_FFFEu32.to_le_bytes(), Endianness::Little)
.expect("failed to parse magic version"),
KernelVersion::Any
);
assert!(matches!(
parse_version(&0xFFFF_FFFEu32.to_le_bytes(), Endianness::Little),
Ok(None)
));

assert_eq!(
parse_version(&0xFFFF_FFFEu32.to_be_bytes(), Endianness::Big)
.expect("failed to parse magic version"),
KernelVersion::Any
);
assert!(matches!(
parse_version(&0xFFFF_FFFEu32.to_be_bytes(), Endianness::Big),
Ok(None)
));

assert_eq!(
parse_version(&1234u32.to_le_bytes(), Endianness::Little)
.expect("failed to parse magic version"),
KernelVersion::Version(1234)
);
assert!(matches!(
parse_version(&1234u32.to_le_bytes(), Endianness::Little),
Ok(Some(1234))
));
}

#[test]
Expand Down Expand Up @@ -1699,11 +1679,7 @@ mod tests {
}

fn fake_obj() -> Object {
Object::new(
Endianness::Little,
CString::new("GPL").unwrap(),
KernelVersion::Any,
)
Object::new(Endianness::Little, CString::new("GPL").unwrap(), None)
}

#[test]
Expand Down Expand Up @@ -1753,7 +1729,7 @@ mod tests {
obj.parse_program(&fake_section(BpfSectionKind::Program,"kprobe/foo", bytes_of(&fake_ins()))),
Ok((Program {
license,
kernel_version: KernelVersion::Any,
kernel_version: None,
section: ProgramSection::KProbe { .. },
.. }, Function {
name,
Expand Down
26 changes: 18 additions & 8 deletions aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ documentation = "https://docs.rs/aya"
edition = "2021"

[dependencies]
libc = { version = "0.2.105" }
async-io = { version = "1.3", optional = true }
aya-obj = { path = "../aya-obj", version = "0.1.0", features = ["std"] }
thiserror = "1"
object = { version = "0.31", default-features = false, features = ["std", "read_core", "elf"] }
bitflags = "2.2.1"
bytes = "1"
lazy_static = "1"
parking_lot = { version = "0.12.0", features = ["send_guard"] }
tokio = { version = "1.24.0", features = ["macros", "rt", "rt-multi-thread", "net"], optional = true }
async-io = { version = "1.3", optional = true }
libc = { version = "0.2.105" }
log = "0.4"
object = { version = "0.31", default-features = false, features = [
"std",
"read_core",
"elf",
] }
parking_lot = { version = "0.12.0", features = ["send_guard"] }
thiserror = "1"
tokio = { version = "1.24.0", features = [
"macros",
"rt",
"rt-multi-thread",
"net",
], optional = true }
procfs = { version = "0.15.1", default-features = false }

[dev-dependencies]
matches = "0.1.8"
futures = { version = "0.3.12", default-features = false, features = ["std"] }
matches = "0.1.8"

[features]
default = []
Expand All @@ -35,4 +45,4 @@ async_std = ["async-io", "async"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs","-D", "warnings"]
rustdoc-args = ["--cfg", "docsrs", "-D", "warnings"]
Loading