Skip to content

Commit aa7d7b3

Browse files
authored
Revert "[IR] Avoid UB in SymbolTableListTraits" (#142093)
Reverts #139096 due to invalid uses of `offsetof` on non-standard-layout types.
1 parent d490526 commit aa7d7b3

File tree

4 files changed

+4
-22
lines changed

4 files changed

+4
-22
lines changed

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,6 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
546546
return &BasicBlock::InstList;
547547
}
548548

549-
static size_t getSublistOffset(Instruction *) {
550-
return offsetof(BasicBlock, InstList);
551-
}
552-
553549
/// Dedicated function for splicing debug-info: when we have an empty
554550
/// splice (i.e. zero instructions), the caller may still intend any
555551
/// debug-info in between the two "positions" to be spliced.

llvm/include/llvm/IR/Function.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,6 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
811811
return &Function::BasicBlocks;
812812
}
813813

814-
static size_t getSublistOffset(BasicBlock *) {
815-
return offsetof(Function, BasicBlocks);
816-
}
817-
818814
public:
819815
const BasicBlock &getEntryBlock() const { return front(); }
820816
BasicBlock &getEntryBlock() { return front(); }

llvm/include/llvm/IR/Module.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,6 @@ class LLVM_ABI Module {
608608
static GlobalListType Module::*getSublistAccess(GlobalVariable*) {
609609
return &Module::GlobalList;
610610
}
611-
static size_t getSublistOffset(GlobalVariable *) {
612-
return offsetof(Module, GlobalList);
613-
}
614611
friend class llvm::SymbolTableListTraits<llvm::GlobalVariable>;
615612

616613
public:
@@ -621,9 +618,6 @@ class LLVM_ABI Module {
621618
static FunctionListType Module::*getSublistAccess(Function*) {
622619
return &Module::FunctionList;
623620
}
624-
static size_t getSublistOffset(Function *) {
625-
return offsetof(Module, FunctionList);
626-
}
627621

628622
/// Detach \p Alias from the list but don't delete it.
629623
void removeAlias(GlobalAlias *Alias) { AliasList.remove(Alias); }
@@ -663,9 +657,6 @@ class LLVM_ABI Module {
663657
static AliasListType Module::*getSublistAccess(GlobalAlias*) {
664658
return &Module::AliasList;
665659
}
666-
static size_t getSublistOffset(GlobalAlias *) {
667-
return offsetof(Module, AliasList);
668-
}
669660
friend class llvm::SymbolTableListTraits<llvm::GlobalAlias>;
670661

671662
/// Get the Module's list of ifuncs (constant).
@@ -676,9 +667,6 @@ class LLVM_ABI Module {
676667
static IFuncListType Module::*getSublistAccess(GlobalIFunc*) {
677668
return &Module::IFuncList;
678669
}
679-
static size_t getSublistOffset(GlobalIFunc *) {
680-
return offsetof(Module, IFuncList);
681-
}
682670
friend class llvm::SymbolTableListTraits<llvm::GlobalIFunc>;
683671

684672
/// Get the Module's list of named metadata (constant).

llvm/include/llvm/IR/SymbolTableListTraits.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ class SymbolTableListTraits : public ilist_alloc_traits<ValueSubClass> {
7777
/// getListOwner - Return the object that owns this list. If this is a list
7878
/// of instructions, it returns the BasicBlock that owns them.
7979
ItemParentClass *getListOwner() {
80-
size_t Offset = ItemParentClass::getSublistOffset(
81-
static_cast<ValueSubClass *>(nullptr));
80+
size_t Offset = reinterpret_cast<size_t>(
81+
&((ItemParentClass *)nullptr->*ItemParentClass::getSublistAccess(
82+
static_cast<ValueSubClass *>(
83+
nullptr))));
8284
ListTy *Anchor = static_cast<ListTy *>(this);
8385
return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
8486
Offset);

0 commit comments

Comments
 (0)