Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 26 additions & 38 deletions Sentry.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions Sources/Sentry/SentryBaseIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ - (BOOL)shouldBeEnabledWithOptions:(SentryOptions *)options
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableCoreDataTracing)
&& !options.enableCoreDataTracing) {
[self logWithOptionName:@"enableCoreDataTracing"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableSwizzling) && !options.enableSwizzling) {
[self logWithOptionName:@"enableSwizzling"];
return NO;
Expand Down
85 changes: 0 additions & 85 deletions Sources/Sentry/SentryCoreDataSwizzling.m

This file was deleted.

17 changes: 17 additions & 0 deletions Sources/Sentry/SentryCoreDataSwizzlingHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import "SentryDefines.h"

NS_ASSUME_NONNULL_BEGIN

@interface SentryCoreDataSwizzlingHelper : NSObject

+ (void)swizzleWithTracker:(SENTRY_SWIFT_MIGRATION_ID(SentryCoreDataTracker))tracker;

+ (void)unswizzle;

#if SENTRY_TEST || SENTRY_TEST_CI
+ (BOOL)swizzlingActive;
#endif

@end

NS_ASSUME_NONNULL_END
78 changes: 78 additions & 0 deletions Sources/Sentry/SentryCoreDataSwizzlingHelper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#import "SentryCoreDataSwizzlingHelper.h"
#import "SentryCoreDataTracker.h"
#import "SentrySwift.h"
#import "SentrySwizzle.h"
#import <CoreData/CoreData.h>
#import <objc/runtime.h>

@implementation SentryCoreDataSwizzlingHelper

static __weak SentryCoreDataTracker *_tracker = nil;
#if SENTRY_TEST || SENTRY_TEST_CI
static BOOL swizzlingIsActive = FALSE;
#endif

// SentrySwizzleInstanceMethod declaration shadows a local variable. The swizzling is working
// fine and we accept this warning.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
+ (void)swizzleWithTracker:(SentryCoreDataTracker *)tracker
{
_tracker = tracker;
#if SENTRY_TEST || SENTRY_TEST_CI
swizzlingIsActive = TRUE;
#endif

SEL fetchSelector = NSSelectorFromString(@"executeFetchRequest:error:");
SentrySwizzleInstanceMethod(NSManagedObjectContext.class, fetchSelector,
SentrySWReturnType(NSArray *),
SentrySWArguments(NSFetchRequest * originalRequest, NSError * *error), SentrySWReplacement({
return _tracker != nil
? [_tracker
managedObjectContext:self
executeFetchRequest:originalRequest
error:error
originalImp:^NSArray *(NSFetchRequest *request, NSError **outError) {
return SentrySWCallOriginal(request, outError);
}]
: SentrySWCallOriginal(originalRequest, error);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition when accessing weak tracker reference twice

Medium Severity

The __weak static _tracker variable is accessed twice in the ternary expression: once for the nil check and once for the method call. Between these accesses, the tracker could be deallocated on another thread, causing the second access to return nil. This would result in the message being sent to nil, returning nil/NO instead of calling the original Core Data implementation. The old code avoided this by capturing the weak reference into a local strong variable first. This could cause fetch operations to silently return empty results or save operations to report false failures.

Additional Locations (1)

Fix in Cursor Fix in Web

}),
SentrySwizzleModeOncePerClassAndSuperclasses, (void *)fetchSelector);

SEL saveSelector = NSSelectorFromString(@"save:");
SentrySwizzleInstanceMethod(NSManagedObjectContext.class, saveSelector,
SentrySWReturnType(BOOL), SentrySWArguments(NSError * *error), SentrySWReplacement({
return _tracker != nil ? [_tracker managedObjectContext:self
save:error
originalImp:^BOOL(NSError **outError) {
return SentrySWCallOriginal(outError);
}]
: SentrySWCallOriginal(error);
}),
SentrySwizzleModeOncePerClassAndSuperclasses, (void *)saveSelector);
}

+ (void)unswizzle
{
#if SENTRY_TEST || SENTRY_TEST_CI
_tracker = nil;
swizzlingIsActive = FALSE;

// Unswizzling is only supported in test targets as it is considered unsafe for production.
SEL fetchSelector = NSSelectorFromString(@"executeFetchRequest:error:");
SentryUnswizzleInstanceMethod(
NSManagedObjectContext.class, fetchSelector, (void *)fetchSelector);

SEL saveSelector = NSSelectorFromString(@"save:");
SentryUnswizzleInstanceMethod(NSManagedObjectContext.class, saveSelector, (void *)saveSelector);
#endif // SENTRY_TEST || SENTRY_TEST_CI
}
#pragma clang diagnostic pop

#if SENTRY_TEST || SENTRY_TEST_CI
+ (BOOL)swizzlingActive
{
return swizzlingIsActive;
}
#endif
@end
41 changes: 0 additions & 41 deletions Sources/Sentry/SentryCoreDataTrackingIntegration.m

This file was deleted.

4 changes: 1 addition & 3 deletions Sources/Sentry/SentrySDKInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#import "SentryAutoBreadcrumbTrackingIntegration.h"
#import "SentryBreadcrumb.h"
#import "SentryClient+Private.h"
#import "SentryCoreDataTrackingIntegration.h"
#import "SentryCrash.h"
#import "SentryHub+Private.h"
#import "SentryInternalDefines.h"
Expand Down Expand Up @@ -503,8 +502,7 @@ + (void)endSession
[SentryAppStartTrackingIntegration class], [SentryPerformanceTrackingIntegration class],
[SentryUIEventTrackingIntegration class],
#endif // SENTRY_HAS_UIKIT
[SentryAutoBreadcrumbTrackingIntegration class], [SentryCoreDataTrackingIntegration class],
nil];
[SentryAutoBreadcrumbTrackingIntegration class], nil];

return defaultIntegrations;
}
Expand Down
22 changes: 0 additions & 22 deletions Sources/Sentry/include/SentryCoreDataSwizzling.h

This file was deleted.

17 changes: 17 additions & 0 deletions Sources/Sentry/include/SentryCoreDataSwizzlingHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import "SentryDefines.h"

NS_ASSUME_NONNULL_BEGIN

@interface SentryCoreDataSwizzlingHelper : NSObject

+ (void)swizzleWithTracker:(SENTRY_SWIFT_MIGRATION_ID(SentryCoreDataTracker))tracker;

+ (void)unswizzle;

#if SENTRY_TEST || SENTRY_TEST_CI
+ (BOOL)swizzlingActive;
#endif

@end

NS_ASSUME_NONNULL_END
4 changes: 2 additions & 2 deletions Sources/Sentry/include/SentryCoreDataTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

NS_ASSUME_NONNULL_BEGIN

@protocol SentryProcessInfoSource;
@class SentryDefaultThreadInspector;

@interface SentryCoreDataTracker : NSObject
SENTRY_NO_INIT

- (instancetype)initWithThreadInspector:(SentryDefaultThreadInspector *)threadInspector
processInfoWrapper:(id<SentryProcessInfoSource>)processInfoWrapper;
processInfoWrapper:
(SENTRY_SWIFT_MIGRATION_ID(id<SentryProcessInfoSource>))processInfoWrapper;

- (NSArray *)managedObjectContext:(NSManagedObjectContext *)context
executeFetchRequest:(NSFetchRequest *)request
Expand Down
9 changes: 0 additions & 9 deletions Sources/Sentry/include/SentryCoreDataTrackingIntegration.h

This file was deleted.

2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentryPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#import "SentryANRTrackerV2.h"
#import "SentryAsyncLog.h"
#import "SentryContinuousProfiler.h"
#import "SentryCoreDataSwizzlingHelper.h"
#import "SentryCoreDataTracker.h"
#import "SentryCrash.h"
#import "SentryCrashDebug.h"
#import "SentryCrashInstallation+Private.h"
Expand Down
1 change: 1 addition & 0 deletions Sources/Swift/Core/Integrations/Integrations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private struct AnyIntegration {
.init(SentryNetworkTrackingIntegration.self),
.init(SentryHangTrackerIntegrationObjC.self),
.init(SentryMetricsIntegration.self),
.init(SentryCoreDataTrackingIntegration.self),
.init(SentryFileIOTrackingIntegration.self)
])

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// swiftlint:disable missing_docs
@_implementationOnly import _SentryPrivate

final class SentryCoreDataSwizzling: NSObject {
func start(with tracker: Any) {
SentryCoreDataSwizzlingHelper.swizzle(withTracker: tracker)
}

func stop() {
SentryCoreDataSwizzlingHelper.unswizzle()
}
}
// swiftlint:enable missing_docs
Loading
Loading