Only call default hook if neither entry nor exit hook is given for syscall

This commit is contained in:
York Jasper Niebuhr 2025-09-14 21:21:14 +02:00
parent 58bf9c77da
commit 72caec00d6
3 changed files with 22 additions and 38 deletions

View File

@ -117,44 +117,30 @@ void Environment::register_default_hooks(hook_t entry, hook_t exit) {
m_default_hooks.exit = exit; m_default_hooks.exit = exit;
} }
bool Environment::entry_hook(reg_t syscall) const { void Environment::entry_hook(reg_t syscall) const {
if (!m_hooks.contains(syscall)) if (!m_hooks.contains(syscall)) {
return false; if (m_default_hooks.entry)
m_default_hooks.entry();
return;
}
const HookPair& hooks = m_hooks.at(syscall); const HookPair& hooks = m_hooks.at(syscall);
if (!hooks.entry) if (hooks.entry)
return false; hooks.entry();
hooks.entry();
return true;
} }
bool Environment::exit_hook(reg_t syscall) const { void Environment::exit_hook(reg_t syscall) const {
if (!m_hooks.contains(syscall)) if (!m_hooks.contains(syscall)) {
return false; if (m_default_hooks.exit)
m_default_hooks.exit();
return;
}
const HookPair& hooks = m_hooks.at(syscall); const HookPair& hooks = m_hooks.at(syscall);
if (!hooks.exit) if (hooks.exit)
return false; hooks.exit();
hooks.exit();
return true;
}
bool Environment::default_entry_hook() const {
if (!m_default_hooks.entry)
return false;
m_default_hooks.entry();
return true;
}
bool Environment::default_exit_hook() const {
if (!m_default_hooks.exit)
return false;
m_default_hooks.exit();
return true;
} }
TraceContextCollection& Environment::contexts() { TraceContextCollection& Environment::contexts() {

View File

@ -61,10 +61,8 @@ public:
void register_hooks(reg_t syscall, hook_t entry, hook_t exit); void register_hooks(reg_t syscall, hook_t entry, hook_t exit);
void register_default_hooks(hook_t entry, hook_t exit); void register_default_hooks(hook_t entry, hook_t exit);
bool entry_hook(reg_t syscall) const; void entry_hook(reg_t syscall) const;
bool exit_hook(reg_t syscall) const; void exit_hook(reg_t syscall) const;
bool default_entry_hook() const;
bool default_exit_hook() const;
TraceContextCollection& contexts(); TraceContextCollection& contexts();
const TraceContextCollection& contexts() const; const TraceContextCollection& contexts() const;

View File

@ -166,9 +166,9 @@ bool rewire_run(int argc, const char* const* argv) {
reg_t syscall = ctx->regs.orig_rax; reg_t syscall = ctx->regs.orig_rax;
if (ctx->mode == TraceContext::EXECUTION_MODE::USER) if (ctx->mode == TraceContext::EXECUTION_MODE::USER)
ENV.entry_hook(syscall) || ENV.default_entry_hook(); // Short-circuit guaranteed by C++ standard ;) ENV.entry_hook(syscall);
else if (ctx->mode == TraceContext::EXECUTION_MODE::KERNEL) else if (ctx->mode == TraceContext::EXECUTION_MODE::KERNEL)
ENV.exit_hook(syscall) || ENV.default_exit_hook(); ENV.exit_hook(syscall);
if (ctx->regs_dirty) { if (ctx->regs_dirty) {
if (ptrace(PTRACE_SETREGS, pid, NULL, &ctx->regs) != 0) if (ptrace(PTRACE_SETREGS, pid, NULL, &ctx->regs) != 0)