Issue 237188
Summary [clang-cl][MS ABI] Incorrect return convention for std::strong_ordering with MSVC STL 14.51 causes memory corruption
Labels new issue
Assignees
Reporter PColis
    ## Summary

I've found an ABI incompatibility between clang-cl 22.1.3 and MSVC 14.51 involving the C++20 comparison category types `std::strong_ordering`, `std::weak_ordering`, and `std::partial_ordering`.

Both compilers agree on the mangled function names but disagree on how these types are returned on Windows x64:

- **MSVC** returns them directly in `AL`.
- **clang-cl** uses a hidden return pointer in `RCX`.

This can cause silent memory corruption when mixing object files compiled by MSVC and clang-cl, which is a common scenario when using prebuilt libraries on Windows.

**I've already reported this to the Microsoft STL team:**

[Microsoft STL issue — https://github.com/microsoft/STL/issues/6504]

I'm opening this issue separately because the underlying disagreement appears to come from Clang's Microsoft ABI return classification.

## Environment

- Windows x64
- clang-cl 22.1.3
- MSVC toolset 14.51.36231
- Microsoft STL 14.51
- C++20 or later
- MSVC-compiled third-party libraries, clang-cl-compiled application
- Both `link.exe` and `lld-link.exe` tested

## Minimal reproducer

The ABI difference can be reproduced with a single function:

```cpp
#include <compare>

__declspec(noinline)
std::strong_ordering f(int a, int b) {
    return a <=> b;
}
```

Compile it with both compilers, using the same MSVC 14.51 headers:

```bat
cl /nologo /std:c++20 /Od /c /Fomsvc.obj repro.cpp
clang-cl /nologo /std:c++20 /Od /c /Foclang.obj repro.cpp
```

Then inspect the generated assembly:

```bat
dumpbin /disasm msvc.obj
dumpbin /disasm clang.obj
```

Both objects export the same decorated symbol:

```text
?f@@YA?AUstrong_ordering@std@@HH@Z
```

But the calling conventions are incompatible:

| | MSVC | clang-cl |
|---|---|---|
| RCX | First integer argument | Hidden return pointer |
| RDX | Second integer argument | First integer argument |
| R8 | Unused | Second integer argument |
| Return | AL | Memory pointed to by RCX |

This is the key problem: the linker cannot detect the ABI mismatch because the decorated names are identical.

## Suspected root cause

Microsoft STL PR [#5911](https://github.com/microsoft/STL/pull/5911) introduced deleted constructor templates in the three ordering types:

```cpp
template <int>
constexpr strong_ordering() = delete;
```

The change was intended to preserve ABI compatibility. MSVC continues returning `std::strong_ordering` in a register, as it did with the previous STL headers (14.44).

However, Clang's `isTrivialForMSVC` implementation in [MicrosoftCXXABI.cpp](https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/MicrosoftCXXABI.cpp) contains this check:

```cpp
else if (auto *Template = dyn_cast<FunctionTemplateDecl>(D)) {
    if (isa<CXXConstructorDecl>(Template->getTemplatedDecl()))
        return false;
}
```

As I understand it, Clang treats any constructor template as making the class ineligible for direct register return under the Microsoft ABI, including a deleted constructor template that cannot be invoked.

MSVC apparently does not apply that rule to this particular construct.

Consequently, the same STL type is classified differently by the two compilers.

## Real-world failure

I originally encountered this in a C++ application using Drogon 1.9.13, compiled with MSVC through vcpkg, while the application itself was compiled with clang-cl.

Initializing an ordinary `std::set<std::string>` triggered an `invalid comparator` assertion inside `std::_Debug_lt_pred`.

During debugging, I observed comparisons such as:

```text
"html" < "js"  -> false
```

More importantly, a comparison that started with:

```text
left  = "html"
right = "js"
```

eventually reached `std::string::compare()` with:

```text
this  = "js"
right = "js"
```

Comparing linker maps and stepping through the generated code exposed the ABI mismatch.

With `lld-link`, the selected `std::less<std::string>::operator()` implementation came from MSVC-compiled Drogon code using the C++20 comparison path. It called `std::string::operator<=>`, whose selected definition came from clang-cl-compiled application code.

The MSVC caller passed the two string references in `RCX` and `RDX`. Clang interpreted `RCX` as hidden return storage, shifted the arguments, and wrote the result into memory belonging to the first string object.

This caused memory corruption and eventually the comparator assertion.

Interestingly, the same executable worked when linked with Microsoft's `link.exe`. The linker maps revealed that it selected a different COMDAT implementation from a C++17 dependency, avoiding the `operator<=>` path.

**This is not an LLD bug, nor is switching to `link.exe` a reliable workaround.** The ABI incompatibility remains and can be exposed by other calls returning these comparison category types.

## Expected behavior

clang-cl should use the same return convention as MSVC for these types when targeting the Microsoft ABI with the same STL headers.

In particular, the deleted constructor templates introduced in STL #5911 should not cause a disagreement about register versus indirect return.

## Possible fix

Would it be appropriate to adjust `isTrivialForMSVC` so that deleted constructor templates do not automatically disqualify a type from register return, provided this matches MSVC's actual ABI rules?

I realize the rule may need to be more nuanced than simply ignoring all deleted constructors.

A regression test comparing MSVC and clang-cl return conventions for these three ordering types would also be useful.

The immediate concern is that this is a silent binary compatibility problem rather than a compilation or linkage error. It can affect both Debug and Release builds.

The Microsoft STL issue linked above contains the full investigation, including the original application failure, COMDAT analysis, and additional details.

I'm happy to provide the linker maps or further assembly output if needed.

Thanks for looking into this.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to