From 832d4ba912214f6bade04ba80d41b16d8cbf10f4 Mon Sep 17 00:00:00 2001 From: Tom Kelly Date: Thu, 19 Nov 2020 09:20:09 +0000 Subject: [PATCH] Must allocate main stack *after* creation of the stack cache fix debug memory guard in caml_free_stack --- runtime/domain.c | 17 +++++++++-------- runtime/fiber.c | 11 +++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/runtime/domain.c b/runtime/domain.c index e6e3514bb90..eee81f99ca0 100644 --- a/runtime/domain.c +++ b/runtime/domain.c @@ -274,21 +274,22 @@ 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; @@ -296,11 +297,11 @@ static void create_domain(uintnat initial_minor_heap_wsize) { #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: diff --git a/runtime/fiber.c b/runtime/fiber.c index 9550e5b4c41..ecd60c18f72 100644 --- a/runtime/fiber.c +++ b/runtime/fiber.c @@ -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 */ @@ -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); } }