[Bitcode] Report error for missing element type for attr upgrade
authorNikita Popov <npopov@redhat.com>
Fri, 11 Mar 2022 09:17:31 +0000 (10:17 +0100)
committerNikita Popov <npopov@redhat.com>
Fri, 11 Mar 2022 09:18:42 +0000 (10:18 +0100)
Otherwise this is going to crash either the TypeFinder or the
Verifier.

llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/test/Bitcode/Inputs/missing-element-type-for-attribute.bc [new file with mode: 0644]
llvm/test/Bitcode/invalid.test

index 214eb15..6a3012d 100644 (file)
@@ -702,7 +702,7 @@ private:
   /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the
   /// corresponding argument's pointee type. Also upgrades intrinsics that now
   /// require an elementtype attribute.
-  void propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
+  Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
 
   /// Converts alignment exponent (i.e. power of two (or zero)) to the
   /// corresponding alignment to use. If alignment is too large, returns
@@ -4081,8 +4081,8 @@ Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
   return Error::success();
 }
 
-void BitcodeReader::propagateAttributeTypes(CallBase *CB,
-                                            ArrayRef<unsigned> ArgTyIDs) {
+Error BitcodeReader::propagateAttributeTypes(CallBase *CB,
+                                             ArrayRef<unsigned> ArgTyIDs) {
   for (unsigned i = 0; i != CB->arg_size(); ++i) {
     for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
                                      Attribute::InAlloca}) {
@@ -4093,6 +4093,9 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
       CB->removeParamAttr(i, Kind);
 
       Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]);
+      if (!PtrEltTy)
+        return error("Missing element type for typed attribute upgrade");
+
       Attribute NewAttr;
       switch (Kind) {
       case Attribute::ByVal:
@@ -4121,6 +4124,8 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
 
       if (CI.isIndirect && !CB->getParamElementType(ArgNo)) {
         Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
+        if (!ElemTy)
+          return error("Missing element type for inline asm upgrade");
         CB->addParamAttr(
             ArgNo, Attribute::get(Context, Attribute::ElementType, ElemTy));
       }
@@ -4134,6 +4139,8 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
   case Intrinsic::preserve_struct_access_index:
     if (!CB->getParamElementType(0)) {
       Type *ElTy = getPtrElementTypeByID(ArgTyIDs[0]);
+      if (!ElTy)
+        return error("Missing element type for elementtype upgrade");
       Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy);
       CB->addParamAttr(0, NewAttr);
     }
@@ -4141,6 +4148,8 @@ void BitcodeReader::propagateAttributeTypes(CallBase *CB,
   default:
     break;
   }
+
+  return Error::success();
 }
 
 /// Lazily parse the specified function body block.
@@ -5067,7 +5076,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
       cast<InvokeInst>(I)->setCallingConv(
           static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
       cast<InvokeInst>(I)->setAttributes(PAL);
-      propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs);
+      if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs))
+        return Err;
 
       break;
     }
@@ -5161,7 +5171,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
       cast<CallBrInst>(I)->setCallingConv(
           static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
       cast<CallBrInst>(I)->setAttributes(PAL);
-      propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs);
+      if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs))
+        return Err;
       break;
     }
     case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
@@ -5773,7 +5784,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
         TCK = CallInst::TCK_NoTail;
       cast<CallInst>(I)->setTailCallKind(TCK);
       cast<CallInst>(I)->setAttributes(PAL);
-      propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs);
+      if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs))
+        return Err;
       if (FMF.any()) {
         if (!isa<FPMathOperator>(I))
           return error("Fast-math-flags specified for call without "
diff --git a/llvm/test/Bitcode/Inputs/missing-element-type-for-attribute.bc b/llvm/test/Bitcode/Inputs/missing-element-type-for-attribute.bc
new file mode 100644 (file)
index 0000000..5661399
Binary files /dev/null and b/llvm/test/Bitcode/Inputs/missing-element-type-for-attribute.bc differ
index ab24a1b..d5fc75e 100644 (file)
@@ -291,3 +291,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-diimportedentity-record.bc 2
 RUN:   FileCheck --check-prefix=INVALID-DIIMPORTEDENTITY-RECORD %s
 
 INVALID-DIIMPORTEDENTITY-RECORD: Invalid DIImportedEntity record
+
+RUN: not llvm-dis -disable-output -opaque-pointers %p/Inputs/missing-element-type-for-attribute.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=MISSING-ELEMENT-TYPE-FOR-ATTRIBUTE %s
+
+MISSING-ELEMENT-TYPE-FOR-ATTRIBUTE: Missing element type for typed attribute upgrade