blob: c37b8bb51c6b7aa7caf7d0cf0eee87a42be07f71 [file] [log] [blame]
Narayan Kamath8b2c8b92014-03-31 16:44:54 +01001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_ZygoteHooks.h"
18
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010019#include <stdlib.h>
20
Andreas Gampe57943812017-12-06 21:39:13 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "art_method-inl.h"
Andreas Gampe57943812017-12-06 21:39:13 -080026#include "base/macros.h"
27#include "base/mutex.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080028#include "base/runtime_debug.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010029#include "debugger.h"
Andreas Gampeaa120012018-03-28 16:23:24 -070030#include "hidden_api.h"
Mathieu Chartier455f67c2015-03-17 13:48:29 -070031#include "jit/jit.h"
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +000032#include "jit/jit_code_cache.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010033#include "jni/java_vm_ext.h"
34#include "jni/jni_internal.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070035#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070036#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070037#include "nativehelper/scoped_utf_chars.h"
Alex Lighte77b48b2017-02-22 11:08:06 -080038#include "non_debuggable_classes.h"
Nicolas Geoffray68bf3902017-09-07 14:40:48 +010039#include "oat_file.h"
40#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070041#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070042#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070043#include "thread-current-inl.h"
Alex Lighte77b48b2017-02-22 11:08:06 -080044#include "thread_list.h"
Andreas Gampe40da2862015-02-27 12:49:04 -080045#include "trace.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010046
Narayan Kamathad4b0d22014-04-02 12:06:02 +010047#include <sys/resource.h>
48
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010049namespace art {
50
Alex Lighte77b48b2017-02-22 11:08:06 -080051// Set to true to always determine the non-debuggable classes even if we would not allow a debugger
52// to actually attach.
Andreas Gampedbe54912017-10-19 13:02:03 -070053static bool kAlwaysCollectNonDebuggableClasses =
54 RegisterRuntimeDebugFlag(&kAlwaysCollectNonDebuggableClasses);
Alex Lighte77b48b2017-02-22 11:08:06 -080055
Andreas Gampe46ee31b2016-12-14 10:11:49 -080056using android::base::StringPrintf;
57
Alex Light861af892017-02-28 12:45:36 -080058class ClassSet {
59 public:
Alex Light84740262017-03-01 09:00:28 -080060 // The number of classes we reasonably expect to have to look at. Realistically the number is more
61 // ~10 but there is little harm in having some extra.
62 static constexpr int kClassSetCapacity = 100;
Alex Light861af892017-02-28 12:45:36 -080063
Alex Light84740262017-03-01 09:00:28 -080064 explicit ClassSet(Thread* const self) : self_(self) {
65 self_->GetJniEnv()->PushFrame(kClassSetCapacity);
Alex Light861af892017-02-28 12:45:36 -080066 }
67
Alex Light84740262017-03-01 09:00:28 -080068 ~ClassSet() {
69 self_->GetJniEnv()->PopFrame();
70 }
71
72 void AddClass(ObjPtr<mirror::Class> klass) REQUIRES(Locks::mutator_lock_) {
Vladimir Markobcf17522018-06-01 13:14:32 +010073 class_set_.insert(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
Alex Light84740262017-03-01 09:00:28 -080074 }
75
76 const std::unordered_set<jclass>& GetClasses() const {
Alex Light861af892017-02-28 12:45:36 -080077 return class_set_;
78 }
79
80 private:
Alex Light84740262017-03-01 09:00:28 -080081 Thread* const self_;
82 std::unordered_set<jclass> class_set_;
Alex Light861af892017-02-28 12:45:36 -080083};
84
85static void DoCollectNonDebuggableCallback(Thread* thread, void* data)
Alex Lighte77b48b2017-02-22 11:08:06 -080086 REQUIRES(Locks::mutator_lock_) {
87 class NonDebuggableStacksVisitor : public StackVisitor {
88 public:
Alex Light861af892017-02-28 12:45:36 -080089 NonDebuggableStacksVisitor(Thread* t, ClassSet* class_set)
90 : StackVisitor(t, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
91 class_set_(class_set) {}
Alex Lighte77b48b2017-02-22 11:08:06 -080092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010093 ~NonDebuggableStacksVisitor() override {}
Alex Lighte77b48b2017-02-22 11:08:06 -080094
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010095 bool VisitFrame() override REQUIRES(Locks::mutator_lock_) {
Alex Lighte77b48b2017-02-22 11:08:06 -080096 if (GetMethod()->IsRuntimeMethod()) {
97 return true;
98 }
Alex Light861af892017-02-28 12:45:36 -080099 class_set_->AddClass(GetMethod()->GetDeclaringClass());
Alex Lighte77b48b2017-02-22 11:08:06 -0800100 if (kIsDebugBuild) {
101 LOG(INFO) << GetMethod()->GetDeclaringClass()->PrettyClass()
102 << " might not be fully debuggable/deoptimizable due to "
103 << GetMethod()->PrettyMethod() << " appearing on the stack during zygote fork.";
104 }
105 return true;
106 }
Alex Light861af892017-02-28 12:45:36 -0800107
108 private:
109 ClassSet* class_set_;
Alex Lighte77b48b2017-02-22 11:08:06 -0800110 };
Alex Light861af892017-02-28 12:45:36 -0800111 NonDebuggableStacksVisitor visitor(thread, reinterpret_cast<ClassSet*>(data));
Alex Lighte77b48b2017-02-22 11:08:06 -0800112 visitor.WalkStack();
113}
114
Alex Light861af892017-02-28 12:45:36 -0800115static void CollectNonDebuggableClasses() REQUIRES(!Locks::mutator_lock_) {
Alex Lighte77b48b2017-02-22 11:08:06 -0800116 Runtime* const runtime = Runtime::Current();
Alex Light861af892017-02-28 12:45:36 -0800117 Thread* const self = Thread::Current();
118 // Get the mutator lock.
119 ScopedObjectAccess soa(self);
120 ClassSet classes(self);
121 {
Alex Light84740262017-03-01 09:00:28 -0800122 // Drop the shared mutator lock.
123 ScopedThreadSuspension sts(self, art::ThreadState::kNative);
124 // Get exclusive mutator lock with suspend all.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700125 ScopedSuspendAll suspend("Checking stacks for non-obsoletable methods!",
126 /*long_suspend=*/false);
Alex Light84740262017-03-01 09:00:28 -0800127 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
128 runtime->GetThreadList()->ForEach(DoCollectNonDebuggableCallback, &classes);
Alex Light861af892017-02-28 12:45:36 -0800129 }
Alex Light84740262017-03-01 09:00:28 -0800130 for (jclass klass : classes.GetClasses()) {
131 NonDebuggableClasses::AddNonDebuggableClass(klass);
Alex Light861af892017-02-28 12:45:36 -0800132 }
Alex Lighte77b48b2017-02-22 11:08:06 -0800133}
134
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100135// Must match values in com.android.internal.os.Zygote.
136enum {
Artur Satayevacdb9a32019-10-28 18:09:53 +0000137 DEBUG_ENABLE_JDWP = 1,
138 DEBUG_ENABLE_CHECKJNI = 1 << 1,
139 DEBUG_ENABLE_ASSERT = 1 << 2,
140 DEBUG_ENABLE_SAFEMODE = 1 << 3,
141 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
142 DEBUG_GENERATE_DEBUG_INFO = 1 << 5,
143 DEBUG_ALWAYS_JIT = 1 << 6,
144 DEBUG_NATIVE_DEBUGGABLE = 1 << 7,
145 DEBUG_JAVA_DEBUGGABLE = 1 << 8,
146 DISABLE_VERIFIER = 1 << 9,
147 ONLY_USE_SYSTEM_OAT_FILES = 1 << 10,
148 DEBUG_GENERATE_MINI_DEBUG_INFO = 1 << 11,
149 HIDDEN_API_ENFORCEMENT_POLICY_MASK = (1 << 12)
150 | (1 << 13),
151 PROFILE_SYSTEM_SERVER = 1 << 14,
152 PROFILE_FROM_SHELL = 1 << 15,
153 USE_APP_IMAGE_STARTUP_CACHE = 1 << 16,
154 DEBUG_IGNORE_APP_SIGNAL_HANDLER = 1 << 17,
155 DISABLE_TEST_API_ENFORCEMENT_POLICY = 1 << 18,
Mathew Inwood597d7f62018-03-22 11:36:47 +0000156
157 // bits to shift (flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) by to get a value
158 // corresponding to hiddenapi::EnforcementPolicy
159 API_ENFORCEMENT_POLICY_SHIFT = CTZ(HIDDEN_API_ENFORCEMENT_POLICY_MASK),
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100160};
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100161
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100162static uint32_t EnableDebugFeatures(uint32_t runtime_flags) {
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700163 Runtime* const runtime = Runtime::Current();
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100164 if ((runtime_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100165 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -0700166 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -0700167 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100168 vm->SetCheckJniEnabled(true);
169 // There's only one thread running at this point, so only one JNIEnv to fix up.
170 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
171 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -0700172 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100173 }
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100174 runtime_flags &= ~DEBUG_ENABLE_CHECKJNI;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100175 }
176
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100177 if ((runtime_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100178 gLogVerbosity.third_party_jni = true;
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100179 runtime_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100180 }
181
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100182 Dbg::SetJdwpAllowed((runtime_flags & DEBUG_ENABLE_JDWP) != 0);
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100183 runtime_flags &= ~DEBUG_ENABLE_JDWP;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100184
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100185 const bool safe_mode = (runtime_flags & DEBUG_ENABLE_SAFEMODE) != 0;
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700186 if (safe_mode) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100187 // Only quicken oat files.
188 runtime->AddCompilerOption("--compiler-filter=quicken");
Nicolas Geoffray0f042e02015-11-05 11:32:24 +0000189 runtime->SetSafeMode(true);
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100190 runtime_flags &= ~DEBUG_ENABLE_SAFEMODE;
Andreas Gamped2abbc92014-12-19 09:53:27 -0800191 }
192
193 // This is for backwards compatibility with Dalvik.
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100194 runtime_flags &= ~DEBUG_ENABLE_ASSERT;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100195
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100196 if ((runtime_flags & DEBUG_ALWAYS_JIT) != 0) {
Siva Chandra05d24152016-01-05 17:43:17 -0800197 jit::JitOptions* jit_options = runtime->GetJITOptions();
198 CHECK(jit_options != nullptr);
David Srbeckycb4f09e2018-10-21 08:45:22 +0100199 Runtime::Current()->DoAndMaybeSwitchInterpreter([=]() {
200 jit_options->SetJitAtFirstUse();
201 });
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100202 runtime_flags &= ~DEBUG_ALWAYS_JIT;
Siva Chandra05d24152016-01-05 17:43:17 -0800203 }
204
Alex Lighte77b48b2017-02-22 11:08:06 -0800205 bool needs_non_debuggable_classes = false;
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100206 if ((runtime_flags & DEBUG_JAVA_DEBUGGABLE) != 0) {
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000207 runtime->AddCompilerOption("--debuggable");
David Srbecky8f99e0b2018-01-30 14:44:52 +0000208 runtime_flags |= DEBUG_GENERATE_MINI_DEBUG_INFO;
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000209 runtime->SetJavaDebuggable(true);
Nicolas Geoffray226805d2018-12-14 10:59:02 +0000210 {
211 // Deoptimize the boot image as it may be non-debuggable.
212 ScopedSuspendAll ssa(__FUNCTION__);
213 runtime->DeoptimizeBootImage();
214 }
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100215 runtime_flags &= ~DEBUG_JAVA_DEBUGGABLE;
Alex Lighte77b48b2017-02-22 11:08:06 -0800216 needs_non_debuggable_classes = true;
217 }
218 if (needs_non_debuggable_classes || kAlwaysCollectNonDebuggableClasses) {
219 CollectNonDebuggableClasses();
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000220 }
221
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100222 if ((runtime_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) {
David Srbecky346dc992016-03-13 22:00:07 +0000223 runtime->AddCompilerOption("--debuggable");
David Srbecky8f99e0b2018-01-30 14:44:52 +0000224 runtime_flags |= DEBUG_GENERATE_DEBUG_INFO;
David Srbeckyf4480162016-03-16 00:06:24 +0000225 runtime->SetNativeDebuggable(true);
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100226 runtime_flags &= ~DEBUG_NATIVE_DEBUGGABLE;
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000227 }
228
David Srbecky8f99e0b2018-01-30 14:44:52 +0000229 if ((runtime_flags & DEBUG_GENERATE_MINI_DEBUG_INFO) != 0) {
230 // Generate native minimal debug information to allow backtracing.
231 runtime->AddCompilerOption("--generate-mini-debug-info");
232 runtime_flags &= ~DEBUG_GENERATE_MINI_DEBUG_INFO;
233 }
234
235 if ((runtime_flags & DEBUG_GENERATE_DEBUG_INFO) != 0) {
236 // Generate all native debug information we can (e.g. line-numbers).
237 runtime->AddCompilerOption("--generate-debug-info");
238 runtime_flags &= ~DEBUG_GENERATE_DEBUG_INFO;
239 }
240
randy.jeong5bef0222019-05-27 10:29:09 +0900241 if ((runtime_flags & DEBUG_IGNORE_APP_SIGNAL_HANDLER) != 0) {
242 runtime->SetSignalHookDebuggable(true);
243 runtime_flags &= ~DEBUG_IGNORE_APP_SIGNAL_HANDLER;
244 }
245
Florian Mayer07710c52019-09-16 15:53:38 +0000246 runtime->SetProfileableFromShell((runtime_flags & PROFILE_FROM_SHELL) != 0);
247 runtime_flags &= ~PROFILE_FROM_SHELL;
248
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100249 return runtime_flags;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100250}
251
252static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
253 Runtime* runtime = Runtime::Current();
254 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100255
256 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100257
258 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700259 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100260}
261
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000262static void ZygoteHooks_nativePostZygoteFork(JNIEnv*, jclass) {
Nicolas Geoffraya67daeb2019-08-12 10:41:25 +0100263 Runtime::Current()->PostZygoteFork();
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000264}
265
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100266static void ZygoteHooks_nativePostForkSystemServer(JNIEnv* env ATTRIBUTE_UNUSED,
Mathieu Chartier882d4142019-11-21 13:27:10 -0800267 jclass klass ATTRIBUTE_UNUSED,
268 jint runtime_flags) {
Nicolas Geoffraya67daeb2019-08-12 10:41:25 +0100269 // Set the runtime state as the first thing, in case JIT and other services
270 // start querying it.
271 Runtime::Current()->SetAsSystemServer();
272
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100273 // This JIT code cache for system server is created whilst the runtime is still single threaded.
274 // System server has a window where it can create executable pages for this purpose, but this is
275 // turned off after this hook. Consequently, the only JIT mode supported is the dual-view JIT
276 // where one mapping is R->RW and the other is RX. Single view requires RX->RWX->RX.
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000277 if (Runtime::Current()->GetJit() != nullptr) {
278 Runtime::Current()->GetJit()->GetCodeCache()->PostForkChildAction(
279 /* is_system_server= */ true, /* is_zygote= */ false);
280 }
Mathieu Chartier882d4142019-11-21 13:27:10 -0800281 // Enable profiling if required based on the flags. This is done here instead of in
282 // nativePostForkChild since nativePostForkChild is called after loading the system server oat
283 // files.
284 bool profile_system_server = (runtime_flags & PROFILE_SYSTEM_SERVER) == PROFILE_SYSTEM_SERVER;
285 Runtime::Current()->GetJITOptions()->SetSaveProfilingInfo(profile_system_server);
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100286}
287
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000288static void ZygoteHooks_nativePostForkChild(JNIEnv* env,
289 jclass,
290 jlong token,
Nicolas Geoffray5510c0a2017-09-12 15:11:37 +0100291 jint runtime_flags,
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000292 jboolean is_system_server,
Robert Sesek77d33982018-01-17 18:39:04 -0500293 jboolean is_zygote,
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700294 jstring instruction_set) {
Robert Sesek77d33982018-01-17 18:39:04 -0500295 DCHECK(!(is_system_server && is_zygote));
Nicolas Geoffraya67daeb2019-08-12 10:41:25 +0100296 // Set the runtime state as the first thing, in case JIT and other services
297 // start querying it.
298 Runtime::Current()->SetAsZygoteChild(is_system_server, is_zygote);
Robert Sesek77d33982018-01-17 18:39:04 -0500299
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100300 Thread* thread = reinterpret_cast<Thread*>(token);
301 // Our system thread ID, etc, has changed so reset Thread state.
302 thread->InitAfterFork();
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100303 runtime_flags = EnableDebugFeatures(runtime_flags);
David Brazdilf50ac102018-10-17 18:00:06 +0100304 hiddenapi::EnforcementPolicy api_enforcement_policy = hiddenapi::EnforcementPolicy::kDisabled;
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100305
Andreas Gampee095c912019-03-14 13:38:49 -0700306 Runtime* runtime = Runtime::Current();
307
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100308 if ((runtime_flags & DISABLE_VERIFIER) != 0) {
Andreas Gampee095c912019-03-14 13:38:49 -0700309 runtime->DisableVerifier();
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100310 runtime_flags &= ~DISABLE_VERIFIER;
311 }
312
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000313 if ((runtime_flags & ONLY_USE_SYSTEM_OAT_FILES) != 0 || is_system_server) {
Nicolas Geoffray2e17cf42020-03-12 21:19:46 +0000314 runtime->GetOatFileManager().SetOnlyUseSystemOatFiles();
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100315 runtime_flags &= ~ONLY_USE_SYSTEM_OAT_FILES;
316 }
317
Mathew Inwood597d7f62018-03-22 11:36:47 +0000318 api_enforcement_policy = hiddenapi::EnforcementPolicyFromInt(
319 (runtime_flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT);
320 runtime_flags &= ~HIDDEN_API_ENFORCEMENT_POLICY_MASK;
David Brazdilc5e5aed2018-01-17 18:26:30 +0000321
Artur Satayevacdb9a32019-10-28 18:09:53 +0000322 if ((runtime_flags & DISABLE_TEST_API_ENFORCEMENT_POLICY) != 0u) {
323 runtime->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled);
324 } else {
325 runtime->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
326 }
327 runtime_flags &= ~DISABLE_TEST_API_ENFORCEMENT_POLICY;
328
Calin Juravle016fcbe22018-05-03 19:47:35 -0700329 bool profile_system_server = (runtime_flags & PROFILE_SYSTEM_SERVER) == PROFILE_SYSTEM_SERVER;
330 runtime_flags &= ~PROFILE_SYSTEM_SERVER;
331
Andreas Gampee095c912019-03-14 13:38:49 -0700332 runtime->SetLoadAppImageStartupCacheEnabled(
Mathieu Chartiera88abfa2019-02-04 11:08:29 -0800333 (runtime_flags & USE_APP_IMAGE_STARTUP_CACHE) != 0u);
334 runtime_flags &= ~USE_APP_IMAGE_STARTUP_CACHE;
335
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100336 if (runtime_flags != 0) {
337 LOG(ERROR) << StringPrintf("Unknown bits set in runtime_flags: %#x", runtime_flags);
338 }
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700339
Andreas Gampee095c912019-03-14 13:38:49 -0700340 runtime->GetHeap()->PostForkChildAction(thread);
341 if (runtime->GetJit() != nullptr) {
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000342 if (!is_system_server) {
343 // System server already called the JIT cache post fork action in `nativePostForkSystemServer`.
Andreas Gampee095c912019-03-14 13:38:49 -0700344 runtime->GetJit()->GetCodeCache()->PostForkChildAction(
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000345 /* is_system_server= */ false, is_zygote);
346 }
347 // This must be called after EnableDebugFeatures.
Nicolas Geoffray0d54cfb2019-05-03 09:13:52 +0100348 runtime->GetJit()->PostForkChildAction(is_system_server, is_zygote);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000349 }
Mathieu Chartiera98a2822017-05-24 16:14:10 -0700350
Andreas Gampe40da2862015-02-27 12:49:04 -0800351 // Update tracing.
352 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
353 Trace::TraceOutputMode output_mode = Trace::GetOutputMode();
354 Trace::TraceMode trace_mode = Trace::GetMode();
Andreas Gampee34a42c2015-04-25 14:44:29 -0700355 size_t buffer_size = Trace::GetBufferSize();
Andreas Gampe40da2862015-02-27 12:49:04 -0800356
357 // Just drop it.
358 Trace::Abort();
359
360 // Only restart if it was streaming mode.
361 // TODO: Expose buffer size, so we can also do file mode.
362 if (output_mode == Trace::TraceOutputMode::kStreaming) {
Dmitriy Filchenko03c01342016-07-11 17:41:28 -0700363 static constexpr size_t kMaxProcessNameLength = 100;
364 char name_buf[kMaxProcessNameLength] = {};
365 int rc = pthread_getname_np(pthread_self(), name_buf, kMaxProcessNameLength);
Andreas Gampe40da2862015-02-27 12:49:04 -0800366 std::string proc_name;
Dmitriy Filchenko03c01342016-07-11 17:41:28 -0700367
368 if (rc == 0) {
369 // On success use the pthread name.
370 proc_name = name_buf;
Andreas Gampe40da2862015-02-27 12:49:04 -0800371 }
Dmitriy Filchenko03c01342016-07-11 17:41:28 -0700372
373 if (proc_name.empty() || proc_name == "zygote" || proc_name == "zygote64") {
Andreas Gampe40da2862015-02-27 12:49:04 -0800374 // Either no process name, or the name hasn't been changed, yet. Just use pid.
375 pid_t pid = getpid();
376 proc_name = StringPrintf("%u", static_cast<uint32_t>(pid));
377 }
378
Calin Juravlef83e7332015-11-04 16:16:47 +0000379 std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str());
380 Trace::Start(trace_file.c_str(),
Calin Juravlef83e7332015-11-04 16:16:47 +0000381 buffer_size,
382 0, // TODO: Expose flags.
383 output_mode,
384 trace_mode,
385 0); // TODO: Expose interval.
386 if (thread->IsExceptionPending()) {
387 ScopedObjectAccess soa(env);
388 thread->ClearException();
Andreas Gampe40da2862015-02-27 12:49:04 -0800389 }
390 }
391 }
392
David Brazdilf50ac102018-10-17 18:00:06 +0100393 bool do_hidden_api_checks = api_enforcement_policy != hiddenapi::EnforcementPolicy::kDisabled;
Robert Sesek77d33982018-01-17 18:39:04 -0500394 DCHECK(!(is_system_server && do_hidden_api_checks))
Mathew Inwood597d7f62018-03-22 11:36:47 +0000395 << "SystemServer should be forked with EnforcementPolicy::kDisable";
Robert Sesek77d33982018-01-17 18:39:04 -0500396 DCHECK(!(is_zygote && do_hidden_api_checks))
Mathew Inwood597d7f62018-03-22 11:36:47 +0000397 << "Child zygote processes should be forked with EnforcementPolicy::kDisable";
Andreas Gampee095c912019-03-14 13:38:49 -0700398 runtime->SetHiddenApiEnforcementPolicy(api_enforcement_policy);
399 runtime->SetDedupeHiddenApiWarnings(true);
David Brazdilf50ac102018-10-17 18:00:06 +0100400 if (api_enforcement_policy != hiddenapi::EnforcementPolicy::kDisabled &&
Andreas Gampee095c912019-03-14 13:38:49 -0700401 runtime->GetHiddenApiEventLogSampleRate() != 0) {
Mathew Inwood1fd97f22018-04-03 15:32:32 +0100402 // Hidden API checks are enabled, and we are sampling access for the event log. Initialize the
403 // random seed, to ensure the sampling is actually random. We do this post-fork, as doing it
404 // pre-fork would result in the same sequence for every forked process.
405 std::srand(static_cast<uint32_t>(NanoTime()));
406 }
David Brazdilc5e5aed2018-01-17 18:26:30 +0000407
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000408 if (instruction_set != nullptr && !is_system_server) {
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700409 ScopedUtfChars isa_string(env, instruction_set);
410 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400411 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Vladimir Marko33bff252017-11-01 14:35:42 +0000412 if (isa != InstructionSet::kNone && isa != kRuntimeISA) {
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700413 action = Runtime::NativeBridgeAction::kInitialize;
414 }
Lev Rumyantsev87f36302019-12-13 15:49:37 -0800415 runtime->InitNonZygoteOrPostFork(env, is_system_server, is_zygote, action, isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400416 } else {
Andreas Gampee095c912019-03-14 13:38:49 -0700417 runtime->InitNonZygoteOrPostFork(
Calin Juravle016fcbe22018-05-03 19:47:35 -0700418 env,
419 is_system_server,
Lev Rumyantsev87f36302019-12-13 15:49:37 -0800420 is_zygote,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700421 Runtime::NativeBridgeAction::kUnload,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700422 /*isa=*/ nullptr,
Calin Juravle016fcbe22018-05-03 19:47:35 -0700423 profile_system_server);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700424 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100425}
426
Andreas Gampef38a6612016-04-11 08:42:26 -0700427static void ZygoteHooks_startZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED,
428 jclass klass ATTRIBUTE_UNUSED) {
429 Runtime::Current()->SetZygoteNoThreadSection(true);
430}
431
432static void ZygoteHooks_stopZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED,
433 jclass klass ATTRIBUTE_UNUSED) {
434 Runtime::Current()->SetZygoteNoThreadSection(false);
435}
436
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100437static JNINativeMethod gMethods[] = {
438 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000439 NATIVE_METHOD(ZygoteHooks, nativePostZygoteFork, "()V"),
Mathieu Chartier882d4142019-11-21 13:27:10 -0800440 NATIVE_METHOD(ZygoteHooks, nativePostForkSystemServer, "(I)V"),
Robert Sesek77d33982018-01-17 18:39:04 -0500441 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZZLjava/lang/String;)V"),
Andreas Gampef38a6612016-04-11 08:42:26 -0700442 NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"),
443 NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100444};
445
446void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
447 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
448}
449
450} // namespace art