Skip to content

Commit

Permalink
libbpf-tools: Fix misaligned pointer accesses in drsnoop
Browse files Browse the repository at this point in the history
The perf buffer in drsnoop doesn't maintain 8 byte alignment
for delta_ns, nr_reclaimed and nr_free_pages in struct event.
When building with "-fsanitize=alignment -fsanitize-trap=undefined"
failures happen in handle_event. Fix these by copying the event
from the perf buffer before accessing.

This is similar to a fix in exitsnoop where different ways to handle
misaligned pointers were discussed:
iovisor#4760

Signed-off-by: Ian Rogers <[email protected]>
  • Loading branch information
captain5050 authored and yonghong-song committed Dec 7, 2023
1 parent 5a547e7 commit 61230b2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions libbpf-tools/drsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,26 @@ static void sig_int(int signo)

void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
{
const struct event *e = data;
struct event e;
struct tm *tm;
char ts[32];
time_t t;

if (data_sz < sizeof(e)) {
printf("Error: packet too small\n");
return;
}
/* Copy data as alignment in the perf buffer isn't guaranteed. */
memcpy(&e, data, sizeof(e));

time(&t);
tm = localtime(&t);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
printf("%-8s %-16s %-6d %8.3f %5lld",
ts, e->task, e->pid, e->delta_ns / 1000000.0,
e->nr_reclaimed);
ts, e.task, e.pid, e.delta_ns / 1000000.0,
e.nr_reclaimed);
if (env.extended)
printf(" %8llu", e->nr_free_pages * page_size / 1024);
printf(" %8llu", e.nr_free_pages * page_size / 1024);
printf("\n");
}

Expand Down

0 comments on commit 61230b2

Please sign in to comment.