Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28fd2ca

Browse files
committed
[BOLT] Fix trap value for non-X86
The trap value used by BOLT was assumed to be single-byte instruction. It made some functions unaligned on AArch64(e.g exceptions-instrumentation test) and caused emission failures. Fix that by changing fill value to StringRef. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D158191
1 parent 5042e1e commit 28fd2ca

6 files changed

Lines changed: 21 additions & 8 deletions

File tree

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,12 @@ class MCPlusBuilder {
639639
return false;
640640
}
641641

642-
/// If non-zero, this is used to fill the executable space with instructions
643-
/// that will trap. Defaults to 0.
644-
virtual unsigned getTrapFillValue() const { return 0; }
642+
/// Used to fill the executable space with instructions
643+
/// that will trap.
644+
virtual StringRef getTrapFillValue() const {
645+
llvm_unreachable("not implemented");
646+
return StringRef();
647+
}
645648

646649
/// Interface and basic functionality of a MCInstMatcher. The idea is to make
647650
/// it easy to match one or more MCInsts against a tree-like pattern and

bolt/lib/Core/BinaryEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
380380
}
381381

382382
if (opts::MarkFuncs)
383-
Streamer.emitIntValue(BC.MIB->getTrapFillValue(), 1);
383+
Streamer.emitBytes(BC.MIB->getTrapFillValue());
384384

385385
// Emit CFI end
386386
if (Function.hasCFI())
@@ -424,7 +424,7 @@ void BinaryEmitter::emitFunctionBody(BinaryFunction &BF, FunctionFragment &FF,
424424
// case, the call site entries in that LSDA have 0 as offset to the landing
425425
// pad, which the runtime interprets as "no handler". To prevent this,
426426
// insert some padding.
427-
Streamer.emitIntValue(BC.MIB->getTrapFillValue(), 1);
427+
Streamer.emitBytes(BC.MIB->getTrapFillValue());
428428
}
429429

430430
// Track the first emitted instruction with debug info.

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5286,8 +5286,10 @@ void RewriteInstance::rewriteFile() {
52865286
if (!BF.getFileOffset() || !BF.isEmitted())
52875287
continue;
52885288
OS.seek(BF.getFileOffset());
5289-
for (unsigned I = 0; I < BF.getMaxSize(); ++I)
5290-
OS.write((unsigned char)BC->MIB->getTrapFillValue());
5289+
StringRef TrapInstr = BC->MIB->getTrapFillValue();
5290+
unsigned NInstr = BF.getMaxSize() / TrapInstr.size();
5291+
for (unsigned I = 0; I < NInstr; ++I)
5292+
OS.write(TrapInstr.data(), TrapInstr.size());
52915293
}
52925294
OS.seek(SavedPos);
52935295
}

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,10 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
11341134
}
11351135
}
11361136

1137+
StringRef getTrapFillValue() const override {
1138+
return StringRef("\0\0\0\0", 4);
1139+
}
1140+
11371141
bool createReturn(MCInst &Inst) const override {
11381142
Inst.setOpcode(AArch64::RET);
11391143
Inst.clear();

bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ class RISCVMCPlusBuilder : public MCPlusBuilder {
171171
return true;
172172
}
173173

174+
StringRef getTrapFillValue() const override {
175+
return StringRef("\0\0\0\0", 4);
176+
}
177+
174178
bool analyzeBranch(InstructionIterator Begin, InstructionIterator End,
175179
const MCSymbol *&TBB, const MCSymbol *&FBB,
176180
MCInst *&CondBranch,

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ class X86MCPlusBuilder : public MCPlusBuilder {
397397
}
398398
}
399399

400-
unsigned getTrapFillValue() const override { return 0xCC; }
400+
StringRef getTrapFillValue() const override { return StringRef("\314", 1); }
401401

402402
struct IndJmpMatcherFrag1 : MCInstMatcher {
403403
std::unique_ptr<MCInstMatcher> Base;

0 commit comments

Comments
 (0)