Skip to content

Commit 30a9cb3

Browse files
committed
[umf] fix erors reported by ASAN
- memory leak in memory_pool_default.c and tests - allocating memory with alignemnt=8 and size%8 != 0
1 parent ae807d8 commit 30a9cb3

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

source/common/unified_malloc_framework/src/memory_pool_default.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum umf_result_t umfPoolCreate(const struct umf_memory_pool_ops_t *ops,
5656
return UMF_RESULT_SUCCESS;
5757

5858
err_pool_init:
59+
free(pool->providers);
5960
err_providers_alloc:
6061
free(pool);
6162

@@ -64,6 +65,7 @@ enum umf_result_t umfPoolCreate(const struct umf_memory_pool_ops_t *ops,
6465

6566
void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
6667
hPool->ops.finalize(hPool->pool_priv);
68+
free(hPool->providers);
6769
free(hPool);
6870
}
6971

test/unified_malloc_framework/common/pool.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,43 @@ auto wrapPoolUnique(umf_memory_pool_handle_t hPool) {
3232
}
3333

3434
bool isReallocSupported(umf_memory_pool_handle_t hPool) {
35-
static constexpr size_t allocSize = 1;
35+
static constexpr size_t allocSize = 8;
3636
bool supported;
3737
auto *ptr = umfPoolMalloc(hPool, allocSize);
3838
auto *new_ptr = umfPoolRealloc(hPool, ptr, allocSize * 2);
3939

4040
if (new_ptr) {
4141
supported = true;
42+
umfPoolFree(hPool, new_ptr);
4243
} else if (umfPoolGetLastAllocationError(hPool) ==
4344
UMF_RESULT_ERROR_NOT_SUPPORTED) {
45+
umfPoolFree(hPool, ptr);
4446
supported = false;
4547
} else {
48+
umfPoolFree(hPool, new_ptr);
4649
throw std::runtime_error("realloc failed with unexpected error");
4750
}
4851

49-
umfPoolFree(hPool, new_ptr);
50-
5152
return supported;
5253
}
5354

5455
bool isCallocSupported(umf_memory_pool_handle_t hPool) {
55-
static constexpr size_t num = 1;
56+
static constexpr size_t num = 8;
5657
static constexpr size_t size = sizeof(int);
5758
bool supported;
5859
auto *ptr = umfPoolCalloc(hPool, num, size);
5960

6061
if (ptr) {
6162
supported = true;
63+
umfPoolFree(hPool, ptr);
6264
} else if (umfPoolGetLastAllocationError(hPool) ==
6365
UMF_RESULT_ERROR_NOT_SUPPORTED) {
6466
supported = false;
6567
} else {
68+
umfPoolFree(hPool, ptr);
6669
throw std::runtime_error("calloc failed with unexpected error");
6770
}
6871

69-
umfPoolFree(hPool, ptr);
70-
7172
return supported;
7273
}
7374

test/unified_malloc_framework/memoryPool.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
243243

244244
std::vector<std::thread> threads;
245245
for (int i = 0; i < NTHREADS; i++) {
246-
threads.emplace_back(poolMalloc, rand() % 64 + 1, pool.get());
246+
threads.emplace_back(poolMalloc, (rand() % 16) * 8, pool.get());
247247
}
248248

249249
for (auto &thread : threads) {

test/unified_malloc_framework/memoryPoolAPI.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ TEST_F(test, getLastFailedMemoryProvider) {
258258
auto ptr = umfPoolMalloc(pool.get(), allocSize);
259259
ASSERT_NE(ptr, nullptr);
260260
ASSERT_EQ(umfGetLastFailedMemoryProvider(), nullptr);
261+
umfPoolFree(pool.get(), ptr);
261262

262263
// make provider return an error during allocation
263264
allocResult = UMF_RESULT_ERROR_UNKNOWN;
@@ -280,6 +281,7 @@ TEST_F(test, getLastFailedMemoryProvider) {
280281
ASSERT_EQ(std::string_view(
281282
umfMemoryProviderGetName(umfGetLastFailedMemoryProvider())),
282283
"provider2");
284+
umfPoolFree(pool.get(), ptr);
283285

284286
// error in another thread should not impact umfGetLastFailedMemoryProvider on this thread
285287
allocResult = UMF_RESULT_ERROR_UNKNOWN;

0 commit comments

Comments
 (0)