Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

c++ - std::ifstream crashes in release build on Windows with exit code 0xC0000409: Unknown software exception

I'm reading a file using std::ifstream:

printf("Before stream initialization
");
ifstream stream(file_path, ios::binary);
printf("Stream initialized
");
ifstream::pos_type position = stream.tellg();
auto file_size = position;
printf("Position acquired
");

However, the program crashes in the release mode of the binary. Here is the compiled assembly code snippet:

.text:0000000000413411                 lea     rcx, aBeforeStreamIn ; "Before stream initialization
"
.text:0000000000413418                 mov     rbx, rax
.text:000000000041341B                 call    _ZL6printfPKcz  ; printf(char const*,...)
.text:000000000041341B ;   } // starts at 41340C
.text:0000000000413420                 lea     rdi, [rsp+878h+var_248]
.text:0000000000413428                 lea     rcx, [rdi+0D8h] ; this
.text:000000000041342F                 mov     [rsp+878h+var_820], rdi
.text:0000000000413434                 call    _ZNSt8ios_baseC1Ev ; std::ios_base::ios_base(void)
.text:0000000000413439                 xor     r8d, r8d
.text:000000000041343C                 mov     rax, cs:_refptr__ZTVSt9basic_iosIcSt11char_traitsIcEE
.text:0000000000413443                 xor     edx, edx
.text:0000000000413445                 mov     [rsp+878h+var_90], r8w
.text:000000000041344E                 pxor    xmm0, xmm0
.text:0000000000413452                 movaps  [rsp+878h+var_88], xmm0
.text:000000000041345A                 movaps  [rsp+878h+var_78], xmm0
.text:0000000000413462                 mov     [rsp+878h+var_98], 0
.text:000000000041346E                 add     rax, 10h
.text:0000000000413472                 mov     [rsp+878h+var_170], rax
.text:000000000041347A                 mov     rax, cs:_refptr__ZTTSt14basic_ifstreamIcSt11char_traitsIcEE
.text:0000000000413481                 mov     rsi, [rax+8]
.text:0000000000413485                 mov     rcx, [rax+10h]
.text:0000000000413489                 mov     rax, [rsi-18h]
.text:000000000041348D                 mov     [rsp+878h+var_248], rsi
.text:0000000000413495                 mov     [rsp+878h+var_7E8], rcx
.text:000000000041349D                 mov     [rsp+878h+var_7F0], rsi
.text:00000000004134A5                 mov     [rsp+rax+878h+var_248], rcx
.text:00000000004134AD                 mov     [rsp+878h+var_240], 0
.text:00000000004134B9                 mov     rcx, [rsi-18h]
.text:00000000004134BD                 add     rcx, rdi
.text:00000000004134C0 ;   try {
.text:00000000004134C0                 call    _ZNSt9basic_iosIcSt11char_traitsIcEE4initEPSt15basic_streambufIcS1_E ; std::basic_ios<char,std::char_traits<char>>::init(std::basic_streambuf<char,std::char_traits<char>> *)
.text:00000000004134C0 ;   } // starts at 4134C0
.text:00000000004134C5                 mov     rax, cs:_refptr__ZTVSt14basic_ifstreamIcSt11char_traitsIcEE
.text:00000000004134CC                 lea     rcx, [rdi+10h]
.text:00000000004134D0                 add     rax, 18h
.text:00000000004134D4                 mov     [rsp+878h+var_248], rax
.text:00000000004134DC                 mov     rax, cs:_refptr__ZTVSt14basic_ifstreamIcSt11char_traitsIcEE
.text:00000000004134E3                 add     rax, 40h
.text:00000000004134E7                 mov     [rsp+878h+var_170], rax
.text:00000000004134EF ;   try {
.text:00000000004134EF                 call    _ZNSt13basic_filebufIcSt11char_traitsIcEEC1Ev ; std::basic_filebuf<char,std::char_traits<char>>::basic_filebuf(void)
.text:00000000004134EF ;   } // starts at 4134EF
.text:00000000004134F4                 lea     rdx, [rdi+10h]
.text:00000000004134F8                 lea     rcx, [rdi+0D8h]
.text:00000000004134FF ;   try {
.text:00000000004134FF                 call    _ZNSt9basic_iosIcSt11char_traitsIcEE4initEPSt15basic_streambufIcS1_E ; std::basic_ios<char,std::char_traits<char>>::init(std::basic_streambuf<char,std::char_traits<char>> *)
.text:0000000000413504                 lea     rcx, [rdi+10h]
.text:0000000000413508                 mov     r8d, 0Eh
.text:000000000041350E                 mov     rdx, rbx
.text:0000000000413511                 call    _ZNSt13basic_filebufIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode ; std::basic_filebuf<char,std::char_traits<char>>::open(char const*,std::_Ios_Openmode)
.text:0000000000413516                 mov     rdx, [rsp+878h+var_248]
.text:000000000041351E                 add     rdi, [rdx-18h]
.text:0000000000413522                 test    rax, rax
.text:0000000000413525                 mov     rcx, rdi
.text:0000000000413528                 jz      loc_414688
.text:000000000041352E                 xor     edx, edx
.text:0000000000413530                 call    _ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate ; std::basic_ios<char,std::char_traits<char>>::clear(std::_Ios_Iostate)
.text:0000000000413530 ;   } // starts at 4134FF
.text:0000000000413535
.text:0000000000413535 loc_413535:                             ; CODE XREF: PointerSearcher::parse_pointer_map(void)+1363↓j
.text:0000000000413535                 lea     rcx, aStreamInitiali ; "Stream initialized
"
.text:000000000041353C ;   try {
.text:000000000041353C                 call    _ZL6printfPKcz  ; printf(char const*,...)

In my function it crashes at this line:

.text:0000000000413504                 lea     rcx, [rdi+10h]

The output is:

Before stream initialization

Process finished with exit code -1073741819 (0xC0000409)

The stacktrace is:

std::locale::operator=(std::locale const&)
std::ios_base::_M_init()
std::basic_ios<char, std::char_traits<char> >::init(std::basic_streambuf<char, std::char_traits<char> >*)
MyExecutable::myFunction()

The crash only happens in the Windows binary. The binary works in release mode for Linux. I'm using the MinGW compiler to compile the Windows binary and the compilation flags are:

-fopenmp -O3 -DNDEBUG

They're the default CMake release build flags. I also made sure the passed file_path is correct.

gdb says:

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00000000004a2521 in std::locale::operator=(std::locale const&) ()

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00000000004a2521 in std::locale::operator=(std::locale const&) ()
[Thread 48616.0xc508 exited with code 3221225477]
[Thread 48616.0xc510 exited with code 3221225477]
[Thread 48616.0xc638 exited with code 3221225477]
[Inferior 1 (process 48616) exited with code 030000000005]

The compiler version:

"C:Program Filesmingw-w64x86_64-8.1.0-win32-seh-rt_v6-rev0mingw64inx86_64-w64-mingw32-gcc.exe" --version
x86_64-w64-mingw32-gcc.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Does anyone have an idea what went wrong and how to fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This seems to be a MinGW compiler bug since when using MSVC in Visual Studio to compile the code, the same exception does not occur either.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...