]> git.ozlabs.org Git - ccan/commitdiff
mem: add memcheck() for valgrind.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 24 Sep 2015 02:19:40 +0000 (11:49 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 24 Sep 2015 02:19:40 +0000 (11:49 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/mem/mem.h
ccan/mem/test/api-memcheck.c [new file with mode: 0644]
ccan/mem/test/compile_fail-memcheck.c [new file with mode: 0644]

index dd66cc62cd8cce990aeac17da41962aa2bf74a74..68a4aa3c18885c207a9fd11828bc5174bead6905 100644 (file)
@@ -227,4 +227,39 @@ static inline bool memoverlaps(const void *a_, size_t al,
  */
 void memswap(void *a, void *b, size_t n);
 
  */
 void memswap(void *a, void *b, size_t n);
 
+#if HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+static inline void *memcheck_(const void *data, size_t len)
+{
+       VALGRIND_CHECK_MEM_IS_DEFINED(data, len);
+       return (void *)data;
+}
+#else
+static inline void *memcheck_(const void *data, size_t len)
+{
+       return (void *)data;
+}
+#endif
+
+#if HAVE_TYPEOF
+/**
+ * memcheck - check that a memory region is initialized
+ * @data: start of region
+ * @len: length in bytes
+ *
+ * When running under valgrind, this causes an error to be printed
+ * if the entire region is not defined.  Otherwise valgrind only
+ * reports an error when an undefined value is used for a branch, or
+ * written out.
+ *
+ * Example:
+ *     // Search for space, but make sure it's all initialized.
+ *     if (memchr(memcheck(somebytes, bytes_len), ' ', bytes_len)) {
+ *             printf("space was found!\n");
+ *     }
+ */
+#define memcheck(data, len) ((__typeof__((data)+0))memcheck_((data), (len)))
+#else
+#define memcheck(data, len) memcheck_((data), (len))
+#endif
 #endif /* CCAN_MEM_H */
 #endif /* CCAN_MEM_H */
diff --git a/ccan/mem/test/api-memcheck.c b/ccan/mem/test/api-memcheck.c
new file mode 100644 (file)
index 0000000..ca8a111
--- /dev/null
@@ -0,0 +1,28 @@
+#include "config.h"
+
+#include <assert.h>
+
+#include <ccan/mem/mem.h>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       char haystack[] = "abcd\0efgh";
+       char *p;
+       const char *pc;
+
+       /* This is how many tests you plan to run */
+       plan_tests(4);
+
+       p = memcheck(haystack, sizeof(haystack));
+       ok1(p == haystack);
+       pc = memcheck(haystack, sizeof(haystack));
+       ok1(pc == haystack);
+       p = memcheck(p, sizeof(haystack));
+       ok1(p == haystack);
+       pc = memcheck(pc, sizeof(haystack));
+       ok1(pc == haystack);
+
+       /* This exits depending on whether all tests passed */
+       return exit_status();
+}
diff --git a/ccan/mem/test/compile_fail-memcheck.c b/ccan/mem/test/compile_fail-memcheck.c
new file mode 100644 (file)
index 0000000..32620f1
--- /dev/null
@@ -0,0 +1,23 @@
+#include "config.h"
+
+#include <assert.h>
+#include <ccan/mem/mem.h>
+
+int main(void)
+{
+       const char *haystack = "abcd\0efgh";
+       char *p;
+
+#ifdef FAIL
+#if !HAVE_TYPEOF
+#error "Can't fail without typeof"
+#else
+       /* Should catch const discard errors. */
+       p = memcheck(haystack, sizeof(haystack));
+#endif
+#else
+       p = memcheck((char *)haystack, sizeof(haystack));
+#endif
+
+       return p == haystack ? 0 : 1;
+}