Fix misaligned pointer returned in heap allocator

Signed-off-by: Aravind Gopalakrishnan <aravind.gopalakrishnan@intel.com>
This commit is contained in:
Aravind Gopalakrishnan
2022-11-02 22:40:45 +00:00
committed by Compute-Runtime-Automation
parent 52b1d92193
commit 3b4695460a
2 changed files with 30 additions and 1 deletions

View File

@@ -192,6 +192,10 @@ class HeapAllocator {
auto ptr = freedChunks[bestFitIndex].ptr + sizeDelta;
freedChunks[bestFitIndex].size = sizeDelta;
if (!isAligned(ptr, requiredAlignment)) {
return 0llu;
}
return ptr;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 Intel Corporation
* Copyright (C) 2018-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -174,6 +174,31 @@ TEST(HeapAllocatorTest, GivenOnlyMoreThanTwiceBiggerSizeChunksInFreedChunksWhenG
EXPECT_EQ(deltaSize, freedChunks[2].size);
}
TEST(HeapAllocatorTest, GivenMoreThanTwiceBiggerSizeChunksInFreedChunksWhenGetIsCalledAndAlignmentDoesNotThenNullIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pLowerBound = ptrBase;
auto allocAlign = 8162u;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, allocationAlignment, sizeThreshold);
std::vector<HeapChunk> freedChunks;
uint64_t ptrExpected = 0llu;
size_t requestedSize = 2 * 4096;
freedChunks.emplace_back(pLowerBound, 9 * 4096);
pLowerBound += 9 * 4096;
freedChunks.emplace_back(pLowerBound, 3 * 4096);
EXPECT_EQ(2u, freedChunks.size());
auto ptrReturned = heapAllocator->getFromFreedChunks(requestedSize, freedChunks, allocAlign);
EXPECT_EQ(ptrExpected, ptrReturned);
EXPECT_EQ(2u, freedChunks.size());
}
TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToLeftBoundaryOfIncomingChunkWhenStoreIsCalledThenChunkIsMerged) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;