eal/arm64: support ASan
[dpdk.git] / lib / eal / common / malloc_heap.c
index ee400f3..55aad27 100644 (file)
@@ -237,6 +237,7 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
                unsigned int flags, size_t align, size_t bound, bool contig)
 {
        struct malloc_elem *elem;
+       size_t user_size = size;
 
        size = RTE_CACHE_LINE_ROUNDUP(size);
        align = RTE_CACHE_LINE_ROUNDUP(align);
@@ -250,6 +251,8 @@ heap_alloc(struct malloc_heap *heap, const char *type __rte_unused, size_t size,
 
                /* increase heap's count of allocated elements */
                heap->alloc_count++;
+
+               asan_set_redzone(elem, user_size);
        }
 
        return elem == NULL ? NULL : (void *)(&elem[1]);
@@ -270,6 +273,8 @@ heap_alloc_biggest(struct malloc_heap *heap, const char *type __rte_unused,
 
                /* increase heap's count of allocated elements */
                heap->alloc_count++;
+
+               asan_set_redzone(elem, size);
        }
 
        return elem == NULL ? NULL : (void *)(&elem[1]);
@@ -694,6 +699,26 @@ alloc_unlock:
        return ret;
 }
 
+static unsigned int
+malloc_get_numa_socket(void)
+{
+       const struct internal_config *conf = eal_get_internal_configuration();
+       unsigned int socket_id = rte_socket_id();
+       unsigned int idx;
+
+       if (socket_id != (unsigned int)SOCKET_ID_ANY)
+               return socket_id;
+
+       /* for control threads, return first socket where memory is available */
+       for (idx = 0; idx < rte_socket_count(); idx++) {
+               socket_id = rte_socket_id_by_idx(idx);
+               if (conf->socket_mem[socket_id] != 0)
+                       return socket_id;
+       }
+
+       return rte_socket_id_by_idx(0);
+}
+
 void *
 malloc_heap_alloc(const char *type, size_t size, int socket_arg,
                unsigned int flags, size_t align, size_t bound, bool contig)
@@ -841,6 +866,8 @@ malloc_heap_free(struct malloc_elem *elem)
        if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
                return -1;
 
+       asan_clear_redzone(elem);
+
        /* elem may be merged with previous element, so keep heap address */
        heap = elem->heap;
        msl = elem->msl;
@@ -848,6 +875,9 @@ malloc_heap_free(struct malloc_elem *elem)
 
        rte_spinlock_lock(&(heap->lock));
 
+       void *asan_ptr = RTE_PTR_ADD(elem, MALLOC_ELEM_HEADER_LEN + elem->pad);
+       size_t asan_data_len = elem->size - MALLOC_ELEM_OVERHEAD - elem->pad;
+
        /* mark element as free */
        elem->state = ELEM_FREE;
 
@@ -1001,6 +1031,8 @@ malloc_heap_free(struct malloc_elem *elem)
 
        rte_mcfg_mem_write_unlock();
 free_unlock:
+       asan_set_freezone(asan_ptr, asan_data_len);
+
        rte_spinlock_unlock(&(heap->lock));
        return ret;
 }