From c92a11576ae387c3f7d6ecdedc51c4da49028a01 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 23 May 2025 17:37:43 +0800 Subject: [PATCH 1/2] Simplify `Crystal::System::Fiber::RESERVED_STACK_SIZE` initializer on Windows --- src/crystal/system/win32/fiber.cr | 2 +- src/crystal/system/win32/signal.cr | 2 +- src/crystal/system/win32/thread.cr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crystal/system/win32/fiber.cr b/src/crystal/system/win32/fiber.cr index 05fd230a9cac..01b87204f12b 100644 --- a/src/crystal/system/win32/fiber.cr +++ b/src/crystal/system/win32/fiber.cr @@ -5,7 +5,7 @@ require "c/winnt" module Crystal::System::Fiber # stack size in bytes needed for last-minute error handling in case of a stack # overflow - RESERVED_STACK_SIZE = LibC::DWORD.new(0x10000) + RESERVED_STACK_SIZE = 0x10000 def self.allocate_stack(stack_size, protect) : Void* if stack_top = LibC.VirtualAlloc(nil, stack_size, LibC::MEM_RESERVE, LibC::PAGE_READWRITE) diff --git a/src/crystal/system/win32/signal.cr b/src/crystal/system/win32/signal.cr index 4cebe7cf9c6a..64b92701b429 100644 --- a/src/crystal/system/win32/signal.cr +++ b/src/crystal/system/win32/signal.cr @@ -47,7 +47,7 @@ module Crystal::System::Signal # ensure that even in the case of stack overflow there is enough reserved # stack space for recovery (for other threads this is done in # `Crystal::System::Thread.thread_proc`) - stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE + stack_size = LibC::DWORD.new(Crystal::System::Fiber::RESERVED_STACK_SIZE) LibC.SetThreadStackGuarantee(pointerof(stack_size)) # this catches invalid argument checks inside the C runtime library diff --git a/src/crystal/system/win32/thread.cr b/src/crystal/system/win32/thread.cr index 2ff7ca438d87..cc488356c27c 100644 --- a/src/crystal/system/win32/thread.cr +++ b/src/crystal/system/win32/thread.cr @@ -34,7 +34,7 @@ module Crystal::System::Thread # ensure that even in the case of stack overflow there is enough reserved # stack space for recovery (for the main thread this is done in # `Exception::CallStack.setup_crash_handler`) - stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE + stack_size = LibC::DWORD.new(Crystal::System::Fiber::RESERVED_STACK_SIZE) LibC.SetThreadStackGuarantee(pointerof(stack_size)) data.as(::Thread).start From 1585fb6b9e4625a09d72385fca02be72216cebdc Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 23 May 2025 18:13:43 +0800 Subject: [PATCH 2/2] fixup --- src/crystal/system/win32/fiber.cr | 3 ++- src/crystal/system/win32/signal.cr | 2 +- src/crystal/system/win32/thread.cr | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/crystal/system/win32/fiber.cr b/src/crystal/system/win32/fiber.cr index 01b87204f12b..3aae5bfc5ac9 100644 --- a/src/crystal/system/win32/fiber.cr +++ b/src/crystal/system/win32/fiber.cr @@ -5,7 +5,8 @@ require "c/winnt" module Crystal::System::Fiber # stack size in bytes needed for last-minute error handling in case of a stack # overflow - RESERVED_STACK_SIZE = 0x10000 + # FIXME: use `LibC::DWORD` explicitly (#15820) + RESERVED_STACK_SIZE = 0x10000_u32 def self.allocate_stack(stack_size, protect) : Void* if stack_top = LibC.VirtualAlloc(nil, stack_size, LibC::MEM_RESERVE, LibC::PAGE_READWRITE) diff --git a/src/crystal/system/win32/signal.cr b/src/crystal/system/win32/signal.cr index 64b92701b429..4cebe7cf9c6a 100644 --- a/src/crystal/system/win32/signal.cr +++ b/src/crystal/system/win32/signal.cr @@ -47,7 +47,7 @@ module Crystal::System::Signal # ensure that even in the case of stack overflow there is enough reserved # stack space for recovery (for other threads this is done in # `Crystal::System::Thread.thread_proc`) - stack_size = LibC::DWORD.new(Crystal::System::Fiber::RESERVED_STACK_SIZE) + stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE LibC.SetThreadStackGuarantee(pointerof(stack_size)) # this catches invalid argument checks inside the C runtime library diff --git a/src/crystal/system/win32/thread.cr b/src/crystal/system/win32/thread.cr index cc488356c27c..2ff7ca438d87 100644 --- a/src/crystal/system/win32/thread.cr +++ b/src/crystal/system/win32/thread.cr @@ -34,7 +34,7 @@ module Crystal::System::Thread # ensure that even in the case of stack overflow there is enough reserved # stack space for recovery (for the main thread this is done in # `Exception::CallStack.setup_crash_handler`) - stack_size = LibC::DWORD.new(Crystal::System::Fiber::RESERVED_STACK_SIZE) + stack_size = Crystal::System::Fiber::RESERVED_STACK_SIZE LibC.SetThreadStackGuarantee(pointerof(stack_size)) data.as(::Thread).start