summaryrefslogtreecommitdiffstats
path: root/src/sanity.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-05-25 14:19:21 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-05-25 14:30:05 -0400
commit24a67ef8265e5873c0967ded296a9e57ed9f2914 (patch)
treeab48ef165aa5f83465b99354794787914f4dd9a9 /src/sanity.h
parenteef75524aec3910097cb6923c30b898ad98179fe (diff)
downloadbfs-24a67ef8265e5873c0967ded296a9e57ed9f2914.tar.xz
sanity: Add wrappers for sanitizer interfaces
Diffstat (limited to 'src/sanity.h')
-rw-r--r--src/sanity.h89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/sanity.h b/src/sanity.h
new file mode 100644
index 0000000..5696036
--- /dev/null
+++ b/src/sanity.h
@@ -0,0 +1,89 @@
+// Copyright © Tavian Barnes <tavianator@tavianator.com>
+// SPDX-License-Identifier: 0BSD
+
+/**
+ * Sanitizer interface.
+ */
+
+#ifndef BFS_SANITY_H
+#define BFS_SANITY_H
+
+#include "config.h"
+#include <stddef.h>
+
+#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
+# define SANITIZE_ADDRESS true
+#endif
+
+#if __has_feature(memory_sanitizer) || defined(__SANITIZE_MEMORY__)
+# define SANITIZE_MEMORY true
+#endif
+
+#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
+# define SANITIZE_THREAD true
+#endif
+
+// Call macro(ptr, size) or macro(ptr, sizeof(*ptr))
+#define SANITIZE_CALL(...) \
+ SANITIZE_CALL_(__VA_ARGS__, )
+
+#define SANITIZE_CALL_(macro, ptr, ...) \
+ SANITIZE_CALL__(macro, ptr, __VA_ARGS__ sizeof(*(ptr)), )
+
+#define SANITIZE_CALL__(macro, ptr, size, ...) \
+ macro(ptr, size)
+
+#if SANITIZE_ADDRESS
+# include <sanitizer/asan_interface.h>
+
+/**
+ * sanitize_alloc(ptr, size = sizeof(*ptr))
+ *
+ * Mark a memory region as allocated.
+ */
+#define sanitize_alloc(...) SANITIZE_CALL(__asan_unpoison_memory_region, __VA_ARGS__)
+
+/**
+ * sanitize_free(ptr, size = sizeof(*ptr))
+ *
+ * Mark a memory region as free.
+ */
+#define sanitize_free(...) SANITIZE_CALL(__asan_poison_memory_region, __VA_ARGS__)
+
+#else
+# define sanitize_alloc sanitize_uninit
+# define sanitize_free sanitize_uninit
+#endif
+
+#if SANITIZE_MEMORY
+# include <sanitizer/msan_interface.h>
+
+/**
+ * sanitize_init(ptr, size = sizeof(*ptr))
+ *
+ * Mark a memory region as initialized.
+ */
+#define sanitize_init(...) SANITIZE_CALL(__msan_unpoison, __VA_ARGS__)
+
+/**
+ * sanitize_uninit(ptr, size = sizeof(*ptr))
+ *
+ * Mark a memory region as uninitialized.
+ */
+#define sanitize_uninit(...) SANITIZE_CALL(__msan_allocated_memory, __VA_ARGS__)
+
+#else
+# define sanitize_init(...)
+# define sanitize_uninit(...)
+#endif
+
+/**
+ * Initialize a variable, unless sanitizers would detect uninitialized uses.
+ */
+#if SANITIZE_MEMORY
+# define uninit(var, value) var
+#else
+# define uninit(var, value) value
+#endif
+
+#endif // BFS_SANITY_H