blob: c056adc82efcb0071786577fdc075826f398cf86 [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,
62 };
63
64 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
65 Runtime* runtime = Runtime::Current();
66 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070067 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070068 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010069 vm->SetCheckJniEnabled(true);
70 // There's only one thread running at this point, so only one JNIEnv to fix up.
71 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
72 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070073 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010074 }
75 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
76 }
77
78 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
79 gLogVerbosity.third_party_jni = true;
80 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
81 }
82
83 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
84 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
85 EnableDebugger();
86 }
87 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
88
Andreas Gamped2abbc92014-12-19 09:53:27 -080089 if ((debug_flags & DEBUG_ENABLE_SAFEMODE) != 0) {
90 // Ensure that any (secondary) oat files will be interpreted.
91 Runtime* runtime = Runtime::Current();
92 runtime->AddCompilerOption("--compiler-filter=interpret-only");
93 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
94 }
95
96 // This is for backwards compatibility with Dalvik.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010097 debug_flags &= ~DEBUG_ENABLE_ASSERT;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010098
99 if (debug_flags != 0) {
100 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
101 }
102}
103
104static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
105 Runtime* runtime = Runtime::Current();
106 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100107
108 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100109
110 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700111 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100112}
113
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700114static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,
115 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100116 Thread* thread = reinterpret_cast<Thread*>(token);
117 // Our system thread ID, etc, has changed so reset Thread state.
118 thread->InitAfterFork();
119 EnableDebugFeatures(debug_flags);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700120
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700121 if (instruction_set != nullptr) {
122 ScopedUtfChars isa_string(env, instruction_set);
123 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400124 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700125 if (isa != kNone && isa != kRuntimeISA) {
126 action = Runtime::NativeBridgeAction::kInitialize;
127 }
jgu21a6da74e2014-09-10 06:57:17 -0400128 Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str());
129 } else {
130 Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700131 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100132}
133
134static JNINativeMethod gMethods[] = {
135 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700136 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100137};
138
139void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
140 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
141}
142
143} // namespace art