Skip to content

Commit a28a805

Browse files
eyakubovichdanielocfb
authored andcommitted
libbpf-rs: handle maps that were autocreate(false)
If map auto creation was disabled, the Object would fail to load since the map's underlying fd will be -1. This checks the if the map autocreate flag was disabled and skips creating a Map for it. Signed-off-by: Eugene Yakubovich <[email protected]>
1 parent 11c1d0f commit a28a805

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

libbpf-rs/src/object.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,11 @@ impl Object {
365365
}
366366
};
367367

368-
let map_obj = unsafe { Map::new(map_ptr) }?;
368+
if unsafe { libbpf_sys::bpf_map__autocreate(map_ptr.as_ptr()) } {
369+
let map_obj = unsafe { Map::new(map_ptr) }?;
370+
obj.maps.insert(map_obj.name().into(), map_obj);
371+
}
369372

370-
// Add the map to the hashmap
371-
obj.maps.insert(map_obj.name().into(), map_obj);
372373
map = map_ptr.as_ptr();
373374
}
374375

libbpf-rs/tests/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,3 +1636,19 @@ fn test_sudo_program_get_fd_and_id() {
16361636
let owned_prog_fd = Program::get_fd_by_id(prog_id).expect("failed to get program fd by id");
16371637
close(owned_prog_fd.as_raw_fd()).expect("failed to close owned program fd");
16381638
}
1639+
1640+
/// Check that autocreate disabled maps don't prevent object loading
1641+
#[test]
1642+
fn test_sudo_map_autocreate_disable() {
1643+
bump_rlimit_mlock();
1644+
1645+
let mut open_obj = open_test_object("map_auto_pin.bpf.o");
1646+
1647+
open_obj
1648+
.map_mut("auto_pin_map")
1649+
.expect("map wasn't found")
1650+
.set_autocreate(false)
1651+
.expect("set_autocreate() failed");
1652+
1653+
open_obj.load().expect("failed to load object");
1654+
}

0 commit comments

Comments
 (0)