Skip to content

Commit

Permalink
libbpf-tools: Fix misaligned pointer accesses in fsslower
Browse files Browse the repository at this point in the history
The perf buffer in fsslower doesn't maintain 8 byte alignment for
delta_us, end_ns, offset and size 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 1820850 commit 6ab9797
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions libbpf-tools/fsslower.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,31 +328,38 @@ static void print_headers()

static 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));

if (csv) {
printf("%lld,%s,%d,%c,", e->end_ns, e->task, e->pid, file_op[e->op]);
if (e->size == LLONG_MAX)
printf("%lld,%s,%d,%c,", e.end_ns, e.task, e.pid, file_op[e.op]);
if (e.size == LLONG_MAX)
printf("LL_MAX,");
else
printf("%zd,", e->size);
printf("%lld,%lld,%s\n", e->offset, e->delta_us, e->file);
printf("%zd,", e.size);
printf("%lld,%lld,%s\n", e.offset, e.delta_us, e.file);
return;
}

time(&t);
tm = localtime(&t);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);

printf("%-8s %-16s %-7d %c ", ts, e->task, e->pid, file_op[e->op]);
if (e->size == LLONG_MAX)
printf("%-8s %-16s %-7d %c ", ts, e.task, e.pid, file_op[e.op]);
if (e.size == LLONG_MAX)
printf("%-7s ", "LL_MAX");
else
printf("%-7zd ", e->size);
printf("%-8lld %7.2f %s\n", e->offset / 1024, (double)e->delta_us / 1000, e->file);
printf("%-7zd ", e.size);
printf("%-8lld %7.2f %s\n", e.offset / 1024, (double)e.delta_us / 1000, e.file);
}

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

0 comments on commit 6ab9797

Please sign in to comment.