diff --git a/src/provider/provider_file_memory.c b/src/provider/provider_file_memory.c index 5cc377f32..12a923322 100644 --- a/src/provider/provider_file_memory.c +++ b/src/provider/provider_file_memory.c @@ -429,6 +429,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider, return UMF_RESULT_ERROR_UNKNOWN; } + assert(file_provider->offset_mmap <= file_provider->size_mmap); + if (file_provider->size_mmap - file_provider->offset_mmap < size) { umf_result = file_mmap_aligned(file_provider, size, alignment); if (umf_result != UMF_RESULT_SUCCESS) { @@ -454,7 +456,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider, size_t new_offset_fd = file_provider->offset_fd + new_offset_mmap - file_provider->offset_mmap; - if (file_provider->size_mmap - new_offset_mmap < size) { + // new_offset_mmap can be greater than file_provider->size_mmap + if (file_provider->size_mmap < size + new_offset_mmap) { umf_result = file_mmap_aligned(file_provider, size, alignment); if (umf_result != UMF_RESULT_SUCCESS) { utils_mutex_unlock(&file_provider->lock); diff --git a/test/poolFixtures.hpp b/test/poolFixtures.hpp index de5a54685..5f39e021d 100644 --- a/test/poolFixtures.hpp +++ b/test/poolFixtures.hpp @@ -136,7 +136,8 @@ TEST_P(umfPoolTest, allocFree) { auto *ptr = umfPoolMalloc(pool.get(), allocSize); ASSERT_NE(ptr, nullptr); std::memset(ptr, 0, allocSize); - umfPoolFree(pool.get(), ptr); + umf_result_t umf_result = umfPoolFree(pool.get(), ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } TEST_P(umfPoolTest, allocFreeNonAlignedSizes) { @@ -144,10 +145,33 @@ TEST_P(umfPoolTest, allocFreeNonAlignedSizes) { auto *ptr = umfPoolMalloc(pool.get(), allocSize); ASSERT_NE(ptr, nullptr); std::memset(ptr, 0, allocSize); - umfPoolFree(pool.get(), ptr); + umf_result_t umf_result = umfPoolFree(pool.get(), ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } } +TEST_P(umfPoolTest, allocFreeAligned) { +// ::aligned_alloc(alignment=4096, size=1) does not work under sanitizers for unknown reason +#if defined(_WIN32) || defined(__SANITIZE_ADDRESS__) || \ + defined(__SANITIZE_THREAD__) + // TODO: implement support for windows + GTEST_SKIP(); +#else + if (!umf_test::isAlignedAllocSupported(pool.get())) { + GTEST_SKIP(); + } + + size_t alignment = 4 * 1024; // 4kB + void *ptr = umfPoolAlignedMalloc(pool.get(), 1, alignment); + ASSERT_NE(ptr, nullptr); + ASSERT_TRUE(reinterpret_cast(ptr) % alignment == 0); + *(reinterpret_cast(ptr)) = (unsigned char)0xFF; + + umf_result_t umf_result = umfPoolFree(pool.get(), ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); +#endif +} + TEST_P(umfPoolTest, reallocFree) { if (!umf_test::isReallocSupported(pool.get())) { GTEST_SKIP(); @@ -160,7 +184,8 @@ TEST_P(umfPoolTest, reallocFree) { auto *new_ptr = umfPoolRealloc(pool.get(), ptr, allocSize * multiplier); ASSERT_NE(new_ptr, nullptr); std::memset(new_ptr, 0, allocSize * multiplier); - umfPoolFree(pool.get(), new_ptr); + umf_result_t umf_result = umfPoolFree(pool.get(), new_ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } TEST_P(umfPoolTest, callocFree) { @@ -174,7 +199,8 @@ TEST_P(umfPoolTest, callocFree) { for (size_t i = 0; i < num; ++i) { ASSERT_EQ(((int *)ptr)[i], 0); } - umfPoolFree(pool.get(), ptr); + umf_result_t umf_result = umfPoolFree(pool.get(), ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } void pow2AlignedAllocHelper(umf_memory_pool_handle_t pool) { @@ -195,9 +221,31 @@ void pow2AlignedAllocHelper(umf_memory_pool_handle_t pool) { } for (auto &ptr : allocs) { - umfPoolFree(pool, ptr); + umf_result_t umf_result = umfPoolFree(pool, ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } } + +// ::aligned_alloc(alignment=4096, size=1) does not work under sanitizers for unknown reason +#if !defined(__SANITIZE_ADDRESS__) && !defined(__SANITIZE_THREAD__) + // the same for size = 1 + for (size_t alignment = 1; alignment <= maxAlignment; alignment <<= 1) { + std::vector allocs; + + for (size_t alloc = 0; alloc < numAllocs; alloc++) { + auto *ptr = umfPoolAlignedMalloc(pool, 1, alignment); + ASSERT_NE(ptr, nullptr); + ASSERT_TRUE(reinterpret_cast(ptr) % alignment == 0); + *(reinterpret_cast(ptr)) = (unsigned char)0xFF; + allocs.push_back(ptr); + } + + for (auto &ptr : allocs) { + umf_result_t umf_result = umfPoolFree(pool, ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); + } + } +#endif } TEST_P(umfPoolTest, pow2AlignedAlloc) { @@ -227,7 +275,8 @@ TEST_P(umfPoolTest, multiThreadedMallocFree) { } for (auto allocation : allocations) { - umfPoolFree(inPool, allocation); + umf_result_t umf_result = umfPoolFree(inPool, allocation); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } }; @@ -280,7 +329,8 @@ TEST_P(umfPoolTest, multiThreadedReallocFree) { for (auto allocation : allocations) { auto *ptr = umfPoolRealloc(inPool, allocation, allocSize * multiplier); - umfPoolFree(inPool, ptr); + umf_result_t umf_result = umfPoolFree(inPool, ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } }; @@ -310,7 +360,8 @@ TEST_P(umfPoolTest, multiThreadedCallocFree) { } for (auto allocation : allocations) { - umfPoolFree(inPool, allocation); + umf_result_t umf_result = umfPoolFree(inPool, allocation); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } }; @@ -335,7 +386,8 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) { } for (auto allocation : allocations) { - umfPoolFree(inPool, allocation); + umf_result_t umf_result = umfPoolFree(inPool, allocation); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } }; @@ -375,7 +427,8 @@ TEST_P(umfMemTest, outOfMem) { ASSERT_NE(allocations.back(), nullptr); for (int i = 0; i < expectedRecycledPoolAllocs; i++) { - umfPoolFree(hPool, allocations.back()); + umf_result_t umf_result = umfPoolFree(hPool, allocations.back()); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); allocations.pop_back(); } @@ -385,7 +438,8 @@ TEST_P(umfMemTest, outOfMem) { } for (auto allocation : allocations) { - umfPoolFree(hPool, allocation); + umf_result_t umf_result = umfPoolFree(hPool, allocation); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } } @@ -490,7 +544,8 @@ TEST_P(umfPoolTest, mallocUsableSize) { // Make sure we can write to this memory memset(ptr, 123, result); - umfPoolFree(pool.get(), ptr); + umf_result_t umf_result = umfPoolFree(pool.get(), ptr); + ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } } }