Skip to content

Commit

Permalink
don't allocate in cgo_context
Browse files Browse the repository at this point in the history
cgo_context may be called from a signal handler. Allocating is
forbidden. Use thread local storage instead.
  • Loading branch information
benesch committed Jul 2, 2018
1 parent 9f05ade commit f7d9931
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions cgosymbolizer_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include "cgosymbolizer_darwin.h"

struct cgo_context {
bool used;
unw_context_t unw_ctx;
unw_cursor_t unw_cursor;
};

__thread struct cgo_context thread_ctx;

void cgo_traceback(void* p) {
struct {
uintptr_t ctx;
Expand Down Expand Up @@ -46,21 +49,21 @@ void cgo_context(void* p) {
}* arg = p;

if (arg->ctx != 0) {
free((struct cgo_context*) arg->ctx);
struct cgo_context* ctx = (struct cgo_context*) arg->ctx;
ctx->used = 0;
return;
}

struct cgo_context* ctx = malloc(sizeof(cgo_context));
if (ctx == NULL)
if (thread_ctx.used)
return;
if (unw_getcontext(&ctx->unw_ctx) != 0)
if (unw_getcontext(&thread_ctx.unw_ctx) != 0)
return;
if (unw_init_local(&ctx->unw_cursor, &ctx->unw_ctx) != 0)
if (unw_init_local(&thread_ctx.unw_cursor, &thread_ctx.unw_ctx) != 0)
return;
// Step the saved state past this frame. It will cease to exist momentarily.
if (unw_step(&ctx->unw_cursor) <= 0)
if (unw_step(&thread_ctx.unw_cursor) <= 0)
return;
arg->ctx = (uintptr_t) ctx;
arg->ctx = (uintptr_t) &thread_ctx;
}

void cgo_symbolizer(void* p) {
Expand Down

0 comments on commit f7d9931

Please sign in to comment.