xref: /optee_os/lib/libutils/ext/include/asan.h (revision 4cafd8a3f88594ce06758c2a85b59c2d32a6ac7e)
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 	/* Shadow memory regions */
36 	size_t s_regs_count;
37 	struct asan_va_reg s_regs[ASAN_VA_REGS_MAX];
38 };
39 
40 #ifdef __KERNEL__
41 #define GET_ASAN_INFO() (&__asan_global_info)
42 #else
43 #define GET_ASAN_INFO() ((struct asan_global_info *) \
44 	(CFG_USER_ASAN_SHADOW_OFFSET - SMALL_PAGE_SIZE))
45 #endif
46 
47 #if ((defined(__KERNEL__) || defined(__LDELF__)) && \
48      defined(CFG_CORE_SANITIZE_KADDRESS))
49 #define ASAN_IS_ENABLED 1
50 #else
51 #define ASAN_IS_ENABLED 0
52 #endif
53 
54 #if ASAN_IS_ENABLED
55 /* ASAN enabled */
56 typedef void (*asan_panic_cb_t)(void);
57 
58 void asan_add_shadowed(const void *va_begin, const void *va_end);
59 void asan_start(void);
60 void asan_panic(void);
61 void asan_set_panic_cb(asan_panic_cb_t panic_cb);
62 
63 void asan_tag_no_access(const void *begin, const void *end);
64 void asan_tag_access(const void *begin, const void *end);
65 void asan_tag_heap_free(const void *begin, const void *end);
66 void *asan_memset_unchecked(void *s, int c, size_t n);
67 void *asan_memcpy_unchecked(void *__restrict s1, const void *__restrict s2,
68 			    size_t n);
69 int asan_user_map_shadow(void *lo, void *hi);
70 #else
71 static inline void asan_tag_no_access(const void *begin __unused,
72 				      const void *end __unused)
73 {
74 }
75 static inline void asan_tag_access(const void *begin __unused,
76 				   const void *end __unused)
77 {
78 }
79 static inline void asan_tag_heap_free(const void *begin __unused,
80 				      const void *end __unused)
81 {
82 }
83 
84 static inline void *asan_memset_unchecked(void *s, int c, size_t n)
85 {
86 	return memset(s, c, n);
87 }
88 
89 static inline void *asan_memcpy_unchecked(void *__restrict s1,
90 					  const void *__restrict s2, size_t n)
91 {
92 	return memcpy(s1, s2, n);
93 }
94 
95 static inline void asan_start(void)
96 {
97 }
98 
99 static inline int asan_user_map_shadow(void *lo __unused, void *hi __unused)
100 {
101 	return 0;
102 }
103 #endif /* ASAN_IS_ENABLED */
104 
105 #endif /*__ASSEMBLER__*/
106 #endif /*__ASAN_H*/
107