Skip to content

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'
...

@HertzDevil HertzDevil added kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:runtime labels Feb 20, 2024
@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
@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

Labels

kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:runtime

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants