This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Warn about direct access to CO-RE bitfields
DraftPublic

Authored by eddyz87 on Sep 20 2022, 10:56 AM.
This is a draft revision that has not yet been submitted for review.

Details

Reviewers
None
Summary

Print a diagnostic warning when bitfields are accessed directly in
structures that are marked with preserve_access_index attribute. Such
bitfields should be accessed using BPF_CORE_READ_BITFIELD and
BPF_CORE_READ_BITFIELD_PROBED macro.

These macro generate a specific pattern:

struct foo {
  int field:2;
} *ctx;

BPF_CORE_READ_BITFIELD(ctx, field)

is expanded as:

({ const void *p = (void*) ctx + __builtin_preserve_field_info(...);
   unsigned long long val;
   ... val = *(<cast>)p; ...
   val;
})

Note that ptr->field is not referenced, instead an address is
computed starting from ctx. This commit exploits this fact and simply
warns about usage of ptr->field in load instructions.

Diff Detail

Event Timeline

eddyz87 created this revision.Sep 20 2022, 10:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 20 2022, 10:56 AM
eddyz87 updated this revision to Diff 463034.Sep 26 2022, 2:59 PM

Updated testcase to avoid duplication with Kernel source code