<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/144164>144164</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang on macOS does not emit stack-passed arguments when calling __attribute__((ms_abi)) functions from System V callers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Inakito
      </td>
    </tr>
</table>

<pre>
    Description:

When compiling with Apple Clang 17.0.0 on macOS, calls from System V ABI functions (e.g., main) to functions marked with __attribute__((ms_abi)) do not correctly follow the Microsoft x64 ABI. Specifically, arguments that exceed the first four (passed in RCX, RDX, R8, R9) are not passed at all — they are not placed on the stack as required.

This causes serious runtime issues in ms_abi variadic functions which expect those parameters.

Environment:

- macOS 14.x (Apple Silicon or Intel, both reproducible)
- Apple Clang 17.0.0 (Xcode 15.3)
- Target: x86_64 (System V by default)
- Tested with -O0 and -O1

Reproduction Case:

```c
#include <stdio.h>

__attribute__((ms_abi)) void systemv_printf(const char *fmt, ...);

int main() {
    double f1 = 1.1, f2 = 2.2, f3 = 3.3, f4 = 4.4, f5 = 5.5;

    // Only the first 4 arguments are passed
    systemv_printf("%g %g %g %g %g\n", f1, f2, f3, f4, f5);
    return 0;
}
```

Implementation of systemv_printf attempts to read the 5th argument from the stack using frame pointer offset — but it's missing.

Expected behavior:

According to the Microsoft x64 ABI specification:
- First four arguments → in RCX, RDX, R8, R9
- Floating-point args duplicated into XMM0–XMM3
- Additional arguments → placed on the stack, 8-byte aligned
- Callee uses va_start or manual stack access to retrieve extra args

Actual behavior:

Clang emits code for the ms_abi call with only the first 4 arguments passed in registers. The 5th and beyond are not passed at all — no stack space is used, and the values are never pushed or stored.

Disassembly confirms this — even with volatile variables and -O0:

```asm
; systemv_printf("%g %g %g %g %g\n", f1, f2, f3, f4, f5);

leaq   ... -> %rcx    ; format string
movq   ... -> %rdx    ; f1
movq   ... -> %r8     ; f2
movq   ... -> %r9     ; f3
; NO instruction for f4 or f5 (e.g. mov to [rsp + N])
callq  systemv_printf
```

Control Test (ms_abi → ms_abi):

When both caller and callee are marked __attribute__((ms_abi)), all arguments including the 5th float are properly passed — the last argument is correctly copied to [rsp+0x20] and accessible.

This suggests that Clang fails to emit proper Microsoft ABI code only when the caller is System V.

Impact:

This breaks interoperability between System V code (the default on macOS) and ms_abi functions — especially for printf-like variadic trampolines or ABI bridging layers, which depend on full argument delivery.

Suggested Fix:

Ensure that when a System V function calls an __attribute__((ms_abi)) function:
- The Microsoft ABI rules are fully applied
- Additional arguments beyond the 4th are pushed on the stack

Thank you!

Let me know if you'd like a full reproducer or test case archive.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0V09v4z4O_TTKhYiRyHaaHHJo0ykwwPZXYDrYnVshS3SsrSx5JDltvv2CkvNnfu3s7GWBooUaiXwkHx8ZEYLeW8Qtq-9YfT8TY-yc33614lVHN2ucOm7vMUivh6idZeUtW9DPvzq0IF0_aKPtHt507OB2GAzCzgi7h-VNsSgW4Cz0Qj49M74DKYwJ0HrXw_MxROzhn3B79xXa0UqyHYDxNRb7gi73QlvGNxDd1ee98K-osrOXFxGj180Y8eWF8TXj6z68iEYzvqGHyoF1EaTzHmU0R2idMe4NYofwqKV3wbUR3lcVYSjgeUCpW00Yj-Rf-P3Yo40BYici4LtEVOlxq32I0LrRE95BhIAKtIVvux_08Nt9_rNOvxMU4TFhme6KCMIYYF84Wy_YpiKrx8slIyQqShw5C1HIVxABPP4ctUdV5PR_73QAKcaAAQJ67cYAfrRR9wg6hBEDQcoJgYPwWigtrzL51mnZAb4PKCPEzgWEQXjRY0QfJh9f7EF7ZykL57LPczlhWRXvFH4u-bM2WjoLzsNXG9FQ6I2LHXgcvFOj1I1BqgsZ-IQljK9_SKcQlnVRnu59F36P5Bne16uXVUW3zrxpjqCwFaOJ5-sY4okb86cFCKtg_rT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