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

Disable stack checks by default #2404

Merged
merged 18 commits into from
May 9, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Stack computations
  • Loading branch information
mshinwell committed May 9, 2024
commit 47a11f66209e23aee967c72e20572ebbb4019069
33 changes: 28 additions & 5 deletions ocaml/runtime/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,29 @@ Caml_inline struct stack_info* alloc_for_stack (mlsize_t wosize)
size_t bsize = Bsize_wsize(wosize);
int page_size = getpagesize();
int num_pages = (bsize + page_size - 1) / page_size;
bsize = (num_pages + 2) * page_size;
size_t len = sizeof(struct stack_info) +
bsize +
8 /* for alignment to 16-bytes, needed for arm64 */ +
sizeof(struct stack_handler);

// If we were using this for arm64, another 8 bytes is needed before
// the struct stack_handler.
CAML_STATIC_ASSERT(sizeof(struct stack_info) + 8 + sizeof(struct stack_handler)
< page_size);
// We need two clear pages in order to be able to guarantee we can create
// a guard page which is page-aligned.
len = (num_pages + 3) * page_size;

// Stack layout (higher addresses are at the top):
//
// --------------------
// struct stack_handler
// 8 bytes on arm64
// --------------------
// the stack itself
// -------------------- <- page-aligned
// guard page
// -------------------- <- page-aligned
// ... (for alignment)
// struct stack_info
// -------------------- <- block, possibly unaligned

struct stack_info* block;
block = mmap(NULL, len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
Expand All @@ -174,6 +192,11 @@ Caml_inline struct stack_info* alloc_for_stack (mlsize_t wosize)
munmap(block, len);
return NULL;
}

// Assert that the guard page does not impinge on the actual stack area.
CAMLassert(block + len - (sizeof(struct stack_handler) + 8 + bsize)
>= Protected_stack_page(block, page_size) + page_size);

block->size = len;
return block;
#else
Expand Down
Loading