From 77211ec0866344655c439839753653234a5e281e Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 16 Dec 2024 12:49:42 -0500 Subject: sanity: New sanitize_resize() function This wraps __sanitizer_annotate_contiguous_container() to give byte-precise tracking of usable allocation sizes with ASan. --- src/sanity.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/sanity.h') 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__ -- cgit v1.2.3