-
-
Notifications
You must be signed in to change notification settings - Fork 408
fix: catch CoreAnimation exceptions in redaction #8537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b87611f
fix: catch CoreAnimation exceptions in redaction
philprime 4f2f1e2
docs: add changelog for #8537
philprime 1eb0d30
Merge branch 'main' into philprime/catch-animation-exceptions-7810
philprime 301f70e
Merge branch 'main' into philprime/catch-animation-exceptions-7810
philprime a4336e6
Merge remote-tracking branch 'origin/main' into philprime/catch-animaβ¦
philprime 3e7d9a2
fix: guard presentation layer and overmask on layer crash
philprime 90ad160
Merge remote-tracking branch 'origin/main' into philprime/catch-animaβ¦
philprime 04589d1
Merge remote-tracking branch 'origin/main' into philprime/catch-animaβ¦
philprime abc189e
fix: Log caught Objective-C exceptions
philprime 394c8b2
docs: Link replay exception troubleshooting
philprime d4c3607
fix: Narrow replay exception handling
philprime db6e799
docs: Link presentation fallback guidance
philprime 58b66b1
fix: Preserve replay unmask fallback
philprime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #import "SentryObjCExceptionHelper.h" | ||
|
|
||
| @implementation SentryObjCExceptionHelper | ||
|
|
||
| + (BOOL)tryBlock:(NS_NOESCAPE void (^)(void))block | ||
| { | ||
| @try { | ||
| block(); | ||
| return YES; | ||
| } @catch (NSException *exception) { | ||
|
philprime marked this conversation as resolved.
|
||
| return NO; | ||
| } | ||
| } | ||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #import <Foundation/Foundation.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /// Helper to run a block inside an Objective-C `@try`/`@catch`. | ||
| /// | ||
| /// Swift cannot catch Objective-C exceptions. Some system frameworks (notably Core Animation) | ||
| /// raise `NSException`s from code paths the SDK has to touch β for example resolving a layer's | ||
| /// presentation layer while a malformed `CABasicAnimation` is running raises | ||
| /// `-[NSConcreteValue doubleValue]: unrecognized selector`. When such an exception unwinds through | ||
| /// Swift frames it force-kills the host app. This helper lets Swift callers run the risky access | ||
| /// inside an Objective-C exception handler so they can degrade gracefully instead of crashing. | ||
| /// | ||
| /// See https://github.com/getsentry/sentry-cocoa/issues/7810 for context. | ||
| @interface SentryObjCExceptionHelper : NSObject | ||
|
|
||
| /// Executes `block` inside an Objective-C `@try`/`@catch`. | ||
| /// | ||
| /// - Parameter block: The block to execute. It is run synchronously before this method returns. | ||
| /// - Returns: `YES` if `block` completed without raising an `NSException`, `NO` if an exception | ||
| /// was caught and swallowed. | ||
| + (BOOL)tryBlock:(NS_NOESCAPE void (^)(void))block NS_SWIFT_NAME(tryBlock(_:)); | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
Tests/SentryTests/ViewCapture/SentryUIRedactBuilderTests+LayerTraversalCrash.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #if os(iOS) && !targetEnvironment(macCatalyst) | ||
| @_spi(Private) @testable import Sentry | ||
| import Foundation | ||
| import SentryTestUtils | ||
| import UIKit | ||
| import XCTest | ||
|
|
||
| /// Reproduction for https://github.com/getsentry/sentry-cocoa/issues/7810 | ||
| /// | ||
| /// Session Replay crashes with `-[NSConcreteValue doubleValue]: unrecognized selector` | ||
| /// inside `SentryUIRedactBuilder.mapRedactRegion`. The crash originates from Core Animation | ||
| /// while `mapRedactRegion` walks the layer tree via `layer.sublayers`: resolving the | ||
| /// presentation layer of a layer that has an active `CABasicAnimation` whose value is a | ||
| /// boxed struct (`NSConcreteValue` wrapping `CGRect`/`CGPoint`/`CATransform3D`) makes Core | ||
| /// Animation send `doubleValue` to a value that does not respond to it, raising an | ||
| /// `NSInvalidArgumentException`. | ||
| /// | ||
| /// The exception unwinds through the Swift frames of `mapRedactRegion` β which cannot catch | ||
| /// Objective-C exceptions β and force-kills the app. | ||
| /// | ||
| /// These tests simulate that failure deterministically by overriding `CALayer.sublayers` | ||
| /// to raise the exact `NSInvalidArgumentException` at the crash site. | ||
| /// | ||
| /// See `SentryUIRedactBuilderTests.swift` for more information on how to print the internal | ||
| /// view hierarchy of a view. | ||
| class SentryUIRedactBuilderTests_LayerTraversalCrash: SentryUIRedactBuilderTests { // swiftlint:disable:this type_name | ||
|
|
||
| /// A `CALayer` whose `sublayers` getter raises the same `NSInvalidArgumentException` | ||
| /// that Core Animation raises when interpolating a struct-valued animation as a scalar. | ||
| private final class ThrowingSublayersLayer: CALayer { | ||
| override var sublayers: [CALayer]? { | ||
| get { | ||
| NSException( | ||
| name: .invalidArgumentException, | ||
| reason: "-[NSConcreteValue doubleValue]: unrecognized selector sent to instance 0x12e68f150", | ||
| userInfo: nil | ||
| ).raise() | ||
| return nil | ||
| } | ||
| set { | ||
| // No-op: this layer never exposes sublayers, it only crashes on read. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A plain `UIView` backed by ``ThrowingSublayersLayer`` so that traversing its layer | ||
| /// subtree reproduces the crash. It intentionally is not text/image content, so it is | ||
| /// neither redacted nor ignored on its own merits. | ||
| private final class ThrowingSublayersView: UIView { | ||
| override class var layerClass: AnyClass { ThrowingSublayersLayer.self } | ||
| } | ||
|
|
||
| private func getSut(maskAllText: Bool, maskAllImages: Bool) -> SentryUIRedactBuilder { | ||
| return SentryUIRedactBuilder(options: TestRedactOptions( | ||
| maskAllText: maskAllText, | ||
| maskAllImages: maskAllImages | ||
| )) | ||
| } | ||
|
|
||
| func testRedactRegionsFor_whenSublayersAccessThrows_shouldNotCrash() throws { | ||
| // -- Arrange -- | ||
| let rootView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) | ||
|
|
||
| // A label that must still be redacted even though a sibling subtree crashes. | ||
| let label = UILabel(frame: CGRect(x: 10, y: 10, width: 80, height: 20)) | ||
| label.text = "Secret Text" | ||
| label.textColor = .purple | ||
| rootView.addSubview(label) | ||
|
|
||
| // A view whose layer raises when its sublayers are accessed, mimicking the | ||
| // Core Animation crash during a running struct-valued animation. | ||
| let throwingView = ThrowingSublayersView(frame: CGRect(x: 0, y: 40, width: 100, height: 40)) | ||
| rootView.addSubview(throwingView) | ||
|
|
||
| // View Hierarchy: | ||
| // --------------- | ||
| // <UIView: 0x...; frame = (0 0; 100 100); layer = <CALayer: 0x...>> | ||
| // | <UILabel: 0x...; frame = (10 10; 80 20); layer = <_UILabelLayer: 0x...>> | ||
| // | <ThrowingSublayersView: 0x...; frame = (0 40; 100 40); layer = <ThrowingSublayersLayer: 0x...>> | ||
|
|
||
| // -- Act -- | ||
| let sut = getSut(maskAllText: true, maskAllImages: true) | ||
| let result = sut.redactRegionsFor(view: rootView) | ||
|
|
||
| // -- Assert -- | ||
| // Traversal of the throwing subtree must be skipped gracefully instead of crashing, | ||
| // and the sibling label must still be redacted. | ||
| let labelRegions = result.filter { $0.type == .redact && $0.color == UIColor.purple } | ||
| XCTAssertEqual(labelRegions.count, 1, "Sibling label should still be redacted despite the crashing subtree") | ||
|
|
||
| let labelRegion = try XCTUnwrap(labelRegions.first) | ||
| XCTAssertEqual(labelRegion.size, CGSize(width: 80, height: 20)) | ||
| XCTAssertEqual(labelRegion.transform, CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 10, ty: 10)) | ||
| } | ||
|
|
||
| func testRedactRegionsFor_whenRootSublayersAccessThrows_shouldNotCrash() { | ||
| // -- Arrange -- | ||
| // The root view itself is backed by a layer whose sublayers access throws. | ||
| let rootView = ThrowingSublayersView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) | ||
|
|
||
| // -- Act -- | ||
| let sut = getSut(maskAllText: true, maskAllImages: true) | ||
| let result = sut.redactRegionsFor(view: rootView) | ||
|
|
||
| // -- Assert -- | ||
| // Should not crash. No regions can be collected because traversal stops at the root. | ||
| XCTAssertEqual(result.count, 0) | ||
| } | ||
| } | ||
|
|
||
| #endif // os(iOS) && !targetEnvironment(macCatalyst) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.