From 7f9c8cbebe6b0717e1d647aa3c4d7041c3297d44 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Tue, 5 Nov 2024 12:49:57 +0000 Subject: [PATCH 01/24] Sentry options - ios | introduce `IOS.ExceptionMode` option --- src/Sentry/Platforms/Cocoa/IOSOptions.cs | 94 ++++++++++++++++++++++++ src/Sentry/Platforms/Cocoa/SentrySdk.cs | 6 +- src/Sentry/SentryOptions.cs | 1 + 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/Sentry/Platforms/Cocoa/IOSOptions.cs diff --git a/src/Sentry/Platforms/Cocoa/IOSOptions.cs b/src/Sentry/Platforms/Cocoa/IOSOptions.cs new file mode 100644 index 0000000000..bdb76bc44c --- /dev/null +++ b/src/Sentry/Platforms/Cocoa/IOSOptions.cs @@ -0,0 +1,94 @@ + + +// ReSharper disable once CheckNamespace +namespace Sentry; + + +/// +/// Extension for MarshalExceptionMode Enum +/// +public static class MarshalExceptionModeExtension +{ + /// + /// Converts the ExceptionMode enum to ObjCRuntime.Runtime.MarshalManagedException + /// + /// ObjCRuntime.Runtime.MarshalManagedException equivallent of the exception mode + public static ObjCRuntime.MarshalManagedExceptionMode ToObjC(this MarshalExceptionMode value) + { + switch (value) + { + case MarshalExceptionMode.Default: + return ObjCRuntime.MarshalManagedExceptionMode.Default; + case MarshalExceptionMode.UnwindNativeCode: + return ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; + case MarshalExceptionMode.Abort: + return ObjCRuntime.MarshalManagedExceptionMode.Abort; + default: + return ObjCRuntime.MarshalManagedExceptionMode.Default; + } + } + +} + +/// +/// Exception mode referencing ObjCRuntime.Runtime.MarshalManagedException +/// +public enum MarshalExceptionMode +{ + /// + /// The default varies by platform. It's always ThrowObjectiveCException in .NET. For legacy Xamarin projects, it's ThrowObjectiveCException if the GC is in cooperative mode (watchOS), and UnwindNativeCode otherwise (iOS / watchOS / macOS). The default may change in the future. + /// + Default, + /// + /// This is the previous (undefined) behavior. This isn't available when using the GC in cooperative mode (which is the only option on watchOS; thus, this isn't a valid option on watchOS), nor when using CoreCLR, but it's the default option for all other platforms in legacy Xamarin projects + /// + UnwindNativeCode, + /// + /// Convert the managed exception into an Objective-C exception and throw the Objective-C exception. This is the default in .NET and on watchOS in legacy Xamarin projects + /// + ThrowObjectiveCException, + /// + /// Abort the process + /// + Abort, + /// + /// Disables the exception interception, so it doesn't make sense to set this value in the event handler, but once the event is raised it's too late to disable it. In any case, if set, it will behave as UnwindNativeCode + /// + Disable, + /// + /// Skip MarshalExceptionMode setting + /// + None +} +public partial class SentryOptions +{ + + /// + /// The .NET SDK specific options for the IOS platform. + /// + public IOSOptions IOS { get; } + + /// + /// The .NET SDK specific options for the IOS platform. + /// + + public class IOSOptions + { + /// + /// Gets or sets the exception mode. + /// The default is + /// + /// + /// - Setting ExceptionMode to `None` will disable the switch of marshalling bahavior (recommended for NativeAOT) + /// see https://learn.microsoft.com/en-us/previous-versions/xamarin/ios/platform/exception-marshaling#events + /// + /// + /// ... + /// options.IOS.ExceptionMode = ExceptionMode.None + /// ... + /// + /// + public MarshalExceptionMode ExceptionMode { get; set; } = MarshalExceptionMode.UnwindNativeCode; + + } +} diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index ac075af7a6..546c3fc7d2 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -10,11 +10,15 @@ public static partial class SentrySdk private static void InitSentryCocoaSdk(SentryOptions options) { options.LogDebug("Initializing native SDK"); + + if (options.IOS.ExceptionMode != MarshalExceptionMode.None) + { // Workaround for https://github.com/xamarin/xamarin-macios/issues/15252 ObjCRuntime.Runtime.MarshalManagedException += (_, args) => { - args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; + args.ExceptionMode = options.IOS.ExceptionMode.ToObjC(); }; + } // Set default release and distribution options.Release ??= GetDefaultReleaseString(); diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index f4e9b7dcfe..998f7595e9 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -1249,6 +1249,7 @@ public SentryOptions() AssemblyReader = name => reader.Value?.TryReadAssembly(name); #elif __IOS__ + IOS = new IOSOptions(); Native = new NativeOptions(this); #endif From b0f054d72d8203c2c59586a072c62f9b12525230 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 5 Nov 2024 14:32:56 +0000 Subject: [PATCH 02/24] Format code --- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index 546c3fc7d2..65c6a28919 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -13,11 +13,11 @@ private static void InitSentryCocoaSdk(SentryOptions options) if (options.IOS.ExceptionMode != MarshalExceptionMode.None) { - // Workaround for https://github.com/xamarin/xamarin-macios/issues/15252 - ObjCRuntime.Runtime.MarshalManagedException += (_, args) => - { + // Workaround for https://github.com/xamarin/xamarin-macios/issues/15252 + ObjCRuntime.Runtime.MarshalManagedException += (_, args) => + { args.ExceptionMode = options.IOS.ExceptionMode.ToObjC(); - }; + }; } // Set default release and distribution From af21af80f06905cc464f32a4631263e1a0567c63 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Tue, 5 Nov 2024 15:00:31 +0000 Subject: [PATCH 03/24] change log - update | add entry for #3729 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17d4d0fa3c..e9729bf205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixes - When using OTel and ASP.NET Core the SDK could try to process OTel spans after the SDK had been closed ([#3726](https://github.com/getsentry/sentry-dotnet/pull/3726)) +- iOS - added a native option to set ExceptionMode preventing use of unsupported marshalling behaviour for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) ### Dependencies From 70a493be61e4cfc9b755b63c0fa4ddff1511676b Mon Sep 17 00:00:00 2001 From: bricefriha Date: Tue, 5 Nov 2024 16:04:41 +0000 Subject: [PATCH 04/24] IOS Options - MarshalExceptionMode | move enum to StructsAndEnums.cs --- src/Sentry.Bindings.Cocoa/StructsAndEnums.cs | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs b/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs index 4acb1ce487..2dd4307cad 100644 --- a/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs +++ b/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs @@ -87,3 +87,60 @@ internal enum SentryTransactionNameSource : long Component = 4, Task = 5 } + +/// +/// Extension for MarshalExceptionMode Enum +/// +public static class MarshalExceptionModeExtension +{ + /// + /// Converts the ExceptionMode enum to ObjCRuntime.Runtime.MarshalManagedException + /// + /// ObjCRuntime.Runtime.MarshalManagedException equivallent of the exception mode + public static ObjCRuntime.MarshalManagedExceptionMode ToObjC(this MarshalExceptionMode value) + { + switch (value) + { + case MarshalExceptionMode.Default: + return ObjCRuntime.MarshalManagedExceptionMode.Default; + case MarshalExceptionMode.UnwindNativeCode: + return ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; + case MarshalExceptionMode.Abort: + return ObjCRuntime.MarshalManagedExceptionMode.Abort; + default: + return ObjCRuntime.MarshalManagedExceptionMode.Default; + } + } + +} + +/// +/// Exception mode referencing ObjCRuntime.Runtime.MarshalManagedException +/// +public enum MarshalExceptionMode +{ + /// + /// The default varies by platform. It's always ThrowObjectiveCException in .NET. For legacy Xamarin projects, it's ThrowObjectiveCException if the GC is in cooperative mode (watchOS), and UnwindNativeCode otherwise (iOS / watchOS / macOS). The default may change in the future. + /// + Default, + /// + /// This is the previous (undefined) behavior. This isn't available when using the GC in cooperative mode (which is the only option on watchOS; thus, this isn't a valid option on watchOS), nor when using CoreCLR, but it's the default option for all other platforms in legacy Xamarin projects + /// + UnwindNativeCode, + /// + /// Convert the managed exception into an Objective-C exception and throw the Objective-C exception. This is the default in .NET and on watchOS in legacy Xamarin projects + /// + ThrowObjectiveCException, + /// + /// Abort the process + /// + Abort, + /// + /// Disables the exception interception, so it doesn't make sense to set this value in the event handler, but once the event is raised it's too late to disable it. In any case, if set, it will behave as UnwindNativeCode + /// + Disable, + /// + /// Skip MarshalExceptionMode setting + /// + None +} From 11251b5c828fda58e62a177e33c8832f8c0f9782 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 09:19:14 +0000 Subject: [PATCH 05/24] iOS option - MarshalManagedException | introduce `EnableMarshalManagedException` as a replacement for ExceptionMode --- src/Sentry.Bindings.Cocoa/StructsAndEnums.cs | 57 ------------ src/Sentry/Platforms/Cocoa/IOSOptions.cs | 94 -------------------- src/Sentry/Platforms/Cocoa/SentryOptions.cs | 10 ++- 3 files changed, 9 insertions(+), 152 deletions(-) delete mode 100644 src/Sentry/Platforms/Cocoa/IOSOptions.cs diff --git a/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs b/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs index 2dd4307cad..4acb1ce487 100644 --- a/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs +++ b/src/Sentry.Bindings.Cocoa/StructsAndEnums.cs @@ -87,60 +87,3 @@ internal enum SentryTransactionNameSource : long Component = 4, Task = 5 } - -/// -/// Extension for MarshalExceptionMode Enum -/// -public static class MarshalExceptionModeExtension -{ - /// - /// Converts the ExceptionMode enum to ObjCRuntime.Runtime.MarshalManagedException - /// - /// ObjCRuntime.Runtime.MarshalManagedException equivallent of the exception mode - public static ObjCRuntime.MarshalManagedExceptionMode ToObjC(this MarshalExceptionMode value) - { - switch (value) - { - case MarshalExceptionMode.Default: - return ObjCRuntime.MarshalManagedExceptionMode.Default; - case MarshalExceptionMode.UnwindNativeCode: - return ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; - case MarshalExceptionMode.Abort: - return ObjCRuntime.MarshalManagedExceptionMode.Abort; - default: - return ObjCRuntime.MarshalManagedExceptionMode.Default; - } - } - -} - -/// -/// Exception mode referencing ObjCRuntime.Runtime.MarshalManagedException -/// -public enum MarshalExceptionMode -{ - /// - /// The default varies by platform. It's always ThrowObjectiveCException in .NET. For legacy Xamarin projects, it's ThrowObjectiveCException if the GC is in cooperative mode (watchOS), and UnwindNativeCode otherwise (iOS / watchOS / macOS). The default may change in the future. - /// - Default, - /// - /// This is the previous (undefined) behavior. This isn't available when using the GC in cooperative mode (which is the only option on watchOS; thus, this isn't a valid option on watchOS), nor when using CoreCLR, but it's the default option for all other platforms in legacy Xamarin projects - /// - UnwindNativeCode, - /// - /// Convert the managed exception into an Objective-C exception and throw the Objective-C exception. This is the default in .NET and on watchOS in legacy Xamarin projects - /// - ThrowObjectiveCException, - /// - /// Abort the process - /// - Abort, - /// - /// Disables the exception interception, so it doesn't make sense to set this value in the event handler, but once the event is raised it's too late to disable it. In any case, if set, it will behave as UnwindNativeCode - /// - Disable, - /// - /// Skip MarshalExceptionMode setting - /// - None -} diff --git a/src/Sentry/Platforms/Cocoa/IOSOptions.cs b/src/Sentry/Platforms/Cocoa/IOSOptions.cs deleted file mode 100644 index bdb76bc44c..0000000000 --- a/src/Sentry/Platforms/Cocoa/IOSOptions.cs +++ /dev/null @@ -1,94 +0,0 @@ - - -// ReSharper disable once CheckNamespace -namespace Sentry; - - -/// -/// Extension for MarshalExceptionMode Enum -/// -public static class MarshalExceptionModeExtension -{ - /// - /// Converts the ExceptionMode enum to ObjCRuntime.Runtime.MarshalManagedException - /// - /// ObjCRuntime.Runtime.MarshalManagedException equivallent of the exception mode - public static ObjCRuntime.MarshalManagedExceptionMode ToObjC(this MarshalExceptionMode value) - { - switch (value) - { - case MarshalExceptionMode.Default: - return ObjCRuntime.MarshalManagedExceptionMode.Default; - case MarshalExceptionMode.UnwindNativeCode: - return ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; - case MarshalExceptionMode.Abort: - return ObjCRuntime.MarshalManagedExceptionMode.Abort; - default: - return ObjCRuntime.MarshalManagedExceptionMode.Default; - } - } - -} - -/// -/// Exception mode referencing ObjCRuntime.Runtime.MarshalManagedException -/// -public enum MarshalExceptionMode -{ - /// - /// The default varies by platform. It's always ThrowObjectiveCException in .NET. For legacy Xamarin projects, it's ThrowObjectiveCException if the GC is in cooperative mode (watchOS), and UnwindNativeCode otherwise (iOS / watchOS / macOS). The default may change in the future. - /// - Default, - /// - /// This is the previous (undefined) behavior. This isn't available when using the GC in cooperative mode (which is the only option on watchOS; thus, this isn't a valid option on watchOS), nor when using CoreCLR, but it's the default option for all other platforms in legacy Xamarin projects - /// - UnwindNativeCode, - /// - /// Convert the managed exception into an Objective-C exception and throw the Objective-C exception. This is the default in .NET and on watchOS in legacy Xamarin projects - /// - ThrowObjectiveCException, - /// - /// Abort the process - /// - Abort, - /// - /// Disables the exception interception, so it doesn't make sense to set this value in the event handler, but once the event is raised it's too late to disable it. In any case, if set, it will behave as UnwindNativeCode - /// - Disable, - /// - /// Skip MarshalExceptionMode setting - /// - None -} -public partial class SentryOptions -{ - - /// - /// The .NET SDK specific options for the IOS platform. - /// - public IOSOptions IOS { get; } - - /// - /// The .NET SDK specific options for the IOS platform. - /// - - public class IOSOptions - { - /// - /// Gets or sets the exception mode. - /// The default is - /// - /// - /// - Setting ExceptionMode to `None` will disable the switch of marshalling bahavior (recommended for NativeAOT) - /// see https://learn.microsoft.com/en-us/previous-versions/xamarin/ios/platform/exception-marshaling#events - /// - /// - /// ... - /// options.IOS.ExceptionMode = ExceptionMode.None - /// ... - /// - /// - public MarshalExceptionMode ExceptionMode { get; set; } = MarshalExceptionMode.UnwindNativeCode; - - } -} diff --git a/src/Sentry/Platforms/Cocoa/SentryOptions.cs b/src/Sentry/Platforms/Cocoa/SentryOptions.cs index c14213087a..00d0915a02 100644 --- a/src/Sentry/Platforms/Cocoa/SentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/SentryOptions.cs @@ -183,7 +183,15 @@ internal NativeOptions(SentryOptions options) /// The default value is false (disabled). /// public bool EnableTracing { get; set; } = false; - + /// + /// Enable or disable the subscription to ObjCRuntime.Runtime.MarshalManagedException. + /// default value: true + /// + /// + /// We recommend to set EnableMarshalManagedException to false for NativeAOT to avoid the used of unsupported marshelling behaviour + /// for more, see: https://github.com/getsentry/sentry-dotnet/pull/3729 + /// + public bool EnableMarshalManagedException { get; set; } = true; internal List? InAppExcludes { get; private set; } internal List? InAppIncludes { get; private set; } From 1173231ed630977ae445299379dfa79f0a1893a0 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 09:35:54 +0000 Subject: [PATCH 06/24] Change log - | update entry for EnableMarshalManagedException --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9729bf205..080980a545 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ### Fixes - When using OTel and ASP.NET Core the SDK could try to process OTel spans after the SDK had been closed ([#3726](https://github.com/getsentry/sentry-dotnet/pull/3726)) -- iOS - added a native option to set ExceptionMode preventing use of unsupported marshalling behaviour for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) +- iOS - added a native option to set Enable MarshalManagedException preventing use of unsupported marshalling behaviour for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) ### Dependencies From b571c57cd71eca256504a5670e899de408ebb97a Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 09:53:37 +0000 Subject: [PATCH 07/24] iOS option - MarshalManagedException | make sure to get `EnableMarshalManagedException` to disable MarshalException settings --- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index 65c6a28919..062b5b1498 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -11,12 +11,12 @@ private static void InitSentryCocoaSdk(SentryOptions options) { options.LogDebug("Initializing native SDK"); - if (options.IOS.ExceptionMode != MarshalExceptionMode.None) + if (options.Native.EnableMarshalManagedException) { // Workaround for https://github.com/xamarin/xamarin-macios/issues/15252 ObjCRuntime.Runtime.MarshalManagedException += (_, args) => { - args.ExceptionMode = options.IOS.ExceptionMode.ToObjC(); + args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; }; } From 82151e7a5bf55a4897ae05d2244716fbc8ba5811 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 10:01:49 +0000 Subject: [PATCH 08/24] SentryOptions - iOS | remove IOSOptions reference --- src/Sentry/SentryOptions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Sentry/SentryOptions.cs b/src/Sentry/SentryOptions.cs index 998f7595e9..f4e9b7dcfe 100644 --- a/src/Sentry/SentryOptions.cs +++ b/src/Sentry/SentryOptions.cs @@ -1249,7 +1249,6 @@ public SentryOptions() AssemblyReader = name => reader.Value?.TryReadAssembly(name); #elif __IOS__ - IOS = new IOSOptions(); Native = new NativeOptions(this); #endif From e5a1f81bfc9970bae911aa700574a0db7d5af0bb Mon Sep 17 00:00:00 2001 From: Brice Friha <37577669+bricefriha@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:24:06 +0000 Subject: [PATCH 09/24] Update SentryOptions.cs | added spacing between EnableTracing and EnableMarshelManagedException --- src/Sentry/Platforms/Cocoa/SentryOptions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Sentry/Platforms/Cocoa/SentryOptions.cs b/src/Sentry/Platforms/Cocoa/SentryOptions.cs index 00d0915a02..05e1a901b5 100644 --- a/src/Sentry/Platforms/Cocoa/SentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/SentryOptions.cs @@ -183,6 +183,7 @@ internal NativeOptions(SentryOptions options) /// The default value is false (disabled). /// public bool EnableTracing { get; set; } = false; + /// /// Enable or disable the subscription to ObjCRuntime.Runtime.MarshalManagedException. /// default value: true From 38e403d0b4a3d26a6d7c193cdd861bd05738175a Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Wed, 6 Nov 2024 10:32:58 +0000 Subject: [PATCH 10/24] Format code --- modules/sentry-native | 2 +- src/Sentry/Platforms/Cocoa/SentryOptions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/sentry-native b/modules/sentry-native index 33739b5ccf..bf14b8533a 160000 --- a/modules/sentry-native +++ b/modules/sentry-native @@ -1 +1 @@ -Subproject commit 33739b5ccf0c0d1513d284ac64b610e74adb62cf +Subproject commit bf14b8533a3b26853e4e6fecf2f955deaa29e2d8 diff --git a/src/Sentry/Platforms/Cocoa/SentryOptions.cs b/src/Sentry/Platforms/Cocoa/SentryOptions.cs index 05e1a901b5..fa36277269 100644 --- a/src/Sentry/Platforms/Cocoa/SentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/SentryOptions.cs @@ -183,7 +183,7 @@ internal NativeOptions(SentryOptions options) /// The default value is false (disabled). /// public bool EnableTracing { get; set; } = false; - + /// /// Enable or disable the subscription to ObjCRuntime.Runtime.MarshalManagedException. /// default value: true From 7b19d724428e1a45f2e0e1b8c6c86557ea63da2f Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 11:39:41 +0000 Subject: [PATCH 11/24] iOS - bindablesentry options | add `EnableMarshalManagedException` --- src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs b/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs index 5697ccf8cb..35adc27bae 100644 --- a/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs @@ -25,6 +25,7 @@ public class NativeOptions public bool? EnableUIViewControllerTracing { get; set; } public bool? EnableUserInteractionTracing { get; set; } public bool? EnableTracing { get; set; } + public bool? EnableMarshalManagedException { get; set; } public void ApplyTo(SentryOptions.NativeOptions options) { @@ -43,6 +44,7 @@ public void ApplyTo(SentryOptions.NativeOptions options) options.EnableUIViewControllerTracing = EnableUIViewControllerTracing ?? options.EnableUIViewControllerTracing; options.EnableUserInteractionTracing = EnableUserInteractionTracing ?? options.EnableUserInteractionTracing; options.EnableTracing = EnableTracing ?? options.EnableTracing; + options.EnableMarshalManagedException = EnableMarshalManagedException ?? options.EnableMarshalManagedException; } } } From aa2cc4d63a20c7aa97e7703f68f0afe569d0b942 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 13:41:45 +0000 Subject: [PATCH 12/24] unit tests - iOS options | EnableMarshalManagedException setter test --- test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs b/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs index 15fe05a767..b2ae657f4c 100644 --- a/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs +++ b/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs @@ -88,4 +88,13 @@ public void BeforeCaptureScreenshot_NotSet() Assert.Null(options.BeforeCaptureInternal); } + +#if IOS + [Fact] + public void EnableMarshalManagedException_Default() + { + var options = new SentryMauiOptions(); + Assert.True(options.Native.EnableMarshalManagedException); + } +#endif } From 0528ac682d9f7bc023501a92e7c3d56417451ae4 Mon Sep 17 00:00:00 2001 From: Brice Friha <37577669+bricefriha@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:03:24 +0000 Subject: [PATCH 13/24] Cocoa/SentryOptions.cs | Update code doc Co-authored-by: Bruno Garcia --- src/Sentry/Platforms/Cocoa/SentryOptions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sentry/Platforms/Cocoa/SentryOptions.cs b/src/Sentry/Platforms/Cocoa/SentryOptions.cs index fa36277269..618acd1159 100644 --- a/src/Sentry/Platforms/Cocoa/SentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/SentryOptions.cs @@ -189,9 +189,9 @@ internal NativeOptions(SentryOptions options) /// default value: true /// /// - /// We recommend to set EnableMarshalManagedException to false for NativeAOT to avoid the used of unsupported marshelling behaviour - /// for more, see: https://github.com/getsentry/sentry-dotnet/pull/3729 + /// We recommend to set EnableMarshalManagedException to false for NativeAOT to avoid the used of unsupported marshelling behaviour. /// + /// public bool EnableMarshalManagedException { get; set; } = true; internal List? InAppExcludes { get; private set; } internal List? InAppIncludes { get; private set; } From f271bb0d8a04187797f4803c689ecd3deb451d75 Mon Sep 17 00:00:00 2001 From: Brice Friha <37577669+bricefriha@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:04:46 +0000 Subject: [PATCH 14/24] Cocoa/SentrySdk.cs - MarshalManagedException | Update code doc Co-authored-by: Bruno Garcia --- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index 062b5b1498..b80ea0d174 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -13,7 +13,8 @@ private static void InitSentryCocoaSdk(SentryOptions options) if (options.Native.EnableMarshalManagedException) { - // Workaround for https://github.com/xamarin/xamarin-macios/issues/15252 + // Needed for Native AOT but not for MonoVM anymore: + // https://github.com/xamarin/xamarin-macios/issues/15252#issuecomment-2349301905 ObjCRuntime.Runtime.MarshalManagedException += (_, args) => { args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; From 9308b84d207436db5f881a33287967ac6279682e Mon Sep 17 00:00:00 2001 From: Brice Friha <37577669+bricefriha@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:06:32 +0000 Subject: [PATCH 15/24] Update CHANGELOG.md entry to #3729 Co-authored-by: Bruno Garcia --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 080980a545..2bc8d9a39d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ### Fixes - When using OTel and ASP.NET Core the SDK could try to process OTel spans after the SDK had been closed ([#3726](https://github.com/getsentry/sentry-dotnet/pull/3726)) -- iOS - added a native option to set Enable MarshalManagedException preventing use of unsupported marshalling behaviour for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) +- iOS - added a native option to set Enable MarshalManagedException, preventing use of unsupported marshalling behaviour for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) ### Dependencies From e066eb381570e5f46ec6d5e144e4c92372636646 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 15:34:23 +0000 Subject: [PATCH 16/24] iOS - initialisation | bypass the subscription to the subscription of MarshalManagedException event if compiled with Native AOT --- src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs | 2 -- src/Sentry/Platforms/Cocoa/SentryOptions.cs | 9 --------- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 3 ++- test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs | 9 --------- 4 files changed, 2 insertions(+), 21 deletions(-) diff --git a/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs b/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs index 35adc27bae..5697ccf8cb 100644 --- a/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/BindableSentryOptions.cs @@ -25,7 +25,6 @@ public class NativeOptions public bool? EnableUIViewControllerTracing { get; set; } public bool? EnableUserInteractionTracing { get; set; } public bool? EnableTracing { get; set; } - public bool? EnableMarshalManagedException { get; set; } public void ApplyTo(SentryOptions.NativeOptions options) { @@ -44,7 +43,6 @@ public void ApplyTo(SentryOptions.NativeOptions options) options.EnableUIViewControllerTracing = EnableUIViewControllerTracing ?? options.EnableUIViewControllerTracing; options.EnableUserInteractionTracing = EnableUserInteractionTracing ?? options.EnableUserInteractionTracing; options.EnableTracing = EnableTracing ?? options.EnableTracing; - options.EnableMarshalManagedException = EnableMarshalManagedException ?? options.EnableMarshalManagedException; } } } diff --git a/src/Sentry/Platforms/Cocoa/SentryOptions.cs b/src/Sentry/Platforms/Cocoa/SentryOptions.cs index 618acd1159..c14213087a 100644 --- a/src/Sentry/Platforms/Cocoa/SentryOptions.cs +++ b/src/Sentry/Platforms/Cocoa/SentryOptions.cs @@ -184,15 +184,6 @@ internal NativeOptions(SentryOptions options) /// public bool EnableTracing { get; set; } = false; - /// - /// Enable or disable the subscription to ObjCRuntime.Runtime.MarshalManagedException. - /// default value: true - /// - /// - /// We recommend to set EnableMarshalManagedException to false for NativeAOT to avoid the used of unsupported marshelling behaviour. - /// - /// - public bool EnableMarshalManagedException { get; set; } = true; internal List? InAppExcludes { get; private set; } internal List? InAppIncludes { get; private set; } diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index b80ea0d174..8e78b50971 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -11,7 +11,8 @@ private static void InitSentryCocoaSdk(SentryOptions options) { options.LogDebug("Initializing native SDK"); - if (options.Native.EnableMarshalManagedException) + // If compiled with native AOT + if (!(!RuntimeFeature.IsDynamicCodeCompiled && !RuntimeFeature.IsDynamicCodeSupported)) { // Needed for Native AOT but not for MonoVM anymore: // https://github.com/xamarin/xamarin-macios/issues/15252#issuecomment-2349301905 diff --git a/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs b/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs index b2ae657f4c..15fe05a767 100644 --- a/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs +++ b/test/Sentry.Maui.Tests/SentryMauiOptionsTests.cs @@ -88,13 +88,4 @@ public void BeforeCaptureScreenshot_NotSet() Assert.Null(options.BeforeCaptureInternal); } - -#if IOS - [Fact] - public void EnableMarshalManagedException_Default() - { - var options = new SentryMauiOptions(); - Assert.True(options.Native.EnableMarshalManagedException); - } -#endif } From 3386940ca745362ffd5f61f789c37bd8761aae31 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 6 Nov 2024 16:55:13 +0000 Subject: [PATCH 17/24] iOS - initialisation | just realised we already have a method that checks AOT --- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index 8e78b50971..da688b91ab 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -1,5 +1,6 @@ using Sentry.Cocoa; using Sentry.Cocoa.Extensions; +using Sentry.Internal; using Sentry.Extensibility; // ReSharper disable once CheckNamespace @@ -12,7 +13,7 @@ private static void InitSentryCocoaSdk(SentryOptions options) options.LogDebug("Initializing native SDK"); // If compiled with native AOT - if (!(!RuntimeFeature.IsDynamicCodeCompiled && !RuntimeFeature.IsDynamicCodeSupported)) + if (!AotHelper.IsAOT) { // Needed for Native AOT but not for MonoVM anymore: // https://github.com/xamarin/xamarin-macios/issues/15252#issuecomment-2349301905 From 35f6cdf384bcdbccce195fc0602f017a78bfdcec Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Wed, 6 Nov 2024 17:04:46 +0000 Subject: [PATCH 18/24] Format code --- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index da688b91ab..b6a833af72 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -1,7 +1,7 @@ using Sentry.Cocoa; using Sentry.Cocoa.Extensions; -using Sentry.Internal; using Sentry.Extensibility; +using Sentry.Internal; // ReSharper disable once CheckNamespace namespace Sentry; From 137a68fe683f8e498a6a0509603ccf0135bb42f6 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Thu, 7 Nov 2024 08:50:08 +0000 Subject: [PATCH 19/24] changelog | update entry post merge --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b437a432b..fbb2f2ce8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixes + +- iOS - prevent use of unsupported marshalling behavior `MarshalManagedExceptionMode.UnwindNativeCode` for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) + ### Dependencies - Bump Cocoa SDK from v8.36.0 to v8.39.0 ([#3727](https://github.com/getsentry/sentry-dotnet/pull/3727)) @@ -20,7 +24,6 @@ ### Fixes - When using OTel and ASP.NET Core the SDK could try to process OTel spans after the SDK had been closed ([#3726](https://github.com/getsentry/sentry-dotnet/pull/3726)) -- iOS - added a native option to set Enable MarshalManagedException, preventing use of unsupported marshalling behaviour for [Native AOT] ([#3729](https://github.com/getsentry/sentry-dotnet/pull/3729)) ### Dependencies From fa4d4340d376ab521b6db700bb710a49747634b9 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Thu, 7 Nov 2024 09:26:34 +0000 Subject: [PATCH 20/24] cocoa - build transitive | prevent use of unsupported marshalling behaviour: move the AOT check to compilation --- .../buildTransitive/Sentry.Bindings.Cocoa.targets | 1 + src/Sentry/Platforms/Cocoa/SentrySdk.cs | 15 ++++----------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets b/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets index 4b74ad10fe..a81ed4d676 100644 --- a/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets +++ b/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets @@ -13,6 +13,7 @@ $(TEMP)\Xamarin\HotRestart\Resources\ + --marshal-objectivec-exceptions:disable diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index b6a833af72..ac075af7a6 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -1,7 +1,6 @@ using Sentry.Cocoa; using Sentry.Cocoa.Extensions; using Sentry.Extensibility; -using Sentry.Internal; // ReSharper disable once CheckNamespace namespace Sentry; @@ -11,17 +10,11 @@ public static partial class SentrySdk private static void InitSentryCocoaSdk(SentryOptions options) { options.LogDebug("Initializing native SDK"); - - // If compiled with native AOT - if (!AotHelper.IsAOT) + // Workaround for https://github.com/xamarin/xamarin-macios/issues/15252 + ObjCRuntime.Runtime.MarshalManagedException += (_, args) => { - // Needed for Native AOT but not for MonoVM anymore: - // https://github.com/xamarin/xamarin-macios/issues/15252#issuecomment-2349301905 - ObjCRuntime.Runtime.MarshalManagedException += (_, args) => - { - args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; - }; - } + args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode; + }; // Set default release and distribution options.Release ??= GetDefaultReleaseString(); From 9137e0cbf81832daed095d4c7644cd514ac3fce2 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Fri, 8 Nov 2024 17:50:49 +0000 Subject: [PATCH 21/24] InitSentryCocoaSdk - Native AOT | set `marshal-managed-exceptions` at compilation instead of runtime --- .../Sentry.Bindings.Cocoa.targets | 16 +++++++++++++++- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 5 ----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets b/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets index a81ed4d676..240f1c78c2 100644 --- a/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets +++ b/src/Sentry.Bindings.Cocoa/buildTransitive/Sentry.Bindings.Cocoa.targets @@ -1,5 +1,20 @@ + + + + marshal-managed-exceptions:disable + True + + + + --marshal-managed-exceptions:unwindnativecode + + + - marshal-managed-exceptions:disable + --marshal-managed-exceptions:disable