1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_LMS_PRI_H
33 #define _LOS_LMS_PRI_H
34
35 #include <pthread.h>
36 #include <malloc.h>
37 #include "los_lms.h"
38
39 #ifdef __cplusplus
40 #if __cplusplus
41 extern "C" {
42 #endif /* __cplusplus */
43 #endif /* __cplusplus */
44
45 #define UNKNOWN_ERROR 3
46 #define FREE_ERRORMODE 2
47 #define STORE_ERRMODE 1
48 #define LOAD_ERRMODE 0
49
50 #define SANITIZER_INTERFACE_ATTRIBUTE
51 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
52
53 #define LMS_SHADOW_ACCESSIBLE 0x00
54 #define LMS_SHADOW_AFTERFREE 0x03
55 #define LMS_SHADOW_REDZONE 0x02
56 #define LMS_SHADOW_PAINT 0x01
57 #define LMS_SHADOW_MASK 0x03
58
59 #define LMS_SHADOW_BITS_PER_CELL 2
60 #define LMS_MEM_BYTES_PER_SHADOW_CELL 4
61 #define LMS_SHADOW_U8_CELL_NUM 4
62 #define LMS_SHADOW_U8_REFER_BYTES 16
63
64 #define LMS_SHADOW_ACCESSIBLE_U8 0x00
65 #define LMS_SHADOW_AFTERFREE_U8 0xFF
66 #define LMS_SHADOW_REDZONE_U8 0xAA
67 #define LMS_SHADOW_MASK_U8 0xFF
68 #define LMS_SHADOW_PAINT_U8 0x55
69
70 #define MEM_REGION_SIZE_1 1
71 #define MEM_REGION_SIZE_2 2
72 #define MEM_REGION_SIZE_4 4
73 #define MEM_REGION_SIZE_8 8
74 #define MEM_REGION_SIZE_16 16
75
76 #define LMS_RZ_SIZE 4
77 #define LMS_OK 0
78 #define LMS_NOK 1
79
80 #define PAGE_ADDR_MASK 0xFFFFE000
81 #define SHADOW_BASE \
82 ((USPACE_MAP_BASE + (USPACE_MAP_SIZE / (LMS_SHADOW_U8_REFER_BYTES + 1)) * LMS_SHADOW_U8_REFER_BYTES) & \
83 PAGE_ADDR_MASK)
84 #define OVERHEAD (2 * sizeof(size_t))
85
86 #define LMS_MEM_ALIGN_DOWN(value, align) (((uint32_t)(value)) & ~((uint32_t)((align) - 1)))
87 #define LMS_MEM_ALIGN_UP(value, align) (((uint32_t)(value) + ((align) - 1)) & ~((uint32_t)((align) - 1)))
88
89 typedef struct {
90 uintptr_t memAddr;
91 uintptr_t shadowAddr;
92 uint32_t shadowOffset;
93 uint32_t shadowValue;
94 } LmsAddrInfo;
95
96 extern pthread_mutex_t g_lmsMutex;
97
LmsLock(pthread_mutex_t * lock)98 ATTRIBUTE_NO_SANITIZE_ADDRESS static inline void LmsLock(pthread_mutex_t *lock)
99 {
100 (void)pthread_mutex_lock(lock);
101 }
102
LmsTrylock(pthread_mutex_t * lock)103 ATTRIBUTE_NO_SANITIZE_ADDRESS static inline int LmsTrylock(pthread_mutex_t *lock)
104 {
105 return pthread_mutex_trylock(lock);
106 }
107
LmsUnlock(pthread_mutex_t * lock)108 ATTRIBUTE_NO_SANITIZE_ADDRESS static inline void LmsUnlock(pthread_mutex_t *lock)
109 {
110 (void)pthread_mutex_unlock(lock);
111 }
112
LmsCrash(void)113 ATTRIBUTE_NO_SANITIZE_ADDRESS static inline void LmsCrash(void)
114 {
115 *(volatile char *)(SHADOW_BASE - 1) = 0;
116 }
117
118 uint32_t LmsIsShadowAddrMapped(uintptr_t sdStartAddr, uintptr_t sdEndAddr);
119
120 void LmsSetShadowValue(uintptr_t startAddr, uintptr_t endAddr, char value);
121
122 void LmsGetShadowValue(uintptr_t addr, uint32_t *shadowValue);
123
124 void LmsReportError(uintptr_t p, size_t size, uint32_t errMod);
125
126 void LmsMem2Shadow(uintptr_t memAddr, uintptr_t *shadowAddr, uint32_t *shadowOffset);
127
128 void LmsCheckValid(const char *dest, const char *src);
129
130 void *__real_malloc(size_t);
131
132 void __real_free(void *);
133
134 void *__real_calloc(size_t, size_t);
135
136 void *__real_realloc(void *, size_t);
137
138 void *__real_valloc(size_t);
139
140 void *__real_aligned_alloc(size_t, size_t);
141
142 void *__real_memcpy(void *__restrict, const void *__restrict, size_t);
143
144 void *__real_memmove(void *, const void *, size_t);
145
146 char *__real_strcat(char *, const char *);
147
148 char *__real_strcpy(char *, const char *);
149
150 void *__real_memset(void *, int, size_t);
151
152 SANITIZER_INTERFACE_ATTRIBUTE void __asan_store1_noabort(uintptr_t p);
153 SANITIZER_INTERFACE_ATTRIBUTE void __asan_store4_noabort(uintptr_t p);
154 SANITIZER_INTERFACE_ATTRIBUTE void __asan_load4_noabort(uintptr_t p);
155 SANITIZER_INTERFACE_ATTRIBUTE void __asan_load1_noabort(uintptr_t p);
156 SANITIZER_INTERFACE_ATTRIBUTE void __asan_loadN_noabort(uintptr_t p, uint32_t size);
157 SANITIZER_INTERFACE_ATTRIBUTE void __asan_storeN_noabort(uintptr_t p, uint32_t size);
158 SANITIZER_INTERFACE_ATTRIBUTE void __asan_store2_noabort(uintptr_t p);
159 SANITIZER_INTERFACE_ATTRIBUTE void __asan_load2_noabort(uintptr_t p);
160 SANITIZER_INTERFACE_ATTRIBUTE void __asan_store8_noabort(uintptr_t p);
161 SANITIZER_INTERFACE_ATTRIBUTE void __asan_load8_noabort(uintptr_t p);
162 SANITIZER_INTERFACE_ATTRIBUTE void __asan_load16_noabort(uintptr_t p);
163 SANITIZER_INTERFACE_ATTRIBUTE void __asan_store16_noabort(uintptr_t p);
164 SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_no_return(void);
165
166 #ifdef __cplusplus
167 #if __cplusplus
168 }
169 #endif /* __cplusplus */
170 #endif /* __cplusplus */
171
172 #endif /* _LOS_LMS_PRI_H */