xref: /optee_os/lib/libutils/ext/include/asan.h (revision 60aa5df76a7b755920e4cd602359bd8a7488e5f3)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  */
5 #ifndef __ASAN_H
6 #define __ASAN_H
7 
8 #include <stdint.h>
9 
10 #define ASAN_DATA_RED_ZONE	-1
11 #define ASAN_HEAP_RED_ZONE	-2
12 
13 #define ASAN_BLOCK_SIZE		U(8)
14 #define ASAN_BLOCK_SHIFT	U(3)
15 #define ASAN_BLOCK_MASK		(ASAN_BLOCK_SIZE - 1)
16 
17 #ifndef __ASSEMBLER__
18 #include <compiler.h>
19 #include <string.h>
20 #include <types_ext.h>
21 
22 #define ASAN_VA_REGS_MAX 32
23 
24 /* Represents memory mapped region */
25 struct asan_va_reg {
26 	vaddr_t lo;
27 	vaddr_t hi;
28 };
29 
30 /* Global structure with ASan metadata */
31 struct asan_global_info {
32 	/* Virtual memory regions allowed for ASan checks */
33 	size_t regs_count;
34 	struct asan_va_reg regs[ASAN_VA_REGS_MAX];
35 };
36 
37 #define GET_ASAN_INFO() (&__asan_global_info)
38 
39 typedef void (*asan_panic_cb_t)(void);
40 
41 void asan_add_shadowed(const void *va_begin, const void *va_end);
42 void asan_start(void);
43 void asan_panic(void);
44 void asan_set_panic_cb(asan_panic_cb_t panic_cb);
45 
46 #ifdef CFG_CORE_SANITIZE_KADDRESS
47 void asan_tag_no_access(const void *begin, const void *end);
48 void asan_tag_access(const void *begin, const void *end);
49 void asan_tag_heap_free(const void *begin, const void *end);
50 void *asan_memset_unchecked(void *s, int c, size_t n);
51 void *asan_memcpy_unchecked(void *__restrict s1, const void *__restrict s2,
52 			    size_t n);
53 #else
54 static inline void asan_tag_no_access(const void *begin __unused,
55 				      const void *end __unused)
56 {
57 }
58 static inline void asan_tag_access(const void *begin __unused,
59 				   const void *end __unused)
60 {
61 }
62 static inline void asan_tag_heap_free(const void *begin __unused,
63 				      const void *end __unused)
64 {
65 }
66 
67 static inline void *asan_memset_unchecked(void *s, int c, size_t n)
68 {
69 	return memset(s, c, n);
70 }
71 
72 static inline void *asan_memcpy_unchecked(void *__restrict s1,
73 					  const void *__restrict s2, size_t n)
74 {
75 	return memcpy(s1, s2, n);
76 }
77 
78 #endif
79 
80 #endif /*__ASSEMBLER__*/
81 #endif /*__ASAN_H*/
82