Skip to content

Commit

Permalink
Must allocate main stack *after* creation of the stack cache
Browse files Browse the repository at this point in the history
fix debug memory guard in caml_free_stack
  • Loading branch information
ctk21 committed Nov 19, 2020
1 parent 8ffb289 commit 832d4ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
17 changes: 9 additions & 8 deletions runtime/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,33 +274,34 @@ static void create_domain(uintnat initial_minor_heap_wsize) {
goto reallocate_minor_heap_failure;
}

domain_state->current_stack =
caml_alloc_main_stack(Stack_size / sizeof(value));
if(domain_state->current_stack == NULL) {
goto alloc_main_stack_failure;
}

domain_state->dls_root = caml_create_root_noexc(Val_unit);
if(domain_state->dls_root == NULL) {
goto create_root_failure;
}

domain_state->stack_cache = caml_alloc_stack_cache();
if(domain_state->stack_cache == NULL) {
goto create_stack_cache_failure;
}

domain_state->current_stack =
caml_alloc_main_stack(Stack_size / sizeof(value));
if(domain_state->current_stack == NULL) {
goto alloc_main_stack_failure;
}

domain_state->backtrace_buffer = NULL;
#ifndef NATIVE_CODE
domain_state->external_raise = NULL;
domain_state->trap_sp_off = 1;
#endif
goto domain_init_complete;

caml_free_stack(domain_state->current_stack);
alloc_main_stack_failure:
create_stack_cache_failure:
caml_delete_root(domain_state->dls_root);
create_root_failure:
caml_free_stack(domain_state->current_stack);
alloc_main_stack_failure:
reallocate_minor_heap_failure:
caml_teardown_major_gc();
init_major_gc_failure:
Expand Down
11 changes: 7 additions & 4 deletions runtime/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ static struct stack_info* alloc_size_class_stack_noexc(mlsize_t wosize, struct s

if (size_bucket != NULL && *size_bucket != NULL) {
stack = *size_bucket;
CAMLassert(stack->size_bucket == stack_cache_bucket(wosize));
*size_bucket = (struct stack_info*)stack->exception_ptr;
CAMLassert(stack->size_bucket == stack_cache_bucket(wosize));
hand = stack->handler;
} else {
/* couldn't get a cached stack, so have to create one */
Expand Down Expand Up @@ -418,13 +418,16 @@ void caml_free_stack (struct stack_info* stack)
{
CAMLnoalloc;
CAMLassert(stack->magic == 42);
#ifdef DEBUG
memset(stack, 0x42, (char*)stack->handler - (char*)stack);
#endif
if (stack->size_bucket != NULL) {
stack->exception_ptr = (void*)(*stack->size_bucket);
*stack->size_bucket = stack;
#ifdef DEBUG
memset(Stack_base(stack), 0x42, (Stack_high(stack)-Stack_base(stack))*sizeof(value));
#endif
} else {
#ifdef DEBUG
memset(stack, 0x42, (char*)stack->handler - (char*)stack);
#endif
caml_stat_free(stack);
}
}
Expand Down

0 comments on commit 832d4ba

Please sign in to comment.