[llvm] 67348c8 - [Bitstream] Check for unterminated VBR

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 8 00:49:48 PST 2022


Author: Nikita Popov
Date: 2022-02-08T09:49:39+01:00
New Revision: 67348c8acfc205785996f2aea21b442f4b76f2c2

URL: https://github.com/llvm/llvm-project/commit/67348c8acfc205785996f2aea21b442f4b76f2c2
DIFF: https://github.com/llvm/llvm-project/commit/67348c8acfc205785996f2aea21b442f4b76f2c2.diff

LOG: [Bitstream] Check for unterminated VBR

This avoid shift larger than bitwidth UB.

Added: 
    llvm/test/Bitcode/Inputs/unterminated-vbr.bc

Modified: 
    llvm/include/llvm/Bitstream/BitstreamReader.h
    llvm/test/Bitcode/invalid.test

Removed: 
    llvm/test/Bitcode/Inputs/invalid-value-symbol-table-2.bc
    llvm/test/Bitcode/Inputs/size-not-plausible.bc
    llvm/test/Bitcode/invalid-no-ubsan.test


################################################################################
diff  --git a/llvm/include/llvm/Bitstream/BitstreamReader.h b/llvm/include/llvm/Bitstream/BitstreamReader.h
index 10670648f2fc9..5a66b2dc4971d 100644
--- a/llvm/include/llvm/Bitstream/BitstreamReader.h
+++ b/llvm/include/llvm/Bitstream/BitstreamReader.h
@@ -247,6 +247,10 @@ class SimpleBitstreamCursor {
         return Result;
 
       NextBit += NumBits-1;
+      if (NextBit >= 32)
+        return createStringError(std::errc::illegal_byte_sequence,
+                                 "Unterminated VBR");
+
       MaybeRead = Read(NumBits);
       if (!MaybeRead)
         return MaybeRead;
@@ -274,6 +278,10 @@ class SimpleBitstreamCursor {
         return Result;
 
       NextBit += NumBits-1;
+      if (NextBit >= 64)
+        return createStringError(std::errc::illegal_byte_sequence,
+                                 "Unterminated VBR");
+
       MaybeRead = Read(NumBits);
       if (!MaybeRead)
         return MaybeRead;

diff  --git a/llvm/test/Bitcode/Inputs/invalid-value-symbol-table-2.bc b/llvm/test/Bitcode/Inputs/invalid-value-symbol-table-2.bc
deleted file mode 100644
index a6e4250730f90..0000000000000
Binary files a/llvm/test/Bitcode/Inputs/invalid-value-symbol-table-2.bc and /dev/null 
diff er

diff  --git a/llvm/test/Bitcode/Inputs/size-not-plausible.bc b/llvm/test/Bitcode/Inputs/unterminated-vbr.bc
similarity index 100%
rename from llvm/test/Bitcode/Inputs/size-not-plausible.bc
rename to llvm/test/Bitcode/Inputs/unterminated-vbr.bc

diff  --git a/llvm/test/Bitcode/invalid-no-ubsan.test b/llvm/test/Bitcode/invalid-no-ubsan.test
deleted file mode 100644
index 2cc8e057ac7fb..0000000000000
--- a/llvm/test/Bitcode/invalid-no-ubsan.test
+++ /dev/null
@@ -1,19 +0,0 @@
-# These tests cover invalid inputs.
-# When running under UBSan these tests hit UBSan issues before the validity
-# checks that the test is intending to exercise.
-# Under UBSan these tests fail because UBSan error is not the expected error.
-#
-# TODO: This code should be fixed to not exhibit UB, and these tests should be
-# incorporated back into invalid.test and run under UBSan again.
-
-UNSUPPORTED: ubsan
-
-RUN: not llvm-dis -disable-output %p/Inputs/size-not-plausible.bc 2>&1 | \
-RUN:   FileCheck --check-prefix=SIZE-NOT-PLAUSIBLE %s
-
-SIZE-NOT-PLAUSIBLE: Size is not plausible
-
-RUN: not llvm-dis -disable-output %p/Inputs/invalid-value-symbol-table-2.bc 2>&1 | \
-RUN:   FileCheck --check-prefix=INVALID-VALUE-SYMBOL-TABLE-2 %s
-
-INVALID-VALUE-SYMBOL-TABLE-2: Expected value symbol table subbloc

diff  --git a/llvm/test/Bitcode/invalid.test b/llvm/test/Bitcode/invalid.test
index c5cbabfd80ba1..8af940d5606aa 100644
--- a/llvm/test/Bitcode/invalid.test
+++ b/llvm/test/Bitcode/invalid.test
@@ -266,3 +266,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-value-symbol-table.bc 2>&1 |
 RUN:   FileCheck --check-prefix=INVALID-VALUE-SYMBOL-TABLE %s
 
 INVALID-VALUE-SYMBOL-TABLE: Invalid value reference in symbol table
+
+RUN: not llvm-dis -disable-output %p/Inputs/unterminated-vbr.bc 2>&1 | \
+RUN:   FileCheck --check-prefix=UNTERMINATED-VBR %s
+
+UNTERMINATED-VBR: Unterminated VBR


        


More information about the llvm-commits mailing list