xref: /aosp_15_r20/art/runtime/native/dalvik_system_ZygoteHooks.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "dalvik_system_ZygoteHooks.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <stdlib.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
22*795d594fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
25*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/runtime_debug.h"
29*795d594fSAndroid Build Coastguard Worker #include "debugger.h"
30*795d594fSAndroid Build Coastguard Worker #include "hidden_api.h"
31*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
32*795d594fSAndroid Build Coastguard Worker #include "jit/jit_code_cache.h"
33*795d594fSAndroid Build Coastguard Worker #include "jni/java_vm_ext.h"
34*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
35*795d594fSAndroid Build Coastguard Worker #include "native_util.h"
36*795d594fSAndroid Build Coastguard Worker #include "nativehelper/jni_macros.h"
37*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_utf_chars.h"
38*795d594fSAndroid Build Coastguard Worker #include "non_debuggable_classes.h"
39*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file.h"
40*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file_manager.h"
41*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "stack.h"
43*795d594fSAndroid Build Coastguard Worker #include "startup_completed_task.h"
44*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
45*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
46*795d594fSAndroid Build Coastguard Worker #include "trace.h"
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker #include <sys/resource.h>
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker // Set to true to always determine the non-debuggable classes even if we would not allow a debugger
53*795d594fSAndroid Build Coastguard Worker // to actually attach.
54*795d594fSAndroid Build Coastguard Worker static bool kAlwaysCollectNonDebuggableClasses =
55*795d594fSAndroid Build Coastguard Worker     RegisterRuntimeDebugFlag(&kAlwaysCollectNonDebuggableClasses);
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker class ClassSet {
60*795d594fSAndroid Build Coastguard Worker  public:
61*795d594fSAndroid Build Coastguard Worker   // The number of classes we reasonably expect to have to look at. Realistically the number is more
62*795d594fSAndroid Build Coastguard Worker   // ~10 but there is little harm in having some extra.
63*795d594fSAndroid Build Coastguard Worker   static constexpr int kClassSetCapacity = 100;
64*795d594fSAndroid Build Coastguard Worker 
ClassSet(Thread * const self)65*795d594fSAndroid Build Coastguard Worker   explicit ClassSet(Thread* const self) : self_(self) {
66*795d594fSAndroid Build Coastguard Worker     self_->GetJniEnv()->PushFrame(kClassSetCapacity);
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker 
~ClassSet()69*795d594fSAndroid Build Coastguard Worker   ~ClassSet() {
70*795d594fSAndroid Build Coastguard Worker     self_->GetJniEnv()->PopFrame();
71*795d594fSAndroid Build Coastguard Worker   }
72*795d594fSAndroid Build Coastguard Worker 
AddClass(ObjPtr<mirror::Class> klass)73*795d594fSAndroid Build Coastguard Worker   void AddClass(ObjPtr<mirror::Class> klass) REQUIRES(Locks::mutator_lock_) {
74*795d594fSAndroid Build Coastguard Worker     class_set_.insert(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
GetClasses() const77*795d594fSAndroid Build Coastguard Worker   const std::unordered_set<jclass>& GetClasses() const {
78*795d594fSAndroid Build Coastguard Worker     return class_set_;
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker  private:
82*795d594fSAndroid Build Coastguard Worker   Thread* const self_;
83*795d594fSAndroid Build Coastguard Worker   std::unordered_set<jclass> class_set_;
84*795d594fSAndroid Build Coastguard Worker };
85*795d594fSAndroid Build Coastguard Worker 
DoCollectNonDebuggableCallback(Thread * thread,void * data)86*795d594fSAndroid Build Coastguard Worker static void DoCollectNonDebuggableCallback(Thread* thread, void* data)
87*795d594fSAndroid Build Coastguard Worker     REQUIRES(Locks::mutator_lock_) {
88*795d594fSAndroid Build Coastguard Worker   class NonDebuggableStacksVisitor : public StackVisitor {
89*795d594fSAndroid Build Coastguard Worker    public:
90*795d594fSAndroid Build Coastguard Worker     NonDebuggableStacksVisitor(Thread* t, ClassSet* class_set)
91*795d594fSAndroid Build Coastguard Worker         : StackVisitor(t, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
92*795d594fSAndroid Build Coastguard Worker           class_set_(class_set) {}
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker     ~NonDebuggableStacksVisitor() override {}
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker     bool VisitFrame() override REQUIRES(Locks::mutator_lock_) {
97*795d594fSAndroid Build Coastguard Worker       if (GetMethod()->IsRuntimeMethod()) {
98*795d594fSAndroid Build Coastguard Worker         return true;
99*795d594fSAndroid Build Coastguard Worker       }
100*795d594fSAndroid Build Coastguard Worker       class_set_->AddClass(GetMethod()->GetDeclaringClass());
101*795d594fSAndroid Build Coastguard Worker       if (kIsDebugBuild) {
102*795d594fSAndroid Build Coastguard Worker         LOG(INFO) << GetMethod()->GetDeclaringClass()->PrettyClass()
103*795d594fSAndroid Build Coastguard Worker                   << " might not be fully debuggable/deoptimizable due to "
104*795d594fSAndroid Build Coastguard Worker                   << GetMethod()->PrettyMethod() << " appearing on the stack during zygote fork.";
105*795d594fSAndroid Build Coastguard Worker       }
106*795d594fSAndroid Build Coastguard Worker       return true;
107*795d594fSAndroid Build Coastguard Worker     }
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker    private:
110*795d594fSAndroid Build Coastguard Worker     ClassSet* class_set_;
111*795d594fSAndroid Build Coastguard Worker   };
112*795d594fSAndroid Build Coastguard Worker   NonDebuggableStacksVisitor visitor(thread, reinterpret_cast<ClassSet*>(data));
113*795d594fSAndroid Build Coastguard Worker   visitor.WalkStack();
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker 
CollectNonDebuggableClasses()116*795d594fSAndroid Build Coastguard Worker static void CollectNonDebuggableClasses() REQUIRES(!Locks::mutator_lock_) {
117*795d594fSAndroid Build Coastguard Worker   Runtime* const runtime = Runtime::Current();
118*795d594fSAndroid Build Coastguard Worker   Thread* const self = Thread::Current();
119*795d594fSAndroid Build Coastguard Worker   // Get the mutator lock.
120*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(self);
121*795d594fSAndroid Build Coastguard Worker   ClassSet classes(self);
122*795d594fSAndroid Build Coastguard Worker   {
123*795d594fSAndroid Build Coastguard Worker     // Drop the shared mutator lock.
124*795d594fSAndroid Build Coastguard Worker     ScopedThreadSuspension sts(self, art::ThreadState::kNative);
125*795d594fSAndroid Build Coastguard Worker     // Get exclusive mutator lock with suspend all.
126*795d594fSAndroid Build Coastguard Worker     ScopedSuspendAll suspend("Checking stacks for non-obsoletable methods!",
127*795d594fSAndroid Build Coastguard Worker                              /*long_suspend=*/false);
128*795d594fSAndroid Build Coastguard Worker     MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
129*795d594fSAndroid Build Coastguard Worker     runtime->GetThreadList()->ForEach(DoCollectNonDebuggableCallback, &classes);
130*795d594fSAndroid Build Coastguard Worker   }
131*795d594fSAndroid Build Coastguard Worker   for (jclass klass : classes.GetClasses()) {
132*795d594fSAndroid Build Coastguard Worker     NonDebuggableClasses::AddNonDebuggableClass(klass);
133*795d594fSAndroid Build Coastguard Worker   }
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker 
136*795d594fSAndroid Build Coastguard Worker // Must match values in com.android.internal.os.Zygote.
137*795d594fSAndroid Build Coastguard Worker enum {
138*795d594fSAndroid Build Coastguard Worker   DEBUG_ENABLE_JDWP                   = 1,
139*795d594fSAndroid Build Coastguard Worker   DEBUG_ENABLE_CHECKJNI               = 1 << 1,
140*795d594fSAndroid Build Coastguard Worker   DEBUG_ENABLE_ASSERT                 = 1 << 2,
141*795d594fSAndroid Build Coastguard Worker   DEBUG_ENABLE_SAFEMODE               = 1 << 3,
142*795d594fSAndroid Build Coastguard Worker   DEBUG_ENABLE_JNI_LOGGING            = 1 << 4,
143*795d594fSAndroid Build Coastguard Worker   DEBUG_GENERATE_DEBUG_INFO           = 1 << 5,
144*795d594fSAndroid Build Coastguard Worker   DEBUG_ALWAYS_JIT                    = 1 << 6,
145*795d594fSAndroid Build Coastguard Worker   DEBUG_NATIVE_DEBUGGABLE             = 1 << 7,
146*795d594fSAndroid Build Coastguard Worker   DEBUG_JAVA_DEBUGGABLE               = 1 << 8,
147*795d594fSAndroid Build Coastguard Worker   DISABLE_VERIFIER                    = 1 << 9,
148*795d594fSAndroid Build Coastguard Worker   ONLY_USE_TRUSTED_OAT_FILES          = 1 << 10,  // Formerly ONLY_USE_SYSTEM_OAT_FILES
149*795d594fSAndroid Build Coastguard Worker   DEBUG_GENERATE_MINI_DEBUG_INFO      = 1 << 11,
150*795d594fSAndroid Build Coastguard Worker   HIDDEN_API_ENFORCEMENT_POLICY_MASK  = (1 << 12)
151*795d594fSAndroid Build Coastguard Worker                                       | (1 << 13),
152*795d594fSAndroid Build Coastguard Worker   PROFILE_SYSTEM_SERVER               = 1 << 14,
153*795d594fSAndroid Build Coastguard Worker   PROFILE_FROM_SHELL                  = 1 << 15,
154*795d594fSAndroid Build Coastguard Worker   USE_APP_IMAGE_STARTUP_CACHE         = 1 << 16,
155*795d594fSAndroid Build Coastguard Worker   DEBUG_IGNORE_APP_SIGNAL_HANDLER     = 1 << 17,
156*795d594fSAndroid Build Coastguard Worker   DISABLE_TEST_API_ENFORCEMENT_POLICY = 1 << 18,
157*795d594fSAndroid Build Coastguard Worker   PROFILEABLE                         = 1 << 24,
158*795d594fSAndroid Build Coastguard Worker 
159*795d594fSAndroid Build Coastguard Worker   // bits to shift (flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) by to get a value
160*795d594fSAndroid Build Coastguard Worker   // corresponding to hiddenapi::EnforcementPolicy
161*795d594fSAndroid Build Coastguard Worker   API_ENFORCEMENT_POLICY_SHIFT = CTZ(HIDDEN_API_ENFORCEMENT_POLICY_MASK),
162*795d594fSAndroid Build Coastguard Worker };
163*795d594fSAndroid Build Coastguard Worker 
EnableDebugFeatures(uint32_t runtime_flags)164*795d594fSAndroid Build Coastguard Worker static uint32_t EnableDebugFeatures(uint32_t runtime_flags) {
165*795d594fSAndroid Build Coastguard Worker   Runtime* const runtime = Runtime::Current();
166*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
167*795d594fSAndroid Build Coastguard Worker     JavaVMExt* vm = runtime->GetJavaVM();
168*795d594fSAndroid Build Coastguard Worker     if (!vm->IsCheckJniEnabled()) {
169*795d594fSAndroid Build Coastguard Worker       LOG(INFO) << "Late-enabling -Xcheck:jni";
170*795d594fSAndroid Build Coastguard Worker       vm->SetCheckJniEnabled(true);
171*795d594fSAndroid Build Coastguard Worker       // This is the only thread that's running at this point and the above call sets
172*795d594fSAndroid Build Coastguard Worker       // the CheckJNI flag in the corresponding `JniEnvExt`.
173*795d594fSAndroid Build Coastguard Worker       DCHECK(Thread::Current()->GetJniEnv()->IsCheckJniEnabled());
174*795d594fSAndroid Build Coastguard Worker     } else {
175*795d594fSAndroid Build Coastguard Worker       LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
176*795d594fSAndroid Build Coastguard Worker     }
177*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_ENABLE_CHECKJNI;
178*795d594fSAndroid Build Coastguard Worker   }
179*795d594fSAndroid Build Coastguard Worker 
180*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
181*795d594fSAndroid Build Coastguard Worker     gLogVerbosity.third_party_jni = true;
182*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
183*795d594fSAndroid Build Coastguard Worker   }
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker   Dbg::SetJdwpAllowed((runtime_flags & DEBUG_ENABLE_JDWP) != 0);
186*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~DEBUG_ENABLE_JDWP;
187*795d594fSAndroid Build Coastguard Worker 
188*795d594fSAndroid Build Coastguard Worker   const bool safe_mode = (runtime_flags & DEBUG_ENABLE_SAFEMODE) != 0;
189*795d594fSAndroid Build Coastguard Worker   if (safe_mode) {
190*795d594fSAndroid Build Coastguard Worker     // Only verify oat files.
191*795d594fSAndroid Build Coastguard Worker     runtime->AddCompilerOption("--compiler-filter=verify");
192*795d594fSAndroid Build Coastguard Worker     runtime->SetSafeMode(true);
193*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_ENABLE_SAFEMODE;
194*795d594fSAndroid Build Coastguard Worker   }
195*795d594fSAndroid Build Coastguard Worker 
196*795d594fSAndroid Build Coastguard Worker   // This is for backwards compatibility with Dalvik.
197*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~DEBUG_ENABLE_ASSERT;
198*795d594fSAndroid Build Coastguard Worker 
199*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_ALWAYS_JIT) != 0) {
200*795d594fSAndroid Build Coastguard Worker     jit::JitOptions* jit_options = runtime->GetJITOptions();
201*795d594fSAndroid Build Coastguard Worker     CHECK(jit_options != nullptr);
202*795d594fSAndroid Build Coastguard Worker     jit_options->SetJitAtFirstUse();
203*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_ALWAYS_JIT;
204*795d594fSAndroid Build Coastguard Worker   }
205*795d594fSAndroid Build Coastguard Worker 
206*795d594fSAndroid Build Coastguard Worker   bool needs_non_debuggable_classes = false;
207*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_JAVA_DEBUGGABLE) != 0) {
208*795d594fSAndroid Build Coastguard Worker     runtime->AddCompilerOption("--debuggable");
209*795d594fSAndroid Build Coastguard Worker     runtime_flags |= DEBUG_GENERATE_MINI_DEBUG_INFO;
210*795d594fSAndroid Build Coastguard Worker     runtime->SetRuntimeDebugState(Runtime::RuntimeDebugState::kJavaDebuggableAtInit);
211*795d594fSAndroid Build Coastguard Worker     {
212*795d594fSAndroid Build Coastguard Worker       // Deoptimize the boot image as it may be non-debuggable.
213*795d594fSAndroid Build Coastguard Worker       ScopedSuspendAll ssa(__FUNCTION__);
214*795d594fSAndroid Build Coastguard Worker       runtime->DeoptimizeBootImage();
215*795d594fSAndroid Build Coastguard Worker     }
216*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_JAVA_DEBUGGABLE;
217*795d594fSAndroid Build Coastguard Worker     needs_non_debuggable_classes = true;
218*795d594fSAndroid Build Coastguard Worker   }
219*795d594fSAndroid Build Coastguard Worker   if (needs_non_debuggable_classes || kAlwaysCollectNonDebuggableClasses) {
220*795d594fSAndroid Build Coastguard Worker     CollectNonDebuggableClasses();
221*795d594fSAndroid Build Coastguard Worker   }
222*795d594fSAndroid Build Coastguard Worker 
223*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) {
224*795d594fSAndroid Build Coastguard Worker     runtime->AddCompilerOption("--debuggable");
225*795d594fSAndroid Build Coastguard Worker     runtime_flags |= DEBUG_GENERATE_DEBUG_INFO;
226*795d594fSAndroid Build Coastguard Worker     runtime->SetNativeDebuggable(true);
227*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_NATIVE_DEBUGGABLE;
228*795d594fSAndroid Build Coastguard Worker   }
229*795d594fSAndroid Build Coastguard Worker 
230*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_GENERATE_MINI_DEBUG_INFO) != 0) {
231*795d594fSAndroid Build Coastguard Worker     // Generate native minimal debug information to allow backtracing.
232*795d594fSAndroid Build Coastguard Worker     runtime->AddCompilerOption("--generate-mini-debug-info");
233*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_GENERATE_MINI_DEBUG_INFO;
234*795d594fSAndroid Build Coastguard Worker   }
235*795d594fSAndroid Build Coastguard Worker 
236*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_GENERATE_DEBUG_INFO) != 0) {
237*795d594fSAndroid Build Coastguard Worker     // Generate all native debug information we can (e.g. line-numbers).
238*795d594fSAndroid Build Coastguard Worker     runtime->AddCompilerOption("--generate-debug-info");
239*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_GENERATE_DEBUG_INFO;
240*795d594fSAndroid Build Coastguard Worker   }
241*795d594fSAndroid Build Coastguard Worker 
242*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DEBUG_IGNORE_APP_SIGNAL_HANDLER) != 0) {
243*795d594fSAndroid Build Coastguard Worker     runtime->SetSignalHookDebuggable(true);
244*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DEBUG_IGNORE_APP_SIGNAL_HANDLER;
245*795d594fSAndroid Build Coastguard Worker   }
246*795d594fSAndroid Build Coastguard Worker 
247*795d594fSAndroid Build Coastguard Worker   runtime->SetProfileableFromShell((runtime_flags & PROFILE_FROM_SHELL) != 0);
248*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~PROFILE_FROM_SHELL;
249*795d594fSAndroid Build Coastguard Worker   runtime->SetProfileable((runtime_flags & PROFILEABLE) != 0);
250*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~PROFILEABLE;
251*795d594fSAndroid Build Coastguard Worker 
252*795d594fSAndroid Build Coastguard Worker   return runtime_flags;
253*795d594fSAndroid Build Coastguard Worker }
254*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_nativePreFork(JNIEnv * env,jclass)255*795d594fSAndroid Build Coastguard Worker static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
256*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
257*795d594fSAndroid Build Coastguard Worker   CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
258*795d594fSAndroid Build Coastguard Worker 
259*795d594fSAndroid Build Coastguard Worker   runtime->PreZygoteFork();
260*795d594fSAndroid Build Coastguard Worker 
261*795d594fSAndroid Build Coastguard Worker   // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
262*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<jlong>(Thread::ForEnv(env));
263*795d594fSAndroid Build Coastguard Worker }
264*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_nativePostZygoteFork(JNIEnv *,jclass)265*795d594fSAndroid Build Coastguard Worker static void ZygoteHooks_nativePostZygoteFork(JNIEnv*, jclass) {
266*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->PostZygoteFork();
267*795d594fSAndroid Build Coastguard Worker }
268*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_nativePostForkSystemServer(JNIEnv * env,jclass klass,jint runtime_flags)269*795d594fSAndroid Build Coastguard Worker static void ZygoteHooks_nativePostForkSystemServer([[maybe_unused]] JNIEnv* env,
270*795d594fSAndroid Build Coastguard Worker                                                    [[maybe_unused]] jclass klass,
271*795d594fSAndroid Build Coastguard Worker                                                    jint runtime_flags) {
272*795d594fSAndroid Build Coastguard Worker   // Reload the current flags first. In case we need to take actions based on them.
273*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->ReloadAllFlags(__FUNCTION__);
274*795d594fSAndroid Build Coastguard Worker 
275*795d594fSAndroid Build Coastguard Worker   // Set the runtime state as the first thing, in case JIT and other services
276*795d594fSAndroid Build Coastguard Worker   // start querying it.
277*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetAsSystemServer();
278*795d594fSAndroid Build Coastguard Worker 
279*795d594fSAndroid Build Coastguard Worker   // This JIT code cache for system server is created whilst the runtime is still single threaded.
280*795d594fSAndroid Build Coastguard Worker   // System server has a window where it can create executable pages for this purpose, but this is
281*795d594fSAndroid Build Coastguard Worker   // turned off after this hook. Consequently, the only JIT mode supported is the dual-view JIT
282*795d594fSAndroid Build Coastguard Worker   // where one mapping is R->RW and the other is RX. Single view requires RX->RWX->RX.
283*795d594fSAndroid Build Coastguard Worker   if (Runtime::Current()->GetJit() != nullptr) {
284*795d594fSAndroid Build Coastguard Worker     Runtime::Current()->GetJit()->GetCodeCache()->PostForkChildAction(
285*795d594fSAndroid Build Coastguard Worker         /* is_system_server= */ true, /* is_zygote= */ false);
286*795d594fSAndroid Build Coastguard Worker   }
287*795d594fSAndroid Build Coastguard Worker   // Enable profiling if required based on the flags. This is done here instead of in
288*795d594fSAndroid Build Coastguard Worker   // nativePostForkChild since nativePostForkChild is called after loading the system server oat
289*795d594fSAndroid Build Coastguard Worker   // files.
290*795d594fSAndroid Build Coastguard Worker   bool profile_system_server = (runtime_flags & PROFILE_SYSTEM_SERVER) == PROFILE_SYSTEM_SERVER;
291*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetJITOptions()->SetSaveProfilingInfo(profile_system_server);
292*795d594fSAndroid Build Coastguard Worker }
293*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_nativePostForkChild(JNIEnv * env,jclass,jlong token,jint runtime_flags,jboolean is_system_server,jboolean is_zygote,jstring instruction_set)294*795d594fSAndroid Build Coastguard Worker static void ZygoteHooks_nativePostForkChild(JNIEnv* env,
295*795d594fSAndroid Build Coastguard Worker                                             jclass,
296*795d594fSAndroid Build Coastguard Worker                                             jlong token,
297*795d594fSAndroid Build Coastguard Worker                                             jint runtime_flags,
298*795d594fSAndroid Build Coastguard Worker                                             jboolean is_system_server,
299*795d594fSAndroid Build Coastguard Worker                                             jboolean is_zygote,
300*795d594fSAndroid Build Coastguard Worker                                             jstring instruction_set) {
301*795d594fSAndroid Build Coastguard Worker   DCHECK(!(is_system_server && is_zygote));
302*795d594fSAndroid Build Coastguard Worker   // Reload the current flags first. In case we need to take any updated actions.
303*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->ReloadAllFlags(__FUNCTION__);
304*795d594fSAndroid Build Coastguard Worker   // Then, set the runtime state, in case JIT and other services
305*795d594fSAndroid Build Coastguard Worker   // start querying it.
306*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetAsZygoteChild(is_system_server, is_zygote);
307*795d594fSAndroid Build Coastguard Worker 
308*795d594fSAndroid Build Coastguard Worker   Thread* thread = reinterpret_cast<Thread*>(token);
309*795d594fSAndroid Build Coastguard Worker   // Our system thread ID, etc, has changed so reset Thread state.
310*795d594fSAndroid Build Coastguard Worker   thread->InitAfterFork();
311*795d594fSAndroid Build Coastguard Worker   runtime_flags = EnableDebugFeatures(runtime_flags);
312*795d594fSAndroid Build Coastguard Worker   hiddenapi::EnforcementPolicy api_enforcement_policy = hiddenapi::EnforcementPolicy::kDisabled;
313*795d594fSAndroid Build Coastguard Worker 
314*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
315*795d594fSAndroid Build Coastguard Worker 
316*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DISABLE_VERIFIER) != 0) {
317*795d594fSAndroid Build Coastguard Worker     runtime->DisableVerifier();
318*795d594fSAndroid Build Coastguard Worker     runtime_flags &= ~DISABLE_VERIFIER;
319*795d594fSAndroid Build Coastguard Worker   }
320*795d594fSAndroid Build Coastguard Worker 
321*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & ONLY_USE_TRUSTED_OAT_FILES) == 0 && !is_system_server) {
322*795d594fSAndroid Build Coastguard Worker     runtime->GetOatFileManager().ClearOnlyUseTrustedOatFiles();
323*795d594fSAndroid Build Coastguard Worker   }
324*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~ONLY_USE_TRUSTED_OAT_FILES;
325*795d594fSAndroid Build Coastguard Worker 
326*795d594fSAndroid Build Coastguard Worker   api_enforcement_policy = hiddenapi::EnforcementPolicyFromInt(
327*795d594fSAndroid Build Coastguard Worker       (runtime_flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT);
328*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~HIDDEN_API_ENFORCEMENT_POLICY_MASK;
329*795d594fSAndroid Build Coastguard Worker 
330*795d594fSAndroid Build Coastguard Worker   if ((runtime_flags & DISABLE_TEST_API_ENFORCEMENT_POLICY) != 0u) {
331*795d594fSAndroid Build Coastguard Worker     runtime->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled);
332*795d594fSAndroid Build Coastguard Worker   } else {
333*795d594fSAndroid Build Coastguard Worker     runtime->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
334*795d594fSAndroid Build Coastguard Worker   }
335*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~DISABLE_TEST_API_ENFORCEMENT_POLICY;
336*795d594fSAndroid Build Coastguard Worker 
337*795d594fSAndroid Build Coastguard Worker   bool profile_system_server = (runtime_flags & PROFILE_SYSTEM_SERVER) == PROFILE_SYSTEM_SERVER;
338*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~PROFILE_SYSTEM_SERVER;
339*795d594fSAndroid Build Coastguard Worker 
340*795d594fSAndroid Build Coastguard Worker   runtime->SetLoadAppImageStartupCacheEnabled(
341*795d594fSAndroid Build Coastguard Worker       (runtime_flags & USE_APP_IMAGE_STARTUP_CACHE) != 0u);
342*795d594fSAndroid Build Coastguard Worker   runtime_flags &= ~USE_APP_IMAGE_STARTUP_CACHE;
343*795d594fSAndroid Build Coastguard Worker 
344*795d594fSAndroid Build Coastguard Worker   if (runtime_flags != 0) {
345*795d594fSAndroid Build Coastguard Worker     LOG(ERROR) << StringPrintf("Unknown bits set in runtime_flags: %#x", runtime_flags);
346*795d594fSAndroid Build Coastguard Worker   }
347*795d594fSAndroid Build Coastguard Worker 
348*795d594fSAndroid Build Coastguard Worker   runtime->GetHeap()->PostForkChildAction(thread);
349*795d594fSAndroid Build Coastguard Worker 
350*795d594fSAndroid Build Coastguard Worker   if (!is_zygote) {
351*795d594fSAndroid Build Coastguard Worker     // Setup an app startup complete task in case the app doesn't notify it
352*795d594fSAndroid Build Coastguard Worker     // through VMRuntime::notifyStartupCompleted.
353*795d594fSAndroid Build Coastguard Worker     static constexpr uint64_t kMaxAppStartupTimeNs = MsToNs(5000);  // 5 seconds
354*795d594fSAndroid Build Coastguard Worker     runtime->GetHeap()->AddHeapTask(new StartupCompletedTask(NanoTime() + kMaxAppStartupTimeNs));
355*795d594fSAndroid Build Coastguard Worker   }
356*795d594fSAndroid Build Coastguard Worker 
357*795d594fSAndroid Build Coastguard Worker   if (runtime->GetJit() != nullptr) {
358*795d594fSAndroid Build Coastguard Worker     if (!is_system_server) {
359*795d594fSAndroid Build Coastguard Worker       // System server already called the JIT cache post fork action in `nativePostForkSystemServer`.
360*795d594fSAndroid Build Coastguard Worker       runtime->GetJit()->GetCodeCache()->PostForkChildAction(
361*795d594fSAndroid Build Coastguard Worker           /* is_system_server= */ false, is_zygote);
362*795d594fSAndroid Build Coastguard Worker     }
363*795d594fSAndroid Build Coastguard Worker     // This must be called after EnableDebugFeatures.
364*795d594fSAndroid Build Coastguard Worker     runtime->GetJit()->PostForkChildAction(is_system_server, is_zygote);
365*795d594fSAndroid Build Coastguard Worker   }
366*795d594fSAndroid Build Coastguard Worker 
367*795d594fSAndroid Build Coastguard Worker   // Update tracing.
368*795d594fSAndroid Build Coastguard Worker   if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
369*795d594fSAndroid Build Coastguard Worker     TraceOutputMode output_mode = Trace::GetOutputMode();
370*795d594fSAndroid Build Coastguard Worker     Trace::TraceMode trace_mode = Trace::GetMode();
371*795d594fSAndroid Build Coastguard Worker     size_t buffer_size = Trace::GetBufferSize();
372*795d594fSAndroid Build Coastguard Worker     int flags = Trace::GetFlags();
373*795d594fSAndroid Build Coastguard Worker     int interval = Trace::GetIntervalInMillis();
374*795d594fSAndroid Build Coastguard Worker 
375*795d594fSAndroid Build Coastguard Worker     // Just drop it.
376*795d594fSAndroid Build Coastguard Worker     Trace::Abort();
377*795d594fSAndroid Build Coastguard Worker 
378*795d594fSAndroid Build Coastguard Worker     // Only restart if it was streaming mode.
379*795d594fSAndroid Build Coastguard Worker     // TODO: Expose buffer size, so we can also do file mode.
380*795d594fSAndroid Build Coastguard Worker     if (output_mode == TraceOutputMode::kStreaming) {
381*795d594fSAndroid Build Coastguard Worker       static constexpr size_t kMaxProcessNameLength = 100;
382*795d594fSAndroid Build Coastguard Worker       char name_buf[kMaxProcessNameLength] = {};
383*795d594fSAndroid Build Coastguard Worker       int rc = pthread_getname_np(pthread_self(), name_buf, kMaxProcessNameLength);
384*795d594fSAndroid Build Coastguard Worker       std::string proc_name;
385*795d594fSAndroid Build Coastguard Worker 
386*795d594fSAndroid Build Coastguard Worker       if (rc == 0) {
387*795d594fSAndroid Build Coastguard Worker           // On success use the pthread name.
388*795d594fSAndroid Build Coastguard Worker           proc_name = name_buf;
389*795d594fSAndroid Build Coastguard Worker       }
390*795d594fSAndroid Build Coastguard Worker 
391*795d594fSAndroid Build Coastguard Worker       if (proc_name.empty() || proc_name == "zygote" || proc_name == "zygote64") {
392*795d594fSAndroid Build Coastguard Worker         // Either no process name, or the name hasn't been changed, yet. Just use pid.
393*795d594fSAndroid Build Coastguard Worker         pid_t pid = getpid();
394*795d594fSAndroid Build Coastguard Worker         proc_name = StringPrintf("%u", static_cast<uint32_t>(pid));
395*795d594fSAndroid Build Coastguard Worker       }
396*795d594fSAndroid Build Coastguard Worker 
397*795d594fSAndroid Build Coastguard Worker       const char* path = kIsTargetBuild ? "/data/misc/trace" : "/tmp";
398*795d594fSAndroid Build Coastguard Worker       std::string trace_file = StringPrintf("%s/%s.trace.bin", path, proc_name.c_str());
399*795d594fSAndroid Build Coastguard Worker       Trace::Start(trace_file.c_str(),
400*795d594fSAndroid Build Coastguard Worker                    buffer_size,
401*795d594fSAndroid Build Coastguard Worker                    flags,
402*795d594fSAndroid Build Coastguard Worker                    output_mode,
403*795d594fSAndroid Build Coastguard Worker                    trace_mode,
404*795d594fSAndroid Build Coastguard Worker                    interval);
405*795d594fSAndroid Build Coastguard Worker       if (thread->IsExceptionPending()) {
406*795d594fSAndroid Build Coastguard Worker         ScopedObjectAccess soa(env);
407*795d594fSAndroid Build Coastguard Worker         thread->ClearException();
408*795d594fSAndroid Build Coastguard Worker       }
409*795d594fSAndroid Build Coastguard Worker     }
410*795d594fSAndroid Build Coastguard Worker   }
411*795d594fSAndroid Build Coastguard Worker 
412*795d594fSAndroid Build Coastguard Worker   bool do_hidden_api_checks = api_enforcement_policy != hiddenapi::EnforcementPolicy::kDisabled;
413*795d594fSAndroid Build Coastguard Worker   DCHECK(!(is_system_server && do_hidden_api_checks))
414*795d594fSAndroid Build Coastguard Worker       << "SystemServer should be forked with EnforcementPolicy::kDisable";
415*795d594fSAndroid Build Coastguard Worker   DCHECK(!(is_zygote && do_hidden_api_checks))
416*795d594fSAndroid Build Coastguard Worker       << "Child zygote processes should be forked with EnforcementPolicy::kDisable";
417*795d594fSAndroid Build Coastguard Worker   runtime->SetHiddenApiEnforcementPolicy(api_enforcement_policy);
418*795d594fSAndroid Build Coastguard Worker   runtime->SetDedupeHiddenApiWarnings(true);
419*795d594fSAndroid Build Coastguard Worker   if (api_enforcement_policy != hiddenapi::EnforcementPolicy::kDisabled &&
420*795d594fSAndroid Build Coastguard Worker       runtime->GetHiddenApiEventLogSampleRate() != 0) {
421*795d594fSAndroid Build Coastguard Worker     // Hidden API checks are enabled, and we are sampling access for the event log. Initialize the
422*795d594fSAndroid Build Coastguard Worker     // random seed, to ensure the sampling is actually random. We do this post-fork, as doing it
423*795d594fSAndroid Build Coastguard Worker     // pre-fork would result in the same sequence for every forked process.
424*795d594fSAndroid Build Coastguard Worker     std::srand(static_cast<uint32_t>(NanoTime()));
425*795d594fSAndroid Build Coastguard Worker   }
426*795d594fSAndroid Build Coastguard Worker 
427*795d594fSAndroid Build Coastguard Worker   if (instruction_set != nullptr && !is_system_server) {
428*795d594fSAndroid Build Coastguard Worker     ScopedUtfChars isa_string(env, instruction_set);
429*795d594fSAndroid Build Coastguard Worker     InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
430*795d594fSAndroid Build Coastguard Worker     Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
431*795d594fSAndroid Build Coastguard Worker     if (isa != InstructionSet::kNone && isa != kRuntimeISA) {
432*795d594fSAndroid Build Coastguard Worker       action = Runtime::NativeBridgeAction::kInitialize;
433*795d594fSAndroid Build Coastguard Worker     }
434*795d594fSAndroid Build Coastguard Worker     runtime->InitNonZygoteOrPostFork(env, is_system_server, is_zygote, action, isa_string.c_str());
435*795d594fSAndroid Build Coastguard Worker   } else {
436*795d594fSAndroid Build Coastguard Worker     runtime->InitNonZygoteOrPostFork(
437*795d594fSAndroid Build Coastguard Worker         env,
438*795d594fSAndroid Build Coastguard Worker         is_system_server,
439*795d594fSAndroid Build Coastguard Worker         is_zygote,
440*795d594fSAndroid Build Coastguard Worker         Runtime::NativeBridgeAction::kUnload,
441*795d594fSAndroid Build Coastguard Worker         /*isa=*/ nullptr,
442*795d594fSAndroid Build Coastguard Worker         profile_system_server);
443*795d594fSAndroid Build Coastguard Worker   }
444*795d594fSAndroid Build Coastguard Worker }
445*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_startZygoteNoThreadCreation(JNIEnv * env,jclass klass)446*795d594fSAndroid Build Coastguard Worker static void ZygoteHooks_startZygoteNoThreadCreation([[maybe_unused]] JNIEnv* env,
447*795d594fSAndroid Build Coastguard Worker                                                     [[maybe_unused]] jclass klass) {
448*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetZygoteNoThreadSection(true);
449*795d594fSAndroid Build Coastguard Worker }
450*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_stopZygoteNoThreadCreation(JNIEnv * env,jclass klass)451*795d594fSAndroid Build Coastguard Worker static void ZygoteHooks_stopZygoteNoThreadCreation([[maybe_unused]] JNIEnv* env,
452*795d594fSAndroid Build Coastguard Worker                                                    [[maybe_unused]] jclass klass) {
453*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetZygoteNoThreadSection(false);
454*795d594fSAndroid Build Coastguard Worker }
455*795d594fSAndroid Build Coastguard Worker 
ZygoteHooks_nativeZygoteLongSuspendOk(JNIEnv * env,jclass klass)456*795d594fSAndroid Build Coastguard Worker static jboolean ZygoteHooks_nativeZygoteLongSuspendOk([[maybe_unused]] JNIEnv* env,
457*795d594fSAndroid Build Coastguard Worker                                                       [[maybe_unused]] jclass klass) {
458*795d594fSAndroid Build Coastguard Worker   // Indefinite thread suspensions are not OK if we're supposed to be JIT-compiling for other
459*795d594fSAndroid Build Coastguard Worker   // processes.  We only care about JIT compilation that affects other processes.  The zygote
460*795d594fSAndroid Build Coastguard Worker   // itself doesn't run appreciable amounts of Java code when running single-threaded, so
461*795d594fSAndroid Build Coastguard Worker   // suspending the JIT in non-jit-zygote mode is OK.
462*795d594fSAndroid Build Coastguard Worker   // TODO: Make this potentially return true once we're done with JIT compilation in JIT Zygote.
463*795d594fSAndroid Build Coastguard Worker   // Only called in zygote. Thus static is OK here.
464*795d594fSAndroid Build Coastguard Worker   static bool isJitZygote = jit::Jit::InZygoteUsingJit();
465*795d594fSAndroid Build Coastguard Worker   static bool explicitlyDisabled = Runtime::Current()->IsJavaZygoteForkLoopRequired();
466*795d594fSAndroid Build Coastguard Worker   return (isJitZygote || explicitlyDisabled) ? JNI_FALSE : JNI_TRUE;
467*795d594fSAndroid Build Coastguard Worker }
468*795d594fSAndroid Build Coastguard Worker 
469*795d594fSAndroid Build Coastguard Worker static JNINativeMethod gMethods[] = {
470*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
471*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, nativePostZygoteFork, "()V"),
472*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, nativePostForkSystemServer, "(I)V"),
473*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZZLjava/lang/String;)V"),
474*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, nativeZygoteLongSuspendOk, "()Z"),
475*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"),
476*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"),
477*795d594fSAndroid Build Coastguard Worker };
478*795d594fSAndroid Build Coastguard Worker 
register_dalvik_system_ZygoteHooks(JNIEnv * env)479*795d594fSAndroid Build Coastguard Worker void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
480*795d594fSAndroid Build Coastguard Worker   REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
481*795d594fSAndroid Build Coastguard Worker }
482*795d594fSAndroid Build Coastguard Worker 
483*795d594fSAndroid Build Coastguard Worker }  // namespace art
484