Issue 144708
Summary CLang MSVC compatibility bug in return by register.
Labels clang
Assignees
Reporter technotopia
    Call from MSVC compiled code to CLang compiled method can result in access violation.

Condition: returned object is OK to be returned by register according to MSVC, but CLang assumes it should return a pointer on stack. Condition is described here:

https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170

POD object that fits size limitation and has no user defined constructor/destructor et.c

However, there is a problem with return of a class that has _default_ constructor and destructor:

```
//clang
class inlines_float_2
{
public:
	inlines_float_2() = default;
	~inlines_float_2() = default;
	float val1;
	float val2;

	void set(float v1, float v2) { val1 = v1; val2 = v2; }
	bool less() const { return val1 < val2; }
	bool in(float v) const { return (val1 < v) && (val2 > v); }
};
```

```
//msvc
std::cout << "going to make 2 float struct with added default constructor/destructor. Should be bug:" << std::endl;
    auto if2 = factory::make_in_f2(dummy, v);
    std::cout << "got " << if2.val1 << ", " << if2.val2 << std::endl;

```

Disassembly of MSVC code, it expects return value in RAX:

<!-- Failed to upload "MSVC_CLang_rvalue_compatibility.zip" -->

Disassembly of CLang code, it expects arguments but tries to access uninitialized data.

> inlines_float_2 factory::make_in_f2(const std::vector<uint8_t>& dummy, const std::vector<float>& v)
> {
> 00007FFFF2DA1270  sub         rsp,48h  
> 00007FFFF2DA1274  mov qword ptr [rsp+20h],rcx  
> 00007FFFF2DA1279  mov         rax,rcx  
> 00007FFFF2DA127C  mov         qword ptr [rsp+28h],rax  
> 00007FFFF2DA1281 mov         qword ptr [rsp+40h],rcx  
> 00007FFFF2DA1286  mov         qword ptr [rsp+38h],r8  
> 00007FFFF2DA128B  mov         qword ptr [rsp+30h],rdx

---

Build options for MSVC project: 

/JMC /permissive- /ifcOutput "x64\Debug\" /GS /W3 /Gy /Zc:wchar_t /I"C:\dev\MSVC_CLang_rvalue_compatibility\" /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc143.pdb" /Zc:inline /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /arch:AVX2 /Gd /MDd /std:c++20 /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\MSVC_CLang_rvalue_compatibility.pch" /diagnostics:column 

Build options for CLang project:

/MP /GS /W3 /Gy /Zi /Od /D "_DEBUG" /D "CLANGLIB_EXPORTS" /D "_WINDOWS" /D "_USRDLL" /D "DATA_EXPORTS" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /WX- /arch:AVX2 /Gd /MDd /std:c++20 /Fa"x64\Debug\" /EHa /nologo /Fo"x64\Debug\" /Fp"x64\Debug\clang_lib.pch" /diagnostics:column 

WinSDK: 10.0.19041.0

Built under Visual Studio Version 17.2.23

---

Bottom line: I'm not sure which compiler is wrong as having default constructor and destructor is not a clear case in standard - whether it counts as POD or not.
---

Attaching a simple solution that reproduces the crash

<!-- Failed to upload "MSVC_CLang_rvalue_compatibility.zip" -->
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to