[llvm] 9b970eb - [AsmWriter] Don't crash when printing a null operand bundle.

Vasileios Porpodas via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 10 16:38:14 PST 2023


Author: Vasileios Porpodas
Date: 2023-01-10T16:37:23-08:00
New Revision: 9b970eb5daa3d774ae8fdad07491853979f935db

URL: https://github.com/llvm/llvm-project/commit/9b970eb5daa3d774ae8fdad07491853979f935db
DIFF: https://github.com/llvm/llvm-project/commit/9b970eb5daa3d774ae8fdad07491853979f935db.diff

LOG: [AsmWriter] Don't crash when printing a null operand bundle.

Differential Revision: https://reviews.llvm.org/D141415

Added: 
    

Modified: 
    llvm/lib/IR/AsmWriter.cpp
    llvm/unittests/IR/AsmWriterTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 234f4d88bcee1..6108ce09c289a 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -2765,9 +2765,13 @@ void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
         Out << ", ";
       FirstInput = false;
 
-      TypePrinter.print(Input->getType(), Out);
-      Out << " ";
-      WriteAsOperandInternal(Out, Input, WriterCtx);
+      if (Input == nullptr)
+        Out << "<null operand bundle!>";
+      else {
+        TypePrinter.print(Input->getType(), Out);
+        Out << " ";
+        WriteAsOperandInternal(Out, Input, WriterCtx);
+      }
     }
 
     Out << ')';

diff  --git a/llvm/unittests/IR/AsmWriterTest.cpp b/llvm/unittests/IR/AsmWriterTest.cpp
index 9fc7ce410fe09..a3967f72f00f9 100644
--- a/llvm/unittests/IR/AsmWriterTest.cpp
+++ b/llvm/unittests/IR/AsmWriterTest.cpp
@@ -81,4 +81,27 @@ TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
   std::size_t r = OS.str().find("<cannot get addrspace!>");
   EXPECT_TRUE(r != std::string::npos);
 }
+
+TEST(AsmWriterTest, PrintNullOperandBundle) {
+  LLVMContext C;
+  Type *Int32Ty = Type::getInt32Ty(C);
+  FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
+  Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
+  Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
+  std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
+  std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
+  OperandBundleDef Bundle("bundle", UndefValue::get(Int32Ty));
+  std::unique_ptr<InvokeInst> Invoke(
+      InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
+                         Bundle, "result"));
+  // Makes the operand bundle null.
+  Invoke->dropAllReferences();
+  Invoke->setNormalDest(NormalDest.get());
+  Invoke->setUnwindDest(UnwindDest.get());
+
+  std::string S;
+  raw_string_ostream OS(S);
+  Invoke->print(OS);
+  EXPECT_TRUE(OS.str().find("<null operand bundle!>") != std::string::npos);
+}
 }


        


More information about the llvm-commits mailing list