]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use assertions to check for failed allocations
authorOndřej Surý <ondrej@isc.org>
Wed, 29 Sep 2021 08:58:58 +0000 (10:58 +0200)
committerOndřej Surý <ondrej@isc.org>
Thu, 30 Sep 2021 11:54:55 +0000 (13:54 +0200)
It was discovered that named could crash due to a segmentation fault
when jemalloc was in use and memory allocation failed.  This was not
intended to happen as jemalloc's "xmalloc" option was set to "true" in
the "malloc_conf" configuration variable.  However, that variable was
only set after jemalloc was already done with parsing it, which
effectively caused setting that variable to have no effect.

While investigating this issue, it was also discovered that enabling the
"xmalloc" option makes jemalloc use a slow processing path, decreasing
its performance by about 25%. [1]

Additionally, further testing (carried out after fixing the way
"malloc_conf" was set) revealed that the non-default configuration
options do not have any measurable effect on either authoritative or
recursive DNS server performance.

Replace code setting various jemalloc options to non-default values with
assertion checks of mallocx()/rallocx() return values.

[1] https://github.com/jemalloc/jemalloc/pull/523

lib/isc/jemalloc_shim.h
lib/isc/mem.c

index 95dbf0dc15369e5716fb0219842f9ad859eea22c..b207eabc8901e95bcdb61489d38c29d2a68a96e9 100644 (file)
@@ -26,9 +26,8 @@ const char *malloc_conf = NULL;
 static inline void *
 mallocx(size_t size, int flags) {
        UNUSED(flags);
-       void *__ptr = malloc(size);
-       REQUIRE(__ptr != NULL);
-       return (__ptr);
+
+       return (malloc(size));
 }
 
 static inline void
@@ -44,10 +43,7 @@ rallocx(void *ptr, size_t size, int flags) {
        UNUSED(flags);
        REQUIRE(size != 0);
 
-       void *__ptr = realloc(ptr, size);
-       REQUIRE(__ptr != NULL);
-
-       return (__ptr);
+       return (realloc(ptr, size));
 }
 
 #ifdef HAVE_MALLOC_SIZE
@@ -90,7 +86,7 @@ mallocx(size_t size, int flags) {
        UNUSED(flags);
 
        size_info *si = malloc(size + sizeof(*si));
-       REQUIRE(si != NULL);
+       INSIST(si != NULL);
 
        si->size = size;
        ptr = &si[1];
@@ -124,7 +120,7 @@ rallocx(void *ptr, size_t size, int flags) {
        UNUSED(flags);
 
        si = realloc(si, size + sizeof(*si));
-       REQUIRE(si != NULL);
+       INSIST(si != NULL);
 
        si->size = size;
        ptr = &si[1];
index 67235bf41fe7d723c7b8c15410089a24078bf254..a75524ece39dfbe59dfecfa50fe24c0165ca25a2 100644 (file)
@@ -339,6 +339,7 @@ mem_get(isc_mem_t *ctx, size_t size) {
        ADJUST_ZERO_ALLOCATION_SIZE(size);
 
        ret = mallocx(size, 0);
+       INSIST(ret != NULL);
 
        if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
                memset(ret, 0xbe, size); /* Mnemonic for "beef". */
@@ -368,6 +369,7 @@ mem_realloc(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size) {
        ADJUST_ZERO_ALLOCATION_SIZE(new_size);
 
        new_ptr = rallocx(old_ptr, new_size, 0);
+       INSIST(new_ptr != NULL);
 
        if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
                ssize_t diff_size = new_size - old_size;
@@ -424,9 +426,6 @@ mem_putstats(isc_mem_t *ctx, void *ptr, size_t size) {
 
 static void
 mem_initialize(void) {
-       malloc_conf = "xmalloc:true,background_thread:true,metadata_thp:auto,"
-                     "dirty_decay_ms:30000,muzzy_decay_ms:30000";
-
        isc_mutex_init(&contextslock);
        ISC_LIST_INIT(contexts);
        totallost = 0;
@@ -456,6 +455,7 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) {
        REQUIRE(ctxp != NULL && *ctxp == NULL);
 
        ctx = mallocx(sizeof(*ctx), 0);
+       INSIST(ctx != NULL);
 
        *ctx = (isc_mem_t){
                .magic = MEM_MAGIC,
@@ -488,6 +488,8 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) {
 
                ctx->debuglist =
                        mallocx((DEBUG_TABLE_COUNT * sizeof(debuglist_t)), 0);
+               INSIST(ctx->debuglist != NULL);
+
                for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
                        ISC_LIST_INIT(ctx->debuglist[i]);
                }