Merge pull request #1457 from drk1rd/fix/relinker-ud1-trap

fix(relinker): decode UD1 as a trap
This commit is contained in:
Adrià
2026-10-08 16:17:06 +02:00
committed by GitHub
3 changed files with 21 additions and 0 deletions
@@ -481,6 +481,12 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction(
info.FlowKind = ControlFlowKind::Trap;
return info;
}
if (op == TwoByteUd1) {
if (pos < info.Length)
readModRm(pos, false);
info.FlowKind = ControlFlowKind::Trap;
return info;
}
if (op >= TwoByteJccRel32Min && op <= TwoByteJccRel32Max) {
std::int32_t disp = 0;
std::memcpy(&disp, data + pos, 4);
@@ -168,6 +168,9 @@ void decoderRipRelative() {
}
for (const auto& instruction : kRegisterVectorOperations)
require(!decoder.DecodeInstruction(instruction.data(), instruction.size()).HasRipRelativeDisp, "Vector instruction without a RIP-relative operand was reported as RIP-relative");
const Bytes ud1 = {0x0F, 0xB9, 0x05, 0x10, 0x00, 0x00, 0x00};
const auto trap = decoder.DecodeInstruction(ud1.data(), ud1.size());
require(trap.Length == ud1.size() && trap.FlowKind == Codegen::ControlFlowKind::Trap && trap.HasRipRelativeDisp && read<std::int32_t>(ud1, trap.RipRelativeDispOffset) == 0x10, "UD1 was not decoded as a trap with its RIP-relative operand");
}
void decoderTwoByteOpcodeLengths() {
@@ -68,6 +68,17 @@ void deadTailAndExplicitEntry() {
require(live.ImportSlots == std::set<std::uint64_t>{0x2000}, "Explicit entry after UD2 lost its import");
}
void ud1DeadTail() {
auto input = fixture();
emit(input, 0, {0x0F, 0xB9, 0xC0});
importThunk(input, 16, 0x2000);
require(AnalyzeStrictReachability(input).ImportSlots.empty(), "UD1 fallthrough retained a dead import");
input = fixture();
ripOperand(input, 0, {0x0F, 0xB9, 0x05}, 0x3000);
importThunk(input, 16, 0x2000);
require(AnalyzeStrictReachability(input).ImportSlots.empty(), "UD1 with a RIP-relative operand retained a dead import");
}
void conditionalTailCall() {
auto input = fixture();
emit(input, 0, {0x75, 0x0E, 0xC3});
@@ -283,6 +294,7 @@ void filterAndPltCompaction() {
int main() {
try {
deadTailAndExplicitEntry();
ud1DeadTail();
conditionalTailCall();
callbackAndRelocationRoots();
registerImportCall();