How zhkl0228/unidbg works
This project is an emulator framework—“unidbg”—designed to emulate native Android and iOS libraries. It contains multiple modules that together provide an emulation API, several backend engines (each using different techniques such as dynamic binary translation and hypervisor-based acceleration), and specialized support for both Android (JNI, syscall emulation, JavaVM/ JNIEnv simulation) and iOS (Mach-O parsing, Objective-C/ Swift runtime emulation and hooking). The project is built using Maven (notice the pom.xml files) and is split into multiple subprojects (backend components, unidbg-android, unidbg-api, and unidbg-ios), each with its own source tree and purpose.
Main Components
• Core Emulator API (unidbg-api): This forms the heart of the framework. It provides the abstraction for emulation, defines interfaces for emulated memory, registers, contexts, and debugging hooks.
• Backend Engines (located under “backend”):
– Dynarmic: A fast dynamic binary translation backend for ARM (both 32-bit and 64-bit).
– Hypervisor: Uses Apple M1 hypervisor technology for accelerated ARM64 emulation.
– KVM: Provides Linux KVM support (e.g., for Raspberry Pi B4).
– Unicorn2: Implements CPU emulation with support for hooks (instruction trace, memory read/write trace) and a simple debugger/gdb stub.
• Android Emulation (unidbg-android):
– Implements Android-specific features such as JNI emulation, syscall handling, hooks via xHook, and various file I/O abstractions.
• iOS Emulation (unidbg-ios):
– Provides iOS support with Mach-O parsing, support for ObjC/Swift runtime, hooking libraries (e.g., fishhook, substrate, whale) and additional utilities for handling iOS binary formats.
• Supporting Utilities and Tests:
– There are numerous tests and sample applications that exercise these capabilities. Assets (like GIFs) and test scripts are present to demonstrate functionality.
Relationships and Interactions
• The Core Emulator API sits at the center and is used by both the Android and iOS modules.
• The Android and iOS emulators interact with one or more backend engines to perform actual ARM code emulation.
• The backend engines are isolated modules that implement a standard “Backend” interface (as evident from the shared package names and backend factories) and are selectable based on requirements such as performance or target platform.
• Hooking subcomponents (e.g., xHook, fishhook, substrate, whale) are integrated within the Android and iOS modules to intercept and patch native calls.
• The emulator also includes support for debugging features (instruction tracing, gdb stub) in the unicorn backend and others.
Architectural Patterns and Principles
• Modularity – The system is split into multiple well-defined modules, each responsible for different platform emulations and backend engines.
• Abstraction – There is an abstraction layer (the API) that provides a consistent interface regardless of which backend or platform-specific implementation is running.
• Separation of Concerns – Android and iOS specific implementations are kept logically apart while sharing a common emulation core.