From 1a4bd09e63df2300845fcb9bfa150f28943b9aea Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 25 Dec 2019 17:00:16 +0100 Subject: [PATCH] also BADFOOD on realloc --- contrib/build-common | 2 +- src/util/.gitignore | 1 + src/util/common_allocation.c | 27 +++++++++++++++++++++++-- src/util/perf_malloc.c | 39 ++++++++++++++++++++++++++++++++---- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/contrib/build-common b/contrib/build-common index 1915a74bb..6ac60bd0b 160000 --- a/contrib/build-common +++ b/contrib/build-common @@ -1 +1 @@ -Subproject commit 1915a74bbb4cd2ae9bc541a382dfebc37064a2fd +Subproject commit 6ac60bd0b1f96324b4175fa03aaf9780ed8efb47 diff --git a/src/util/.gitignore b/src/util/.gitignore index dfa6c7947..01ebcc834 100644 --- a/src/util/.gitignore +++ b/src/util/.gitignore @@ -74,3 +74,4 @@ test_regex test_tun gnunet-timeout python27_location +perf_malloc diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c index 137af7b85..35c557000 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c @@ -264,6 +264,30 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber) n += sizeof(size_t); ptr = &((size_t *) ptr)[-1]; mem_used = mem_used - *((size_t *) ptr) + n; +#endif +#if defined(M_SIZE) +#if ENABLE_POISONING + { + uint64_t *base = ptr; + size_t s = M_SIZE (ptr); + + if (s > n) + { + const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL); + char *cbase = ptr; + + GNUNET_memcpy (&cbase[n], + &baadfood, + GNUNET_MIN (8 - (n % 8), + s - n)); + for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++) + base[i] = baadfood; + GNUNET_memcpy (&base[s / 8], + &baadfood, + s % 8); + } + } +#endif #endif ptr = realloc (ptr, n); if ((NULL == ptr) && (n > 0)) @@ -316,9 +340,8 @@ GNUNET_xfree_ (void *ptr, const char *filename, int linenumber) const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL); uint64_t *base = ptr; size_t s = M_SIZE (ptr); - size_t i; - for (i = 0; i < s / 8; i++) + for (size_t i = 0; i < s / 8; i++) base[i] = baadfood; GNUNET_memcpy (&base[s / 8], &baadfood, s % 8); } diff --git a/src/util/perf_malloc.c b/src/util/perf_malloc.c index 727e15979..6582505c8 100644 --- a/src/util/perf_malloc.c +++ b/src/util/perf_malloc.c @@ -28,13 +28,12 @@ #include static uint64_t -perfMalloc () +perf_malloc () { - size_t i; uint64_t ret; ret = 0; - for (i = 1; i < 1024 * 1024; i += 1024) + for (size_t i = 1; i < 1024 * 1024; i += 1024) { ret += i; GNUNET_free (GNUNET_malloc (i)); @@ -43,6 +42,32 @@ perfMalloc () } +static uint64_t +perf_realloc () +{ + uint64_t ret; + + ret = 0; + for (size_t i = 10; i < 1024 * 1024 / 5; i += 1024) + { + char *ptr; + + ret += i; + ptr = GNUNET_malloc (i); + memset (ptr, 1, i); + ptr = GNUNET_realloc (ptr, i + 5); + for (size_t j=0;j