Skip to content

Commit d8fa120

Browse files
dratwasfacebook-github-bot
authored andcommitted
fix indexed RAM bundle (#24967)
Summary: Co-Authored: zamotany With React Native 0.59.8 the app keeps crashing with indexed RAM bundle on Android with the following error: ``` 2019-05-09 11:58:06.684 2793-2856/? E/AndroidRuntime: FATAL EXCEPTION: mqt_js Process: com.ramtestapp, PID: 2793 com.facebook.jni.CppException: getPropertyAsObject: property '__fbRequireBatchedBridge' is not an Object no stack at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29) at android.os.Looper.loop(Looper.java:193) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:232) at java.lang.Thread.run(Thread.java:764) ``` After investigation we found that when using any bundle, let it be non-ram, FIle RAM bundle or Index RAM bundle, the `CatalystInstanceImpl.java` is always using `loadScriptsFromAsset`, which is calling `CatalystInstanceImpl::jniLoadScriptFromAssets` in C++. This method when checking if bundle is a RAM bundle, uses `JniJSModulesUnbundle::isUnbundle` which only check for js-modules/UNBUNDLE - file generated when building File RAM bundle. There is no other logic to handle Indexed RAM bundle, so it figures that the bundle is not RAM, cause there is no js-modules/UNBUNDLE file and tries to load as regular bundle and fails. In this PR we added check if it is indexed RAM bundle in `jniLoadScriptFromAssets` and handle it if it is. ## Changelog [Android] [Fixed] fix indexed RAM bundle Solves #21282 Pull Request resolved: #24967 Differential Revision: D15575924 Pulled By: cpojer fbshipit-source-id: 5ea428e0b793edd8242243f39f933d1092b35260
1 parent b45d3b8 commit d8fa120

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
199199
sourceURL,
200200
loadSynchronously);
201201
return;
202+
} else if (Instance::isIndexedRAMBundle(&script)) {
203+
instance_->loadRAMBundleFromString(std::move(script), sourceURL);
202204
} else {
203205
instance_->loadScriptFromString(std::move(script), sourceURL, loadSynchronously);
204206
}

ReactCommon/cxxreact/Instance.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ bool Instance::isIndexedRAMBundle(const char *sourcePath) {
108108
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
109109
}
110110

111+
bool Instance::isIndexedRAMBundle(std::unique_ptr<const JSBigString>* script) {
112+
BundleHeader header;
113+
strncpy(reinterpret_cast<char *>(&header), script->get()->c_str(), sizeof(header));
114+
115+
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
116+
}
117+
118+
void Instance::loadRAMBundleFromString(std::unique_ptr<const JSBigString> script, const std::string& sourceURL) {
119+
auto bundle = folly::make_unique<JSIndexedRAMBundle>(std::move(script));
120+
auto startupScript = bundle->getStartupCode();
121+
auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle));
122+
loadRAMBundle(
123+
std::move(registry),
124+
std::move(startupScript),
125+
sourceURL,
126+
true);
127+
}
128+
111129
void Instance::loadRAMBundleFromFile(const std::string& sourcePath,
112130
const std::string& sourceURL,
113131
bool loadSynchronously) {

ReactCommon/cxxreact/Instance.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class RN_EXPORT Instance {
4747
void loadScriptFromString(std::unique_ptr<const JSBigString> string,
4848
std::string sourceURL, bool loadSynchronously);
4949
static bool isIndexedRAMBundle(const char *sourcePath);
50+
static bool isIndexedRAMBundle(std::unique_ptr<const JSBigString>* string);
51+
void loadRAMBundleFromString(std::unique_ptr<const JSBigString> script, const std::string& sourceURL);
5052
void loadRAMBundleFromFile(const std::string& sourcePath,
5153
const std::string& sourceURL,
5254
bool loadSynchronously);

ReactCommon/cxxreact/JSIndexedRAMBundle.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "JSIndexedRAMBundle.h"
77

88
#include <glog/logging.h>
9-
9+
#include <fstream>
10+
#include <sstream>
1011
#include <folly/Memory.h>
1112

1213
namespace facebook {
@@ -18,14 +19,30 @@ std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> JSIndexedRAMBundl
1819
};
1920
}
2021

21-
JSIndexedRAMBundle::JSIndexedRAMBundle(const char *sourcePath) :
22-
m_bundle (sourcePath, std::ios_base::in) {
22+
JSIndexedRAMBundle::JSIndexedRAMBundle(const char *sourcePath) {
23+
m_bundle = std::make_unique<std::ifstream>(sourcePath, std::ifstream::binary);
2324
if (!m_bundle) {
2425
throw std::ios_base::failure(
2526
folly::to<std::string>("Bundle ", sourcePath,
26-
"cannot be opened: ", m_bundle.rdstate()));
27+
"cannot be opened: ", m_bundle->rdstate()));
2728
}
29+
init();
30+
}
31+
32+
JSIndexedRAMBundle::JSIndexedRAMBundle(std::unique_ptr<const JSBigString> script) {
33+
// tmpStream is needed because m_bundle is std::istream type
34+
// which has no member 'write'
35+
std::unique_ptr<std::stringstream> tmpStream = std::make_unique<std::stringstream>();
36+
tmpStream->write(script->c_str(), script->size());
37+
m_bundle = std::move(tmpStream);
38+
if (!m_bundle) {
39+
throw std::ios_base::failure(
40+
folly::to<std::string>("Bundle from string cannot be opened: ", m_bundle->rdstate()));
41+
}
42+
init();
43+
}
2844

45+
void JSIndexedRAMBundle::init() {
2946
// read in magic header, number of entries, and length of the startup section
3047
uint32_t header[3];
3148
static_assert(
@@ -78,12 +95,12 @@ std::string JSIndexedRAMBundle::getModuleCode(const uint32_t id) const {
7895
}
7996

8097
void JSIndexedRAMBundle::readBundle(char *buffer, const std::streamsize bytes) const {
81-
if (!m_bundle.read(buffer, bytes)) {
82-
if (m_bundle.rdstate() & std::ios::eofbit) {
98+
if (!m_bundle->read(buffer, bytes)) {
99+
if (m_bundle->rdstate() & std::ios::eofbit) {
83100
throw std::ios_base::failure("Unexpected end of RAM Bundle file");
84101
}
85102
throw std::ios_base::failure(
86-
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle.rdstate()));
103+
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle->rdstate()));
87104
}
88105
}
89106

@@ -92,9 +109,9 @@ void JSIndexedRAMBundle::readBundle(
92109
const std::streamsize bytes,
93110
const std::ifstream::pos_type position) const {
94111

95-
if (!m_bundle.seekg(position)) {
112+
if (!m_bundle->seekg(position)) {
96113
throw std::ios_base::failure(
97-
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle.rdstate()));
114+
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle->rdstate()));
98115
}
99116
readBundle(buffer, bytes);
100117
}

ReactCommon/cxxreact/JSIndexedRAMBundle.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#pragma once
77

8-
#include <fstream>
8+
#include <istream>
99
#include <memory>
1010

1111
#include <cxxreact/JSBigString.h>
@@ -24,6 +24,7 @@ class RN_EXPORT JSIndexedRAMBundle : public JSModulesUnbundle {
2424

2525
// Throws std::runtime_error on failure.
2626
JSIndexedRAMBundle(const char *sourceURL);
27+
JSIndexedRAMBundle(std::unique_ptr<const JSBigString> script);
2728

2829
// Throws std::runtime_error on failure.
2930
std::unique_ptr<const JSBigString> getStartupCode();
@@ -51,14 +52,15 @@ class RN_EXPORT JSIndexedRAMBundle : public JSModulesUnbundle {
5152
}
5253
};
5354

55+
void init();
5456
std::string getModuleCode(const uint32_t id) const;
5557
void readBundle(char *buffer, const std::streamsize bytes) const;
5658
void readBundle(
5759
char *buffer, const
5860
std::streamsize bytes,
59-
const std::ifstream::pos_type position) const;
61+
const std::istream::pos_type position) const;
6062

61-
mutable std::ifstream m_bundle;
63+
mutable std::unique_ptr<std::istream> m_bundle;
6264
ModuleTable m_table;
6365
size_t m_baseOffset;
6466
std::unique_ptr<JSBigBufferString> m_startupCode;

0 commit comments

Comments
 (0)