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 MSVC invalid parameter handler #14313

Merged

Conversation

HertzDevil
Copy link
Contributor

On Windows, if you call one of the C runtime functions that triggers an invalid argument error, by default the process will exit immediately with the code 0xC0000409 (see #13235 for an example where this happens). This PR adds a handler for such errors:

LibC._close(-1)
CRT invalid parameter handler invoked: (no message)
  from minkernel\crts\ucrt\src\appcrt\misc\invalid_parameter.cpp:114 in '_invalid_parameter_internal'
  from minkernel\crts\ucrt\src\appcrt\lowio\close.cpp:71 in '_close_internal'
  from minkernel\crts\ucrt\src\appcrt\lowio\close.cpp:76 in '_close'
  from usr\test.cr:1 in '__crystal_main'
  from src\crystal\main.cr:129 in 'main_user_code'
  from src\crystal\main.cr:115 in 'main'
  from src\crystal\main.cr:141 in 'main'
  from src\crystal\system\win32\wmain.cr:37 in 'wmain'
  from D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288 in '__scrt_common_main_seh'
  from C:\WINDOWS\System32\KERNEL32.DLL +75133 in 'BaseThreadInitThunk'
  from C:\WINDOWS\SYSTEM32\ntdll.dll +371288 in 'RtlUserThreadStart'

The error message is unavailable unless the debug version of the C runtime is used: (#14312)

# src/lib_c.cr
@[Link("libucrtd")]
lib LibC
  # ...
end
CRT invalid parameter handler invoked: (fh >= 0 && (unsigned)fh < (unsigned)_nhandle)
  from minkernel\crts\ucrt\src\appcrt\misc\invalid_parameter.cpp:110 in '_invalid_parameter_internal'
  from minkernel\crts\ucrt\src\appcrt\lowio\close.cpp:55 in '_close_internal'
  from minkernel\crts\ucrt\src\appcrt\lowio\close.cpp:76 in '_close'
  from usr\test.cr:1 in '__crystal_main'
...

@straight-shoota straight-shoota added this to the 1.12.0 milestone Feb 20, 2024
@straight-shoota straight-shoota merged commit 677cab3 into crystal-lang:master Feb 22, 2024
57 checks passed
@HertzDevil HertzDevil deleted the feature/msvc-invalid-parameter branch February 22, 2024 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants