summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-02-13 09:10:11 -0500
committerNick Mathewson <nickm@torproject.org>2017-02-27 16:25:10 -0500
commitb923c4dc9f011ab8f62e03211407d042dbe9f92a (patch)
tree2badc258988ec649c0bca6e36e3e4f6cb98b156b /src/common
parent2670844b2b64172fb9b02d5e02f928ceb8f59bf7 (diff)
downloadtor-b923c4dc9f011ab8f62e03211407d042dbe9f92a.tar.gz
tor-b923c4dc9f011ab8f62e03211407d042dbe9f92a.zip
Code to disable memory sentinels for fuzzing
This feature makes it possible to turn off memory sentinels (like those used for safety in buffers.c and memarea.c) when fuzzing, so that we can catch bugs that they would otherwise prevent.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/memarea.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/common/memarea.c b/src/common/memarea.c
index 7d16b702e3..12781e76ea 100644
--- a/src/common/memarea.c
+++ b/src/common/memarea.c
@@ -12,6 +12,9 @@
#include "util.h"
#include "compat.h"
#include "torlog.h"
+#include "container.h"
+
+#ifndef DISABLE_MEMORY_SENTINELS
/** If true, we try to detect any attempts to write beyond the length of a
* memarea. */
@@ -304,3 +307,91 @@ memarea_assert_ok(memarea_t *area)
}
}
+#else
+
+struct memarea_t {
+ smartlist_t *pieces;
+};
+
+memarea_t *
+memarea_new(void)
+{
+ memarea_t *ma = tor_malloc_zero(sizeof(memarea_t));
+ ma->pieces = smartlist_new();
+ return ma;
+}
+void
+memarea_drop_all(memarea_t *area)
+{
+ memarea_clear(area);
+ smartlist_free(area->pieces);
+ tor_free(area);
+}
+void
+memarea_clear(memarea_t *area)
+{
+ SMARTLIST_FOREACH(area->pieces, void *, p, tor_free_(p));
+ smartlist_clear(area->pieces);
+}
+int
+memarea_owns_ptr(const memarea_t *area, const void *ptr)
+{
+ SMARTLIST_FOREACH(area->pieces, const void *, p, if (ptr == p) return 1;);
+ return 0;
+}
+
+void *
+memarea_alloc(memarea_t *area, size_t sz)
+{
+ void *result = tor_malloc(sz);
+ smartlist_add(area->pieces, result);
+ return result;
+}
+
+void *
+memarea_alloc_zero(memarea_t *area, size_t sz)
+{
+ void *result = tor_malloc_zero(sz);
+ smartlist_add(area->pieces, result);
+ return result;
+}
+void *
+memarea_memdup(memarea_t *area, const void *s, size_t n)
+{
+ void *r = memarea_alloc(area, n);
+ memcpy(r, s, n);
+ return r;
+}
+char *
+memarea_strdup(memarea_t *area, const char *s)
+{
+ size_t n = strlen(s);
+ char *r = memarea_alloc(area, n+1);
+ memcpy(r, s, n);
+ r[n] = 0;
+ return r;
+}
+char *
+memarea_strndup(memarea_t *area, const char *s, size_t n)
+{
+ size_t ln = strnlen(s, n);
+ char *r = memarea_alloc(area, ln+1);
+ memcpy(r, s, ln);
+ r[ln] = 0;
+ return r;
+}
+void
+memarea_get_stats(memarea_t *area,
+ size_t *allocated_out, size_t *used_out)
+{
+ (void)area;
+ *allocated_out = *used_out = 128;
+}
+void
+memarea_assert_ok(memarea_t *area)
+{
+ (void)area;
+}
+
+#endif
+