[PATCH] D141343: [AsmWriter] Don't crash when printing addrspace and the operand is null

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


vporpo updated this revision to Diff 487895.
vporpo added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D141343

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
@@ -62,4 +62,23 @@
             OS.str());
 }
 
+TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
+  LLVMContext Ctx;
+  std::unique_ptr<Module> M;
+  SmallVector<Type *, 3> FArgTypes;
+  FArgTypes.push_back(Type::getInt64Ty(Ctx));
+  FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
+  Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
+  Argument *Arg0 = F->getArg(0);
+  Value *Args[] = {Arg0};
+  std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
+  // This will make Call's operand null.
+  Call->dropAllReferences();
+
+  std::string S;
+  raw_string_ostream OS(S);
+  Call->print(OS);
+  std::size_t r = OS.str().find("<cannot get addrspace!>");
+  EXPECT_TRUE(r != std::string::npos);
+}
 }
Index: llvm/lib/IR/AsmWriter.cpp
===================================================================
--- llvm/lib/IR/AsmWriter.cpp
+++ llvm/lib/IR/AsmWriter.cpp
@@ -4008,6 +4008,10 @@
 static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
                                     raw_ostream &Out) {
   // We print the address space of the call if it is non-zero.
+  if (Operand == nullptr) {
+    Out << " <cannot get addrspace!>";
+    return;
+  }
   unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
   bool PrintAddrSpace = CallAddrSpace != 0;
   if (!PrintAddrSpace) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141343.487895.patch
Type: text/x-patch
Size: 1578 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230110/8a985852/attachment.bin>


More information about the llvm-commits mailing list