blob: db4daea978d8929cdd7615cc2612cd5818a636e9 [file] [log] [blame]
Benoît Ganne9fb6d402019-04-15 15:28:21 +02001#ifndef _included_clib_sanitizer_h
2#define _included_clib_sanitizer_h
3
4#ifdef CLIB_SANITIZE_ADDR
5
6#include <sanitizer/asan_interface.h>
7#include <vppinfra/clib.h>
8
9#define CLIB_NOSANITIZE_ADDR __attribute__((no_sanitize_address))
10#define CLIB_MEM_POISON(a, s) ASAN_POISON_MEMORY_REGION((a), (s))
11#define CLIB_MEM_UNPOISON(a, s) ASAN_UNPOISON_MEMORY_REGION((a), (s))
12
13#define CLIB_MEM_OVERFLOW(f, src, n) \
14 ({ \
15 typeof (f) clib_mem_overflow_ret__; \
16 const void *clib_mem_overflow_src__ = (src); \
17 size_t clib_mem_overflow_n__ = (n); \
18 const void *clib_mem_overflow_start__ = __asan_region_is_poisoned((void *)clib_mem_overflow_src__, clib_mem_overflow_n__); \
19 clib_mem_overflow_n__ -= (size_t)(clib_mem_overflow_start__ - clib_mem_overflow_src__); \
20 if (clib_mem_overflow_start__) \
21 CLIB_MEM_UNPOISON(clib_mem_overflow_start__, clib_mem_overflow_n__); \
22 clib_mem_overflow_ret__ = f; \
23 if (clib_mem_overflow_start__) \
24 CLIB_MEM_POISON(clib_mem_overflow_start__, clib_mem_overflow_n__); \
25 clib_mem_overflow_ret__; \
26 })
27
28#define CLIB_MEM_OVERFLOW_LOAD(f, src) \
29 ({ \
30 typeof(src) clib_mem_overflow_load_src__ = (src); \
31 CLIB_MEM_OVERFLOW(f(clib_mem_overflow_load_src__), clib_mem_overflow_load_src__, sizeof(typeof(f(clib_mem_overflow_load_src__)))); \
32 })
33
34static_always_inline void
35CLIB_MEM_POISON_LEN (void *src, size_t oldlen, size_t newlen)
36{
37 if (oldlen > newlen)
38 CLIB_MEM_POISON (src + newlen, oldlen - newlen);
39 else if (newlen > oldlen)
40 CLIB_MEM_UNPOISON (src + oldlen, newlen - oldlen);
41}
42
43#else /* CLIB_SANITIZE_ADDR */
44
45#define CLIB_NOSANITIZE_ADDR
46#define CLIB_MEM_POISON(a, s) (void)(a)
47#define CLIB_MEM_UNPOISON(a, s) (void)(a)
48#define CLIB_MEM_OVERFLOW(a, b, c) a
49#define CLIB_MEM_OVERFLOW_LOAD(f, src) f(src)
50#define CLIB_MEM_POISON_LEN(a, b, c)
51
52#endif /* CLIB_SANITIZE_ADDR */
53
Benoît Ganne01a103b2020-07-09 14:23:51 +020054/*
55 * clang tends to force alignment of all sections when compiling for address
56 * sanitizer. This confuse VPP plugin infra, prevent clang to do that
57 * On the contrary, GCC does not support this kind of attribute on sections
58 * sigh.
59 */
60#ifdef __clang__
61#define CLIB_NOSANITIZE_PLUGIN_REG_SECTION CLIB_NOSANITIZE_ADDR
62#else
63#define CLIB_NOSANITIZE_PLUGIN_REG_SECTION
64#endif
65
Benoît Ganne9fb6d402019-04-15 15:28:21 +020066#endif /* _included_clib_sanitizer_h */
67
68/*
69 * fd.io coding-style-patch-verification: ON
70 *
71 * Local Variables:
72 * eval: (c-set-style "gnu")
73 * End:
74 */