From: Ondřej Surý Date: Thu, 24 Jul 2025 06:06:04 +0000 (+0200) Subject: Change the 'isc_g_mctx' to be always available X-Git-Tag: v9.21.11~11^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f7e5c1db38ec0ac6945510a65d931c546d485d41;p=thirdparty%2Fbind9.git Change the 'isc_g_mctx' to be always available This required couple of internal changes to the isc_mem_debugging. The isc_mem_debugging is now internal to isc_mem unit and there are three new functions: 1. isc_mem_setdebugging() can change the debugging setting for an individual memory context. This is need for the memory contexts used for OpenSSL, libxml and libuv accounting as recording and tracing memory is broken there. 2. isc_mem_debugon() / isc_mem_debugoff() can be used to change default memory debugging flags as well as debugging flags for isc_g_mctx. Additionally, the memory debugging is inconsistent across the code-base. For now, we are keeping the existing flags, but three new environment variables have been added 'ISC_MEM_DEBUGRECORD', 'ISC_MEM_DEBUGTRACE' and 'ISC_MEM_DEBUGUSAGE' to set the global debugging flags at any program using the memory contexts. --- diff --git a/bin/check/check-tool.c b/bin/check/check-tool.c index 8a1b3ac3482..dd833f6c4d8 100644 --- a/bin/check/check-tool.c +++ b/bin/check/check-tool.c @@ -106,10 +106,6 @@ add(char *key, int value) { isc_result_t result; isc_symvalue_t symvalue; - if (isc_g_mctx == NULL) { - isc_mem_create("check-tool", &isc_g_mctx); - } - if (symtab == NULL) { isc_symtab_create(isc_g_mctx, freekey, isc_g_mctx, false, &symtab); diff --git a/bin/check/named-checkconf.c b/bin/check/named-checkconf.c index 0a49aec2996..5485837fc98 100644 --- a/bin/check/named-checkconf.c +++ b/bin/check/named-checkconf.c @@ -599,15 +599,15 @@ main(int argc, char **argv) { case 'm': if (strcasecmp(isc_commandline_argument, "record") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); } if (strcasecmp(isc_commandline_argument, "trace") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); } if (strcasecmp(isc_commandline_argument, "usage") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; + isc_mem_debugon(ISC_MEM_DEBUGUSAGE); } break; default: @@ -616,8 +616,6 @@ main(int argc, char **argv) { } isc_commandline_reset = true; - isc_mem_create("default", &isc_g_mctx); - while ((c = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != EOF) { switch (c) { case 'a': @@ -688,7 +686,6 @@ main(int argc, char **argv) { } FALLTHROUGH; case 'h': - isc_mem_detach(&isc_g_mctx); usage(); default: @@ -711,7 +708,6 @@ main(int argc, char **argv) { } if (isc_commandline_index + 1 < argc) { - isc_mem_detach(&isc_g_mctx); usage(); } if (argv[isc_commandline_index] != NULL) { @@ -752,9 +748,5 @@ cleanup: cfg_parser_destroy(&parser); } - if (isc_g_mctx != NULL) { - isc_mem_detach(&isc_g_mctx); - } - return result == ISC_R_SUCCESS ? 0 : 1; } diff --git a/bin/check/named-checkzone.c b/bin/check/named-checkzone.c index 7f90883c81c..98a18ad78be 100644 --- a/bin/check/named-checkzone.c +++ b/bin/check/named-checkzone.c @@ -109,11 +109,6 @@ main(int argc, char **argv) { FILE *errout = stdout; char *endp; - /* - * Uncomment the following line if memory debugging is needed: - * isc_mem_debugging |= ISC_MEM_DEBUGRECORD; - */ - outputstyle = &dns_master_style_full; isc_commandline_init(argc, argv); @@ -518,7 +513,6 @@ main(int argc, char **argv) { usage(); } - isc_mem_create("default", &isc_g_mctx); if (!quiet) { RUNTIME_CHECK(setup_logging(errout) == ISC_R_SUCCESS); } @@ -560,7 +554,6 @@ main(int argc, char **argv) { fprintf(errout, "OK\n"); } destroy(); - isc_mem_detach(&isc_g_mctx); return (result == ISC_R_SUCCESS) ? 0 : 1; } diff --git a/bin/confgen/rndc-confgen.c b/bin/confgen/rndc-confgen.c index 2b75446f9ba..89ff68f398c 100644 --- a/bin/confgen/rndc-confgen.c +++ b/bin/confgen/rndc-confgen.c @@ -145,7 +145,7 @@ main(int argc, char **argv) { keyname = isc_commandline_argument; break; case 'M': - isc_mem_debugging = ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); break; case 'm': @@ -218,7 +218,6 @@ main(int argc, char **argv) { } algname = dst_hmac_algorithm_totext(alg); - isc_mem_create("default", &isc_g_mctx); isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret)); generate_key(isc_g_mctx, alg, keysize, &key_txtbuffer); @@ -282,7 +281,5 @@ options {\n\ isc_mem_stats(isc_g_mctx, stderr); } - isc_mem_detach(&isc_g_mctx); - return 0; } diff --git a/bin/confgen/tsig-keygen.c b/bin/confgen/tsig-keygen.c index ed01c35c039..553a45eaf2d 100644 --- a/bin/confgen/tsig-keygen.c +++ b/bin/confgen/tsig-keygen.c @@ -130,7 +130,7 @@ main(int argc, char **argv) { } break; case 'M': - isc_mem_debugging = ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); break; case 'm': show_final_mem = true; @@ -194,8 +194,6 @@ main(int argc, char **argv) { /* Use canonical algorithm name */ algname = dst_hmac_algorithm_totext(alg); - isc_mem_create("default", &isc_g_mctx); - if (keyname == NULL) { const char *suffix = NULL; @@ -278,7 +276,5 @@ nsupdate -k \n"); isc_mem_stats(isc_g_mctx, stderr); } - isc_mem_detach(&isc_g_mctx); - return 0; } diff --git a/bin/delv/delv.c b/bin/delv/delv.c index 22882e289e4..021aeb6deeb 100644 --- a/bin/delv/delv.c +++ b/bin/delv/delv.c @@ -1599,8 +1599,8 @@ preparse_args(int argc, char **argv) { } break; case 'm': - isc_mem_debugging = ISC_MEM_DEBUGTRACE | - ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGTRACE | + ISC_MEM_DEBUGRECORD); break; case '4': if (ipv6only) { diff --git a/bin/dig/dig.c b/bin/dig/dig.c index 41d7a3bd284..d4dec3f116b 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -2960,8 +2960,8 @@ preparse_args(int argc, char **argv) { break; case 'm': memdebugging = true; - isc_mem_debugging = ISC_MEM_DEBUGTRACE | - ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGTRACE | + ISC_MEM_DEBUGRECORD); break; case 'r': /* diff --git a/bin/dig/host.c b/bin/dig/host.c index 6df09b890ce..2afcf7638df 100644 --- a/bin/dig/host.c +++ b/bin/dig/host.c @@ -577,15 +577,15 @@ pre_parse_args(int argc, char **argv) { memdebugging = true; if (strcasecmp("trace", isc_commandline_argument) == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); } else if (strcasecmp("record", isc_commandline_argument) == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); } else if (strcasecmp("usage", isc_commandline_argument) == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; + isc_mem_debugon(ISC_MEM_DEBUGUSAGE); } break; diff --git a/bin/dnssec/dnssec-cds.c b/bin/dnssec/dnssec-cds.c index 5b92267bd41..22d1852f622 100644 --- a/bin/dnssec/dnssec-cds.c +++ b/bin/dnssec/dnssec-cds.c @@ -1052,11 +1052,8 @@ cleanup(void) { free_keytable(&new_key_tbl); } free_all_sets(); - if (isc_g_mctx != NULL) { - if (print_mem_stats && verbose > 10) { - isc_mem_stats(isc_g_mctx, stdout); - } - isc_mem_detach(&isc_g_mctx); + if (print_mem_stats && verbose > 10) { + isc_mem_stats(isc_g_mctx, stdout); } } @@ -1075,8 +1072,6 @@ main(int argc, char *argv[]) { isc_commandline_init(argc, argv); - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; #define OPTIONS "a:c:Dd:f:i:ms:T:uv:V" @@ -1112,8 +1107,8 @@ main(int argc, char *argv[]) { } break; case 'm': - isc_mem_debugging = ISC_MEM_DEBUGTRACE | - ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGTRACE | + ISC_MEM_DEBUGRECORD); break; case 's': startstr = isc_commandline_argument; diff --git a/bin/dnssec/dnssec-dsfromkey.c b/bin/dnssec/dnssec-dsfromkey.c index cb4976cb5d6..0eaf67d5053 100644 --- a/bin/dnssec/dnssec-dsfromkey.c +++ b/bin/dnssec/dnssec-dsfromkey.c @@ -383,8 +383,6 @@ main(int argc, char **argv) { isc_commandline_init(argc, argv); - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; #define OPTIONS "12Aa:Cc:d:Ff:K:sT:v:whV" @@ -547,7 +545,6 @@ main(int argc, char **argv) { if (verbose > 10) { isc_mem_stats(isc_g_mctx, stdout); } - isc_mem_detach(&isc_g_mctx); fflush(stdout); if (ferror(stdout)) { diff --git a/bin/dnssec/dnssec-importkey.c b/bin/dnssec/dnssec-importkey.c index 6a1b3768dcf..31f51c01ed6 100644 --- a/bin/dnssec/dnssec-importkey.c +++ b/bin/dnssec/dnssec-importkey.c @@ -309,8 +309,6 @@ main(int argc, char **argv) { isc_commandline_init(argc, argv); - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; #define CMDLINE_FLAGS "D:f:hK:L:P:v:V" @@ -454,7 +452,6 @@ main(int argc, char **argv) { if (verbose > 10) { isc_mem_stats(isc_g_mctx, stdout); } - isc_mem_detach(&isc_g_mctx); fflush(stdout); if (ferror(stdout)) { diff --git a/bin/dnssec/dnssec-keyfromlabel.c b/bin/dnssec/dnssec-keyfromlabel.c index e4b3db66878..31a61b5bab3 100644 --- a/bin/dnssec/dnssec-keyfromlabel.c +++ b/bin/dnssec/dnssec-keyfromlabel.c @@ -149,8 +149,6 @@ main(int argc, char **argv) { isc_commandline_init(argc, argv); - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; #define CMDLINE_FLAGS "3A:a:Cc:D:E:Ff:GhI:i:kK:L:l:M:n:P:p:R:S:t:v:Vy" @@ -699,7 +697,6 @@ main(int argc, char **argv) { isc_mem_stats(isc_g_mctx, stdout); } isc_mem_free(isc_g_mctx, label); - isc_mem_detach(&isc_g_mctx); if (freeit != NULL) { free(freeit); diff --git a/bin/dnssec/dnssec-keygen.c b/bin/dnssec/dnssec-keygen.c index 910e3c27239..d52df7ec646 100644 --- a/bin/dnssec/dnssec-keygen.c +++ b/bin/dnssec/dnssec-keygen.c @@ -820,15 +820,15 @@ main(int argc, char **argv) { case 'm': if (strcasecmp(isc_commandline_argument, "record") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); } if (strcasecmp(isc_commandline_argument, "trace") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); } if (strcasecmp(isc_commandline_argument, "usage") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; + isc_mem_debugon(ISC_MEM_DEBUGUSAGE); } break; default: @@ -837,8 +837,6 @@ main(int argc, char **argv) { } isc_commandline_reset = true; - isc_mem_create("default", &isc_g_mctx); - while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) { switch (ch) { case '3': @@ -1209,7 +1207,6 @@ main(int argc, char **argv) { if (verbose > 10) { isc_mem_stats(isc_g_mctx, stdout); } - isc_mem_detach(&isc_g_mctx); if (freeit != NULL) { free(freeit); diff --git a/bin/dnssec/dnssec-ksr.c b/bin/dnssec/dnssec-ksr.c index 55e8ca9e259..8c1b059232f 100644 --- a/bin/dnssec/dnssec-ksr.c +++ b/bin/dnssec/dnssec-ksr.c @@ -1330,8 +1330,6 @@ main(int argc, char *argv[]) { isc_commandline_init(argc, argv); - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; #define OPTIONS "E:e:Ff:hi:K:k:l:ov:V" diff --git a/bin/dnssec/dnssec-revoke.c b/bin/dnssec/dnssec-revoke.c index 7708b7b7596..a105f886842 100644 --- a/bin/dnssec/dnssec-revoke.c +++ b/bin/dnssec/dnssec-revoke.c @@ -81,8 +81,6 @@ main(int argc, char **argv) { usage(); } - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; while ((ch = isc_commandline_parse(argc, argv, "E:fK:rRhv:V")) != -1) { @@ -251,7 +249,6 @@ cleanup: if (dir != NULL) { isc_mem_free(isc_g_mctx, dir); } - isc_mem_detach(&isc_g_mctx); return 0; } diff --git a/bin/dnssec/dnssec-settime.c b/bin/dnssec/dnssec-settime.c index 4a747fb0ad7..312706e3142 100644 --- a/bin/dnssec/dnssec-settime.c +++ b/bin/dnssec/dnssec-settime.c @@ -243,8 +243,6 @@ main(int argc, char **argv) { usage(); } - isc_mem_create("default", &isc_g_mctx); - setup_logging(); isc_commandline_errprint = false; @@ -951,7 +949,6 @@ main(int argc, char **argv) { isc_mem_stats(isc_g_mctx, stdout); } isc_mem_free(isc_g_mctx, directory); - isc_mem_detach(&isc_g_mctx); return 0; } diff --git a/bin/dnssec/dnssec-signzone.c b/bin/dnssec/dnssec-signzone.c index b3325f9affd..ff0fd055cf0 100644 --- a/bin/dnssec/dnssec-signzone.c +++ b/bin/dnssec/dnssec-signzone.c @@ -3297,15 +3297,15 @@ main(int argc, char *argv[]) { case 'm': if (strcasecmp(isc_commandline_argument, "record") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); } if (strcasecmp(isc_commandline_argument, "trace") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); } if (strcasecmp(isc_commandline_argument, "usage") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; + isc_mem_debugon(ISC_MEM_DEBUGUSAGE); } break; default: diff --git a/bin/dnssec/dnssec-verify.c b/bin/dnssec/dnssec-verify.c index 795226e5ea7..f0586f710e4 100644 --- a/bin/dnssec/dnssec-verify.c +++ b/bin/dnssec/dnssec-verify.c @@ -184,15 +184,15 @@ main(int argc, char *argv[]) { case 'm': if (strcasecmp(isc_commandline_argument, "record") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); } if (strcasecmp(isc_commandline_argument, "trace") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); } if (strcasecmp(isc_commandline_argument, "usage") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; + isc_mem_debugon(ISC_MEM_DEBUGUSAGE); } break; default: @@ -201,8 +201,6 @@ main(int argc, char *argv[]) { } isc_commandline_reset = true; - isc_mem_create("default", &isc_g_mctx); - isc_commandline_errprint = false; while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) { @@ -332,7 +330,6 @@ main(int argc, char *argv[]) { if (verbose > 10) { isc_mem_stats(isc_g_mctx, stdout); } - isc_mem_detach(&isc_g_mctx); return result == ISC_R_SUCCESS ? 0 : 1; } diff --git a/bin/named/main.c b/bin/named/main.c index a12257f3b9f..7a848284295 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -361,6 +361,8 @@ parse_int(char *arg, const char *desc) { return tmp; } +static unsigned int mem_debugging = 0; + static struct flag_def { const char *name; unsigned int value; @@ -860,7 +862,8 @@ parse_command_line(int argc, char *argv[]) { break; case 'm': set_flags(isc_commandline_argument, mem_debug_flags, - &isc_mem_debugging); + &mem_debugging); + isc_mem_debugon(mem_debugging); break; case 'N': /* Deprecated. */ case 'n': diff --git a/bin/named/server.c b/bin/named/server.c index 8a6f00011e9..771ec91cbe9 100644 --- a/bin/named/server.c +++ b/bin/named/server.c @@ -8918,7 +8918,7 @@ apply_configuration(cfg_parser_t *configparser, cfg_obj_t *config, named_g_memstatistics = cfg_obj_asboolean(obj); } else { named_g_memstatistics = - ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0); + ((isc_mem_debugon(0) & ISC_MEM_DEBUGRECORD) != 0); } obj = NULL; diff --git a/bin/nsupdate/nsupdate.c b/bin/nsupdate/nsupdate.c index 8d8d9a993fd..24990effda4 100644 --- a/bin/nsupdate/nsupdate.c +++ b/bin/nsupdate/nsupdate.c @@ -1002,8 +1002,8 @@ pre_parse_args(int argc, char **argv) { debugging = true; ddebugging = true; memdebugging = true; - isc_mem_debugging = ISC_MEM_DEBUGTRACE | - ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGTRACE | + ISC_MEM_DEBUGRECORD); break; case '4': diff --git a/bin/rndc/rndc.c b/bin/rndc/rndc.c index 74526f2663a..e60f96ce377 100644 --- a/bin/rndc/rndc.c +++ b/bin/rndc/rndc.c @@ -869,7 +869,7 @@ main(int argc, char **argv) { break; case 'M': - isc_mem_debugging = ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); break; case 'm': diff --git a/bin/tests/system/rsabigexponent/bigkey.c b/bin/tests/system/rsabigexponent/bigkey.c index a81efbbfb81..af09f42b770 100644 --- a/bin/tests/system/rsabigexponent/bigkey.c +++ b/bin/tests/system/rsabigexponent/bigkey.c @@ -104,8 +104,6 @@ main(int argc, char **argv) { exit(EXIT_FAILURE); } - isc_mem_create(argv[0], &isc_g_mctx); - logconfig = isc_logconfig_get(); isc_log_settag(logconfig, "bigkey"); @@ -137,7 +135,6 @@ main(int argc, char **argv) { printf("%s\n", filename); dst_key_free(&key); - isc_mem_detach(&isc_g_mctx); return 0; } diff --git a/bin/tests/system/wire-test.c b/bin/tests/system/wire-test.c index 9225d23a5f8..5e0f475cb4b 100644 --- a/bin/tests/system/wire-test.c +++ b/bin/tests/system/wire-test.c @@ -120,15 +120,15 @@ main(int argc, char *argv[]) { case 'm': if (strcasecmp(isc_commandline_argument, "record") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); } if (strcasecmp(isc_commandline_argument, "trace") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGTRACE; + isc_mem_debugon(ISC_MEM_DEBUGTRACE); } if (strcasecmp(isc_commandline_argument, "usage") == 0) { - isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; + isc_mem_debugon(ISC_MEM_DEBUGUSAGE); } break; default: @@ -137,8 +137,6 @@ main(int argc, char *argv[]) { } isc_commandline_reset = true; - isc_mem_create(argv[0], &isc_g_mctx); - while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) { switch (ch) { case 'b': @@ -258,7 +256,6 @@ main(int argc, char *argv[]) { if (printmemstats) { isc_mem_stats(isc_g_mctx, stdout); } - isc_mem_detach(&isc_g_mctx); return 0; } diff --git a/bin/tools/dnstap-read.c b/bin/tools/dnstap-read.c index 4d63fce4262..e226efa45b0 100644 --- a/bin/tools/dnstap-read.c +++ b/bin/tools/dnstap-read.c @@ -55,7 +55,6 @@ #include "dnstap.pb-c.h" -bool memrecord = false; bool printmessage = false; bool hexmessage = false; bool yaml = false; @@ -347,8 +346,7 @@ main(int argc, char *argv[]) { while ((ch = isc_commandline_parse(argc, argv, "mptxy")) != -1) { switch (ch) { case 'm': - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; - memrecord = true; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); break; case 'p': printmessage = true; @@ -375,8 +373,6 @@ main(int argc, char *argv[]) { fatal("no file specified"); } - isc_mem_create("default", &isc_g_mctx); - CHECKM(dns_dt_open(argv[0], dns_dtmode_file, isc_g_mctx, &handle), "dns_dt_openfile"); @@ -425,7 +421,6 @@ cleanup: if (message != NULL) { dns_message_detach(&message); } - isc_mem_detach(&isc_g_mctx); exit(rv); } diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index 2f25279ba4f..68a6beefeef 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -1818,8 +1818,8 @@ preparse_args(int argc, char **argv) { while (strpbrk(option, single_dash_opts) == &option[0]) { switch (option[0]) { case 'm': - isc_mem_debugging = ISC_MEM_DEBUGTRACE | - ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGTRACE | + ISC_MEM_DEBUGRECORD); break; case '4': if (ipv6only) { diff --git a/bin/tools/named-journalprint.c b/bin/tools/named-journalprint.c index 56f2e2afff1..0678b03f106 100644 --- a/bin/tools/named-journalprint.c +++ b/bin/tools/named-journalprint.c @@ -91,7 +91,6 @@ main(int argc, char **argv) { } file = argv[0]; - isc_mem_create("default", &isc_g_mctx); setup_logging(stderr); if (upgrade) { @@ -110,6 +109,6 @@ main(int argc, char **argv) { fprintf(stderr, "%s\n", isc_result_totext(result)); } } - isc_mem_detach(&isc_g_mctx); + return result != ISC_R_SUCCESS ? 1 : 0; } diff --git a/bin/tools/named-makejournal.c b/bin/tools/named-makejournal.c index 8ac42856d16..c18da012e28 100644 --- a/bin/tools/named-makejournal.c +++ b/bin/tools/named-makejournal.c @@ -128,7 +128,7 @@ main(int argc, char **argv) { usage(0); break; case 'm': - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; + isc_mem_debugon(ISC_MEM_DEBUGRECORD); break; default: usage(1); @@ -151,8 +151,6 @@ main(int argc, char **argv) { journal = (const char *)jbuf; } - isc_mem_create("default", &isc_g_mctx); - logconfig = isc_logconfig_get(); isc_log_createandusechannel( logconfig, "default_stderr", ISC_LOG_TOFILEDESC, @@ -218,9 +216,5 @@ cleanup: dns_db_detach(&olddb); } - if (isc_g_mctx != NULL) { - isc_mem_detach(&isc_g_mctx); - } - return (result != ISC_R_SUCCESS) ? 1 : 0; } diff --git a/bin/tools/named-rrchecker.c b/bin/tools/named-rrchecker.c index ac12752aafa..99a0ba4dfac 100644 --- a/bin/tools/named-rrchecker.c +++ b/bin/tools/named-rrchecker.c @@ -59,9 +59,6 @@ cleanup(void) { isc_lex_close(lex); isc_lex_destroy(&lex); } - if (isc_g_mctx != NULL) { - isc_mem_detach(&isc_g_mctx); - } } ISC_NORETURN static void @@ -169,7 +166,6 @@ main(int argc, char *argv[]) { exit(EXIT_SUCCESS); } - isc_mem_create("default", &isc_g_mctx); isc_lex_create(isc_g_mctx, 256, &lex); /* diff --git a/lib/isc/crypto.c b/lib/isc/crypto.c index 186149d60eb..9e4347794f7 100644 --- a/lib/isc/crypto.c +++ b/lib/isc/crypto.c @@ -253,6 +253,7 @@ isc__crypto_initialize(void) { uint64_t opts = OPENSSL_INIT_LOAD_CONFIG; isc_mem_create("OpenSSL", &isc__crypto_mctx); + isc_mem_setdebugging(isc__crypto_mctx, 0); isc_mem_setdestroycheck(isc__crypto_mctx, false); #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x30000000L diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 8a4415df440..4792ed3d6bd 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -35,7 +35,6 @@ #define ISC_MEM_TRACKLINES 0 #endif /* ifndef ISC_MEM_TRACKLINES */ -extern unsigned int isc_mem_debugging; extern unsigned int isc_mem_defaultflags; /*@{*/ @@ -45,14 +44,10 @@ extern unsigned int isc_mem_defaultflags; #define ISC_MEM_DEBUGALL \ (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGUSAGE) /*!< - * The variable isc_mem_debugging holds a set of flags for - * turning certain memory debugging options on or off at - * runtime. It is initialized to the value ISC_MEM_DEGBUGGING, - * which is 0 by default but may be overridden at compile time. - * The following flags can be specified: + * The following memory debugging options can be specified: * * \li #ISC_MEM_DEBUGTRACE - * Log each allocation and free to isc_lctx. + * Log each allocation and free. * * \li #ISC_MEM_DEBUGRECORD * Remember each allocation, and match them up on free. @@ -61,6 +56,15 @@ extern unsigned int isc_mem_defaultflags; * \li #ISC_MEM_DEBUGUSAGE * Every time the memory usage is greater (lower) than hi_water * (lo_water) mark, print the current inuse memory. + * + * These flags are set in the static variable mem_debugging. + * When a new memory context is created, its debugging flags + * are set to the current value of mem_debugging. + * + * By default, no flags are set. This can be overridden by changing + * ISC_MEM_DEBUGGING in mem.c. The flags can be activated at runtime by + * setting environment variables (for example, "ISC_MEM_DEBUGRECORD=1") + * or by calling isc_mem_debugon() (see below). */ /*@}*/ @@ -217,7 +221,6 @@ extern volatile void *isc__mem_malloc; #endif void isc__mem_create(const char *name, isc_mem_t **_ISC_MEM_FLARG); - /*!< * \brief Create a memory context. * @@ -225,6 +228,33 @@ isc__mem_create(const char *name, isc_mem_t **_ISC_MEM_FLARG); * mctxp != NULL && *mctxp == NULL */ /*@}*/ +unsigned int +isc_mem_debugon(unsigned int debugging); +unsigned int +isc_mem_debugoff(unsigned int debugging); +/*!< + * Set or clear debugging the flags for the main memory context + * (isc_g_mctx), and the default debugging flags for all memory + * contexts yet to be created (mem_debugging). + * + * Note: These are bitwise operations. To clear the existing flags + * before setting new ones, use isc_mem_debugoff(ISC_MEM_DEBUGALL). + * + * Returns: + * \li the previous value of the debugging flags + */ + +void +isc_mem_setdebugging(isc_mem_t *ctx, unsigned int debugging); +/*!< + * Set the debugging flags for a single memory context. + * + * Note: This is an assignemnt, not a bitwise operation. + * + * Requires: + * \li 'ctx' valid memory context without active allocation. + */ + #if ISC_MEM_TRACE #define isc_mem_ref(ptr) isc_mem__ref(ptr, __func__, __FILE__, __LINE__) #define isc_mem_unref(ptr) isc_mem__unref(ptr, __func__, __FILE__, __LINE__) diff --git a/lib/isc/log.c b/lib/isc/log.c index 5aa98f684c4..d46054f4344 100644 --- a/lib/isc/log.c +++ b/lib/isc/log.c @@ -1552,6 +1552,7 @@ isc__log_initialize(void) { isc_mem_t *mctx = NULL; isc_mem_create("log", &mctx); + isc_mem_setdebugging(mctx, 0); isc__lctx = isc_mem_get(mctx, sizeof(*isc__lctx)); *isc__lctx = (isc_log_t){ diff --git a/lib/isc/managers.c b/lib/isc/managers.c index 55020fdecf0..a01a8bac7dc 100644 --- a/lib/isc/managers.c +++ b/lib/isc/managers.c @@ -19,7 +19,6 @@ void isc_managers_create(uint32_t workers) { - isc_mem_create("default", &isc_g_mctx); isc_loopmgr_create(isc_g_mctx, workers); isc_netmgr_create(isc_g_mctx); isc_rwlock_setworkers(workers); @@ -32,5 +31,4 @@ isc_managers_destroy(void) { */ isc_netmgr_destroy(); isc_loopmgr_destroy(); - isc_mem_detach(&isc_g_mctx); } diff --git a/lib/isc/mem.c b/lib/isc/mem.c index b424b3faeb3..f9e807f12b4 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -35,6 +35,7 @@ #include #include #include +#include #ifdef HAVE_LIBXML2 #include @@ -64,7 +65,8 @@ #ifndef ISC_MEM_DEBUGGING #define ISC_MEM_DEBUGGING 0 #endif /* ifndef ISC_MEM_DEBUGGING */ -unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING; + +static unsigned int mem_debugging = ISC_MEM_DEBUGGING; unsigned int isc_mem_defaultflags = ISC_MEMFLAG_DEFAULT; volatile void *isc__mem_malloc = mallocx; @@ -384,6 +386,23 @@ mem_putstats(isc_mem_t *ctx, size_t size) { * Private. */ +static bool +debugging_enabled(const char *name) { + char env_buf[256]; + size_t env_size = sizeof(env_buf); + + int r = uv_os_getenv(name, env_buf, &env_size); + switch (r) { + case 0: + return true; + case UV_ENOENT: + return false; + default: + UV_RUNTIME_CHECK(uv_os_getenv, r); + UNREACHABLE(); + } +} + void isc__mem_initialize(void) { /* @@ -395,6 +414,20 @@ isc__mem_initialize(void) { isc_mutex_init(&contextslock); ISC_LIST_INIT(contexts); + + if (debugging_enabled("ISC_MEM_DEBUGTRACE")) { + mem_debugging |= ISC_MEM_DEBUGTRACE; + } + + if (debugging_enabled("ISC_MEM_DEBUGRECORD")) { + mem_debugging |= ISC_MEM_DEBUGRECORD; + } + + if (debugging_enabled("ISC_MEM_DEBUGUSAGE")) { + mem_debugging |= ISC_MEM_DEBUGUSAGE; + } + + isc_mem_create("default", &isc_g_mctx); } void @@ -403,6 +436,8 @@ isc__mem_shutdown(void) { rcu_barrier(); + isc_mem_detach(&isc_g_mctx); + isc__mem_checkdestroyed(); LOCK(&contextslock); @@ -414,6 +449,40 @@ isc__mem_shutdown(void) { } } +void +isc_mem_setdebugging(isc_mem_t *ctx, unsigned int debugging) { + REQUIRE(VALID_CONTEXT(ctx)); + REQUIRE(isc_mem_inuse(ctx) == 0); + + ctx->debugging = debugging; +} + +unsigned int +isc_mem_debugon(unsigned int debugging) { + unsigned int old_mem_debugging = mem_debugging; + + if (debugging != 0) { + mem_debugging |= debugging; + + isc_mem_setdebugging(isc_g_mctx, mem_debugging); + } + + return old_mem_debugging; +} + +unsigned int +isc_mem_debugoff(unsigned int debugging) { + unsigned int old_mem_debugging = mem_debugging; + + if (debugging != 0) { + mem_debugging &= ~debugging; + + isc_mem_setdebugging(isc_g_mctx, mem_debugging); + } + + return old_mem_debugging; +} + static void mem_create(const char *name, isc_mem_t **ctxp, unsigned int debugging, unsigned int flags, unsigned int jemalloc_flags) { @@ -845,7 +914,7 @@ isc_mem_isovermem(isc_mem_t *ctx) { return false; } - if ((isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0) { + if ((ctx->debugging & ISC_MEM_DEBUGUSAGE) != 0) { fprintf(stderr, "overmem %s mctx %p inuse %zu hi_water %zu\n", ctx->name, ctx, inuse, hiwater); @@ -865,7 +934,7 @@ isc_mem_isovermem(isc_mem_t *ctx) { return true; } - if ((isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0) { + if ((ctx->debugging & ISC_MEM_DEBUGUSAGE) != 0) { fprintf(stderr, "overmem %s mctx %p inuse %zu lo_water %zu\n", ctx->name, ctx, inuse, lowater); @@ -1158,7 +1227,7 @@ isc__mem_checkdestroyed(void) { LOCK(&contextslock); if (!ISC_LIST_EMPTY(contexts)) { #if ISC_MEM_TRACKLINES - if ((isc_mem_debugging & TRACE_OR_RECORD) != 0) { + if ((mem_debugging & TRACE_OR_RECORD) != 0) { print_contexts(file); } #endif /* if ISC_MEM_TRACKLINES */ @@ -1378,9 +1447,9 @@ error: void isc__mem_create(const char *name, isc_mem_t **mctxp FLARG) { - mem_create(name, mctxp, isc_mem_debugging, isc_mem_defaultflags, 0); + mem_create(name, mctxp, mem_debugging, isc_mem_defaultflags, 0); #if ISC_MEM_TRACKLINES - if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { + if ((mem_debugging & ISC_MEM_DEBUGTRACE) != 0) { fprintf(stderr, "create mctx %p func %s file %s line %u\n", *mctxp, func, file, line); } diff --git a/lib/isc/thread.c b/lib/isc/thread.c index 8bc0e94ad48..5841a467fea 100644 --- a/lib/isc/thread.c +++ b/lib/isc/thread.c @@ -48,7 +48,7 @@ static struct call_rcu_data *isc__thread_call_rcu_data = NULL; /* * We can't use isc_mem API here, because it's called too early and the - * isc_mem_debugging flags can be changed later causing mismatch between flags + * memory debugging flags can be changed later causing mismatch between flags * used for isc_mem_get() and isc_mem_put(). */ diff --git a/lib/isc/uv.c b/lib/isc/uv.c index 59330e2d407..f0dcd51212d 100644 --- a/lib/isc/uv.c +++ b/lib/isc/uv.c @@ -133,6 +133,7 @@ isc__uv_initialize(void) { #if UV_VERSION_HEX >= UV_VERSION(1, 38, 0) int r; isc_mem_create("uv", &isc__uv_mctx); + isc_mem_setdebugging(isc__uv_mctx, 0); isc_mem_setdestroycheck(isc__uv_mctx, false); r = uv_replace_allocator(isc__uv_malloc, isc__uv_realloc, diff --git a/lib/isc/xml.c b/lib/isc/xml.c index 664a6e8fe81..2bfd0e94575 100644 --- a/lib/isc/xml.c +++ b/lib/isc/xml.c @@ -53,6 +53,7 @@ isc__xml_initialize(void) { #ifdef HAVE_LIBXML2 #ifndef LIBXML_HAS_DEPRECATED_MEMORY_ALLOCATION_FUNCTIONS isc_mem_create("libxml2", &isc__xml_mctx); + isc_mem_setdebugging(isc__xml_mctx, 0); isc_mem_setdestroycheck(isc__xml_mctx, false); RUNTIME_CHECK(xmlMemSetup(isc__xml_free, isc__xml_malloc, diff --git a/tests/bench/compress.c b/tests/bench/compress.c index 59b8759c4d5..14ac7090980 100644 --- a/tests/bench/compress.c +++ b/tests/bench/compress.c @@ -42,9 +42,6 @@ main(void) { isc_result_t result; isc_buffer_t buf; - isc_mem_t *mctx = NULL; - isc_mem_create("test", &mctx); - static dns_fixedname_t fixedname[65536]; unsigned int count = 0; @@ -76,14 +73,14 @@ main(void) { dns_compress_t cctx; isc_buffer_init(&buf, wire, sizeof(wire)); - dns_compress_init(&cctx, mctx, 0); + dns_compress_init(&cctx, isc_g_mctx, 0); for (unsigned int i = 0; i < count; i++) { dns_name_t *name = dns_fixedname_name(&fixedname[i]); result = dns_name_towire(name, &cctx, &buf); if (result == ISC_R_NOSPACE) { dns_compress_invalidate(&cctx); - dns_compress_init(&cctx, mctx, 0); + dns_compress_init(&cctx, isc_g_mctx, 0); isc_buffer_init(&buf, wire, sizeof(wire)); } else { CHECKRESULT(result, "dns_name_towire"); @@ -100,7 +97,5 @@ main(void) { printf("names %u\n", count); - isc_mem_detach(&mctx); - return 0; } diff --git a/tests/bench/load-names.c b/tests/bench/load-names.c index fcf9ddf6cf2..210f13ec956 100644 --- a/tests/bench/load-names.c +++ b/tests/bench/load-names.c @@ -422,8 +422,6 @@ main(int argc, char *argv[]) { isc_rwlock_init(&rwl); - isc_mem_create("test", &isc_g_mctx); - if (argc != 2) { fprintf(stderr, "usage: load-names \n"); @@ -493,18 +491,16 @@ main(int argc, char *argv[]) { "---------- | ---------- | ---------- |\n"); for (struct fun *fun = fun_list; fun->name != NULL; fun++) { - isc_mem_t *mem = NULL; void *map = NULL; - isc_mem_create("test", &mem); - map = fun->new(mem); + map = fun->new(isc_g_mctx); size_t nitems = lines / (nthreads + 1); isc_barrier_init(&barrier, nthreads); isc_time_t t0 = isc_time_now_hires(); - size_t m0 = isc_mem_inuse(mem); + size_t m0 = isc_mem_inuse(isc_g_mctx); for (size_t i = 0; i < nthreads; i++) { threads[i] = (struct thread_s){ @@ -526,13 +522,13 @@ main(int argc, char *argv[]) { d1 += threads[i].d1; } - size_t m1 = isc_mem_inuse(mem); + size_t m1 = isc_mem_inuse(isc_g_mctx); rcu_barrier(); isc_time_t t1 = isc_time_now_hires(); uint64_t d3 = isc_time_microdiff(&t1, &t0); - size_t m2 = isc_mem_inuse(mem); + size_t m2 = isc_mem_inuse(isc_g_mctx); printf("%10s | %10zu | %10.4f | %10.4f | %10.4f | " "%10.4f | %10.4f |\n", diff --git a/tests/bench/qp-dump.c b/tests/bench/qp-dump.c index 1ed80fad105..7d362ec5292 100644 --- a/tests/bench/qp-dump.c +++ b/tests/bench/qp-dump.c @@ -162,8 +162,6 @@ main(int argc, char *argv[]) { exit(EXIT_SUCCESS); } - isc_mem_create("test", &isc_g_mctx); - filename = argv[0]; result = isc_file_getsize(filename, &fileoff); if (result != ISC_R_SUCCESS) { diff --git a/tests/bench/qplookups.c b/tests/bench/qplookups.c index 966fabf6295..1c142c123b3 100644 --- a/tests/bench/qplookups.c +++ b/tests/bench/qplookups.c @@ -197,8 +197,6 @@ main(int argc, char **argv) { usage(); } - isc_mem_create("test", &isc_g_mctx); - dns_qp_create(isc_g_mctx, &methods, NULL, &qp); start = isc_time_monotonic(); diff --git a/tests/bench/qpmulti.c b/tests/bench/qpmulti.c index cf7243204b3..fb5f38c9611 100644 --- a/tests/bench/qpmulti.c +++ b/tests/bench/qpmulti.c @@ -854,8 +854,6 @@ setup_tickers(isc_mem_t *mctx) { int main(void) { - isc_mem_t *mctx = NULL; - setlinebuf(stdout); uint32_t nloops; @@ -868,20 +866,18 @@ main(void) { } INSIST(nloops > 1); - isc_mem_create("test", &mctx); - isc_mem_setdestroycheck(mctx, true); + isc_mem_setdestroycheck(isc_g_mctx, true); init_logging(); - init_items(mctx); + init_items(isc_g_mctx); - isc_loopmgr_create(mctx, nloops); - setup_tickers(mctx); + isc_loopmgr_create(isc_g_mctx, nloops); + setup_tickers(isc_g_mctx); isc_loop_setup(isc_loop_main(), startup, NULL); isc_loopmgr_run(); isc_loopmgr_destroy(); - isc_mem_free(mctx, item); + isc_mem_free(isc_g_mctx, item); isc_mem_checkdestroyed(stdout); - isc_mem_detach(&mctx); return 0; } diff --git a/tests/isc/mem_test.c b/tests/isc/mem_test.c index 13d73dfc4e9..a19a8ee9649 100644 --- a/tests/isc/mem_test.c +++ b/tests/isc/mem_test.c @@ -182,7 +182,6 @@ ISC_RUN_TEST_IMPL(isc_mem_inuse) { ssize_t diff; void *ptr; - mctx = NULL; isc_mem_create("test", &mctx); before = isc_mem_inuse(mctx); @@ -334,18 +333,19 @@ ISC_RUN_TEST_IMPL(isc_mem_noflags) { char buf[4096], *p; FILE *f; void *ptr; + unsigned int debugging; result = isc_stdio_open("mem.output", "w", &f); assert_int_equal(result, ISC_R_SUCCESS); - isc_mem_debugging = 0; + debugging = isc_mem_debugoff(ISC_MEM_DEBUGALL); isc_mem_create("test", &mctx); ptr = isc_mem_get(mctx, 2048); assert_non_null(ptr); isc__mem_printactive(mctx, f); isc_mem_put(mctx, ptr, 2048); isc_mem_detach(&mctx); - isc_mem_debugging = ISC_MEM_DEBUGRECORD; + isc_mem_debugon(debugging); isc_stdio_close(f); memset(buf, 0, sizeof(buf)); @@ -354,7 +354,7 @@ ISC_RUN_TEST_IMPL(isc_mem_noflags) { result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL); assert_int_equal(result, ISC_R_EOF); isc_stdio_close(f); - isc_file_remove("mem.output"); + // isc_file_remove("mem.output"); buf[sizeof(buf) - 1] = 0; @@ -433,19 +433,22 @@ ISC_RUN_TEST_IMPL(isc_mem_traceflag) { char buf[4096], *p; FILE *f; void *ptr; + unsigned int debugging; /* redirect stderr so we can check trace output */ f = freopen("mem.output", "w", stderr); assert_non_null(f); - isc_mem_debugging = ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGTRACE; + debugging = isc_mem_debugoff(ISC_MEM_DEBUGALL); + isc_mem_debugon(ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGTRACE); isc_mem_create("test", &mctx); ptr = isc_mem_get(mctx, 2048); assert_non_null(ptr); isc__mem_printactive(mctx, f); isc_mem_put(mctx, ptr, 2048); isc_mem_detach(&mctx); - isc_mem_debugging = ISC_MEM_DEBUGRECORD; + isc_mem_debugoff(ISC_MEM_DEBUGALL); + isc_mem_debugon(debugging); isc_stdio_close(f); memset(buf, 0, sizeof(buf)); diff --git a/tests/libtest/isc.c b/tests/libtest/isc.c index 5ababa92ac8..5b6334a64d3 100644 --- a/tests/libtest/isc.c +++ b/tests/libtest/isc.c @@ -67,16 +67,13 @@ setup_workers(void **state ISC_ATTR_UNUSED) { int setup_mctx(void **state ISC_ATTR_UNUSED) { - isc_mem_debugging |= ISC_MEM_DEBUGRECORD; - isc_mem_create("test", &isc_g_mctx); + isc_mem_debugon(ISC_MEM_DEBUGRECORD); return 0; } int teardown_mctx(void **state ISC_ATTR_UNUSED) { - isc_mem_detach(&isc_g_mctx); - return 0; }