Bug 24665 - We need to override copyWithZone: for INSCopying classes
Summary: We need to override copyWithZone: for INSCopying classes
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: Master
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2014-11-19 12:35 UTC by Rolf Bjarne Kvinge [MSFT]
Modified: 2014-12-01 05:25 UTC (History)
2 users (show)

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:

Description Rolf Bjarne Kvinge [MSFT] 2014-11-19 12:35:43 UTC
Test case:
- Checkout git@github.com:xamarin/Profiler (I have 55fc81632e37bb0e38de24f086933affc5695dba)
- git revert -n 96d2ff6e8b0d1a1c83b40f1132db101da6f4dce6 (This is a work around hack)
- Apply this: https://gist.github.com/rolfbjarne/f0d690b0e68c96039ddd (needed because the XM class libs haven't been properly audited for NRC yet)
- Open solution, enable NRC, build & run
- It'll crash right away.

- NSCell implements copyWithZone: using NSCopyObject, which is evil [1].
- NSCopyObject will do a bitwise copy of all the ivars (without any other processing), which includes copying our GCHandle stored in the __monoObjectGCHandle field.
- Suddenly there are two different native objects pointing to the same managed object through the __monoObjectGCHandle field.
- One of those native objects is freed. The GCHandle will be disposed.
- Now there is a native object with an invalid GCHandle.
- Some other managed object creates a GCHandle with the same numerical valid as the invalid one.
- The copied native object now points to a different managed object (potentially with a completely different type).
- OSX calls a method on the copied native object, we fetch the "corresponding" managed object, and bad things happen when we call an instance in type FOO where the instance is type BAR.

Hacky workaround: https://gist.github.com/rolfbjarne/d9cbf624ba10c2c77159
- This works by clearing out the __monoObjectGCHandle field when the object is copied, so that the GCHandle doesn't end up duplicated.

Proper fix: detect when a user class implements INSCopying and implement copyWithZone: automagically. This also needs to work when the user overrides copyWithZone:.

[1] https://www.mikeash.com/pyblog/friday-qa-2010-08-27-defensive-programming-in-cocoa.html - section about "Copying"
Comment 1 Rolf Bjarne Kvinge [MSFT] 2014-12-01 05:25:57 UTC

maccore/master: d762a2202ef87fa8407da5b92444dd4e144485ba
xamcore/master: d8a69da963360b381cc16e310b05363502d57e81