fix: stop parsing printf buffer after invalid string index

- IGC stores invalid string index at the end of printf buffer,
invalid index is equal to (int32_t)-1 extended to (char*)
- printKernelOutput() should not parse buffer after invalid index was
found to avoid invalid string pointer dereference

Related-To: NEO-5753

Signed-off-by: Mateusz Hoppe <mateusz.hoppe@intel.com>
This commit is contained in:
Mateusz Hoppe 2023-05-27 16:44:07 +00:00 committed by Compute-Runtime-Automation
parent 1bbaffc31b
commit f8b375cae5
2 changed files with 22 additions and 0 deletions

View File

@ -45,6 +45,9 @@ void PrintFormatter::printKernelOutput(const std::function<void(char *)> &print)
while (currentOffset + sizeof(char *) <= printfOutputBufferSize) {
char *formatString = nullptr;
read(&formatString);
if (formatString == reinterpret_cast<char *>(static_cast<uintptr_t>(0xffffffff))) {
break;
}
if (formatString != nullptr) {
printString(formatString, print);
}

View File

@ -985,6 +985,25 @@ TEST_F(PrintFormatterTest, GivenNoStringMapAndBufferWithFormatStringAnd2StringsT
EXPECT_STREQ(expectedOutput, output);
}
TEST_F(PrintFormatterTest, GivenNoStringMapAndInvalidStringIndexStoredWhenPrintingOutputThenBufferParsingStopsAfterInvalidIndex) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *string1 = "str1";
storeData(string1);
const int invalidIndex = -1;
const int zero = 0;
storeData(invalidIndex);
storeData(zero);
const char *string2 = "str2";
storeData(string2);
const char *expectedOutput = "str1";
char output[maxPrintfOutputLength];
printFormatter->printKernelOutput([&output](char *str) { strncpy_s(output, maxPrintfOutputLength, str, maxPrintfOutputLength - 1); });
EXPECT_STREQ(expectedOutput, output);
}
TEST_F(PrintFormatterTest, GivenTypeSmallerThan4BThenItIsReadAs4BValue) {
printFormatter.reset(new PrintFormatter(static_cast<uint8_t *>(data->getUnderlyingBuffer()), printfBufferSize, true));
const char *formatString = "%c %hd %d";