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 | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 27 | #include "ScopedUtfChars.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 28 | #include "thread-inl.h" |
| 29 | |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 30 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 31 | #include <sys/prctl.h> |
| 32 | #endif |
| 33 | |
Narayan Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 34 | #include <sys/resource.h> |
| 35 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 36 | namespace art { |
| 37 | |
| 38 | static void EnableDebugger() { |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 39 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 40 | // To let a non-privileged gdbserver attach to this |
| 41 | // process, we must set our dumpable flag. |
| 42 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 43 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 44 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 45 | #endif |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 46 | // We don't want core dumps, though, so set the core dump size to 0. |
| 47 | rlimit rl; |
| 48 | rl.rlim_cur = 0; |
| 49 | rl.rlim_max = RLIM_INFINITY; |
| 50 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 51 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void EnableDebugFeatures(uint32_t debug_flags) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 56 | // Must match values in com.android.internal.os.Zygote. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 57 | enum { |
| 58 | DEBUG_ENABLE_DEBUGGER = 1, |
| 59 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 60 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 61 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 62 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 63 | DEBUG_ENABLE_JIT = 1 << 5, |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 66 | Runtime* const runtime = Runtime::Current(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 67 | if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 68 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 69 | if (!vm->IsCheckJniEnabled()) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 70 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 71 | vm->SetCheckJniEnabled(true); |
| 72 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
| 73 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
| 74 | } else { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 75 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 76 | } |
| 77 | debug_flags &= ~DEBUG_ENABLE_CHECKJNI; |
| 78 | } |
| 79 | |
| 80 | if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
| 81 | gLogVerbosity.third_party_jni = true; |
| 82 | debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
| 83 | } |
| 84 | |
| 85 | Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); |
| 86 | if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { |
| 87 | EnableDebugger(); |
| 88 | } |
| 89 | debug_flags &= ~DEBUG_ENABLE_DEBUGGER; |
| 90 | |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 91 | const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0; |
| 92 | if (safe_mode) { |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 93 | // Ensure that any (secondary) oat files will be interpreted. |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 94 | runtime->AddCompilerOption("--compiler-filter=interpret-only"); |
| 95 | debug_flags &= ~DEBUG_ENABLE_SAFEMODE; |
| 96 | } |
| 97 | |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame^] | 98 | bool use_jit = false; |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 99 | if ((debug_flags & DEBUG_ENABLE_JIT) != 0) { |
| 100 | if (safe_mode) { |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame^] | 101 | LOG(INFO) << "Not enabling JIT due to safe mode"; |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 102 | } else { |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame^] | 103 | use_jit = true; |
| 104 | LOG(INFO) << "Late-enabling JIT"; |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 105 | } |
| 106 | debug_flags &= ~DEBUG_ENABLE_JIT; |
| 107 | } |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame^] | 108 | runtime->GetJITOptions()->SetUseJIT(use_jit); |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 109 | |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 110 | // This is for backwards compatibility with Dalvik. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 111 | debug_flags &= ~DEBUG_ENABLE_ASSERT; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 112 | |
| 113 | if (debug_flags != 0) { |
| 114 | LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 119 | Runtime* runtime = Runtime::Current(); |
| 120 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 121 | |
| 122 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 123 | |
| 124 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 125 | return reinterpret_cast<jlong>(ThreadForEnv(env)); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 128 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags, |
| 129 | jstring instruction_set) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 130 | Thread* thread = reinterpret_cast<Thread*>(token); |
| 131 | // Our system thread ID, etc, has changed so reset Thread state. |
| 132 | thread->InitAfterFork(); |
| 133 | EnableDebugFeatures(debug_flags); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 134 | |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 135 | if (instruction_set != nullptr) { |
| 136 | ScopedUtfChars isa_string(env, instruction_set); |
| 137 | InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 138 | Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 139 | if (isa != kNone && isa != kRuntimeISA) { |
| 140 | action = Runtime::NativeBridgeAction::kInitialize; |
| 141 | } |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 142 | Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str()); |
| 143 | } else { |
| 144 | Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 145 | } |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static JNINativeMethod gMethods[] = { |
| 149 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 150 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"), |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 154 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 155 | } |
| 156 | |
| 157 | } // namespace art |