Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulate NMR API failures: Fix memory leaks and segmentation fault #2253

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion libs/api/ebpf_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,7 @@ _initialize_ebpf_object_from_native_file(
program->handle = ebpf_handle_invalid;
program->program_type = info->program_type;
program->attach_type = info->expected_attach_type;
program->fd = ebpf_fd_invalid;

program->section_name = ebpf_duplicate_string(info->section_name);
if (program->section_name == nullptr) {
Expand All @@ -1753,11 +1754,17 @@ _initialize_ebpf_object_from_native_file(
}

Exit:
ebpf_free(program);
if (result != EBPF_SUCCESS) {
if (program) {
// Deallocate program, if it was allocated but not added to
// the object programs vector.
clean_up_ebpf_program(program);
}

clean_up_ebpf_programs(object.programs);
clean_up_ebpf_maps(object.maps);
}

ebpf_free_sections(infos);
EBPF_RETURN_RESULT(result);
}
Expand Down
17 changes: 17 additions & 0 deletions libs/api_common/store_helper_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,15 @@ ebpf_store_load_program_information(
Exit:
if (result != EBPF_SUCCESS) {
ebpf_free(*program_info);

// Deallocate the dynamic memory in the program_info_array vector.
if (program_info_array.size() > 0) {
for (auto program_data : program_info_array) {
ebpf_program_info_free(program_data);
}
}
}

if (program_data_key) {
close_registry_key(program_data_key);
}
Expand Down Expand Up @@ -569,6 +577,15 @@ ebpf_store_load_section_information(
Exit:
if (result != EBPF_SUCCESS) {
ebpf_free(*section_info);
// Deallocate the dynamic memory in the section_info_array vector.
if (section_info_array.size() > 0) {
for (auto section_data : section_info_array) {
ebpf_free(section_data->program_type);
shpalani marked this conversation as resolved.
Show resolved Hide resolved
ebpf_free(section_data->attach_type);
ebpf_free(const_cast<char*>(section_data->section_prefix));
ebpf_free(section_data);
}
}
}
if (section_data_key) {
close_registry_key(section_data_key);
Expand Down
1 change: 1 addition & 0 deletions libs/execution_context/ebpf_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ ebpf_native_terminate()
// ebpf_provider_unload is blocking call until all the
// native modules have been detached.
ebpf_provider_unload(_ebpf_native_provider);
_ebpf_native_provider = NULL;

// All native modules should be cleaned up by now.
ebpf_assert(!_ebpf_native_client_table || ebpf_hash_table_key_count(_ebpf_native_client_table) == 0);
Expand Down