1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#define L2_JNI__NO_DEFINE_PROXIES
#include "config.h"
#include "jniwrap.h"
#include "macros.h"
#include "command.h"
#include <stddef.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
/* this isn't even a little bit thread safe but it is what it is */
jint JNICALL l2_JNI__GetDefaultJavaVMInitArgs_load(void *args);
jint JNICALL l2_JNI__CreateJavaVM_load(JavaVM **pvm, void **penv, void *args);
l2_JNI__GetDefaultJavaVMInitArgs_t *l2_JNI__GetDefaultJavaVMInitArgs_call = &l2_JNI__GetDefaultJavaVMInitArgs_load;
l2_JNI__CreateJavaVM_t *l2_JNI__CreateJavaVM_call = &l2_JNI__CreateJavaVM_load;
void *volatile l2_JNI__jnimod = NULL;
int l2_jni__try_load(const char *path)
{
struct stat st;
if (stat(path, &st) < 0) {
CMD_ERROR("Could not stat %s: %s", path, strerror(errno));
return -1;
}
void *thelib = dlopen(path, RTLD_LAZY);
if (!thelib) {
CMD_ERROR("Failed to load JVM library %s: %s", path, dlerror());
return -1;
}
l2_JNI__jnimod = thelib;
return 0;
}
int l2_jni_init(const char *modpath)
{
if (l2_JNI__jnimod) return 0;
if (l2_jni__try_load(modpath) < 0) {
CMD_ERROR0("Could not load JVM library.");
return -1;
}
return 0;
}
typedef void (l2_jni__fsym_unspec)(void);
l2_jni__fsym_unspec *l2_jni__try_load_sym(const char *sym)
{
void *themod = l2_JNI__jnimod;
if (!themod) {
CMD_MSG("bug", "JNI function %s called before l2_jni_init!", sym);
abort();
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
l2_jni__fsym_unspec *thesym = dlsym(themod, sym);
#pragma GCC diagnostic pop
if (!thesym) {
CMD_ERROR("Could not load JNI symbol %s: %s", sym, dlerror());
abort();
}
return thesym;
}
jint JNICALL l2_JNI__GetDefaultJavaVMInitArgs_load(void *args)
{
l2_JNI__GetDefaultJavaVMInitArgs_t *thesym = (l2_JNI__GetDefaultJavaVMInitArgs_t *)l2_jni__try_load_sym("JNI_GetDefaultJavaVMInitArgs");
l2_JNI__GetDefaultJavaVMInitArgs_call = thesym;
return (*thesym)(args);
}
jint JNICALL l2_JNI__CreateJavaVM_load(JavaVM **pvm, void **penv, void *args)
{
l2_JNI__CreateJavaVM_t *thesym = (l2_JNI__CreateJavaVM_t *)l2_jni__try_load_sym("JNI_CreateJavaVM");
l2_JNI__CreateJavaVM_call = thesym;
return (*thesym)(pvm, penv, args);
}
|