Skip to content

Commit

Permalink
Sort programs to match bpf2c (#2784)
Browse files Browse the repository at this point in the history
* Sort programs to match bpf2c

Signed-off-by: Alan Jowett <[email protected]>

* Fix sort order

Signed-off-by: Alan Jowett <[email protected]>

* Sort by section, then program name

Signed-off-by: Alan Jowett <[email protected]>

* Fix code analysis failure

Signed-off-by: Alan Jowett <[email protected]>

---------

Signed-off-by: Alan Jowett <[email protected]>
Co-authored-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett and Alan Jowett authored Aug 24, 2023
1 parent 7cc11a4 commit 5c9ca4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 13 additions & 0 deletions libs/api/ebpf_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef unsigned char boolean;
#include "utilities.hpp"
#include "windows_platform_common.hpp"

#include <algorithm>
#include <codecvt>
#include <fcntl.h>
#include <io.h>
Expand Down Expand Up @@ -1749,6 +1750,18 @@ _initialize_ebpf_object_from_native_file(
program = nullptr;
}

// The bpf2c tool orders programs by section name, so we need to sort the programs
// vector to match. If there are multiple programs per section, sort by program name.
std::sort(object.programs.begin(), object.programs.end(), [](const ebpf_program_t* a, const ebpf_program_t* b) {
int section_name_comparison = strcmp(a->section_name, b->section_name);
int program_name_comparison = strcmp(a->program_name, b->program_name);
if (section_name_comparison != 0) {
return section_name_comparison < 0;
} else {
return program_name_comparison < 0;
}
});

Exit:
if (result != EBPF_SUCCESS) {
if (program) {
Expand Down
12 changes: 6 additions & 6 deletions tests/end_to_end/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ typedef class _single_instance_hook : public _hook_helper
};
HANDLE nmr_provider_handle;

PNPI_REGISTRATION_INSTANCE client_registration_instance;
const void* client_binding_context;
const ebpf_extension_data_t* client_data;
const ebpf_extension_dispatch_table_t* client_dispatch_table;
HANDLE nmr_binding_handle;
bpf_link* link_object;
PNPI_REGISTRATION_INSTANCE client_registration_instance = nullptr;
const void* client_binding_context = nullptr;
const ebpf_extension_data_t* client_data = nullptr;
const ebpf_extension_dispatch_table_t* client_dispatch_table = nullptr;
HANDLE nmr_binding_handle = nullptr;
bpf_link* link_object = nullptr;
} single_instance_hook_t;

typedef class xdp_md_helper : public xdp_md_t
Expand Down

0 comments on commit 5c9ca4b

Please sign in to comment.