llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[TableGen] SIGSEGV in AddParentClass for a multiclass with dump instantiated via defm X : MC, Class

Open nodecoda opened this issue 1 month ago • 1 comments

Summary

llvm-tblgen segfaults when a multiclass body contains a dump statement and a defm instantiates that multiclass together with a regular class parent (defm X : MC, C;). TGParser::AddParentClass(RecordsEntry&, ClassReference&) handles Record and Assertion entries but then unconditionally dereferences Entry.Loop; a Dump entry has Loop == nullptr.

Reproducer

class C;
multiclass MC { dump "hi"; def D; }
defm X : MC, C;

Command and output (assertions build, main @ fd64c07f9ae1):

$ llvm-tblgen -print-records test.td
Stack dump:
0.  Program arguments: llvm-tblgen -print-records test.td
...
4  libLLVMTableGen.so ... llvm::TGParser::AddParentClass(llvm::RecordsEntry&, llvm::ClassReference&) + 76
5  libLLVMTableGen.so ... llvm::TGParser::ParseDefm() + 2415
Segmentation fault (core dumped)
# exit code 139

Also reproduces with:

  • an anonymous defm : MC, C;
  • dump as the only entry in the multiclass body
  • dump inside a foreach in the multiclass body

Does not reproduce with:

  • defm X : MC; (no class parent, so the RecordsEntry overload is not called)
  • an assert entry (explicitly skipped at TGParser.cpp:390-391)
  • a defvar (not part of NewEntries)

Analysis

  • dump is a legal multiclass body statement (whitelist at TGParser.cpp:4530-4537).
  • ParseDefm calls AddParentClass(Entry, ParentClass) for every entry when the defm has a class parent (TGParser.cpp:4636-4655, call at :4647).
  • AddParentClass(RecordsEntry&) is at TGParser.cpp:385-401:
if (Entry.Rec)
  return AddParentClass(Entry.Rec.get(), ParentClass);

if (Entry.Assertion)
  return false;

for (auto &E : Entry.Loop->Entries) {   // Entry.Loop is null for Dump
  if (AddParentClass(E, ParentClass))
    return true;
}
  • RecordsEntry already has a Dump member (TGParser.h:57-70); this overload was never updated for it.

History: the recursion was introduced by 7d69e0f37d28 (2018-06-21, "TableGen: Allow foreach in multiclass to depend on template args"); the Assertion special case by 985ab6e1fa57 (2021-04-30, "Fix two bugs in 'defm' when complex 'assert' is involved"). Dump has been legal in multiclass bodies throughout.

Note: this is distinct from PR #236438, which keeps dumps when records are copied/instantiated but does not touch this RecordsEntry overload.

Suggested fix

Treat Dump like Assertion:

if (Entry.Assertion || Entry.Dump)
  return false;

(dump messages are emitted later during resolve()), plus a lit regression test in llvm/test/TableGen/ that checks the exit code and the note: output.

Version

llvm-tblgen 24.0.0git, llvm-project main @ fd64c07f9ae1. llvm/test/TableGen passes 479/479, so this path is currently untested.

nodecoda avatar Nov 04 '26 14:11 nodecoda

Related but independent crash found in the same area: #238797 (!head/!tail on deferred-empty lists → assert/UB).

nodecoda avatar Nov 04 '26 14:11 nodecoda