Skip to content

Commit 11dfefb

Browse files
committed
[IR] Avoid UB in SymbolTableListTraits
1 parent fe56c8f commit 11dfefb

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ 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+
549553
/// Dedicated function for splicing debug-info: when we have an empty
550554
/// splice (i.e. zero instructions), the caller may still intend any
551555
/// debug-info in between the two "positions" to be spliced.

llvm/include/llvm/IR/Function.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,10 @@ 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+
814818
public:
815819
const BasicBlock &getEntryBlock() const { return front(); }
816820
BasicBlock &getEntryBlock() { return front(); }

llvm/include/llvm/IR/Module.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/Function.h"
2626
#include "llvm/IR/GlobalAlias.h"
2727
#include "llvm/IR/GlobalIFunc.h"
28+
#include "llvm/IR/GlobalValue.h"
2829
#include "llvm/IR/GlobalVariable.h"
2930
#include "llvm/IR/Metadata.h"
3031
#include "llvm/IR/ProfileSummary.h"
@@ -609,6 +610,9 @@ class LLVM_ABI Module {
609610
static GlobalListType Module::*getSublistAccess(GlobalVariable*) {
610611
return &Module::GlobalList;
611612
}
613+
static size_t getSublistOffset(GlobalVariable*) {
614+
return offsetof(Module, GlobalList);
615+
}
612616
friend class llvm::SymbolTableListTraits<llvm::GlobalVariable>;
613617

614618
public:
@@ -619,6 +623,9 @@ class LLVM_ABI Module {
619623
static FunctionListType Module::*getSublistAccess(Function*) {
620624
return &Module::FunctionList;
621625
}
626+
static size_t getSublistOffset(Function*) {
627+
return offsetof(Module, FunctionList);
628+
}
622629

623630
/// Detach \p Alias from the list but don't delete it.
624631
void removeAlias(GlobalAlias *Alias) { AliasList.remove(Alias); }
@@ -658,6 +665,9 @@ class LLVM_ABI Module {
658665
static AliasListType Module::*getSublistAccess(GlobalAlias*) {
659666
return &Module::AliasList;
660667
}
668+
static size_t getSublistOffset(GlobalAlias*) {
669+
return offsetof(Module, AliasList);
670+
}
661671
friend class llvm::SymbolTableListTraits<llvm::GlobalAlias>;
662672

663673
/// Get the Module's list of ifuncs (constant).
@@ -668,6 +678,9 @@ class LLVM_ABI Module {
668678
static IFuncListType Module::*getSublistAccess(GlobalIFunc*) {
669679
return &Module::IFuncList;
670680
}
681+
static size_t getSublistOffset(GlobalIFunc*) {
682+
return offsetof(Module, IFuncList);
683+
}
671684
friend class llvm::SymbolTableListTraits<llvm::GlobalIFunc>;
672685

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

llvm/include/llvm/IR/SymbolTableListTraits.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ 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 = reinterpret_cast<size_t>(
81-
&((ItemParentClass *)nullptr->*ItemParentClass::getSublistAccess(
82-
static_cast<ValueSubClass *>(
83-
nullptr))));
80+
size_t Offset = ItemParentClass::getSublistOffset(static_cast<ValueSubClass *>(nullptr));
8481
ListTy *Anchor = static_cast<ListTy *>(this);
8582
return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
8683
Offset);

0 commit comments

Comments
 (0)