1 /*
2 * Copyright (C) 2017 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 <inttypes.h>
18 #include <pthread.h>
19 #include <sched.h>
20
21 #include "android-base/logging.h"
22 #include "android-base/macros.h"
23 #include "jni.h"
24 #include "jvmti.h"
25 #include "scoped_local_ref.h"
26
27 // Test infrastructure
28 #include "jvmti_helper.h"
29 #include "test_env.h"
30
31 namespace art {
32 namespace Test930AgentThread {
33
34 struct AgentData {
AgentDataart::Test930AgentThread::AgentData35 AgentData() : main_thread(nullptr),
36 jvmti_env(nullptr),
37 priority(0) {
38 }
39
40 jthread main_thread;
41 jvmtiEnv* jvmti_env;
42 pthread_barrier_t b;
43 jint priority;
44 };
45
AgentMain(jvmtiEnv * jenv,JNIEnv * env,void * arg)46 static void AgentMain(jvmtiEnv* jenv, JNIEnv* env, void* arg) {
47 AgentData* data = reinterpret_cast<AgentData*>(arg);
48
49 // Check some basics.
50 // This thread is not the main thread.
51 jthread this_thread;
52 jvmtiError this_thread_result = jenv->GetCurrentThread(&this_thread);
53 CheckJvmtiError(jenv, this_thread_result);
54 CHECK(!env->IsSameObject(this_thread, data->main_thread));
55
56 // The thread is a daemon.
57 jvmtiThreadInfo info;
58 jvmtiError info_result = jenv->GetThreadInfo(this_thread, &info);
59 CheckJvmtiError(jenv, info_result);
60 CHECK(info.is_daemon);
61 CheckJvmtiError(jenv, jenv->Deallocate(reinterpret_cast<unsigned char*>(info.name)));
62 if (info.thread_group != nullptr) {
63 env->DeleteLocalRef(info.thread_group);
64 }
65 if (info.context_class_loader != nullptr) {
66 env->DeleteLocalRef(info.context_class_loader);
67 }
68
69 // The thread has the requested priority.
70 // TODO: Our thread priorities do not work on the host.
71 // CHECK_EQ(info.priority, data->priority);
72
73 // Check further parts of the thread:
74 jint thread_count;
75 jthread* threads;
76 jvmtiError threads_result = jenv->GetAllThreads(&thread_count, &threads);
77 CheckJvmtiError(jenv, threads_result);
78 bool found = false;
79 for (jint i = 0; i != thread_count; ++i) {
80 if (env->IsSameObject(threads[i], this_thread)) {
81 found = true;
82 break;
83 }
84 }
85 CHECK(found);
86
87 // Done, let the main thread progress.
88 int wait_result = pthread_barrier_wait(&data->b);
89 CHECK(wait_result == PTHREAD_BARRIER_SERIAL_THREAD || wait_result == 0);
90 }
91
Java_art_Test931_testAgentThread(JNIEnv * env,jclass Main_klass)92 extern "C" JNIEXPORT void JNICALL Java_art_Test931_testAgentThread(
93 JNIEnv* env, [[maybe_unused]] jclass Main_klass) {
94 // Create a Thread object.
95 ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF("Agent Thread"));
96 if (thread_name.get() == nullptr) {
97 return;
98 }
99
100 ScopedLocalRef<jclass> thread_klass(env, env->FindClass("java/lang/Thread"));
101 if (thread_klass.get() == nullptr) {
102 return;
103 }
104 ScopedLocalRef<jobject> thread(env, env->AllocObject(thread_klass.get()));
105 if (thread.get() == nullptr) {
106 return;
107 }
108
109 // Get a ThreadGroup from the current thread. We need a non-null one as we're gonna call a
110 // runtime-only constructor (so we can set priority and daemon state).
111 jvmtiThreadInfo cur_thread_info;
112 jvmtiError info_result = jvmti_env->GetThreadInfo(nullptr, &cur_thread_info);
113 if (JvmtiErrorToException(env, jvmti_env, info_result)) {
114 return;
115 }
116 CheckJvmtiError(jvmti_env,
117 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(cur_thread_info.name)));
118 ScopedLocalRef<jobject> thread_group(env, cur_thread_info.thread_group);
119 if (cur_thread_info.context_class_loader != nullptr) {
120 env->DeleteLocalRef(cur_thread_info.context_class_loader);
121 }
122
123 jmethodID initID = env->GetMethodID(
124 thread_klass.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V");
125 if (initID == nullptr) {
126 return;
127 }
128 env->CallNonvirtualVoidMethod(
129 thread.get(), thread_klass.get(), initID, thread_group.get(), thread_name.get());
130 if (env->ExceptionCheck()) {
131 return;
132 }
133
134 jthread main_thread;
135 jvmtiError main_thread_result = jvmti_env->GetCurrentThread(&main_thread);
136 if (JvmtiErrorToException(env, jvmti_env, main_thread_result)) {
137 return;
138 }
139
140 AgentData data;
141 data.main_thread = env->NewGlobalRef(main_thread);
142 data.jvmti_env = jvmti_env;
143 data.priority = JVMTI_THREAD_MIN_PRIORITY;
144 CHECK_EQ(0, pthread_barrier_init(&data.b, nullptr, 2));
145
146 jvmtiError result = jvmti_env->RunAgentThread(thread.get(), AgentMain, &data, data.priority);
147 if (JvmtiErrorToException(env, jvmti_env, result)) {
148 return;
149 }
150
151 int wait_result = pthread_barrier_wait(&data.b);
152 CHECK(wait_result == PTHREAD_BARRIER_SERIAL_THREAD || wait_result == 0);
153
154 // Scheduling may mean that the agent thread is put to sleep. Wait until it's dead in an effort
155 // to not unload the plugin and crash.
156 for (;;) {
157 sleep(1);
158 jint thread_state;
159 jvmtiError state_result = jvmti_env->GetThreadState(thread.get(), &thread_state);
160 if (JvmtiErrorToException(env, jvmti_env, state_result)) {
161 return;
162 }
163 if (thread_state == 0 || // Was never alive.
164 (thread_state & JVMTI_THREAD_STATE_TERMINATED) != 0) { // Was alive and died.
165 break;
166 }
167 }
168 // Yield and sleep a bit more, to give the plugin time to tear down the native thread structure.
169 sched_yield();
170 sleep(1);
171
172 env->DeleteGlobalRef(data.main_thread);
173
174 pthread_barrier_destroy(&data.b);
175 }
176
177 } // namespace Test930AgentThread
178 } // namespace art
179