Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_ZygoteHooks.h" |
| 18 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | #include "base/macros.h" |
| 27 | #include "base/mutex.h" |
Andreas Gampe | dcc528d | 2017-12-07 13:37:10 -0800 | [diff] [blame] | 28 | #include "base/runtime_debug.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 29 | #include "debugger.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 30 | #include "hidden_api.h" |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 31 | #include "jit/jit.h" |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 32 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 33 | #include "jni/java_vm_ext.h" |
| 34 | #include "jni/jni_internal.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 35 | #include "native_util.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 36 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 37 | #include "nativehelper/scoped_utf_chars.h" |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 38 | #include "non_debuggable_classes.h" |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 39 | #include "oat_file.h" |
| 40 | #include "oat_file_manager.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 41 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 42 | #include "stack.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 43 | #include "thread-current-inl.h" |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 44 | #include "thread_list.h" |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 45 | #include "trace.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 46 | |
Narayan Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 47 | #include <sys/resource.h> |
| 48 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 49 | namespace art { |
| 50 | |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 51 | // Set to true to always determine the non-debuggable classes even if we would not allow a debugger |
| 52 | // to actually attach. |
Andreas Gampe | dbe5491 | 2017-10-19 13:02:03 -0700 | [diff] [blame] | 53 | static bool kAlwaysCollectNonDebuggableClasses = |
| 54 | RegisterRuntimeDebugFlag(&kAlwaysCollectNonDebuggableClasses); |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 55 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 56 | using android::base::StringPrintf; |
| 57 | |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 58 | class ClassSet { |
| 59 | public: |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 60 | // 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 Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 63 | |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 64 | explicit ClassSet(Thread* const self) : self_(self) { |
| 65 | self_->GetJniEnv()->PushFrame(kClassSetCapacity); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 68 | ~ClassSet() { |
| 69 | self_->GetJniEnv()->PopFrame(); |
| 70 | } |
| 71 | |
| 72 | void AddClass(ObjPtr<mirror::Class> klass) REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 73 | class_set_.insert(self_->GetJniEnv()->AddLocalReference<jclass>(klass)); |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | const std::unordered_set<jclass>& GetClasses() const { |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 77 | return class_set_; |
| 78 | } |
| 79 | |
| 80 | private: |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 81 | Thread* const self_; |
| 82 | std::unordered_set<jclass> class_set_; |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static void DoCollectNonDebuggableCallback(Thread* thread, void* data) |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 86 | REQUIRES(Locks::mutator_lock_) { |
| 87 | class NonDebuggableStacksVisitor : public StackVisitor { |
| 88 | public: |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 89 | NonDebuggableStacksVisitor(Thread* t, ClassSet* class_set) |
| 90 | : StackVisitor(t, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 91 | class_set_(class_set) {} |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 92 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 93 | ~NonDebuggableStacksVisitor() override {} |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 94 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 95 | bool VisitFrame() override REQUIRES(Locks::mutator_lock_) { |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 96 | if (GetMethod()->IsRuntimeMethod()) { |
| 97 | return true; |
| 98 | } |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 99 | class_set_->AddClass(GetMethod()->GetDeclaringClass()); |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 100 | 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 Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | ClassSet* class_set_; |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 110 | }; |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 111 | NonDebuggableStacksVisitor visitor(thread, reinterpret_cast<ClassSet*>(data)); |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 112 | visitor.WalkStack(); |
| 113 | } |
| 114 | |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 115 | static void CollectNonDebuggableClasses() REQUIRES(!Locks::mutator_lock_) { |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 116 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 117 | Thread* const self = Thread::Current(); |
| 118 | // Get the mutator lock. |
| 119 | ScopedObjectAccess soa(self); |
| 120 | ClassSet classes(self); |
| 121 | { |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 122 | // Drop the shared mutator lock. |
| 123 | ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
| 124 | // Get exclusive mutator lock with suspend all. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 125 | ScopedSuspendAll suspend("Checking stacks for non-obsoletable methods!", |
| 126 | /*long_suspend=*/false); |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 127 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 128 | runtime->GetThreadList()->ForEach(DoCollectNonDebuggableCallback, &classes); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 129 | } |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 130 | for (jclass klass : classes.GetClasses()) { |
| 131 | NonDebuggableClasses::AddNonDebuggableClass(klass); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 132 | } |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 135 | // Must match values in com.android.internal.os.Zygote. |
| 136 | enum { |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 137 | 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), |
Calin Juravle | 016fcbe2 | 2018-05-03 19:47:35 -0700 | [diff] [blame] | 151 | PROFILE_SYSTEM_SERVER = 1 << 14, |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 152 | USE_APP_IMAGE_STARTUP_CACHE = 1 << 16, |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 153 | |
| 154 | // bits to shift (flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) by to get a value |
| 155 | // corresponding to hiddenapi::EnforcementPolicy |
| 156 | API_ENFORCEMENT_POLICY_SHIFT = CTZ(HIDDEN_API_ENFORCEMENT_POLICY_MASK), |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 157 | }; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 158 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 159 | static uint32_t EnableDebugFeatures(uint32_t runtime_flags) { |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 160 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 161 | if ((runtime_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 162 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 163 | if (!vm->IsCheckJniEnabled()) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 164 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 165 | vm->SetCheckJniEnabled(true); |
| 166 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
| 167 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
| 168 | } else { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 169 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 170 | } |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 171 | runtime_flags &= ~DEBUG_ENABLE_CHECKJNI; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 174 | if ((runtime_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 175 | gLogVerbosity.third_party_jni = true; |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 176 | runtime_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 179 | Dbg::SetJdwpAllowed((runtime_flags & DEBUG_ENABLE_JDWP) != 0); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 180 | runtime_flags &= ~DEBUG_ENABLE_JDWP; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 181 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 182 | const bool safe_mode = (runtime_flags & DEBUG_ENABLE_SAFEMODE) != 0; |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 183 | if (safe_mode) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 184 | // Only quicken oat files. |
| 185 | runtime->AddCompilerOption("--compiler-filter=quicken"); |
Nicolas Geoffray | 0f042e0 | 2015-11-05 11:32:24 +0000 | [diff] [blame] | 186 | runtime->SetSafeMode(true); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 187 | runtime_flags &= ~DEBUG_ENABLE_SAFEMODE; |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // This is for backwards compatibility with Dalvik. |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 191 | runtime_flags &= ~DEBUG_ENABLE_ASSERT; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 192 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 193 | if ((runtime_flags & DEBUG_ALWAYS_JIT) != 0) { |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 194 | jit::JitOptions* jit_options = runtime->GetJITOptions(); |
| 195 | CHECK(jit_options != nullptr); |
David Srbecky | cb4f09e | 2018-10-21 08:45:22 +0100 | [diff] [blame] | 196 | Runtime::Current()->DoAndMaybeSwitchInterpreter([=]() { |
| 197 | jit_options->SetJitAtFirstUse(); |
| 198 | }); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 199 | runtime_flags &= ~DEBUG_ALWAYS_JIT; |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 202 | bool needs_non_debuggable_classes = false; |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 203 | if ((runtime_flags & DEBUG_JAVA_DEBUGGABLE) != 0) { |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 204 | runtime->AddCompilerOption("--debuggable"); |
David Srbecky | 8f99e0b | 2018-01-30 14:44:52 +0000 | [diff] [blame] | 205 | runtime_flags |= DEBUG_GENERATE_MINI_DEBUG_INFO; |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 206 | runtime->SetJavaDebuggable(true); |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 207 | { |
| 208 | // Deoptimize the boot image as it may be non-debuggable. |
| 209 | ScopedSuspendAll ssa(__FUNCTION__); |
| 210 | runtime->DeoptimizeBootImage(); |
| 211 | } |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 212 | runtime_flags &= ~DEBUG_JAVA_DEBUGGABLE; |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 213 | needs_non_debuggable_classes = true; |
| 214 | } |
| 215 | if (needs_non_debuggable_classes || kAlwaysCollectNonDebuggableClasses) { |
| 216 | CollectNonDebuggableClasses(); |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 219 | if ((runtime_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) { |
David Srbecky | 346dc99 | 2016-03-13 22:00:07 +0000 | [diff] [blame] | 220 | runtime->AddCompilerOption("--debuggable"); |
David Srbecky | 8f99e0b | 2018-01-30 14:44:52 +0000 | [diff] [blame] | 221 | runtime_flags |= DEBUG_GENERATE_DEBUG_INFO; |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 222 | runtime->SetNativeDebuggable(true); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 223 | runtime_flags &= ~DEBUG_NATIVE_DEBUGGABLE; |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
David Srbecky | 8f99e0b | 2018-01-30 14:44:52 +0000 | [diff] [blame] | 226 | if ((runtime_flags & DEBUG_GENERATE_MINI_DEBUG_INFO) != 0) { |
| 227 | // Generate native minimal debug information to allow backtracing. |
| 228 | runtime->AddCompilerOption("--generate-mini-debug-info"); |
| 229 | runtime_flags &= ~DEBUG_GENERATE_MINI_DEBUG_INFO; |
| 230 | } |
| 231 | |
| 232 | if ((runtime_flags & DEBUG_GENERATE_DEBUG_INFO) != 0) { |
| 233 | // Generate all native debug information we can (e.g. line-numbers). |
| 234 | runtime->AddCompilerOption("--generate-debug-info"); |
| 235 | runtime_flags &= ~DEBUG_GENERATE_DEBUG_INFO; |
| 236 | } |
| 237 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 238 | return runtime_flags; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 242 | Runtime* runtime = Runtime::Current(); |
| 243 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 244 | |
| 245 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 246 | |
| 247 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 248 | return reinterpret_cast<jlong>(ThreadForEnv(env)); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 251 | static void ZygoteHooks_nativePostZygoteFork(JNIEnv*, jclass) { |
| 252 | Runtime* runtime = Runtime::Current(); |
| 253 | if (runtime->IsZygote()) { |
| 254 | runtime->PostZygoteFork(); |
| 255 | } |
| 256 | } |
| 257 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 258 | static void ZygoteHooks_nativePostForkSystemServer(JNIEnv* env ATTRIBUTE_UNUSED, |
| 259 | jclass klass ATTRIBUTE_UNUSED) { |
| 260 | // This JIT code cache for system server is created whilst the runtime is still single threaded. |
| 261 | // System server has a window where it can create executable pages for this purpose, but this is |
| 262 | // turned off after this hook. Consequently, the only JIT mode supported is the dual-view JIT |
| 263 | // where one mapping is R->RW and the other is RX. Single view requires RX->RWX->RX. |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 264 | if (Runtime::Current()->GetJit() != nullptr) { |
| 265 | Runtime::Current()->GetJit()->GetCodeCache()->PostForkChildAction( |
| 266 | /* is_system_server= */ true, /* is_zygote= */ false); |
| 267 | } |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 270 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, |
| 271 | jclass, |
| 272 | jlong token, |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 273 | jint runtime_flags, |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 274 | jboolean is_system_server, |
Robert Sesek | 77d3398 | 2018-01-17 18:39:04 -0500 | [diff] [blame] | 275 | jboolean is_zygote, |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 276 | jstring instruction_set) { |
Robert Sesek | 77d3398 | 2018-01-17 18:39:04 -0500 | [diff] [blame] | 277 | DCHECK(!(is_system_server && is_zygote)); |
| 278 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 279 | Thread* thread = reinterpret_cast<Thread*>(token); |
| 280 | // Our system thread ID, etc, has changed so reset Thread state. |
| 281 | thread->InitAfterFork(); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 282 | runtime_flags = EnableDebugFeatures(runtime_flags); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 283 | hiddenapi::EnforcementPolicy api_enforcement_policy = hiddenapi::EnforcementPolicy::kDisabled; |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 284 | |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 285 | Runtime* runtime = Runtime::Current(); |
| 286 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 287 | if ((runtime_flags & DISABLE_VERIFIER) != 0) { |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 288 | runtime->DisableVerifier(); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 289 | runtime_flags &= ~DISABLE_VERIFIER; |
| 290 | } |
| 291 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 292 | if ((runtime_flags & ONLY_USE_SYSTEM_OAT_FILES) != 0 || is_system_server) { |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 293 | runtime->GetOatFileManager().SetOnlyUseSystemOatFiles(); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 294 | runtime_flags &= ~ONLY_USE_SYSTEM_OAT_FILES; |
| 295 | } |
| 296 | |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 297 | api_enforcement_policy = hiddenapi::EnforcementPolicyFromInt( |
| 298 | (runtime_flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT); |
| 299 | runtime_flags &= ~HIDDEN_API_ENFORCEMENT_POLICY_MASK; |
David Brazdil | c5e5aed | 2018-01-17 18:26:30 +0000 | [diff] [blame] | 300 | |
Calin Juravle | 016fcbe2 | 2018-05-03 19:47:35 -0700 | [diff] [blame] | 301 | bool profile_system_server = (runtime_flags & PROFILE_SYSTEM_SERVER) == PROFILE_SYSTEM_SERVER; |
| 302 | runtime_flags &= ~PROFILE_SYSTEM_SERVER; |
| 303 | |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 304 | runtime->SetLoadAppImageStartupCacheEnabled( |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 305 | (runtime_flags & USE_APP_IMAGE_STARTUP_CACHE) != 0u); |
| 306 | runtime_flags &= ~USE_APP_IMAGE_STARTUP_CACHE; |
| 307 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 308 | if (runtime_flags != 0) { |
| 309 | LOG(ERROR) << StringPrintf("Unknown bits set in runtime_flags: %#x", runtime_flags); |
| 310 | } |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 311 | |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 312 | runtime->GetHeap()->PostForkChildAction(thread); |
| 313 | if (runtime->GetJit() != nullptr) { |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 314 | if (!is_system_server) { |
| 315 | // System server already called the JIT cache post fork action in `nativePostForkSystemServer`. |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 316 | runtime->GetJit()->GetCodeCache()->PostForkChildAction( |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 317 | /* is_system_server= */ false, is_zygote); |
| 318 | } |
| 319 | // This must be called after EnableDebugFeatures. |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 320 | runtime->GetJit()->PostForkChildAction(is_zygote); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 321 | } |
Mathieu Chartier | a98a282 | 2017-05-24 16:14:10 -0700 | [diff] [blame] | 322 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 323 | // Update tracing. |
| 324 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 325 | Trace::TraceOutputMode output_mode = Trace::GetOutputMode(); |
| 326 | Trace::TraceMode trace_mode = Trace::GetMode(); |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 327 | size_t buffer_size = Trace::GetBufferSize(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 328 | |
| 329 | // Just drop it. |
| 330 | Trace::Abort(); |
| 331 | |
| 332 | // Only restart if it was streaming mode. |
| 333 | // TODO: Expose buffer size, so we can also do file mode. |
| 334 | if (output_mode == Trace::TraceOutputMode::kStreaming) { |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 335 | static constexpr size_t kMaxProcessNameLength = 100; |
| 336 | char name_buf[kMaxProcessNameLength] = {}; |
| 337 | int rc = pthread_getname_np(pthread_self(), name_buf, kMaxProcessNameLength); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 338 | std::string proc_name; |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 339 | |
| 340 | if (rc == 0) { |
| 341 | // On success use the pthread name. |
| 342 | proc_name = name_buf; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 343 | } |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 344 | |
| 345 | if (proc_name.empty() || proc_name == "zygote" || proc_name == "zygote64") { |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 346 | // Either no process name, or the name hasn't been changed, yet. Just use pid. |
| 347 | pid_t pid = getpid(); |
| 348 | proc_name = StringPrintf("%u", static_cast<uint32_t>(pid)); |
| 349 | } |
| 350 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 351 | std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str()); |
| 352 | Trace::Start(trace_file.c_str(), |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 353 | buffer_size, |
| 354 | 0, // TODO: Expose flags. |
| 355 | output_mode, |
| 356 | trace_mode, |
| 357 | 0); // TODO: Expose interval. |
| 358 | if (thread->IsExceptionPending()) { |
| 359 | ScopedObjectAccess soa(env); |
| 360 | thread->ClearException(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 365 | bool do_hidden_api_checks = api_enforcement_policy != hiddenapi::EnforcementPolicy::kDisabled; |
Robert Sesek | 77d3398 | 2018-01-17 18:39:04 -0500 | [diff] [blame] | 366 | DCHECK(!(is_system_server && do_hidden_api_checks)) |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 367 | << "SystemServer should be forked with EnforcementPolicy::kDisable"; |
Robert Sesek | 77d3398 | 2018-01-17 18:39:04 -0500 | [diff] [blame] | 368 | DCHECK(!(is_zygote && do_hidden_api_checks)) |
Mathew Inwood | 597d7f6 | 2018-03-22 11:36:47 +0000 | [diff] [blame] | 369 | << "Child zygote processes should be forked with EnforcementPolicy::kDisable"; |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 370 | runtime->SetHiddenApiEnforcementPolicy(api_enforcement_policy); |
| 371 | runtime->SetDedupeHiddenApiWarnings(true); |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 372 | if (api_enforcement_policy != hiddenapi::EnforcementPolicy::kDisabled && |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 373 | runtime->GetHiddenApiEventLogSampleRate() != 0) { |
Mathew Inwood | 1fd97f2 | 2018-04-03 15:32:32 +0100 | [diff] [blame] | 374 | // Hidden API checks are enabled, and we are sampling access for the event log. Initialize the |
| 375 | // random seed, to ensure the sampling is actually random. We do this post-fork, as doing it |
| 376 | // pre-fork would result in the same sequence for every forked process. |
| 377 | std::srand(static_cast<uint32_t>(NanoTime())); |
| 378 | } |
David Brazdil | c5e5aed | 2018-01-17 18:26:30 +0000 | [diff] [blame] | 379 | |
Robert Sesek | 77d3398 | 2018-01-17 18:39:04 -0500 | [diff] [blame] | 380 | if (is_zygote) { |
| 381 | // If creating a child-zygote, do not call into the runtime's post-fork logic. |
| 382 | // Doing so would spin up threads for Binder and JDWP. Instead, the Java side |
| 383 | // of the child process will call a static main in a class specified by the parent. |
| 384 | return; |
| 385 | } |
| 386 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 387 | if (instruction_set != nullptr && !is_system_server) { |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 388 | ScopedUtfChars isa_string(env, instruction_set); |
| 389 | InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 390 | Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 391 | if (isa != InstructionSet::kNone && isa != kRuntimeISA) { |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 392 | action = Runtime::NativeBridgeAction::kInitialize; |
| 393 | } |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 394 | runtime->InitNonZygoteOrPostFork(env, is_system_server, action, isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 395 | } else { |
Andreas Gampe | e095c91 | 2019-03-14 13:38:49 -0700 | [diff] [blame^] | 396 | runtime->InitNonZygoteOrPostFork( |
Calin Juravle | 016fcbe2 | 2018-05-03 19:47:35 -0700 | [diff] [blame] | 397 | env, |
| 398 | is_system_server, |
| 399 | Runtime::NativeBridgeAction::kUnload, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 400 | /*isa=*/ nullptr, |
Calin Juravle | 016fcbe2 | 2018-05-03 19:47:35 -0700 | [diff] [blame] | 401 | profile_system_server); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 402 | } |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Andreas Gampe | f38a661 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 405 | static void ZygoteHooks_startZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED, |
| 406 | jclass klass ATTRIBUTE_UNUSED) { |
| 407 | Runtime::Current()->SetZygoteNoThreadSection(true); |
| 408 | } |
| 409 | |
| 410 | static void ZygoteHooks_stopZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED, |
| 411 | jclass klass ATTRIBUTE_UNUSED) { |
| 412 | Runtime::Current()->SetZygoteNoThreadSection(false); |
| 413 | } |
| 414 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 415 | static JNINativeMethod gMethods[] = { |
| 416 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 417 | NATIVE_METHOD(ZygoteHooks, nativePostZygoteFork, "()V"), |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 418 | NATIVE_METHOD(ZygoteHooks, nativePostForkSystemServer, "()V"), |
Robert Sesek | 77d3398 | 2018-01-17 18:39:04 -0500 | [diff] [blame] | 419 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZZLjava/lang/String;)V"), |
Andreas Gampe | f38a661 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 420 | NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"), |
| 421 | NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"), |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 425 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 426 | } |
| 427 | |
| 428 | } // namespace art |