Author: aaronballman Date: Mon Aug 19 06:37:41 2019 New Revision: 369259 URL: http://llvm.org/viewvc/llvm-project?rev=369259&view=rev Log: Don't keep stale pointers to LoopInfos.
CGLoopInfo was keeping pointers to parent loop LoopInfos, but when the loop info vector grew, it reallocated the storage and invalidated all of the parent pointers, causing use-after-free. Manage the lifetimes of the LoopInfos separately so that the pointers aren't stale. Patch by Bevin Hansson. Added: cfe/trunk/test/CodeGen/loop-info-asan.c Modified: cfe/trunk/lib/CodeGen/CGLoopInfo.cpp cfe/trunk/lib/CodeGen/CGLoopInfo.h Modified: cfe/trunk/lib/CodeGen/CGLoopInfo.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGLoopInfo.cpp?rev=369259&r1=369258&r2=369259&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGLoopInfo.cpp (original) +++ cfe/trunk/lib/CodeGen/CGLoopInfo.cpp Mon Aug 19 06:37:41 2019 @@ -563,8 +563,9 @@ void LoopInfo::finish() { void LoopInfoStack::push(BasicBlock *Header, const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc) { - Active.push_back(LoopInfo(Header, StagedAttrs, StartLoc, EndLoc, - Active.empty() ? nullptr : &Active.back())); + Active.emplace_back( + new LoopInfo(Header, StagedAttrs, StartLoc, EndLoc, + Active.empty() ? nullptr : Active.back().get())); // Clear the attributes so nested loops do not inherit them. StagedAttrs.clear(); } @@ -756,16 +757,16 @@ void LoopInfoStack::push(BasicBlock *Hea void LoopInfoStack::pop() { assert(!Active.empty() && "No active loops to pop"); - Active.back().finish(); + Active.back()->finish(); Active.pop_back(); } void LoopInfoStack::InsertHelper(Instruction *I) const { if (I->mayReadOrWriteMemory()) { SmallVector<Metadata *, 4> AccessGroups; - for (const LoopInfo &AL : Active) { + for (const auto &AL : Active) { // Here we assume that every loop that has an access group is parallel. - if (MDNode *Group = AL.getAccessGroup()) + if (MDNode *Group = AL->getAccessGroup()) AccessGroups.push_back(Group); } MDNode *UnionMD = nullptr; Modified: cfe/trunk/lib/CodeGen/CGLoopInfo.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGLoopInfo.h?rev=369259&r1=369258&r2=369259&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGLoopInfo.h (original) +++ cfe/trunk/lib/CodeGen/CGLoopInfo.h Mon Aug 19 06:37:41 2019 @@ -275,11 +275,11 @@ private: bool hasInfo() const { return !Active.empty(); } /// Return the LoopInfo for the current loop. HasInfo should be called /// first to ensure LoopInfo is present. - const LoopInfo &getInfo() const { return Active.back(); } + const LoopInfo &getInfo() const { return *Active.back(); } /// The set of attributes that will be applied to the next pushed loop. LoopAttributes StagedAttrs; /// Stack of active loops. - llvm::SmallVector<LoopInfo, 4> Active; + llvm::SmallVector<std::unique_ptr<LoopInfo>, 4> Active; }; } // end namespace CodeGen Added: cfe/trunk/test/CodeGen/loop-info-asan.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/loop-info-asan.c?rev=369259&view=auto ============================================================================== --- cfe/trunk/test/CodeGen/loop-info-asan.c (added) +++ cfe/trunk/test/CodeGen/loop-info-asan.c Mon Aug 19 06:37:41 2019 @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -o /dev/null + +// This test should not exhibit use-after-free in LoopInfo. + +int a() { + for (;;) + for (;;) + for (;;) + for (;;) + for (;;) + for (;;) + for (;;) + for (;;) + for (;;) + ; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits