diff --git a/runtime/kernel/kernel.cpp b/runtime/kernel/kernel.cpp index 5c5b48ea25..a821cb843b 100644 --- a/runtime/kernel/kernel.cpp +++ b/runtime/kernel/kernel.cpp @@ -708,16 +708,18 @@ void Kernel::substituteKernelHeap(void *newKernelHeap, size_t newKernelHeapSize) SKernelBinaryHeaderCommon *pHeader = const_cast(pKernelInfo->heapInfo.pKernelHeader); pHeader->KernelHeapSize = static_cast(newKernelHeapSize); pKernelInfo->isKernelHeapSubstituted = true; + auto memoryManager = device.getMemoryManager(); auto currentAllocationSize = pKernelInfo->kernelAllocation->getUnderlyingBufferSize(); + bool status = false; if (currentAllocationSize >= newKernelHeapSize) { - memcpy_s(pKernelInfo->kernelAllocation->getUnderlyingBuffer(), newKernelHeapSize, newKernelHeap, newKernelHeapSize); + status = memoryManager->copyMemoryToAllocation(pKernelInfo->kernelAllocation, newKernelHeap, static_cast(newKernelHeapSize)); } else { - auto memoryManager = device.getMemoryManager(); memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(pKernelInfo->kernelAllocation); pKernelInfo->kernelAllocation = nullptr; - pKernelInfo->createKernelAllocation(memoryManager); + status = pKernelInfo->createKernelAllocation(memoryManager); } + UNRECOVERABLE_IF(!status); } bool Kernel::isKernelHeapSubstituted() const { diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index a4c6174b31..fa397d1591 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -406,7 +406,7 @@ HeapIndex MemoryManager::selectHeap(const GraphicsAllocation *allocation, const return HeapIndex::HEAP_LIMITED; } bool MemoryManager::copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, const void *memoryToCopy, uint32_t sizeToCopy) const { - if (!graphicsAllocation || !memoryToCopy || sizeToCopy == 0u) { + if (!graphicsAllocation->getUnderlyingBuffer()) { return false; } memcpy_s(graphicsAllocation->getUnderlyingBuffer(), graphicsAllocation->getUnderlyingBufferSize(), memoryToCopy, sizeToCopy); diff --git a/runtime/program/kernel_info.cpp b/runtime/program/kernel_info.cpp index 4efcf9021d..6e2e785e45 100644 --- a/runtime/program/kernel_info.cpp +++ b/runtime/program/kernel_info.cpp @@ -488,6 +488,9 @@ bool KernelInfo::createKernelAllocation(MemoryManager *memoryManager) { UNRECOVERABLE_IF(kernelAllocation); auto kernelIsaSize = heapInfo.pKernelHeader->KernelHeapSize; kernelAllocation = memoryManager->allocateGraphicsMemoryWithProperties({kernelIsaSize, GraphicsAllocation::AllocationType::KERNEL_ISA}); + if (!kernelAllocation) { + return false; + } return memoryManager->copyMemoryToAllocation(kernelAllocation, heapInfo.pKernelHeap, kernelIsaSize); } diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 3c0f0c1a9b..fe3f2dd6fc 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -1723,16 +1723,12 @@ TEST_F(MemoryAllocatorTest, whenCommandStreamerIsNotRegisteredThenReturnNullEngi auto engineControl = memoryManager->getRegisteredEngineForCsr(dummyCsr); EXPECT_EQ(nullptr, engineControl); } -TEST(MemoryManagerCopyMemoryTest, givenNullPointerOrZeroSizeWhenCopyMemoryToAllocationThenReturnFalse) { +TEST(MemoryManagerCopyMemoryTest, givenAllocationWithNoStorageWhenCopyMemoryToAllocationThenReturnFalse) { ExecutionEnvironment executionEnvironment; MockMemoryManager memoryManager(false, false, executionEnvironment); - constexpr uint8_t allocationSize = 10; - uint8_t allocationStorage[allocationSize]; - MockGraphicsAllocation allocation{allocationStorage, allocationSize}; uint8_t memory = 1; - EXPECT_FALSE(memoryManager.copyMemoryToAllocation(nullptr, &memory, sizeof(memory))); - EXPECT_FALSE(memoryManager.copyMemoryToAllocation(&allocation, nullptr, sizeof(memory))); - EXPECT_FALSE(memoryManager.copyMemoryToAllocation(&allocation, &memory, 0u)); + MockGraphicsAllocation invalidAllocation{nullptr, 0u}; + EXPECT_FALSE(memoryManager.copyMemoryToAllocation(&invalidAllocation, &memory, sizeof(memory))); } TEST(MemoryManagerCopyMemoryTest, givenValidAllocationAndMemoryWhenCopyMemoryToAllocationThenDataIsCopied) { ExecutionEnvironment executionEnvironment;