This is an automated email from the ASF dual-hosted git repository.
kgiusti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git
The following commit(s) were added to refs/heads/master by this push:
new 94e484f DISPATCH-1225: fill freed memory with a pattern for debugging
94e484f is described below
commit 94e484f964b38891e70b5d74ba9afaf7859dc151
Author: Kenneth Giusti <[email protected]>
AuthorDate: Tue Dec 18 09:45:07 2018 -0500
DISPATCH-1225: fill freed memory with a pattern for debugging
This closes #426
---
include/qpid/dispatch/alloc.h | 13 +++++++++++++
include/qpid/dispatch/alloc_malloc.h | 12 +++++++++---
src/alloc_pool.c | 7 +++----
3 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/include/qpid/dispatch/alloc.h b/include/qpid/dispatch/alloc.h
index 1f6a3c8..3ee4566 100644
--- a/include/qpid/dispatch/alloc.h
+++ b/include/qpid/dispatch/alloc.h
@@ -20,6 +20,19 @@
*/
#include "config.h"
+#include <string.h>
+
+
+#if !defined(NDEBUG)
+#define QD_MEMORY_DEBUG 1
+// when debugging fill allocated/deallocated memory
+// to catch uinitialized access or use after free
+#define QD_MEMORY_FREE 0x99
+#define QD_MEMORY_INIT 0x11
+#define QD_MEMORY_FILL(P,C,S) do { if (P) { memset((P),(C),(S)); } } while (0)
+#else
+#define QD_MEMORY_FILL(P,C,S)
+#endif
#if USE_MEMORY_POOL
#include "alloc_pool.h"
diff --git a/include/qpid/dispatch/alloc_malloc.h
b/include/qpid/dispatch/alloc_malloc.h
index 3ba6cf3..12d683e 100644
--- a/include/qpid/dispatch/alloc_malloc.h
+++ b/include/qpid/dispatch/alloc_malloc.h
@@ -20,6 +20,7 @@
*/
#include <stdint.h>
+#include <string.h>
/**
*@file
@@ -32,9 +33,14 @@
T *new_##T(void); \
void free_##T(T *p)
-#define ALLOC_DEFINE_CONFIG(T,S,A,C) \
- T *new_##T(void) { size_t *a = (A); return (T*) malloc((S)+ (a ? *a : 0));
} \
- void free_##T(T *p) { free(p); } \
+#define ALLOC_DEFINE_CONFIG(T,S,A,C) \
+ T *new_##T(void) { size_t *a = (A); \
+ T *p = malloc((S)+ (a ? *a : 0)); \
+ QD_MEMORY_FILL(p, QD_MEMORY_INIT, (S) + (a ? *a : 0)); \
+ return p; } \
+ void free_##T(T *p) { size_t *a = (A); \
+ QD_MEMORY_FILL(p, QD_MEMORY_FREE, (S) + (a ? *a : 0)); \
+ free(p); } \
void *unused##T
#define ALLOC_DEFINE(T) ALLOC_DEFINE_CONFIG(T, sizeof(T), 0, 0)
diff --git a/src/alloc_pool.c b/src/alloc_pool.c
index 170fd57..28e8da3 100644
--- a/src/alloc_pool.c
+++ b/src/alloc_pool.c
@@ -28,10 +28,6 @@
#include "entity_cache.h"
#include "config.h"
-#if !defined(NDEBUG)
-#define QD_MEMORY_DEBUG 1
-#endif
-
const char *QD_ALLOCATOR_TYPE = "allocator";
typedef struct qd_alloc_type_t qd_alloc_type_t;
@@ -149,6 +145,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t
**tpool)
item->desc = desc;
item->header = PATTERN_FRONT;
*((uint32_t*) ((char*) &item[1] + desc->total_size))= PATTERN_BACK;
+ QD_MEMORY_FILL(&item[1], QD_MEMORY_INIT, desc->total_size);
#endif
return &item[1];
}
@@ -202,6 +199,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t
**tpool)
item->desc = desc;
item->header = PATTERN_FRONT;
*((uint32_t*) ((char*) &item[1] + desc->total_size))= PATTERN_BACK;
+ QD_MEMORY_FILL(&item[1], QD_MEMORY_INIT, desc->total_size);
#endif
return &item[1];
}
@@ -224,6 +222,7 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t
**tpool, char *p)
assert (*((uint32_t*) (p + desc->total_size)) == PATTERN_BACK);
assert (item->desc == desc); // Check for double-free
item->desc = 0;
+ QD_MEMORY_FILL(p, QD_MEMORY_FREE, desc->total_size);
#endif
//
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]