From: Saleem Abdulrasool Date: Tue, 24 Oct 2017 00:09:10 +0000 (+0000) Subject: ObjCARC: do not increment past the end of the BB X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cd872ebe94f93d2e4e6893d65d3347f5efeee5c6;p=llvm ObjCARC: do not increment past the end of the BB The `BasicBlock::getFirstInsertionPt` call may return `std::end` for the BB. Dereferencing the end iterator results in an assertion failure "(!NodePtr->isKnownSentinel()), function operator*". Ensure that the returned iterator is valid before dereferencing it. If the end is returned, move one position backward to get a valid insertion point. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316401 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/ObjCARC/PtrState.cpp b/lib/Transforms/ObjCARC/PtrState.cpp index d13e941044f..c512ff584a1 100644 --- a/lib/Transforms/ObjCARC/PtrState.cpp +++ b/lib/Transforms/ObjCARC/PtrState.cpp @@ -250,10 +250,14 @@ void BottomUpPtrState::HandlePotentialUse(BasicBlock *BB, Instruction *Inst, // If this is an invoke instruction, we're scanning it as part of // one of its successor blocks, since we can't insert code after it // in its own block, and we don't want to split critical edges. - if (isa(Inst)) - InsertReverseInsertPt(&*BB->getFirstInsertionPt()); - else - InsertReverseInsertPt(&*++Inst->getIterator()); + BasicBlock::iterator InsertAfter; + if (isa(Inst)) { + const auto IP = BB->getFirstInsertionPt(); + InsertAfter = IP == BB->end() ? std::prev(BB->end()) : IP; + } else { + InsertAfter = std::next(Inst->getIterator()); + } + InsertReverseInsertPt(&*InsertAfter); }; // Check for possible direct uses. diff --git a/test/Transforms/ObjCARC/invoke-2.ll b/test/Transforms/ObjCARC/invoke-2.ll new file mode 100644 index 00000000000..ef5c675a593 --- /dev/null +++ b/test/Transforms/ObjCARC/invoke-2.ll @@ -0,0 +1,57 @@ +; RUN: opt -mtriple x86_64-unknown-windows-msvc -objc-arc -o - %s | llvm-dis -o - - | FileCheck %s + +target triple = "x86_64-unknown-windows-msvc" + +declare i32 @__CxxFrameHandler3(...) + +declare dllimport i8* @objc_msgSend(i8*, i8*, ...) local_unnamed_addr + +declare dllimport i8* @objc_retain(i8* returned) local_unnamed_addr +declare dllimport void @objc_release(i8*) local_unnamed_addr +declare dllimport i8* @objc_retainAutoreleasedReturnValue(i8* returned) local_unnamed_addr + +declare dllimport i8* @objc_begin_catch(i8*) local_unnamed_addr +declare dllimport void @objc_end_catch() local_unnamed_addr + +@OBJC_METH_VAR_NAME_ = private unnamed_addr constant [2 x i8] c"m\00", align 1 +@OBJC_SELECTOR_REFERENCES_ = private externally_initialized global i8* getelementptr inbounds ([2 x i8], [2 x i8]* @OBJC_METH_VAR_NAME_, i64 0, i64 0), section ".objc_selrefs$B", align 8 + +define void @f(i8* %i) local_unnamed_addr personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) { +entry: + %0 = tail call i8* @objc_retain(i8* %i) + %1 = load i8*, i8** @OBJC_SELECTOR_REFERENCES_, align 8, !invariant.load !0 + %call = invoke i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8* (i8*, i8*)*)(i8* %0, i8* %1) + to label %invoke.cont unwind label %catch.dispatch, !clang.arc.no_objc_arc_exceptions !0 + +catch.dispatch: ; preds = %entry + %2 = catchswitch within none [label %catch] unwind to caller + +invoke.cont: ; preds = %entry + %3 = tail call i8* @objc_retainAutoreleasedReturnValue(i8* %call) + tail call void @objc_release(i8* %3) #0, !clang.imprecise_release !0 + br label %eh.cont + +eh.cont: ; preds = %invoke.cont, %catch + tail call void @objc_release(i8* %0) #0, !clang.imprecise_release !0 + ret void + +catch: ; preds = %catch.dispatch + %4 = catchpad within %2 [i8* null, i32 0, i8* null] + %exn.adjusted = tail call i8* @objc_begin_catch(i8* undef) + tail call void @objc_end_catch(), !clang.arc.no_objc_arc_exceptions !0 + br label %eh.cont +} + +; CHECK-LABEL: @f + +; CHECK-NOT: tail call i8* @objc_retain(i8* %i) +; CHECK: load i8*, i8** @OBJC_SELECTOR_REFERENCES_, align 8 + +; CHECK: eh.cont: +; CHECK-NOT: call void @objc_release(i8* +; CHECK: ret void + +attributes #0 = { nounwind } + +!0 = !{} +