trace2: remove use of xcalloc()

Remove use of xcalloc() from the trace2 API due to its possible use of
die(), which could lead to recursive die() handlers. This is used in the
trace2 API to track an array of thread contexts when logging multi-
threaded operations.

Instead of killing the process on a failure, we attempt to proceed as
much as possible. We replace the dynamic thread context with a
statically-allocated context that uses the "unknown" thread name to
identify that we are in an error case.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
seen
Derrick Stolee 2026-08-31 17:25:43 +00:00 committed by Junio C Hamano
parent 4adf78e107
commit a80c36bda0
5 changed files with 69 additions and 5 deletions

View File

@ -17,6 +17,9 @@
#undef xstrdup #undef xstrdup
#define xstrdup(str) BANNED(xstrdup) #define xstrdup(str) BANNED(xstrdup)


#undef xcalloc
#define xcalloc(nmemb, size) BANNED(xcalloc)

#undef xstrfmt #undef xstrfmt
#define xstrfmt(...) BANNED(xstrfmt) #define xstrfmt(...) BANNED(xstrfmt)



View File

@ -55,7 +55,11 @@ static struct tr2_counter_metadata tr2_counter_metadata[TRACE2_NUMBER_OF_COUNTER
void tr2_counter_increment(enum trace2_counter_id cid, uint64_t value) void tr2_counter_increment(enum trace2_counter_id cid, uint64_t value)
{ {
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
struct tr2_counter *c = &ctx->counter_block.counter[cid]; struct tr2_counter *c;

if (tr2tls_is_fallback(ctx))
return;
c = &ctx->counter_block.counter[cid];


c->value += value; c->value += value;


@ -69,6 +73,8 @@ void tr2_update_final_counters(void)
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
enum trace2_counter_id cid; enum trace2_counter_id cid;


if (tr2tls_is_fallback(ctx))
return;
if (!ctx->used_any_counter) if (!ctx->used_any_counter)
return; return;


@ -90,6 +96,8 @@ void tr2_emit_per_thread_counters(tr2_tgt_evt_counter_t *fn_apply)
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
enum trace2_counter_id cid; enum trace2_counter_id cid;


if (tr2tls_is_fallback(ctx))
return;
if (!ctx->used_any_per_thread_counter) if (!ctx->used_any_per_thread_counter)
return; return;



View File

@ -14,6 +14,9 @@
#define TR2_REGION_NESTING_INITIAL_SIZE (100) #define TR2_REGION_NESTING_INITIAL_SIZE (100)


static struct tr2tls_thread_ctx *tr2tls_thread_main; static struct tr2tls_thread_ctx *tr2tls_thread_main;
static struct tr2tls_thread_ctx tr2tls_thread_fallback = {
.thread_name = "unknown",
};
static uint64_t tr2tls_us_start_process; static uint64_t tr2tls_us_start_process;


static pthread_mutex_t tr2tls_mutex; static pthread_mutex_t tr2tls_mutex;
@ -38,16 +41,23 @@ void tr2tls_start_process_clock(void)
struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name, struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
uint64_t us_thread_start) uint64_t us_thread_start)
{ {
struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx)); struct tr2tls_thread_ctx *ctx = calloc(1, sizeof(*ctx));
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;


if (!ctx)
goto fallback;

/* /*
* Implicitly "tr2tls_push_self()" to capture the thread's start * Implicitly "tr2tls_push_self()" to capture the thread's start
* time in array_us_start[0]. For the main thread this gives us the * time in array_us_start[0]. For the main thread this gives us the
* application run time. * application run time.
*/ */
ctx->alloc = TR2_REGION_NESTING_INITIAL_SIZE; ctx->alloc = TR2_REGION_NESTING_INITIAL_SIZE;
ctx->array_us_start = (uint64_t *)xcalloc(ctx->alloc, sizeof(uint64_t)); ctx->array_us_start = calloc(ctx->alloc, sizeof(uint64_t));
if (!ctx->array_us_start) {
free(ctx);
goto fallback;
}
ctx->array_us_start[ctx->nr_open_regions++] = us_thread_start; ctx->array_us_start[ctx->nr_open_regions++] = us_thread_start;


ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id); ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
@ -63,6 +73,10 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
pthread_setspecific(tr2tls_key, ctx); pthread_setspecific(tr2tls_key, ctx);


return ctx; return ctx;

fallback:
pthread_setspecific(tr2tls_key, &tr2tls_thread_fallback);
return &tr2tls_thread_fallback;
} }


struct tr2tls_thread_ctx *tr2tls_get_self(void) struct tr2tls_thread_ctx *tr2tls_get_self(void)
@ -85,6 +99,11 @@ struct tr2tls_thread_ctx *tr2tls_get_self(void)
return ctx; return ctx;
} }


int tr2tls_is_fallback(const struct tr2tls_thread_ctx *ctx)
{
return ctx == &tr2tls_thread_fallback;
}

int tr2tls_is_main_thread(void) int tr2tls_is_main_thread(void)
{ {
if (!HAVE_THREADS) if (!HAVE_THREADS)
@ -101,6 +120,9 @@ void tr2tls_unset_self(void)


pthread_setspecific(tr2tls_key, NULL); pthread_setspecific(tr2tls_key, NULL);


if (tr2tls_is_fallback(ctx))
return;

free((char *)ctx->thread_name); free((char *)ctx->thread_name);
free(ctx->array_us_start); free(ctx->array_us_start);
free(ctx); free(ctx);
@ -112,6 +134,9 @@ void tr2tls_push_self(uint64_t us_now)
uint64_t *new_array; uint64_t *new_array;
size_t new_alloc; size_t new_alloc;


if (tr2tls_is_fallback(ctx))
return;

if (ctx->nr_skipped_regions) { if (ctx->nr_skipped_regions) {
ctx->nr_skipped_regions++; ctx->nr_skipped_regions++;
return; return;
@ -143,6 +168,9 @@ void tr2tls_pop_self(void)
{ {
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();


if (tr2tls_is_fallback(ctx))
return;

if (ctx->nr_skipped_regions) { if (ctx->nr_skipped_regions) {
ctx->nr_skipped_regions--; ctx->nr_skipped_regions--;
return; return;
@ -158,6 +186,9 @@ void tr2tls_pop_unwind_self(void)
{ {
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();


if (tr2tls_is_fallback(ctx))
return;

while (ctx->nr_open_regions > 1) while (ctx->nr_open_regions > 1)
tr2tls_pop_self(); tr2tls_pop_self();
} }
@ -168,6 +199,8 @@ uint64_t tr2tls_region_elasped_self(uint64_t us)
uint64_t us_start; uint64_t us_start;


ctx = tr2tls_get_self(); ctx = tr2tls_get_self();
if (tr2tls_is_fallback(ctx))
return 0;
if (ctx->nr_skipped_regions) if (ctx->nr_skipped_regions)
return 0; return 0;
if (!ctx->nr_open_regions) if (!ctx->nr_open_regions)
@ -189,6 +222,10 @@ uint64_t tr2tls_absolute_elapsed(uint64_t us)
static void tr2tls_key_destructor(void *payload) static void tr2tls_key_destructor(void *payload)
{ {
struct tr2tls_thread_ctx *ctx = payload; struct tr2tls_thread_ctx *ctx = payload;

if (tr2tls_is_fallback(ctx))
return;

free((char *)ctx->thread_name); free((char *)ctx->thread_name);
free(ctx->array_us_start); free(ctx->array_us_start);
free(ctx); free(ctx);

View File

@ -54,6 +54,12 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
*/ */
struct tr2tls_thread_ctx *tr2tls_get_self(void); struct tr2tls_thread_ctx *tr2tls_get_self(void);


/*
* Return true if the context is the non-allocating fallback used after an
* allocation failure. Callers must not modify a fallback context.
*/
int tr2tls_is_fallback(const struct tr2tls_thread_ctx *ctx);

/* /*
* return true if the current thread is the main thread. * return true if the current thread is the main thread.
*/ */

View File

@ -39,8 +39,11 @@ static struct tr2_timer_metadata tr2_timer_metadata[TRACE2_NUMBER_OF_TIMERS] = {
void tr2_start_timer(enum trace2_timer_id tid) void tr2_start_timer(enum trace2_timer_id tid)
{ {
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
struct tr2_timer *t = &ctx->timer_block.timer[tid]; struct tr2_timer *t;


if (tr2tls_is_fallback(ctx))
return;
t = &ctx->timer_block.timer[tid];
t->recursion_count++; t->recursion_count++;
if (t->recursion_count > 1) if (t->recursion_count > 1)
return; /* ignore recursive starts */ return; /* ignore recursive starts */
@ -51,10 +54,13 @@ void tr2_start_timer(enum trace2_timer_id tid)
void tr2_stop_timer(enum trace2_timer_id tid) void tr2_stop_timer(enum trace2_timer_id tid)
{ {
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
struct tr2_timer *t = &ctx->timer_block.timer[tid]; struct tr2_timer *t;
uint64_t ns_now; uint64_t ns_now;
uint64_t ns_interval; uint64_t ns_interval;


if (tr2tls_is_fallback(ctx))
return;
t = &ctx->timer_block.timer[tid];
assert(t->recursion_count > 0); assert(t->recursion_count > 0);


t->recursion_count--; t->recursion_count--;
@ -92,6 +98,8 @@ void tr2_update_final_timers(void)
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
enum trace2_timer_id tid; enum trace2_timer_id tid;


if (tr2tls_is_fallback(ctx))
return;
if (!ctx->used_any_timer) if (!ctx->used_any_timer)
return; return;


@ -138,6 +146,8 @@ void tr2_emit_per_thread_timers(tr2_tgt_evt_timer_t *fn_apply)
struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
enum trace2_timer_id tid; enum trace2_timer_id tid;


if (tr2tls_is_fallback(ctx))
return;
if (!ctx->used_any_per_thread_timer) if (!ctx->used_any_per_thread_timer)
return; return;