summaryrefslogtreecommitdiffstats
path: root/src/sanity.h
blob: be77eef8ba745d7704dd56a6d50d6088609763ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

/**
 * Sanitizer interface.
 */

#ifndef BFS_SANITY_H
#define BFS_SANITY_H

#include <stddef.h>

// 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__)

/**
 * 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__
#  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(...) ((void)0)
#  define sanitize_uninit(...) ((void)0)
#endif

/**
 * Initialize a variable, unless sanitizers would detect uninitialized uses.
 */
#if __SANITIZE_MEMORY__
#  define uninit(value)
#else
#  define uninit(value) = value
#endif

#endif // BFS_SANITY_H