From: Ondřej Surý Date: Fri, 23 Sep 2022 11:49:31 +0000 (+0200) Subject: Make the debugging flags local to the memory context X-Git-Tag: v9.19.6~27^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2d2022a5094bb82598e695425ba54f8e29643a01;p=thirdparty%2Fbind9.git 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. --- diff --git a/lib/isc/mem.c b/lib/isc/mem.c index f828672119e..767f910c135 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -132,6 +132,7 @@ static uint64_t totallost; struct isc_mem { unsigned int magic; unsigned int flags; + unsigned int debugging; isc_mutex_t lock; bool checkfree; struct stats stats[STATS_BUCKETS + 1]; @@ -184,23 +185,23 @@ struct isc_mempool { */ #if !ISC_MEM_TRACKLINES -#define ADD_TRACE(a, b, c, d, e) -#define DELETE_TRACE(a, b, c, d, e) +#define ADD_TRACE(mctx, ptr, size, file, line) +#define DELETE_TRACE(mctx, ptr, size, file, line) #define ISC_MEMFUNC_SCOPE #else /* if !ISC_MEM_TRACKLINES */ #define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD) -#define SHOULD_TRACE_OR_RECORD(ptr) \ - ((isc_mem_debugging & TRACE_OR_RECORD) != 0 && ptr != NULL) +#define SHOULD_TRACE_OR_RECORD(mctx, ptr) \ + (((mctx)->debugging & TRACE_OR_RECORD) != 0 && ptr != NULL) -#define ADD_TRACE(a, b, c, d, e) \ - if (SHOULD_TRACE_OR_RECORD(b)) { \ - add_trace_entry(a, b, c, d, e); \ +#define ADD_TRACE(mctx, ptr, size, file, line) \ + if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \ + add_trace_entry(mctx, ptr, size, file, line); \ } -#define DELETE_TRACE(a, b, c, d, e) \ - if (SHOULD_TRACE_OR_RECORD(b)) { \ - delete_trace_entry(a, b, c, d, e); \ +#define DELETE_TRACE(mctx, ptr, size, file, line) \ + if (SHOULD_TRACE_OR_RECORD(mctx, ptr)) { \ + delete_trace_entry(mctx, ptr, size, file, line); \ } static void @@ -239,7 +240,7 @@ add_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size FLARG) { 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", ptr, size, file, line, mctx); } @@ -284,7 +285,7 @@ delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size, 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", ptr, size, file, line, mctx); } @@ -456,7 +457,7 @@ isc__mem_shutdown(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; REQUIRE(ctxp != NULL && *ctxp == NULL); @@ -466,6 +467,7 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) { *ctx = (isc_mem_t){ .magic = MEM_MAGIC, + .debugging = debugging, .flags = flags, .checkfree = true, }; @@ -490,7 +492,7 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) { ISC_LIST_INIT(ctx->pools); #if ISC_MEM_TRACKLINES - if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) { + if ((ctx->debugging & ISC_MEM_DEBUGRECORD) != 0) { unsigned int i; ctx->debuglist = @@ -605,7 +607,7 @@ isc__mem_detach(isc_mem_t **ctxp FLARG) { if (isc_refcount_decrement(&ctx->references) == 1) { isc_refcount_destroy(&ctx->references); #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, line); } @@ -662,7 +664,7 @@ isc__mem_destroy(isc_mem_t **ctxp FLARG) { *ctxp = NULL; #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, line); } @@ -713,7 +715,7 @@ hi_water(isc_mem_t *ctx) { (void)atomic_compare_exchange_strong(&ctx->maxinuse, &maxinuse, inuse); - if ((isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0) { + if ((ctx->debugging & ISC_MEM_DEBUGUSAGE) != 0) { fprintf(stderr, "maxinuse = %lu\n", (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_debugging & ISC_MEM_DEBUGTRACE) != 0) { + if ((mctx->debugging & ISC_MEM_DEBUGTRACE) != 0) { fprintf(stderr, "create pool %p file %s line %u mctx %p\n", mpctx, file, line, mctx); } @@ -1229,7 +1231,7 @@ isc__mempool_destroy(isc_mempool_t **restrict mpctxp FLARG) { mctx = mpctx->mctx; #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", mpctx, file, line, mctx); } @@ -1747,7 +1749,7 @@ error: void 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_debugging & ISC_MEM_DEBUGTRACE) != 0) { fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp, diff --git a/tests/isc/mem_test.c b/tests/isc/mem_test.c index f2bbdbd3f47..183b2e78912 100644 --- a/tests/isc/mem_test.c +++ b/tests/isc/mem_test.c @@ -296,7 +296,7 @@ ISC_RUN_TEST_IMPL(isc_mem_reget) { ISC_RUN_TEST_IMPL(isc_mem_noflags) { isc_result_t result; isc_mem_t *mctx2 = NULL; - char buf[4096], *p, *q; + char buf[4096], *p; FILE *f; void *ptr; @@ -305,13 +305,14 @@ ISC_RUN_TEST_IMPL(isc_mem_noflags) { UNUSED(state); - isc_mem_create(&mctx2); isc_mem_debugging = 0; + isc_mem_create(&mctx2); ptr = isc_mem_get(mctx2, 2048); assert_non_null(ptr); isc__mem_printactive(mctx2, f); isc_mem_put(mctx2, ptr, 2048); isc_mem_destroy(&mctx2); + isc_mem_debugging = ISC_MEM_DEBUGRECORD; isc_stdio_close(f); memset(buf, 0, sizeof(buf)); @@ -325,15 +326,7 @@ ISC_RUN_TEST_IMPL(isc_mem_noflags) { buf[sizeof(buf) - 1] = 0; p = strchr(buf, '\n'); - assert_non_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; + assert_null(p); } /* test mem with record flag */ @@ -390,13 +383,14 @@ ISC_RUN_TEST_IMPL(isc_mem_traceflag) { UNUSED(state); + isc_mem_debugging = ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGTRACE; isc_mem_create(&mctx2); - isc_mem_debugging = ISC_MEM_DEBUGTRACE; ptr = isc_mem_get(mctx2, 2048); assert_non_null(ptr); isc__mem_printactive(mctx2, f); isc_mem_put(mctx2, ptr, 2048); isc_mem_destroy(&mctx2); + isc_mem_debugging = ISC_MEM_DEBUGRECORD; isc_stdio_close(f); memset(buf, 0, sizeof(buf)); @@ -412,9 +406,13 @@ ISC_RUN_TEST_IMPL(isc_mem_traceflag) { buf[sizeof(buf) - 1] = 0; - assert_memory_equal(buf, "add ", 4); + assert_memory_equal(buf, "create ", 6); p = strchr(buf, '\n'); 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'); assert_non_null(p); 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'); assert_non_null(p); assert_memory_equal(p + 1, "del ", 4); - - isc_mem_debugging = ISC_MEM_DEBUGRECORD; } #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_reget) -#if !defined(__SANITIZE_THREAD__) -ISC_TEST_ENTRY(isc_mem_benchmark) -#endif /* __SANITIZE_THREAD__ */ #if ISC_MEM_TRACKLINES ISC_TEST_ENTRY(isc_mem_noflags) ISC_TEST_ENTRY(isc_mem_recordflag) @@ -515,6 +508,9 @@ ISC_TEST_ENTRY(isc_mem_recordflag) */ ISC_TEST_ENTRY(isc_mem_traceflag) #endif /* if ISC_MEM_TRACKLINES */ +#if !defined(__SANITIZE_THREAD__) +ISC_TEST_ENTRY(isc_mem_benchmark) +#endif /* __SANITIZE_THREAD__ */ ISC_TEST_LIST_END