|
| 1 | +// Copyright (C) 2024 Intel Corporation |
| 2 | +// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +#include "utils.hpp" |
| 6 | + |
| 7 | +namespace fuzz { |
| 8 | + |
| 9 | +constexpr int MAX_PROVIDER_VECTOR_SIZE = 1024; |
| 10 | +constexpr int MAX_POOLS_VECTOR_SIZE = 20; |
| 11 | +constexpr int MAX_POOLS_ALLOC_SIZE = 1 * 1024; // 1 kB |
| 12 | +constexpr int MAX_PROVIDER_ALLOC_SIZE = 100 * 1024; // 100 kB |
| 13 | + |
| 14 | +int umf_memory_provider_create(TestState &test_state) { |
| 15 | + std::cout << "Begin creating a provider" << std::endl; |
| 16 | + umf_memory_provider_ops_t *provider_ops = umfOsMemoryProviderOps(); |
| 17 | + umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault(); |
| 18 | + umf_result_t res = |
| 19 | + umfMemoryProviderCreate(provider_ops, ¶ms, &test_state.provider); |
| 20 | + |
| 21 | + if (res != UMF_RESULT_SUCCESS) { |
| 22 | + std::cout << "Failed to create a memory provider: " << res << std::endl; |
| 23 | + return -1; |
| 24 | + } |
| 25 | + std::cout << "OS memory provider created at " << (void *)test_state.provider |
| 26 | + << std::endl; |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +int umf_memory_provider_alloc(TestState &test_state) { |
| 31 | + std::cout << "Begin memory_provider_alloc" << std::endl; |
| 32 | + void *ptr; |
| 33 | + size_t alloc_size; |
| 34 | + constexpr size_t alignment = 0; |
| 35 | + |
| 36 | + if (test_state.provider_memory_allocations.size() >= |
| 37 | + MAX_PROVIDER_VECTOR_SIZE) { |
| 38 | + return -1; |
| 39 | + } |
| 40 | + |
| 41 | + int ret = test_state.get_next_alloc_size(test_state, alloc_size, |
| 42 | + MAX_PROVIDER_ALLOC_SIZE); |
| 43 | + if (ret != 0) { |
| 44 | + std::cout << "Failed to get alloc size" << std::endl; |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + umf_result_t res = umfMemoryProviderAlloc(test_state.provider, alloc_size, |
| 49 | + alignment, &ptr); |
| 50 | + if (res != UMF_RESULT_SUCCESS) { |
| 51 | + std::cout << "Failed to allocate memory from the provider: " << res |
| 52 | + << std::endl; |
| 53 | + return -1; |
| 54 | + } |
| 55 | + test_state.provider_memory_allocations.push_back( |
| 56 | + std::make_pair(ptr, alloc_size)); |
| 57 | + std::cout << "Allocated memory at " << ptr << " with alloc_size " |
| 58 | + << alloc_size << std::endl; |
| 59 | + std::cout << "Size of vector with allocated memory from the provider: " |
| 60 | + << test_state.provider_memory_allocations.size() << std::endl; |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +int umf_memory_provider_free(TestState &test_state) { |
| 65 | + std::cout << "Begin memory_provider_free" << std::endl; |
| 66 | + if (test_state.provider_memory_allocations.empty()) { |
| 67 | + std::cout << "No memory allocated" << std::endl; |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + std::pair<void *, size_t> alloc = |
| 72 | + test_state.provider_memory_allocations.back(); |
| 73 | + umf_result_t res = |
| 74 | + umfMemoryProviderFree(test_state.provider, alloc.first, alloc.second); |
| 75 | + |
| 76 | + if (res != UMF_RESULT_SUCCESS) { |
| 77 | + std::cout << "Failed to free memory to the provider: " << res |
| 78 | + << std::endl; |
| 79 | + ; |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + std::cout << "Freed memory from the provider at " << alloc.first |
| 84 | + << " with alloc_size " << alloc.second << std::endl; |
| 85 | + test_state.provider_memory_allocations.pop_back(); |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +int umf_pool_create(TestState &test_state) { |
| 90 | + if (test_state.pools.size() > MAX_POOLS_VECTOR_SIZE) { |
| 91 | + std::cout << "Max pools limit reached" << std::endl; |
| 92 | + return -1; |
| 93 | + } |
| 94 | + |
| 95 | + umf_memory_pool_ops_t *pool_ops = umfScalablePoolOps(); |
| 96 | + void *pool_params = NULL; |
| 97 | + umf_pool_create_flags_t flags = 0; |
| 98 | + umf_memory_pool_handle_t pool; |
| 99 | + umf_result_t res = |
| 100 | + umfPoolCreate(pool_ops, test_state.provider, pool_params, flags, &pool); |
| 101 | + |
| 102 | + if (res != UMF_RESULT_SUCCESS) { |
| 103 | + std::cout << "Failed to create a pool: " << res << std::endl; |
| 104 | + return -1; |
| 105 | + } |
| 106 | + |
| 107 | + test_state.pools.insert(std::make_pair(pool, std::vector<void *>())); |
| 108 | + std::cout << "Scalable memory pool created at " << pool |
| 109 | + << " and pools available: " << test_state.pools.size() |
| 110 | + << std::endl; |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int umf_pool_destroy(TestState &test_state) { |
| 115 | + std::cout << "Begin destroy pool" << std::endl; |
| 116 | + if (test_state.pools.empty()) { |
| 117 | + std::cout << "No pools created" << std::endl; |
| 118 | + return -1; |
| 119 | + } |
| 120 | + auto pool = (*test_state.pools.begin()).first; |
| 121 | + umfPoolDestroy(pool); |
| 122 | + test_state.pools.erase(pool); |
| 123 | + std::cout << "Destroyed pool at " << pool << std::endl; |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +int umf_pool_malloc(TestState &test_state) { |
| 128 | + std::cout << "Begin pool_malloc" << std::endl; |
| 129 | + if (test_state.pools.empty()) { |
| 130 | + std::cout << "No pools created" << std::endl; |
| 131 | + return -1; |
| 132 | + } |
| 133 | + size_t alloc_size; |
| 134 | + int ret = test_state.get_next_alloc_size(test_state, alloc_size, |
| 135 | + MAX_POOLS_ALLOC_SIZE); |
| 136 | + if (ret != 0) { |
| 137 | + std::cout << "Failed to get next allocation size" << std::endl; |
| 138 | + return -1; |
| 139 | + } |
| 140 | + auto &pool_entry = *test_state.pools.rbegin(); |
| 141 | + void *ptr = umfPoolMalloc(pool_entry.first, alloc_size); |
| 142 | + if (!ptr) { |
| 143 | + std::cout |
| 144 | + << "Failed to allocate memory in the pool with handle address: " |
| 145 | + << pool_entry.first << std::endl; |
| 146 | + } |
| 147 | + |
| 148 | + pool_entry.second.push_back(ptr); |
| 149 | + std::cout << "Allocated memory at " << ptr |
| 150 | + << " with allocation size: " << alloc_size << std::endl; |
| 151 | + return 0; |
| 152 | +} |
| 153 | + |
| 154 | +int umf_free(TestState &test_state) { |
| 155 | + std::cout << "Begin releasing pool memory" << std::endl; |
| 156 | + for (auto &pool : test_state.pools) { |
| 157 | + if (pool.second.empty()) { |
| 158 | + continue; |
| 159 | + } else { |
| 160 | + umfFree(pool.second.back()); |
| 161 | + pool.second.pop_back(); |
| 162 | + std::cout << "Freed memory from the pool at: " << pool.second.back() |
| 163 | + << std::endl; |
| 164 | + break; |
| 165 | + } |
| 166 | + std::cout << "No pool memory to free" << std::endl; |
| 167 | + return -1; |
| 168 | + } |
| 169 | + return 0; |
| 170 | +} |
| 171 | + |
| 172 | +void cleanup(TestState &test_state) { |
| 173 | + std::cout << "Begin cleanup state" << std::endl; |
| 174 | + for (auto &alloc : test_state.provider_memory_allocations) { |
| 175 | + umfMemoryProviderFree(test_state.provider, alloc.first, alloc.second); |
| 176 | + } |
| 177 | + |
| 178 | + for (auto &pool_entry : test_state.pools) { |
| 179 | + for (auto &ptr : pool_entry.second) { |
| 180 | + umfFree(ptr); |
| 181 | + } |
| 182 | + umfPoolDestroy(pool_entry.first); |
| 183 | + } |
| 184 | + std::cout << "Freed all allocated memory from provider and pools and " |
| 185 | + "destroyed all pools" |
| 186 | + << std::endl; |
| 187 | + umfMemoryProviderDestroy(test_state.provider); |
| 188 | + std::cout << "Destroyed the provider" << std::endl; |
| 189 | +} |
| 190 | + |
| 191 | +extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
| 192 | + int next_api_call; |
| 193 | + auto data_provider = std::make_unique<FuzzedDataProvider>(data, size); |
| 194 | + TestState test_state(std::move(data_provider)); |
| 195 | + int ret = -1; |
| 196 | + |
| 197 | + // clang-format off |
| 198 | + int (*api_wrappers[])(TestState &) = { |
| 199 | + umf_memory_provider_alloc, |
| 200 | + umf_memory_provider_free, |
| 201 | + umf_pool_create, |
| 202 | + umf_pool_destroy, |
| 203 | + umf_pool_malloc, |
| 204 | + umf_free, |
| 205 | + }; |
| 206 | + // clang-format on |
| 207 | + umf_memory_provider_create(test_state); |
| 208 | + |
| 209 | + while ((next_api_call = test_state.get_next_api_call()) != -1) { |
| 210 | + ret = api_wrappers[next_api_call](test_state); |
| 211 | + if (ret) { |
| 212 | + cleanup(test_state); |
| 213 | + return -1; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + cleanup(test_state); |
| 218 | + return 0; |
| 219 | +} |
| 220 | +} // namespace fuzz |
0 commit comments