Skip to content

[BPF] Handle C++ record elements in BTF and CO-RE - #226717

Closed
maxskokov wants to merge 2 commits into
llvm:mainfrom
maxskokov:bpf-btf-skip-methods
Closed

maxskokov wants to merge 2 commits into
llvm:mainfrom
maxskokov:bpf-btf-skip-methods

Conversation

@maxskokov

@maxskokov maxskokov commented Sep 26, 2026 •

Copy link
Copy Markdown

Depends on #226790; please land that first (see CO-RE below).

Since #155783 (LLVM 22.1), compiling C++ for BPF with -g hits llvm_unreachable() in BTF emission as soon as a record has a base class, a static data member (DWARF 5) or a method; #205396 moved the crash into the offset sort:

struct S { void *p; ~S(); };
long f(S *s) { return (long)s->p; }

A C++ record lists these among its debug info elements next to its data members. The CO-RE pass and visitMapDefType() walk the same elements and crash too.

BTF. A non-virtual base becomes an anonymous member of the base type at its offset, as pahole encodes inheritance; virtual bases, static data members and methods are skipped.

CO-RE. The DI index of a CO-RE access is an index into the record's debug info elements. The backend takes that element and maps it to its position among the BTF members, built from the same ordered list as BTF, so bases, static members and methods no longer shift the access string. For an inherited field, only its position within the base is relocated. An index that does not refer to a data member, e.g. into a record the debug info only declares, is reported as an error at the access instead of crashing.

For C++ records clang currently emits a field ordinal instead of the DI element index; #226790 fixes that. Without it, this PR would turn the crash into a silent miscompile (&s->y gets the offset of x below), hence the dependency.

Testing

BTF with single and multiple inheritance, an empty base, a method and a static data member, loaded with BPF_BTF_LOAD on Linux 6.12:

compiler base class in BTF kernel
clang 19 member with type_id=0 rejected: "Invalid type_id"
22.1–main (from the source) — llvm_unreachable(): crash with assertions, UB without
this PR anonymous member of the base type accepted

CO-RE; clang 19 with -fstandalone-debug (with plain -g it segfaults on V), the other columns the same with -g and -g -fstandalone-debug:

access clang 19 main this PR alone this PR + #226790
struct S : Base { int x, y; }, &s->y offset 4, 0:1 (that is x) crash offset 4, 0:1 (that is x) offset 8, 0:2
struct V : virtual Base { int v; }, &v->v offset 3, 0:0 crash error at the access offset 8, 0:1

IR that only declares the record in the debug info gets an error at the access, also for a nested access and for field info. The kernel BPF selftests (999 C programs) compile to byte-identical code, .BTF and .BTF.ext.(checked on the previous revision; C indices are unchanged)

Known limitations

This fixes a crash that is a regression since 22.1 (#155783); once this and #226790 land I'd like to request a backport of both to 23.x.

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)
@github-actions

Copy link
Copy Markdown

Hello @maxskokov 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@maxskokov

Copy link
Copy Markdown
Author

I've read the policies, including the AI Tool Use Policy. AI tool use is noted in the description with an Assisted-by line, and I've reviewed and tested the change myself.

@maxskokov

Copy link
Copy Markdown
Author

cc @tamird (author of #205396) @yonghong-song @eddyz87

maxskokov added a commit to maxskokov/llvm-project that referenced this pull request Sep 26, 2026
A C++ record declared with the class keyword has DW_TAG_class_type in
the debug info. Since llvm#155783, visitCompositeType() hits
llvm_unreachable() on it; before, the class type was emitted as type 0.
The CO-RE pass only computes the field offset for DW_TAG_structure_type,
so an access into a class got the offset 0: for "class C { public: int
x, y; }", &c->y was relocated as offset 0 instead of 4.

Handle DW_TAG_class_type wherever DW_TAG_structure_type is handled, so
that a class becomes a BTF struct (not a union) and is relocated like
one. A class with methods, base classes or static data members also
needs llvm#226717.

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 declared with the class keyword has DW_TAG_class_type in
the debug info. Since llvm#155783, visitCompositeType() hits
llvm_unreachable() on it; before, the class type was emitted as type 0.
The CO-RE pass only computes the field offset for DW_TAG_structure_type,
so an access into a class got the offset 0: for "class C { public: int
x, y; }", &c->y was relocated as offset 0 instead of 4.

Handle DW_TAG_class_type wherever DW_TAG_structure_type is handled, so
that a class becomes a BTF struct (not a union) and is relocated like
one. A class with methods, base classes or static data members also
needs llvm#226717.

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 declared with the class keyword has DW_TAG_class_type in
the debug info. Since llvm#155783, visitCompositeType() hits
llvm_unreachable() on it; before, the class type was emitted as type 0.
The CO-RE pass only computes the field offset for DW_TAG_structure_type,
so an access into a class got the offset 0: for "class C { public: int
x, y; }", &c->y was relocated as offset 0 instead of 4.

Handle DW_TAG_class_type wherever DW_TAG_structure_type is handled, so
that a class becomes a BTF struct (not a union) and is relocated like
one. A class with methods, base classes or static data members also
needs llvm#226717.

Assisted-by: Claude Code (Claude Opus 5.5)
@tamird

tamird commented Sep 27, 2026

Copy link
Copy Markdown
Member

@maxskokov, the Clang-side fix is now in #226790. The intrinsic’s di_index already denotes a debug-info element index, so that PR makes Clang emit the correct index and complete the required record/base metadata. With that fix, the backend can keep direct DI lookups; the C++ BTF member filtering and ordering can remain here.

Assisted-by: OpenAI Codex

The di_index operand of llvm.preserve.{struct,union}.access.index is an
index into the record's debug info elements; llvm#226790 makes clang emit it
that way for C++ records, whose elements also include bases, static data
members, methods and the vtable pointer. Look the element up directly
instead of mapping a field ordinal to it, and only check that it is a
data member before taking its position among the BTF members. An index
that does not refer to one, e.g. into a record the debug info only
declares, is still reported as an error at the access, and no longer
crashes when it is the parent of a nested access.

Assisted-by: Claude Code (Claude Opus 5.5)
@maxskokov

Copy link
Copy Markdown
Author

@tamird Agreed, di_index as a DI element index is the right contract. I pushed 714c834: the field ordinal mapping is gone, the backend looks the element up directly and only checks that it is a non-static member before mapping it to the BTF member index. An index that doesn't refer to one (e.g. a declared-only record from another producer) is still reported at the access, now also as the parent of a nested access. The CO-RE tests use DI element indices now.

I checked it end to end with #226790 applied: &s->y in struct S : Base, Base2 { static int s1; int x; void m(); int y; } is at offset 12 / 0:3, and &v->v in struct V : virtual Base { virtual void f(); int v; } at 8 / 0:1, with plain -g too.

Since this PR alone would turn the C++ CO-RE crash into a wrong offset, I'll mark it as depending on #226790. For 23.x, would you be OK with backporting #226790 together with this one?

@maxskokov

Copy link
Copy Markdown
Author

Superseded by #233715.

@maxskokov maxskokov closed this Oct 20, 2026
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.

2 participants