Skip to content

Commit fbcb2c5

Browse files
authored
Merge pull request rex-rs#43 from rex-rs/entry-generation
- rex/`*prog_types`: mark all `*_::new` helpers as `unsafe` and gate access via macros - rex-macros: apply clippy format-arg suggestions - rex{,-macros}: remove unneeded fields from program struct - rust: clean up leftover Rex entry generation logic - rex-macros: generate all entry functions via proc-macro - rust: remove entry generation of all program types from LLVM
2 parents 3357617 + ed55d9a commit fbcb2c5

File tree

12 files changed

+55
-116
lines changed

12 files changed

+55
-116
lines changed

rex-macros/src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ macro_rules! pop_string_args {
1414
pub(crate) fn parse_string_args(
1515
input: TokenStream,
1616
) -> Result<HashMap<String, LitStr>> {
17-
let parsed: syn::ExprArray = parse_str(&format!("[{}]", input))?;
17+
let parsed: syn::ExprArray = parse_str(&format!("[{input}]"))?;
1818
let mut map = HashMap::new();
1919

2020
// Iterate over the expressions and extract key-value pairs

rex-macros/src/kprobe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ impl KProbe {
4747
pub(crate) fn expand(&self, flavor: KprobeFlavor) -> Result<TokenStream> {
4848
let fn_name = self.item.sig.ident.clone();
4949
let item = &self.item;
50-
let function_name = format!("{}", fn_name);
50+
let function_name = format!("{fn_name}");
5151
let prog_ident =
5252
format_ident!("PROG_{}", fn_name.to_string().to_uppercase());
5353

5454
let attached_function = if self.function.is_some() {
5555
format!("rex/{}/{}", flavor, self.function.as_ref().unwrap())
5656
} else {
57-
format!("rex/{}", flavor)
57+
format!("rex/{flavor}")
5858
};
5959

6060
let entry_name = format_ident!("__rex_entry_{}", fn_name);
@@ -65,7 +65,7 @@ impl KProbe {
6565

6666
#[used]
6767
static #prog_ident: kprobe =
68-
kprobe::new(#fn_name, #function_name);
68+
unsafe { kprobe::new(#fn_name) };
6969

7070
#[unsafe(export_name = #function_name)]
7171
#[unsafe(link_section = #attached_function)]

rex-macros/src/perf_event.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@ impl PerfEvent {
2020
// TODO: section may update in the future
2121
let fn_name = self.item.sig.ident.clone();
2222
let item = &self.item;
23-
let function_name = format!("{}", fn_name);
23+
let function_name = format!("{fn_name}");
2424
let prog_ident =
2525
format_ident!("PROG_{}", fn_name.to_string().to_uppercase());
2626

27+
let entry_name = format_ident!("__rex_entry_{}", fn_name);
28+
2729
let function_body_tokens = quote! {
2830
#[inline(always)]
2931
#item
3032

3133
#[used]
32-
#[unsafe(link_section = "rex/perf_event")]
3334
static #prog_ident: perf_event =
34-
perf_event::new(#fn_name, #function_name);
35+
unsafe { perf_event::new(#fn_name) };
36+
37+
#[unsafe(export_name = #function_name)]
38+
#[unsafe(link_section = "rex/perf_event")]
39+
extern "C" fn #entry_name(ctx: *mut ()) -> u32 {
40+
use rex::prog_type::rex_prog;
41+
#prog_ident.prog_run(ctx)
42+
}
3543
};
3644
Ok(function_body_tokens)
3745
}

rex-macros/src/tc.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,26 @@ impl SchedCls {
2424
// TODO: section may update in the future
2525
let fn_name = self.item.sig.ident.clone();
2626
let item = &self.item;
27-
let function_name = format!("{}", fn_name);
27+
let function_name = format!("{fn_name}");
2828
let prog_ident =
2929
format_ident!("PROG_{}", fn_name.to_string().to_uppercase());
3030

31+
let entry_name = format_ident!("__rex_entry_{}", fn_name);
32+
3133
let function_body_tokens = quote! {
3234
#[inline(always)]
3335
#item
3436

3537
#[used]
36-
#[unsafe(link_section = "rex/tc")]
3738
static #prog_ident: sched_cls =
38-
sched_cls::new(#fn_name, #function_name);
39+
unsafe { sched_cls::new(#fn_name) };
40+
41+
#[unsafe(export_name = #function_name)]
42+
#[unsafe(link_section = "rex/tc")]
43+
extern "C" fn #entry_name(ctx: *mut ()) -> u32 {
44+
use rex::prog_type::rex_prog;
45+
#prog_ident.prog_run(ctx)
46+
}
3947
};
4048
Ok(function_body_tokens)
4149
}

rex-macros/src/tracepoint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl TracePoint {
4444

4545
// other tracepoint pieces
4646
let item = &self.item;
47-
let function_name = format!("{}", fn_name);
47+
let function_name = format!("{fn_name}");
4848
let prog_ident =
4949
format_ident!("PROG_{}", fn_name.to_string().to_uppercase());
5050

@@ -58,7 +58,7 @@ impl TracePoint {
5858
"RawSyscallsExitCtx" => "raw_syscalls/sys_exit",
5959
_ => abort_call_site!("Please provide a valid context type. If your needed context isn't supported consider opening a PR!"),
6060
};
61-
let attached_name = format!("rex/tracepoint/{}", hook_point_name);
61+
let attached_name = format!("rex/tracepoint/{hook_point_name}");
6262

6363
let entry_name = format_ident!("__rex_entry_{}", fn_name);
6464

@@ -68,7 +68,7 @@ impl TracePoint {
6868

6969
#[used]
7070
static #prog_ident: tracepoint<#full_context_type> =
71-
tracepoint::new(#fn_name, #function_name);
71+
unsafe { tracepoint::new(#fn_name) };
7272

7373
#[unsafe(export_name = #function_name)]
7474
#[unsafe(link_section = #attached_name)]

rex-macros/src/xdp.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@ impl Xdp {
2121
// TODO: section may update in the future
2222
let fn_name = self.item.sig.ident.clone();
2323
let item = &self.item;
24-
let function_name = format!("{}", fn_name);
24+
let function_name = format!("{fn_name}");
2525
let prog_ident =
2626
format_ident!("PROG_{}", fn_name.to_string().to_uppercase());
2727

28+
let entry_name = format_ident!("__rex_entry_{}", fn_name);
29+
2830
let function_body_tokens = quote! {
2931
#[inline(always)]
3032
#item
3133

3234
#[used]
33-
#[unsafe(link_section = "rex/xdp")]
3435
static #prog_ident: xdp =
35-
xdp::new(#fn_name, #function_name);
36+
unsafe { xdp::new(#fn_name) };
37+
38+
#[unsafe(export_name = #function_name)]
39+
#[unsafe(link_section = "rex/xdp")]
40+
extern "C" fn #entry_name(ctx: *mut ()) -> u32 {
41+
use rex::prog_type::rex_prog;
42+
#prog_ident.prog_run(ctx)
43+
}
3644
};
3745
Ok(function_body_tokens)
3846
}

rex/src/kprobe/kprobe_impl.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
use crate::bindings::uapi::linux::bpf::{bpf_map_type, BPF_PROG_TYPE_KPROBE};
1+
use crate::bindings::uapi::linux::bpf::bpf_map_type;
22
use crate::prog_type::rex_prog;
33
use crate::pt_regs::PtRegs;
44
use crate::task_struct::TaskStruct;
55
use crate::{ffi, Result};
66

7-
/// First 3 fields should always be rtti, prog_fn, and name
8-
///
9-
/// rtti should be u64, therefore after compiling the
10-
/// packed struct type rustc generates for LLVM does
11-
/// not additional padding after rtti
12-
///
137
/// prog_fn should have &Self as its first argument
14-
///
15-
/// name is a &'static str
168
#[repr(C)]
179
pub struct kprobe {
18-
rtti: u64,
1910
prog: fn(&Self, &mut PtRegs) -> Result,
20-
name: &'static str,
2111
}
2212

2313
impl kprobe {
2414
crate::base_helper::base_helper_defs!();
2515

26-
pub const fn new(
27-
f: fn(&kprobe, &mut PtRegs) -> Result,
28-
nm: &'static str,
29-
) -> kprobe {
30-
Self {
31-
rtti: BPF_PROG_TYPE_KPROBE as u64,
32-
prog: f,
33-
name: nm,
34-
}
16+
pub const unsafe fn new(f: fn(&kprobe, &mut PtRegs) -> Result) -> kprobe {
17+
Self { prog: f }
3518
}
3619

3720
// Now returns a mutable ref, but since every reg is private the user prog

rex/src/perf_event/perf_event_impl.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use core::intrinsics::unlikely;
22

33
use crate::base_helper::termination_check;
44
use crate::bindings::linux::kernel::bpf_perf_event_data_kern;
5-
use crate::bindings::uapi::linux::bpf::{
6-
bpf_map_type, bpf_perf_event_value, BPF_PROG_TYPE_PERF_EVENT,
7-
};
5+
use crate::bindings::uapi::linux::bpf::{bpf_map_type, bpf_perf_event_value};
86
use crate::ffi;
97
use crate::linux::errno::EINVAL;
108
use crate::map::*;
@@ -35,34 +33,19 @@ impl bpf_perf_event_data {
3533
}
3634
}
3735

38-
/// First 3 fields should always be rtti, prog_fn, and name
39-
///
40-
/// rtti should be u64, therefore after compiling the
41-
/// packed struct type rustc generates for LLVM does
42-
/// not additional padding after rtti
43-
///
4436
/// prog_fn should have &Self as its first argument
45-
///
46-
/// name is a &'static str
4737
#[repr(C)]
4838
pub struct perf_event {
49-
rtti: u64,
5039
prog: fn(&Self, &bpf_perf_event_data) -> Result,
51-
name: &'static str,
5240
}
5341

5442
impl perf_event {
5543
crate::base_helper::base_helper_defs!();
5644

57-
pub const fn new(
45+
pub const unsafe fn new(
5846
f: fn(&perf_event, &bpf_perf_event_data) -> Result,
59-
nm: &'static str,
6047
) -> perf_event {
61-
Self {
62-
rtti: BPF_PROG_TYPE_PERF_EVENT as u64,
63-
prog: f,
64-
name: nm,
65-
}
48+
Self { prog: f }
6649
}
6750

6851
fn convert_ctx(&self, ctx: *mut ()) -> &'static bpf_perf_event_data {

rex/src/sched_cls/sched_cls_impl.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::bindings::linux::kernel::{
66
ethhdr, iphdr, sk_buff, sock, tcphdr, udphdr,
77
};
88
use crate::bindings::uapi::linux::bpf::bpf_map_type;
9-
pub use crate::bindings::uapi::linux::bpf::BPF_PROG_TYPE_SCHED_CLS;
109
pub use crate::bindings::uapi::linux::pkt_cls::{
1110
TC_ACT_OK, TC_ACT_REDIRECT, TC_ACT_SHOT,
1211
};
@@ -165,35 +164,19 @@ impl<'a> __sk_buff<'a> {
165164
}
166165
}
167166

168-
/// First 3 fields should always be rtti, prog_fn, and name
169-
///
170-
/// rtti should be u64, therefore after compiling the
171-
/// packed struct type rustc generates for LLVM does
172-
/// not additional padding after rtti
173-
///
174167
/// prog_fn should have &Self as its first argument
175-
///
176-
/// name is a &'static str
177168
#[repr(C)]
178169
pub struct sched_cls {
179-
rtti: u64,
180170
prog: fn(&Self, &mut __sk_buff) -> Result,
181-
name: &'static str,
182171
}
183172

184173
impl sched_cls {
185174
crate::base_helper::base_helper_defs!();
186175

187-
pub const fn new(
188-
// TODO update based on signature
176+
pub const unsafe fn new(
189177
f: fn(&sched_cls, &mut __sk_buff) -> Result,
190-
nm: &'static str,
191178
) -> sched_cls {
192-
Self {
193-
rtti: BPF_PROG_TYPE_SCHED_CLS as u64,
194-
prog: f,
195-
name: nm,
196-
}
179+
Self { prog: f }
197180
}
198181

199182
// NOTE: copied from xdp impl, may change in the future

rex/src/tracepoint/tp_impl.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use super::{
44
SyscallsExitOpenatCtx,
55
};
66
use crate::base_helper::termination_check;
7-
use crate::bindings::uapi::linux::bpf::{
8-
bpf_map_type, BPF_PROG_TYPE_TRACEPOINT,
9-
};
7+
use crate::bindings::uapi::linux::bpf::bpf_map_type;
108
use crate::map::RexPerfEventArray;
119
use crate::prog_type::rex_prog;
1210
use crate::task_struct::TaskStruct;
@@ -22,34 +20,19 @@ impl TracepointContext for SyscallsEnterDupCtx {}
2220
impl TracepointContext for RawSyscallsEnterCtx {}
2321
impl TracepointContext for RawSyscallsExitCtx {}
2422

25-
/// First 3 fields should always be rtti, prog_fn, and name
26-
///
27-
/// rtti should be u64, therefore after compiling the
28-
/// packed struct type rustc generates for LLVM does
29-
/// not additional padding after rtti
30-
///
3123
/// prog_fn should have &Self as its first argument
32-
///
33-
/// name is a &'static str
3424
#[repr(C)]
3525
pub struct tracepoint<C: TracepointContext + 'static> {
36-
rtti: u64,
3726
prog: fn(&Self, &'static C) -> Result,
38-
name: &'static str,
3927
}
4028

4129
impl<C: TracepointContext + 'static> tracepoint<C> {
4230
crate::base_helper::base_helper_defs!();
4331

44-
pub const fn new(
32+
pub const unsafe fn new(
4533
f: fn(&tracepoint<C>, &'static C) -> Result,
46-
nm: &'static str,
4734
) -> tracepoint<C> {
48-
Self {
49-
rtti: BPF_PROG_TYPE_TRACEPOINT as u64,
50-
prog: f,
51-
name: nm,
52-
}
35+
Self { prog: f }
5336
}
5437

5538
fn convert_ctx(&self, ctx: *mut ()) -> &'static C {

0 commit comments

Comments
 (0)