Skip to content
SGNightPublic

About

In-memory patcher for Intel Houdini 17 -- the ART native bridge for ARM64 apps on Android 17 x86_64

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

libnb — in-memory patcher for Intel Houdini 17

libnb is the native bridge ART loads on an x86_64 Android 17 device that runs ARM64 apps through Intel Houdini. It loads the real translator (libhoudini.so), patches 7 bytes of it in memory, and forwards every ART callback to it.

ART ──dlopen("libnb.so")──▶ libnb ──dlopen("libhoudini.so")──▶ libhoudini.so
                              │                                 (patched here)
                              └── forwards NativeBridgeItf ─────────┘

The Intel blob ships unmodified. The patch lives in source, next to a table that says what each byte does, and the shim checks the byte before it writes it — so a blob that is not the one the table was written for is detected and left alone, with a line in logcat saying so.

The translator you need

The blob this patches is the stock Intel Houdini that ships in the Android 17 GoogleBook image:

https://github.com/SGNight/vendor_intel_proprietary_houdini

That repo ships the translator (libhoudini.so), its init companion (libtcb.so), the binfmt runner (houdini64) and the ARM64 guest libraries, as a Soong tree you drop into your own source. Use it as-is — the blob it ships is byte-identical to the one this patch table was written for:

build Intel Houdini 17.0.0_z.39885.g
build id 2747b95bd407148e139ca403d595f9f954561741
sha256 a252c506ea7435d99839f8ae741908f8c19e2d67a445919146128eabe7fee9cb
size 7 391 632 B, x86-64

Every byte in a Houdini image moves between releases, so a table from another build must never be reused here. tools/check-patches.py fails if the blob and the table disagree.

The patches

There are two groups. The first three are what makes the games run at all on an Intel host; the other 4 remove the translator's Intel-only CPU gate, so the same blob also runs on an AMD APU.

All 7 are single bytes in .text, at the vaddr == file offset of the first LOAD segment (the engine does not rely on that: it resolves vaddrs through the module's program headers).

# vaddr byte what it does
1 0x291357 01 → 10 pool-B workspace count, reset path: 1 → 16
2 0x2917cc 01 → 10 pool-B workspace count, gate function: 1 → 16
3 0x445d11 02 → ff shared-ring translation path made unreachable
4–7 0x291d91 … 0x4947af 76/74 → eb the four "GenuineIntel" gates: each check's guard made unconditional

1 + 2 — the pool-B workspace deadlock. Houdini translates guest code in a workspace taken from a free-list bitmap. Pool A holds the translator's worker threads; pool B holds the workspaces that guest threads execute translated code in. The stock blob ships pool B = 1, hard-coded as the immediate of two movl $1, pool_b_count(%rip) instructions. A guest thread returns its workspace only when the thread exits, not when the translated block finishes — so with one workspace, the second guest thread that needs one spins forever in the raw sched_yield loop at 0x291c20: no timeout, no abort, no fallback. The app freezes with every core at 100 % and only a new process clears it. Changing the immediate from 1 to 16 gives sixteen guest threads a workspace each (the free bitmap is a single u32, so 16 is well inside the limit of 31).

3 — the shared-ring translation path. For every translation request Houdini decides between translating inline and handing the request to a shared "contexts ring", gated by one global counter:

0x445d0b  83 3d 5a e9 22 00 00 02   cmp dword [0x67466c], 2
0x445d12  0f 83 c6 09 00 00         jae 0x4466de      ; -> ring consumer

The counter starts at 1, and six direct stores plus one indirect writer raise it to 2. At 2 the jae is taken and the request goes to the ring consumer. But the ring only exists after the pool initialiser at 0x291550 has run, and that initialiser is only reached on the eighth translation request of a context. If the counter reaches 2 first, the guest thread takes the ring path, there is no ring, and the process spins in the ring-empty wait at 0x291b42 forever: the game never draws a first frame. The immediate of the cmp is a sign-extended byte, so changing it from 02 to ff compares against −1: jae is then taken only when the counter is >= 0xffffffff, i.e. never. One byte, and it also covers the indirect writer that a per-site patch cannot reach.

4–7 — the "GenuineIntel" gate. The translator compares the host CPU's vendor string against "GenuineIntel" in four places, and the binary contains no "AuthenticAMD" at all — no constant, no string. There is no AMD branch and no unknown-vendor branch.

cpuid                     ; leaf 0: EBX/ECX/EDX = the vendor string
cmp  ebx, 0x756E6547      ; "Genu"
jne  <mismatch>           ; <- one branch per register
cmp  edx, 0x49656E69      ; "ineI"
jne  <mismatch>
cmp  ecx, 0x6C65746E      ; "ntel"
jne  <mismatch>

Three of the four sites are wrapped in a counter whose threshold pair lives in .data pre-initialised to 10, so the check runs on the first call, not eventually. On a non-Intel CPU:

  • the CPU-feature library returns without publishing its feature bitmap, and the two tier dispatchers above it spin forever — the translator hangs at init and never translates a block;
  • the translation dispatcher reports an error and enters a trap that ends in a jump to itself;
  • the two pool initialisers report an error and return early, leaving the pools uninitialised.

Each of the four checks sits behind a two-byte conditional branch that skips over it, and the guard's target is the instruction the check falls through to. The patch writes 0xeb (jmp rel8) over the guard's opcode: the displacement is untouched, so the target does not move, and the whole check is skipped. One byte per site — 4 bytes for the whole gate.

On an Intel host these 4 bytes are inert: the comparison already matches there, so the check was never going to fail, and nothing outside the check reads what it computes — the three registers cpuid filled are read only inside the check, and the counter/threshold pair each guard keeps in .data is touched only inside its own function. They exist for the AMD APU case only, and they change nothing about an Intel run.

The full analysis — the census, the four sites, and the offline verification against the pristine donor — is in docs/libhoudini/10-cpu-vendor-gate.md in the port's tree.

Adding it to your tree

Copy this directory into your AOSP tree — device/<vendor>/<device>/libnb is the usual place — and copy the translator repo next to it as houdini/.

In your device.mk, one line:

include $(LOCAL_PATH)/libnb/libnb.mk

In your BoardConfig.mk, two lines — both are board variables, so they cannot be set from the product makefile:

TARGET_NATIVE_BRIDGE_ABI := arm64-v8a
BOARD_VENDOR_SEPOLICY_DIRS += $(LOCAL_PATH)/libnb/sepolicy

TARGET_NATIVE_BRIDGE_ABI is what puts arm64 into the framework's abilist at all: without it no arm64 app is ever offered, and nothing below matters.

What the bridge needs, and what breaks without it

who reads it what breaks without it
ro.dalvik.vm.native.bridge=libnb.so ART, in the zygote (AndroidRuntime.cpp:1147) ART dlopen()s that exact name. Anything else and there is no bridge at all.
ro.dalvik.vm.isa.arm64=x86_64 the framework (Utils.getTranslatedIsa) The framework does not know an arm64 app can run here, so it is never offered.
ro.dalvik.vm.isa.arm=x86 the same The same, for 32-bit arm.
ro.enable.native.bridge.exec=1 nobody — not ART, not the translator Nothing. It is the flag AOSP's own riscv bridge gates its binfmt rc on. Set for parity; the translator's only properties are ro.hardware.egl and ro.product.device.
etc/init/init.libnb.rc init The standalone arm64-executable path is never registered.
sepolicy/libnb_binfmt.te SELinux On an enforcing build init cannot mount binfmt_misc nor write the registration strings, so the exec path is silently not registered.

Why the init script has to follow the bridge name. There are two paths into the translator and they do not share code:

  • The JNI path — ART dlopen()s the bridge, the bridge loads the translator and patches it. This is what an arm64 .so inside an app uses.
  • The exec path — binfmt_misc hands a standalone arm64 executable to /system/bin/houdini64, which links the translator directly and never calls a libnb callback.

The exec path is registered by an init rc, and the translator's own rc registers the binfmt entries on ro.dalvik.vm.native.bridge=libhoudini.so — so the moment the bridge is renamed to libnb.so, that rc registers nothing. The exec path stops working and the JNI path carries on, which is why the failure looks like "arm64 binaries are not translated" and not like a config error. That is why libnb ships its own rc.

Why the policy rule is the one that bites. init has to mount binfmt_misc and write the two registration strings, and under an enforcing policy it does not have those permissions by default. Measured on the port, permissive mode, so the denials were logged and then ignored:

avc:  denied  { mounton } for  comm="init" path="/proc/sys/fs/binfmt_misc"
      scontext=u:r:init:s0 tcontext=u:object_r:proc:s0 tclass=dir
avc:  denied  { write } for  comm="init" name="register" dev="binfmt_misc"
      scontext=u:r:init:s0 tcontext=u:object_r:binfmt_miscfs:s0 tclass=file

A permissive build carries on; an enforcing one does not. AOSP's own x86 targets carry the same two lines next to their binfmt rules.

The translator's own files

Its packages come from the translator repo:

PRODUCT_PACKAGES += \
    libhoudini \
    libtcb \
    houdini64 \
    houdini_guest_libc

include $(LOCAL_PATH)/houdini/guest-libs.mk

Its non-ELF assets go in as files, not packages — the faked guest cpuinfo the translator reads, and the two binfmt registration strings libnb's rc feeds to /proc/sys/fs/binfmt_misc/register:

PRODUCT_COPY_FILES += \
    $(LOCAL_PATH)/houdini/etc/houdini/cpuinfo.arm64.txt:$(TARGET_COPY_OUT_SYSTEM)/etc/houdini/cpuinfo.arm64.txt \
    $(LOCAL_PATH)/houdini/etc/houdini/binfmt_misc/arm64_exe:$(TARGET_COPY_OUT_SYSTEM)/etc/houdini/binfmt_misc/arm64_exe \
    $(LOCAL_PATH)/houdini/etc/houdini/binfmt_misc/arm64_dyn:$(TARGET_COPY_OUT_SYSTEM)/etc/houdini/binfmt_misc/arm64_dyn

Do not also copy the translator's init.houdini.rc. libnb's rc replaces it, and two rcs writing the same registration fail the second time with "File exists" on every boot.

The binfmt runner

/system/bin/houdini64 is the binfmt_misc interpreter for arm64 binaries and it links the translator directly, so it never calls a libnb callback — a shim that only patched on the first callback would leave that path unpatched. Point it at libnb instead; the rewrite is one string in .dynstr and the replacement is the same length, so no offset in the file moves:

python3 tools/patch-houdini64-bridge.py \
    --in  houdini/bin/houdini64 \
    --out houdini/bin/houdini64

Now /system/bin/houdini64 → libnb.so → libhoudini.so, and libnb's constructor patches the translator before houdini64 runs a single instruction of it.

Building

In the tree:

m libnb
# -> out/target/product/<product>/system/lib64/libnb.so

Standalone, with an NDK:

export ANDROID_NDK_HOME=/path/to/ndk
cmake -B build -DCOMPILE_ARCH=x86_64 \
      -DHOUDINI_LIB=/path/to/libhoudini.so
cmake --build build
# -> build/libnb.so

HOUDINI_LIB is the translator you are shipping, and it is not optional in practice. Without it the artifact has no DT_NEEDED on libhoudini.so, so the exec path (houdini64) would load and run the translator unpatched — the artifact looks fine and every ARM64 game still freezes. The build warns loudly when it is missing.

Two things the standalone build gets right that are easy to get wrong, and that the build now guards:

  • It is an Android build, not a host build. The NDK toolchain file has to be selected before project() runs; set afterwards, CMake configures a plain Linux build and links a .so that cannot run on the device — and says nothing about it. The build now fails loudly if the NDK is missing.
  • It exports one symbol. -fvisibility=hidden alone is not enough for a statically linked libc++: the runtime's own symbols still land in the dynamic symbol table, and in the zygote's namespace they would interpose on every app's libc++ symbols. -Wl,--exclude-libs,ALL is what actually keeps the exported set to NativeBridgeItf, and the standalone build uses it.

The regression tests build on the host, with no NDK and no device:

cmake -S test -B /tmp/libnb-test
cmake --build /tmp/libnb-test
ctest --test-dir /tmp/libnb-test --output-on-failure

Checking and verifying

Before you ship an image, check that the blob you are shipping still matches the patch table:

python3 tools/check-patches.py <path to libhoudini.so>

On a device, prove the patch is in memory and not in the file — the two have to disagree: the file on the device is the stock blob, and the bytes the zygote has mapped are the patched ones. A statically patched blob cannot show that difference.

tools/verify-on-device.sh
    ok       @0x291357  file=01 (stock blob)  memory=10 (patched)
    ok       @0x2917cc  file=01 (stock blob)  memory=10 (patched)
    ok       @0x445d11  file=02 (stock blob)  memory=ff (patched)
    ok       @0x291d91  file=76 (stock blob)  memory=eb (patched)
    ...
    ok       @0x4947af  file=74 (stock blob)  memory=eb (patched)

And the shim's own log:

adb logcat -d -s libnb
libnb: real bridge /system/lib64/libhoudini.so loaded, NativeBridgeItf version 8
libnb: bridge build id 2747b95bd407148e139ca403d595f9f954561741 (Intel Houdini 17.0.0_z.39885.g)
libnb: pool_b_reset   0x01 -> 0x10 at vaddr 0x291357 -- pool-B workspace count 1 -> 16 (the reset path)
libnb: pool_b_init    0x01 -> 0x10 at vaddr 0x2917cc -- pool-B workspace count 1 -> 16 (the gate function)
libnb: ring_path_test 0x02 -> 0xff at vaddr 0x445d11 -- shared-ring translation path made unreachable
libnb: vnd_pool_guard 0x76 -> 0xeb at vaddr 0x291d91 -- GenuineIntel gate (pool init): the check is skipped
libnb: patch summary: 7 applied, 0 already present, 0 failed, 7/7 verified live
libnb: pool_b_count   = 0x0 (expected before initialize: 0x0)

The last line matters: it proves the pool was not already sized by the unpatched code before the patch went in. If it reads 0x1, the translator initialised its pools during dlopen, before the patch landed, and the patched byte never governed the pool size — the failure mode would be the pool-B deadlock again.

Failure behaviour

  • A byte that carries neither the expected nor the replacement value means the module is a different build. Nothing is written, the shim logs wrong build, not patching, and it forwards to the unpatched bridge. The apps then behave exactly as they do with a vanilla blob — slow, and the pool-B deadlock is back — but nothing is corrupted and nothing crashes.
  • The real bridge is loaded with RTLD_LAZY; if it is missing, initialize returns false and ART falls back to "no native bridge".

Options

build flag default what it does
LOG_DEBUG off debug-level logging
FORCE_GUEST_ISA off hand the real bridge the guest isa (arm64) instead of whatever ART passed
USE_NATIVEBRIDGE libhoudini the name of the real bridge
REAL_BRIDGE_PATH /system/lib64/libhoudini.so where to load it from

FORCE_GUEST_ISA is off because the zygote path already hands the bridge the guest isa, and the games run through that path. It matters for the non-zygote path — a bare app_process, where ART passes the host isa (x86_64) and the translator's initialize rejects it.

Layout

libnb/
├── Android.bp                          the module that ships
├── CMakeLists.txt                      standalone NDK build
├── libnb.mk                            the product-side wiring (include from device.mk)
├── include/                            nativebridge.h, patches.h, log.h, nbtools/
├── src/                                main.cpp, patch_engine.cpp, the patch table
├── test/                               host regression tests
├── etc/init/init.libnb.rc              the init rc for the libnb.so bridge name
├── sepolicy/libnb_binfmt.te            the SELinux init needs to register binfmt
└── tools/
    ├── check-patches.py                the blob must match the table
    ├── patch-houdini64-bridge.py       houdini64 must load the bridge via libnb
    └── verify-on-device.sh             file vs memory at the patch sites

Modelled on https://github.com/qwerty12356-wart/test_libnb, which carries patch tables for houdini 11/12/13/14 and for ndk_translation.

About

In-memory patcher for Intel Houdini 17 -- the ART native bridge for ARM64 apps on Android 17 x86_64

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages