Skip to content

[BPF] Sort BTF struct members by offset - #205396

Merged
yonghong-song merged 1 commit into
llvm:mainfrom
tamird:bpf/sort-btf-struct-members
Jun 25, 2026
Merged

yonghong-song merged 1 commit into
llvm:mainfrom
tamird:bpf/sort-btf-struct-members

Conversation

@tamird

@tamird tamird commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

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

@tamird
tamird force-pushed the bpf/sort-btf-struct-members branch from 0ed516d to 5a71df9 Compare June 23, 2026 18:31
@tamird tamird changed the title [BPF] sort BTF struct members by offset [BPF] Sort BTF struct members by offset Jun 23, 2026
@tamird
tamird force-pushed the bpf/sort-btf-struct-members branch from 5a71df9 to f451d46 Compare June 23, 2026 18:40
@tamird
tamird marked this pull request as ready for review June 23, 2026 18:47
@tamird

tamird commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

cc @vadorovsky @alessandrod

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 *> Elements member 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>

Comment thread llvm/lib/Target/BPF/BPFCORE.h Resolved
@tamird
tamird force-pushed the bpf/sort-btf-struct-members branch 2 times, most recently from 4ba6170 to 75a35f4 Compare June 23, 2026 19:11
@tamird
tamird requested a review from Copilot June 23, 2026 19:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread llvm/lib/Target/BPF/BPFCORE.h Resolved
@tamird
tamird force-pushed the bpf/sort-btf-struct-members branch from 75a35f4 to db48f23 Compare June 23, 2026 20:02
@yonghong-song

Copy link
Copy Markdown
Contributor

From https://github.com/rust-lang/reference/blob/master/src/types/struct.md, I see:

r[type.struct.layout] The memory layout of a struct is undefined by default to allow for compiler optimizations like field reordering, but it can be fixed with the [repr attribute](https://github.com/rust-lang/reference/blob/master/src/type-layout.md#representations). In either case, fields may be given in any order in a corresponding struct expression; the resulting struct value will always have the same memory layout.

Is it possible for rust frontend to have attribute to enforce the order similar to source?

@tamird

tamird commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

From https://github.com/rust-lang/reference/blob/master/src/types/struct.md, I see:

r[type.struct.layout] The memory layout of a struct is undefined by default to allow for compiler optimizations like field reordering, but it can be fixed with the [repr attribute](https://github.com/rust-lang/reference/blob/master/src/type-layout.md#representations). In either case, fields may be given in any order in a corresponding struct expression; the resulting struct value will always have the same memory layout.

Is it possible for rust frontend to have attribute to enforce the order similar to source?

I guess you mean [repr(C)]. But you can't control that for foreign types.

Comment thread llvm/lib/Target/BPF/BPFCORE.h Resolved
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
@tamird
tamird force-pushed the bpf/sort-btf-struct-members branch from db48f23 to 86e7e0d Compare June 24, 2026 18:15
@yonghong-song

Copy link
Copy Markdown
Contributor

From https://github.com/rust-lang/reference/blob/master/src/types/struct.md, I see:

r[type.struct.layout] The memory layout of a struct is undefined by default to allow for compiler optimizations like field reordering, but it can be fixed with the [repr attribute](https://github.com/rust-lang/reference/blob/master/src/type-layout.md#representations). In either case, fields may be given in any order in a corresponding struct expression; the resulting struct value will always have the same memory layout.

Is it possible for rust frontend to have attribute to enforce the order similar to source?

I guess you mean [repr(C)]. But you can't control that for foreign types.

Okay, I see. I tried a simple example with llvm22 rustc compiler and it does show random ordering in debuginfo:

!14 = !DICompositeType(tag: DW_TAG_structure_type, name: "S", scope: !9, file: !15, size: 64, align: 32, flags: DIFlagPublic, elements: !16, templateParams: !22, identifier: "868b2f6fe4ee6a5f8eb0e2930411721")
!16 = !{!17, !18, !20}
!17 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !14, file: !15, baseType: !12, size: 8, align: 8, offset: 48, flags: DIFlagPublic)
!18 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !14, file: !15, baseType: !19, size: 32, align: 32, flags: DIFlagPublic)
!19 = !DIBasicType(name: "u32", size: 32, encoding: DW_ATE_unsigned)
!20 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !14, file: !15, baseType: !21, size: 16, align: 16, offset: 32, flags: DIFlagPublic)

It does look like random ordering for struct members.

@yonghong-song yonghong-song left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a failure in AARCH64 but it is not related to this patch. This pull request LGTM.

@tamird

tamird commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

@yonghong-song thanks for the review! I gave up my commit bit so please merge this when you're ready.

@yonghong-song
yonghong-song merged commit 8987645 into llvm:main Jun 25, 2026
8 of 9 checks passed
@tamird
tamird deleted the bpf/sort-btf-struct-members branch June 25, 2026 17:51
maarcosrmz pushed a commit to maarcosrmz/llvm-project that referenced this pull request Jul 1, 2026
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
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
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)
yonghong-song pushed a commit to maxskokov/llvm-project that referenced this pull request Oct 20, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants