<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - sizeof(struct) == 0 for struct with large, explicitly aligned non-static data member"
   href="https://bugs.llvm.org/show_bug.cgi?id=49184">49184</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>sizeof(struct) == 0 for struct with large, explicitly aligned non-static data member
          </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>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>joejev@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24525" name="attach_24525" title="A reproducer which shows how the structure size is incorrectly reported as 0.">attachment 24525</a> <a href="attachment.cgi?id=24525&action=edit" title="A reproducer which shows how the structure size is incorrectly reported as 0.">[details]</a></span>
A reproducer which shows how the structure size is incorrectly reported as 0.

For structs which contain very large non-static data members with an explicit
alignment, sizeof can report 0. I believe I have tracked this down to an
overflow in llvm::alignTo from clang/lib/AST/MathExtras.H (clarifying because I
found an alternative implementation in llvm/Support/Alignment.h).

The example struct I have tested is:

struct node {
    alignas(2) char data[2305843009213693951u];  //
std::numeric_limits<std::ptrdiff_t>::max() / 4;
};

With this definition, sizeof(node) == 0 even though sizeof(node::data) ==
2305843009213693951u.

In ItaniumRecordLayoutBuilder::FinishLayout, when computing RoundedSize, which
appears to be the final size of the struct, we start with a proper Size and
DataSize of 18446744073709551608 (std::numeric_limits<std::ptrdiff_t>::max() /
4 * 8, where 8 is the number of bits in char on my system). The Alignment also
appears correct at 16 (2 * 8, where 8 is the number of bits in char on my
system).
llvm::alignTo is defined as: (Value + Align - 1 - Skew) / Align * Align + Skew
In this invocation, Skew is 0.
When we take Value + Align, 18446744073709551608 + 16, we overflow and get 8,
which is less than Align and the division truncates to 0.

I would be happy to contribute a patch to either fix this issue, but I am not
sure if this should be corrected in llvm::alignTo or if this case should be
detected earlier and error.</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>