Repository navigation
[BPF] Sort BTF struct members by offset - #205396
Conversation
0ed516d to
5a71df9
Compare
5a71df9 to
f451d46
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates the LLVM BPF backend to emit BTF struct members in nondecreasing offset order, ensuring BTF produced from source-ordered debug metadata (notably Rust) is accepted by the kernel. It also aligns CO-RE access strings and decl-tag component indexes with the reordered BTF member order while keeping relocation/offset patching based on the original debug indices.
Changes:
- Introduce a shared helper to obtain BTF record elements in BTF-required order (sorted for structs by offset).
- Update BTF struct emission and decl-tag component indexing to use the sorted element order.
- Update CO-RE access string construction to translate DI member indices into their reordered BTF indices; add regression tests for BTF output and CO-RE strings.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| llvm/test/CodeGen/BPF/CORE/struct-member-order.ll | New regression test ensuring CO-RE access strings use the member’s reordered BTF index. |
| llvm/test/CodeGen/BPF/BTF/struct-member-order.ll | New regression test validating BTF struct members are emitted in nondecreasing offset order and decl-tag component indexes follow that order. |
| llvm/lib/Target/BPF/BTFDebug.h | Extend BTFTypeStruct API to carry an explicit element list used for emission. |
| llvm/lib/Target/BPF/BTFDebug.cpp | Use BTF-ordered elements for struct type visitation/emission and decl-tag component indexing. |
| llvm/lib/Target/BPF/BPFCORE.h | Add getBTFRecordElements() helper that returns BTF-ordered record elements (sorted for structs). |
| llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp | Translate struct DI member indices to reordered BTF indices when forming CO-RE access strings. |
Comments suppressed due to low confidence (1)
llvm/lib/Target/BPF/BTFDebug.h:25
- This header declares/uses std::vector/std::string/std::tuple types but does not include the corresponding standard headers. Relying on transitive includes is brittle and can break with unrelated include-order changes; adding the new
std::vector<const DINode *> Elementsmember increases that risk further.
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/CodeGen/DebugHandlerBase.h"
#include "llvm/DebugInfo/BTF/BTF.h"
#include <cstdint>
#include <map>
#include <set>
4ba6170 to
75a35f4
Compare
75a35f4 to
db48f23
Compare
Is it possible for rust frontend to have attribute to enforce the order similar to source? |
I guess you mean |
Sort BTF structure elements by their debug offsets. Prevent source-ordered Rust debug metadata from producing descending member offsets after rustc chooses a different physical layout, because the kernel rejects such BTF. Use the sorted order for member emission, declaration-tag component indexes, and CO-RE access strings. Keep physical offset calculations on the original debug indexes so changing BTF order does not change generated code. Eliminate the need for the bpf-linker metadata rewrite by enforcing the ordering in the LLVM BPF backend: aya-rs/bpf-linker@1007ec7
db48f23 to
86e7e0d
Compare
Okay, I see. I tried a simple example with llvm22 rustc compiler and it does show random ordering in debuginfo: It does look like random ordering for struct members. |
yonghong-song
left a comment
There was a problem hiding this comment.
There is a failure in AARCH64 but it is not related to this patch. This pull request LGTM.
|
@yonghong-song thanks for the review! I gave up my commit bit so please merge this when you're ready. |
Sort BTF structure elements by their debug offsets. Prevent source-ordered Rust debug metadata from producing descending member offsets after rustc chooses a different physical layout, because the kernel rejects such BTF. Use the sorted order for member emission, declaration-tag component indexes, and CO-RE access strings. Keep physical offset calculations on the original debug indexes so changing BTF order does not change generated code. Eliminate the need for the bpf-linker metadata rewrite by enforcing the ordering in the LLVM BPF backend: aya-rs/bpf-linker@1007ec7
A C++ record lists base classes, static data members and methods among its elements next to its data members. Since llvm#205396, visitStructType() sorts all elements by offset and hits the llvm_unreachable() in getBTFRecordElementOffset() on the first element that is neither a member nor a variant part, so compiling C++ for BPF with -g crashes as soon as a record has a base class, a static data member or a method. The CO-RE pass computes the BTF member index the same way and crashes on the same records. Before that, every element was emitted as a BTF member. Methods and static data members became bogus members, e.g. a destructor turned into a member of type 0 at an offset past the end of the record. Base classes were emitted at the right offset, but also with type 0, and the kernel rejects a member of type void. BTF: emit a non-virtual base class as an anonymous member of the base type at its offset, which keeps the layout complete. Skip virtual bases: their offset is only known at run time, and the DI offset of a virtual base is the position of its offset in the vtable. Skip static data members, methods and other elements. The declaration tag component index follows the emitted members. CO-RE: fixing BTF alone would not be safe. Clang numbers the fields of a record for a CO-RE access, not its elements, so for a C++ record with a base class, a static data member or a vtable pointer before a field, the index has always pointed at the wrong element: for "struct S : Base { int x, y; }", &s->y was computed as the offset of x, with an access string naming x, and for a class with virtual functions the vtable pointer was taken for the first field. That went unnoticed while the BTF of such records could not be loaded; with loadable BTF it becomes a silent miscompile. Map the access index to the field it counts, skipping base classes, static data members, methods and artificial members, and compute the access string index among the emitted BTF members. A record that the debug info only declares, as happens for classes with a vtable, has no fields to map to; report that instead of crashing, and point at -fstandalone-debug. An access to an inherited field is a CO-RE access within the base: the offset of a non-virtual base is a constant added before it, and the offset of a virtual base is loaded from the vtable. So only the position of the field within its base is relocated, not the position of the base. C records are unaffected: the BPF selftests of the kernel compile to byte-identical code, .BTF and .BTF.ext. Rust was not tested; the only Rust-specific effect would be that an artificial member is no longer counted when mapping a CO-RE access index. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its elements next to its data members. Since llvm#205396, visitStructType() sorts all elements by offset and hits the llvm_unreachable() in getBTFRecordElementOffset() on the first element that is neither a member nor a variant part, so compiling C++ for BPF with -g crashes as soon as a record has a base class, a static data member or a method. The CO-RE pass computes the BTF member index the same way and crashes on the same records, and visitMapDefType() casts every element of a map definition to a member. Before that, every element was emitted as a BTF member. Methods and static data members became bogus members of type 0, and base classes were emitted at the right offset but also with type 0, which the kernel rejects. BTF: emit a non-virtual base class as an anonymous member of the base type at its offset, which keeps the layout complete. Skip virtual bases, whose offset is only known at run time (the DI offset of a virtual base is the position of its offset in the vtable), static data members, methods and other elements, also in map definitions. CO-RE: fixing BTF alone would turn the crash into a silent miscompile. Clang numbers the fields of a record for a CO-RE access, while the backend indexed its elements, which for a C++ record start with the base classes and include static data members and the vtable pointer: for "struct S : Base { int x, y; }", &s->y was computed as the offset of x. Map the access index to the field it counts, skipping base classes, static data members, methods and clang's vtable pointer, and take the access string index from the same ordered list of BTF members that BTF is emitted from. A record that the debug info only declares, as happens for classes with a vtable, has no fields to map to; report it at the access, pointing at -fstandalone-debug. An access to an inherited field is a CO-RE access within the base: only the position of the field within its base is relocated, not the position of the base. Records whose elements are all fields, as in C, are unaffected: the BPF selftests of the kernel compile to byte-identical code, .BTF and .BTF.ext. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its debug info elements. Since llvm#205396, BTF emission sorts all elements by offset and crashes on these; the CO-RE pass and visitMapDefType() crash on them too. BTF: emit a non-virtual base as an anonymous member of the base type at its offset and skip the other elements. Virtual bases are skipped, as their offset is only known at run time. CO-RE: fixing BTF alone would turn the crash into a silent miscompile, since clang numbers the fields of a record while the backend indexed its elements: for "struct S : Base { int x, y; }", &s->y got the offset of x. Map the access index to the field, take the access string index from the same member list BTF is built from, and report a record the debug info only declares at the access, pointing at -fstandalone-debug. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its debug info elements. Since llvm#205396, BTF emission sorts all elements by offset and crashes on these; the CO-RE pass and visitMapDefType() crash on them too. BTF: emit a non-virtual base as an anonymous member of the base type at its offset and skip the other elements. Virtual bases are skipped, as their offset is only known at run time. CO-RE: fixing BTF alone would turn the crash into a silent miscompile, since clang numbers the fields of a record while the backend indexed its elements: for "struct S : Base { int x, y; }", &s->y got the offset of x. Map the access index to the field and take the access string index from the same member list BTF is built from. For an inherited field only its position within the base is relocated; the offset of the base stays a constant, or is loaded from the vtable for a virtual base. Report an access into a record the debug info only declares as an error at the access, and drop a field info call left behind by it. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its debug info elements. Since llvm#205396, BTF emission sorts all elements by offset and crashes on these; the CO-RE pass and visitMapDefType() crash on them too. BTF: emit a non-virtual base as an anonymous member of the base type at its offset and skip the other elements. Virtual bases are skipped, as their offset is only known at run time. CO-RE: fixing BTF alone would turn the crash into a silent miscompile, since clang numbers the fields of a record while the backend indexed its elements: for "struct S : Base { int x, y; }", &s->y got the offset of x. Map the access index to the field and take the access string index from the same member list BTF is built from. For an inherited field only its position within the base is relocated; the offset of the base stays a constant, or is loaded from the vtable for a virtual base. Report an access into a record the debug info only declares as an error at the access, and drop a field info call left behind by it. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its debug info elements. Since llvm#205396, BTF emission sorts all elements by offset and crashes on these; the CO-RE pass and visitMapDefType() crash on them too. BTF: emit a non-virtual base as an anonymous member of the base type at its offset and skip the other elements. Virtual bases are skipped, as their offset is only known at run time. CO-RE: fixing BTF alone would turn the crash into a silent miscompile, since clang numbers the fields of a record while the backend indexed its elements: for "struct S : Base { int x, y; }", &s->y got the offset of x. Map the access index to the field and take the access string index from the same member list BTF is built from. For an inherited field only its position within the base is relocated; the offset of the base stays a constant, or is loaded from the vtable for a virtual base. Report an access into a record the debug info only declares as an error at the access, and drop a field info call left behind by it. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its debug info elements. Since llvm#155783, BTF emission hits llvm_unreachable() on any record element other than a member or a variant part, and llvm#205396 moved the crash into the offset sort; the CO-RE pass and visitMapDefType() crash on them too. BTF: emit a non-virtual base as an anonymous member of the base type at its offset, as pahole does, and skip the other elements. Virtual bases are skipped, as their offset is only known at run time. CO-RE: fixing BTF alone would turn the crash into a silent miscompile, since clang numbers the fields of a record while the backend indexed its elements: for "struct S : Base { int x, y; }", &s->y got the offset of x. Map the access index to the field and take the access string index from the same member list BTF is built from. For an inherited field only its position within the base is relocated; the offset of the base stays a constant, or is loaded from the vtable for a virtual base. Report an access into a record the debug info only declares as an error at the access, and replace a field info call on it with 0. Assisted-by: Claude Code (Claude Opus 5.5)
A C++ record lists base classes, static data members and methods among its debug info elements. Since llvm#155783, BTF emission hits llvm_unreachable() on any record element other than a member or a variant part, and llvm#205396 moved the crash into the offset sort; the CO-RE pass and visitMapDefType() crash on them too. BTF: emit a non-virtual base as an anonymous member of the base type at its offset, as pahole does, and skip the other elements. Virtual bases are skipped, as their offset is only known at run time. CO-RE: fixing BTF alone would turn the crash into a silent miscompile, since clang numbers the fields of a record while the backend indexed its elements: for "struct S : Base { int x, y; }", &s->y got the offset of x. Map the access index to the field and take the access string index from the same member list BTF is built from. For an inherited field only its position within the base is relocated; the offset of the base stays a constant, or is loaded from the vtable for a virtual base. Report an access into a record the debug info only declares as an error at the access, and replace a field info call on it with 0. Assisted-by: Claude Code (Claude Opus 5.5)
Sort BTF structure elements by their debug offsets. Prevent
source-ordered Rust debug metadata from producing descending member
offsets after rustc chooses a different physical layout, because the
kernel rejects such BTF.
Use the sorted order for member emission, declaration-tag component
indexes, and CO-RE access strings. Keep physical offset calculations on
the original debug indexes so changing BTF order does not change
generated code.
Eliminate the need for the bpf-linker metadata rewrite by enforcing the
ordering in the LLVM BPF backend:
aya-rs/bpf-linker@1007ec7