]> granicus.if.org Git - llvm/commitdiff
[DataLayout] Check StackNatural and FunctionPtr alignments.
authorFlorian Hahn <flo@fhahn.com>
Wed, 7 Aug 2019 17:20:55 +0000 (17:20 +0000)
committerFlorian Hahn <flo@fhahn.com>
Wed, 7 Aug 2019 17:20:55 +0000 (17:20 +0000)
MaybeAlignment asserts that the passed in value is == 0 or a power of 2.

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16272

Reviewers: michaelplatings, gchatelet, jakehehrlich, jfb

Reviewed By: gchatelet

Tags: #llvm

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368191 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/DataLayout.cpp
test/Assembler/datalayout-invalid-function-ptr-alignment.ll [new file with mode: 0644]
test/Assembler/datalayout-invalid-stack-natural-alignment.ll [new file with mode: 0644]
test/Bitcode/invalid-functionptr-align.ll [new file with mode: 0644]
test/Bitcode/invalid-functionptr-align.ll.bc [new file with mode: 0644]

index ab90388fae3ca1d80b2517b0063d8aadfa83704f..1b9ff3922d1015502615ecd2028ce09465aa3af7 100644 (file)
@@ -378,7 +378,10 @@ void DataLayout::parseSpecifier(StringRef Desc) {
       }
       break;
     case 'S': { // Stack natural alignment.
-      StackNaturalAlign = MaybeAlign(inBytes(getInt(Tok)));
+      uint64_t Alignment = inBytes(getInt(Tok));
+      if (Alignment != 0 && !llvm::isPowerOf2_64(Alignment))
+        report_fatal_error("Alignment is neither 0 nor a power of 2");
+      StackNaturalAlign = MaybeAlign(Alignment);
       break;
     }
     case 'F': {
@@ -394,7 +397,10 @@ void DataLayout::parseSpecifier(StringRef Desc) {
                            "datalayout string");
       }
       Tok = Tok.substr(1);
-      FunctionPtrAlign = MaybeAlign(inBytes(getInt(Tok)));
+      uint64_t Alignment = inBytes(getInt(Tok));
+      if (Alignment != 0 && !llvm::isPowerOf2_64(Alignment))
+        report_fatal_error("Alignment is neither 0 nor a power of 2");
+      FunctionPtrAlign = MaybeAlign(Alignment);
       break;
     }
     case 'P': { // Function address space.
diff --git a/test/Assembler/datalayout-invalid-function-ptr-alignment.ll b/test/Assembler/datalayout-invalid-function-ptr-alignment.ll
new file mode 100644 (file)
index 0000000..21cd6a6
--- /dev/null
@@ -0,0 +1,5 @@
+; RUN: not llvm-as %s 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: Alignment is neither 0 nor a power of 2
+
+target datalayout = "Fi24"
diff --git a/test/Assembler/datalayout-invalid-stack-natural-alignment.ll b/test/Assembler/datalayout-invalid-stack-natural-alignment.ll
new file mode 100644 (file)
index 0000000..c8d7ba6
--- /dev/null
@@ -0,0 +1,5 @@
+; RUN: not llvm-as %s 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: Alignment is neither 0 nor a power of 2
+
+target datalayout = "S24"
diff --git a/test/Bitcode/invalid-functionptr-align.ll b/test/Bitcode/invalid-functionptr-align.ll
new file mode 100644 (file)
index 0000000..4ff797a
--- /dev/null
@@ -0,0 +1,5 @@
+; Bitcode with invalid function pointer alignment.
+
+; RUN: not llvm-dis %s.bc -o - 2>&1 | FileCheck %s
+
+CHECK: LLVM ERROR: Alignment is neither 0 nor a power of 2
diff --git a/test/Bitcode/invalid-functionptr-align.ll.bc b/test/Bitcode/invalid-functionptr-align.ll.bc
new file mode 100644 (file)
index 0000000..38e4ed8
Binary files /dev/null and b/test/Bitcode/invalid-functionptr-align.ll.bc differ