Repository navigation
Commit 5534fd9
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: #600171 parent 2324866 commit 5534fd9
1 file changed
Lines changed: 7 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
| |||
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | | - | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
125 | | - | |
| 126 | + | |
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| |||
236 | 237 | | |
237 | 238 | | |
238 | 239 | | |
239 | | - | |
| 240 | + | |
240 | 241 | | |
241 | 242 | | |
242 | 243 | | |
243 | 244 | | |
244 | 245 | | |
245 | | - | |
| 246 | + | |
246 | 247 | | |
247 | 248 | | |
248 | 249 | | |
| |||
280 | 281 | | |
281 | 282 | | |
282 | 283 | | |
283 | | - | |
| 284 | + | |
284 | 285 | | |
285 | 286 | | |
286 | 287 | | |
| |||
303 | 304 | | |
304 | 305 | | |
305 | 306 | | |
306 | | - | |
| 307 | + | |
307 | 308 | | |
308 | 309 | | |
309 | 310 | | |
| |||
0 commit comments