From 6ac5e42db3e915052b5e2d93126c903b16f6963f Mon Sep 17 00:00:00 2001 From: EKTehtris <86344566+EKTehtris@users.noreply.github.com> Date: Tue, 29 Jun 2021 03:31:59 +0200 Subject: [PATCH] fix: ability to target specific arch in aya-bpf + comments --- bpf/aya-bpf/build.rs | 9 +++ bpf/aya-bpf/src/programs/probe.rs | 127 ++++++++++-------------------- 2 files changed, 49 insertions(+), 87 deletions(-) create mode 100644 bpf/aya-bpf/build.rs diff --git a/bpf/aya-bpf/build.rs b/bpf/aya-bpf/build.rs new file mode 100644 index 00000000..f6ed9c89 --- /dev/null +++ b/bpf/aya-bpf/build.rs @@ -0,0 +1,9 @@ +use std::env; + +fn main() { + if !env::var("CARGO_CFG_BPF_TARGET_ARCH").is_ok() { + let arch = env::var("HOST").unwrap(); + let arch = arch.splitn(2, "-").next().unwrap(); + println!("cargo:rustc-cfg=bpf_target_arch=\"{}\"", arch); + } +} diff --git a/bpf/aya-bpf/src/programs/probe.rs b/bpf/aya-bpf/src/programs/probe.rs index 02963145..bef556e8 100644 --- a/bpf/aya-bpf/src/programs/probe.rs +++ b/bpf/aya-bpf/src/programs/probe.rs @@ -9,7 +9,7 @@ pub struct ProbeContext { impl ProbeContext { pub fn new(ctx: *mut c_void) -> ProbeContext { ProbeContext { - regs: Regs::from(ctx) + regs: Regs::from(ctx as *mut pt_regs) } } } @@ -24,9 +24,9 @@ pub struct Regs { regs: *mut pt_regs, } -impl From<*mut c_void> for Regs { - fn from(ctx: *mut c_void) -> Self { - Regs { regs: ctx as *mut pt_regs } +impl From<*mut pt_regs> for Regs { + fn from(ctx: *mut pt_regs) -> Self { + Regs { regs: ctx } } } @@ -38,141 +38,94 @@ impl Regs { self.regs.is_null() } - /// Utility to get the raw ptr, useful to interact with the context directly. - pub fn as_raw_ptr(&self) -> *mut c_void { - self.regs as *mut c_void - } - /// Utility to get the underlying regs, useful for advanced users to access the - /// underlying binding without having to recast. - pub fn as_regs(&self) -> *mut pt_regs { + /// underlying bindings. + pub fn as_mut_ptr(&self) -> *mut pt_regs { self.regs } + #[doc(hidden)] + /// Utility to get the raw ptr, useful to interact with the context directly. + pub(crate) fn as_raw_ptr(&self) -> *mut c_void { + self.regs as *mut c_void + } + + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_PARM1")] /// Utility to get the First Parameter - pub fn parm1(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rdi as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + pub fn parm1(&self) -> Option<::aya_bpf_cty::c_ulong> { + unsafe { &*(self.regs) }.rdi() } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_PARM2")] /// Utility to get the Second Parameter - pub fn parm2(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rsi as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + pub fn parm2(&self) -> Option<::aya_bpf_cty::c_ulong> { + unsafe { &*(self.regs) }.rsi() } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_PARM3")] /// Utility to get the Third Parameter - pub fn parm3(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rdx as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + pub fn parm3(&self) -> Option<::aya_bpf_cty::c_ulong> { + unsafe { &*(self.regs) }.rdx() } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_PARM4")] /// Utility to get the Fourth Parameter - pub fn parm4(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rcx as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + pub fn parm4(&self) -> Option<::aya_bpf_cty::c_ulong> { + unsafe { &*(self.regs) }.rcx() } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_PARM5")] /// Utility to get the Fifth Parameter - pub fn parm5(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.r8 as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + pub fn parm5(&self) -> Option<::aya_bpf_cty::c_ulong> { + unsafe { &*(self.regs) }.r8() } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_PARM6")] /// Utility to get the Sixth Parameter (not available for s390x) - pub fn parm6(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.r9 as *const c_void; - #[cfg(target_arch = "s390x")] - // panic!("Sixth parameter does not exists for s390x.") - return 0 as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + pub fn parm6(&self) -> Option<::aya_bpf_cty::c_ulong> { + unsafe { &*(self.regs) }.r9() } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_RET")] /// Utility to get the Stack Pointer pub fn ret(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rsp as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + unsafe { &*(self.regs) }.rsp as *const c_void } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_FP")] /// Utility to get the Frame Pointer /// Only available with CONFIG_FRAME_POINTER enabled on kernel. pub fn fp(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rbp as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + unsafe { &*(self.regs) }.rbp as *const c_void } - + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_RC")] /// Utility to get the Return Register pub fn rc(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rax as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + unsafe { &*(self.regs) }.rax as *const c_void } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_IP")] /// Utility to get the Instruction Pointer register pub fn ip(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rip as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + unsafe { &*(self.regs) }.rip as *const c_void } + #[cfg(any(bpf_target_arch = "x86_64", bpf_target_arch = "aarch64"))] #[doc(alias = "PT_REGS_SP")] /// Utility to get the Stack Pointer pub fn sp(&self) -> *const c_void { - // assert!(self.regs.is_null(),"You should ensure that the registers are not null"); - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - return unsafe { &*(self.regs) }.rsp as *const c_void; - // unreachable!(); - // panic!("Aya does not support this platform yet."); - return 0 as *const c_void; + unsafe { &*(self.regs) }.rsp as *const c_void } }