Repository navigation
Fix dangling const-ref JSI args in bridging - #60219
Open
jkasprzyk17 wants to merge 1 commit into
Open
jkasprzyk17 wants to merge 1 commit into
jkasprzyk17 wants to merge 1 commit into
Conversation
bridging::fromJs<const jsi::Object &> (and jsi::Function / jsi::Value) returned a reference to a temporary created inside fromJs, so C++ TurboModule methods and bridged std::functions taking JSI values by const reference read a destroyed value. Return the converted value by value instead; the caller's temporary lives until the end of the call expression. The set of supported conversions is unchanged. Fixes react#60212
This branch has not been deployed
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
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
Fixes #60212.
bridging::callFromJs(C++ TurboModule methods) and bridgedstd::functions convert each argument withbridging::fromJs<ArgT>. When a parameter isconst jsi::Object &,const jsi::Function &orconst jsi::Value &, the JSI overload offromJsinBase.hreturned a reference to a temporary created insidefromJs. The temporary was destroyed asfromJsreturned, so the method read a destroyed value. Clang warns about it with-Wreturn-stack-address, and AddressSanitizer reports stack-use-after-return.This overload now returns the converted value instead of a reference. The temporary then lives in the caller until the end of the call expression, which covers the method call. The non-JSI overloads already behave this way. SFINAE still checks
static_cast<ReturnT>, so the set of supported conversions is unchanged. The C++ API snapshots are updated for the new return type.Changelog:
[General] [Fixed] - Fix C++ TurboModule methods and bridged functions that take
jsi::Object,jsi::Functionorjsi::Valueby const reference reading a destroyed argumentTest Plan
Added
BridgingTest.jsiReferenceArgumentCallFromJsTest(ClassTest.cpp). It calls methods takingconst jsi::Object&,const jsi::Function&andconst jsi::Value&with temporary and named arguments, reads each argument, and checks at compile time thatfromJs<const jsi::X&>returns a value. ExtendedBridgingTest.functionTestwith a bridged lambda taking(const jsi::Object&, const jsi::Value&).I ran the bridging tests (
BridgingTest.cpp,ClassTest.cpp) with a standalone CMake/gtest build on macOS arm64 (Apple clang), against a prebuilt Hermes, with-fsanitize=address -fsanitize-address-use-after-return=always:[ PASSED ] 64 tests.No-Wreturn-stack-addresswarnings.Base.h: 7-Wreturn-stack-addresswarnings atBase.h:70.jsiReferenceArgumentCallFromJsTest(before the compile-time checks were added) aborts withAddressSanitizer: stack-use-after-returninTestClass::isObject, called fromcallFromJs. With the compile-time checks, the test file no longer compiles (3static_assertfailures).Other checks:
yarn format-check-cpp: passes.yarn cxx-api-validate(Doxygen 1.16.1, as in CI): all snapshots match afteryarn cxx-api-build.Not run: the repo's own test target for these tests, and Android/iOS app builds.