Skip to content

Commit

Permalink
Detect execution access violation (Linux/Un*x only)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Jan 23, 2021
1 parent 1cf1868 commit 0f04a57
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,21 +1768,24 @@ static void signal_handler(int sig, siginfo_t* info, void* uct) noexcept
x64_context* context = static_cast<ucontext_t*>(uct);

#ifdef __APPLE__
const bool is_writing = context->uc_mcontext->__es.__err & 0x2;
const u64 err = context->uc_mcontext->__es.__err;
#elif defined(__DragonFly__) || defined(__FreeBSD__)
const bool is_writing = context->uc_mcontext.mc_err & 0x2;
const u64 err = context->uc_mcontext.mc_err;
#elif defined(__OpenBSD__)
const bool is_writing = context->sc_err & 0x2;
const u64 err = context->sc_err;
#elif defined(__NetBSD__)
const bool is_writing = context->uc_mcontext.__gregs[_REG_ERR] & 0x2;
const u64 err = context->uc_mcontext.__gregs[_REG_ERR];
#else
const bool is_writing = context->uc_mcontext.gregs[REG_ERR] & 0x2;
const u64 err = context->uc_mcontext.gregs[REG_ERR];
#endif

const bool is_executing = err & 0x10;
const bool is_writing = err & 0x2;

const u64 exec64 = (reinterpret_cast<u64>(info->si_addr) - reinterpret_cast<u64>(vm::g_exec_addr)) / 2;
const auto cause = is_writing ? "writing" : "reading";
const auto cause = is_executing ? "executing" : is_writing ? "writing" : "reading";

if (auto [addr, ok] = vm::try_get_addr(info->si_addr); ok)
if (auto [addr, ok] = vm::try_get_addr(info->si_addr); ok && !is_executing)
{
// Try to process access violation
if (thread_ctrl::get_current() && handle_access_violation(addr, is_writing, context))
Expand All @@ -1791,7 +1794,7 @@ static void signal_handler(int sig, siginfo_t* info, void* uct) noexcept
}
}

if (exec64 < 0x100000000ull)
if (exec64 < 0x100000000ull && !is_executing)
{
if (thread_ctrl::get_current() && handle_access_violation(static_cast<u32>(exec64), is_writing, context))
{
Expand Down

0 comments on commit 0f04a57

Please sign in to comment.