Skip to content

Commit 8559ea9

Browse files
committed
Add PrivateSentrySDKOnly.isManagedRuntime
1 parent b8af70b commit 8559ea9

File tree

8 files changed

+43
-30
lines changed

8 files changed

+43
-30
lines changed

Sources/Sentry/PrivateSentrySDKOnly.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ @implementation PrivateSentrySDKOnly
3333

3434
static SentryOnAppStartMeasurementAvailable _onAppStartMeasurementAvailable;
3535
static BOOL _appStartMeasurementHybridSDKMode = NO;
36+
static BOOL _isManagedRuntime = NO;
3637
#if SENTRY_HAS_UIKIT
3738
static BOOL _framesTrackingMeasurementHybridSDKMode = NO;
3839
#endif // SENTRY_HAS_UIKIT
@@ -169,6 +170,16 @@ + (void)setAppStartMeasurementHybridSDKMode:(BOOL)appStartMeasurementHybridSDKMo
169170
_appStartMeasurementHybridSDKMode = appStartMeasurementHybridSDKMode;
170171
}
171172

173+
+ (BOOL)isManagedRuntime
174+
{
175+
return _isManagedRuntime;
176+
}
177+
178+
+ (void)setIsManagedRuntime:(BOOL)isManagedRuntime
179+
{
180+
_isManagedRuntime = isManagedRuntime;
181+
}
182+
172183
+ (void)setSdkName:(NSString *)sdkName andVersionString:(NSString *)versionString
173184
{
174185
SentryMeta.sdkName = sdkName;

Sources/Sentry/SentryCrashIntegration.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "SentryCrashIntegration.h"
22
#import "SentryCrashInstallationReporter.h"
33

4+
#import "PrivateSentrySDKOnly.h"
45
#import "SentryCrashC.h"
56
#import "SentryCrashIntegrationSessionHandler.h"
67
#import "SentryCrashMonitor_CPPException.h"
@@ -157,6 +158,7 @@ - (void)startCrashHandler:(NSString *)cacheDirectory
157158
}
158159

159160
sentrycrashcm_setEnableSigtermReporting(enableSigtermReporting);
161+
sentrycrashcm_setManagedRuntime([PrivateSentrySDKOnly isManagedRuntime]);
160162

161163
[installation install:cacheDirectory];
162164

Sources/Sentry/include/HybridPublic/PrivateSentrySDKOnly.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ typedef void (^SentryOnAppStartMeasurementAvailable)(
153153
*/
154154
@property (class, nonatomic, assign) BOOL appStartMeasurementHybridSDKMode;
155155

156+
/**
157+
* Whether the SDK is running in a managed Mono/CoreCLR runtime environment and
158+
* needs to chain Unix signal handlers.
159+
*/
160+
@property (class, nonatomic, assign) BOOL isManagedRuntime;
161+
156162
#if SENTRY_UIKIT_AVAILABLE
157163
/**
158164
* Allows hybrid SDKs to enable frame tracking measurements despite other options.

Sources/Sentry/include/SentryCrashMonitor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ bool sentrycrashcm_notifyFatalExceptionCaptured(bool isAsyncSafeEnvironment);
9393
*/
9494
void sentrycrashcm_handleException(struct SentryCrash_MonitorContext *context);
9595

96+
/** Whether to chain Unix signal handlers for managed Mono/CoreCLR runtimes, and
97+
* whether to register null Mach exception ports for EXC_MASK_BAD_ACCESS and
98+
* EXC_MASK_ARITHMETIC.
99+
*/
100+
bool sentrycrashcm_isManagedRuntime(void);
101+
void sentrycrashcm_setManagedRuntime(bool isManagedRuntime);
102+
96103
#ifdef __cplusplus
97104
}
98105
#endif

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static SentryCrashMonitorType g_activeMonitors = SentryCrashMonitorTypeNone;
8585
static bool g_handlingFatalException = false;
8686
static bool g_crashedDuringExceptionHandling = false;
8787
static bool g_requiresAsyncSafety = false;
88+
static bool g_isManagedRuntime = false;
8889

8990
static void (*g_onExceptionEvent)(struct SentryCrash_MonitorContext *monitorContext);
9091

@@ -226,3 +227,13 @@ sentrycrashcm_handleException(struct SentryCrash_MonitorContext *context)
226227
sentrycrashcm_setActiveMonitors(SentryCrashMonitorTypeNone);
227228
}
228229
}
230+
231+
bool sentrycrashcm_isManagedRuntime(void)
232+
{
233+
return g_isManagedRuntime;
234+
}
235+
236+
void sentrycrashcm_setManagedRuntime(bool isManagedRuntime)
237+
{
238+
g_isManagedRuntime = isManagedRuntime;
239+
}

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ installExceptionHandler(void)
487487
}
488488

489489
if (sentrycrashcm_isManagedRuntime()) {
490-
SENTRY_ASYNC_SAFE_LOG_DEBUG("Detected managed Mono/CoreCLR runtime. Not registering "
491-
"exception port for EXC_BAD_ACCESS or EXC_MASK_ARITHMETIC.");
490+
SENTRY_ASYNC_SAFE_LOG_DEBUG("Not registering Mach exception port for EXC_BAD_ACCESS "
491+
"or EXC_MASK_ARITHMETIC in a managed Mono/CoreCLR runtime");
492492
mask &= ~(EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC);
493493
}
494494

@@ -568,23 +568,6 @@ addContextualInfoToEvent(struct SentryCrash_MonitorContext *eventContext)
568568

569569
#endif // SENTRY_HAS_MACH
570570

571-
bool
572-
sentrycrashcm_isManagedRuntime(void)
573-
{
574-
#if SENTRY_HAS_MACH
575-
for (mach_msg_type_number_t i = 0; i < g_previousExceptionPorts.count; i++) {
576-
// https://github.com/dotnet/runtime/blob/6d96e28597e7da0d790d495ba834cc4908e442cd/src/mono/mono/mini/mini-darwin.c#L85-L90
577-
// https://github.com/dotnet/runtime/blob/6d96e28597e7da0d790d495ba834cc4908e442cd/src/coreclr/nativeaot/Runtime/unix/HardwareExceptions.cpp#L615-L621
578-
if (g_previousExceptionPorts.masks[i] == (EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC)
579-
&& g_previousExceptionPorts.behaviors[i] == EXCEPTION_STATE_IDENTITY
580-
&& g_previousExceptionPorts.flavors[i] == MACHINE_THREAD_STATE) {
581-
return true;
582-
}
583-
}
584-
#endif // SENTRY_HAS_MACH
585-
return false;
586-
}
587-
588571
SentryCrashMonitorAPI *
589572
sentrycrashcm_machexception_getAPI(void)
590573
{

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ bool sentrycrashcm_isReservedThread(thread_t thread);
4949
*/
5050
bool sentrycrashcm_hasReservedThreads(void);
5151

52-
/** Detect managed Mono/CoreCLR runtime's Mach exception port.
53-
*/
54-
bool sentrycrashcm_isManagedRuntime(void);
55-
5652
#ifdef __cplusplus
5753
}
5854
#endif

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151

5252
static volatile bool g_isEnabled = false;
5353
static bool g_isSigtermReportingEnabled = false;
54-
static bool g_isManagedRuntime = false;
5554

5655
static SentryCrash_MonitorContext g_monitorContext;
5756
static SentryCrashStackCursor g_stackCursor;
@@ -110,9 +109,9 @@ handleSignal(int sigNum, siginfo_t *signalInfo, void *userContext)
110109
{
111110
SENTRY_ASYNC_SAFE_LOG_DEBUG("Trapped signal %d", sigNum);
112111

113-
// Let managed Mono/CoreCLR runtime handle the signal first,
114-
// as they may convert it into a managed exception.
115-
if (g_isManagedRuntime) {
112+
if (sentrycrashcm_isManagedRuntime()) {
113+
// Let managed Mono/CoreCLR runtime handle the signal first,
114+
// as they may convert it into a managed exception.
116115
SENTRY_ASYNC_SAFE_LOG_DEBUG(
117116
"Detected managed Mono/CoreCLR runtime. Passing signal to previous handlers.");
118117

@@ -161,7 +160,7 @@ handleSignal(int sigNum, siginfo_t *signalInfo, void *userContext)
161160

162161
// Re-raise the signal to invoke the previous handlers unless already called
163162
// above for managed runtimes.
164-
if (!g_isManagedRuntime) {
163+
if (!sentrycrashcm_isManagedRuntime()) {
165164
SENTRY_ASYNC_SAFE_LOG_DEBUG("Re-raising signal for regular handlers to catch.");
166165
// This is technically not allowed, but it works in OSX and iOS.
167166
raise(sigNum);
@@ -177,8 +176,6 @@ installSignalHandler(void)
177176
{
178177
SENTRY_ASYNC_SAFE_LOG_DEBUG("Installing signal handler.");
179178

180-
g_isManagedRuntime = sentrycrashcm_isManagedRuntime();
181-
182179
# if SENTRY_HAS_SIGNAL_STACK
183180

184181
if (g_signalStack.ss_size == 0) {

0 commit comments

Comments
 (0)