On Fri, May 10, 2013 at 7:35 PM, Richard Trieu <span dir="ltr"><<a href="mailto:rtrieu@google.com" target="_blank">rtrieu@google.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
For large arrays, the size calculation in bits may overflow a uint64_t.  If only the size in chars is important, perform the calculation in chars to prevent this overflow which triggers an assertion.  This fixes PR15216.<br>

<br>
<a href="http://llvm-reviews.chandlerc.com/D781" target="_blank">http://llvm-reviews.chandlerc.com/D781</a><br>
<br>
Files:<br>
  test/Sema/offsetof.c<br>
  include/clang/AST/ASTContext.h<br>
  lib/AST/ASTContext.cpp<br>
<br>
Index: test/Sema/offsetof.c<br>
===================================================================<br>
--- test/Sema/offsetof.c<br>
+++ test/Sema/offsetof.c<br>
@@ -69,3 +69,15 @@<br>
 int test5() {<br>
   return __builtin_offsetof(Array, array[*(int*)0]); // expected-warning{{indirection of non-volatile null pointer}} expected-note{{__builtin_trap}}<br>
 }<br>
+<br>
+// PR15216<br>
+// Don't crash on when taking computing the offset of structs with large arrays.<br>
+const unsigned long Size = (1l << 62);<br>
+<br>
+struct Chunk {<br>
+  char padding[Size];<br>
+  char data;<br>
+};<br>
+<br>
+int test6 = __builtin_offsetof(struct Chunk, data);<br>
+<br>
Index: include/clang/AST/ASTContext.h<br>
===================================================================<br>
--- include/clang/AST/ASTContext.h<br>
+++ include/clang/AST/ASTContext.h<br>
@@ -1591,6 +1591,9 @@<br>
   std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T) const;<br>
   std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T) const;<br>
<br>
+  std::pair<CharUnits, CharUnits> getConstantArrayInfoInChars(<br>
+      const ConstantArrayType* CAT) const;<br>
+<br>
   /// \brief Return the "preferred" alignment of the specified type \p T for<br>
   /// the current target, in bits.<br>
   ///<br>
Index: lib/AST/ASTContext.cpp<br>
===================================================================<br>
--- lib/AST/ASTContext.cpp<br>
+++ lib/AST/ASTContext.cpp<br>
@@ -1345,11 +1345,28 @@<br>
<br>
 std::pair<CharUnits, CharUnits><br>
 ASTContext::getTypeInfoInChars(const Type *T) const {<br>
+  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))<br>
+    return getConstantArrayInfoInChars(CAT);<br>
   std::pair<uint64_t, unsigned> Info = getTypeInfo(T);<br>
   return std::make_pair(toCharUnitsFromBits(Info.first),<br>
                         toCharUnitsFromBits(Info.second));<br>
 }<br>
<br>
+/// getConstantArrayInfoInChars - Performing the computation in CharUnits<br>
+/// instead of in bits prevents overflowing the uint64_t for some large arrays.<br>
+std::pair<CharUnits, CharUnits><br>
+ASTContext::getConstantArrayInfoInChars(const ConstantArrayType *CAT) const {<br></blockquote><div><br></div><div>Can you make this a static non-member helper function? I don't think we need it to be part of ASTContext's interface.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+  std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());<br>
+  uint64_t Size = CAT->getSize().getZExtValue();<br>
+  assert((Size == 0 || EltInfo.first/getCharWidth() <= (uint64_t)(-1)/Size) &&<br>
+         "Overflow in array type char size evaluation");<br>
+  uint64_t Width = EltInfo.first / getCharWidth() * Size;<br>
+  unsigned Align = EltInfo.second / getCharWidth();<br>
+  Width = llvm::RoundUpToAlignment(Width, Align);<br>
+  return std::make_pair(CharUnits::fromQuantity(Width),<br>
+                        CharUnits::fromQuantity(Align));<br>
+}<br>
+<br>
 std::pair<CharUnits, CharUnits><br>
 ASTContext::getTypeInfoInChars(QualType T) const {<br>
   return getTypeInfoInChars(T.getTypePtr());<br>
<br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br>