Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crash handler on Windows #11570

Merged

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Dec 11, 2021

Catches access violations and stack overflows using a vectored exception handler. This does not require a C shim.

Since the handler runs on the current thread without stack unwinding, we cannot print the backtrace on the same thread in the case of stack overflow. This PR creates a new thread directly through Win32, suspends the current thread, prints the backtrace on the new one, and then immediately terminates. This is probably fine since the program is dying anyway.

Windows structured exceptions share some aspects with POSIX signals. This PR does not attempt to reconcile the two concepts in the standard library (see also #7339); it only picks these particular hardware exceptions because they are equivalent to SIGSEGV and provide useful feedback when dealing with unsafe code. Note that the two exceptions have distinct error codes, so things like #7482 shouldn't happen on Windows.

Example output:

> type test.cr
def foo(count)
  if count == 0
    x = Pointer(Int32).new(0x1234)
    x.value = 1
  else
    foo(count - 1)
  end
end
foo(ARGV.shift.to_i)

> crystal test.cr 10
Invalid memory access (C0000005) at address 0x1234
[0x7ff6cbc80f8a] foo at C:\Users\Quinton\crystal-win\test.cr:3
[0x7ff6cbc80fc5] foo at C:\Users\Quinton\crystal-win\test.cr:6 (10 times)
[0x7ff6cbc71397] __crystal_main at C:\Users\Quinton\crystal-win\test.cr:10
[0x7ff6cbd0c189] main_user_code at C:\Users\Quinton\crystal-win\src\crystal\main.cr:110
[0x7ff6cbd0bfd8] main at C:\Users\Quinton\crystal-win\src\crystal\main.cr:96
[0x7ff6cbc7fc19] main at C:\Users\Quinton\crystal-win\src\crystal\main.cr:119
[0x7ff6cbd3aa14] __scrt_common_main_seh at d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
[0x7fffff8a7034] BaseThreadInitThunk +20 in C:\WINDOWS\System32\KERNEL32.DLL
[0x7ff800cc2651] RtlUserThreadStart +33 in C:\WINDOWS\SYSTEM32\ntdll.dll

> crystal test.cr 10000000
Stack overflow (e.g., infinite or very deep recursion)
[0x7ff79db90fc0] foo at C:\Users\Quinton\crystal-win\test.cr:6
[0x7ff79db90fc5] foo at C:\Users\Quinton\crystal-win\test.cr:6 (16087 times)
[0x7ff79db81397] __crystal_main at C:\Users\Quinton\crystal-win\test.cr:10
...

@HertzDevil HertzDevil added kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:system topic:stdlib:runtime and removed topic:stdlib:system labels Dec 11, 2021
@HertzDevil
Copy link
Contributor Author

HertzDevil commented Dec 11, 2021

Weird, the stack overflow spec works on my machine with the artifact... (the same spec even works on my fork's CI)

@oprypin oprypin removed their request for review December 22, 2021 16:35
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Thanks! 🙇

@beta-ziliani beta-ziliani added this to the 1.4.0 milestone Jan 28, 2022
@straight-shoota straight-shoota merged commit 966b3a5 into crystal-lang:master Jan 31, 2022
@HertzDevil HertzDevil deleted the feature/windows-crash-handler branch February 1, 2022 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants