unidbg

repository·master·Indexed 26 days ago

https://github.com/zhkl0228/unidbg

An emulation framework for Android native libraries (JNI) and experimental iOS environments, designed for educational purposes regarding ELF/MachO formats and ARM assembly. It features multiple backends including dynarmic, Apple Silicon Hypervisor, KVM, and unicorn2, and supports AI-assisted debugging via the Model Context Protocol (MCP).

Tokens
4.7K
Snippets
15
Records
28
Agent score
90%

What's inside unidbg

  1. Overview of Apple Silicon Hypervisor backend

    master

    The Apple Silicon Hypervisor backend is an ARM64 emulator backend that utilizes the macOS Hypervisor.framework. It enables the emulation of Android and iOS ARM64 native libraries on Apple Silicon (M1/M2/M3/M4) at near-native speeds.

    Supported Platform:

    • macOS on ARM64 (Apple Silicon)
    • Output artifact: osx_arm64/libhypervisor.dylib
  2. Cross-compile using Docker (Linux / Windows)

    master
    You can use Docker with docker buildx multi-platform support to compile the backend. Docker will automatically clone the dynarmic source from GitHub and compile it inside the container, so no local pre-build of the static libraries is required.
  3. Sign the Java Binary for Hypervisor.framework

    master

    Because the Hypervisor.framework requires specific entitlements, you must sign your java binary using ldid before execution. Run the following command from the backend/hypervisor/assets directory:

    cd backend/hypervisor/assets
    sudo ./ldid -M -Shypervisor.entitlements "$JAVA_HOME"/bin/java
  4. Build the KVM backend

    master

    Builds are managed via the build.sh script located in backend/kvm/src/main/native/.

    Build artifacts are output to: src/main/resources/natives/linux_arm64/.

    Available build modes:

    • Docker (Default): Works on any host with Docker and ARM64 support.
    • Native: Requires a Linux ARM64 host.
    • Clean Docker: Rebuilds using Docker without using the cache.
    cd backend/kvm/src/main/native
    
    # Build via Docker (default, works on any host)
    ./build.sh docker
    
    # Build natively (requires Linux ARM64 host)
    ./build.sh native
    
    # Clean Docker build (--no-cache)
    ./build.sh --clean docker
  5. Detect memory leaks in emulated native code

    master

    You can track guest-side memory allocations (such as mmap, munmap, and brk) to identify leaks in emulated native code. Using try-with-resources with emulator.traceMemoryLeaks() will automatically print a leak report containing the guest ARM backtrace and the host Java stack trace when the block closes.

    To access the leaks programmatically before the tracker closes, use the getLeaks() method which returns a List<AllocationRecord>.

    try (MemoryTracker tracker = emulator.traceMemoryLeaks()) {
        module.callFunction(emulator, "targetFunction", arg1, arg2);
        List<AllocationRecord> leaks = tracker.getLeaks();
        assert leaks.isEmpty() : "Memory leak detected!";
    }
  6. Build ARM64 and x86_64 static libraries on macOS

    master

    First, clone the dynarmic source recursively:

    git clone --recursive https://github.com/zhkl0228/yuzu-dynarmic ~/git/dynarmic

    Then, follow these steps to build the static libraries:

    Build ARM64 static libraries:

    cd ~/git/dynarmic
    mkdir -p build_arm64 && cd build_arm64
    cmake .. -DCMAKE_BUILD_TYPE=Release \
             -DCMAKE_OSX_DEPLOYMENT_TARGET=13.0 \
             -DDYNARMIC_TESTS=OFF \
             -DDYNARMIC_WARNINGS_AS_ERRORS=OFF \
             -DDYNARMIC_USE_BUNDLED_EXTERNALS=ON
    make -j$(sysctl -n hw.ncpu)

    Build x86_64 static libraries (cross-compile):

    cd ~/git/dynarmic
    mkdir -p build_x86_64 && cd build_x86_64
    cmake .. -DCMAKE_BUILD_TYPE=Release \
             -DCMAKE_OSX_ARCHITECTURES=x86_64 \
             -DCMAKE_OSX_DEPLOYMENT_TARGET=10.15 \
             -DDYNARMIC_TESTS=OFF \
             -DDYNARMIC_WARNINGS_AS_ERRORS=OFF \
             -DDYNARMIC_USE_BUNDLED_EXTERNALS=ON
    make -j$(sysctl -n hw.ncpu)
    git clone --recursive https://github.com/zhkl0228/yuzu-dynarmic ~/git/dynarmic