]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add mempool get/put tracking with AddressSanitizer
authorOndřej Surý <ondrej@sury.org>
Thu, 25 Feb 2021 10:08:34 +0000 (11:08 +0100)
committerEvan Hunt <each@isc.org>
Fri, 26 Feb 2021 18:13:17 +0000 (10:13 -0800)
When AddressSanitizer is in use, disable the internal mempool
implementation and redirect the isc_mempool_get to isc_mem_get
(and similarly for isc_mempool_put). This is the method recommended
by the AddressSanitizer authors for tracking allocations and
deallocations instead of custom poison/unpoison code (see
https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning).

lib/isc/mem.c
lib/isc/tests/mem_test.c

index e9156994d2b447b5fc9862f0a03a7fc321708e37..ba634662effcf40a84e4b87d7713b4087407397a 100644 (file)
@@ -1755,6 +1755,63 @@ isc_mempool_associatelock(isc_mempool_t *mpctx0, isc_mutex_t *lock) {
        mpctx->lock = lock;
 }
 
+#if __SANITIZE_ADDRESS__
+void *
+isc__mempool_get(isc_mempool_t *mpctx0 FLARG) {
+       void *item = NULL;
+
+       REQUIRE(VALID_MEMPOOL(mpctx0));
+
+       isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
+       isc_mem_t *mctx = (isc_mem_t *)mpctx->mctx;
+
+       if (mpctx->lock != NULL) {
+               LOCK(mpctx->lock);
+       }
+
+       /*
+        * Don't let the caller go over quota
+        */
+       if (ISC_UNLIKELY(mpctx->allocated >= mpctx->maxalloc)) {
+               goto out;
+       }
+
+       item = isc__mem_get(mctx, mpctx->size FLARG_PASS);
+       mpctx->gets++;
+       mpctx->allocated++;
+
+out:
+       if (mpctx->lock != NULL) {
+               UNLOCK(mpctx->lock);
+       }
+
+       return (item);
+}
+
+void
+isc__mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
+       REQUIRE(VALID_MEMPOOL(mpctx0));
+
+       isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
+       isc_mem_t *mctx = (isc_mem_t *)mpctx->mctx;
+
+       REQUIRE(mem != NULL);
+
+       if (mpctx->lock != NULL) {
+               LOCK(mpctx->lock);
+       }
+
+       INSIST(mpctx->allocated > 0);
+       mpctx->allocated--;
+
+       isc__mem_put(mctx, mem, mpctx->size FLARG_PASS);
+
+       if (mpctx->lock != NULL) {
+               UNLOCK(mpctx->lock);
+       }
+}
+
+#else /* __SANITIZE_ADDRESS__ */
 void *
 isc__mempool_get(isc_mempool_t *mpctx0 FLARG) {
        REQUIRE(VALID_MEMPOOL(mpctx0));
@@ -1890,6 +1947,8 @@ isc__mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
        }
 }
 
+#endif /* __SANITIZE_ADDRESS__ */
+
 /*
  * Quotas
  */
index 3b7c3e9ee1872ba0e504fcc3f22ba02af44f57cd..2fcc8b2688f95a988e68bec734b6e12a6e4552fe 100644 (file)
@@ -106,8 +106,10 @@ isc_mem_test(void **state) {
                items1[i] = NULL;
        }
 
+#if !__SANITIZE_ADDRESS__
        rval = isc_mempool_getfreecount(mp1);
        assert_int_equal(rval, 10);
+#endif /* !__SANITIZE_ADDRESS__ */
 
        rval = isc_mempool_getallocated(mp1);
        assert_int_equal(rval, 19);