From 24a669d225f9cacf1cd3846e333f9690a06d1e1c Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 27 Mar 2014 15:26:56 +0000 Subject: Prevent alias from pointing to weak aliases. This adds back r204781. Original message: Aliases are just another name for a position in a file. As such, the regular symbol resolutions are not applied. For example, given define void @my_func() { ret void } @my_alias = alias weak void ()* @my_func @my_alias2 = alias void ()* @my_alias We produce without this patch: .weak my_alias my_alias = my_func .globl my_alias2 my_alias2 = my_alias That is, in the resulting ELF file my_alias, my_func and my_alias are just 3 names pointing to offset 0 of .text. That is *not* the semantics of IR linking. For example, linking in a @my_alias = alias void ()* @other_func would require the strong my_alias to override the weak one and my_alias2 would end up pointing to other_func. There is no way to represent that with aliases being just another name, so the best solution seems to be to just disallow it, converting a miscompile into an error. llvm-svn: 204934 --- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 2 +- llvm/lib/ExecutionEngine/JIT/JITEmitter.cpp | 2 +- llvm/lib/IR/Globals.cpp | 35 +++++++++------------- llvm/lib/IR/Verifier.cpp | 10 +++++-- llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp | 12 ++++---- llvm/lib/Target/PowerPC/PPCFastISel.cpp | 2 +- llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp | 4 +-- llvm/lib/Target/TargetMachine.cpp | 2 +- llvm/lib/Target/X86/X86FastISel.cpp | 2 +- llvm/lib/Target/X86/X86ISelLowering.cpp | 2 +- llvm/lib/Target/XCore/XCoreISelLowering.cpp | 2 +- .../Instrumentation/DataFlowSanitizer.cpp | 3 +- 12 files changed, 37 insertions(+), 41 deletions(-) (limited to 'llvm/lib') diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 61961d8ca2d..52806235fd2 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -1172,7 +1172,7 @@ SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL, if (!GVar) { // If GV is an alias then use the aliasee for determining thread-localness. if (const GlobalAlias *GA = dyn_cast(GV)) - GVar = dyn_cast_or_null(GA->resolveAliasedGlobal(false)); + GVar = dyn_cast_or_null(GA->getAliasedGlobal()); } unsigned Opc; diff --git a/llvm/lib/ExecutionEngine/JIT/JITEmitter.cpp b/llvm/lib/ExecutionEngine/JIT/JITEmitter.cpp index 4b2f012eafb..9d215ec96ae 100644 --- a/llvm/lib/ExecutionEngine/JIT/JITEmitter.cpp +++ b/llvm/lib/ExecutionEngine/JIT/JITEmitter.cpp @@ -688,7 +688,7 @@ void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, return TheJIT->getOrEmitGlobalVariable(GV); if (GlobalAlias *GA = dyn_cast(V)) - return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false)); + return TheJIT->getPointerToGlobal(GA->getAliasedGlobal()); // If we have already compiled the function, return a pointer to its body. Function *F = cast(V); diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index 11152d5d6c2..f338dd76aaa 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -236,10 +236,10 @@ void GlobalAlias::setAliasee(Constant *Aliasee) { setOperand(0, Aliasee); } -GlobalValue *GlobalAlias::getAliasedGlobal() { - Constant *C = getAliasee(); - if (C == 0) return 0; - +static GlobalValue *getAliaseeGV(GlobalAlias *GA) { + Constant *C = GA->getAliasee(); + assert(C && "Must alias something"); + if (GlobalValue *GV = dyn_cast(C)) return GV; @@ -248,30 +248,23 @@ GlobalValue *GlobalAlias::getAliasedGlobal() { CE->getOpcode() == Instruction::AddrSpaceCast || CE->getOpcode() == Instruction::GetElementPtr) && "Unsupported aliasee"); - + return cast(CE->getOperand(0)); } -GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) { +GlobalValue *GlobalAlias::getAliasedGlobal() { SmallPtrSet Visited; - // Check if we need to stop early. - if (stopOnWeak && mayBeOverridden()) - return this; - - GlobalValue *GV = getAliasedGlobal(); - Visited.insert(GV); - - // Iterate over aliasing chain, stopping on weak alias if necessary. - while (GlobalAlias *GA = dyn_cast(GV)) { - if (stopOnWeak && GA->mayBeOverridden()) - break; - - GV = GA->getAliasedGlobal(); + GlobalAlias *GA = this; + for (;;) { + GlobalValue *GV = getAliaseeGV(GA); if (!Visited.insert(GV)) return 0; - } - return GV; + // Iterate over aliasing chain. + GA = dyn_cast(GV); + if (!GA) + return GV; + } } diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 50c6ae204e9..f5c8ac623b1 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -502,10 +502,14 @@ void Verifier::visitGlobalAlias(const GlobalAlias &GA) { } } Assert1(!GV->isDeclaration(), "Alias must point to a definition", &GA); + if (const GlobalAlias *GAAliasee = dyn_cast(GV)) { + Assert1(!GAAliasee->mayBeOverridden(), "Alias cannot point to a weak alias", + &GA); + } - const GlobalValue* Resolved = GA.resolveAliasedGlobal(/*stopOnWeak*/ false); - Assert1(Resolved, - "Aliasing chain should end with function or global variable", &GA); + const GlobalValue *AG = GA.getAliasedGlobal(); + Assert1(AG, "Aliasing chain should end with function or global variable", + &GA); visitGlobalValue(GA); } diff --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp index daf90c86ef9..9ce8ea911cf 100644 --- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp +++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp @@ -381,8 +381,8 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) { if (MO.isGlobal()) { const GlobalValue *GValue = MO.getGlobal(); const GlobalAlias *GAlias = dyn_cast(GValue); - const GlobalValue *RealGValue = GAlias ? - GAlias->resolveAliasedGlobal(false) : GValue; + const GlobalValue *RealGValue = + GAlias ? GAlias->getAliasedGlobal() : GValue; MOSymbol = getSymbol(RealGValue); const GlobalVariable *GVar = dyn_cast(RealGValue); IsExternal = GVar && !GVar->hasInitializer(); @@ -428,8 +428,8 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) { else if (MO.isGlobal()) { const GlobalValue *GValue = MO.getGlobal(); const GlobalAlias *GAlias = dyn_cast(GValue); - const GlobalValue *RealGValue = GAlias ? - GAlias->resolveAliasedGlobal(false) : GValue; + const GlobalValue *RealGValue = + GAlias ? GAlias->getAliasedGlobal() : GValue; MOSymbol = getSymbol(RealGValue); const GlobalVariable *GVar = dyn_cast(RealGValue); @@ -463,8 +463,8 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) { if (MO.isGlobal()) { const GlobalValue *GValue = MO.getGlobal(); const GlobalAlias *GAlias = dyn_cast(GValue); - const GlobalValue *RealGValue = GAlias ? - GAlias->resolveAliasedGlobal(false) : GValue; + const GlobalValue *RealGValue = + GAlias ? GAlias->getAliasedGlobal() : GValue; MOSymbol = getSymbol(RealGValue); const GlobalVariable *GVar = dyn_cast(RealGValue); IsExternal = GVar && !GVar->hasInitializer(); diff --git a/llvm/lib/Target/PowerPC/PPCFastISel.cpp b/llvm/lib/Target/PowerPC/PPCFastISel.cpp index 4753160e091..dd45683032a 100644 --- a/llvm/lib/Target/PowerPC/PPCFastISel.cpp +++ b/llvm/lib/Target/PowerPC/PPCFastISel.cpp @@ -1863,7 +1863,7 @@ unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) { if (!GVar) { // If GV is an alias, use the aliasee for determining thread-locality. if (const GlobalAlias *GA = dyn_cast(GV)) - GVar = dyn_cast_or_null(GA->resolveAliasedGlobal(false)); + GVar = dyn_cast_or_null(GA->getAliasedGlobal()); } // FIXME: We don't yet handle the complexity of TLS. diff --git a/llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp b/llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp index d216026d2e7..b8a042503d2 100644 --- a/llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp +++ b/llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp @@ -1463,8 +1463,8 @@ SDNode *PPCDAGToDAGISel::Select(SDNode *N) { if (GlobalAddressSDNode *G = dyn_cast(GA)) { const GlobalValue *GValue = G->getGlobal(); const GlobalAlias *GAlias = dyn_cast(GValue); - const GlobalValue *RealGValue = GAlias ? - GAlias->resolveAliasedGlobal(false) : GValue; + const GlobalValue *RealGValue = + GAlias ? GAlias->getAliasedGlobal() : GValue; const GlobalVariable *GVar = dyn_cast(RealGValue); assert((GVar || isa(RealGValue)) && "Unexpected global value subclass!"); diff --git a/llvm/lib/Target/TargetMachine.cpp b/llvm/lib/Target/TargetMachine.cpp index f8a58ec2922..fe3c870c980 100644 --- a/llvm/lib/Target/TargetMachine.cpp +++ b/llvm/lib/Target/TargetMachine.cpp @@ -129,7 +129,7 @@ TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { // If GV is an alias then use the aliasee for determining // thread-localness. if (const GlobalAlias *GA = dyn_cast(GV)) - GV = GA->resolveAliasedGlobal(false); + GV = GA->getAliasedGlobal(); const GlobalVariable *Var = cast(GV); bool isLocal = Var->hasLocalLinkage(); diff --git a/llvm/lib/Target/X86/X86FastISel.cpp b/llvm/lib/Target/X86/X86FastISel.cpp index c3e002082dd..9c57f0d1f19 100644 --- a/llvm/lib/Target/X86/X86FastISel.cpp +++ b/llvm/lib/Target/X86/X86FastISel.cpp @@ -363,7 +363,7 @@ bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) { // it works...). if (const GlobalAlias *GA = dyn_cast(GV)) if (const GlobalVariable *GVar = - dyn_cast_or_null(GA->resolveAliasedGlobal(false))) + dyn_cast_or_null(GA->getAliasedGlobal())) if (GVar->isThreadLocal()) return false; diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index ec923ba7aa2..630fba01519 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -8509,7 +8509,7 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { // If GV is an alias then use the aliasee for determining // thread-localness. if (const GlobalAlias *GA = dyn_cast(GV)) - GV = GA->resolveAliasedGlobal(false); + GV = GA->getAliasedGlobal(); SDLoc dl(GA); SDValue Chain = DAG.getEntryNode(); diff --git a/llvm/lib/Target/XCore/XCoreISelLowering.cpp b/llvm/lib/Target/XCore/XCoreISelLowering.cpp index 079e886457b..1b74013ac18 100644 --- a/llvm/lib/Target/XCore/XCoreISelLowering.cpp +++ b/llvm/lib/Target/XCore/XCoreISelLowering.cpp @@ -277,7 +277,7 @@ getGlobalAddressWrapper(SDValue GA, const GlobalValue *GV, const GlobalValue *UnderlyingGV = GV; // If GV is an alias then use the aliasee to determine the wrapper type if (const GlobalAlias *GA = dyn_cast(GV)) - UnderlyingGV = GA->resolveAliasedGlobal(); + UnderlyingGV = GA->getAliasedGlobal(); if (const GlobalVariable *GVar = dyn_cast(UnderlyingGV)) { if ( ( GVar->isConstant() && UnderlyingGV->isLocalLinkage(GV->getLinkage()) ) diff --git a/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp index 0249df1c6d8..df1549d405c 100644 --- a/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp @@ -554,8 +554,7 @@ bool DataFlowSanitizer::runOnModule(Module &M) { ++i; // Don't stop on weak. We assume people aren't playing games with the // instrumentedness of overridden weak aliases. - if (Function *F = dyn_cast( - GA->resolveAliasedGlobal(/*stopOnWeak=*/false))) { + if (Function *F = dyn_cast(GA->getAliasedGlobal())) { bool GAInst = isInstrumented(GA), FInst = isInstrumented(F); if (GAInst && FInst) { addGlobalNamePrefix(GA); -- cgit v1.2.3