]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Simplify Address Sanitizer tweaks in mem.c
authorOndřej Surý <ondrej@isc.org>
Wed, 15 Dec 2021 11:07:22 +0000 (12:07 +0100)
committerOndřej Surý <ondrej@sury.org>
Fri, 17 Dec 2021 13:43:05 +0000 (14:43 +0100)
Previously, whole isc_mempool_get() and isc_mempool_set() would be
replaced by simpler version when run with address sanitizer.

Change the code to limit the fillcount to 1 and freemax to 0.  This
change will make isc_mempool_get() to always allocate and use a single
new item and isc_mempool_put() will always return the item to the
allocator.

lib/isc/mem.c

index f6459df737479238860467ab2042eabab571f016..0fa088a0e1fbf0db66b7062454fa6bb26c88f18a 100644 (file)
@@ -1259,27 +1259,6 @@ isc__mempool_destroy(isc_mempool_t **restrict mpctxp FLARG) {
        isc_mem_put(mpctx->mctx, mpctx, sizeof(isc_mempool_t));
 }
 
-#if __SANITIZE_ADDRESS__
-void *
-isc__mempool_get(isc_mempool_t *restrict mpctx FLARG) {
-       REQUIRE(VALID_MEMPOOL(mpctx));
-
-       mpctx->allocated++;
-       mpctx->gets++;
-
-       return (isc__mem_get(mpctx->mctx, mpctx->size FLARG_PASS));
-}
-
-void
-isc__mempool_put(isc_mempool_t *restrict mpctx, void *mem FLARG) {
-       REQUIRE(VALID_MEMPOOL(mpctx));
-       REQUIRE(mem != NULL);
-
-       mpctx->allocated--;
-       isc__mem_put(mpctx->mctx, mem, mpctx->size FLARG_PASS);
-}
-
-#else /* __SANITIZE_ADDRESS__ */
 void *
 isc__mempool_get(isc_mempool_t *restrict mpctx FLARG) {
        element *restrict item = NULL;
@@ -1290,10 +1269,13 @@ isc__mempool_get(isc_mempool_t *restrict mpctx FLARG) {
 
        if (mpctx->items == NULL) {
                isc_mem_t *mctx = mpctx->mctx;
+#if !__SANITIZE_ADDRESS__
                const size_t fillcount = mpctx->fillcount;
+#else
+               const size_t fillcount = 1;
+#endif
                /*
-                * We need to dip into the well.  Lock the memory
-                * context here and fill up our free list.
+                * We need to dip into the well.  Fill up our free list.
                 */
                for (size_t i = 0; i < fillcount; i++) {
                        item = mem_get(mctx, mpctx->size);
@@ -1328,7 +1310,11 @@ isc__mempool_put(isc_mempool_t *restrict mpctx, void *mem FLARG) {
 
        isc_mem_t *mctx = mpctx->mctx;
        const size_t freecount = mpctx->freecount;
+#if !__SANITIZE_ADDRESS__
        const size_t freemax = mpctx->freemax;
+#else
+       const size_t freemax = 0;
+#endif
 
        INSIST(mpctx->allocated > 0);
        mpctx->allocated--;
@@ -1353,8 +1339,6 @@ isc__mempool_put(isc_mempool_t *restrict mpctx, void *mem FLARG) {
        mpctx->freecount++;
 }
 
-#endif /* __SANITIZE_ADDRESS__ */
-
 /*
  * Quotas
  */
@@ -1363,7 +1347,6 @@ void
 isc_mempool_setfreemax(isc_mempool_t *restrict mpctx,
                       const unsigned int limit) {
        REQUIRE(VALID_MEMPOOL(mpctx));
-
        mpctx->freemax = limit;
 }