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

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


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9b970eb5daa3: [AsmWriter] Don't crash when printing a null operand bundle. (authored by vporpo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141415/new/

https://reviews.llvm.org/D141415

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


Index: llvm/unittests/IR/AsmWriterTest.cpp
===================================================================
--- llvm/unittests/IR/AsmWriterTest.cpp
+++ llvm/unittests/IR/AsmWriterTest.cpp
@@ -81,4 +81,27 @@
   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);
+}
 }
Index: llvm/lib/IR/AsmWriter.cpp
===================================================================
--- llvm/lib/IR/AsmWriter.cpp
+++ llvm/lib/IR/AsmWriter.cpp
@@ -2765,9 +2765,13 @@
         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 << ')';


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141415.488032.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230111/a05c42ed/attachment.bin>


More information about the llvm-commits mailing list