Skip to content

Commit ef62591

Browse files
d-e-s-odanielocfb
authored andcommitted
Add test checking generation of struct ops definitions
Add a test that checks the generation of a struct ops type definition containing variables from two datasecs. Signed-off-by: Daniel Müller <[email protected]>
1 parent 3d49a82 commit ef62591

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

libbpf-cargo/src/test.rs

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,15 +1283,8 @@ fn btf_from_mmap(mmap: &Mmap) -> GenBtf<'_> {
12831283
GenBtf::from(btf)
12841284
}
12851285

1286-
/// Tests the type_definition output of a type_id against a given expected output
1287-
/// Will trim leading and trailing whitespace from both expected output and from
1288-
/// the generated type_definition
1289-
/// fails calling text if type_definition does not match expected_output
12901286
#[track_caller]
1291-
fn assert_definition(btf: &GenBtf<'_>, btf_item: &BtfType<'_>, expected_output: &str) {
1292-
let actual_output = btf
1293-
.type_definition(*btf_item, &mut HashSet::new())
1294-
.expect("Failed to generate struct Foo defn");
1287+
fn assert_output(actual_output: &str, expected_output: &str) {
12951288
let ao = actual_output.trim_end().trim_start();
12961289
let eo = expected_output.trim_end().trim_start();
12971290

@@ -1307,6 +1300,18 @@ fn assert_definition(btf: &GenBtf<'_>, btf_item: &BtfType<'_>, expected_output:
13071300
assert!(eo == ao);
13081301
}
13091302

1303+
/// Tests the type_definition output of a type_id against a given expected output
1304+
/// Will trim leading and trailing whitespace from both expected output and from
1305+
/// the generated type_definition
1306+
/// fails calling text if type_definition does not match expected_output
1307+
#[track_caller]
1308+
fn assert_definition(btf: &GenBtf<'_>, btf_item: &BtfType<'_>, expected_output: &str) {
1309+
let actual_output = btf
1310+
.type_definition(*btf_item, &mut HashSet::new())
1311+
.expect("Failed to generate struct Foo defn");
1312+
assert_output(&actual_output, expected_output)
1313+
}
1314+
13101315
#[test]
13111316
fn test_btf_dump_reserved_keyword_escaping() {
13121317
let prog_text = r#"
@@ -2731,6 +2736,88 @@ pub struct __anon_4 {
27312736
assert_definition(&btf, &struct_bpf_sock_tuple, expected_output);
27322737
}
27332738

2739+
#[test]
2740+
fn test_btf_dump_definition_struct_ops_mixed() {
2741+
let prog_text = r#"
2742+
#include "vmlinux.h"
2743+
#include <bpf/bpf_helpers.h>
2744+
#include <bpf/bpf_tracing.h>
2745+
2746+
SEC("struct_ops/test_1")
2747+
int BPF_PROG(test_1, struct bpf_dummy_ops_state *state)
2748+
{
2749+
return 0;
2750+
}
2751+
2752+
SEC(".struct_ops")
2753+
struct bpf_dummy_ops dummy_1 = {
2754+
.test_1 = (void *)test_1,
2755+
};
2756+
2757+
SEC(".struct_ops.link")
2758+
struct bpf_dummy_ops dummy_2 = {
2759+
.test_1 = (void *)test_1,
2760+
};
2761+
"#;
2762+
2763+
let expected_output = r#"
2764+
#[derive(Debug, Clone)]
2765+
#[repr(C)]
2766+
pub struct struct_ops {
2767+
pub dummy_1: *mut bpf_dummy_ops,
2768+
pub dummy_2: *mut bpf_dummy_ops,
2769+
}
2770+
2771+
impl struct_ops {
2772+
2773+
pub fn dummy_1(&self) -> &bpf_dummy_ops {
2774+
// SAFETY: The library ensures that the member is pointing to
2775+
// valid data.
2776+
unsafe { self.dummy_1.as_ref() }.unwrap()
2777+
}
2778+
2779+
pub fn dummy_1_mut(&mut self) -> &mut bpf_dummy_ops {
2780+
// SAFETY: The library ensures that the member is pointing to
2781+
// valid data.
2782+
unsafe { self.dummy_1.as_mut() }.unwrap()
2783+
}
2784+
2785+
pub fn dummy_2(&self) -> &bpf_dummy_ops {
2786+
// SAFETY: The library ensures that the member is pointing to
2787+
// valid data.
2788+
unsafe { self.dummy_2.as_ref() }.unwrap()
2789+
}
2790+
2791+
pub fn dummy_2_mut(&mut self) -> &mut bpf_dummy_ops {
2792+
// SAFETY: The library ensures that the member is pointing to
2793+
// valid data.
2794+
unsafe { self.dummy_2.as_mut() }.unwrap()
2795+
}
2796+
}
2797+
#[derive(Debug, Copy, Clone)]
2798+
#[repr(C)]
2799+
pub struct bpf_dummy_ops {
2800+
pub test_1: *mut libbpf_rs::libbpf_sys::bpf_program,
2801+
pub test_2: *mut libbpf_rs::libbpf_sys::bpf_program,
2802+
}
2803+
impl Default for bpf_dummy_ops {
2804+
fn default() -> Self {
2805+
bpf_dummy_ops {
2806+
test_1: std::ptr::null_mut(),
2807+
test_2: std::ptr::null_mut(),
2808+
}
2809+
}
2810+
}
2811+
"#;
2812+
2813+
let mmap = build_btf_mmap(prog_text);
2814+
let btf = btf_from_mmap(&mmap);
2815+
let mut processed = HashSet::new();
2816+
let def = btf.struct_ops_type_definition(&mut processed).unwrap();
2817+
2818+
assert_output(&def, expected_output);
2819+
}
2820+
27342821
#[test]
27352822
fn test_btf_dump_float() {
27362823
let prog_text = r#"

0 commit comments

Comments
 (0)