a16-jni-table-bug.md 3.63 KB

Vector v2.0 A16 Crash: JNI Table Size Mismatch

Symptom

JNI DETECTED ERROR IN APPLICATION: negative buffer capacity in NewDirectByteBuffer
from int com.android.internal.os.Zygote.nativeForkSystemServer

Magisk auto-rolls-back Zygisk: zygote crashed too many times, rolling-back

Root Cause

File: zygisk/src/main/cpp/ipc_bridge.cpp, line 557, in IPCBridge::HookBridge()

memcpy(&native_interface_hook_, env->functions, sizeof(JNINativeInterface));

Vector v2.0 was compiled with NDK 26. The JNINativeInterface structure has grown between Android 14 and 16 as ART added new JNI functions. sizeof(JNINativeInterface) at compile time (NDK 26: ~2376 bytes) is smaller than the actual runtime structure on A16.

The memcpy truncates the table. The reserved0 field (offset 0, which ART uses to store the runtime size) IS copied correctly, so SetTableOverride() trusts the table has the full runtime size. But function pointers beyond the compile-time boundary are uninitialized memory (from = {} zero-init). When ART calls these garbage pointers during nativeForkSystemServer, it hits a null pointer or corrupt NewDirectByteBuffer parameters → crash.

Fix

Two files need to change:

1. zygisk/src/main/cpp/include/ipc_bridge.h line 167

Before:

JNINativeInterface native_interface_hook_{};

After:

JNINativeInterface* native_interface_hook_ = nullptr;
size_t table_size_ = 0;

2. zygisk/src/main/cpp/ipc_bridge.cpp lines 555-566

Before:

memcpy(&native_interface_hook_, env->functions, sizeof(JNINativeInterface));
call_boolean_method_v_backup_ = env->functions->CallBooleanMethodV;
native_interface_hook_.CallBooleanMethodV = &CallBooleanMethodV_Hook;
set_table_override_func(&native_interface_hook_);

After:

// Get runtime size from ART's reserved0 field
table_size_ = (size_t)env->functions->reserved0;
if (table_size_ == 0 || table_size_ < sizeof(JNINativeInterface)) {
    table_size_ = sizeof(JNINativeInterface);
}
LOGI("JNI table runtime size: %zu (compile-time: %zu)", table_size_, sizeof(JNINativeInterface));

native_interface_hook_ = (JNINativeInterface*)malloc(table_size_);
if (!native_interface_hook_) {
    LOGE("Failed to allocate JNI hook table.");
    return;
}
memcpy(native_interface_hook_, env->functions, table_size_);

call_boolean_method_v_backup_ = native_interface_hook_->CallBooleanMethodV;
native_interface_hook_->CallBooleanMethodV = &CallBooleanMethodV_Hook;
set_table_override_func(native_interface_hook_);

Build

Requires x86_64 desktop with Android NDK.

git clone --depth 1 --recursive https://github.com/JingMatrix/LSPosed.git
cd LSPosed
# Apply the patch above
export ANDROID_HOME=... ANDROID_NDK_HOME=...
./gradlew :zygisk:assembleRelease

Output: zygisk/build/outputs/apk/release/zygisk-release.apk containing the fixed arm64-v8a.so.

Deploy

# Replace the zygisk .so in the installed module
su -c 'cp fixed_arm64-v8a.so /data/adb/modules/zygisk_vector/zygisk/arm64-v8a.so'
su -c 'chmod 644 /data/adb/modules/zygisk_vector/zygisk/arm64-v8a.so'
# Ensure Zygisk is enabled
su -c 'magisk --sqlite "INSERT OR REPLACE INTO settings (key,value) VALUES (\"zygisk\",1);"'
# Reboot
su -c 'reboot'

After reboot:

su -c 'pidof lspd'                           # Should show PID
su -c 'cat /proc/$(pidof zygote64)/maps|grep zygisk'  # Should show arm64-v8a.so

References

  • ART source: art/runtime/jni/jni_internal.cc — JNI_NewDirectByteBuffer validates capacity >= 0
  • ART JNIEnvExt::SetTableOverride reads reserved0 for table size validation
  • Vector v2.0 zygisk .so: 1,219,416 bytes for arm64-v8a