Calling JNI functions with a JNI exception pending

356 views
Skip to first unread message

scgames2

unread,
Apr 13, 2020, 7:15:12 PM4/13/20
to android-ndk
The documentation here gives a list of JNI functions that you're allowed to call when a JNI exception is pending. This list includes PushLocalFrame(). It seems therefore that this code:

// Generate a 'class not found' exception.
environment
->FindClass("foo");

// Generate an exception due to invalid capacity.
environment
->PushLocalFrame(1000000000);

Should be safe. However, using the Android Studio emulator, on API level 27 and later, the PushLocalFrame() call crashes with a message like the following:

Abort message: 'Throwing new exception 'PushLocalFrame: Requested size exceeds maximum: 1000000006' with unexpected pending exception: java.lang.ClassNotFoundException: Didn't find class "foo" on path... [etc.]

Obviously PushLocalFrame() is unlikely to throw under normal circumstances, but crashing in this case seems to conflict with the documentation's claim that these functions are safe to call when there's an exception pending.

What behavior should be expected here? Could this be a flaw in the specification or documentation? Or perhaps a bug in the Android implementation?

Dan Albert

unread,
Apr 13, 2020, 7:19:52 PM4/13/20
to android-ndk, Elliott Hughes
+enh

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/8f9498b6-57fd-4333-9ed6-e7217fad6a5d%40googlegroups.com.

enh

unread,
Apr 13, 2020, 7:45:02 PM4/13/20
to Dan Albert, android-ndk
(I think you dropped the original poster, but...)

No, that's not quite the same thing... There are functions you can never call with a pending exception, and this isn't one of those. But if you call this and it needs to throw and there's already a pending exception, boom. 

The guarantee is only meant to enable you to write exception recovery code, not to ignore pending exceptions 😀

scgames2

unread,
Apr 14, 2020, 11:14:35 AM4/14/20
to android-ndk
Thanks for the reply. It sounds like 'safe' here doesn't necessarily include cases where the function throws, which answers my question.

The guarantee is only meant to enable you to write exception recovery code, not to ignore pending exceptions 😀
 
I'm not sure what you mean by this - no one said anything about ignoring exceptions. In fact, the whole purpose of clarifying the issue is to write safe exception recovery code, as you mention. Maybe there's some misunderstanding here.

Also, just in case the information might be useful to someone, I'll mention that in API level 26 at least, this did not cause a crash. Instead, in API level 26, PushLocalFrame() returns an error value, but preserves the pending exception rather than throwing a new one, and completes successfully. (As far as I can tell the specification/documentation doesn't say anything about what behavior should be expected in this case. In practice it seems to be implementation-dependent.)
+enh

To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages