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_TA_SANITIZE_KADDRESS)) || \ 49 ((defined(__KERNEL__) || defined(__LDELF__)) && \ 50 defined(CFG_CORE_SANITIZE_KADDRESS)) 51 #define ASAN_IS_ENABLED 1 52 #else 53 #define ASAN_IS_ENABLED 0 54 #endif 55 56 #if ASAN_IS_ENABLED 57 /* ASAN enabled */ 58 typedef void (*asan_panic_cb_t)(void); 59 60 void asan_add_shadowed(const void *va_begin, const void *va_end); 61 void asan_start(void); 62 void asan_panic(void); 63 void asan_set_panic_cb(asan_panic_cb_t panic_cb); 64 65 void asan_tag_no_access(const void *begin, const void *end); 66 void asan_tag_access(const void *begin, const void *end); 67 void asan_tag_heap_free(const void *begin, const void *end); 68 void *asan_memset_unchecked(void *s, int c, size_t n); 69 void *asan_memcpy_unchecked(void *__restrict s1, const void *__restrict s2, 70 size_t n); 71 int asan_user_map_shadow(void *lo, void *hi); 72 #else 73 static inline void asan_tag_no_access(const void *begin __unused, 74 const void *end __unused) 75 { 76 } 77 static inline void asan_tag_access(const void *begin __unused, 78 const void *end __unused) 79 { 80 } 81 static inline void asan_tag_heap_free(const void *begin __unused, 82 const void *end __unused) 83 { 84 } 85 86 static inline void *asan_memset_unchecked(void *s, int c, size_t n) 87 { 88 return memset(s, c, n); 89 } 90 91 static inline void *asan_memcpy_unchecked(void *__restrict s1, 92 const void *__restrict s2, size_t n) 93 { 94 return memcpy(s1, s2, n); 95 } 96 97 static inline void asan_start(void) 98 { 99 } 100 101 static inline int asan_user_map_shadow(void *lo __unused, void *hi __unused) 102 { 103 return 0; 104 } 105 #endif /* ASAN_IS_ENABLED */ 106 107 #endif /*__ASSEMBLER__*/ 108 #endif /*__ASAN_H*/ 109