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

Move shadow space reservation to x86_64 makecontext #15434

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions src/fiber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,9 @@ class Fiber

fiber_main = ->(f : Fiber) { f.run }

# FIXME: This line shouldn't be necessary (#7975)
stack_ptr = nil
{% if flag?(:win32) %}
# align stack bottom to 16 bytes
@stack_bottom = Pointer(Void).new(@stack_bottom.address & ~0x0f_u64)
ysbaddaden marked this conversation as resolved.
Show resolved Hide resolved

# It's the caller's responsibility to allocate 32 bytes of "shadow space" on the stack right
# before calling the function (regardless of the actual number of parameters used)

stack_ptr = @stack_bottom - sizeof(Void*) * 6
{% else %}
# point to first addressable pointer on the stack (@stack_bottom points past
# the stack because the stack grows down):
stack_ptr = @stack_bottom - sizeof(Void*)
{% end %}
# point to first addressable pointer on the stack (@stack_bottom points past
# the stack because the stack grows down):
stack_ptr = @stack_bottom - sizeof(Void*)

# align the stack pointer to 16 bytes:
stack_ptr = Pointer(Void*).new(stack_ptr.address & ~0x0f_u64)
Expand Down
5 changes: 3 additions & 2 deletions src/fiber/context/x86_64-microsoft.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ class Fiber
# A great explanation on stack contexts for win32:
# https://web.archive.org/web/20220527113808/https://cfsamson.gitbook.io/green-threads-explained-in-200-lines-of-rust/supporting-windows

# 8 registers + 3 qwords for NT_TIB + 1 parameter + 10 128bit XMM registers
@context.stack_top = (stack_ptr - (12 + 10*2)).as(Void*)
# 4 shadow space + (8 registers + 3 qwords for NT_TIB + 1 parameter) + 10 128bit XMM registers
@context.stack_top = (stack_ptr - (4 + 12 + 10*2)).as(Void*)
@context.resumable = 1

# actual stack top, not including guard pages and reserved pages
LibC.GetNativeSystemInfo(out system_info)
stack_top = @stack_bottom - system_info.dwPageSize

stack_ptr -= 4 # shadow space (or home space) before return address
stack_ptr[0] = fiber_main.pointer # %rbx: Initial `resume` will `ret` to this address
stack_ptr[-1] = self.as(Void*) # %rcx: puts `self` as first argument for `fiber_main`

Expand Down