Repository navigation
Conversation
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)
|
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.
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 questionsHow 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 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, |
|
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. |
|
cc @tamird (author of #205396) @yonghong-song @eddyz87 |
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)
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)
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, the Clang-side fix is now in #226790. The intrinsic’s 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)
|
@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? |
|
Superseded by #233715. |
Depends on #226790; please land that first (see CO-RE below).
Since #155783 (LLVM 22.1), compiling C++ for BPF with
-ghitsllvm_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: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->ygets the offset ofxbelow), hence the dependency.Testing
BTF with single and multiple inheritance, an empty base, a method and a static data member, loaded with
BPF_BTF_LOADon Linux 6.12:type_id=0llvm_unreachable(): crash with assertions, UB withoutCO-RE; clang 19 with
-fstandalone-debug(with plain-git segfaults onV), the other columns the same with-gand-g -fstandalone-debug:struct S : Base { int x, y; },&s->y0:1(that isx)0:1(that isx)0:2struct V : virtual Base { int v; },&v->v0:00:1IR 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,
.BTFand.BTF.ext.(checked on the previous revision; C indices are unchanged)Known limitations
bpftool btf dump format cprints an anonymous base member asstruct Base;, which only keeps the layout with-fms-extensionsand cannot express an empty base overlapping a field. BTF from pahole has the same shape._vptr$Xis not a valid BTF name; a separate PR escapes such names.class(DW_TAG_class_type) still crash BTF since [BPF] Support forDW_TAG_variant_partin BTF generation #155783 and get a wrong CO-RE offset; [BPF] Treat class records as structs in BTF and CO-RE #226723 handles them.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)