From 7dc896fccea25fd48d1bf44228ea4848ff8ebcfa Mon Sep 17 00:00:00 2001 From: Filipe Cabecinhas Date: Wed, 15 Apr 2015 08:48:08 +0000 Subject: [PATCH] Verify sizes when trying to read a VBR Also added an assert to ReadVBR64. llvm-svn: 234984 --- llvm/include/llvm/Bitcode/BitstreamReader.h | 1 + llvm/lib/Bitcode/Reader/BitstreamReader.cpp | 2 ++ llvm/test/Bitcode/Inputs/invalid-VBR-too-big.bc | Bin 0 -> 612 bytes llvm/test/Bitcode/invalid.test | 5 +++++ 4 files changed, 8 insertions(+) create mode 100644 llvm/test/Bitcode/Inputs/invalid-VBR-too-big.bc diff --git a/llvm/include/llvm/Bitcode/BitstreamReader.h b/llvm/include/llvm/Bitcode/BitstreamReader.h index 18f6b9e011e0..bae816675c00 100644 --- a/llvm/include/llvm/Bitcode/BitstreamReader.h +++ b/llvm/include/llvm/Bitcode/BitstreamReader.h @@ -395,6 +395,7 @@ public: // Read a VBR that may have a value up to 64-bits in size. The chunk size of // the VBR must still be <= 32 bits though. uint64_t ReadVBR64(unsigned NumBits) { + assert(NumBits <= 64 && "VBR can only be up to 64 bits in size."); uint32_t Piece = Read(NumBits); if ((Piece & (1U << (NumBits-1))) == 0) return uint64_t(Piece); diff --git a/llvm/lib/Bitcode/Reader/BitstreamReader.cpp b/llvm/lib/Bitcode/Reader/BitstreamReader.cpp index beaaf7a7d667..6e3bea1e87bc 100644 --- a/llvm/lib/Bitcode/Reader/BitstreamReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitstreamReader.cpp @@ -62,6 +62,8 @@ static uint64_t readAbbreviatedField(BitstreamCursor &Cursor, case BitCodeAbbrevOp::Fixed: return Cursor.Read((unsigned)Op.getEncodingData()); case BitCodeAbbrevOp::VBR: + if ((unsigned)Op.getEncodingData() > 64) + report_fatal_error("Invalid record"); return Cursor.ReadVBR64((unsigned)Op.getEncodingData()); case BitCodeAbbrevOp::Char6: return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6)); diff --git a/llvm/test/Bitcode/Inputs/invalid-VBR-too-big.bc b/llvm/test/Bitcode/Inputs/invalid-VBR-too-big.bc new file mode 100644 index 0000000000000000000000000000000000000000..35d00ba154b5eb9dc4a6e06f94db7ea3c0202667 GIT binary patch literal 612 zcmZ>AK5$Qwhk;=l0|NthlL7-1kQM@B_D1E2jwe_=*#wL%Co#70sIqc!%CU4OHSoAH zIZfhrN)a#;vEY#K)3syKB`@je^r&ED}feT0* zDV^X@NNHu6thl5FNIY&?I6*|nr>%#(CB;WTK$)SK#Y0d4XtDDYkS-vQSOjzx2pkYd zg)kV}G?*bQ0~bjMqe1Z$RPIS41A`!tZOqXibL62+nh2w9hqFb?;U1?3_R0$O;u(cJ z&lvdM3h;et;4|iNk~?%z_S{05Gy_(!vS#Ts%(f?-ZF`t)fo2pcFfeccX*UIniM%`x z#u5hN z4-hyjl9;9tvsG3=Q1NP;gG))hQo~vY(PFX uy-?qS?S<)aoTZRLtR9e?K=w2ySqQLT+5r@SxCf^0Ad%`AlX05|G7|s^=YF67 literal 0 HcmV?d00001 diff --git a/llvm/test/Bitcode/invalid.test b/llvm/test/Bitcode/invalid.test index 9cab227ab198..59543d2ae79d 100644 --- a/llvm/test/Bitcode/invalid.test +++ b/llvm/test/Bitcode/invalid.test @@ -55,3 +55,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-no-proper-module.bc 2>&1 | \ RUN: FileCheck --check-prefix=NO-MODULE %s NO-MODULE: Malformed IR file + +RUN: not llvm-dis -disable-output %p/Inputs/invalid-VBR-too-big.bc 2>&1 | \ +RUN: FileCheck --check-prefix=HUGE-VBR %s + +HUGE-VBR: Invalid record