llvm.org GIT mirror llvm / 0033461
IR: Update references to temporaries before deleting During `MDNode::deleteTemporary()`, call `replaceAllUsesWith(nullptr)` to update all tracking references to `nullptr`. This fixes PR22280, where inverted destruction order between tracking references and the temporaries themselves caused a use-after-free in `LLParser`. An alternative fix would be to add an assertion that there are no users, and continue to fix inverted destruction order in clients (like `LLParser`), but instead I decided to make getting-teardown-right easy. (If someone disagrees let me know.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226866 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
4 changed file(s) with 19 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
726726
727727 /// \brief Deallocate a node created by getTemporary.
728728 ///
729 /// The node must not have any users.
729 /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
730 /// references will be reset.
730731 static void deleteTemporary(MDNode *N);
731732
732733 LLVMContext &getContext() const { return Context.getContext(); }
787787
788788 void MDNode::deleteTemporary(MDNode *N) {
789789 assert(N->isTemporary() && "Expected temporary node");
790 N->replaceAllUsesWith(nullptr);
790791 N->deleteAsSubclass();
791792 }
792793
0 ; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
1 !named = !{!0}
2
3 ; CHECK: [[@LINE+1]]:14: error: use of undefined metadata '!1'
4 !0 = !{!0, !1}
516516 }
517517 }
518518
519 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
520 TrackingMDRef Ref;
521 EXPECT_EQ(nullptr, Ref.get());
522 {
523 auto Temp = MDTuple::getTemporary(Context, None);
524 Ref.reset(Temp.get());
525 EXPECT_EQ(Temp.get(), Ref.get());
526 }
527 EXPECT_EQ(nullptr, Ref.get());
528 }
529
519530 typedef MetadataTest MDLocationTest;
520531
521532 TEST_F(MDLocationTest, Overflow) {