llvm.org GIT mirror llvm / f452675
Fix crash when printing instructions that have a metadata attached but no parent. Fix PR24852 (crash with -debug -instcombine) Patch by Than McIntosh <thanm@google.com> Summary: Add guards to the asm writer to prevent crashing when dumping an instruction that has no basic block. Differential Revision: http://reviews.llvm.org/D15798 From: Than McIntosh <thanm@google.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257094 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
3 changed file(s) with 39 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
31203120 return;
31213121
31223122 if (MDNames.empty())
3123 TheModule->getMDKindNames(MDNames);
3123 MDs[0].second->getContext().getMDKindNames(MDNames);
31243124
31253125 for (const auto &I : MDs) {
31263126 unsigned Kind = I.first;
0 //===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 #include "llvm/IR/IRBuilder.h"
9 #include "llvm/IR/Function.h"
10 #include "llvm/IR/LLVMContext.h"
11 #include "llvm/IR/MDBuilder.h"
12 #include "llvm/IR/Module.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16
17 namespace {
18
19 TEST(AsmWriterTest, DebugPrintDetachedInstruction) {
20
21 // PR24852: Ensure that an instruction can be printed even when it
22 // has metadata attached but no parent.
23 LLVMContext Ctx;
24 auto Ty = Type::getInt32Ty(Ctx);
25 auto Undef = UndefValue::get(Ty);
26 std::unique_ptr Add(BinaryOperator::CreateAdd(Undef, Undef));
27 Add->setMetadata(
28 "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))}));
29 std::string S;
30 raw_string_ostream OS(S);
31 Add->print(OS);
32 std::size_t r = OS.str().find(" = add i32 undef, undef, !
33 EXPECT_TRUE(r != std::string::npos);
34 }
35
36 }
55 )
66
77 set(IRSources
8 AsmWriterTest.cpp
89 AttributesTest.cpp
910 ConstantRangeTest.cpp
1011 ConstantsTest.cpp