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

Fix crashes caused by improper error handling on bpf_look/update/delete_elem #2623

Merged
merged 2 commits into from
Jun 3, 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
11 changes: 6 additions & 5 deletions src/bpftrace.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <arpa/inet.h>
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <ctime>
Expand Down Expand Up @@ -1531,7 +1532,7 @@ int BPFtrace::clear_map(IMap &map)
for (auto &key : keys)
{
int err = bpf_delete_elem(map.mapfd_, key.data());
if (err)
if (err && err != -ENOENT)
{
LOG(ERROR) << "failed to look up elem: " << err;
return -1;
Expand Down Expand Up @@ -1577,7 +1578,7 @@ int BPFtrace::zero_map(IMap &map)
{
int err = bpf_update_elem(map.mapfd_, key.data(), zero.data(), BPF_EXIST);

if (err)
if (err && err != -ENOENT)
{
LOG(ERROR) << "failed to look up elem: " << err;
return -1;
Expand Down Expand Up @@ -1616,7 +1617,7 @@ int BPFtrace::print_map(IMap &map, uint32_t top, uint32_t div)
value_size *= nvalues;
auto value = std::vector<uint8_t>(value_size);
int err = bpf_lookup_elem(map.mapfd_, key.data(), value.data());
if (err == -1)
if (err == -ENOENT)
{
// key was removed by the eBPF program during bpf_get_next_key() and bpf_lookup_elem(),
// let's skip this key
Expand Down Expand Up @@ -1702,7 +1703,7 @@ int BPFtrace::print_map_hist(IMap &map, uint32_t top, uint32_t div)
int value_size = map.type_.GetSize() * nvalues;
auto value = std::vector<uint8_t>(value_size);
int err = bpf_lookup_elem(map.mapfd_, key.data(), value.data());
if (err == -1)
if (err == -ENOENT)
{
// key was removed by the eBPF program during bpf_get_next_key() and bpf_lookup_elem(),
// let's skip this key
Expand Down Expand Up @@ -1781,7 +1782,7 @@ int BPFtrace::print_map_stats(IMap &map, uint32_t top, uint32_t div)
int value_size = map.type_.GetSize() * nvalues;
auto value = std::vector<uint8_t>(value_size);
int err = bpf_lookup_elem(map.mapfd_, key.data(), value.data());
if (err == -1)
if (err == -ENOENT)
{
// key was removed by the eBPF program during bpf_get_next_key() and bpf_lookup_elem(),
// let's skip this key
Expand Down
5 changes: 5 additions & 0 deletions tests/runtime/basic
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ PROG BEGIN { @x = 10; printf("%d", @x++); printf(" %d", ++@x); printf(" %d", @x-
EXPECT 10 12 12 10
TIMEOUT 1

NAME parallel map access
RUN {{BPFTRACE}} runtime/scripts/parallel_map_access.bt
EXPECT SUCCESS
TIMEOUT 2

NAME increment/decrement variable
PROG BEGIN { $x = 10; printf("%d", $x++); printf(" %d", ++$x); printf(" %d", $x--); printf(" %d\n", --$x); exit(); }
EXPECT 10 12 12 10
Expand Down
24 changes: 24 additions & 0 deletions tests/runtime/scripts/parallel_map_access.bt
maokelong marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Regression testing the map's thread safety.
// Bpftrace shouldn't crash during map's print/clear/zero operations
// while delete is called at the same time.
//
// For more information, please refer to:
// https://github.com/iovisor/bpftrace/pull/2623

i:us:1 {
@data[rand % 100] = count();
if (rand % 5 == 0) {
delete(@data[rand % 10]);
}
}

i:ms:1 {
print(@data);
clear(@data);
zero(@data);
}

i:s:1 {
print("SUCCESS");
exit();
}