<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 - x86_64: force_align_arg_pointer fails to realign stack before spilling SSE registers"
   href="https://bugs.llvm.org/show_bug.cgi?id=48080">48080</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>x86_64: force_align_arg_pointer fails to realign stack before spilling SSE registers
          </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>bshanks@codeweavers.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>In the prologue of a function marked with ms_abi and force_align_arg_pointer,
which calls a sysv_abi function, Clang aligns %rsp but then uses the unaligned
%rbp to spill %xmm6-15 to the stack. This causes a crash. 
I've seen this cause a real-world game to crash under Wine, where
force_align_arg_pointer is used on all ms_abi functions called by Windows
application code, since only 4 byte alignment is guaranteed.

Here's a test app that misaligns the stack and then calls the ms_abi function.
Crashes with clang 11.0 but succeeds with gcc.

Godbolt: <a href="https://godbolt.org/z/rY7nhq">https://godbolt.org/z/rY7nhq</a>

#include <stdio.h>

int b(int num) {
    printf("%d", num*num);
    return num * num;
}

extern int  __attribute__((ms_abi)) __attribute__((force_align_arg_pointer))
a(int num) {
    return b(num);
}

int main()
{
    //a(4);
    asm( "subq $4, %%rsp\n"
         "movl %0, %%ecx\n"
         "call %P1\n"
         "addq $4, %%rsp\n"
         :
         : "r" (4), "i"(a)
    );
    return 0;
}


Compiled by Clang into:

a(int):                                  # @a(int)
        pushq   %rbp
        movq    %rsp, %rbp
        pushq   %rsi
        pushq   %rdi
        andq    $-16, %rsp                      <== %rsp aligned
        subq    $176, %rsp
        movaps  %xmm15, -32(%rbp)               # 16-byte Spill    <== but %rbp
used for spill
        movaps  %xmm14, -48(%rbp)               # 16-byte Spill
        movaps  %xmm13, -64(%rbp)               # 16-byte Spill
...</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>