diff options
author | Dan Gohman <sunfish@mozilla.com> | 2014-03-06 11:51:23 -0800 |
---|---|---|
committer | Dan Gohman <sunfish@mozilla.com> | 2014-03-06 11:54:11 -0800 |
commit | d644d4a9ca114ceb5d6e69f0c0df6cd571bb33c4 (patch) | |
tree | cf8470bae153163b847a30e90ce368434c9ea3b1 /lib | |
parent | e8e4da6a470cbb8d5b88144363bd76a1d4805a00 (diff) |
Fix a use-after-free error in GlobalOpt CleanupConstantGlobalUsers
This is a backport of r197178 from LLVM trunk.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Transforms/IPO/GlobalOpt.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index cbea844e07..0e628c08a6 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -37,6 +37,7 @@ #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/ValueHandle.h" #include "llvm/Target/TargetLibraryInfo.h" #include <algorithm> using namespace llvm; @@ -473,9 +474,17 @@ static bool CleanupPointerRootUsers(GlobalVariable *GV, static bool CleanupConstantGlobalUsers(Value *V, Constant *Init, DataLayout *TD, TargetLibraryInfo *TLI) { bool Changed = false; - SmallVector<User*, 8> WorkList(V->use_begin(), V->use_end()); + // Note that we need to use a weak value handle for the worklist items. When + // we delete a constant array, we may also be holding pointer to one of its + // elements (or an element of one of its elements if we're dealing with an + // array of arrays) in the worklist. + SmallVector<WeakVH, 8> WorkList(V->use_begin(), V->use_end()); while (!WorkList.empty()) { - User *U = WorkList.pop_back_val(); + Value *UV = WorkList.pop_back_val(); + if (!UV) + continue; + + User *U = cast<User>(UV); if (LoadInst *LI = dyn_cast<LoadInst>(U)) { if (Init) { |