blob: 8395150fb67e02e17b5b4c2dcc327ce8e009e7af [file] [log] [blame]
Narayan Kamath8b2c8b92014-03-31 16:44:54 +01001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_ZygoteHooks.h"
18
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010019#include <stdlib.h>
20
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include "arch/instruction_set.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010022#include "debugger.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070023#include "java_vm_ext.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010024#include "jni_internal.h"
25#include "JNIHelp.h"
Andreas Gampe6be67ee2014-09-02 21:22:18 -070026#include "ScopedUtfChars.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010027#include "thread-inl.h"
28
Elliott Hughes0a18df82015-01-09 15:16:16 -080029#if defined(__linux__)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010030#include <sys/prctl.h>
31#endif
32
Narayan Kamathad4b0d22014-04-02 12:06:02 +010033#include <sys/resource.h>
34
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010035namespace art {
36
37static void EnableDebugger() {
Elliott Hughes0a18df82015-01-09 15:16:16 -080038#if defined(__linux__)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010039 // To let a non-privileged gdbserver attach to this
40 // process, we must set our dumpable flag.
41 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
42 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
43 }
Ian Rogersc5f17732014-06-05 20:48:42 -070044#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010045 // We don't want core dumps, though, so set the core dump size to 0.
46 rlimit rl;
47 rl.rlim_cur = 0;
48 rl.rlim_max = RLIM_INFINITY;
49 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
50 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
51 }
52}
53
54static void EnableDebugFeatures(uint32_t debug_flags) {
Ian Rogers68d8b422014-07-17 11:09:10 -070055 // Must match values in com.android.internal.os.Zygote.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010056 enum {
57 DEBUG_ENABLE_DEBUGGER = 1,
58 DEBUG_ENABLE_CHECKJNI = 1 << 1,
59 DEBUG_ENABLE_ASSERT = 1 << 2,
60 DEBUG_ENABLE_SAFEMODE = 1 << 3,
61 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070062 DEBUG_ENABLE_JIT = 1 << 5,
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010063 };
64
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070065 Runtime* const runtime = Runtime::Current();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010066 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010067 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070068 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070069 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010070 vm->SetCheckJniEnabled(true);
71 // There's only one thread running at this point, so only one JNIEnv to fix up.
72 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
73 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070074 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010075 }
76 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
77 }
78
79 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
80 gLogVerbosity.third_party_jni = true;
81 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
82 }
83
84 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
85 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
86 EnableDebugger();
87 }
88 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
89
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070090 const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0;
91 if (safe_mode) {
Andreas Gamped2abbc92014-12-19 09:53:27 -080092 // Ensure that any (secondary) oat files will be interpreted.
Andreas Gamped2abbc92014-12-19 09:53:27 -080093 runtime->AddCompilerOption("--compiler-filter=interpret-only");
94 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
95 }
96
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070097 if ((debug_flags & DEBUG_ENABLE_JIT) != 0) {
98 if (safe_mode) {
99 LOG(INFO) << "Not enabling JIT due to VM safe mode";
100 } else {
101 if (runtime->GetJit() == nullptr) {
102 runtime->CreateJit();
103 } else {
104 LOG(INFO) << "Not late-enabling JIT (already on)";
105 }
106 }
107 debug_flags &= ~DEBUG_ENABLE_JIT;
108 }
109
Andreas Gamped2abbc92014-12-19 09:53:27 -0800110 // This is for backwards compatibility with Dalvik.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100111 debug_flags &= ~DEBUG_ENABLE_ASSERT;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100112
113 if (debug_flags != 0) {
114 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
115 }
116}
117
118static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
119 Runtime* runtime = Runtime::Current();
120 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100121
122 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100123
124 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700125 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100126}
127
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700128static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,
129 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100130 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 Gampe6be67ee2014-09-02 21:22:18 -0700134
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700135 if (instruction_set != nullptr) {
136 ScopedUtfChars isa_string(env, instruction_set);
137 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400138 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700139 if (isa != kNone && isa != kRuntimeISA) {
140 action = Runtime::NativeBridgeAction::kInitialize;
141 }
jgu21a6da74e2014-09-10 06:57:17 -0400142 Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str());
143 } else {
144 Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700145 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100146}
147
148static JNINativeMethod gMethods[] = {
149 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700150 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100151};
152
153void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
154 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
155}
156
157} // namespace art