<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 - UBSan reports an error and incorrect alignment when global new returns an offset pointer"
   href="https://bugs.llvm.org/show_bug.cgi?id=51693">51693</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>UBSan reports an error and incorrect alignment when global new returns an offset pointer
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>compiler-rt
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>12.0
          </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>ubsan
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>lambert.clara@yahoo.fr
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Hello!

I found that ubsan will report an incorrect alignment for a type in case it is
allocated with the global operator new (without alignment), if we have it
return  an offset ptr.

I wrote a small repro: <a href="https://godbolt.org/z/n8Yh8eoaE">https://godbolt.org/z/n8Yh8eoaE</a>

The type is aligned on 8 bytes (verified by static_assert on its alignof), but
ubsan reports: "constructor call on misaligned address 0x000002af8fd8 for type
'Param', which requires 16 byte alignment".

Now I suppose changing the ptr returned by new that way breaks the 
__STDCPP_DEFAULT_NEW_ALIGNMENT__, but in the specs in
[basic.stc.dynamic.allocation] it says for the non-aligned, non array new:
"Otherwise, the storage is aligned for any object that does not have
new-extended alignment and is of the requested size", which is pretty vague.

I would either expect to get an error message to indicate that break, or
nothing, because in the end the pointer returned by new is 8 bytes aligned, and
matches the 8 bytes alignment requirement of the type.

I think the issue comes from this line:

<a href="https://github.com/llvm/llvm-project/blob/4f7fb13f87e10bd2cd89ccf2be70b026032237a7/clang/lib/CodeGen/CGExprCXX.cpp#L1737">https://github.com/llvm/llvm-project/blob/4f7fb13f87e10bd2cd89ccf2be70b026032237a7/clang/lib/CodeGen/CGExprCXX.cpp#L1737</a>

Instead of the allocator alignment result.getAlignment(), it should be the type
alignment allocAlign. I've tried it, and ran the tests, the error goes away and
the tests pass.

Open to ideas :)
Thanks!</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>