Skip to content

Commit

Permalink
Use full key width when computing array index (#2780)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
Co-authored-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett and Alan Jowett authored Aug 22, 2023
1 parent b33adb3 commit 9fd8504
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion libs/execution_context/ebpf_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ _update_array_map_entry(
return EBPF_INVALID_ARGUMENT;
}

uint8_t* entry = &map->data[*key * map->ebpf_map_definition.value_size];
uint8_t* entry = &map->data[key_value * map->ebpf_map_definition.value_size];
if (data) {
memcpy(entry, data, map->ebpf_map_definition.value_size);
} else {
Expand Down
16 changes: 9 additions & 7 deletions libs/execution_context/unit/execution_context_unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ typedef std::unique_ptr<ebpf_map_t, ebpf_object_deleter<ebpf_map_t>> map_ptr;
typedef std::unique_ptr<ebpf_program_t, ebpf_object_deleter<ebpf_program_t>> program_ptr;
typedef std::unique_ptr<ebpf_link_t, ebpf_object_deleter<ebpf_link_t>> link_ptr;

static const uint32_t _test_map_size = 512;

static void
_test_crud_operations(ebpf_map_type_t map_type)
{
Expand Down Expand Up @@ -176,7 +178,7 @@ _test_crud_operations(ebpf_map_type_t map_type)
dpc = {emulate_dpc_t(1)};
}

ebpf_map_definition_in_memory_t map_definition{map_type, sizeof(uint32_t), sizeof(uint64_t), 10};
ebpf_map_definition_in_memory_t map_definition{map_type, sizeof(uint32_t), sizeof(uint64_t), _test_map_size};
map_ptr map;
{
ebpf_map_t* local_map;
Expand All @@ -186,7 +188,7 @@ _test_crud_operations(ebpf_map_type_t map_type)
map.reset(local_map);
}
std::vector<uint8_t> value(ebpf_map_get_definition(map.get())->value_size);
for (uint32_t key = 0; key < 10; key++) {
for (uint32_t key = 0; key < _test_map_size; key++) {
*reinterpret_cast<uint64_t*>(value.data()) = static_cast<uint64_t>(key) * static_cast<uint64_t>(key);
REQUIRE(
ebpf_map_update_entry(
Expand All @@ -200,7 +202,7 @@ _test_crud_operations(ebpf_map_type_t map_type)
}

// Test for inserting max_entries + 1.
uint32_t bad_key = 10;
uint32_t bad_key = _test_map_size;
*reinterpret_cast<uint64_t*>(value.data()) = static_cast<uint64_t>(bad_key) * static_cast<uint64_t>(bad_key);
REQUIRE(
ebpf_map_update_entry(
Expand All @@ -219,12 +221,12 @@ _test_crud_operations(ebpf_map_type_t map_type)
expected_result);
}

for (uint32_t key = 0; key < 10; key++) {
for (uint32_t key = 0; key < _test_map_size; key++) {
ebpf_result_t expected_result;
if (replace_on_full) {
expected_result = key == 0 ? EBPF_OBJECT_NOT_FOUND : EBPF_SUCCESS;
} else {
expected_result = key == 10 ? EBPF_OBJECT_NOT_FOUND : EBPF_SUCCESS;
expected_result = key == _test_map_size ? EBPF_OBJECT_NOT_FOUND : EBPF_SUCCESS;
}
REQUIRE(
ebpf_map_find_entry(
Expand All @@ -238,7 +240,7 @@ _test_crud_operations(ebpf_map_type_t map_type)
uint32_t previous_key;
uint32_t next_key;
std::set<uint32_t> keys;
for (uint32_t key = 0; key < 10; key++) {
for (uint32_t key = 0; key < _test_map_size; key++) {
REQUIRE(
ebpf_map_next_key(
map.get(),
Expand All @@ -249,7 +251,7 @@ _test_crud_operations(ebpf_map_type_t map_type)
previous_key = next_key;
keys.insert(previous_key);
}
REQUIRE(keys.size() == 10);
REQUIRE(keys.size() == _test_map_size);
REQUIRE(
ebpf_map_next_key(
map.get(),
Expand Down

0 comments on commit 9fd8504

Please sign in to comment.