Skip to content

Commit 5534fd9

Browse files
authored
Make RCTDeviceInfo constants atomic so getConstants can't read a released dictionary
## Summary: Since `invalidateCachedConstants` started rewriting `_constants` on resize, orientation and app activation, `getConstants` can crash with `EXC_BAD_ACCESS` on the JS thread. It's a sync TurboModule method, so it reads the ivar on the JS thread while the main thread may be swapping it out and releasing the old dictionary. If the release lands between the load and the retain, JS retains a freed object. This makes `constants` an atomic property and uses it at every read and write, so a reader always retains the dictionary before a writer can release it. It doesn't add a JS to main sync hop. Fixes #60014 ## Changelog: [IOS] [FIXED] - Fix a crash in `NativeDeviceInfo.getConstants` caused by a data race on the cached constants ## Test Plan: I don't have Xcode on this machine, so I couldn't build RNTester. I checked the pattern with a small ARC program: one thread reassigns a dictionary ivar in a loop and another reads it through a getter. With a plain ivar it segfaults on 3 of 3 runs. With an `atomic, copy` property it survives 3 of 3 runs, about 50M reads each. The issue author ran a similar patch in their reproducer (https://github.com/jeremy-robotsson/rn-deviceinfo-constants-race): 10/10 crashes with `-StormConstants` without it, 0/10 with it. Their patch also moved `didReceiveNewContentSizeMultiplier` to the main queue, which I left out because #57989 already covers it. The file is clang-format clean. Differential Revision: D132239323 Pull Request resolved: #60017
1 parent 2324866 commit 5534fd9

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎packages/react-native/React/CoreModules/RCTDeviceInfo.mm‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
using namespace facebook::react;
2323

2424
@interface RCTDeviceInfo () <NativeDeviceInfoSpec, RCTInitializing, RCTInvalidating>
25+
// Atomic because the sync -getConstants reads it on the JS thread while the main thread rewrites it.
26+
@property (atomic, copy) NSDictionary<NSString *, id> *constants;
2527
@end
2628

2729
@implementation RCTDeviceInfo {
@@ -31,7 +33,6 @@ @implementation RCTDeviceInfo {
3133
NSDictionary *_currentInterfaceDimensions;
3234
BOOL _isFullscreen;
3335
std::atomic<BOOL> _invalidated;
34-
NSDictionary *_constants;
3536

3637
__weak UIWindow *_applicationWindow;
3738
NSDictionary * (^_dimensionsProvider)(void);
@@ -122,7 +123,7 @@ - (void)initialize
122123
- (void)invalidateCachedConstants
123124
{
124125
RCTAssertMainQueue();
125-
_constants = @{
126+
self.constants = @{
126127
@"Dimensions" : [self _exportedDimensions],
127128
// Note:
128129
// This prop is deprecated and will be removed in a future release.
@@ -236,13 +237,13 @@ - (NSDictionary *)_exportedDimensions
236237

237238
- (NSDictionary<NSString *, id> *)getConstants
238239
{
239-
return _constants;
240+
return self.constants;
240241
}
241242

242243
- (void)didReceiveNewContentSizeMultiplier
243244
{
244245
[self invalidateCachedConstants];
245-
NSDictionary *nextInterfaceDimensions = _constants[@"Dimensions"];
246+
NSDictionary *nextInterfaceDimensions = self.constants[@"Dimensions"];
246247

247248
RCTModuleRegistry *moduleRegistry = _moduleRegistry;
248249
RCTExecuteOnMainQueue(^{
@@ -280,7 +281,7 @@ - (void)interfaceOrientationDidChange
280281
if ((isOrientationChanging || isResizingOrChangingToFullscreen) && RCTIsAppActive()) {
281282
#pragma clang diagnostic push
282283
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
283-
NSDictionary *nextInterfaceDimensions = _constants[@"Dimensions"];
284+
NSDictionary *nextInterfaceDimensions = self.constants[@"Dimensions"];
284285
[[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions"
285286
body:nextInterfaceDimensions];
286287
// We only want to track the current _currentInterfaceOrientation and _isFullscreen only
@@ -303,7 +304,7 @@ - (void)interfaceFrameDidChange
303304
- (void)_interfaceFrameDidChange
304305
{
305306
[self invalidateCachedConstants];
306-
NSDictionary *nextInterfaceDimensions = _constants[@"Dimensions"];
307+
NSDictionary *nextInterfaceDimensions = self.constants[@"Dimensions"];
307308

308309
// update and publish the even only when the app is in active state
309310
if (!([nextInterfaceDimensions isEqual:_currentInterfaceDimensions]) && RCTIsAppActive()) {

0 commit comments

Comments
 (0)