Fix SafeHandle marshalling in ref/in/out parameters (#17330)
* Fix SafeHandle marshalling in ref/in/out parameters
* Reorder code to make it exception safe, update comments
* Revert the code reordering because it breaks the invariant that new handle returned from native code should always create new SafeHandle on the managed side
* Fix test_0_safehandle_ref test. The test was expecting incorrect behavior, verified on .NET Core 3 and .NET Framework 4.8.
* Address PR feedback
* Fix cut & paste error
* Fix build
* Really fix the test_0_safehandle_ref tests
* Add more tests for SafeHandle marshalling
* Add exclusion for broken CoreFX test