Skip to content

Commit c76a37d

Browse files
vtjnashXnartharax
authored andcommitted
fix malloc-stack CI failure (JuliaLang#49082)
We see Windows CI fail often here, and we don't actually need this memory--only the first thread should initialize it. (indeed, we were temporarily corrupting these data-structures in the process by reinitializing them, though that was not typically detectable.) From worker 7: Exception: EXCEPTION_ACCESS_VIOLATION at 0x7ffa26726e87 -- jl_makecontext at C:/workdir/src\win32_ucontext.c:67 From worker 7: in expression starting at C:\buildkite-agent\builds\win2k22-amdci6-6\julialang\julia-master\julia-ceffaee345\share\julia\test\ccall.jl:1066 From worker 7: jl_makecontext at C:/workdir/src\win32_ucontext.c:67 From worker 7: jl_install_thread_signal_handler at C:/workdir/src\signals-win.c:490 From worker 7: jl_init_root_task at C:/workdir/src\task.c:1568 From worker 7: ijl_adopt_thread at C:/workdir/src\threading.c:414 From worker 7: unknown function (ip: 000001d791a58969) From worker 7: uv__queue_work at /workspace/srcdir/libuv\src\threadpool.c:305 From worker 7: worker at /workspace/srcdir/libuv\src\threadpool.c:122 From worker 7: uv__thread_start at /workspace/srcdir/libuv\src/win\thread.c:111 From worker 7: beginthreadex at C:\Windows\System32\msvcrt.dll (unknown line) From worker 7: endthreadex at C:\Windows\System32\msvcrt.dll (unknown line) From worker 7: BaseThreadInitThunk at C:\Windows\System32\KERNEL32.DLL (unknown line) From worker 7: RtlUserThreadStart at C:\Windows\SYSTEM32\ntdll.dll (unknown line) From worker 7: Allocations: 352796158 (Pool: 352389694; Big: 406464); GC: 722
1 parent b21d15c commit c76a37d

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

src/gc-stacks.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ JL_DLLEXPORT void *jl_malloc_stack(size_t *bufsz, jl_task_t *owner) JL_NOTSAFEPO
165165
ssize = LLT_ALIGN(ssize, jl_page_size);
166166
}
167167
if (stk == NULL) {
168-
if (jl_atomic_load_relaxed(&num_stack_mappings) >= MAX_STACK_MAPPINGS)
168+
if (jl_atomic_load_relaxed(&num_stack_mappings) >= MAX_STACK_MAPPINGS) {
169169
// we accept that this can go over by as much as nthreads since it's not a CAS
170+
errno = ENOMEM;
170171
return NULL;
172+
}
171173
// TODO: allocate blocks of stacks? but need to mprotect individually anyways
172174
stk = malloc_stack(ssize);
173175
if (stk == MAP_FAILED)

src/signals-unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ static void allocate_segv_handler(void)
621621
static void *alloc_sigstack(size_t *ssize)
622622
{
623623
void *stk = jl_malloc_stack(ssize, NULL);
624-
if (stk == MAP_FAILED)
624+
if (stk == NULL)
625625
jl_errorf("fatal error allocating signal stack: mmap: %s", strerror(errno));
626626
return stk;
627627
}

src/signals-win.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,15 @@ void jl_install_default_signal_handlers(void)
483483

484484
void jl_install_thread_signal_handler(jl_ptls_t ptls)
485485
{
486-
size_t ssize = sig_stack_size;
487-
void *stk = jl_malloc_stack(&ssize, NULL);
488-
collect_backtrace_fiber.uc_stack.ss_sp = (void*)stk;
489-
collect_backtrace_fiber.uc_stack.ss_size = ssize;
490-
jl_makecontext(&collect_backtrace_fiber, start_backtrace_fiber);
491-
uv_mutex_init(&backtrace_lock);
492-
have_backtrace_fiber = 1;
486+
if (!have_backtrace_fiber) {
487+
size_t ssize = sig_stack_size;
488+
void *stk = jl_malloc_stack(&ssize, NULL);
489+
if (stk == NULL)
490+
jl_errorf("fatal error allocating signal stack: mmap: %s", strerror(errno));
491+
collect_backtrace_fiber.uc_stack.ss_sp = (void*)stk;
492+
collect_backtrace_fiber.uc_stack.ss_size = ssize;
493+
jl_makecontext(&collect_backtrace_fiber, start_backtrace_fiber);
494+
uv_mutex_init(&backtrace_lock);
495+
have_backtrace_fiber = 1;
496+
}
493497
}

src/task.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,12 +1552,15 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
15521552
#endif
15531553
if (jl_setjmp(ptls->copy_stack_ctx.uc_mcontext, 0))
15541554
start_task(); // sanitizer_finish_switch_fiber is part of start_task
1555-
return ct;
15561555
}
1557-
ssize = JL_STACK_SIZE;
1558-
char *stkbuf = jl_alloc_fiber(&ptls->base_ctx, &ssize, NULL);
1559-
ptls->stackbase = stkbuf + ssize;
1560-
ptls->stacksize = ssize;
1556+
else {
1557+
ssize = JL_STACK_SIZE;
1558+
char *stkbuf = jl_alloc_fiber(&ptls->base_ctx, &ssize, NULL);
1559+
if (stkbuf != NULL) {
1560+
ptls->stackbase = stkbuf + ssize;
1561+
ptls->stacksize = ssize;
1562+
}
1563+
}
15611564
#endif
15621565

15631566
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON)

0 commit comments

Comments
 (0)