Skip to content

Commit

Permalink
libbpf-tools: Fix misaligned pointer accesses in statsnoop
Browse files Browse the repository at this point in the history
The perf buffer in statsnoop doesn't maintain 8 byte alignment
for ts_ns 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 dkruces committed Nov 28, 2024
1 parent 8cccfe3 commit 293e2ad
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions libbpf-tools/statsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,31 @@ static void sig_int(int signo)
static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
{
static __u64 start_timestamp = 0;
const struct event *e = data;
struct event e;
int fd, err;
double ts = 0.0;

if (e->ret >= 0) {
fd = e->ret;
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));

if (e.ret >= 0) {
fd = e.ret;
err = 0;
} else {
fd = -1;
err = -e->ret;
err = -e.ret;
}
if (!start_timestamp)
start_timestamp = e->ts_ns;
start_timestamp = e.ts_ns;
if (emit_timestamp) {
ts = (double)(e->ts_ns - start_timestamp) / 1000000000;
ts = (double)(e.ts_ns - start_timestamp) / 1000000000;
printf("%-14.9f ", ts);
}
printf("%-7d %-20s %-4d %-4d %-s\n", e->pid, e->comm, fd, err, e->pathname);
printf("%-7d %-20s %-4d %-4d %-s\n", e.pid, e.comm, fd, err, e.pathname);
}

static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
Expand Down

0 comments on commit 293e2ad

Please sign in to comment.