dethrace-labs / dethrace

Reverse engineering the 1997 game "Carmageddon"
https://twitter.com/dethrace_labs
GNU General Public License v3.0
667 stars 38 forks source link

Fix Windows arm64 build #371

Closed madebr closed 1 month ago

madebr commented 2 months ago

I disabled the ci job, but when enabled it would look as follows: https://github.com/madebr/dethrace/actions/runs/9550571423

Also, it's completely untested

ArminiusTux commented 2 months ago

Thank you @madebr for this fix!

dethrace How about a neat icon for this game - maybe @Toshiba-3 or @Memorix101 could help?

madebr commented 2 months ago

Thank you @madebr for this fix! How about a neat icon for this game - maybe @Toshiba-3 or @Memorix101 could help?

Great to hear! The arm64 incompatibility was in the error reporting code, which you hopefully never see. You'd need to purposefully add a crashing bug to the game to verify my fixes are correct. If they are working, the code should print a stacktrace of the crash location.

@dethrace-labs Perhaps we should use dbghelp instead: CaptureStackBackTrace, Syminitialize, SymFromAddr and SymGetLineFromAddr are way simpler and not architecture dependent. They are used in the SDL tests to track memory allocations (and detect leaks). (Or perhaps these are not useful)

dethrace-labs commented 2 months ago

@madebr agreed - when I had a quick look on this issue I thought CaptureStackBackTrace et all would be much simpler.

@ArminiusTux for the icon I’m working on building a proper app package for MacOS and as part of that will include the windows icon config

madebr commented 2 months ago

I looked into stacktrace generation past weekend, and I don't think CaptureStackBackTrace is useful here. That function captures the stack of the current thread stack where in an error state, we want to print the stack of another failing thread stack. CaptureStackBackTrace is useful for assert-like functions to show where the assertion failed, and how you got there.

I used these somewhat complicated functions to print stacktraces of crashed child processes on ci. https://github.com/libsdl-org/SDL/blob/main/test/win32/sdlprocdump.c (a few tests of SDL2/SDL3 very occasionally segfault on ci and nobody has been able to reproduce it)

madebr commented 2 months ago

My latest changes will also print a stacktrace-like on wine where dbghelp is not 100% supported, and when addr2line.exe does not exist:

Dethrace version: v0.7.1-13-g0ad96de2
Using root directory: \home\maarten\projects\dethrace\cmake-build-debug-mingw64\
Game mode: Carmageddon
Error: EXCEPTION_INT_DIVIDE_BY_ZERO
0: Z:\home\maarten\projects\dethrace\cmake-build-debug-mingw64\dethrace.exe 0x40126f20
1: Z:\home\maarten\projects\dethrace\cmake-build-debug-mingw64\dethrace.exe 0x400012ee
2: Z:\home\maarten\projects\dethrace\cmake-build-debug-mingw64\dethrace.exe 0x400013e6
3: C:\windows\system32\kernel32.dll 0xffa88c79
4: C:\windows\system32\ntdll.dll 0xffc86e8f

This is how it looks like on Windows with MSVC:

Error: EXCEPTION_INT_DIVIDE_BY_ZERO
0: dethrace!main+0x151 C:\Users\maarten\source\repos\dethrace\src\DETHRACE\main.c Line 48
1: dethrace!invoke_main+0x39 D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl Line 79
2: dethrace!__scrt_common_main_seh+0x12e D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl Line 288
3: dethrace!__scrt_common_main+0xe D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl Line 331
4: dethrace!mainCRTStartup+0xe D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp Line 17
5: C:\WINDOWS\System32\KERNEL32.DLL 0x6ba77344
6: C:\WINDOWS\SYSTEM32\ntdll.dll 0x6ce7cc91

This is how it looks like with MingW on Windows with no addr2line:

Error: EXCEPTION_INT_DIVIDE_BY_ZERO
0: C:\Users\maarten\source\repos\dethrace\build\Debug-mingw\dethrace.exe 0x8629580a
1: C:\Users\maarten\source\repos\dethrace\build\Debug-mingw\dethrace.exe 0x8617143f
2: C:\Users\maarten\source\repos\dethrace\build\Debug-mingw\dethrace.exe 0x86171117
3: C:\WINDOWS\System32\KERNEL32.DLL 0x6ba77344
4: C:\WINDOWS\SYSTEM32\ntdll.dll 0x6ce7cc91

I kept addr2line support, but it will only be used when the environment variable ADDR2LINE is set (and an addr2line executable actually exists). It always prints the following on my Windows system:

Error: EXCEPTION_INT_DIVIDE_BY_ZERO
0: ?? ??:0
1: ?? ??:0
2: ?? ??:0
3: ?? ??:0
4: ?? ??:0

edit: The last commit also prints the address, and changes the MSVC output to

0: 0x9dcd40b1 dethrace!main+0x151 C:\Users\maarten\source\repos\dethrace\src\DETHRACE\main.c Line 48
1: 0x9debf9a9 dethrace!invoke_main+0x39 D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl Line 79
2: 0x9debf84e dethrace!__scrt_common_main_seh+0x12e D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl Line 288
3: 0x9debf70e dethrace!__scrt_common_main+0xe D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl Line 331
4: 0x9debfa3e dethrace!mainCRTStartup+0xe D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp Line 17
5: 0x6ba77344 KERNEL32!BaseThreadInitThunk+0x14
6: 0x6ce7cc91 ntdll!RtlUserThreadStart+0x21