geokar2006 / ByNameModding

MOVED https://github.com/ByNameModding/BNM-Android
114 stars 34 forks source link

Latest BNM version crash using Zygisk #40

Closed chiteroman closed 1 year ago

chiteroman commented 1 year ago

I'm using BNM with a Zygisk module. It works in this BNM version: https://github.com/geokar2006/ByNameModding/tree/6583b7f238a14627a7306f6983534a88772fab9c

But in latest version when I call AttachIl2Cpp() the game crash. I'm using the latest version of Dobby (https://github.com/jmpews/Dobby/releases/tag/latest)

I hook the dlopen using this:

void *loader = DobbySymbolResolver(nullptr, "__dl__Z9do_dlopenPKciPK17android_dlextinfoPKv");
HOOK(loader, dlopen_, dlopen_backup);

And this is the hook logic:

typedef void *(*dlopen_type)(const char *name, int flags, const void *caller_addr);

dlopen_type dlopen_backup = nullptr;

void *il2cppHandle = nullptr;

void *dlopen_(const char *name, int flags, const void *caller_addr) {

    void *handle = dlopen_backup(name, flags, caller_addr);
    if (handle != nullptr && strutil::contains(name, "libil2cpp.so")) {
        il2cppHandle = handle;
        LOGDBNM("FOUND il2cpp lib: %s", name);
    }
    return handle;
}

strutil -> https://github.com/Shot511/strutil

Then I load that void* into a custom Load function in BNM:

void BNM::Load(void *il2cppHandle) {
    BNM_dlLib = il2cppHandle;
    void *init = DobbySymbolResolver(nullptr, "il2cpp_init");
    Dl_info info;
    BNM_dladdr(init, &info);
    auto l = strlen(info.dli_fname) + 1;
    auto s = new char[l];
    memset((void *) s, 0, l);
    strcpy(s, info.dli_fname);
    BNM_LibAbsolutePath = s;
    BNM_LibAbsoluteAddress = (DWORD) info.dli_fbase;
    HOOK(init, BNM_il2cpp_init, old_BNM_il2cpp_init);
}

In latest version I try to use BNM::External::LoadBNM(void *) but when the program reach AttachIl2Cpp() the game crash. In the old version I told I can use BNM normally and everything works perfectly.

Maybe are there something broken in the code?

This is my custom BNM version with a few code for work with Zygisk: https://anonfiles.com/Ed04o4Y1y5/ByNameModding_7z

chiteroman commented 1 year ago

Solved, I made a mistake in code. Now it works.

    namespace Zygisk {
        [[maybe_unused]] void LoadBNM(void *dl) {
            BNM_Internal::dlLib = dl;
            void *init = DobbySymbolResolver(nullptr, "il2cpp_init");
            Dl_info info;
            BNM_dladdr(init, &info);
            auto l = strlen(info.dli_fname) + 1;
            auto s = new char[l];
            memset((void *) s, 0, l);
            strcpy(s, info.dli_fname);
            BNM_Internal::LibAbsolutePath = s;
            BNM_Internal::LibAbsoluteAddress = (BNM_PTR) info.dli_fbase;
            HOOK(init, BNM_Internal::BNM_il2cpp_init, BNM_Internal::old_BNM_il2cpp_init);
        }
    }