Skip to content

Commit

Permalink
libbpf-rs: handle maps that were autocreate(false)
Browse files Browse the repository at this point in the history
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 <eyakubovich@gmail.com>
  • Loading branch information
eyakubovich committed Jan 2, 2024
1 parent 99257aa commit d25c42e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions libbpf-rs/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,11 @@ impl Object {
}
};

let map_obj = unsafe { Map::new(map_ptr) }?;
if unsafe { libbpf_sys::bpf_map__autocreate(map_ptr.as_ptr()) } {
let map_obj = unsafe { Map::new(map_ptr) }?;
obj.maps.insert(map_obj.name().into(), map_obj);
}

// Add the map to the hashmap
obj.maps.insert(map_obj.name().into(), map_obj);
map = map_ptr.as_ptr();
}

Expand Down
16 changes: 16 additions & 0 deletions libbpf-rs/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,3 +1636,19 @@ fn test_sudo_program_get_fd_and_id() {
let owned_prog_fd = Program::get_fd_by_id(prog_id).expect("failed to get program fd by id");
close(owned_prog_fd.as_raw_fd()).expect("failed to close owned program fd");
}

/// Check that autocreate disabled maps don't prevent object loading
#[test]
fn test_sudo_map_autocreate_disable() {
bump_rlimit_mlock();

let mut open_obj = open_test_object("map_auto_pin.bpf.o");

open_obj
.map_mut("auto_pin_map")
.expect("map wasn't found")
.set_autocreate(false)
.expect("set_autocreate() failed");

open_obj.load().expect("failed to load object");
}

0 comments on commit d25c42e

Please sign in to comment.