blob: 09669543a8e64ebfbfa80dafa33f2f17b808d15f [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
21#include "debugger.h"
Andreas Gampe6be67ee2014-09-02 21:22:18 -070022#include "instruction_set.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
29#if defined(HAVE_PRCTL)
30#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() {
38 // To let a non-privileged gdbserver attach to this
39 // process, we must set our dumpable flag.
Ian Rogersc5f17732014-06-05 20:48:42 -070040#if defined(HAVE_PRCTL)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010041 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
89 // These two are for backwards compatibility with Dalvik.
90 debug_flags &= ~DEBUG_ENABLE_ASSERT;
91 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
92
93 if (debug_flags != 0) {
94 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
95 }
96}
97
98static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
99 Runtime* runtime = Runtime::Current();
100 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100101
102 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100103
104 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700105 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100106}
107
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700108static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,
109 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100110 Thread* thread = reinterpret_cast<Thread*>(token);
111 // Our system thread ID, etc, has changed so reset Thread state.
112 thread->InitAfterFork();
113 EnableDebugFeatures(debug_flags);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700114
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700115 if (instruction_set != nullptr) {
116 ScopedUtfChars isa_string(env, instruction_set);
117 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400118 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700119 if (isa != kNone && isa != kRuntimeISA) {
120 action = Runtime::NativeBridgeAction::kInitialize;
121 }
jgu21a6da74e2014-09-10 06:57:17 -0400122 Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str());
123 } else {
124 Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700125 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100126}
127
128static JNINativeMethod gMethods[] = {
129 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700130 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100131};
132
133void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
134 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
135}
136
137} // namespace art