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