1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 #include <android-base/logging.h>
17 #include <jni.h>
18 #include <jvmti.h>
19
20 #include "base/runtime_debug.h"
21 #include "jit/jit.h"
22 #include "runtime-inl.h"
23 #include "scoped_thread_state_change-inl.h"
24 #include "thread-inl.h"
25 #include "thread_list.h"
26
27 namespace jitload {
28
29 // Special env version that allows JVMTI-like access on userdebug builds.
30 static constexpr jint kArtTiVersion = JVMTI_VERSION_1_2 | 0x40000000;
31
32 #define CHECK_CALL_SUCCESS(c) \
33 do { \
34 auto vc = (c); \
35 CHECK(vc == JNI_OK || vc == JVMTI_ERROR_NONE) << "call " << #c << " did not succeed\n"; \
36 } while (false)
37
GetJitThread()38 static jthread GetJitThread() {
39 art::ScopedObjectAccess soa(art::Thread::Current());
40 auto* jit = art::Runtime::Current()->GetJit();
41 if (jit == nullptr) {
42 return nullptr;
43 }
44 auto* thread_pool = jit->GetThreadPool();
45 if (thread_pool == nullptr) {
46 return nullptr;
47 }
48 // Currently we only have a single jit thread so we only look at that one.
49 return soa.AddLocalReference<jthread>(
50 thread_pool->GetWorkers()[0]->GetThread()->GetPeerFromOtherThread());
51 }
52
VmInitCb(jvmtiEnv * jvmti,JNIEnv * env,jthread curthread)53 JNICALL void VmInitCb(jvmtiEnv* jvmti,
54 [[maybe_unused]] JNIEnv* env,
55 [[maybe_unused]] jthread curthread) {
56 jthread jit_thread = GetJitThread();
57 if (jit_thread != nullptr) {
58 CHECK_EQ(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, jit_thread),
59 JVMTI_ERROR_NONE);
60 }
61 }
62
63 struct AgentOptions {
64 bool fatal;
65 uint64_t cnt;
66 };
67
DataDumpRequestCb(jvmtiEnv * jvmti)68 JNICALL static void DataDumpRequestCb(jvmtiEnv* jvmti) {
69 AgentOptions* ops;
70 CHECK_CALL_SUCCESS(jvmti->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&ops)));
71 LOG(WARNING) << "Jit thread has loaded " << ops->cnt << " classes";
72 }
73
ClassPrepareJit(jvmtiEnv * jvmti,JNIEnv * jni_env,jthread thr,jclass klass)74 JNICALL void ClassPrepareJit(jvmtiEnv* jvmti,
75 [[maybe_unused]] JNIEnv* jni_env,
76 [[maybe_unused]] jthread thr,
77 jclass klass) {
78 AgentOptions* ops;
79 CHECK_CALL_SUCCESS(jvmti->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&ops)));
80 char* klass_name;
81 CHECK_CALL_SUCCESS(jvmti->GetClassSignature(klass, &klass_name, nullptr));
82 (ops->fatal ? LOG_STREAM(FATAL)
83 : LOG_STREAM(WARNING)) << "Loaded " << klass_name << " on jit thread!";
84 ops->cnt++;
85 CHECK_CALL_SUCCESS(jvmti->Deallocate(reinterpret_cast<unsigned char*>(klass_name)));
86 }
87
VMDeathCb(jvmtiEnv * jvmti,JNIEnv * env)88 JNICALL void VMDeathCb(jvmtiEnv* jvmti, [[maybe_unused]] JNIEnv* env) { DataDumpRequestCb(jvmti); }
89
SetupJvmti(JavaVM * vm,const char * options)90 static jvmtiEnv* SetupJvmti(JavaVM* vm, const char* options) {
91 android::base::InitLogging(/* argv= */nullptr);
92
93 jvmtiEnv* jvmti = nullptr;
94 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti), JVMTI_VERSION_1_0) != JNI_OK &&
95 vm->GetEnv(reinterpret_cast<void**>(&jvmti), kArtTiVersion) != JNI_OK) {
96 LOG(FATAL) << "Unable to setup JVMTI environment!";
97 }
98 jvmtiEventCallbacks cb {
99 .VMInit = VmInitCb,
100 .VMDeath = VMDeathCb,
101 .ClassPrepare = ClassPrepareJit,
102 .DataDumpRequest = DataDumpRequestCb,
103 };
104 AgentOptions* ops;
105 CHECK_CALL_SUCCESS(
106 jvmti->Allocate(sizeof(AgentOptions), reinterpret_cast<unsigned char**>(&ops)));
107 ops->fatal = (strcmp(options, "fatal") == 0);
108 ops->cnt = 0;
109 CHECK_CALL_SUCCESS(jvmti->SetEnvironmentLocalStorage(ops));
110 CHECK_CALL_SUCCESS(jvmti->SetEventCallbacks(&cb, sizeof(cb)));
111 CHECK_CALL_SUCCESS(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, nullptr));
112 CHECK_CALL_SUCCESS(
113 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, nullptr));
114 return jvmti;
115 }
116
117 // Early attachment (e.g. 'java -agent[lib|path]:filename.so').
Agent_OnLoad(JavaVM * vm,char * options,void *)118 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* /* reserved */) {
119 SetupJvmti(vm, options);
120 return JNI_OK;
121 }
122
123 // Late attachment (e.g. 'am attach-agent').
Agent_OnAttach(JavaVM * vm,char * options,void *)124 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *vm, char* options, void* /* reserved */) {
125 jvmtiEnv* jvmti = SetupJvmti(vm, options);
126
127 JNIEnv* jni = nullptr;
128 jthread thr = nullptr;
129 CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&jni), JNI_VERSION_1_6));
130 CHECK_CALL_SUCCESS(jvmti->GetCurrentThread(&thr));
131
132 // Final setup is done in the VmInitCb.
133 VmInitCb(jvmti, jni, thr);
134
135 jni->DeleteLocalRef(thr);
136 return JNI_OK;
137 }
138
139 #undef CHECK_CALL_SUCCESS
140
141 } // namespace jitload
142
143