-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: Add JavaScript wrappers to captured console to prevent crashes in React Native 0.75 #6520
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
Conversation
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
tomekzaw
reviewed
Sep 17, 2024
tomekzaw
approved these changes
Sep 18, 2024
bartlomiejbloniarz
pushed a commit
that referenced
this pull request
Sep 19, 2024
…n React Native 0.75 (#6520) ## Summary Currently there seems to be a bug in the JSI layer which causes a crash when we try to copy some of the console methods, i.e. `clear` or `dirxml`. The crash happens only in React Native 0.75. It's not reproducible in neither 0.76 nor 0.74. It also happens only in the configuration of a debug app and production bundle. I haven't yet discovered what exactly causes the crash. It's tied to the console methods sometimes being `HostFunction`s. Therefore, as a workaround we don't copy the methods as they are in the original console object, we copy JavaScript wrappers instead. The fix should't be treated as a complete solution. Same crash could also happen on copying of some other HostFunctions. ## Details 1. In Hermes runtime there's a global `console` object. Reanimated sends methods of this object via JSI to store "references" to them. 2. Sometimes, some of the console methods turn out to be HostFunctions. In this case, we obtain the underlying `std::function` from the `jsi::Function` with `getHostFunction`. 3. We copy the obtained `std::function` and store that copy. This is where the crash happens, on the copy assignment (memory - bad access). Certain conditions have to be met for this flow to occur and for the crash to happen: - It's present only in React Native 0.75.x. I've tried both 0.74 and RC of 0.76 and everything is okay there. - I've been troubleshooting for iOS but allegedly it's platform agnostic. Also, the crash appears on both Paper and Fabric. - When I mentioned that "some of the console methods turn out to be HostFunctions" - this only happens in RN 0.75. In both 0.74 and RC of 0.76 the methods we copy are never HostFunctions. - It's present in configuration of a debug build of the app with a production bundle. In cases of debug&debug or prod&prod there's no crash. - Methods which causes the crash are the more exotic ones - `clear`, `dirxml` etc. They all are HostFunctions when crash conditions are present. Common methods like `log`, `warn` etc. are standard JS functions and can be copied with no issues. I rummaged through Hermes repo to find where i.e. `clear` method originates. I found this file which from I understand creates the "first" console object: https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/tools/node-hermes/nodelib/wrappers/internal/console/constructor.js#L470 However, nothing in here suggests that any method should be a HostObject. Therefore I assume it's either replaced somewhere else by Hermes or React Native, i.e. - When Chrome Debug Protocol is connected into the runtime https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/API/hermes/inspector/chrome/CDPHandler.cpp#L2372 - Initialization of Hermes, although I assume, `console` object is injected into the global scope for the first time here: https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/tools/node-hermes/node-hermes.cpp#L306 On the React Native side, I found the place where they replace the console with some polyfills in debug bundles. Since the bundle is in production mode, this doesn't happen: https://github.com/facebook/react-native/blob/949296571b36080d4ebe0bc8fac9ef3907367475/packages/polyfills/console.js#L596 ## Test plan Apply the following patch to Metro to be able to juggle between bundle types: ```diff diff --git a/src/lib/splitBundleOptions.js b/src/lib/splitBundleOptions.js index 3f35705877cd2d91fc032ea6a75a12b449155ac7..f78656a8c5bdeff4d3915cf2a7262613b6f447ba 100644 --- a/src/lib/splitBundleOptions.js +++ b/src/lib/splitBundleOptions.js @@ -1,15 +1,18 @@ "use strict"; +const overrideBundleType = true; +const isDevBundle = false; + function splitBundleOptions(options) { return { entryFile: options.entryFile, resolverOptions: { customResolverOptions: options.customResolverOptions, - dev: options.dev, + dev: overrideBundleType ? isDevBundle : options.dev, }, transformOptions: { customTransformOptions: options.customTransformOptions, - dev: options.dev, + dev: overrideBundleType ? isDevBundle : options.dev, hot: options.hot, minify: options.minify, platform: options.platform, ``` ## Notes Sorry for the wall of text but this crash is way too annoying to document and screenshot everything thoroughly 🤷 Fixes - #6415
r0h0gg6
pushed a commit
to r0h0gg6/react-native-reanimated
that referenced
this pull request
Jul 28, 2025
…n React Native 0.75 (software-mansion#6520) ## Summary Currently there seems to be a bug in the JSI layer which causes a crash when we try to copy some of the console methods, i.e. `clear` or `dirxml`. The crash happens only in React Native 0.75. It's not reproducible in neither 0.76 nor 0.74. It also happens only in the configuration of a debug app and production bundle. I haven't yet discovered what exactly causes the crash. It's tied to the console methods sometimes being `HostFunction`s. Therefore, as a workaround we don't copy the methods as they are in the original console object, we copy JavaScript wrappers instead. The fix should't be treated as a complete solution. Same crash could also happen on copying of some other HostFunctions. ## Details 1. In Hermes runtime there's a global `console` object. Reanimated sends methods of this object via JSI to store "references" to them. 2. Sometimes, some of the console methods turn out to be HostFunctions. In this case, we obtain the underlying `std::function` from the `jsi::Function` with `getHostFunction`. 3. We copy the obtained `std::function` and store that copy. This is where the crash happens, on the copy assignment (memory - bad access). Certain conditions have to be met for this flow to occur and for the crash to happen: - It's present only in React Native 0.75.x. I've tried both 0.74 and RC of 0.76 and everything is okay there. - I've been troubleshooting for iOS but allegedly it's platform agnostic. Also, the crash appears on both Paper and Fabric. - When I mentioned that "some of the console methods turn out to be HostFunctions" - this only happens in RN 0.75. In both 0.74 and RC of 0.76 the methods we copy are never HostFunctions. - It's present in configuration of a debug build of the app with a production bundle. In cases of debug&debug or prod&prod there's no crash. - Methods which causes the crash are the more exotic ones - `clear`, `dirxml` etc. They all are HostFunctions when crash conditions are present. Common methods like `log`, `warn` etc. are standard JS functions and can be copied with no issues. I rummaged through Hermes repo to find where i.e. `clear` method originates. I found this file which from I understand creates the "first" console object: https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/tools/node-hermes/nodelib/wrappers/internal/console/constructor.js#L470 However, nothing in here suggests that any method should be a HostObject. Therefore I assume it's either replaced somewhere else by Hermes or React Native, i.e. - When Chrome Debug Protocol is connected into the runtime https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/API/hermes/inspector/chrome/CDPHandler.cpp#L2372 - Initialization of Hermes, although I assume, `console` object is injected into the global scope for the first time here: https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/tools/node-hermes/node-hermes.cpp#L306 On the React Native side, I found the place where they replace the console with some polyfills in debug bundles. Since the bundle is in production mode, this doesn't happen: https://github.com/facebook/react-native/blob/949296571b36080d4ebe0bc8fac9ef3907367475/packages/polyfills/console.js#L596 ## Test plan Apply the following patch to Metro to be able to juggle between bundle types: ```diff diff --git a/src/lib/splitBundleOptions.js b/src/lib/splitBundleOptions.js index 3f35705877cd2d91fc032ea6a75a12b449155ac7..f78656a8c5bdeff4d3915cf2a7262613b6f447ba 100644 --- a/src/lib/splitBundleOptions.js +++ b/src/lib/splitBundleOptions.js @@ -1,15 +1,18 @@ "use strict"; +const overrideBundleType = true; +const isDevBundle = false; + function splitBundleOptions(options) { return { entryFile: options.entryFile, resolverOptions: { customResolverOptions: options.customResolverOptions, - dev: options.dev, + dev: overrideBundleType ? isDevBundle : options.dev, }, transformOptions: { customTransformOptions: options.customTransformOptions, - dev: options.dev, + dev: overrideBundleType ? isDevBundle : options.dev, hot: options.hot, minify: options.minify, platform: options.platform, ``` ## Notes Sorry for the wall of text but this crash is way too annoying to document and screenshot everything thoroughly 🤷 Fixes - software-mansion#6415
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Currently there seems to be a bug in the JSI layer which causes a crash when we try to copy some of the console methods, i.e.
clearordirxml.The crash happens only in React Native 0.75. It's not reproducible in neither 0.76 nor 0.74. It also happens only in the configuration of a debug app and production bundle.
I haven't yet discovered what exactly causes the crash. It's tied to the console methods sometimes being
HostFunctions. Therefore, as a workaround we don't copy the methods as they are in the original console object, we copy JavaScript wrappers instead.The fix should't be treated as a complete solution. Same crash could also happen on copying of some other HostFunctions.
Details
consoleobject. Reanimated sends methods of this object via JSI to store "references" to them.std::functionfrom thejsi::FunctionwithgetHostFunction.std::functionand store that copy. This is where the crash happens, on the copy assignment (memory - bad access).Certain conditions have to be met for this flow to occur and for the crash to happen:
clear,dirxmletc. They all are HostFunctions when crash conditions are present. Common methods likelog,warnetc. are standard JS functions and can be copied with no issues.I rummaged through Hermes repo to find where i.e.
clearmethod originates. I found this file which from I understand creates the "first" console object:https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/tools/node-hermes/nodelib/wrappers/internal/console/constructor.js#L470
However, nothing in here suggests that any method should be a HostObject. Therefore I assume it's either replaced somewhere else by Hermes or React Native, i.e.
https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/API/hermes/inspector/chrome/CDPHandler.cpp#L2372
consoleobject is injected into the global scope for the first time here:https://github.com/facebook/hermes/blob/649ceaa4c8b2ed331251152ccb7fcee07f19fc4c/tools/node-hermes/node-hermes.cpp#L306
On the React Native side, I found the place where they replace the console with some polyfills in debug bundles. Since the bundle is in production mode, this doesn't happen:
https://github.com/facebook/react-native/blob/949296571b36080d4ebe0bc8fac9ef3907367475/packages/polyfills/console.js#L596
Test plan
Apply the following patch to Metro to be able to juggle between bundle types:
Notes
Sorry for the wall of text but this crash is way too annoying to document and screenshot everything thoroughly 🤷
Fixes