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

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Feb 7, 2025

This has been puzzling me for a while, and working on #15409 I finally took the time to investigate.

The shadow space (or home space) is a requirement of the x64 call convention: we must reserve 32 bytes before the return address on the stack. It only applies to x64, not to arm64 for example.

We don't have to deal with this when creating the stack. This is a consideration for each individual makecontext to handle.

Explanation:

The old version for win32:

  1. aligned the ptr to 16 bytes
  2. decremented the ptr by 6 pointer-size (8 bytes): 1 to move it into the stack area (8 bytes), 4 for the shadow space (32 bytes), and 1 last to keep the 16-bytes alignment (another 8 bytes);
  3. aligned the ptr to 16 bytes (not needed).

The new version (actually the old non-win32 version):

  1. decrements the ptr by 1 pointer-size to move it back into the stack area;
  2. aligns the ptr to 16 bytes.

In both versions stack_ptr point to the last first (the stack grows down) addressable Void* in the stack and is aligned to 16 bytes.

Then the new x86_64-microsoft variant of makecontext reserves 32 bytes of shadow space (still aligned to 16 bytes) and the rest of makecontext is still aligned to 16 bytes.

References:

The shadow space (or home space) is a requirement of the x64 call
convention: we must reserve 32 bytes before the return address on the
stack. It only applies to x64, not to arm64 for example.

We don't have to deal with this when creating the stack. This is a
consideration for each individual `makecontext` to handle.
@straight-shoota straight-shoota added this to the 1.16.0 milestone Feb 9, 2025
@straight-shoota straight-shoota merged commit 4f6b61e into crystal-lang:master Feb 10, 2025
73 checks passed
@ysbaddaden ysbaddaden deleted the refactor/fiber-win32-stack-makecontext branch February 10, 2025 11:04
ysbaddaden added a commit to ysbaddaden/crystal that referenced this pull request Feb 10, 2025
@ysbaddaden ysbaddaden mentioned this pull request Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants