Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#include "stack_size.h"
#include "shared/runtime/pyexec.h"

#ifdef USE_YK
#include <yk.h>
#endif

// Command line options, with their defaults
bool mp_compile_only = false;
static uint emit_opt = MP_EMIT_OPT_NONE;
Expand Down Expand Up @@ -443,6 +447,10 @@ int main(int argc, char **argv) {
// Define a reasonable stack limit to detect stack overflow.
mp_uint_t stack_size = 40000 * UNIX_STACK_MULTIPLIER;

#ifdef USE_YK
yk_init();
#endif

// We should capture stack top ASAP after start, and it should be
// captured guaranteedly before any other stack variables are allocated.
// For this, actual main (renamed main_) should not be inlined into
Expand Down
16 changes: 16 additions & 0 deletions shared/runtime/gchelper_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ static void gc_helper_get_regs(gc_helper_regs_t arr) {

#endif // MICROPY_GCREGS_SETJMP

#ifdef USE_YK
void gc_collect_shadowstack(void *start, void *end) {
gc_collect_root(start, ((intptr_t) end - (intptr_t) start) / sizeof(void *));
}
#endif

// Explicitly mark this as noinline to make sure the regs variable
// is effectively at the top of the stack: otherwise, in builds where
// LTO is enabled and a lot of inlining takes place we risk a stack
Expand All @@ -217,6 +223,16 @@ MP_NOINLINE void gc_helper_collect_regs_and_stack(void) {
// GC stack (and regs because we captured them)
void **regs_ptr = (void **)(void *)&regs;
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)&regs) / sizeof(uintptr_t));

#ifdef USE_YK
// Now scan the shadow stack.
//
// FIXME: this will scan the shadow stacks of *all* threads, but we should
// probably only scan the current thread's shadow stack here. yk doesn't
// currently have provide an API (e.g. a `yk_curthread_sstack_bounds()`)
// that could enable this.
yk_foreach_shadowstack(gc_collect_shadowstack);
#endif
}

#endif // MICROPY_ENABLE_GC