Skip to content

Commit

Permalink
utils
Browse files Browse the repository at this point in the history
  • Loading branch information
0vercl0k committed May 18, 2024
1 parent 9ea3e43 commit 3fe3992
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/wtf/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ ExceptionCodeToStr(const uint32_t ExceptionCode) {
return "UNKNOWN";
}

[[nodiscard]] bool BreakOnIDTEntries(Backend_t &Backend, const CpuState_t &CpuState) {
[[nodiscard]] std::optional<Gva_t>
ReadIDTEntryHandler(const Backend_t &Backend, const CpuState_t &CpuState,
const size_t Vector) {
struct IdtEntry {
uint16_t Low;
uint16_t Selector;
Expand All @@ -503,16 +505,26 @@ ExceptionCodeToStr(const uint32_t ExceptionCode) {
}
};

const auto Address = Gva_t(CpuState.Idtr.Base + (Vector * sizeof(IdtEntry)));
IdtEntry Entry;
if (!Backend.VirtReadStruct<IdtEntry>(Address, &Entry)) {
return {};
}

return Entry.Handler();
}

[[nodiscard]] bool BreakOnIDTEntries(Backend_t &Backend,
const CpuState_t &CpuState) {
for (size_t Idx = 0; Idx < 256; Idx++) {
const auto Address = Gva_t(CpuState.Idtr.Base + (Idx * sizeof(IdtEntry)));
IdtEntry Entry;
if (!Backend.VirtReadStruct<IdtEntry>(Address, &Entry)) {
const auto Handler = ReadIDTEntryHandler(Backend, CpuState, Idx);
if (!Handler) {
fmt::print("ReadIDTEntryHandler failed\n");
return false;
}

if (!Backend.SetBreakpoint(Entry.Handler(), [](Backend_t *Backend) {
Backend->TrapFlag(true);
})) {
if (!Backend.SetBreakpoint(
*Handler, [](Backend_t *Backend) { Backend->TrapFlag(true); })) {
return false;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/wtf/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,17 @@ SaveFile(const fs::path &Path, const uint8_t *Buffer, const size_t BufferSize);

[[nodiscard]] std::string_view ExceptionCodeToStr(const uint32_t ExceptionCode);

//
// Read the IDT[Vector] handler.
//

[[nodiscard]] std::optional<Gva_t>
ReadIDTEntryHandler(const Backend_t &Backend, const CpuState_t &CpuState,
const size_t Vector);

//
// Set a breakpoint on every IDT handlers that turn on TF.
//

[[nodiscard]] bool BreakOnIDTEntries(Backend_t &Backend,
const CpuState_t &CpuState);

0 comments on commit 3fe3992

Please sign in to comment.