Define PROF_TCTX_SENTINEL instead of using magic numbers

This makes the code more readable on its own, and also sets the stage
for more cleanly handling the pointer provenance lints in a following
commit.
This commit is contained in:
Kevin Svetlitski 2023-07-21 18:13:58 -07:00 committed by Qi Wang
parent c49c17f128
commit 7e54dd1ddb
7 changed files with 39 additions and 24 deletions

View file

@ -2529,12 +2529,12 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
sample_event);
emap_alloc_ctx_t alloc_ctx;
if (likely((uintptr_t)tctx == (uintptr_t)1U)) {
if (likely(tctx == PROF_TCTX_SENTINEL)) {
alloc_ctx.slab = sz_can_use_slab(usize);
allocation = imalloc_no_sample(
sopts, dopts, tsd, usize, usize, ind,
alloc_ctx.slab);
} else if ((uintptr_t)tctx > (uintptr_t)1U) {
} else if (tctx != NULL) {
allocation = imalloc_sample(
sopts, dopts, tsd, usize, ind);
alloc_ctx.slab = false;
@ -3366,7 +3366,7 @@ irallocx_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,
bool sample_event = te_prof_sample_event_lookahead(tsd, usize);
prof_tctx_t *tctx = prof_alloc_prep(tsd, prof_active, sample_event);
void *p;
if (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {
if (unlikely(tctx != PROF_TCTX_SENTINEL)) {
p = irallocx_prof_sample(tsd_tsdn(tsd), old_ptr, old_usize,
usize, alignment, zero, tcache, arena, tctx, hook_args);
} else {
@ -3612,7 +3612,7 @@ ixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,
prof_tctx_t *tctx = prof_alloc_prep(tsd, prof_active, sample_event);
size_t usize;
if (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {
if (unlikely(tctx != PROF_TCTX_SENTINEL)) {
usize = ixallocx_prof_sample(tsd_tsdn(tsd), ptr, old_usize,
size, extra, alignment, zero, tctx);
} else {

View file

@ -287,7 +287,7 @@ large_prof_info_get(tsd_t *tsd, edata_t *edata, prof_info_t *prof_info,
prof_tctx_t *alloc_tctx = edata_prof_tctx_get(edata);
prof_info->alloc_tctx = alloc_tctx;
if ((uintptr_t)alloc_tctx > (uintptr_t)1U) {
if (prof_tctx_is_valid(alloc_tctx)) {
nstime_copy(&prof_info->alloc_time,
edata_prof_alloc_time_get(edata));
prof_info->alloc_size = edata_prof_alloc_size_get(edata);
@ -308,7 +308,7 @@ large_prof_tctx_set(edata_t *edata, prof_tctx_t *tctx) {
void
large_prof_tctx_reset(edata_t *edata) {
large_prof_tctx_set(edata, (prof_tctx_t *)(uintptr_t)1U);
large_prof_tctx_set(edata, PROF_TCTX_SENTINEL);
}
void

View file

@ -91,11 +91,19 @@ prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx) {
cassert(config_prof);
if (tsd_reentrancy_level_get(tsd) > 0) {
assert((uintptr_t)tctx == (uintptr_t)1U);
assert(tctx == PROF_TCTX_SENTINEL);
return;
}
if ((uintptr_t)tctx > (uintptr_t)1U) {
if (prof_tctx_is_valid(tctx)) {
/*
* This `assert` really shouldn't be necessary. It's here
* because there's a bug in the clang static analyzer; it
* somehow does not realize that by `prof_tctx_is_valid(tctx)`
* being true that we've already ensured that `tctx` is not
* `NULL`.
*/
assert(tctx != NULL);
malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);
tctx->prepared = false;
prof_tctx_try_destroy(tsd, tctx);
@ -169,7 +177,7 @@ prof_free_sampled_object(tsd_t *tsd, const void *ptr, size_t usize,
assert(prof_info != NULL);
prof_tctx_t *tctx = prof_info->alloc_tctx;
assert((uintptr_t)tctx > (uintptr_t)1U);
assert(prof_tctx_is_valid(tctx));
szind_t szind = sz_size2index(usize);