From 22ed1be1a31351eb0c751bcdf8d447f0eb9d2041 Mon Sep 17 00:00:00 2001 From: John Falkowski Date: Mon, 28 Feb 2022 17:12:41 +0000 Subject: [PATCH] Add checks for mmap and getSpace Signed-off-by: John Falkowski --- shared/source/command_stream/linear_stream.h | 1 + .../unit_test/command_stream/linear_stream_tests.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/shared/source/command_stream/linear_stream.h b/shared/source/command_stream/linear_stream.h index 6243e512a3..7eee2a7718 100644 --- a/shared/source/command_stream/linear_stream.h +++ b/shared/source/command_stream/linear_stream.h @@ -62,6 +62,7 @@ inline void *LinearStream::getSpace(size_t size) { cmdContainer->closeAndAllocateNextCommandBuffer(); } UNRECOVERABLE_IF(sizeUsed + size > maxAvailableSpace); + UNRECOVERABLE_IF(reinterpret_cast(buffer) <= 0); auto memory = ptrOffset(buffer, sizeUsed); sizeUsed += size; return memory; diff --git a/shared/test/unit_test/command_stream/linear_stream_tests.cpp b/shared/test/unit_test/command_stream/linear_stream_tests.cpp index a65c92b087..417ee1b144 100644 --- a/shared/test/unit_test/command_stream/linear_stream_tests.cpp +++ b/shared/test/unit_test/command_stream/linear_stream_tests.cpp @@ -37,6 +37,17 @@ TEST_F(LinearStreamTest, GivenSizeUint32WhenGettingSpaceUsedThenNonNullPointerIs EXPECT_NE(nullptr, linearStream.getSpace(sizeof(uint32_t))); } +TEST_F(LinearStreamTest, GivenNullBufferWhenGettingSpaceThenAssert) { + linearStream.replaceBuffer(nullptr, 100); + EXPECT_THROW(linearStream.getSpace(1), std::exception); +} + +TEST_F(LinearStreamTest, GivenBadBufferPtrWhenGettingSpaceThenAssert) { + int64_t ptr = -1; + linearStream.replaceBuffer(reinterpret_cast(ptr), 100); + EXPECT_THROW(linearStream.getSpace(1), std::exception); +} + TEST_F(LinearStreamTest, WhenAllocatingMultipleTimesThenPointersIncrementedCorrectly) { size_t allocSize = 1; auto ptr1 = linearStream.getSpace(allocSize);