aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-12-25 17:00:16 +0100
committerChristian Grothoff <christian@grothoff.org>2019-12-25 17:01:55 +0100
commit1a4bd09e63df2300845fcb9bfa150f28943b9aea (patch)
treeb50d2ac1b5c845e89e53d54015a70edbf2918504
parent64d6551e3c6608afcd5dfd5ebdf6ad9bbac0b9f5 (diff)
downloadgnunet-1a4bd09e63df2300845fcb9bfa150f28943b9aea.tar.gz
gnunet-1a4bd09e63df2300845fcb9bfa150f28943b9aea.zip
also BADFOOD on realloc
m---------contrib/build-common0
-rw-r--r--src/util/.gitignore1
-rw-r--r--src/util/common_allocation.c27
-rw-r--r--src/util/perf_malloc.c39
4 files changed, 61 insertions, 6 deletions
diff --git a/contrib/build-common b/contrib/build-common
Subproject 1915a74bbb4cd2ae9bc541a382dfebc37064a2f Subproject 6ac60bd0b1f96324b4175fa03aaf9780ed8efb4
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
74test_tun 74test_tun
75gnunet-timeout 75gnunet-timeout
76python27_location 76python27_location
77perf_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
@@ -265,6 +265,30 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
265 ptr = &((size_t *) ptr)[-1]; 265 ptr = &((size_t *) ptr)[-1];
266 mem_used = mem_used - *((size_t *) ptr) + n; 266 mem_used = mem_used - *((size_t *) ptr) + n;
267#endif 267#endif
268#if defined(M_SIZE)
269#if ENABLE_POISONING
270 {
271 uint64_t *base = ptr;
272 size_t s = M_SIZE (ptr);
273
274 if (s > n)
275 {
276 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
277 char *cbase = ptr;
278
279 GNUNET_memcpy (&cbase[n],
280 &baadfood,
281 GNUNET_MIN (8 - (n % 8),
282 s - n));
283 for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++)
284 base[i] = baadfood;
285 GNUNET_memcpy (&base[s / 8],
286 &baadfood,
287 s % 8);
288 }
289 }
290#endif
291#endif
268 ptr = realloc (ptr, n); 292 ptr = realloc (ptr, n);
269 if ((NULL == ptr) && (n > 0)) 293 if ((NULL == ptr) && (n > 0))
270 { 294 {
@@ -316,9 +340,8 @@ GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
316 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL); 340 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
317 uint64_t *base = ptr; 341 uint64_t *base = ptr;
318 size_t s = M_SIZE (ptr); 342 size_t s = M_SIZE (ptr);
319 size_t i;
320 343
321 for (i = 0; i < s / 8; i++) 344 for (size_t i = 0; i < s / 8; i++)
322 base[i] = baadfood; 345 base[i] = baadfood;
323 GNUNET_memcpy (&base[s / 8], &baadfood, s % 8); 346 GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
324 } 347 }
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 @@
28#include <gauger.h> 28#include <gauger.h>
29 29
30static uint64_t 30static uint64_t
31perfMalloc () 31perf_malloc ()
32{ 32{
33 size_t i;
34 uint64_t ret; 33 uint64_t ret;
35 34
36 ret = 0; 35 ret = 0;
37 for (i = 1; i < 1024 * 1024; i += 1024) 36 for (size_t i = 1; i < 1024 * 1024; i += 1024)
38 { 37 {
39 ret += i; 38 ret += i;
40 GNUNET_free (GNUNET_malloc (i)); 39 GNUNET_free (GNUNET_malloc (i));
@@ -43,6 +42,32 @@ perfMalloc ()
43} 42}
44 43
45 44
45static uint64_t
46perf_realloc ()
47{
48 uint64_t ret;
49
50 ret = 0;
51 for (size_t i = 10; i < 1024 * 1024 / 5; i += 1024)
52 {
53 char *ptr;
54
55 ret += i;
56 ptr = GNUNET_malloc (i);
57 memset (ptr, 1, i);
58 ptr = GNUNET_realloc (ptr, i + 5);
59 for (size_t j=0;j<i;j++)
60 GNUNET_assert (1 == ptr[j]);
61 memset (ptr, 6, i + 5);
62 ptr = GNUNET_realloc (ptr, i - 5);
63 for (size_t j=0;j<i-5;j++)
64 GNUNET_assert (6 == ptr[j]);
65 GNUNET_free (ptr);
66 }
67 return ret;
68}
69
70
46int 71int
47main (int argc, char *argv[]) 72main (int argc, char *argv[])
48{ 73{
@@ -50,7 +75,7 @@ main (int argc, char *argv[])
50 uint64_t kb; 75 uint64_t kb;
51 76
52 start = GNUNET_TIME_absolute_get (); 77 start = GNUNET_TIME_absolute_get ();
53 kb = perfMalloc (); 78 kb = perf_malloc ();
54 printf ("Malloc perf took %s\n", 79 printf ("Malloc perf took %s\n",
55 GNUNET_STRINGS_relative_time_to_string ( 80 GNUNET_STRINGS_relative_time_to_string (
56 GNUNET_TIME_absolute_get_duration (start), 81 GNUNET_TIME_absolute_get_duration (start),
@@ -59,6 +84,12 @@ main (int argc, char *argv[])
59 kb / 1024 / (1 84 kb / 1024 / (1
60 + GNUNET_TIME_absolute_get_duration 85 + GNUNET_TIME_absolute_get_duration
61 (start).rel_value_us / 1000LL), "kb/ms"); 86 (start).rel_value_us / 1000LL), "kb/ms");
87 start = GNUNET_TIME_absolute_get ();
88 kb = perf_realloc ();
89 printf ("Realloc perf took %s\n",
90 GNUNET_STRINGS_relative_time_to_string (
91 GNUNET_TIME_absolute_get_duration (start),
92 GNUNET_YES));
62 return 0; 93 return 0;
63} 94}
64 95