Skip to content
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

fix: Add swizzling for NSFileManager.createFileAtPath #4634

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

philprime
Copy link
Contributor

@philprime philprime commented Dec 13, 2024

📜 Description

This PR fixes the automatic tracking of file.write spans when using NSFileManager. createFileAtPath.

The changes are split of from #4605.

💡 Motivation and Context

Before macOS 15, iOS 18 and tvOS 18 the implementation would use the already swizzled methods of NSData. As the internal implementation has changed, the NSFileManager must be swizzled directly.

See #4546 for full discussion.

💚 How did you test it?

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

codecov bot commented Dec 13, 2024

Codecov Report

Attention: Patch coverage is 97.05015% with 10 lines in your changes missing coverage. Please review.

Project coverage is 90.659%. Comparing base (0cb72fd) to head (6ed2cfb).

Files with missing lines Patch % Lines
...Performance/IO/SentryNSFileManagerSwizzlingTests.m 94.488% 7 Missing ⚠️
Sources/Sentry/SentrySwizzle.m 95.161% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #4634       +/-   ##
=============================================
+ Coverage   90.584%   90.659%   +0.074%     
=============================================
  Files          621       623        +2     
  Lines        71120     71441      +321     
  Branches     25306     26003      +697     
=============================================
+ Hits         64424     64768      +344     
+ Misses        6602      6576       -26     
- Partials        94        97        +3     
Files with missing lines Coverage Δ
Sources/Sentry/SentryFileIOTracker.m 95.483% <100.000%> (ø)
Sources/Sentry/SentryFileIOTrackingIntegration.m 100.000% <100.000%> (ø)
Sources/Sentry/SentryNSDataSwizzling.m 100.000% <100.000%> (ø)
Sources/Sentry/SentryNSFileManagerSwizzling.m 100.000% <100.000%> (ø)
...ources/Sentry/include/HybridPublic/SentrySwizzle.h 97.297% <100.000%> (+0.154%) ⬆️
Sources/Swift/SentryExperimentalOptions.swift 100.000% <100.000%> (ø)
Tests/SentryTests/Helper/SentrySwizzleTests.m 92.417% <100.000%> (+0.664%) ⬆️
...ions/Performance/IO/SentryFileIOTrackerTests.swift 99.401% <100.000%> (ø)
...ance/IO/SentryFileIOTrackingIntegrationObjCTests.m 96.178% <100.000%> (+1.768%) ⬆️
...ance/IO/SentryFileIOTrackingIntegrationTests.swift 93.023% <100.000%> (+1.901%) ⬆️
... and 2 more

... and 22 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0cb72fd...6ed2cfb. Read the comment docs.

Copy link

github-actions bot commented Dec 13, 2024

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1246.73 ms 1253.11 ms 6.38 ms
Size 22.32 KiB 762.79 KiB 740.48 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
b9a9ffd 1201.61 ms 1215.06 ms 13.45 ms
282cc99 1238.27 ms 1253.46 ms 15.19 ms
984eb2d 1220.62 ms 1235.24 ms 14.62 ms
4350d44 1228.75 ms 1246.75 ms 18.00 ms
279841c 1237.29 ms 1254.12 ms 16.83 ms
e145ca1 1207.19 ms 1230.67 ms 23.48 ms
9b9f2a0 1233.36 ms 1244.04 ms 10.68 ms
7b022df 1220.53 ms 1227.56 ms 7.03 ms
aa225e2 1218.00 ms 1236.45 ms 18.45 ms
9f0d9e0 1226.31 ms 1242.70 ms 16.40 ms

App size

Revision Plain With Sentry Diff
b9a9ffd 21.58 KiB 418.43 KiB 396.85 KiB
282cc99 22.85 KiB 414.09 KiB 391.24 KiB
984eb2d 20.76 KiB 425.77 KiB 405.01 KiB
4350d44 21.58 KiB 629.82 KiB 608.24 KiB
279841c 22.84 KiB 403.19 KiB 380.34 KiB
e145ca1 21.58 KiB 669.70 KiB 648.12 KiB
9b9f2a0 21.58 KiB 707.42 KiB 685.84 KiB
7b022df 22.85 KiB 412.95 KiB 390.10 KiB
aa225e2 21.58 KiB 694.47 KiB 672.89 KiB
9f0d9e0 21.58 KiB 424.28 KiB 402.70 KiB

Previous results on branch: philprime/nsfilemanager-swizzling

Startup times

Revision Plain With Sentry Diff
0274a46 1243.38 ms 1262.28 ms 18.89 ms
ded7094 1241.53 ms 1260.89 ms 19.36 ms
6285002 1232.50 ms 1246.02 ms 13.52 ms
b6e09ac 1241.63 ms 1263.66 ms 22.03 ms
c035d04 1234.16 ms 1250.73 ms 16.57 ms
ea7187f 1243.06 ms 1253.69 ms 10.63 ms
e474b38 1232.46 ms 1246.29 ms 13.83 ms
be0667c 1238.42 ms 1266.65 ms 28.24 ms

App size

Revision Plain With Sentry Diff
0274a46 22.30 KiB 761.98 KiB 739.68 KiB
ded7094 22.31 KiB 760.40 KiB 738.09 KiB
6285002 22.31 KiB 758.89 KiB 736.59 KiB
b6e09ac 22.31 KiB 758.58 KiB 736.28 KiB
c035d04 22.31 KiB 761.99 KiB 739.68 KiB
ea7187f 22.30 KiB 762.61 KiB 740.31 KiB
e474b38 22.31 KiB 760.40 KiB 738.10 KiB
be0667c 22.31 KiB 758.05 KiB 735.74 KiB

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

@philprime philprime marked this pull request as ready for review December 13, 2024 12:28
Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link
Contributor

@brustolin brustolin left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

Sources/Sentry/SentryNSFileManagerSwizzling.m Outdated Show resolved Hide resolved
Sources/Sentry/SentryNSFileManagerSwizzling.m Outdated Show resolved Hide resolved
Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m

@philprime
Copy link
Contributor Author

So right now after adding the unit tests for enabling and disabling the swizzling via the experimental feature flag, the tests in SentryFileIOTrackingIntegrationObjCTests.m are causing the tests in SentryNSFileManagerSwizzlingTests.swift to partially fail.

I currently believe we need to unswizzle methods in the uninstall of integrations, so I'll look into that to get this fixed.

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m
  • Sources/Sentry/SentrySwizzle.m

@philprime philprime mentioned this pull request Dec 19, 2024
8 tasks
@philipphofmann
Copy link
Member

So right now after adding the unit tests for enabling and disabling the swizzling via the experimental feature flag, the tests in SentryFileIOTrackingIntegrationObjCTests.m are causing the tests in SentryNSFileManagerSwizzlingTests.swift to partially fail.

I currently believe we need to unswizzle methods in the uninstall of integrations, so I'll look into that to get this fixed.

That sounds like side effects which you won't be able to solve with unswizzling. Let's have a chat about this, as this sounds like something that will bite us later.

@philprime
Copy link
Contributor Author

@philipphofmann I already narrowed the issue down to the swizzling. I went ahead and created a local Xcode scheme only running tests in these files:

Screenshot 2024-12-19 at 16 39 34

Without unswizzling, if I run the tests individually, they all pass. If I run all the tests, they fail. I added breakpoints and manually tested, and in test cases where swizzling should not happen, the methods were still swizzled from previous tests.

Maybe the whole unswizzling/uninstall should only be implemented in the tests, but that's probably better to be discussed in #4647

Copy link

🚨 Detected changes in high risk code 🚨

High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:

  • Sources/Sentry/SentryNSDataSwizzling.m
  • Sources/Sentry/SentryNSFileManagerSwizzling.m
  • Sources/Sentry/SentrySwizzle.m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants