Use common parse routine to read alignment values from bitcode.

Karl Schimpf kschimpf at google.com
Fri Feb 20 15:54:33 PST 2015


Here is another copy of the patch, including
test/BitcodeInputs/invalid-align.bc



Karl Schimpf

On Fri, Feb 20, 2015 at 3:43 PM, Rafael Espíndola <
rafael.espindola at gmail.com> wrote:

> You have to attach b/test/Bitcode/Inputs/invalid-align.bc too (or use
> git diff --binary).
>
> On 20 February 2015 at 18:19, Karl Schimpf <kschimpf at google.com> wrote:
> > Oops, forgot to include final patch to be committed. Here it is.
> >
> >
> > Karl Schimpf
> >
> > On Fri, Feb 20, 2015 at 3:08 PM, Karl Schimpf <kschimpf at google.com>
> wrote:
> >>
> >> Fixed the name to BitcodeReader::parseAlignmentValue, added test
> >> (test/Bitcode/Inputs/invalid-align.bc), and updated
> >> test/BitcodeInputs/invalid.tests.
> >>
> >> jfb: Could you please commit this since I do not have commit privilege
> >> yet. Rafael has reviewed it and has given it a LGTM.
> >>
> >> Karl Schimpf
> >>
> >> On Thu, Feb 19, 2015 at 7:09 PM, Filipe Cabecinhas <filcab at gmail.com>
> >> wrote:
> >>>
> >>> Feel free to use test/Bitcode/invalid.test and add a
> >>> test/Bitcode/Inputs/invalid-*.bc, since it's an invalid bitcode file.
> I've
> >>> been putting them there so they don't “pollute” test/Bitcode.
> >>>
> >>> Thanks,
> >>>
> >>>   Filipe
> >>>
> >>> On Thu, Feb 19, 2015 at 6:11 PM, Rafael Espíndola
> >>> <rafael.espindola at gmail.com> wrote:
> >>>>
> >>>> It is OK to check in a .bc file.
> >>>>
> >>>> BitcodeReader::ParseAlignmentValue
> >>>>
> >>>> Please use the new convention (start functions with lowercase).
> >>>>
> >>>> LGTM with that.
> >>>
> >>>
> >>
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150220/ba4bf2c0/attachment.html>
-------------- next part --------------
diff --git a/include/llvm/IR/Value.h b/include/llvm/IR/Value.h
index 517dd26..b7213a6 100644
--- a/include/llvm/IR/Value.h
+++ b/include/llvm/IR/Value.h
@@ -469,7 +469,8 @@ public:
   ///
   /// This is the greatest alignment value supported by load, store, and alloca
   /// instructions, and global values.
-  static const unsigned MaximumAlignment = 1u << 29;
+  static const unsigned MaxAlignmentExponent = 29;
+  static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
 
   /// \brief Mutate the type of this Value to be of the specified type.
   ///
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index bb6ba6f..3b3a6eb 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -794,6 +794,16 @@ static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
   }
 }
 
+std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
+                                                   unsigned &Alignment) {
+  // Note: Alignment in bitcode files is incremented by 1, so that zero
+  // can be used for default alignment.
+  if (Exponent > Value::MaxAlignmentExponent + 1)
+    return Error("Invalid alignment value");
+  Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
+  return std::error_code();
+}
+
 std::error_code BitcodeReader::ParseAttrKind(uint64_t Code,
                                              Attribute::AttrKind *Kind) {
   *Kind = GetAttrFromCode(Code);
@@ -2462,7 +2472,9 @@ std::error_code BitcodeReader::ParseModule(bool Resume) {
       bool isConstant = Record[1];
       uint64_t RawLinkage = Record[3];
       GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
-      unsigned Alignment = (1 << Record[4]) >> 1;
+      unsigned Alignment;
+      if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
+        return EC;
       std::string Section;
       if (Record[5]) {
         if (Record[5]-1 >= SectionTable.size())
@@ -2542,7 +2554,10 @@ std::error_code BitcodeReader::ParseModule(bool Resume) {
       Func->setLinkage(getDecodedLinkage(RawLinkage));
       Func->setAttributes(getAttributes(Record[4]));
 
-      Func->setAlignment((1 << Record[5]) >> 1);
+      unsigned Alignment;
+      if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
+        return EC;
+      Func->setAlignment(Alignment);
       if (Record[6]) {
         if (Record[6]-1 >= SectionTable.size())
           return Error("Invalid ID");
@@ -3539,12 +3554,17 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
         dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
       Type *OpTy = getTypeByID(Record[1]);
       Value *Size = getFnValueByID(Record[2], OpTy);
-      unsigned AlignRecord = Record[3];
-      bool InAlloca = AlignRecord & (1 << 5);
-      unsigned Align = AlignRecord & ((1 << 5) - 1);
+      uint64_t AlignRecord = Record[3];
+      const uint64_t InAllocaMask = uint64_t(1) << 5;
+      bool InAlloca = AlignRecord & InAllocaMask;
+      unsigned Align;
+      if (std::error_code EC =
+          parseAlignmentValue(AlignRecord & ~InAllocaMask, Align)) {
+        return EC;
+      }
       if (!Ty || !Size)
         return Error("Invalid record");
-      AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
+      AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, Align);
       AI->setUsedWithInAlloca(InAlloca);
       I = AI;
       InstructionList.push_back(I);
@@ -3556,8 +3576,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
           OpNum+2 != Record.size())
         return Error("Invalid record");
-
-      I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
+      unsigned Align;
+      if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
+        return EC;
+      I = new LoadInst(Op, "", Record[OpNum+1], Align);
       InstructionList.push_back(I);
       break;
     }
@@ -3577,8 +3599,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
         return Error("Invalid record");
       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
 
-      I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
-                       Ordering, SynchScope);
+      unsigned Align;
+      if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
+        return EC;
+      I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
       InstructionList.push_back(I);
       break;
     }
@@ -3590,8 +3614,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
           OpNum+2 != Record.size())
         return Error("Invalid record");
-
-      I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
+      unsigned Align;
+      if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
+        return EC;
+      I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
       InstructionList.push_back(I);
       break;
     }
@@ -3613,8 +3639,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
       if (Ordering != NotAtomic && Record[OpNum] == 0)
         return Error("Invalid record");
 
-      I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
-                        Ordering, SynchScope);
+      unsigned Align;
+      if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
+        return EC;
+      I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
       InstructionList.push_back(I);
       break;
     }
diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h
index 5090be4..9803e78 100644
--- a/lib/Bitcode/Reader/BitcodeReader.h
+++ b/lib/Bitcode/Reader/BitcodeReader.h
@@ -335,6 +335,10 @@ private:
     return getFnValueByID(ValNo, Ty);
   }
 
+  /// Converts alignment exponent (i.e. power of two (or zero)) to the
+  /// corresponding alignment to use. If alignment is too large, returns
+  /// a corresponding error code.
+  std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
   std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
   std::error_code ParseModule(bool Resume);
   std::error_code ParseAttributeBlock();
diff --git a/test/Bitcode/Inputs/invalid-align.bc b/test/Bitcode/Inputs/invalid-align.bc
new file mode 100644
index 0000000000000000000000000000000000000000..e84fa6cdda5fa4329ff3c3dee0897cc3b4032a07
GIT binary patch
literal 428
zcmZ>AK5$Qwhk+r3fq{X$Nr8b0NDBcmd!zD1#}h1`Yyw7>lNeigR9QJB<yg9t8hBip
zoF;KQr3e^_Sa3*qav8a(cyLWnR6Y{az$2+xq{4pUgh0}Y1uGnmeP9Hd)2RS77f2f^
zo#0SNX=RzLxTFP0JZ at PyK}5)>t%tiM#YaG*m?gnMgULyXLjt77c at Bt#fD?y-dVx%1
zjs}?{4`tFs7;QV8Em{uuI5n_WR<IY(DCBv@!2ec&?@I%pF^`knp at XvL7Rsa<u!5B}
zOP^u3J>hKI!)$w|qNk*Qy_$o)(xbhgfW4}KxvZkSK%rgoj3D2e349L?`11_-AAjKc
z5Wr_Fa76Z=qRb6N*+YqPX$GCvdz!5;I9qOEw%OBc)52_h=7UIC0(*4^yLm=?5s++X
zFPP9?kicH1!Cv-*6=-aELwh_&d!a;o(Tw)W8I3ycKJdMJ!1wh4-y4QP1qN^!vpMMS
n836s22;_4BX<-&dRu3Z%c2M{;1F at Pwh?oPzVF5%)FfafBb(?oZ

literal 0
HcmV?d00001

diff --git a/test/Bitcode/invalid.test b/test/Bitcode/invalid.test
index 84bc927..fb81888 100644
--- a/test/Bitcode/invalid.test
+++ b/test/Bitcode/invalid.test
@@ -10,6 +10,8 @@ RUN: not llvm-dis -disable-output %p/Inputs/invalid-type-table-forward-ref.bc 2>
 RUN:   FileCheck --check-prefix=BAD-TYPE-TABLE-FORWARD-REF %s
 RUN: not llvm-dis -disable-output %p/Inputs/invalid-bitwidth.bc 2>&1 | \
 RUN:   FileCheck --check-prefix=BAD-BITWIDTH %s
+RUN: not llvm-dis -disable-output %p/Inputs/invalid-align.bc  2>&1 | \
+RUN:   FileCheck --check-prefix=BAD-ALIGN %s
 
 INVALID-ENCODING: Invalid encoding
 BAD-ABBREV: Abbreviation starts with an Array or a Blob
@@ -17,6 +19,7 @@ UNEXPECTED-EOF: Unexpected end of file
 BAD-ABBREV-NUMBER: Invalid abbrev number
 BAD-TYPE-TABLE-FORWARD-REF: Invalid TYPE table: Only named structs can be forward referenced
 BAD-BITWIDTH: Bitwidth for integer type out of range
+BAD-ALIGN: Invalid alignment value
 
 RUN: not llvm-dis -disable-output %p/Inputs/invalid-extractval-array-idx.bc 2>&1 | \
 RUN:   FileCheck --check-prefix=EXTRACT-ARRAY %s


More information about the llvm-commits mailing list