Skip to content

Commit a9b0d75

Browse files
authored
[BOLT] Properly propagate Cursor errors (#84378)
Handle out-of-bounds reading errors correctly in LinuxKernelRewriter.
1 parent 5669660 commit a9b0d75

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

bolt/lib/Rewrite/LinuxKernelRewriter.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ Error LinuxKernelRewriter::readORCTables() {
500500
// Consume the status of the cursor.
501501
if (!IPCursor)
502502
return createStringError(errc::executable_format_error,
503-
"out of bounds while reading ORC IP table");
503+
"out of bounds while reading ORC IP table: %s",
504+
toString(IPCursor.takeError()).c_str());
504505

505506
if (IP < PrevIP && opts::Verbosity)
506507
BC.errs() << "BOLT-WARNING: out of order IP 0x" << Twine::utohexstr(IP)
@@ -522,7 +523,8 @@ Error LinuxKernelRewriter::readORCTables() {
522523
// Consume the status of the cursor.
523524
if (!ORCCursor)
524525
return createStringError(errc::executable_format_error,
525-
"out of bounds while reading ORC");
526+
"out of bounds while reading ORC: %s",
527+
toString(ORCCursor.takeError()).c_str());
526528

527529
if (Entry.ORC == NullORC)
528530
continue;
@@ -843,7 +845,8 @@ Error LinuxKernelRewriter::readStaticCalls() {
843845
// Consume the status of the cursor.
844846
if (!Cursor)
845847
return createStringError(errc::executable_format_error,
846-
"out of bounds while reading static calls");
848+
"out of bounds while reading static calls: %s",
849+
toString(Cursor.takeError()).c_str());
847850

848851
++EntryID;
849852

@@ -954,8 +957,10 @@ Error LinuxKernelRewriter::readExceptionTable() {
954957

955958
// Consume the status of the cursor.
956959
if (!Cursor)
957-
return createStringError(errc::executable_format_error,
958-
"out of bounds while reading exception table");
960+
return createStringError(
961+
errc::executable_format_error,
962+
"out of bounds while reading exception table: %s",
963+
toString(Cursor.takeError()).c_str());
959964

960965
++EntryID;
961966

@@ -1061,8 +1066,10 @@ Error LinuxKernelRewriter::readParaInstructions() {
10611066
const uint8_t Len = DE.getU8(Cursor);
10621067

10631068
if (!Cursor)
1064-
return createStringError(errc::executable_format_error,
1065-
"out of bounds while reading .parainstructions");
1069+
return createStringError(
1070+
errc::executable_format_error,
1071+
"out of bounds while reading .parainstructions: %s",
1072+
toString(Cursor.takeError()).c_str());
10661073

10671074
++EntryID;
10681075

@@ -1129,7 +1136,8 @@ Error LinuxKernelRewriter::readBugTable() {
11291136

11301137
if (!Cursor)
11311138
return createStringError(errc::executable_format_error,
1132-
"out of bounds while reading __bug_table");
1139+
"out of bounds while reading __bug_table: %s",
1140+
toString(Cursor.takeError()).c_str());
11331141

11341142
++EntryID;
11351143

@@ -1196,8 +1204,10 @@ Error LinuxKernelRewriter::readAltInstructions() {
11961204
const uint8_t PadLen = opts::AltInstHasPadLen ? DE.getU8(Cursor) : 0;
11971205

11981206
if (!Cursor)
1199-
return createStringError(errc::executable_format_error,
1200-
"out of bounds while reading .altinstructions");
1207+
return createStringError(
1208+
errc::executable_format_error,
1209+
"out of bounds while reading .altinstructions: %s",
1210+
toString(Cursor.takeError()).c_str());
12011211

12021212
++EntryID;
12031213

bolt/test/X86/linux-alt-instruction.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
# RUN: llvm-bolt %t.exe --print-normalized --keep-nops \
2828
# RUN: --alt-inst-feature-size=4 -o %t.out | FileCheck %s
2929

30+
## Check that out-of-bounds read is handled properly.
31+
32+
# RUN: not llvm-bolt %t.exe --print-normalized --keep-nops \
33+
# RUN: --alt-inst-feature-size=2 -o %t.out
34+
3035
# CHECK: BOLT-INFO: Linux kernel binary detected
3136
# CHECK: BOLT-INFO: parsed 2 alternative instruction entries
3237

0 commit comments

Comments
 (0)