[BitcodeReader] Check if we can create a null constant for type.
authorFlorian Hahn <flo@fhahn.com>
Wed, 21 Aug 2019 18:20:11 +0000 (18:20 +0000)
committerFlorian Hahn <flo@fhahn.com>
Wed, 21 Aug 2019 18:20:11 +0000 (18:20 +0000)
We cannot create null constants for certain types, e.g. VoidTy,
FunctionTy or LabelTy. getNullValue asserts if we pass in an
unsupported type. We should also check for opaque types, but I'm not
sure how.

This fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14795.

Reviewers: t.p.northover, jfb, vsk

Reviewed By: vsk

Tags: #llvm

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

llvm-svn: 369557

llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/test/Bitcode/invalid-type-for-null-constant.ll [new file with mode: 0644]
llvm/test/Bitcode/invalid-type-for-null-constant.ll.bc [new file with mode: 0644]

index 32ae4cd..af90607 100644 (file)
@@ -2377,6 +2377,8 @@ Error BitcodeReader::parseConstants() {
       CurTy = flattenPointerTypes(CurFullTy);
       continue;  // Skip the ValueList manipulation.
     case bitc::CST_CODE_NULL:      // NULL
+      if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())
+        return error("Invalid type for a constant null value");
       V = Constant::getNullValue(CurTy);
       break;
     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
diff --git a/llvm/test/Bitcode/invalid-type-for-null-constant.ll b/llvm/test/Bitcode/invalid-type-for-null-constant.ll
new file mode 100644 (file)
index 0000000..28756fa
--- /dev/null
@@ -0,0 +1,6 @@
+; Bitcode with a CST_CODE_NULL with void type.
+
+; RUN: not llvm-dis %s.bc -o - 2>&1 | FileCheck %s
+
+; CHECK: error: Invalid type for a constant null value
+
diff --git a/llvm/test/Bitcode/invalid-type-for-null-constant.ll.bc b/llvm/test/Bitcode/invalid-type-for-null-constant.ll.bc
new file mode 100644 (file)
index 0000000..6196ebc
Binary files /dev/null and b/llvm/test/Bitcode/invalid-type-for-null-constant.ll.bc differ