blob: d099d3a941f6632b115c492ee5cc32e34354f30a [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
54#endif /* _included_clib_sanitizer_h */
55
56/*
57 * fd.io coding-style-patch-verification: ON
58 *
59 * Local Variables:
60 * eval: (c-set-style "gnu")
61 * End:
62 */