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 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 21 | #include "arch/instruction_set.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 22 | #include "debugger.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 23 | #include "java_vm_ext.h" |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 24 | #include "jit/jit.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 25 | #include "jni_internal.h" |
| 26 | #include "JNIHelp.h" |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 27 | #include "scoped_thread_state_change.h" |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 28 | #include "ScopedUtfChars.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 29 | #include "thread-inl.h" |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 30 | #include "trace.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 31 | |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 32 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 33 | #include <sys/prctl.h> |
| 34 | #endif |
| 35 | |
Narayan Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 36 | #include <sys/resource.h> |
| 37 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 38 | namespace art { |
| 39 | |
| 40 | static void EnableDebugger() { |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 41 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 42 | // To let a non-privileged gdbserver attach to this |
| 43 | // process, we must set our dumpable flag. |
| 44 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 45 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 46 | } |
Oleksiy Vyalov | de9007f | 2016-06-21 16:21:37 -0700 | [diff] [blame] | 47 | |
| 48 | // Even if Yama is on a non-privileged native debugger should |
| 49 | // be able to attach to the debuggable app. |
| 50 | if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) { |
| 51 | // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this |
| 52 | // case since it's expected behaviour. |
| 53 | if (errno != EINVAL) { |
| 54 | PLOG(ERROR) << "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid " << getpid(); |
| 55 | } |
| 56 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 57 | #endif |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 58 | // We don't want core dumps, though, so set the core dump size to 0. |
| 59 | rlimit rl; |
| 60 | rl.rlim_cur = 0; |
| 61 | rl.rlim_max = RLIM_INFINITY; |
| 62 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 63 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void EnableDebugFeatures(uint32_t debug_flags) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 68 | // Must match values in com.android.internal.os.Zygote. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 69 | enum { |
| 70 | DEBUG_ENABLE_DEBUGGER = 1, |
| 71 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 72 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 73 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 74 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
Nicolas Geoffray | 0f042e0 | 2015-11-05 11:32:24 +0000 | [diff] [blame] | 75 | DEBUG_GENERATE_DEBUG_INFO = 1 << 5, |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 76 | DEBUG_ALWAYS_JIT = 1 << 6, |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 77 | DEBUG_NATIVE_DEBUGGABLE = 1 << 7, |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 80 | Runtime* const runtime = Runtime::Current(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 81 | if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 82 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 83 | if (!vm->IsCheckJniEnabled()) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 84 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 85 | vm->SetCheckJniEnabled(true); |
| 86 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
| 87 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
| 88 | } else { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 89 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 90 | } |
| 91 | debug_flags &= ~DEBUG_ENABLE_CHECKJNI; |
| 92 | } |
| 93 | |
| 94 | if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
| 95 | gLogVerbosity.third_party_jni = true; |
| 96 | debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
| 97 | } |
| 98 | |
| 99 | Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); |
| 100 | if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { |
| 101 | EnableDebugger(); |
| 102 | } |
| 103 | debug_flags &= ~DEBUG_ENABLE_DEBUGGER; |
| 104 | |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 105 | const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0; |
| 106 | if (safe_mode) { |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 107 | // Ensure that any (secondary) oat files will be interpreted. |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 108 | runtime->AddCompilerOption("--compiler-filter=interpret-only"); |
Nicolas Geoffray | 0f042e0 | 2015-11-05 11:32:24 +0000 | [diff] [blame] | 109 | runtime->SetSafeMode(true); |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 110 | debug_flags &= ~DEBUG_ENABLE_SAFEMODE; |
| 111 | } |
| 112 | |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 113 | const bool generate_debug_info = (debug_flags & DEBUG_GENERATE_DEBUG_INFO) != 0; |
| 114 | if (generate_debug_info) { |
| 115 | runtime->AddCompilerOption("--generate-debug-info"); |
| 116 | debug_flags &= ~DEBUG_GENERATE_DEBUG_INFO; |
Andreas Gampe | 00bb878 | 2015-04-24 16:33:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 119 | // This is for backwards compatibility with Dalvik. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 120 | debug_flags &= ~DEBUG_ENABLE_ASSERT; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 121 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 122 | if ((debug_flags & DEBUG_ALWAYS_JIT) != 0) { |
| 123 | jit::JitOptions* jit_options = runtime->GetJITOptions(); |
| 124 | CHECK(jit_options != nullptr); |
| 125 | jit_options->SetJitAtFirstUse(); |
| 126 | debug_flags &= ~DEBUG_ALWAYS_JIT; |
| 127 | } |
| 128 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 129 | if ((debug_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) { |
David Srbecky | 346dc99 | 2016-03-13 22:00:07 +0000 | [diff] [blame] | 130 | runtime->AddCompilerOption("--debuggable"); |
| 131 | runtime->AddCompilerOption("--generate-debug-info"); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 132 | runtime->SetNativeDebuggable(true); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 133 | debug_flags &= ~DEBUG_NATIVE_DEBUGGABLE; |
| 134 | } |
| 135 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 136 | if (debug_flags != 0) { |
| 137 | LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 142 | Runtime* runtime = Runtime::Current(); |
| 143 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 144 | |
| 145 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 146 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 147 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 148 | // Tracing active, pause it. |
| 149 | Trace::Pause(); |
| 150 | } |
| 151 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 152 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 153 | return reinterpret_cast<jlong>(ThreadForEnv(env)); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 156 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, |
| 157 | jclass, |
| 158 | jlong token, |
| 159 | jint debug_flags, |
| 160 | jboolean is_system_server, |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 161 | jstring instruction_set) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 162 | Thread* thread = reinterpret_cast<Thread*>(token); |
| 163 | // Our system thread ID, etc, has changed so reset Thread state. |
| 164 | thread->InitAfterFork(); |
| 165 | EnableDebugFeatures(debug_flags); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 166 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 167 | // Update tracing. |
| 168 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 169 | Trace::TraceOutputMode output_mode = Trace::GetOutputMode(); |
| 170 | Trace::TraceMode trace_mode = Trace::GetMode(); |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 171 | size_t buffer_size = Trace::GetBufferSize(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 172 | |
| 173 | // Just drop it. |
| 174 | Trace::Abort(); |
| 175 | |
| 176 | // Only restart if it was streaming mode. |
| 177 | // TODO: Expose buffer size, so we can also do file mode. |
| 178 | if (output_mode == Trace::TraceOutputMode::kStreaming) { |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 179 | static constexpr size_t kMaxProcessNameLength = 100; |
| 180 | char name_buf[kMaxProcessNameLength] = {}; |
| 181 | int rc = pthread_getname_np(pthread_self(), name_buf, kMaxProcessNameLength); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 182 | std::string proc_name; |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 183 | |
| 184 | if (rc == 0) { |
| 185 | // On success use the pthread name. |
| 186 | proc_name = name_buf; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 187 | } |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 188 | |
| 189 | if (proc_name.empty() || proc_name == "zygote" || proc_name == "zygote64") { |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 190 | // Either no process name, or the name hasn't been changed, yet. Just use pid. |
| 191 | pid_t pid = getpid(); |
| 192 | proc_name = StringPrintf("%u", static_cast<uint32_t>(pid)); |
| 193 | } |
| 194 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 195 | std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str()); |
| 196 | Trace::Start(trace_file.c_str(), |
| 197 | -1, |
| 198 | buffer_size, |
| 199 | 0, // TODO: Expose flags. |
| 200 | output_mode, |
| 201 | trace_mode, |
| 202 | 0); // TODO: Expose interval. |
| 203 | if (thread->IsExceptionPending()) { |
| 204 | ScopedObjectAccess soa(env); |
| 205 | thread->ClearException(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 210 | if (instruction_set != nullptr && !is_system_server) { |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 211 | ScopedUtfChars isa_string(env, instruction_set); |
| 212 | InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 213 | Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 214 | if (isa != kNone && isa != kRuntimeISA) { |
| 215 | action = Runtime::NativeBridgeAction::kInitialize; |
| 216 | } |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 217 | Runtime::Current()->InitNonZygoteOrPostFork( |
| 218 | env, is_system_server, action, isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 219 | } else { |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 220 | Runtime::Current()->InitNonZygoteOrPostFork( |
| 221 | env, is_system_server, Runtime::NativeBridgeAction::kUnload, nullptr); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 222 | } |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Andreas Gampe | f38a661 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 225 | static void ZygoteHooks_startZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED, |
| 226 | jclass klass ATTRIBUTE_UNUSED) { |
| 227 | Runtime::Current()->SetZygoteNoThreadSection(true); |
| 228 | } |
| 229 | |
| 230 | static void ZygoteHooks_stopZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED, |
| 231 | jclass klass ATTRIBUTE_UNUSED) { |
| 232 | Runtime::Current()->SetZygoteNoThreadSection(false); |
| 233 | } |
| 234 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 235 | static JNINativeMethod gMethods[] = { |
| 236 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 237 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZLjava/lang/String;)V"), |
Andreas Gampe | f38a661 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 238 | NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"), |
| 239 | NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"), |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 243 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 244 | } |
| 245 | |
| 246 | } // namespace art |