Skip to content

Commit

Permalink
Add functionality to find a task's stack buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbehrends committed Oct 4, 2018
1 parent 32e3851 commit 83bc53b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/julia_gcext.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ JL_DLLEXPORT int jl_gc_conservative_gc_support_enabled(void);
// jl_typeof(obj) is an actual type object.
JL_DLLEXPORT jl_value_t *jl_gc_internal_obj_base_ptr(void *p);

// Return a non-null pointer to the start of the stack area if the task
// has an associated stack buffer. In that case, *size will also contain
// the size of that stack buffer upon return. Also, if task is a thread's
// current task, that thread's id will be stored in *tid; otherwise,
// *tid will be set to -1.
JL_DLLEXPORT void *jl_task_stack_buffer(jl_task_t *task, size_t *size, int *tid);

#endif // _JULIA_GCEXT_H
36 changes: 34 additions & 2 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ volatile int jl_in_stackwalk = 0;
#define MINSTKSZ 131072
#endif

#define ROOT_TASK_STACK_ADJUSTMENT 3000000

static jl_sym_t *done_sym;
static jl_sym_t *failed_sym;
static jl_sym_t *runnable_sym;
Expand Down Expand Up @@ -179,6 +181,36 @@ static void JL_NORETURN finish_task(jl_task_t *t, jl_value_t *resultval JL_MAYBE
abort();
}

JL_DLLEXPORT void *jl_task_stack_buffer(jl_task_t *task, size_t *size, int *tid)
{
size_t off = 0;
#ifndef _OS_WINDOWS_
if (jl_all_tls_states[0]->root_task == task) {
// See jl_init_root_task(). The root task of the main thread
// has its buffer enlarged by an artificial 3000000 bytes, but
// that means that the start of the buffer usually points to
// inaccessible memory. We need to correct for this.
off = ROOT_TASK_STACK_ADJUSTMENT;
}
#endif
*tid = -1;
for (int i = 0; i < jl_n_threads; i++) {
jl_ptls_t ptls = jl_all_tls_states[i];
if (ptls->current_task == task) {
*tid = i;
#ifdef COPY_STACKS
if (task->copy_stack) {
*size = ptls->stacksize;
return (char *)ptls->stackbase - *size;
}
#endif
break; // continue with normal return
}
}
*size = task->bufsz - off;
return (void *)((char *)task->stkbuf + off);
}

static void record_backtrace(void) JL_NOTSAFEPOINT
{
jl_ptls_t ptls = jl_get_ptls_states();
Expand Down Expand Up @@ -813,8 +845,8 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
size_t ssize = (char*)stack_hi - (char*)stack_lo;
#ifndef _OS_WINDOWS_
if (ptls->tid == 0) {
stack = (void*)((char*)stack - 3000000); // offset our guess of the address of the bottom of stack to cover the guard pages too
ssize += 3000000; // sizeof stack is known exactly, but not where we are in that stack
stack = (void*)((char*)stack - ROOT_TASK_STACK_ADJUSTMENT); // offset our guess of the address of the bottom of stack to cover the guard pages too
ssize += ROOT_TASK_STACK_ADJUSTMENT; // sizeof stack is known exactly, but not where we are in that stack
}
#endif
ptls->current_task->stkbuf = stack;
Expand Down

0 comments on commit 83bc53b

Please sign in to comment.