[PATCH] Perform calculation of array size in chars instead of bits to prevent an assertion.

Richard Trieu rtrieu at google.com
Mon May 13 19:44:50 PDT 2013


  Change a few calls from getTypeInfo to getTypeInfoInChars.  This fixes the asserts on padding[1][Size].  This should fix the crashes on valid code.  There is still a similar crash on invalid code that this does not fix.

http://llvm-reviews.chandlerc.com/D781

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D781?vs=1945&id=1953#toc

Files:
  test/Sema/offsetof.c
  lib/AST/ASTContext.cpp

Index: test/Sema/offsetof.c
===================================================================
--- test/Sema/offsetof.c
+++ test/Sema/offsetof.c
@@ -69,3 +69,15 @@
 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); 
Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -1343,8 +1343,27 @@
   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));
@@ -1700,10 +1719,10 @@
 /// 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D781.3.patch
Type: text/x-patch
Size: 2628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130513/aa5486f5/attachment.bin>


More information about the cfe-commits mailing list