Skip to content

Commit

Permalink
More exception dispatching progress
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Sep 5, 2024
1 parent e5b3dc9 commit 096a3c5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
62 changes: 53 additions & 9 deletions src/unicorn_emulator/unicorn_x64_emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,38 @@ namespace unicorn
uc_close(this->uc_);
}

void start(const uint64_t start, const uint64_t end, std::chrono::microseconds timeout,
void start(uint64_t start, const uint64_t end, std::chrono::microseconds timeout,
const size_t count) override
{
if (timeout.count() < 0)
while (true)
{
timeout = {};
}
if (timeout.count() < 0)
{
timeout = {};
}

uce(uc_emu_start(*this, start, end, static_cast<uint64_t>(timeout.count()), count));
this->retry_after_violation_ = false;
const auto res = uc_emu_start(*this, start, end, static_cast<uint64_t>(timeout.count()), count);
if (res == UC_ERR_OK)
{
return;
}

const auto is_violation = res == UC_ERR_READ_UNMAPPED || //
res == UC_ERR_WRITE_UNMAPPED || //
res == UC_ERR_FETCH_UNMAPPED || //
res == UC_ERR_READ_PROT || //
res == UC_ERR_WRITE_PROT || //
res == UC_ERR_FETCH_PROT;

if (is_violation && this->retry_after_violation_)
{
start = this->read_instruction_pointer();
continue;
}

uce(res);
}
}

void stop() override
Expand Down Expand Up @@ -324,7 +347,8 @@ namespace unicorn
auto container = std::make_unique<hook_container>();

uce(uc_hook_add(*this, hook.make_reference(), UC_HOOK_INTR, wrapper.get_function(),
wrapper.get_user_data(), 0, std::numeric_limits<pointer_type>::max()));
wrapper.get_user_data(), 0, std::numeric_limits<pointer_type>::max())
);

container->add(std::move(wrapper), std::move(hook));

Expand All @@ -337,14 +361,33 @@ namespace unicorn
memory_violation_hook_callback callback) override
{
function_wrapper<bool, uc_engine*, uc_mem_type, uint64_t, int, int64_t> wrapper(
[c = std::move(callback)](uc_engine*, const uc_mem_type type,
const uint64_t address, const int size, const int64_t)
[c = std::move(callback), this](uc_engine*, const uc_mem_type type,
const uint64_t address, const int size, const int64_t)
{
const auto ip = this->read_instruction_pointer();

assert(size >= 0);
const auto operation = map_memory_operation(type);
const auto violation = map_memory_violation_type(type);

return c(address, static_cast<uint64_t>(size), operation, violation) == memory_violation_continuation::resume;
const auto resume = c(address, static_cast<uint64_t>(size), operation, violation) ==
memory_violation_continuation::resume;

const auto has_ip_changed = ip != this->read_instruction_pointer();

if (!resume)
{
return false;
}

this->retry_after_violation_ = resume && has_ip_changed;

if (has_ip_changed)
{
return false;
}

return true;
});

unicorn_hook hook{*this};
Expand Down Expand Up @@ -414,6 +457,7 @@ namespace unicorn

private:
uc_engine* uc_{};
bool retry_after_violation_{false};
std::vector<std::unique_ptr<hook_object>> hooks_{};
};
}
Expand Down
23 changes: 3 additions & 20 deletions src/windows_emulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define GDT_LIMIT 0x1000
#define GDT_ENTRY_SIZE 0x8

bool use_gdb = false;
bool use_gdb = true;

struct breakpoint_key
{
Expand Down Expand Up @@ -756,8 +756,6 @@ namespace
printf("Interrupt: %i\n", interrupt);
});

bool continue_execution = true;

emu->hook_memory_violation([&](const uint64_t address, const size_t size, const memory_operation operation,
const memory_violation_type type)
{
Expand All @@ -774,8 +772,7 @@ namespace
}

dispatch_access_violation(*emu, ki_user_exception_dispatcher, address, operation);
continue_execution = true;
return memory_violation_continuation::stop;
return memory_violation_continuation::resume;
});

/*
Expand Down Expand Up @@ -825,21 +822,7 @@ namespace
}
else
{
while (continue_execution)
{
continue_execution = false;
try
{
emu->start_from_ip();
}
catch (...)
{
if (!continue_execution)
{
throw;
}
}
}
emu->start_from_ip();
}
}
catch (...)
Expand Down

0 comments on commit 096a3c5

Please sign in to comment.