boostorg / cobalt

Coroutines for C++20 & asio
https://www.boost.org/doc/libs/master/libs/cobalt/doc/html/index.html
207 stars 24 forks source link

Access violation when returing object with specific layout on MSVC #172

Open Lyulf opened 5 months ago

Lyulf commented 5 months ago

Code:

#include <cstdint>
#include <cstdio>
#include <string>

#include <boost/cobalt.hpp>

namespace cobalt = boost::cobalt;

struct ExampleStruct
{
  std::string str;
  std::uint8_t padding[32];
};

cobalt::task<ExampleStruct> getResult()
{
  // error on this return
  co_return {};
}

cobalt::main co_main(int /*argc*/, char** /*argv*/)
{
  co_await getResult();
  // Never displayed
  std::puts("Done");
  co_return 0;
}

Built on Windows 10 64-bit using MSVC 17.9.5 (cl version 19.39.33523 for x64) with following command:

cl /Femain.exe main.cpp /nologo /external:IC:\Users\chodorowskip\repo\deps\vc143\boost-1.85.0\include\boost-1_85 /std:c++20 /MD /Z7 /O2 /EHsc /D_WIN32_WINNT=0x0601 /W4 /external:W0 /link /PROFILE /LIBPATH:C:\Users\chodorowskip\repo\deps\vc143\boost-1.85.0\lib libboost_cobalt-vc143-mt-x64-1_85.lib

Error

Exception thrown at 0x00007FF728E265E1 in main.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Error doesn't seem to happen with debug configuration.

Pele44 commented 5 months ago

I've check this code on every combination of g++ 13.2 /clang++ 17.0.6, -O2 / -O0, no sanitizer / address sanitizer / thread sanitizer on Linux platform without any problems. In every case "Done" is printed and no sanitizer warning are reported. But on MSVC crash occurs (Windows 11), I can confirm it.

Pele44 commented 5 months ago

Is there a chance that the error is actually in cobalt? Or maybe it's better to report the case to Microsoft? The problem occurs in our project and unfortunately the current workaround is to stop using cobalt, which we would really like not to do, because it is supposed to be a very helpful library, making it easier to solve many of the problems we face. And the specificity of the project means that the code must run on packages generated using MSVC and gcc... We cannot give up the Microsoft compiler.

klemens-morgenstern commented 5 months ago

I had plenty of problems with MSVC and last time I checked they had issues with improper inlining (for which I added a workaround). I however don't like to say "it's their fault" as it might be mine. I didn't get around to checking the code yet - which is why I didn't respond, sorry.

Pele44 commented 5 months ago

Thanks for the answer!

fgoujeon commented 5 months ago

It this related to this MSVC bug?

klemens-morgenstern commented 5 months ago

It might be. I added commit increasing the align here.

I don't know when I get my windows builds working again, so if you get around to test it let me know.

Lyulf commented 5 months ago

I couldn't figure out how to build cobalt without bootstrap and b2, so I just applied the changes from the most recent commit from the branch you provided and they don't seem to work. Still got access violation.

Pele44 commented 2 months ago

Is there a chance that the mentioned bug/workaround for the MSVC bug will be fixed in the near future?