From aa7d7b3e4a30d414c7e66278bfee24cdbf4223a6 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Fri, 30 May 2025 14:53:57 +0800 Subject: [PATCH] Revert "[IR] Avoid UB in `SymbolTableListTraits`" (#142093) Reverts llvm/llvm-project#139096 due to invalid uses of `offsetof` on non-standard-layout types. --- llvm/include/llvm/IR/BasicBlock.h | 4 ---- llvm/include/llvm/IR/Function.h | 4 ---- llvm/include/llvm/IR/Module.h | 12 ------------ llvm/include/llvm/IR/SymbolTableListTraits.h | 6 ++++-- 4 files changed, 4 insertions(+), 22 deletions(-) diff --git a/llvm/include/llvm/IR/BasicBlock.h b/llvm/include/llvm/IR/BasicBlock.h index 10617db09fde..9ee0bacb5c25 100644 --- a/llvm/include/llvm/IR/BasicBlock.h +++ b/llvm/include/llvm/IR/BasicBlock.h @@ -546,10 +546,6 @@ private: return &BasicBlock::InstList; } - static size_t getSublistOffset(Instruction *) { - return offsetof(BasicBlock, InstList); - } - /// Dedicated function for splicing debug-info: when we have an empty /// splice (i.e. zero instructions), the caller may still intend any /// debug-info in between the two "positions" to be spliced. diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index 63100568d07e..6d4a53da7ff2 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -811,10 +811,6 @@ private: return &Function::BasicBlocks; } - static size_t getSublistOffset(BasicBlock *) { - return offsetof(Function, BasicBlocks); - } - public: const BasicBlock &getEntryBlock() const { return front(); } BasicBlock &getEntryBlock() { return front(); } diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h index a305dd99962c..0dc8164b9c95 100644 --- a/llvm/include/llvm/IR/Module.h +++ b/llvm/include/llvm/IR/Module.h @@ -608,9 +608,6 @@ private: static GlobalListType Module::*getSublistAccess(GlobalVariable*) { return &Module::GlobalList; } - static size_t getSublistOffset(GlobalVariable *) { - return offsetof(Module, GlobalList); - } friend class llvm::SymbolTableListTraits; public: @@ -621,9 +618,6 @@ public: static FunctionListType Module::*getSublistAccess(Function*) { return &Module::FunctionList; } - static size_t getSublistOffset(Function *) { - return offsetof(Module, FunctionList); - } /// Detach \p Alias from the list but don't delete it. void removeAlias(GlobalAlias *Alias) { AliasList.remove(Alias); } @@ -663,9 +657,6 @@ private: // Please use functions like insertAlias(), removeAlias() etc. static AliasListType Module::*getSublistAccess(GlobalAlias*) { return &Module::AliasList; } - static size_t getSublistOffset(GlobalAlias *) { - return offsetof(Module, AliasList); - } friend class llvm::SymbolTableListTraits; /// Get the Module's list of ifuncs (constant). @@ -676,9 +667,6 @@ private: // Please use functions like insertAlias(), removeAlias() etc. static IFuncListType Module::*getSublistAccess(GlobalIFunc*) { return &Module::IFuncList; } - static size_t getSublistOffset(GlobalIFunc *) { - return offsetof(Module, IFuncList); - } friend class llvm::SymbolTableListTraits; /// Get the Module's list of named metadata (constant). diff --git a/llvm/include/llvm/IR/SymbolTableListTraits.h b/llvm/include/llvm/IR/SymbolTableListTraits.h index 456833fff62c..fcf6f0fb1528 100644 --- a/llvm/include/llvm/IR/SymbolTableListTraits.h +++ b/llvm/include/llvm/IR/SymbolTableListTraits.h @@ -77,8 +77,10 @@ private: /// getListOwner - Return the object that owns this list. If this is a list /// of instructions, it returns the BasicBlock that owns them. ItemParentClass *getListOwner() { - size_t Offset = ItemParentClass::getSublistOffset( - static_cast(nullptr)); + size_t Offset = reinterpret_cast( + &((ItemParentClass *)nullptr->*ItemParentClass::getSublistAccess( + static_cast( + nullptr)))); ListTy *Anchor = static_cast(this); return reinterpret_cast(reinterpret_cast(Anchor)- Offset);