Skip to content

Commit 7745f2e

Browse files
d-e-s-odanielocfb
authored andcommitted
libbpf-cargo: Add test for special sauce section names
Add a test for custom section names containing dots in them. Also make escaping of said names a more comprehensive thing, fixing another location where we did not escape stuff. Signed-off-by: Daniel Müller <[email protected]>
1 parent 8fd6424 commit 7745f2e

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

libbpf-cargo/src/gen/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ use crate::metadata::UnprocessedObj;
3636

3737
use self::btf::GenBtf;
3838

39+
/// Escape certain characters in a "raw" name of a section, for example.
40+
fn escape_raw_name(name: &str) -> String {
41+
name.replace('.', "_")
42+
}
43+
3944
#[derive(Debug, PartialEq)]
4045
pub(crate) enum InternalMapType<'name> {
4146
Data,
@@ -52,11 +57,11 @@ impl Display for InternalMapType<'_> {
5257
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
5358
match self {
5459
Self::Data => write!(f, "data"),
55-
Self::CustomData(name) => write!(f, "data_{}", name),
60+
Self::CustomData(name) => write!(f, "data_{}", escape_raw_name(name)),
5661
Self::Rodata => write!(f, "rodata"),
57-
Self::CustomRodata(name) => write!(f, "rodata_{}", name),
62+
Self::CustomRodata(name) => write!(f, "rodata_{}", escape_raw_name(name)),
5863
Self::Bss => write!(f, "bss"),
59-
Self::CustomBss(name) => write!(f, "bss_{}", name),
64+
Self::CustomBss(name) => write!(f, "bss_{}", escape_raw_name(name)),
6065
Self::Kconfig => write!(f, "kconfig"),
6166
Self::StructOps => write!(f, "struct_ops"),
6267
}
@@ -239,7 +244,7 @@ fn get_map_name(map: *const libbpf_sys::bpf_map) -> Result<Option<String>> {
239244
let name = get_raw_map_name(map)?;
240245

241246
if unsafe { !libbpf_sys::bpf_map__is_internal(map) } {
242-
Ok(Some(name))
247+
Ok(Some(escape_raw_name(&name)))
243248
} else {
244249
Ok(canonicalize_internal_map_name(&name).map(|map| map.to_string()))
245250
}
@@ -366,7 +371,7 @@ fn gen_skel_map_defs(
366371

367372
for map in MapIter::new(object.as_mut_ptr()) {
368373
let map_name = match get_map_name(map)? {
369-
Some(n) => n.replace('.', "_"),
374+
Some(n) => n,
370375
None => continue,
371376
};
372377

libbpf-cargo/src/test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ fn test_skeleton_datasec() {
650650
void * const myconst = 0;
651651
int mycustomdata SEC(".data.custom");
652652
int mycustombss SEC(".bss.custom");
653-
const int mycustomrodata SEC(".rodata.custom") = 43;
653+
const int mycustomrodata SEC(".rodata.custom.1") = 43;
654654
655655
SEC("kprobe/foo")
656656
int this_is_my_prog(u64 *ctx)
@@ -720,7 +720,7 @@ fn test_skeleton_datasec() {
720720
721721
open_skel.data_custom_mut().mycustomdata = 1337;
722722
open_skel.bss_custom_mut().mycustombss = 12;
723-
assert_eq!(open_skel.rodata_custom().mycustomrodata, 43);
723+
assert_eq!(open_skel.rodata_custom_1().mycustomrodata, 43);
724724
725725
let mut skel = open_skel
726726
.load()
@@ -730,7 +730,7 @@ fn test_skeleton_datasec() {
730730
skel.bss_mut().myglobal = 24;
731731
skel.data_custom_mut().mycustomdata += 1;
732732
skel.bss_custom_mut().mycustombss += 1;
733-
assert_eq!(skel.rodata_custom().mycustomrodata, 43);
733+
assert_eq!(skel.rodata_custom_1().mycustomrodata, 43);
734734
735735
// Read only for rodata after load
736736
let _rodata: &prog_types::rodata = skel.rodata();
@@ -2057,7 +2057,7 @@ fn test_btf_dump_definition_datasec_custom() {
20572057
20582058
int bss_array[1] SEC(".bss.custom");
20592059
int data_array[1] SEC(".data.custom");
2060-
const int rodata_array[1] SEC(".rodata.custom");
2060+
const int rodata_array[1] SEC(".rodata.custom.1");
20612061
"#;
20622062

20632063
let bss_custom_output = r#"
@@ -2079,7 +2079,7 @@ pub struct data_custom {
20792079
let rodata_custom_output = r#"
20802080
#[derive(Debug, Copy, Clone)]
20812081
#[repr(C)]
2082-
pub struct rodata_custom {
2082+
pub struct rodata_custom_1 {
20832083
pub rodata_array: [i32; 1],
20842084
}
20852085
"#;
@@ -2088,7 +2088,7 @@ pub struct rodata_custom {
20882088
let btf = btf_from_mmap(&mmap);
20892089
let bss_custom = find_type_in_btf!(btf, types::DataSec<'_>, ".bss.custom", false);
20902090
let data_custom = find_type_in_btf!(btf, types::DataSec<'_>, ".data.custom", false);
2091-
let rodata_custom = find_type_in_btf!(btf, types::DataSec<'_>, ".rodata.custom", false);
2091+
let rodata_custom = find_type_in_btf!(btf, types::DataSec<'_>, ".rodata.custom.1", false);
20922092

20932093
assert_definition(&btf, &bss_custom, bss_custom_output);
20942094
assert_definition(&btf, &data_custom, data_custom_output);

0 commit comments

Comments
 (0)