xref: /aosp_15_r20/external/cronet/base/android/child_process_service.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <optional>
6 
7 #include "base/android/jni_array.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/library_loader/library_loader_hooks.h"
10 #include "base/debug/dump_without_crashing.h"
11 #include "base/file_descriptor_store.h"
12 #include "base/logging.h"
13 #include "base/posix/global_descriptors.h"
14 #include "base/process_launcher_jni/ChildProcessService_jni.h"
15 
16 using base::android::JavaIntArrayToIntVector;
17 using base::android::JavaParamRef;
18 
19 namespace base {
20 namespace android {
21 
JNI_ChildProcessService_RegisterFileDescriptors(JNIEnv * env,const JavaParamRef<jobjectArray> & j_keys,const JavaParamRef<jintArray> & j_ids,const JavaParamRef<jintArray> & j_fds,const JavaParamRef<jlongArray> & j_offsets,const JavaParamRef<jlongArray> & j_sizes)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<std::optional<std::string>> keys;
30   JavaObjectArrayReader<jstring> keys_array(j_keys);
31   keys.reserve(checked_cast<size_t>(keys_array.size()));
32   for (auto str : keys_array) {
33     std::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),
56                                              static_cast<size_t>(sizes.at(i))};
57     const std::optional<std::string>& key = keys.at(i);
58     const auto id = static_cast<GlobalDescriptors::Key>(ids.at(i));
59     int fd = fds.at(i);
60     if (key) {
61       base::FileDescriptorStore::GetInstance().Set(*key, base::ScopedFD(fd),
62                                                    region);
63     } else {
64       base::GlobalDescriptors::GetInstance()->Set(id, fd, region);
65     }
66   }
67 }
68 
JNI_ChildProcessService_ExitChildProcess(JNIEnv * env)69 void JNI_ChildProcessService_ExitChildProcess(JNIEnv* env) {
70   VLOG(0) << "ChildProcessService: Exiting child process.";
71   base::android::LibraryLoaderExitHook();
72   _exit(0);
73 }
74 
75 // Make sure this isn't inlined so it shows up in stack traces.
76 // the function body unique by adding a log line, so it doesn't get merged
77 // with other functions by link time optimizations (ICF).
JNI_ChildProcessService_DumpProcessStack(JNIEnv * env)78 NOINLINE void JNI_ChildProcessService_DumpProcessStack(JNIEnv* env) {
79   LOG(ERROR) << "Dumping as requested.";
80   base::debug::DumpWithoutCrashing();
81 }
82 
83 }  // namespace android
84 }  // namespace base
85