Limit access to the printf buffer

Change-Id: Ic212f66ebedc374316c4ad8e32b708ae445f2276
Signed-off-by: Koska <andrzej.koska@intel.com>
Related-To: NEO-4595
This commit is contained in:
Koska
2020-04-28 17:05:39 +02:00
committed by sys_ocldev
parent f5211fa0bd
commit 68f7e67e31
2 changed files with 18 additions and 1 deletions

View File

@ -269,6 +269,21 @@ TEST_P(PrintfUint32Test, GivenPrintfFormatWhenConatinsUintThenInstertValueIntoSt
EXPECT_STREQ(referenceOutput, actualOutput); EXPECT_STREQ(referenceOutput, actualOutput);
} }
TEST_P(PrintfUint32Test, GivenPrintfFormatWhenBufferSizeExceedsPrintfBufferCapacityThenUseSmallerValue) {
auto input = GetParam();
printFormatter = std::unique_ptr<PrintFormatter>(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), 0, is32bit, kernelInfo->patchInfo.stringDataMap));
auto stringIndex = injectFormatString(input.format);
storeData(stringIndex);
injectValue(input.value);
char referenceOutput[PrintFormatter::maxPrintfOutputLength] = "";
char actualOutput[PrintFormatter::maxPrintfOutputLength] = "";
printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, PrintFormatter::maxPrintfOutputLength, str, PrintFormatter::maxPrintfOutputLength); });
EXPECT_STREQ(referenceOutput, actualOutput);
}
INSTANTIATE_TEST_CASE_P(PrintfUint32Test, INSTANTIATE_TEST_CASE_P(PrintfUint32Test,
PrintfUint32Test, PrintfUint32Test,
::testing::ValuesIn(uintValues)); ::testing::ValuesIn(uintValues));

View File

@ -25,7 +25,9 @@ void PrintFormatter::printKernelOutput(const std::function<void(char *)> &print)
currentOffset = 0; currentOffset = 0;
// first 4 bytes of the buffer store the actual size of data that was written by printf from within EUs // first 4 bytes of the buffer store the actual size of data that was written by printf from within EUs
read(&printfOutputBufferSize); uint32_t printfOutputBufferSizeRead = 0;
read(&printfOutputBufferSizeRead);
printfOutputBufferSize = std::min(printfOutputBufferSizeRead, printfOutputBufferSize);
uint32_t stringIndex = 0; uint32_t stringIndex = 0;
while (currentOffset + 4 <= printfOutputBufferSize) { while (currentOffset + 4 <= printfOutputBufferSize) {