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 ATTRIBUTE_UNUSED,jthread curthread ATTRIBUTE_UNUSED)53 JNICALL void VmInitCb(jvmtiEnv* jvmti,
54                       JNIEnv* env ATTRIBUTE_UNUSED,
55                       jthread curthread ATTRIBUTE_UNUSED) {
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 ATTRIBUTE_UNUSED,jthread thr ATTRIBUTE_UNUSED,jclass klass)74 JNICALL void ClassPrepareJit(jvmtiEnv* jvmti,
75                              JNIEnv* jni_env ATTRIBUTE_UNUSED,
76                              jthread thr ATTRIBUTE_UNUSED,
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 ATTRIBUTE_UNUSED)88 JNICALL void VMDeathCb(jvmtiEnv* jvmti, JNIEnv* env ATTRIBUTE_UNUSED) {
89   DataDumpRequestCb(jvmti);
90 }
91 
SetupJvmti(JavaVM * vm,const char * options)92 static jvmtiEnv* SetupJvmti(JavaVM* vm, const char* options) {
93   android::base::InitLogging(/* argv= */nullptr);
94 
95   jvmtiEnv* jvmti = nullptr;
96   if (vm->GetEnv(reinterpret_cast<void**>(&jvmti), JVMTI_VERSION_1_0) != JNI_OK &&
97       vm->GetEnv(reinterpret_cast<void**>(&jvmti), kArtTiVersion) != JNI_OK) {
98     LOG(FATAL) << "Unable to setup JVMTI environment!";
99   }
100   jvmtiEventCallbacks cb {
101         .VMInit = VmInitCb,
102         .ClassPrepare = ClassPrepareJit,
103         .DataDumpRequest = DataDumpRequestCb,
104         .VMDeath = VMDeathCb,
105   };
106   AgentOptions* ops;
107   CHECK_CALL_SUCCESS(
108       jvmti->Allocate(sizeof(AgentOptions), reinterpret_cast<unsigned char**>(&ops)));
109   ops->fatal = (strcmp(options, "fatal") == 0);
110   ops->cnt = 0;
111   CHECK_CALL_SUCCESS(jvmti->SetEnvironmentLocalStorage(ops));
112   CHECK_CALL_SUCCESS(jvmti->SetEventCallbacks(&cb, sizeof(cb)));
113   CHECK_CALL_SUCCESS(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, nullptr));
114   CHECK_CALL_SUCCESS(
115       jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, nullptr));
116   return jvmti;
117 }
118 
119 // Early attachment (e.g. 'java -agent[lib|path]:filename.so').
Agent_OnLoad(JavaVM * vm,char * options,void *)120 extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* /* reserved */) {
121   SetupJvmti(vm, options);
122   return JNI_OK;
123 }
124 
125 // Late attachment (e.g. 'am attach-agent').
Agent_OnAttach(JavaVM * vm,char * options,void *)126 extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *vm, char* options, void* /* reserved */) {
127   jvmtiEnv* jvmti = SetupJvmti(vm, options);
128 
129   JNIEnv* jni = nullptr;
130   jthread thr = nullptr;
131   CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&jni), JNI_VERSION_1_6));
132   CHECK_CALL_SUCCESS(jvmti->GetCurrentThread(&thr));
133 
134   // Final setup is done in the VmInitCb.
135   VmInitCb(jvmti, jni, thr);
136 
137   jni->DeleteLocalRef(thr);
138   return JNI_OK;
139 }
140 
141 #undef CHECK_CALL_SUCCESS
142 
143 }  // namespace jitload
144 
145