Bug 15050 - We should not crash when passing a zero jni handle to native
Summary: We should not crash when passing a zero jni handle to native
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 4.8.x
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-09-27 16:08 UTC by Rodrigo Kumpera
Modified: 2017-06-30 02:10 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Rodrigo Kumpera 2013-09-27 16:08:03 UTC
For some reason we crash when we pass a zero jni handle to native.

Is this a crash in dalvik or our code? If it's in our code, we better fix it.
If it's in dalvik, we better offer to handle it under some configuration/option as the native crash makes us look bad
and there are cases where user code bug can trigger this.


It doesn't help if we crash if an user call methods on disposed objects.
Comment 1 Jonathan Pryor 2013-09-27 16:31:25 UTC
> Is this a crash in dalvik or our code?

Dalvik.

> If it's in dalvik, we better offer to handle it under some configuration/option
> as the native crash makes us look bad

Do you have any suggestions on what we should do?

Some cursory suggestions:

1. Make Java.Lang.Object.Handle throw if it's IntPtr.Zero
2. JNIEnv.Call*Method() can check for a null `this` pointer, then
2.a) Generate a warning, then ignore the call
2.b) Generate a warning and abort the process

> It doesn't help if we crash if an user call methods on disposed objects.

I'm less caring if someone explicitly disposes of an object and then things blow up:

  var o = new Java.Lang.Object();
  o.Dispose();
  string s = o.ToString(); // BOOM

However, if/when the GC collects the instance (GC bugs! See Bug #13858 and Bug #14999), things get more confusing.

To return to the "cursory suggestions":

The problem with (1) is that, while it sounds fairly clean (calling a method on an invalidated object would crash the process with an unhandled exception, with a plausibly useful message), it has a flaw:

It provides no way for the user to manually workaround GC bugs unless they use try/catch.

(I suppose this could be opt-in via AndroidEnvironment...)

(2a) just sounds evil.

(2b) should be reasonably easy to do.
Comment 2 Jonathan Pryor 2013-10-08 12:50:53 UTC
monodroid/c71b6125 implements (3): JNIEnv.* methods null-check their arguments and will throw an ArgumentException or ArgumentNullException if IntPtr.Zero or null is passed when it is inappropriate.

Consequently, `o.ToString()` now results in an ArgumentException instead of a process abort.

Keeping this open in case we need to discuss alternatives.