summaryrefslogtreecommitdiffstats
path: root/src/sanity.h
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-12-16 12:49:42 -0500
committerTavian Barnes <tavianator@tavianator.com>2024-12-16 12:57:51 -0500
commit77211ec0866344655c439839753653234a5e281e (patch)
treea48b2a02a4aa74476d610de0c39220a5b8f5bf51 /src/sanity.h
parentce378308af700657d9cba841939b2385b21accaa (diff)
downloadbfs-77211ec0866344655c439839753653234a5e281e.tar.xz
sanity: New sanitize_resize() function
This wraps __sanitizer_annotate_contiguous_container() to give byte-precise tracking of usable allocation sizes with ASan.
Diffstat (limited to 'src/sanity.h')
-rw-r--r--src/sanity.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/sanity.h b/src/sanity.h
index 89d0e4f..be77eef 100644
--- a/src/sanity.h
+++ b/src/sanity.h
@@ -37,9 +37,27 @@
*/
#define sanitize_free(...) SANITIZE_CALL(__asan_poison_memory_region, __VA_ARGS__)
+/**
+ * Adjust the size of an allocated region, for things like dynamic arrays.
+ *
+ * @ptr
+ * The memory region.
+ * @old
+ * The previous usable size of the region.
+ * @new
+ * The new usable size of the region.
+ * @cap
+ * The total allocated capacity of the region.
+ */
+static inline void sanitize_resize(const void *ptr, size_t old, size_t new, size_t cap) {
+ const char *beg = ptr;
+ __sanitizer_annotate_contiguous_container(beg, beg + cap, beg + old, beg + new);
+}
+
#else
# define sanitize_alloc(...) ((void)0)
# define sanitize_free(...) ((void)0)
+# define sanitize_resize(ptr, old, new, cap) ((void)0)
#endif
#if __SANITIZE_MEMORY__