首页 > 解决方案 > 我可以阻止 JLI_Launch 调用调用者的“int main (int argc, char *argv[])”吗?

问题描述

在调用之前我可以做一些简单的事情JLI_Launch来阻止它的 macOS 引导行为(这在 linux 上不会发生)?还是我必须继续阅读有关调用 api的更多信息并使用JLI_Launch(例如JNI_CreateJavaVM)以外的其他内容?如果你想知道为什么这么复杂的启动器,如果我不使用它的架构(不创建子进程),然后将文件放到应用程序的图标上(停靠或不停靠)打开应用程序但应用程序不到文件掉落事件。

JLI_Launch表现得好像代码如下所示:

static int callCount = 0;
int JLI_Launch (/* parameters */) {
     if (callCount == 0) {
        callCount = 1;
        /* figure out what to tell the caller by creating argc,argv */
        main(argc, argv);
     } else {
        callCount = 0;
        // launch the VM
     }
     return 0;
}

appbundle 项目有在调用和之后调用的代码。然后该函数调用重复所有内容的 appbundler ,并使用相同的参数再次调用。第二次调用时,虚拟机运行java代码。JLI_LaunchdlopendlsymJLI_LaunchmainJLI_LaunchJLI_Launch

除了在第二次执行准备代码时可能发生的一些奇怪的灾难(例如,选择与第一次执行时选择不同的 Java 版本)之外,为准备调用而执行的代码量JLI_Launch非常重要。为相同的结果执行两次是浪费。

这是 MCV 示例。我知道它是 C 而不是 Objective-C,但这部分并不重要。至少它似乎并不重要,因为它重现了我认为不受欢迎的“引导”行为。

#include <stdio.h>
#include <dlfcn.h>
#include "jni.h"

int main ( int argc, char *argv[] ) {
    // The code required to identify jargv is somewhat significant.
    char * jargv[] = { "test.exe", "Test", "Hello, world." };
    int jargc = sizeof(jargv) / sizeof(jargv[0]);
    // The code required to identify lib is significant.
    char * lib = "/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jli/libjli.dylib";
    void * h = dlopen(lib, RTLD_LAZY);
    void (*launcher)() = dlsym(h, "JLI_Launch");
    puts("calling JLI_Launch");
    launcher(jargc, jargv,
             0, NULL, 0, NULL,
             "", "", "java", "java",
             JNI_FALSE, JNI_FALSE, JNI_FALSE,
             (jint) 0);
    return 0;
}

这是它将执行的java代码:

public class Test {
    public static void main (String[] args) {
        System.out.println(args[0]);
    }
}

CLG:

$ gcc -o test.exe -g -I $JAVA_HOME/include -I $JAVA_HOME/include/darwin test.c && ./test.exe
calling JLI_Launch
calling JLI_Launch
Hello, world.

这是使用调试器的更多证明:

$ lldb test.exe
(lldb) target create "test.exe"
Current executable set to '.../test.exe' (x86_64).
(lldb) b main
Breakpoint 1: where = test.exe`main + 40 at test.c:6:12, address = 0x0000000100003da8
(lldb) r
Process 27252 launched: '.../test.exe' (x86_64)
Process 27252 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100003da8 test.exe`main(argc=1, argv=0x00007ffeefbfeb00) at test.c:6:12
   3    #include "jni.h"
   4
   5    int main ( int argc, char *argv[] ) {
-> 6        char * jargv[] = { "test.exe", "Test", "Hello, world." };
                   ^
   7        int jargc = sizeof(jargv) / sizeof(jargv[0]);
   8        char * lib = "/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jli/libjli.dylib";
   9        void * h = dlopen(lib, RTLD_LAZY);
Target 0: (test.exe) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0000000100003da8 test.exe`main(argc=1, argv=0x00007ffeefbfeb00) at test.c:6:12
    frame #1: 0x00007fff20659621 libdyld.dylib`start + 1
(lldb) c
Process 27252 resuming
22 locations added to breakpoint 1
calling JLI_Launch
Process 27252 stopped
* thread #2, stop reason = breakpoint 1.1
    frame #0: 0x0000000100003da8 test.exe`main(argc=3, argv=0x0000000100307a70) at test.c:6:12
   3    #include "jni.h"
   4
   5    int main ( int argc, char *argv[] ) {
-> 6        char * jargv[] = { "test.exe", "Test", "Hello, world." };
                   ^
   7        int jargc = sizeof(jargv) / sizeof(jargv[0]);
   8        char * lib = "/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jli/libjli.dylib";
   9        void * h = dlopen(lib, RTLD_LAZY);
Target 0: (test.exe) stopped.
(lldb) bt
* thread #2, stop reason = breakpoint 1.1
  * frame #0: 0x0000000100003da8 test.exe`main(argc=3, argv=0x0000000100307a70) at test.c:6:12
    frame #1: 0x000000010015e31f libjli.dylib`apple_main + 84
    frame #2: 0x00007fff2063e950 libsystem_pthread.dylib`_pthread_start + 224
    frame #3: 0x00007fff2063a47b libsystem_pthread.dylib`thread_start + 15
(lldb)

后记

启动器代码异常的一个线索是在任何地方都没有包含字符串的头文件JLI_。我只能找到JNI_函数定义,并且只能在jni.h.

“bootstrapiness”的另一个线索是使用不同的调用和使用JLI_Launch的调用。但是我不明白为什么我会关心,因为代码两次都发送了相同的值。如果我认为反馈是必要的,那么我会接受它,但我认为没有。mainargcargv_startJLI_Launch

另一个线索是,在 linux 上,至少在 JDK 1.8.0_121 中,这种行为是不存在的。我不得不做一些意想不到的事情来让它发挥作用:

1. call the executable bin/java instead of test.exe
2. soft link the JDK lib folder

显然,在 linux 上,java 调用 dlopen 以根据其对主文件夹的了解来链接其库。

标签: javamacoslaunch

解决方案


在查看了我发布的回溯后,我意识到了两件事:

1. I see the name of the calling function, `apple_main`
2. I might be able to find its definition if I download Java SE 16's source code

我找到了apple_mainin的定义src/java.base/macosx/native/libjli/java_md_macosx.m

/*
 * Unwrap the arguments and re-run main()
 */
static void *apple_main (void *arg)
{
    if (main_fptr == NULL) {
#ifdef STATIC_BUILD
        extern int main(int argc, char **argv);
        main_fptr = &main;
#else
        main_fptr = (int (*)())dlsym(RTLD_DEFAULT, "main");
#endif
        if (main_fptr == NULL) {
            JLI_ReportErrorMessageSys("error locating main entrypoint\n");
            exit(1);
        }
    }

    struct NSAppArgs *args = (struct NSAppArgs *) arg;
    exit(main_fptr(args->argc, args->argv));
}

pthread_start隐藏的调用(根据定义)一些调用序列(以堆栈形式显示),即:

test.c: main (new main thread)
src/java.base/macosx/native/libjli/java_md_macosx.m: apple_main (new main thread)
src/java.base/macosx/native/libjli/java_md_macosx.m: MacOSXStartup (main thread)
src/java.base/macosx/native/libjli/java_md_macosx.m: CreateExecutionEnvironment (main thread)
src/java.base/share/native/libjli/java.c: JLI_Launch (main thread)
test.c: main (main thread)

我现在可以继续了。


推荐阅读