Skip to content

Commit c2585dc

Browse files
0-8-15mgorges
authored andcommitted
ANDROID: Additional error checking on JNI #386
1) Android 9/10 restricts access to JNI and Jave reflection API's. This adds additional checks. Commented out an possible optimization, which could be assumed to works, but does not for me so far. Please leave this comment in, so we recall before we wonder why and try over and over again. Once in a while we should ponder if it still does not work or why. 2) Clear Java exceptions. This enables to continue to run when an exception occured during a JNI call. (Future versions shall forward this as an exception in Gambit.)
1 parent 7312d92 commit c2585dc

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

loaders/android/bootstrap.c.in

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,37 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved){
9090
JNIEnv* GetJNIEnv(){
9191
int error=0;
9292
JNIEnv* env = NULL;
93-
if (s_vm) error=(*s_vm)->AttachCurrentThread(s_vm, &env, NULL);
94-
if (!error&&(*env)->ExceptionCheck(env)) return NULL;
93+
/* static `env` does NOT work! Once in a while we should ponder if
94+
it still does not work or why.
95+
96+
if(env) {
97+
if((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
98+
return env;
99+
}
100+
*/
101+
if(s_vm) error=(*s_vm)->AttachCurrentThread(s_vm, &env, NULL);
102+
if(!error) error = JNI_forward_exception_to_gambit(env);
95103
return (error?NULL:env);
96104
}
97105

106+
int JNI_forward_exception_to_gambit(JNIEnv*env) {
107+
// TBD: actually forward, not only clear!
108+
if((*env)->ExceptionCheck(env)) {
109+
(*env)->ExceptionClear(env);
110+
return 1;
111+
}
112+
return 0;
113+
}
114+
98115
// url launcher ffi
99116
void android_launch_url(char* urlstring){
100117
JNIEnv *env = GetJNIEnv();
101-
jstring jurlstring = (*env)->NewStringUTF(env,urlstring);
102118
if (env&&globalObj) {
119+
jstring jurlstring = (*env)->NewStringUTF(env,urlstring);
103120
jclass cls = (*env)->FindClass(env, "@SYS_PACKAGE_SLASH@/@SYS_APPNAME@");
104-
jmethodID method = (*env)->GetMethodID(env, cls, "openURL", "(Ljava/lang/String;)V");
105-
(*env)->CallVoidMethod(env, globalObj, method, jurlstring);
121+
jmethodID method = cls ? (*env)->GetMethodID(env, cls, "openURL", "(Ljava/lang/String;)V") : NULL;
122+
if (method) (*env)->CallVoidMethod(env, globalObj, method, jurlstring);
123+
JNI_forward_exception_to_gambit(env);
106124
}
107125
}
108126

0 commit comments

Comments
 (0)