[GH-ISSUE #879] [shadPS4][MSYS2] Segfault on startup #247

Closed
opened 2026-02-27 21:05:21 +03:00 by kerem · 2 comments
Owner

Originally created by @nnn27 on GitHub (Sep 12, 2024).
Original GitHub issue: https://github.com/shadps4-emu/shadPS4/issues/879

Environment:

NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ uname -a
MINGW64_NT-10.0-19045 DESKTOP-32G4P3I 3.5.4-0bc1222b.x86_64 2024-09-04 18:28 UTC x86_64 Msys
NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ clang --version
clang version 18.1.8
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: E:/msys64/mingw64/bin
NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ cmake --version
cmake version 3.30.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).
NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ ninja --version
1.12.1

Description:
Emulator crashes on an early init stage before any logs can be collected. It seems to be caused by a dependency between globals - g_trophy_ui calls AddLayer in its constructor causing an operation on change_layers which is also a global. Afaik this is undefined behavior as there is no predefined order for global variable initialization. Wrapping change_layers as a static function variable seems to fix the issue. Below you can find the backtrace and my quick workaround.

Backtrace:

Starting program: E:\msys64\home\NNN\projects\shadPS4\build\shadps4.exe
[New Thread 32412.0x7c30]
[New Thread 32412.0x70e0]
[New Thread 32412.0x632c]
[New Thread 32412.0x6b80]
[New Thread 32412.0x7244]

Thread 1 received signal SIGSEGV, Segmentation fault.
0x0000000140a0ada1 in _ZNSt4pairIbPN5ImGui5LayerEEC2IbRS2_Qaacl16_S_constructibleITL0__TL0_0_EEntcl10_S_danglesIS6_S7_EEEEOT_OT0_ (this=0x0, __x=@0x5ffd97: true, __y=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/mingw64/include/c++/14.2.0/bits/stl_pair.h:444
444             : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
(gdb) bt
#0  0x0000000140a0ada1 in _ZNSt4pairIbPN5ImGui5LayerEEC2IbRS2_Qaacl16_S_constructibleITL0__TL0_0_EEntcl10_S_danglesIS6_S7_EEEEOT_OT0_ (this=0x0, __x=@0x5ffd97: true, __y=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/mingw64/include/c++/14.2.0/bits/stl_pair.h:444
#1  0x0000000140a7fc9c in std::construct_at<std::pair<bool, ImGui::Layer*>, bool, ImGui::Layer*&> (__location=0x0,
    __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>,
    __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/mingw64/include/c++/14.2.0/bits/stl_construct.h:97
#2  0x0000000140a1586d in std::allocator_traits<std::allocator<std::pair<bool, ImGui::Layer*> > >::construct<std::pair<bool, ImGui::Layer*>, bool, ImGui::Layer*&> (__a=..., __p=0x0,
    __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>,
    __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/mingw64/include/c++/14.2.0/bits/alloc_traits.h:536
#3  std::deque<std::pair<bool, ImGui::Layer*>, std::allocator<std::pair<bool, ImGui::Layer*> > >::emplace_back<bool, ImGui::Layer*&> (this=0x1417af048 <change_layers>, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>,
    __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/mingw64/include/c++/14.2.0/bits/deque.tcc:170
#4  0x0000000140007855 in ImGui::Layer::AddLayer (layer=0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/home/NNN/projects/shadPS4/src/imgui/renderer/imgui_core.cpp:187
#5  0x000000014026a96f in Libraries::NpTrophy::TrophyUI::TrophyUI (this=0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>)
    at E:/msys64/home/NNN/projects/shadPS4/src/core/libraries/np_trophy/trophy_ui.cpp:14
#6  0x000000014025b110 in __cxx_global_var_init ()
    at E:/msys64/home/NNN/projects/shadPS4/src/core/libraries/np_trophy/np_trophy.cpp:17
#7  0x000000014026a909 in _GLOBAL__sub_I_np_trophy.cpp ()
#8  0x0000000140779492 in __do_global_ctors () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:44
#9  0x00000001407794ff in __main () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:58
#10 0x00000001400012c8 in __tmainCRTStartup () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:252
#11 0x00000001400013f6 in mainCRTStartup () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:179

Workaround:

-static std::deque<std::pair<bool, ImGui::Layer*>> change_layers;
+std::deque<std::pair<bool, ImGui::Layer*>>& ChangeLayers()
+{
+       static std::deque<std::pair<bool, ImGui::Layer*>> change_layers;
+       return change_layers;
+}
+
Originally created by @nnn27 on GitHub (Sep 12, 2024). Original GitHub issue: https://github.com/shadps4-emu/shadPS4/issues/879 Environment: ``` NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ uname -a MINGW64_NT-10.0-19045 DESKTOP-32G4P3I 3.5.4-0bc1222b.x86_64 2024-09-04 18:28 UTC x86_64 Msys NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ clang --version clang version 18.1.8 Target: x86_64-w64-windows-gnu Thread model: posix InstalledDir: E:/msys64/mingw64/bin NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ cmake --version cmake version 3.30.3 CMake suite maintained and supported by Kitware (kitware.com/cmake). NNN@DESKTOP-32G4P3I:~/projects/shadPS4$ ninja --version 1.12.1 ``` Description: Emulator crashes on an early init stage before any logs can be collected. It seems to be caused by a dependency between globals - **g_trophy_ui** calls **AddLayer** in its constructor causing an operation on **change_layers** which is also a global. Afaik this is undefined behavior as there is no predefined order for global variable initialization. Wrapping **change_layers** as a static function variable seems to fix the issue. Below you can find the backtrace and my quick workaround. Backtrace: ``` Starting program: E:\msys64\home\NNN\projects\shadPS4\build\shadps4.exe [New Thread 32412.0x7c30] [New Thread 32412.0x70e0] [New Thread 32412.0x632c] [New Thread 32412.0x6b80] [New Thread 32412.0x7244] Thread 1 received signal SIGSEGV, Segmentation fault. 0x0000000140a0ada1 in _ZNSt4pairIbPN5ImGui5LayerEEC2IbRS2_Qaacl16_S_constructibleITL0__TL0_0_EEntcl10_S_danglesIS6_S7_EEEEOT_OT0_ (this=0x0, __x=@0x5ffd97: true, __y=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/mingw64/include/c++/14.2.0/bits/stl_pair.h:444 444 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) (gdb) bt #0 0x0000000140a0ada1 in _ZNSt4pairIbPN5ImGui5LayerEEC2IbRS2_Qaacl16_S_constructibleITL0__TL0_0_EEntcl10_S_danglesIS6_S7_EEEEOT_OT0_ (this=0x0, __x=@0x5ffd97: true, __y=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/mingw64/include/c++/14.2.0/bits/stl_pair.h:444 #1 0x0000000140a7fc9c in std::construct_at<std::pair<bool, ImGui::Layer*>, bool, ImGui::Layer*&> (__location=0x0, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/mingw64/include/c++/14.2.0/bits/stl_construct.h:97 #2 0x0000000140a1586d in std::allocator_traits<std::allocator<std::pair<bool, ImGui::Layer*> > >::construct<std::pair<bool, ImGui::Layer*>, bool, ImGui::Layer*&> (__a=..., __p=0x0, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/mingw64/include/c++/14.2.0/bits/alloc_traits.h:536 #3 std::deque<std::pair<bool, ImGui::Layer*>, std::allocator<std::pair<bool, ImGui::Layer*> > >::emplace_back<bool, ImGui::Layer*&> (this=0x1417af048 <change_layers>, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>, __args=@0x5ffda0: 0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/mingw64/include/c++/14.2.0/bits/deque.tcc:170 #4 0x0000000140007855 in ImGui::Layer::AddLayer (layer=0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/home/NNN/projects/shadPS4/src/imgui/renderer/imgui_core.cpp:187 #5 0x000000014026a96f in Libraries::NpTrophy::TrophyUI::TrophyUI (this=0x1417b1818 <Libraries::NpTrophy::g_trophy_ui>) at E:/msys64/home/NNN/projects/shadPS4/src/core/libraries/np_trophy/trophy_ui.cpp:14 #6 0x000000014025b110 in __cxx_global_var_init () at E:/msys64/home/NNN/projects/shadPS4/src/core/libraries/np_trophy/np_trophy.cpp:17 #7 0x000000014026a909 in _GLOBAL__sub_I_np_trophy.cpp () #8 0x0000000140779492 in __do_global_ctors () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:44 #9 0x00000001407794ff in __main () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:58 #10 0x00000001400012c8 in __tmainCRTStartup () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:252 #11 0x00000001400013f6 in mainCRTStartup () at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:179 ``` Workaround: ``` -static std::deque<std::pair<bool, ImGui::Layer*>> change_layers; +std::deque<std::pair<bool, ImGui::Layer*>>& ChangeLayers() +{ + static std::deque<std::pair<bool, ImGui::Layer*>> change_layers; + return change_layers; +} + ```
kerem closed this issue 2026-02-27 21:05:21 +03:00
Author
Owner

@abouvier commented on GitHub (Sep 13, 2024):

Same error on Arch Linux, you should propose a pull request ;)

<!-- gh-comment-id:2350693079 --> @abouvier commented on GitHub (Sep 13, 2024): Same error on Arch Linux, you should propose a pull request ;)
Author
Owner

@nnn27 commented on GitHub (Sep 14, 2024):

Fixed in #907

<!-- gh-comment-id:2350988233 --> @nnn27 commented on GitHub (Sep 14, 2024): Fixed in #907
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shadPS4#247
No description provided.