r181839 - When computing the size of large arrays, use char units instead of bits.

Richard Trieu rtrieu at google.com
Tue May 14 14:59:17 PDT 2013


Author: rtrieu
Date: Tue May 14 16:59:17 2013
New Revision: 181839

URL: http://llvm.org/viewvc/llvm-project?rev=181839&view=rev
Log:
When computing the size of large arrays, use char units instead of bits.
This prevents an overflow and assertion when the number of bits cannot be
stored in 64-bits.

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/test/Sema/offsetof.c

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=181839&r1=181838&r2=181839&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Tue May 14 16:59:17 2013
@@ -1345,8 +1345,27 @@ ASTContext::getTypeInfoDataSizeInChars(Q
   return sizeAndAlign;
 }
 
+/// getConstantArrayInfoInChars - Performing the computation in CharUnits
+/// instead of in bits prevents overflowing the uint64_t for some large arrays.
+std::pair<CharUnits, CharUnits>
+static getConstantArrayInfoInChars(const ASTContext &Context,
+                                   const ConstantArrayType *CAT) {
+  std::pair<CharUnits, CharUnits> EltInfo =
+      Context.getTypeInfoInChars(CAT->getElementType());
+  uint64_t Size = CAT->getSize().getZExtValue();
+  assert((Size == 0 || EltInfo.first.getQuantity() <= (uint64_t)(-1)/Size) &&
+         "Overflow in array type char size evaluation");
+  uint64_t Width = EltInfo.first.getQuantity() * Size;
+  unsigned Align = EltInfo.second.getQuantity();
+  Width = llvm::RoundUpToAlignment(Width, Align);
+  return std::make_pair(CharUnits::fromQuantity(Width),
+                        CharUnits::fromQuantity(Align));
+}
+
 std::pair<CharUnits, CharUnits>
 ASTContext::getTypeInfoInChars(const Type *T) const {
+  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
+    return getConstantArrayInfoInChars(*this, CAT);
   std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
   return std::make_pair(toCharUnitsFromBits(Info.first),
                         toCharUnitsFromBits(Info.second));
@@ -1702,10 +1721,10 @@ int64_t ASTContext::toBits(CharUnits Cha
 /// getTypeSizeInChars - Return the size of the specified type, in characters.
 /// This method does not work on incomplete types.
 CharUnits ASTContext::getTypeSizeInChars(QualType T) const {
-  return toCharUnitsFromBits(getTypeSize(T));
+  return getTypeInfoInChars(T).first;
 }
 CharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
-  return toCharUnitsFromBits(getTypeSize(T));
+  return getTypeInfoInChars(T).first;
 }
 
 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 

Modified: cfe/trunk/test/Sema/offsetof.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/offsetof.c?rev=181839&r1=181838&r2=181839&view=diff
==============================================================================
--- cfe/trunk/test/Sema/offsetof.c (original)
+++ cfe/trunk/test/Sema/offsetof.c Tue May 14 16:59:17 2013
@@ -69,3 +69,15 @@ int test4 = __builtin_offsetof(Array, ar
 int test5() {
   return __builtin_offsetof(Array, array[*(int*)0]); // expected-warning{{indirection of non-volatile null pointer}} expected-note{{__builtin_trap}}
 }
+
+// PR15216
+// Don't crash when taking computing the offset of structs with large arrays.
+const unsigned long Size = (1l << 62);
+
+struct Chunk {
+  char padding[Size];
+  char more_padding[1][Size];
+  char data;
+};
+
+int test6 = __builtin_offsetof(struct Chunk, data); 





More information about the cfe-commits mailing list