Skip to content

Commit

Permalink
Os(Windows): Fix thread trapping not properly catching the to memory
Browse files Browse the repository at this point in the history
  • Loading branch information
cursey committed Apr 5, 2024
1 parent 4420034 commit 4faf792
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/os.windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ SystemInfo system_info() {
}

struct TrapInfo {
uint8_t* page_start;
uint8_t* page_end;
uint8_t* from_page_start;
uint8_t* from_page_end;
uint8_t* from;
uint8_t* to_page_start;
uint8_t* to_page_end;
uint8_t* to;
size_t len;
};
Expand Down Expand Up @@ -185,20 +187,31 @@ class TrapManager final {
}

TrapInfo* find_trap_page(uint8_t* address) {
auto search = std::find_if(m_traps.begin(), m_traps.end(),
[address](auto& trap) { return address >= trap.second.page_start && address < trap.second.page_end; });
auto search = std::find_if(m_traps.begin(), m_traps.end(), [address](auto& trap) {
return address >= trap.second.from_page_start && address < trap.second.from_page_end;
});

if (search == m_traps.end()) {
return nullptr;
if (search != m_traps.end()) {
return &search->second;
}

return &search->second;
search = std::find_if(m_traps.begin(), m_traps.end(), [address](auto& trap) {
return address >= trap.second.to_page_start && address < trap.second.to_page_end;
});

if (search != m_traps.end()) {
return &search->second;
}

return nullptr;
}

void add_trap(uint8_t* from, uint8_t* to, size_t len) {
m_traps.insert_or_assign(from, TrapInfo{.page_start = align_down(from, 0x1000),
.page_end = align_up(from + len, 0x1000),
m_traps.insert_or_assign(from, TrapInfo{.from_page_start = align_down(from, 0x1000),
.from_page_end = align_up(from + len, 0x1000),
.from = from,
.to_page_start = align_down(to, 0x1000),
.to_page_end = align_up(to + len, 0x1000),
.to = to,
.len = len});
}
Expand Down

0 comments on commit 4faf792

Please sign in to comment.