| 6 | #include "base/android/jni_string.h" |
| 7 | #include "base/android/library_loader/library_loader_hooks.h" |
| 8 | #include "base/base_jni_headers/ChildProcessService_jni.h" |
| 9 | #include "base/debug/dump_without_crashing.h" |
| 10 | #include "base/file_descriptor_store.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/optional.h" |
| 14 | #include "base/posix/global_descriptors.h" |
| 15 |
| 16 | using base::android::JavaIntArrayToIntVector; |
| 17 | using base::android::JavaParamRef; |
| 18 |
| 19 | namespace base { |
| 20 | namespace android { |
| 21 |
| 22 | void JNI_ChildProcessService_RegisterFileDescriptors( |
| 23 | JNIEnv* env, |
| 24 | const JavaParamRef<jobjectArray>& j_keys, |
| 25 | const JavaParamRef<jintArray>& j_ids, |
| 26 | const JavaParamRef<jintArray>& j_fds, |
| 27 | const JavaParamRef<jlongArray>& j_offsets, |
| 28 | const JavaParamRef<jlongArray>& j_sizes) { |
| 29 | std::vector<base::Optional<std::string>> keys; |
| 30 | JavaObjectArrayReader<jstring> keys_array(j_keys); |
| 31 | keys.reserve(keys_array.size()); |
| 32 | for (auto str : keys_array) { |
| 33 | base::Optional<std::string> key; |
| 34 | if (str) { |
| 35 | key = base::android::ConvertJavaStringToUTF8(env, str); |
| 36 | } |
| 37 | keys.push_back(std::move(key)); |
| 38 | } |
| 39 |
| 40 | std::vector<int> ids; |
| 41 | base::android::JavaIntArrayToIntVector(env, j_ids, &ids); |
| 42 | std::vector<int> fds; |
| 43 | base::android::JavaIntArrayToIntVector(env, j_fds, &fds); |
| 44 | std::vector<int64_t> offsets; |
| 45 | base::android::JavaLongArrayToInt64Vector(env, j_offsets, &offsets); |
| 46 | std::vector<int64_t> sizes; |
| 47 | base::android::JavaLongArrayToInt64Vector(env, j_sizes, &sizes); |
| 48 |
| 49 | DCHECK_EQ(keys.size(), ids.size()); |
| 50 | DCHECK_EQ(ids.size(), fds.size()); |
| 51 | DCHECK_EQ(fds.size(), offsets.size()); |
| 52 | DCHECK_EQ(offsets.size(), sizes.size()); |
| 53 |
| 54 | for (size_t i = 0; i < ids.size(); i++) { |
| 55 | base::MemoryMappedFile::Region region = {offsets.at(i), sizes.at(i)}; |
| 56 | const base::Optional<std::string>& key = keys.at(i); |
| 57 | int id = ids.at(i); |
| 58 | int fd = fds.at(i); |
| 59 | if (key) { |
| 60 | base::FileDescriptorStore::GetInstance().Set(*key, base::ScopedFD(fd), |
| 61 | region); |
| 62 | } else { |
| 63 | base::GlobalDescriptors::GetInstance()->Set(id, fd, region); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 |
| 68 | void JNI_ChildProcessService_ExitChildProcess(JNIEnv* env) { |
| 69 | VLOG(0) << "ChildProcessService: Exiting child process."; |
| 70 | base::android::LibraryLoaderExitHook(); |
| 71 | _exit(0); |
| 72 | } |
| 73 |
| 74 | // Make sure this isn't inlined so it shows up in stack traces. |
| 75 | // the function body unique by adding a log line, so it doesn't get merged |
| 76 | // with other functions by link time optimizations (ICF). |
| 77 | NOINLINE void JNI_ChildProcessService_DumpProcessStack(JNIEnv* env) { |
| 78 | LOG(ERROR) << "Dumping as requested."; |
| 79 | base::debug::DumpWithoutCrashing(); |
| 80 | } |
| 81 |
| 82 | } // namespace android |
| 83 | } // namespace base |
| 84 |