0

I'm gonna write a c program that loads a native android library (.so) this is my code:
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>

typedef void (*target_func)(JNIEnv* env, jobject obj, int x);

int main(int argc, char ** argv) {
    char *lib = "/path/to/lib.so"


    void *handle = dlopen(lib, RTLD_LAZY);

    if (NULL == handle) {
        printf("load library error\n");
        return 1;
    }

    void *offset_func = dlsym(handle, "Java_com_example_test_MainActivity_myFunc");

    if (NULL == offset_func) {
        printf("getprocaddress error\n");
        return 1;
    }

    target_func target = (target_func)((unsigned char *)offset_func);


    target(nullptr, nullptr, 10);  // Here i need to pass JNIEnv pointer

    return 0;
}

The only thing that i need is to pass the JNIEnv (the first parameter), Is there any way ??
My program runs on android emulator and because of that i tried to resolve JNIEnv from libart.so but i wasn't success to disassemble libart.so in IDA Pro.

I know when we launch an apk in our device, the ART (or Dalvik) creates JNIEnv for the app but i don't know how can i emulate and create JNIEnv similar to ART.

op123d
  • 3
  • 1
  • Dunno whether Android is special in this regard, but the usual way to create a Java VM programmatically (and get a `JNIEnv *` with which to access it) is via the `JNI_CreateJavaVM()` function, which is, of course, part of JNI. – John Bollinger Dec 18 '21 at 14:29
  • @JohnBollinger Thanks for reply! `JNI_CreateJavaVM` actually i think there is no implementation for this function in android. When i use this function i resolve a compiler exception. – op123d Dec 18 '21 at 14:38
  • I can't speak to that without more details, but you might need development components that are not presently installed, or you might need compile or link options that you are not providing, or you might just not be including the JNI header. Or Android might just be different. – John Bollinger Dec 18 '21 at 16:18

0 Answers0