blob: e86e072d26873716491d674bc7d81b04d2728732 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2017 The Chromium Authors
[email protected]5b6f0692012-06-12 05:00:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]30935362012-06-28 21:26:235#include "base/android/jni_array.h"
jcivellidad0cef2017-02-16 18:38:596#include "base/android/jni_string.h"
[email protected]93c9f9b2014-02-10 16:19:227#include "base/android/library_loader/library_loader_hooks.h"
Min Qinf9dd0302019-03-07 18:54:008#include "base/debug/dump_without_crashing.h"
jcivellidad0cef2017-02-16 18:38:599#include "base/file_descriptor_store.h"
[email protected]5b6f0692012-06-12 05:00:0510#include "base/logging.h"
[email protected]613eef62012-11-09 23:46:5411#include "base/posix/global_descriptors.h"
Andrew Grieve1c7dbda2023-05-26 15:34:5512#include "base/process_launcher_jni/ChildProcessService_jni.h"
Anton Bikineev7dd58ad2021-05-18 01:01:3913#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]5b6f0692012-06-12 05:00:0514
[email protected]30935362012-06-28 21:26:2315using base::android::JavaIntArrayToIntVector;
torne86560112016-08-04 15:59:0416using base::android::JavaParamRef;
[email protected]5b6f0692012-06-12 05:00:0517
Jay Civelliddcb2492017-06-23 00:30:4118namespace base {
19namespace android {
[email protected]ccf558a2013-03-07 03:58:1820
Peter Kotwicz162143b12018-01-18 06:27:4221void JNI_ChildProcessService_RegisterFileDescriptors(
Daniel Bratell7aacf952017-11-21 17:51:2522 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:2523 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 Bikineev7dd58ad2021-05-18 01:01:3928 std::vector<absl::optional<std::string>> keys;
Torne (Richard Coles)f73cd9d2019-04-29 20:56:1429 JavaObjectArrayReader<jstring> keys_array(j_keys);
Peter Kasting217e9972022-07-07 17:48:4930 keys.reserve(checked_cast<size_t>(keys_array.size()));
Torne (Richard Coles)f73cd9d2019-04-29 20:56:1431 for (auto str : keys_array) {
Anton Bikineev7dd58ad2021-05-18 01:01:3932 absl::optional<std::string> key;
Torne (Richard Coles)f73cd9d2019-04-29 20:56:1433 if (str) {
Jay Civellia15e0a92017-06-20 22:49:1634 key = base::android::ConvertJavaStringToUTF8(env, str);
35 }
36 keys.push_back(std::move(key));
37 }
38
jcivellidad0cef2017-02-16 18:38:5939 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 Civellia15e0a92017-06-20 22:49:1648 DCHECK_EQ(keys.size(), ids.size());
jcivellidad0cef2017-02-16 18:38:5949 DCHECK_EQ(ids.size(), fds.size());
50 DCHECK_EQ(fds.size(), offsets.size());
51 DCHECK_EQ(offsets.size(), sizes.size());
52
jcivellidad0cef2017-02-16 18:38:5953 for (size_t i = 0; i < ids.size(); i++) {
Peter Kastingd5543152021-06-21 14:26:4754 base::MemoryMappedFile::Region region = {offsets.at(i),
55 static_cast<size_t>(sizes.at(i))};
Anton Bikineev7dd58ad2021-05-18 01:01:3956 const absl::optional<std::string>& key = keys.at(i);
Peter Kasting217e9972022-07-07 17:48:4957 const auto id = static_cast<GlobalDescriptors::Key>(ids.at(i));
jcivellidad0cef2017-02-16 18:38:5958 int fd = fds.at(i);
Jay Civellia15e0a92017-06-20 22:49:1659 if (key) {
60 base::FileDescriptorStore::GetInstance().Set(*key, base::ScopedFD(fd),
61 region);
jcivellidad0cef2017-02-16 18:38:5962 } else {
63 base::GlobalDescriptors::GetInstance()->Set(id, fd, region);
64 }
65 }
agrieve0f9183132015-06-03 13:41:5366}
67
Aiden Bennerc99e9052018-11-30 20:50:5068void JNI_ChildProcessService_ExitChildProcess(JNIEnv* env) {
Peter Kotwicz162143b12018-01-18 06:27:4269 VLOG(0) << "ChildProcessService: Exiting child process.";
[email protected]93c9f9b2014-02-10 16:19:2270 base::android::LibraryLoaderExitHook();
[email protected]5b6f0692012-06-12 05:00:0571 _exit(0);
72}
73
Chris Mumford8f49e092019-05-09 20:02:1874// 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).
77NOINLINE void JNI_ChildProcessService_DumpProcessStack(JNIEnv* env) {
78 LOG(ERROR) << "Dumping as requested.";
Min Qinf9dd0302019-03-07 18:54:0079 base::debug::DumpWithoutCrashing();
80}
81
Jay Civelliddcb2492017-06-23 00:30:4182} // namespace android
83} // namespace base