Sandbox
@zhkl0228/unidbg

Java framework for Android and iOS native emulation

unidbg emulates Android native libraries and experimental iOS binaries so you can load code, call JNI_OnLoad, trace execution, and inspect memory. It plugs in backends like unicorn, dynarmic, hypervisor, and KVM, and adds hooks, leak tracking, and MCP tools for debugging with AI clients.

5,199 stars1.2k forksJavaUpdated 9d ago
Who it's for

Builders who need to inspect, test, or reverse engineer Android native libraries or iOS native code in an emulator.

What it delivers

You can run native code outside the device, trace what it does, and debug it with AI tools without restarting the process.

What it does

Android and iOS emulation

Loads Android native libraries and experimental iOS binaries, with support for ARM32 and ARM64.

JNI and syscall emulation

Implements the JNI Invocation API, JavaVM, JNIEnv, and syscall instruction handling so native code can run.

Hook support

Includes inline hooks through Dobby, Android import hooks through xHook, and iOS hooks through fishhook, substrate, and whale.

Multiple execution backends

Supports unicorn, dynarmic, Apple M1 hypervisor, and Linux KVM backends for different speed and host options.

Debugger and trace tools

Provides a console debugger, gdb stub, instruction tracing, and memory read/write tracing.

MCP debugging integration

Exposes emulator state and actions through MCP so Cursor and other AI tools can inspect modules, registers, memory, breakpoints, and function calls.

Memory leak detection

Tracks guest allocations and reports leaked blocks with guest backtraces and host Java stack traces.

Worker pool

Offers a reusable worker pool for running emulator instances across threads with idle cleanup and limits.

README

unidbg

Allows you to emulate an Android native library, and an experimental iOS emulation.

This is an educational project to learn more about the ELF/MachO file format and ARM assembly.

Use it at your own risk !

Features

  • Support MCP (Model Context Protocol) for AI-assisted debugging with Cursor and other AI tools.
  • Emulation of the JNI Invocation API so JNI_OnLoad can be called.
  • Support JavaVM, JNIEnv.
  • Emulation of syscalls instruction.
  • Support ARM32 and ARM64.
  • Inline hook, thanks to Dobby.
  • Android import hook, thanks to xHook.
  • iOS fishhook and substrate and whale hook.
  • unicorn backend support simple console debugger, gdb stub, instruction trace, memory read/write trace.
  • Support iOS objc and swift runtime.
  • Support dynarmic fast backend.
  • Support Apple M1 hypervisor, the fastest ARM64 backend.
  • Support Linux KVM backend with Raspberry Pi B4.
  • Memory leak detection for emulated native code with guest backtrace and host stack trace.

MCP Debugger (AI Integration)

unidbg supports Model Context Protocol (MCP) for AI-assisted debugging. When the debugger is active, type mcp in the console to start an MCP server that AI tools (e.g. Cursor) can connect to.

Quick Start

unidbg MCP has two operating modes:

Mode 1: Breakpoint Debug — Attach the debugger and run your code. When a breakpoint is hit, Breaker.debug() pauses the emulator — type mcp in the console to start MCP server and let AI assist with analysis. All debugging tools are available (registers, memory, disassembly, stepping, tracing, etc). After resuming, if another breakpoint is hit the debugger pauses again. Once execution completes without hitting a breakpoint, the process exits and MCP shuts down.

Debugger debugger = emulator.attach();
debugger.addBreakPoint(address);
// run your emulation logic — debugger pauses when breakpoint is hit

Mode 2: Custom Tools (Repeatable) — Use McpToolkit to register custom tools and let AI re-run target functions with different parameters. The native library is loaded once; after each execution the process stays alive and MCP remains active for the next run.

McpToolkit toolkit = new McpToolkit();
toolkit.addTool(new McpTool() {
    @Override public String name() { return "encrypt"; }
    @Override public String description() { return "Run encryption"; }
    @Override public String[] paramNames() { return new String[]{"input"}; }
    @Override public void execute(String[] params) {
        String input = params.length > 0 ? params[0] : "default";
        // call encryption with input
    }
});
toolkit.run(emulator.attach());

When the debugger breaks, type mcp (or mcp 9239 to specify port) in the console. Then add to Cursor MCP settings:

{
  "mcpServers": {
    "unidbg-mcp-server": {
      "url": "http://localhost:9239/sse"
    }
  }
}

Available MCP Tools

Status & Info

ToolDescription
check_connectionEmulator status: Family, architecture, backend capabilities, isRunning, loaded modules
list_modules / get_module_infoList loaded modules, get detail including exported symbol count and dependencies
list_exportsList exported/dynamic symbols of a module with optional filter and C++ demangling
find_symbolFind symbol by name or find nearest symbol at address
get_threadsList all threads/tasks in the emulator

Registers & Disassembly

ToolDescription
get_registers / get_register / set_registerRead/write CPU registers
disassembleDisassemble instructions at address (branch targets auto-annotated with symbol names)
assembleAssemble instruction text to machine code
get_callstackGet current call stack (backtrace)

Memory

ToolDescription
read_memory / write_memoryRead/write raw memory bytes
read_string / read_std_stringRead C string or C++ std::string (with SSO detection)
read_pointerRead pointer chain with symbol resolution
read_typedRead memory as typed values (int8–int64, float, double, pointer)
search_memorySearch memory for byte patterns with scope/permission filters
list_memory_mapList all memory mappings with permissions
allocate_memory / free_memory / list_allocationsAllocate (malloc/mmap) with optional initial data, free, and track memory blocks
patchWrite assembled instructions to memory

Breakpoints & Execution

ToolDescription
add_breakpoint / add_breakpoint_by_symbol / add_breakpoint_by_offsetAdd breakpoints by address, symbol, or module+offset
remove_breakpoint / list_breakpointsRemove or list breakpoints (with disassembly)
continue_executionResume execution. Use poll_events to wait for breakpoint_hit or execution_completed
step_over / step_into / step_outStep over, into (N instructions), or out of function
next_blockBreak at next basic block (Unicorn only)
step_until_mnemonicBreak at next instruction matching mnemonic, e.g. bl, ret (Unicorn only)
poll_eventsPoll for breakpoint_hit, execution_completed, trace events

Tracing

ToolDescription
trace_codeTrace instructions with register read/write values (regs_read, prev_write)
trace_read / trace_writeTrace memory reads/writes in address range

Function Calls

ToolDescription
call_functionCall native function by address with typed arguments (hex, string, bytes, null). Returns value with symbol resolution and memory preview
call_symbolCall exported function by module + symbol name, e.g. libc.so + malloc

iOS Only (available when Family=iOS)

ToolDescription
inspect_objc_msgInspect objc_msgSend call: show receiver class name and selector, e.g. -[NSString length]
get_objc_class_nameGet ObjC class name of an object at a given address (pure memory parsing, no state change)
dump_objc_classDump ObjC class definition (properties, methods, protocols, ivars)
dump_gpb_protobufDump GPB protobuf message schema as .proto format (64-bit only)

Custom MCP Tools

Use McpToolkit to register custom tools, each implementing the McpTool interface. This replaces manual if-else dispatch with clean, self-contained tool classes. By this point the native library is fully loaded (JNI_OnLoad / entry point already executed), so the code inside each tool's execute() is the target function logic to analyze. AI can set breakpoints and traces before triggering a custom tool, then inspect execution results across different inputs without restarting the process.

Android Example — See Utilities64.java for an Android JNI example with custom MCP tools:

DalvikModule dm = vm.loadLibrary(new File("libtmessages.29.so"), true);
dm.callJNI_OnLoad(emulator);
cUtilities = vm.resolveClass("org/telegram/messenger/Utilities");

McpToolkit toolkit = new McpToolkit();
toolkit.addTool(new McpTool() {
    @Override public String name() { return "aesCbc"; }
    @Override public String description() { return "Run AES-CBC encryption on input data"; }
    @Override public String[] paramNames() { return new String[]{"input"}; }
    @Override public void execute(String[] params) {
        byte[] input = params.length > 0 ? params[0].getBytes() : new byte[16];
        aesCbcEncryptionByteArray(input);
    }
});
toolkit.addTool(new McpTool() {
    @Override public String name() { return "aesCtr"; }
    @Override public String description() { return "Run AES-CTR decryption on input data"; }
    @Override public String[] paramNames() { return new String[]{"input"}; }
    @Override public void execute(String[] params) {
        byte[] input = params.length > 0 ? params[0].getBytes() : new byte[16];
        aesCtrDecryptionByteArray(input);
    }
});
toolkit.addTool(new McpTool() {
    @Override public String name() { return "pbkdf2"; }
    @Override public String description() { return "Run PBKDF2 key derivation"; }
    @Override public String[] paramNames() { return new String[]{"password", "iterations"}; }
    @Override public void execute(String[] params) {
        String password = params.length > 0 ? params[0] : "123456";
        int iterations = params.length > 1 ? Integer.parseInt(params[1]) : 100000;
        pbkdf2(password.getBytes(), iterations);
    }
});
toolkit.run(emulator.attach());

iOS Example — See IpaLoaderTest.java for an iOS IPA loading example with custom MCP tools:

IpaLoader ipaLoader = new IpaLoader64(ipa, new File("target/rootfs/ipa"));
LoadedIpa loader = ipaLoader.load(this);
emulator = loader.getEmulator();
loader.callEntry();
module = loader.getExecutable();

McpToolkit toolkit = new McpToolkit();
toolkit.addTool(new McpTool() {
    @Override public String name() { return "dumpClass"; }
    @Override public String description() { return "Dump an ObjC class definition by name"; }
    @Override public String[] paramNames() { return new String[]{"className"}; }
    @Override public void execute(String[] params) {
        String className = params.length > 0 ? params[0] : "AppDelegate";
        IClassDumper classDumper = ClassDumper.getInstance(emulator);
        System.out.println("dumpClass(" + className + "):\n" + classDumper.dumpClass(className));
    }
});
toolkit.addTool(new McpTool() {
    @Override public String name() { return "readVersion"; }
    @Override public String description() { return "Read the TelegramCoreVersionString from the executable"; }
    @Override public void execute(String[] params) {
        Symbol sym = module.findSymbolByName("_TelegramCoreVersionString");
        if (sym != null) {
            Pointer pointer = UnidbgPointer.pointer(emulator, sym.getAddress());
            if (pointer != null) {
                System.out.println("_TelegramCoreVersionString=" + pointer.getString(0));
            }
        }
    }
});
toolkit.run(emulator.attach());

Once the MCP server is started, AI can call these tools via MCP to run emulations with custom parameters, set breakpoints, trace execution, and inspect results — all without restarting the process.

Low-level API: You can also use Debugger.addMcpTool() + Debugger.run(DebugRunnable) directly for full control. McpToolkit is a higher-level wrapper that eliminates if-else dispatch.

Memory Leak Detection

Track guest-side memory allocations (mmap/munmap/brk) to detect leaks in emulated native code. Use try-with-resources — tracking starts on creation, and the leak report is printed automatically on close.

try (MemoryTracker tracker = emulator.traceMemoryLeaks()) {
    module.callFunction(emulator, "targetFunction", arg1, arg2);
}

Each leaked block includes guest ARM backtrace (module+offset+symbol) and host Java stack trace. Sample output:

=== Memory Leak Report ===
Tracking duration: 42ms
Total allocations: 5
Total deallocations: 3
Leaked blocks: 2
Total leaked size: 32768 bytes (32.0 KB)

--- Leak #1 ---
Address: 0x40001000, Size: 16384 (16.0 KB), Perms: rw-
Guest Backtrace:
  #0 0x40123456 libexample.so+0x3456 (malloc+0x12)
  #1 0x40124000 libexample.so+0x4000 (doSomething+0x48)
Host Stack Trace:
  com.github.unidbg.linux.AndroidElfLoader.mmap2(AndroidElfLoader.java:785)
  ...

You can also access the report programmatically before close:

try (MemoryTracker tracker = emulator.traceMemoryLeaks()) {
    module.callFunction(emulator, "targetFunction", arg1, arg2);
    List<AllocationRecord> leaks = tracker.getLeaks();
    assert leaks.isEmpty() : "Memory leak detected!";
}

Worker Pool

A thread-safe object pool for reusing emulator instances across multiple threads, avoiding the overhead of repeated initialization.

  • Lazy initialization — Workers are created on-demand only when the pool is empty, not upfront.
  • Max limit — Total alive workers (borrowed + idle) never exceeds the configured maximum.
  • Idle cleanup — Workers idle for longer than the timeout (default 10 minutes) are automatically destroyed by the management thread.
  • Min idle — A minimum number of workers (default 1) is always kept alive, even when idle.

1. Implement a Worker

public class MyWorker implements Worker {
    private final AndroidEmulator emulator;

    public MyWorker() {
        emulator = AndroidEmulatorBuilder.for64Bit().build();
        // load .so, call JNI_OnLoad, etc.
    }

    @Override
    public void destroy() {
        emulator.close();
    }

    public byte[] doWork(byte[] input) {
        // call native methods and return the result
    }
}

2. Create Pool, Borrow, and Close

// Create a worker pool (max = CPU cores, lazy-initialized)
WorkerPool pool = WorkerPoolFactory.create(MyWorker::new);
// Or specify max workers explicitly
// WorkerPool pool = WorkerPoolFactory.create(MyWorker::new, 4);

// Optional: customize idle timeout (default 10 minutes, minimum 1 minute)
pool.setIdleTimeout(30); // idle workers destroyed after 30 minutes
// Optional: customize minimum kept-alive workers (default 1, minimum 1)
pool.setMinIdle(2); // always keep at least 2 workers alive
// Optional: pre-create workers eagerly (default 0, fully lazy)
pool.setInitialSize(4); // eagerly create 4 workers on startup

// Concurrent invocation from multiple threads
ExecutorService executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 100; i++) {
    executor.submit(() -> {
        try (WorkerLoan<MyWorker> loan = pool.borrow(1, TimeUnit.MINUTES)) {
            if (loan != null) {
                byte[] result = loan.get().doWork(input);
            }
        } // worker is automatically returned to the pool
    });
}

executor.shutdown();
executor.awaitTermination(10, TimeUnit.MINUTES);
pool.close(); // destroy all workers and release resources

See TTEncryptWorker.java for a complete example.

Examples

Simple tests under src/test directory:





More tests:

License

Thanks

Stargazers over time

Stargazers over time

Files in the repo

Repository payload15 top-level entries
  • .github
  • .mvn
  • assets
  • backend
  • unidbg-android
  • unidbg-api
  • unidbg-ios
  • .gitignore
  • LICENSE
  • mvnw
  • mvnw.cmd
  • pom.xml
  • README.md
  • test.cmd
  • test.sh

Discussion (0)

Ask about usage, or say what you built with it

Sign in to join the discussion.

No comments yet. Be the first to say what this is good for.

More frameworks & sdks

HKUDS/nanobotFrameworks & SDKs

Ultra-lightweight, open-source, self-hosted personal AI agent framework in Python with WebUI, tools, memory, MCP, multi-agent workflows, automation, and chat apps

48k
microsoft/
SkillOpt
microsoft/SkillOptFrameworks & SDKs

SkillOpt is a text-space optimizer that trains reusable natural-language skills for frozen LLM agents through trajectory-driven edits, validation-gated updates, and deployable best_skill.md artifacts.

17k
omnigent-ai/omnigentFrameworks & SDKs

Omnigent is an open-source AI agent framework and meta-harness: orchestrate Claude Code, Codex, Cursor, Pi, and custom agents — swap harnesses without rewriting, enforce policies and sandboxing, and collaborate in real time from any device.

9.8k
kyegomez/
OpenMythos
kyegomez/OpenMythosFrameworks & SDKs

A theoretical reconstruction of the Claude Mythos architecture, built from first principles using the available research literature.

15k
D4Vinci/ScraplingFrameworks & SDKs

🕷️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!

80k