mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-25 22:51:03 +00:00
Fixes for emscripten
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
|
||||
add_subdirectory(unicorn-emulator)
|
||||
endif()
|
||||
add_subdirectory(unicorn-emulator)
|
||||
|
||||
if (MOMO_ENABLE_RUST_CODE)
|
||||
add_subdirectory(icicle-emulator)
|
||||
|
||||
88
src/backends/unicorn-emulator/function_wrapper2.hpp
Normal file
88
src/backends/unicorn-emulator/function_wrapper2.hpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
#include <utils/object.hpp>
|
||||
|
||||
uint32_t resolve_indexed_argument_part(uint32_t* args, size_t& index)
|
||||
{
|
||||
return args[index++];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T resolve_indexed_argument_internal(uint32_t* args, size_t& index)
|
||||
{
|
||||
const auto a1 = resolve_indexed_argument_part(args, index);
|
||||
|
||||
if(sizeof(T) <= sizeof(a1)) {
|
||||
return (T)a1;
|
||||
}
|
||||
|
||||
const auto a2 = resolve_indexed_argument_part(args, index);
|
||||
|
||||
const auto arg = (a1 | ((uint64_t)a2 << 32));
|
||||
return (T)arg;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T resolve_indexed_argument(uint32_t* args, size_t& index)
|
||||
{
|
||||
auto arg = resolve_indexed_argument_internal<T>(args, index);
|
||||
return arg;
|
||||
}
|
||||
|
||||
template <typename ReturnType, typename... Args>
|
||||
class function_wrapper2 : public utils::object
|
||||
{
|
||||
public:
|
||||
using user_data_pointer = void*;
|
||||
using c_function_type = ReturnType(Args..., user_data_pointer);
|
||||
using functor_type = std::function<ReturnType(Args...)>;
|
||||
|
||||
function_wrapper2() = default;
|
||||
|
||||
function_wrapper2(functor_type functor)
|
||||
: functor_(std::make_unique<functor_type>(std::move(functor)))
|
||||
{
|
||||
}
|
||||
|
||||
c_function_type* get_c_function() const
|
||||
{
|
||||
return (c_function_type*)(void*)+[](uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4,uint32_t a5,uint32_t a6,uint32_t a7,uint32_t a8,uint32_t a9,uint32_t a10,uint32_t a11,uint32_t a12)-> uint64_t {
|
||||
|
||||
uint32_t real_args[] {
|
||||
a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12
|
||||
};
|
||||
|
||||
const auto lambda = +[](Args... args, user_data_pointer user_data) -> ReturnType {
|
||||
return (*static_cast<functor_type*>(user_data))(std::forward<Args>(args)...);
|
||||
};
|
||||
|
||||
size_t index = 0;
|
||||
std::tuple<Args..., user_data_pointer> func_args{resolve_indexed_argument<std::remove_cv_t<std::remove_reference_t<Args>>>(real_args, index)..., resolve_indexed_argument<user_data_pointer>(real_args, index)};
|
||||
|
||||
(void)index;
|
||||
|
||||
if constexpr(!std::is_void_v<ReturnType>){
|
||||
return (uint64_t)std::apply(lambda, std::move(func_args));
|
||||
}
|
||||
|
||||
std::apply(lambda, std::move(func_args));
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
void* get_function() const
|
||||
{
|
||||
return reinterpret_cast<void*>(this->get_c_function());
|
||||
}
|
||||
|
||||
user_data_pointer get_user_data() const
|
||||
{
|
||||
return this->functor_.get();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<functor_type> functor_{};
|
||||
};
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "unicorn_hook.hpp"
|
||||
|
||||
#include "function_wrapper.hpp"
|
||||
#include "function_wrapper2.hpp"
|
||||
#include <ranges>
|
||||
|
||||
namespace unicorn
|
||||
@@ -385,10 +386,7 @@ namespace unicorn
|
||||
|
||||
emulator_hook* hook_instruction(const int instruction_type, instruction_hook_callback callback) override
|
||||
{
|
||||
function_wrapper<int, uc_engine*> wrapper([c = std::move(callback)](uc_engine*) {
|
||||
return (c() == instruction_hook_continuation::skip_instruction) ? 1 : 0;
|
||||
});
|
||||
|
||||
|
||||
unicorn_hook hook{*this};
|
||||
auto container = std::make_unique<hook_container>();
|
||||
|
||||
@@ -396,18 +394,39 @@ namespace unicorn
|
||||
|
||||
if (inst_type == x64_hookable_instructions::invalid)
|
||||
{
|
||||
function_wrapper<int, uc_engine*> wrapper([c = std::move(callback)](uc_engine*) {
|
||||
return (c() == instruction_hook_continuation::skip_instruction) ? 1 : 0;
|
||||
});
|
||||
|
||||
uce(uc_hook_add(*this, hook.make_reference(), UC_HOOK_INSN_INVALID, wrapper.get_function(),
|
||||
wrapper.get_user_data(), 0, std::numeric_limits<pointer_type>::max()));
|
||||
}
|
||||
else
|
||||
{
|
||||
container->add(std::move(wrapper), std::move(hook));
|
||||
}
|
||||
else if(inst_type == x64_hookable_instructions::syscall){
|
||||
function_wrapper<void, uc_engine*> wrapper([c = std::move(callback)](uc_engine*) {
|
||||
c();
|
||||
});
|
||||
|
||||
const auto uc_instruction = map_hookable_instruction(inst_type);
|
||||
uce(uc_hook_add(*this, hook.make_reference(), UC_HOOK_INSN, wrapper.get_function(),
|
||||
wrapper.get_user_data(), 0, std::numeric_limits<pointer_type>::max(),
|
||||
uc_instruction));
|
||||
}
|
||||
|
||||
container->add(std::move(wrapper), std::move(hook));
|
||||
container->add(std::move(wrapper), std::move(hook));
|
||||
}
|
||||
else
|
||||
{
|
||||
function_wrapper<int, uc_engine*> wrapper([c = std::move(callback)](uc_engine*) {
|
||||
return (c() == instruction_hook_continuation::skip_instruction) ? 1 : 0;
|
||||
});
|
||||
|
||||
const auto uc_instruction = map_hookable_instruction(inst_type);
|
||||
uce(uc_hook_add(*this, hook.make_reference(), UC_HOOK_INSN, wrapper.get_function(),
|
||||
wrapper.get_user_data(), 0, std::numeric_limits<pointer_type>::max(),
|
||||
uc_instruction));
|
||||
|
||||
container->add(std::move(wrapper), std::move(hook));
|
||||
}
|
||||
|
||||
auto* result = container->as_opaque_hook();
|
||||
|
||||
@@ -533,7 +552,7 @@ namespace unicorn
|
||||
c(address); //
|
||||
};
|
||||
|
||||
function_wrapper<void, uc_engine*, uint64_t, uint32_t> wrapper(std::move(exec_wrapper));
|
||||
function_wrapper2<void, uc_engine*, uint64_t, uint32_t> wrapper(std::move(exec_wrapper));
|
||||
|
||||
unicorn_hook hook{*this};
|
||||
|
||||
|
||||
@@ -14,11 +14,9 @@ if(NOT MOMO_ENABLE_CLANG_TIDY)
|
||||
target_precompile_headers(windows-emulator PRIVATE std_include.hpp)
|
||||
endif()
|
||||
|
||||
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
|
||||
target_link_libraries(windows-emulator PRIVATE
|
||||
unicorn-emulator
|
||||
)
|
||||
endif()
|
||||
|
||||
if (MOMO_ENABLE_RUST_CODE)
|
||||
target_link_libraries(windows-emulator PRIVATE
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
|
||||
#include "cpu_context.hpp"
|
||||
|
||||
#ifndef OS_EMSCRIPTEN
|
||||
#include <unicorn_x64_emulator.hpp>
|
||||
#endif
|
||||
|
||||
#if MOMO_ENABLE_RUST_CODE
|
||||
#include <icicle_x64_emulator.hpp>
|
||||
@@ -265,10 +263,6 @@ namespace
|
||||
|
||||
std::unique_ptr<x64_emulator> create_default_x64_emulator()
|
||||
{
|
||||
#ifdef OS_EMSCRIPTEN
|
||||
return icicle::create_x64_emulator();
|
||||
#else
|
||||
|
||||
#if MOMO_ENABLE_RUST_CODE
|
||||
const auto* env = getenv("EMULATOR_ICICLE");
|
||||
if (env && (env == "1"sv || env == "true"sv))
|
||||
@@ -276,8 +270,8 @@ std::unique_ptr<x64_emulator> create_default_x64_emulator()
|
||||
return icicle::create_x64_emulator();
|
||||
}
|
||||
#endif
|
||||
|
||||
return unicorn::create_x64_emulator();
|
||||
#endif
|
||||
}
|
||||
|
||||
windows_emulator::windows_emulator(application_settings app_settings, const emulator_settings& settings,
|
||||
|
||||
Reference in New Issue
Block a user