2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-01 15:05:23 +00:00

Make the debugging flags local to the memory context

Previously, the isc_mem_debugging would be single global variable that
would affect the behavior of the memory context whenever it would be
changed which could be after some allocation were already done.

Change the memory debugging options to be local to the memory context
and immutable, so all allocations within the same memory context are
treated the same.
This commit is contained in:
Ondřej Surý
2022-09-23 13:49:31 +02:00
parent 56f9c886aa
commit 2d2022a509
2 changed files with 36 additions and 38 deletions

View File

@@ -132,6 +132,7 @@ static uint64_t totallost;
struct isc_mem { struct isc_mem {
unsigned int magic; unsigned int magic;
unsigned int flags; unsigned int flags;
unsigned int debugging;
isc_mutex_t lock; isc_mutex_t lock;
bool checkfree; bool checkfree;
struct stats stats[STATS_BUCKETS + 1]; struct stats stats[STATS_BUCKETS + 1];
@@ -184,23 +185,23 @@ struct isc_mempool {
*/ */
#if !ISC_MEM_TRACKLINES #if !ISC_MEM_TRACKLINES
#define ADD_TRACE(a, b, c, d, e) #define ADD_TRACE(mctx, ptr, size, file, line)
#define DELETE_TRACE(a, b, c, d, e) #define DELETE_TRACE(mctx, ptr, size, file, line)
#define ISC_MEMFUNC_SCOPE #define ISC_MEMFUNC_SCOPE
#else /* if !ISC_MEM_TRACKLINES */ #else /* if !ISC_MEM_TRACKLINES */
#define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD) #define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
#define SHOULD_TRACE_OR_RECORD(ptr) \ #define SHOULD_TRACE_OR_RECORD(mctx, ptr) \
((isc_mem_debugging & TRACE_OR_RECORD) != 0 && ptr != NULL) (((mctx)->debugging & TRACE_OR_RECORD) != 0 && ptr != NULL)
#define ADD_TRACE(a, b, c, d, e) \ #define ADD_TRACE(mctx, ptr, size, file, line) \
if (SHOULD_TRACE_OR_RECORD(b)) { \ if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \
add_trace_entry(a, b, c, d, e); \ add_trace_entry(mctx, ptr, size, file, line); \
} }
#define DELETE_TRACE(a, b, c, d, e) \ #define DELETE_TRACE(mctx, ptr, size, file, line) \
if (SHOULD_TRACE_OR_RECORD(b)) { \ if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \
delete_trace_entry(a, b, c, d, e); \ delete_trace_entry(mctx, ptr, size, file, line); \
} }
static void static void
@@ -239,7 +240,7 @@ add_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size FLARG) {
MCTXLOCK(mctx); MCTXLOCK(mctx);
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n", fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n",
ptr, size, file, line, mctx); ptr, size, file, line, mctx);
} }
@@ -284,7 +285,7 @@ delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size,
MCTXLOCK(mctx); MCTXLOCK(mctx);
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n", fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n",
ptr, size, file, line, mctx); ptr, size, file, line, mctx);
} }
@@ -456,7 +457,7 @@ isc__mem_shutdown(void) {
} }
static void static void
mem_create(isc_mem_t **ctxp, unsigned int flags) { mem_create(isc_mem_t **ctxp, unsigned int debugging, unsigned int flags) {
isc_mem_t *ctx = NULL; isc_mem_t *ctx = NULL;
REQUIRE(ctxp != NULL && *ctxp == NULL); REQUIRE(ctxp != NULL && *ctxp == NULL);
@@ -466,6 +467,7 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) {
*ctx = (isc_mem_t){ *ctx = (isc_mem_t){
.magic = MEM_MAGIC, .magic = MEM_MAGIC,
.debugging = debugging,
.flags = flags, .flags = flags,
.checkfree = true, .checkfree = true,
}; };
@@ -490,7 +492,7 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) {
ISC_LIST_INIT(ctx->pools); ISC_LIST_INIT(ctx->pools);
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) { if ((ctx->debugging & ISC_MEM_DEBUGRECORD) != 0) {
unsigned int i; unsigned int i;
ctx->debuglist = ctx->debuglist =
@@ -605,7 +607,7 @@ isc__mem_detach(isc_mem_t **ctxp FLARG) {
if (isc_refcount_decrement(&ctx->references) == 1) { if (isc_refcount_decrement(&ctx->references) == 1) {
isc_refcount_destroy(&ctx->references); isc_refcount_destroy(&ctx->references);
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((ctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy mctx %p file %s line %u\n", fprintf(stderr, "destroy mctx %p file %s line %u\n",
ctx, file, line); ctx, file, line);
} }
@@ -662,7 +664,7 @@ isc__mem_destroy(isc_mem_t **ctxp FLARG) {
*ctxp = NULL; *ctxp = NULL;
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((ctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file, fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file,
line); line);
} }
@@ -713,7 +715,7 @@ hi_water(isc_mem_t *ctx) {
(void)atomic_compare_exchange_strong(&ctx->maxinuse, &maxinuse, (void)atomic_compare_exchange_strong(&ctx->maxinuse, &maxinuse,
inuse); inuse);
if ((isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0) { if ((ctx->debugging & ISC_MEM_DEBUGUSAGE) != 0) {
fprintf(stderr, "maxinuse = %lu\n", fprintf(stderr, "maxinuse = %lu\n",
(unsigned long)inuse); (unsigned long)inuse);
} }
@@ -1189,7 +1191,7 @@ isc__mempool_create(isc_mem_t *restrict mctx, const size_t element_size,
}; };
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "create pool %p file %s line %u mctx %p\n", fprintf(stderr, "create pool %p file %s line %u mctx %p\n",
mpctx, file, line, mctx); mpctx, file, line, mctx);
} }
@@ -1229,7 +1231,7 @@ isc__mempool_destroy(isc_mempool_t **restrict mpctxp FLARG) {
mctx = mpctx->mctx; mctx = mpctx->mctx;
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "destroy pool %p file %s line %u mctx %p\n", fprintf(stderr, "destroy pool %p file %s line %u mctx %p\n",
mpctx, file, line, mctx); mpctx, file, line, mctx);
} }
@@ -1747,7 +1749,7 @@ error:
void void
isc__mem_create(isc_mem_t **mctxp FLARG) { isc__mem_create(isc_mem_t **mctxp FLARG) {
mem_create(mctxp, isc_mem_defaultflags); mem_create(mctxp, isc_mem_debugging, isc_mem_defaultflags);
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp, fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp,

View File

@@ -296,7 +296,7 @@ ISC_RUN_TEST_IMPL(isc_mem_reget) {
ISC_RUN_TEST_IMPL(isc_mem_noflags) { ISC_RUN_TEST_IMPL(isc_mem_noflags) {
isc_result_t result; isc_result_t result;
isc_mem_t *mctx2 = NULL; isc_mem_t *mctx2 = NULL;
char buf[4096], *p, *q; char buf[4096], *p;
FILE *f; FILE *f;
void *ptr; void *ptr;
@@ -305,13 +305,14 @@ ISC_RUN_TEST_IMPL(isc_mem_noflags) {
UNUSED(state); UNUSED(state);
isc_mem_create(&mctx2);
isc_mem_debugging = 0; isc_mem_debugging = 0;
isc_mem_create(&mctx2);
ptr = isc_mem_get(mctx2, 2048); ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr); assert_non_null(ptr);
isc__mem_printactive(mctx2, f); isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048); isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2); isc_mem_destroy(&mctx2);
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
isc_stdio_close(f); isc_stdio_close(f);
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
@@ -325,15 +326,7 @@ ISC_RUN_TEST_IMPL(isc_mem_noflags) {
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
p = strchr(buf, '\n'); p = strchr(buf, '\n');
assert_non_null(p); assert_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
p += 2;
q = strchr(p, '\n');
assert_non_null(q);
*q = '\0';
assert_string_equal(p, "None.");
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
} }
/* test mem with record flag */ /* test mem with record flag */
@@ -390,13 +383,14 @@ ISC_RUN_TEST_IMPL(isc_mem_traceflag) {
UNUSED(state); UNUSED(state);
isc_mem_debugging = ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGTRACE;
isc_mem_create(&mctx2); isc_mem_create(&mctx2);
isc_mem_debugging = ISC_MEM_DEBUGTRACE;
ptr = isc_mem_get(mctx2, 2048); ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr); assert_non_null(ptr);
isc__mem_printactive(mctx2, f); isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048); isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2); isc_mem_destroy(&mctx2);
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
isc_stdio_close(f); isc_stdio_close(f);
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
@@ -412,9 +406,13 @@ ISC_RUN_TEST_IMPL(isc_mem_traceflag) {
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
assert_memory_equal(buf, "add ", 4); assert_memory_equal(buf, "create ", 6);
p = strchr(buf, '\n'); p = strchr(buf, '\n');
assert_non_null(p); assert_non_null(p);
assert_memory_equal(p + 1, "add ", 4);
p = strchr(p + 1, '\n');
assert_non_null(p);
p = strchr(p + 1, '\n'); p = strchr(p + 1, '\n');
assert_non_null(p); assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3); assert_in_range(p, 0, buf + sizeof(buf) - 3);
@@ -422,8 +420,6 @@ ISC_RUN_TEST_IMPL(isc_mem_traceflag) {
p = strchr(p + 1, '\n'); p = strchr(p + 1, '\n');
assert_non_null(p); assert_non_null(p);
assert_memory_equal(p + 1, "del ", 4); assert_memory_equal(p + 1, "del ", 4);
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
} }
#endif /* if ISC_MEM_TRACKLINES */ #endif /* if ISC_MEM_TRACKLINES */
@@ -502,9 +498,6 @@ ISC_TEST_ENTRY(isc_mem_inuse)
ISC_TEST_ENTRY(isc_mem_zeroget) ISC_TEST_ENTRY(isc_mem_zeroget)
ISC_TEST_ENTRY(isc_mem_reget) ISC_TEST_ENTRY(isc_mem_reget)
#if !defined(__SANITIZE_THREAD__)
ISC_TEST_ENTRY(isc_mem_benchmark)
#endif /* __SANITIZE_THREAD__ */
#if ISC_MEM_TRACKLINES #if ISC_MEM_TRACKLINES
ISC_TEST_ENTRY(isc_mem_noflags) ISC_TEST_ENTRY(isc_mem_noflags)
ISC_TEST_ENTRY(isc_mem_recordflag) ISC_TEST_ENTRY(isc_mem_recordflag)
@@ -515,6 +508,9 @@ ISC_TEST_ENTRY(isc_mem_recordflag)
*/ */
ISC_TEST_ENTRY(isc_mem_traceflag) ISC_TEST_ENTRY(isc_mem_traceflag)
#endif /* if ISC_MEM_TRACKLINES */ #endif /* if ISC_MEM_TRACKLINES */
#if !defined(__SANITIZE_THREAD__)
ISC_TEST_ENTRY(isc_mem_benchmark)
#endif /* __SANITIZE_THREAD__ */
ISC_TEST_LIST_END ISC_TEST_LIST_END