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
Show file tree
Hide file tree
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
Don't assume mmap returns a page-aligned block.
  • Loading branch information
xclerc committed May 8, 2024
commit 110591d8a9d49bdc754727a348fd373074a1bffe
2 changes: 1 addition & 1 deletion ocaml/runtime/backtrace_nat.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void caml_stash_backtrace_wrapper(value exn, char* rsp, char* trapsp) {
* exception from a segfault... */
struct stack_info *block = Caml_state->current_stack;
int page_size = getpagesize();
char* protected_low = (char *) block + page_size;
char* protected_low = Protected_stack_page(block, page_size);
char* protected_high = protected_low + page_size;
if ((rsp >= protected_low) && (rsp < protected_high)) {
return;
Expand Down
9 changes: 9 additions & 0 deletions ocaml/runtime/caml/fiber.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ CAMLnoreturn_end;

value caml_make_unhandled_effect_exn (value effect);

#if defined(NATIVE_CODE) && !defined(STACK_CHECKS_ENABLED)
// mmap does not seem to guarantee it will always return a page-aligned
// address as per some man pages from a few years ago (more recent man
// pages seem to indicate the alignment is guaranteed), hence the last
// part of the expression below to add an offset guaranteeing alignment
#define Protected_stack_page(block, page_size) \
(((char*) (block)) + (page_size) + (page_size) - ((uintnat) ((char*) (block)) % (page_size)))
#endif

#endif /* CAML_INTERNALS */

#endif /* CAML_FIBER_H */
4 changes: 2 additions & 2 deletions ocaml/runtime/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Caml_inline struct stack_info* alloc_for_stack (mlsize_t wosize)
munmap(block, len);
return NULL;
}
if (mprotect((char *) block + page_size, page_size, PROT_NONE)) {
if (mprotect(Protected_stack_page(block, page_size), page_size, PROT_NONE)) {
munmap(block, len);
return NULL;
}
Expand Down Expand Up @@ -884,7 +884,7 @@ void caml_free_stack (struct stack_info* stack)
#ifndef USE_MMAP_MAP_STACK
#if defined(NATIVE_CODE) && !defined(STACK_CHECKS_ENABLED)
int page_size = getpagesize();
mprotect((void *) ((char *) stack + page_size),
mprotect((void *) Protected_stack_page(stack, page_size),
page_size,
PROT_READ | PROT_WRITE);
#endif
Expand Down
2 changes: 1 addition & 1 deletion ocaml/runtime/signals_nat.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ DECLARE_SIGNAL_HANDLER(segv_handler)
struct stack_info *block = Caml_state->current_stack;
char* fault_addr = info->si_addr;
int page_size = getpagesize();
char* protected_low = (char *) block + page_size;
char* protected_low = Protected_stack_page(block, page_size);
char* protected_high = protected_low + page_size;
if ((fault_addr >= protected_low) && (fault_addr < protected_high)) {
context->uc_mcontext.gregs[REG_RIP]= (greg_t) &caml_raise_stack_overflow_nat;
Expand Down
Loading