This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Fix CO-RE bugs with bitfields
ClosedPublic

Authored by yonghong-song on Nov 4 2019, 7:35 PM.

Details

Summary

bitfield handling is not robust with current implementation.
I have seen two issues as described below.

Issue 1:

struct s {
  long long f1;
  char f2; 
  char b1:1;
} *p; 
The current approach will generate an access bit size
56 (from b1 to the end of structure) which will be
rejected as it is not power of 2.

Issue 2:

struct s {
  char f1;
  char b1:3;
  char b2:5; 
  char b3:6:
  char b4:2; 
  char f2;
};
The LLVM will group 4 bitfields together with 2 bytes. But
loading 2 bytes is not correct as it violates alignment
requirement. Note that sometimes, LLVM breaks a large
bitfield groups into multiple groups, but not in this case.

To resolve the above two issues, this patch takes a
different approach. The alignment for the structure is used
to construct the offset of the bitfield access. The bitfield
incurred memory access is an aligned memory access with alignment/size
equal to the alignment of the structure.
This also simplified the code.

This may not be the optimal memory access in terms of memory access
width. But this should be okay since extracting the bitfield value
will have the same amount of work regardless of what kind of
memory access width.

Diff Detail

Event Timeline

yonghong-song created this revision.Nov 4 2019, 7:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2019, 7:35 PM
ast accepted this revision.Nov 4 2019, 7:57 PM
This revision is now accepted and ready to land.Nov 4 2019, 7:57 PM
This revision was automatically updated to reflect the committed changes.