Jay Civelli | ddcb249 | 2017-06-23 00:30:41 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 5 | #include "base/android/jni_array.h" |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 6 | #include "base/android/jni_string.h" |
[email protected] | 93c9f9b | 2014-02-10 16:19:22 | [diff] [blame] | 7 | #include "base/android/library_loader/library_loader_hooks.h" |
Eric Stevenson | c0926cb | 2019-06-19 15:03:16 | [diff] [blame] | 8 | #include "base/base_jni_headers/ChildProcessService_jni.h" |
Min Qin | f9dd030 | 2019-03-07 18:54:00 | [diff] [blame] | 9 | #include "base/debug/dump_without_crashing.h" |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 10 | #include "base/file_descriptor_store.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 613eef6 | 2012-11-09 23:46:54 | [diff] [blame] | 12 | #include "base/posix/global_descriptors.h" |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 13 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 14 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 15 | using base::android::JavaIntArrayToIntVector; |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 16 | using base::android::JavaParamRef; |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 17 | |
Jay Civelli | ddcb249 | 2017-06-23 00:30:41 | [diff] [blame] | 18 | namespace base { |
| 19 | namespace android { |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame] | 20 | |
Peter Kotwicz | 162143b1 | 2018-01-18 06:27:42 | [diff] [blame] | 21 | void JNI_ChildProcessService_RegisterFileDescriptors( |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 22 | JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 23 | const JavaParamRef<jobjectArray>& j_keys, |
| 24 | const JavaParamRef<jintArray>& j_ids, |
| 25 | const JavaParamRef<jintArray>& j_fds, |
| 26 | const JavaParamRef<jlongArray>& j_offsets, |
| 27 | const JavaParamRef<jlongArray>& j_sizes) { |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 28 | std::vector<absl::optional<std::string>> keys; |
Torne (Richard Coles) | f73cd9d | 2019-04-29 20:56:14 | [diff] [blame] | 29 | JavaObjectArrayReader<jstring> keys_array(j_keys); |
| 30 | keys.reserve(keys_array.size()); |
| 31 | for (auto str : keys_array) { |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 32 | absl::optional<std::string> key; |
Torne (Richard Coles) | f73cd9d | 2019-04-29 20:56:14 | [diff] [blame] | 33 | if (str) { |
Jay Civelli | a15e0a9 | 2017-06-20 22:49:16 | [diff] [blame] | 34 | key = base::android::ConvertJavaStringToUTF8(env, str); |
| 35 | } |
| 36 | keys.push_back(std::move(key)); |
| 37 | } |
| 38 | |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 39 | std::vector<int> ids; |
| 40 | base::android::JavaIntArrayToIntVector(env, j_ids, &ids); |
| 41 | std::vector<int> fds; |
| 42 | base::android::JavaIntArrayToIntVector(env, j_fds, &fds); |
| 43 | std::vector<int64_t> offsets; |
| 44 | base::android::JavaLongArrayToInt64Vector(env, j_offsets, &offsets); |
| 45 | std::vector<int64_t> sizes; |
| 46 | base::android::JavaLongArrayToInt64Vector(env, j_sizes, &sizes); |
| 47 | |
Jay Civelli | a15e0a9 | 2017-06-20 22:49:16 | [diff] [blame] | 48 | DCHECK_EQ(keys.size(), ids.size()); |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 49 | DCHECK_EQ(ids.size(), fds.size()); |
| 50 | DCHECK_EQ(fds.size(), offsets.size()); |
| 51 | DCHECK_EQ(offsets.size(), sizes.size()); |
| 52 | |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 53 | for (size_t i = 0; i < ids.size(); i++) { |
Peter Kasting | d554315 | 2021-06-21 14:26:47 | [diff] [blame^] | 54 | base::MemoryMappedFile::Region region = {offsets.at(i), |
| 55 | static_cast<size_t>(sizes.at(i))}; |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 56 | const absl::optional<std::string>& key = keys.at(i); |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 57 | int id = ids.at(i); |
| 58 | int fd = fds.at(i); |
Jay Civelli | a15e0a9 | 2017-06-20 22:49:16 | [diff] [blame] | 59 | if (key) { |
| 60 | base::FileDescriptorStore::GetInstance().Set(*key, base::ScopedFD(fd), |
| 61 | region); |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 62 | } else { |
| 63 | base::GlobalDescriptors::GetInstance()->Set(id, fd, region); |
| 64 | } |
| 65 | } |
agrieve | 0f918313 | 2015-06-03 13:41:53 | [diff] [blame] | 66 | } |
| 67 | |
Aiden Benner | c99e905 | 2018-11-30 20:50:50 | [diff] [blame] | 68 | void JNI_ChildProcessService_ExitChildProcess(JNIEnv* env) { |
Peter Kotwicz | 162143b1 | 2018-01-18 06:27:42 | [diff] [blame] | 69 | VLOG(0) << "ChildProcessService: Exiting child process."; |
[email protected] | 93c9f9b | 2014-02-10 16:19:22 | [diff] [blame] | 70 | base::android::LibraryLoaderExitHook(); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 71 | _exit(0); |
| 72 | } |
| 73 | |
Chris Mumford | 8f49e09 | 2019-05-09 20:02:18 | [diff] [blame] | 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."; |
Min Qin | f9dd030 | 2019-03-07 18:54:00 | [diff] [blame] | 79 | base::debug::DumpWithoutCrashing(); |
| 80 | } |
| 81 | |
Jay Civelli | ddcb249 | 2017-06-23 00:30:41 | [diff] [blame] | 82 | } // namespace android |
| 83 | } // namespace base |