blob: e4691262066f77f2044eeb95592e5b724d832a0e [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
17#include <stdlib.h>
18
19#include "debugger.h"
Andreas Gampe6be67ee2014-09-02 21:22:18 -070020#include "instruction_set.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070021#include "java_vm_ext.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010022#include "jni_internal.h"
23#include "JNIHelp.h"
Andreas Gampe6be67ee2014-09-02 21:22:18 -070024#include "ScopedUtfChars.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010025#include "thread-inl.h"
26
27#if defined(HAVE_PRCTL)
28#include <sys/prctl.h>
29#endif
30
Narayan Kamathad4b0d22014-04-02 12:06:02 +010031#include <sys/resource.h>
32
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010033namespace art {
34
35static void EnableDebugger() {
36 // To let a non-privileged gdbserver attach to this
37 // process, we must set our dumpable flag.
Ian Rogersc5f17732014-06-05 20:48:42 -070038#if defined(HAVE_PRCTL)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010039 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
40 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
41 }
Ian Rogersc5f17732014-06-05 20:48:42 -070042#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010043 // We don't want core dumps, though, so set the core dump size to 0.
44 rlimit rl;
45 rl.rlim_cur = 0;
46 rl.rlim_max = RLIM_INFINITY;
47 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
48 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
49 }
50}
51
52static void EnableDebugFeatures(uint32_t debug_flags) {
Ian Rogers68d8b422014-07-17 11:09:10 -070053 // Must match values in com.android.internal.os.Zygote.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010054 enum {
55 DEBUG_ENABLE_DEBUGGER = 1,
56 DEBUG_ENABLE_CHECKJNI = 1 << 1,
57 DEBUG_ENABLE_ASSERT = 1 << 2,
58 DEBUG_ENABLE_SAFEMODE = 1 << 3,
59 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
60 };
61
62 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
63 Runtime* runtime = Runtime::Current();
64 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070065 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070066 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010067 vm->SetCheckJniEnabled(true);
68 // There's only one thread running at this point, so only one JNIEnv to fix up.
69 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
70 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070071 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010072 }
73 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
74 }
75
76 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
77 gLogVerbosity.third_party_jni = true;
78 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
79 }
80
81 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
82 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
83 EnableDebugger();
84 }
85 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
86
87 // These two are for backwards compatibility with Dalvik.
88 debug_flags &= ~DEBUG_ENABLE_ASSERT;
89 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
90
91 if (debug_flags != 0) {
92 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
93 }
94}
95
96static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
97 Runtime* runtime = Runtime::Current();
98 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +010099
100 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100101
102 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
103 Thread* self = Thread::Current();
104 return reinterpret_cast<jlong>(self);
105}
106
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700107static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,
108 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100109 Thread* thread = reinterpret_cast<Thread*>(token);
110 // Our system thread ID, etc, has changed so reset Thread state.
111 thread->InitAfterFork();
112 EnableDebugFeatures(debug_flags);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700113
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700114 if (instruction_set != nullptr) {
115 ScopedUtfChars isa_string(env, instruction_set);
116 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400117 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700118 if (isa != kNone && isa != kRuntimeISA) {
119 action = Runtime::NativeBridgeAction::kInitialize;
120 }
jgu21a6da74e2014-09-10 06:57:17 -0400121 Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str());
122 } else {
123 Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700124 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100125}
126
127static JNINativeMethod gMethods[] = {
128 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700129 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100130};
131
132void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
133 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
134}
135
136} // namespace art