aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Nicolas "Pixel" Noble <pixel@nobis-crew.org>2016-03-30 04:30:44 +0200
committerGravatar Nicolas "Pixel" Noble <pixel@nobis-crew.org>2016-03-30 04:30:44 +0200
commit8677d3f824af560599f3cb16faacfa80492a106c (patch)
tree56be2c24336e2c66b5ee6abff79b62039a55ad6c
parent508e89f4c9e80b62836391e7bacab7aa62df58ca (diff)
Adding memory leak detections for the json fuzzer.
-rw-r--r--test/core/json/fuzzer.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/test/core/json/fuzzer.c b/test/core/json/fuzzer.c
index 65f89e64a4..9f430a0dca 100644
--- a/test/core/json/fuzzer.c
+++ b/test/core/json/fuzzer.c
@@ -38,13 +38,52 @@
#include "src/core/lib/json/json.h"
+static size_t g_total_size = 0;
+static gpr_allocation_functions g_old_allocs;
+
+void *guard_malloc(size_t size) {
+ size_t *ptr;
+ g_total_size += size;
+ ptr = g_old_allocs.malloc(size + sizeof(size));
+ *ptr++ = size;
+ return ptr;
+}
+
+void *guard_realloc(void *ptr, size_t size) {
+ size_t *ptr = vptr;
+ --ptr;
+ g_total_size -= *ptr;
+ ptr = g_old_allocs.realloc(ptr, size + sizeof(size));
+ g_total_size += size;
+ *ptr++ = size;
+ return ptr;
+}
+
+void *guard_free(void *vptr) {
+ size_t *ptr = vptr;
+ --ptr;
+ g_total_size -= *ptr;
+ g_old_allocs.free(ptr);
+}
+
+struct gpr_allocation_functions g_guard_allocs = {
+ guard_malloc,
+ guard_realloc,
+ guard_free
+};
+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
- char *s = gpr_malloc(size);
+ char *s;
+ g_old_allocs = gpr_get_allocation_functions();
+ gpr_set_allocation_functions(g_guard_allocs);
+ s = gpr_malloc(size);
memcpy(s, data, size);
grpc_json *x;
if ((x = grpc_json_parse_string_with_len(s, size))) {
grpc_json_destroy(x);
}
gpr_free(s);
+ gpr_set_allocation_functions(g_old_allocs);
+ GPR_ASSERT(g_total_size == 0);
return 0;
}