<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Many allocations in clang aren't correctly aligned."
   href="http://llvm.org/bugs/show_bug.cgi?id=22728">22728</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Many allocations in clang aren't correctly aligned.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ahmed.bougacha@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider "DeclRefExpr::Create(.., NameInfo, ..)".  It allocates itself
and a few other structs in a single allocation, using the well-known
pattern:

  std::size_t Size = sizeof(DeclRefExpr);
  if (...)
    Size += sizeof(...);
  ...
  void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());

The alignment should also take into account the trailing classes,
really.  In every Allocate call that has a non-trivial size, the
alignment should be correspondingly non-trivial.  Some calls just use
ASTContext's default, 8 (enough for most purposes, at least; better
than the explicit alignOf, which might be 4).


[skip forward if you don't care about the investigation]

Currently, I think we get away with this because 1) X86 is lenient, 2)
most people don't self-host on another architecture, and 3) at least
on ARM, alignment checking has to be explicitly enabled, statically or
dynamically.

On ARM, if you try running clang with either:
- SCTRL.A==1 (alignment checking on non-strict-aligned instructions)
(I should say I couldn't get this to work); or
- different LLVM CodeGen picking stricter instructions that actually
enforce alignment (what made me investigate),

it should crash, at some point.  On the LIT-tests, the first (of many)
to fail was
  test/Analysis/dead-stores.cpp
Indeed, on trunk clang, the "this" pointer in
  ASTTemplateArgumentListInfo::initializeFrom(Info, b, b, b)
is sometimes dynamically 4-aligned, whereas it is supposed to be
8-aligned (according to alignof and the IR we generate.)



This pattern is pervasive throughout clang, and even though a few
classes try to get it right (with a trailing AlignedCharArray), most
don't.

UBSan's alignment sanitizer would be *very* useful here.  On ARM,
self-hosting clang using clang, with the alignment sanitizer:
  -fsanitize=alignment -fsanitize-undefined-trap-on-error

Should exhibit the problems.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>