mem: instrument allocator for ASan
[dpdk.git] / lib / eal / common / malloc_elem.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef MALLOC_ELEM_H_
6 #define MALLOC_ELEM_H_
7
8 #include <stdbool.h>
9
10 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
11
12 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
13 struct malloc_heap;
14
15 enum elem_state {
16         ELEM_FREE = 0,
17         ELEM_BUSY,
18         ELEM_PAD  /* element is a padding-only header */
19 };
20
21 struct malloc_elem {
22         struct malloc_heap *heap;
23         struct malloc_elem *volatile prev;
24         /**< points to prev elem in memseg */
25         struct malloc_elem *volatile next;
26         /**< points to next elem in memseg */
27         LIST_ENTRY(malloc_elem) free_list;
28         /**< list of free elements in heap */
29         struct rte_memseg_list *msl;
30         volatile enum elem_state state;
31         uint32_t pad;
32         size_t size;
33         struct malloc_elem *orig_elem;
34         size_t orig_size;
35 #ifdef RTE_MALLOC_DEBUG
36         uint64_t header_cookie;         /* Cookie marking start of data */
37                                         /* trailer cookie at start + size */
38 #endif
39 #ifdef RTE_MALLOC_ASAN
40         size_t user_size;
41         uint64_t asan_cookie[2]; /* must be next to header_cookie */
42 #endif
43 } __rte_cache_aligned;
44
45 static const unsigned int MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
46
47 #ifndef RTE_MALLOC_DEBUG
48 #ifdef RTE_MALLOC_ASAN
49 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
50 #else
51 static const unsigned int MALLOC_ELEM_TRAILER_LEN;
52 #endif
53
54 /* dummy function - just check if pointer is non-null */
55 static inline int
56 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
57
58 /* dummy function - no header if malloc_debug is not enabled */
59 static inline void
60 set_header(struct malloc_elem *elem __rte_unused){ }
61
62 /* dummy function - no trailer if malloc_debug is not enabled */
63 static inline void
64 set_trailer(struct malloc_elem *elem __rte_unused){ }
65
66
67 #else
68 static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
69
70 #define MALLOC_HEADER_COOKIE   0xbadbadbadadd2e55ULL /**< Header cookie. */
71 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
72
73 /* define macros to make referencing the header and trailer cookies easier */
74 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
75                 elem->size - MALLOC_ELEM_TRAILER_LEN)))
76 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
77
78 static inline void
79 set_header(struct malloc_elem *elem)
80 {
81         if (elem != NULL)
82                 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
83 }
84
85 static inline void
86 set_trailer(struct malloc_elem *elem)
87 {
88         if (elem != NULL)
89                 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
90 }
91
92 /* check that the header and trailer cookies are set correctly */
93 static inline int
94 malloc_elem_cookies_ok(const struct malloc_elem *elem)
95 {
96         return elem != NULL &&
97                         MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
98                         MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
99 }
100
101 #endif
102
103 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
104
105 #ifdef RTE_MALLOC_ASAN
106
107 #ifdef RTE_ARCH_X86_64
108 #define ASAN_SHADOW_OFFSET    0x00007fff8000
109 #endif
110
111 #define ASAN_SHADOW_GRAIN_SIZE  8
112 #define ASAN_MEM_FREE_FLAG      0xfd
113 #define ASAN_MEM_REDZONE_FLAG   0xfa
114 #define ASAN_SHADOW_SCALE    3
115
116 #define ASAN_MEM_SHIFT(mem) ((void *)((uintptr_t)(mem) >> ASAN_SHADOW_SCALE))
117 #define ASAN_MEM_TO_SHADOW(mem) \
118         RTE_PTR_ADD(ASAN_MEM_SHIFT(mem), ASAN_SHADOW_OFFSET)
119
120 #if defined(__clang__)
121 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
122 #else
123 #define __rte_no_asan __attribute__((no_sanitize_address))
124 #endif
125
126 __rte_no_asan
127 static inline void
128 asan_set_shadow(void *addr, char val)
129 {
130         *(char *)addr = val;
131 }
132
133 static inline void
134 asan_set_zone(void *ptr, size_t len, uint32_t val)
135 {
136         size_t offset, i;
137         void *shadow;
138         size_t zone_len = len / ASAN_SHADOW_GRAIN_SIZE;
139         if (len % ASAN_SHADOW_GRAIN_SIZE != 0)
140                 zone_len += 1;
141
142         for (i = 0; i < zone_len; i++) {
143                 offset = i * ASAN_SHADOW_GRAIN_SIZE;
144                 shadow = ASAN_MEM_TO_SHADOW((uintptr_t)ptr + offset);
145                 asan_set_shadow(shadow, val);
146         }
147 }
148
149 /*
150  * When the memory is released, the release mark is
151  * set in the corresponding range of the shadow area.
152  */
153 static inline void
154 asan_set_freezone(void *ptr, size_t size)
155 {
156         asan_set_zone(ptr, size, ASAN_MEM_FREE_FLAG);
157 }
158
159 /*
160  * When the memory is allocated, memory state must set as accessible.
161  */
162 static inline void
163 asan_clear_alloczone(struct malloc_elem *elem)
164 {
165         asan_set_zone((void *)elem, elem->size, 0x0);
166 }
167
168 static inline void
169 asan_clear_split_alloczone(struct malloc_elem *elem)
170 {
171         void *ptr = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
172         asan_set_zone(ptr, MALLOC_ELEM_OVERHEAD, 0x0);
173 }
174
175 /*
176  * When the memory is allocated, the memory boundary is
177  * marked in the corresponding range of the shadow area.
178  * Requirement: redzone >= 16, is a power of two.
179  */
180 static inline void
181 asan_set_redzone(struct malloc_elem *elem, size_t user_size)
182 {
183         uintptr_t head_redzone;
184         uintptr_t tail_redzone;
185         void *front_shadow;
186         void *tail_shadow;
187         uint32_t val;
188
189         if (elem != NULL) {
190                 if (elem->state != ELEM_PAD)
191                         elem = RTE_PTR_ADD(elem, elem->pad);
192
193                 elem->user_size = user_size;
194
195                 /* Set mark before the start of the allocated memory */
196                 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
197                         MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
198                 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
199                 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
200                 front_shadow = ASAN_MEM_TO_SHADOW(head_redzone
201                         - ASAN_SHADOW_GRAIN_SIZE);
202                 asan_set_shadow(front_shadow, ASAN_MEM_REDZONE_FLAG);
203
204                 /* Set mark after the end of the allocated memory */
205                 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
206                         MALLOC_ELEM_HEADER_LEN
207                         + elem->user_size);
208                 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
209                 val = (tail_redzone % ASAN_SHADOW_GRAIN_SIZE);
210                 val = (val == 0) ? ASAN_MEM_REDZONE_FLAG : val;
211                 asan_set_shadow(tail_shadow, val);
212                 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
213                         + ASAN_SHADOW_GRAIN_SIZE);
214                 asan_set_shadow(tail_shadow, ASAN_MEM_REDZONE_FLAG);
215         }
216 }
217
218 /*
219  * When the memory is released, the mark of the memory boundary
220  * in the corresponding range of the shadow area is cleared.
221  * Requirement: redzone >= 16, is a power of two.
222  */
223 static inline void
224 asan_clear_redzone(struct malloc_elem *elem)
225 {
226         uintptr_t head_redzone;
227         uintptr_t tail_redzone;
228         void *head_shadow;
229         void *tail_shadow;
230
231         if (elem != NULL) {
232                 elem = RTE_PTR_ADD(elem, elem->pad);
233
234                 /* Clear mark before the start of the allocated memory */
235                 head_redzone = (uintptr_t)RTE_PTR_ADD(elem,
236                         MALLOC_ELEM_HEADER_LEN - ASAN_SHADOW_GRAIN_SIZE);
237                 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone);
238                 asan_set_shadow(head_shadow, 0x00);
239                 head_shadow = ASAN_MEM_TO_SHADOW(head_redzone
240                                 - ASAN_SHADOW_GRAIN_SIZE);
241                 asan_set_shadow(head_shadow, 0x00);
242
243                 /* Clear mark after the end of the allocated memory */
244                 tail_redzone = (uintptr_t)RTE_PTR_ADD(elem,
245                         MALLOC_ELEM_HEADER_LEN + elem->user_size);
246                 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone);
247                 asan_set_shadow(tail_shadow, 0x00);
248                 tail_shadow = ASAN_MEM_TO_SHADOW(tail_redzone
249                                 + ASAN_SHADOW_GRAIN_SIZE);
250                 asan_set_shadow(tail_shadow, 0x00);
251         }
252 }
253
254 static inline size_t
255 old_malloc_size(struct malloc_elem *elem)
256 {
257         if (elem->state != ELEM_PAD)
258                 elem = RTE_PTR_ADD(elem, elem->pad);
259
260         return elem->user_size;
261 }
262
263 #else /* !RTE_MALLOC_ASAN */
264
265 #define __rte_no_asan
266
267 static inline void
268 asan_set_freezone(void *ptr __rte_unused, size_t size __rte_unused) { }
269
270 static inline void
271 asan_clear_alloczone(struct malloc_elem *elem __rte_unused) { }
272
273 static inline void
274 asan_clear_split_alloczone(struct malloc_elem *elem __rte_unused) { }
275
276 static inline void
277 asan_set_redzone(struct malloc_elem *elem __rte_unused,
278                                         size_t user_size __rte_unused) { }
279
280 static inline void
281 asan_clear_redzone(struct malloc_elem *elem __rte_unused) { }
282
283 static inline size_t
284 old_malloc_size(struct malloc_elem *elem)
285 {
286         return elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
287 }
288 #endif /* !RTE_MALLOC_ASAN */
289
290 /*
291  * Given a pointer to the start of a memory block returned by malloc, get
292  * the actual malloc_elem header for that block.
293  */
294 static inline struct malloc_elem *
295 malloc_elem_from_data(const void *data)
296 {
297         if (data == NULL)
298                 return NULL;
299
300         struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
301         if (!malloc_elem_cookies_ok(elem))
302                 return NULL;
303         return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
304 }
305
306 /*
307  * initialise a malloc_elem header
308  */
309 void
310 malloc_elem_init(struct malloc_elem *elem,
311                 struct malloc_heap *heap,
312                 struct rte_memseg_list *msl,
313                 size_t size,
314                 struct malloc_elem *orig_elem,
315                 size_t orig_size);
316
317 void
318 malloc_elem_insert(struct malloc_elem *elem);
319
320 /*
321  * return true if the current malloc_elem can hold a block of data
322  * of the requested size and with the requested alignment
323  */
324 int
325 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
326                 unsigned int align, size_t bound, bool contig);
327
328 /*
329  * reserve a block of data in an existing malloc_elem. If the malloc_elem
330  * is much larger than the data block requested, we split the element in two.
331  */
332 struct malloc_elem *
333 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
334                 unsigned int align, size_t bound, bool contig);
335
336 /*
337  * free a malloc_elem block by adding it to the free list. If the
338  * blocks either immediately before or immediately after newly freed block
339  * are also free, the blocks are merged together.
340  */
341 struct malloc_elem *
342 malloc_elem_free(struct malloc_elem *elem);
343
344 struct malloc_elem *
345 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
346
347 /*
348  * attempt to resize a malloc_elem by expanding into any free space
349  * immediately after it in memory.
350  */
351 int
352 malloc_elem_resize(struct malloc_elem *elem, size_t size);
353
354 void
355 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
356
357 void
358 malloc_elem_free_list_remove(struct malloc_elem *elem);
359
360 /*
361  * dump contents of malloc elem to a file.
362  */
363 void
364 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
365
366 /*
367  * Given an element size, compute its freelist index.
368  */
369 size_t
370 malloc_elem_free_list_index(size_t size);
371
372 /*
373  * Add element to its heap's free list.
374  */
375 void
376 malloc_elem_free_list_insert(struct malloc_elem *elem);
377
378 /*
379  * Find biggest IOVA-contiguous zone within an element with specified alignment.
380  */
381 size_t
382 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
383
384 #endif /* MALLOC_ELEM_H_ */