|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Intel Corporation |
| 3 | + * |
| 4 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 5 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +*/ |
| 7 | + |
| 8 | +#if defined(__APPLE__) |
| 9 | +#include <malloc/malloc.h> |
| 10 | +#else |
| 11 | +#include <malloc.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +#include <umf/proxy_lib_new_delete.h> |
| 15 | + |
| 16 | +#include "base.hpp" |
| 17 | +#include "test_helpers.h" |
| 18 | +#include "utils_common.h" |
| 19 | + |
| 20 | +using umf_test::test; |
| 21 | + |
| 22 | +// size threshold defined by the env variable UMF_PROXY="size.threshold=64" |
| 23 | +#define SIZE_THRESHOLD 64 |
| 24 | +#define SIZE_EQ (SIZE_THRESHOLD) |
| 25 | +#define SIZE_LT (SIZE_THRESHOLD - 1) |
| 26 | + |
| 27 | +#define ALIGN_1024 1024 |
| 28 | + |
| 29 | +TEST_F(test, proxyLib_basic) { |
| 30 | + // a check to verify we are running the proxy library |
| 31 | + void *ptr = (void *)0x01; |
| 32 | + |
| 33 | +#ifdef _WIN32 |
| 34 | + size_t size = _msize(ptr); |
| 35 | +#elif __APPLE__ |
| 36 | + size_t size = ::malloc_size(ptr); |
| 37 | +#else |
| 38 | + size_t size = ::malloc_usable_size(ptr); |
| 39 | +#endif |
| 40 | + |
| 41 | + ASSERT_EQ(size, 0xDEADBEEF); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_F(test, proxyLib_realloc_size0) { |
| 45 | + // realloc(ptr, 0) == free(ptr) |
| 46 | + // realloc(ptr, 0) returns NULL |
| 47 | + ASSERT_EQ(::realloc(::malloc(SIZE_EQ), 0), nullptr); |
| 48 | +} |
| 49 | + |
| 50 | +// The proxyLib_size_threshold_* tests test the size threshold of the proxy library. |
| 51 | +// The size threshold is set to SIZE_THRESHOLD bytes in this test, so all allocations of: |
| 52 | +// 1) size < SIZE_THRESHOLD go through the default system allocator |
| 53 | +// (umfPoolByPtr(ptr_size < SIZE_THRESHOLD) == nullptr) |
| 54 | +// 2) size >= SIZE_THRESHOLD go through the proxy library allocator |
| 55 | +// (umfPoolByPtr(ptr_size >= SIZE_THRESHOLD) != nullptr) |
| 56 | + |
| 57 | +TEST_F(test, proxyLib_size_threshold_aligned_alloc) { |
| 58 | +#ifdef _WIN32 |
| 59 | + void *ptr_LT = _aligned_malloc(SIZE_LT, ALIGN_1024); |
| 60 | + void *ptr_EQ = _aligned_malloc(SIZE_EQ, ALIGN_1024); |
| 61 | +#else |
| 62 | + void *ptr_LT = ::aligned_alloc(ALIGN_1024, SIZE_LT); |
| 63 | + void *ptr_EQ = ::aligned_alloc(ALIGN_1024, SIZE_EQ); |
| 64 | +#endif |
| 65 | + |
| 66 | + ASSERT_NE(ptr_LT, nullptr); |
| 67 | + ASSERT_NE(ptr_EQ, nullptr); |
| 68 | + |
| 69 | + // verify alignment |
| 70 | + ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_LT, ALIGN_1024)), 1); |
| 71 | + ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_EQ, ALIGN_1024)), 1); |
| 72 | + |
| 73 | + ASSERT_EQ(umfPoolByPtr(ptr_LT), nullptr); |
| 74 | + ASSERT_NE(umfPoolByPtr(ptr_EQ), nullptr); |
| 75 | + |
| 76 | +#ifdef _WIN32 |
| 77 | + _aligned_free(ptr_LT); |
| 78 | + _aligned_free(ptr_EQ); |
| 79 | +#else |
| 80 | + ::free(ptr_LT); |
| 81 | + ::free(ptr_EQ); |
| 82 | +#endif |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(test, proxyLib_size_threshold_malloc) { |
| 86 | + void *ptr_LT = malloc(SIZE_LT); |
| 87 | + void *ptr_EQ = malloc(SIZE_EQ); |
| 88 | + |
| 89 | + ASSERT_NE(ptr_LT, nullptr); |
| 90 | + ASSERT_NE(ptr_EQ, nullptr); |
| 91 | + |
| 92 | + ASSERT_EQ(umfPoolByPtr(ptr_LT), nullptr); |
| 93 | + ASSERT_NE(umfPoolByPtr(ptr_EQ), nullptr); |
| 94 | + |
| 95 | + ::free(ptr_LT); |
| 96 | + ::free(ptr_EQ); |
| 97 | +} |
| 98 | + |
| 99 | +TEST_F(test, proxyLib_size_threshold_calloc) { |
| 100 | + void *ptr_LT = calloc(SIZE_LT, 1); |
| 101 | + void *ptr_EQ = calloc(SIZE_EQ, 1); |
| 102 | + |
| 103 | + ASSERT_NE(ptr_LT, nullptr); |
| 104 | + ASSERT_NE(ptr_EQ, nullptr); |
| 105 | + |
| 106 | + ASSERT_EQ(umfPoolByPtr(ptr_LT), nullptr); |
| 107 | + ASSERT_NE(umfPoolByPtr(ptr_EQ), nullptr); |
| 108 | + |
| 109 | + ::free(ptr_LT); |
| 110 | + ::free(ptr_EQ); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_F(test, proxyLib_size_threshold_realloc_up) { |
| 114 | + void *ptr_LT = malloc(SIZE_LT); |
| 115 | + void *ptr_EQ = malloc(SIZE_EQ); |
| 116 | + |
| 117 | + ASSERT_NE(ptr_LT, nullptr); |
| 118 | + ASSERT_NE(ptr_EQ, nullptr); |
| 119 | + |
| 120 | + void *ptr_LT_r = realloc(ptr_LT, 2 * SIZE_LT); |
| 121 | + void *ptr_EQ_r = realloc(ptr_EQ, 2 * SIZE_EQ); |
| 122 | + |
| 123 | + ASSERT_NE(ptr_LT_r, nullptr); |
| 124 | + ASSERT_NE(ptr_EQ_r, nullptr); |
| 125 | + |
| 126 | + ASSERT_EQ(umfPoolByPtr(ptr_LT_r), nullptr); |
| 127 | + ASSERT_NE(umfPoolByPtr(ptr_EQ_r), nullptr); |
| 128 | + |
| 129 | + ::free(ptr_LT_r); |
| 130 | + ::free(ptr_EQ_r); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(test, proxyLib_size_threshold_realloc_down) { |
| 134 | + void *ptr_LT = malloc(SIZE_LT); |
| 135 | + void *ptr_EQ = malloc(SIZE_EQ); |
| 136 | + |
| 137 | + ASSERT_NE(ptr_LT, nullptr); |
| 138 | + ASSERT_NE(ptr_EQ, nullptr); |
| 139 | + |
| 140 | + void *ptr_LT_r = realloc(ptr_LT, SIZE_LT / 2); |
| 141 | + void *ptr_EQ_r = realloc(ptr_EQ, SIZE_EQ / 2); |
| 142 | + |
| 143 | + ASSERT_NE(ptr_LT_r, nullptr); |
| 144 | + ASSERT_NE(ptr_EQ_r, nullptr); |
| 145 | + |
| 146 | + ASSERT_EQ(umfPoolByPtr(ptr_LT_r), nullptr); |
| 147 | + ASSERT_NE(umfPoolByPtr(ptr_EQ_r), nullptr); |
| 148 | + |
| 149 | + ::free(ptr_LT_r); |
| 150 | + ::free(ptr_EQ_r); |
| 151 | +} |
| 152 | + |
| 153 | +TEST_F(test, proxyLib_size_threshold_malloc_usable_size) { |
| 154 | + |
| 155 | + void *ptr_LT = ::malloc(SIZE_LT); |
| 156 | + void *ptr_EQ = ::malloc(SIZE_EQ); |
| 157 | + |
| 158 | + ASSERT_NE(ptr_LT, nullptr); |
| 159 | + ASSERT_NE(ptr_EQ, nullptr); |
| 160 | + |
| 161 | + if (ptr_LT == nullptr || ptr_EQ == nullptr) { |
| 162 | + // Fix for the following CodeQL's warning on Windows: |
| 163 | + // 'ptr' could be '0': this does not adhere to the specification for the function '_msize'. |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | +#ifdef _WIN32 |
| 168 | + size_t size_LT = _msize(ptr_LT); |
| 169 | + size_t size_EQ = _msize(ptr_EQ); |
| 170 | +#elif __APPLE__ |
| 171 | + size_t size_LT = ::malloc_size(ptr_LT); |
| 172 | + size_t size_EQ = ::malloc_size(ptr_EQ); |
| 173 | +#else |
| 174 | + size_t size_LT = ::malloc_usable_size(ptr_LT); |
| 175 | + size_t size_EQ = ::malloc_usable_size(ptr_EQ); |
| 176 | +#endif |
| 177 | + |
| 178 | + ASSERT_EQ((int)(size_LT == 0 || size_LT >= SIZE_LT), 1); |
| 179 | + ASSERT_EQ((int)(size_EQ == 0 || size_EQ >= SIZE_EQ), 1); |
| 180 | + |
| 181 | + ::free(ptr_LT); |
| 182 | + ::free(ptr_EQ); |
| 183 | +} |
0 commit comments