cobalt icon indicating copy to clipboard operation
cobalt copied to clipboard

Access violation when returing object with specific layout on MSVC

Open Lyulf opened this issue 1 year ago • 16 comments

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.

Lyulf avatar Apr 17 '24 14:04 Lyulf

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 avatar Apr 18 '24 07:04 Pele44

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.

Pele44 avatar Apr 26 '24 07:04 Pele44

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.

klemens-morgenstern avatar Apr 26 '24 15:04 klemens-morgenstern

Thanks for the answer!

Pele44 avatar Apr 26 '24 16:04 Pele44

It this related to this MSVC bug?

fgoujeon avatar Apr 27 '24 10:04 fgoujeon

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.

klemens-morgenstern avatar Apr 28 '24 02:04 klemens-morgenstern

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.

Lyulf avatar Apr 30 '24 09:04 Lyulf

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

Pele44 avatar Jul 12 '24 04:07 Pele44

Spent a lot of time converting a lot of our async callbacks to coroutines, only to find out this happens in release.

Any way this can be looked at?

EDIT: For anyone with this issue, the BOOST_COBALT_NO_PMR preprocessor definition saves the day.

Mishura4 avatar Jan 24 '25 19:01 Mishura4

Which boost version did you build with?

klemens-morgenstern avatar Jan 26 '25 13:01 klemens-morgenstern

Boost is 1.87, MSVC is latest non-preview version (14.42.34433)

Mishura4 avatar Jan 27 '25 17:01 Mishura4