From 6b172f5e787a4e468592f6665d09a2d8bdd9e3e8 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 5 Jan 2025 17:07:12 +0100 Subject: [PATCH] Add macOS support --- .github/workflows/build.yml | 34 +++++++++++++++++++++ src/common/platform/compiler.hpp | 12 ++++++-- src/common/utils/io.cpp | 4 +-- src/emulator/memory_manager.cpp | 2 +- src/fuzzing-engine/fuzzer.cpp | 4 ++- src/windows-emulator/kusd_mmio.cpp | 2 +- src/windows-emulator/std_include.hpp | 2 +- src/windows-emulator/syscall_dispatcher.cpp | 4 +-- src/windows-emulator/syscalls.cpp | 11 ++----- src/windows-emulator/windows_emulator.cpp | 8 ++--- 10 files changed, 61 insertions(+), 22 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 52fd0ce1..8715720b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -132,3 +132,37 @@ jobs: name: Linux Clang ${{matrix.configuration}} Artifacts path: | build/${{matrix.preset}}/artifacts/* + + build-mac: + name: Build macOS + runs-on: macos-latest + strategy: + fail-fast: false + matrix: + configuration: + - Debug + - Release + include: + - configuration: Debug + preset: debug + - configuration: Release + preset: release + steps: + - name: Checkout Source + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install Dependencies + run: | + brew install ninja + + - name: CMake Build + run: cmake --workflow --preset=${{matrix.preset}} + + - name: Upload Artifacts + uses: actions/upload-artifact@v4 + with: + name: macOS ${{matrix.configuration}} Artifacts + path: | + build/${{matrix.preset}}/artifacts/* diff --git a/src/common/platform/compiler.hpp b/src/common/platform/compiler.hpp index 21220486..51e9e46b 100644 --- a/src/common/platform/compiler.hpp +++ b/src/common/platform/compiler.hpp @@ -30,12 +30,20 @@ #define NO_INLINE __attribute__((noinline)) #define DECLSPEC_ALIGN(n) alignas(n) - #define _fseeki64 fseeko64 - #define _ftelli64 ftello64 #define fopen_s fopen #define RESTRICTED_POINTER __restrict // TODO: warning stdcall problem #define WINAPI +#ifdef OS_MAC + #define _fseeki64 fseeko + #define _ftelli64 ftello + #define _stat64 stat +#else + #define _fseeki64 fseeko64 + #define _ftelli64 ftello64 + #define _stat64 stat64 +#endif + #endif \ No newline at end of file diff --git a/src/common/utils/io.cpp b/src/common/utils/io.cpp index fb151bb0..e7258219 100644 --- a/src/common/utils/io.cpp +++ b/src/common/utils/io.cpp @@ -28,12 +28,12 @@ namespace utils::io io::create_directory(file.parent_path()); } - std::basic_ofstream stream( + std::ofstream stream( file, std::ios::binary | std::ofstream::out | (append ? std::ofstream::app : std::ofstream::out)); if (stream.is_open()) { - stream.write(data.data(), static_cast(data.size())); + stream.write(reinterpret_cast(data.data()), static_cast(data.size())); stream.close(); return true; } diff --git a/src/emulator/memory_manager.cpp b/src/emulator/memory_manager.cpp index 1f076c33..41064f4c 100644 --- a/src/emulator/memory_manager.cpp +++ b/src/emulator/memory_manager.cpp @@ -243,7 +243,7 @@ bool memory_manager::allocate_memory(const uint64_t address, const size_t size, return false; } - const auto entry = this->reserved_regions_.try_emplace(address, size).first; + const auto entry = this->reserved_regions_.try_emplace(address, reserved_region{.length = size,}).first; if (!reserve_only) { diff --git a/src/fuzzing-engine/fuzzer.cpp b/src/fuzzing-engine/fuzzer.cpp index e08e39d5..294a5cf0 100644 --- a/src/fuzzing-engine/fuzzer.cpp +++ b/src/fuzzing-engine/fuzzer.cpp @@ -1,4 +1,6 @@ #include "fuzzer.hpp" +#include + #include "input_generator.hpp" namespace fuzzer @@ -123,7 +125,7 @@ namespace fuzzer const auto executions = context.executions.exchange(0); const auto highest_scorer = context.generator.get_highest_scorer(); const auto avg_score = context.generator.get_average_score(); - printf("Executions/s: %zd - Score: %zX - Avg: %.3f\n", executions, highest_scorer.score, avg_score); + printf("Executions/s: %" PRIu64 " - Score: %" PRIx64 " - Avg: %.3f\n", executions, highest_scorer.score, avg_score); } } } diff --git a/src/windows-emulator/kusd_mmio.cpp b/src/windows-emulator/kusd_mmio.cpp index fce57201..80b2033e 100644 --- a/src/windows-emulator/kusd_mmio.cpp +++ b/src/windows-emulator/kusd_mmio.cpp @@ -154,7 +154,7 @@ uint64_t kusd_mmio::read(const uint64_t addr, const size_t size) } const auto end = addr + size; - const auto valid_end = std::min(end, KUSD_SIZE); + const auto valid_end = std::min(end, static_cast(KUSD_SIZE)); const auto real_size = valid_end - addr; if (real_size > sizeof(result)) diff --git a/src/windows-emulator/std_include.hpp b/src/windows-emulator/std_include.hpp index bc95a00c..a82acdab 100644 --- a/src/windows-emulator/std_include.hpp +++ b/src/windows-emulator/std_include.hpp @@ -56,7 +56,7 @@ #include #include -#include +#include #include "platform/platform.hpp" diff --git a/src/windows-emulator/syscall_dispatcher.cpp b/src/windows-emulator/syscall_dispatcher.cpp index bd13a2df..aa601916 100644 --- a/src/windows-emulator/syscall_dispatcher.cpp +++ b/src/windows-emulator/syscall_dispatcher.cpp @@ -126,13 +126,13 @@ void syscall_dispatcher::dispatch(windows_emulator& win_emu) } catch (std::exception& e) { - printf("Syscall threw an exception: %X (0x%zX) - %s\n", syscall_id, address, e.what()); + printf("Syscall threw an exception: %X (0x%" PRIx64 ") - %s\n", syscall_id, address, e.what()); emu.reg(x64_register::rax, STATUS_UNSUCCESSFUL); emu.stop(); } catch (...) { - printf("Syscall threw an unknown exception: %X (0x%zX)\n", syscall_id, address); + printf("Syscall threw an unknown exception: %X (0x%" PRIx64 ")\n", syscall_id, address); emu.reg(x64_register::rax, STATUS_UNSUCCESSFUL); emu.stop(); } diff --git a/src/windows-emulator/syscalls.cpp b/src/windows-emulator/syscalls.cpp index b15f5d5e..de41f0b7 100644 --- a/src/windows-emulator/syscalls.cpp +++ b/src/windows-emulator/syscalls.cpp @@ -834,7 +834,7 @@ namespace const auto mod = c.proc.mod_manager.find_by_address(base_address); if (!mod) { - printf("Bad address for memory image request: 0x%zX\n", base_address); + printf("Bad address for memory image request: 0x%" PRIx64 "\n", base_address); return STATUS_INVALID_ADDRESS; } @@ -1487,7 +1487,7 @@ namespace for (const auto& file : std::filesystem::directory_iterator(dir)) { - files.emplace_back(file.path().filename()); + files.emplace_back(file_entry{.file_path = file.path().filename(),}); } return files; @@ -2899,13 +2899,8 @@ namespace const auto filename = read_unicode_string(c.emu, emulator_object>>{c.emu, attributes.ObjectName}); const auto u8_filename = u16_to_u8(filename); -#ifdef OS_WINDOWS struct _stat64 file_stat{}; if (_stat64(u8_filename.c_str(), &file_stat) != 0) -#else - struct stat64 file_stat{}; - if (stat64(u8_filename.c_str(), &file_stat) != 0) -#endif { return STATUS_OBJECT_NAME_NOT_FOUND; } @@ -3199,7 +3194,7 @@ namespace } else { - printf("Unsupported thread attribute type: %zX\n", type); + printf("Unsupported thread attribute type: %" PRIx64 "\n", type); } }, i); } diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 4ce13ac1..f02ad024 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -250,7 +250,7 @@ namespace const auto total_length = allocator.get_next_address() - context.process_params.value(); - proc_params.Length = static_cast(std::max(sizeof(proc_params), total_length)); + proc_params.Length = static_cast(std::max(static_cast(sizeof(proc_params)), total_length)); proc_params.MaximumLength = proc_params.Length; }); @@ -869,7 +869,7 @@ void windows_emulator::on_instruction_execution(uint64_t address) auto& emu = this->emu(); printf( - "Inst: %16" PRIu64 " - RAX: %16" PRIu64 " - RBX: %16" PRIu64 " - RCX: %16" PRIu64 " - RDX: %16" PRIu64 " - R8: %16" PRIu64 " - R9: %16" PRIu64 " - RDI: %16" PRIu64 " - RSI: %16" PRIu64 " - %s\n", + "Inst: %16" PRIx64 " - RAX: %16" PRIx64 " - RBX: %16" PRIx64 " - RCX: %16" PRIx64 " - RDX: %16" PRIx64 " - R8: %16" PRIx64 " - R9: %16" PRIx64 " - RDI: %16" PRIx64 " - RSI: %16" PRIx64 " - %s\n", address, emu.reg(x64_register::rax), emu.reg(x64_register::rbx), emu.reg(x64_register::rcx), @@ -907,7 +907,7 @@ void windows_emulator::setup_hooks() { const auto ip = this->emu().read_instruction_pointer(); - this->log.print(color::gray, "Invalid instruction at: 0x%" PRIu64 "\n", ip); + this->log.print(color::gray, "Invalid instruction at: 0x%" PRIx64 "\n", ip); return instruction_hook_continuation::skip_instruction; }); @@ -921,7 +921,7 @@ void windows_emulator::setup_hooks() } const auto rip = this->emu().read_instruction_pointer(); - this->log.print(color::gray, "Interrupt: %i 0x%" PRIu64 "\n", interrupt, rip); + this->log.print(color::gray, "Interrupt: %i 0x%" PRIx64 "\n", interrupt, rip); if (this->fuzzing || true) // TODO: Fix {