ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <stdio.h>
 
#include <mutex>
#include <string>
#include <vector>
 
#include "android-base/logging.h"
#include "android-base/stringprintf.h"
#include "jni.h"
#include "jvmti.h"
#include "scoped_local_ref.h"
 
// Test infrastructure
#include "jni_helper.h"
#include "jvmti_helper.h"
#include "test_env.h"
#include "ti_macros.h"
 
namespace art {
namespace Test924Threads {
 
struct WaiterStruct {
  std::atomic<bool> started;
  std::atomic<bool> finish;
};
 
extern "C" JNIEXPORT jlong JNICALL Java_art_Test924_nativeWaiterStructAlloc(
    JNIEnv* env, jclass TestClass ATTRIBUTE_UNUSED) {
  WaiterStruct* s = nullptr;
  if (JvmtiErrorToException(env,
                            jvmti_env,
                            jvmti_env->Allocate(sizeof(WaiterStruct),
                                                reinterpret_cast<unsigned char**>(&s)))) {
    return 0;
  }
  s->started = false;
  s->finish = false;
  return static_cast<jlong>(reinterpret_cast<intptr_t>(s));
}
 
extern "C" JNIEXPORT void JNICALL Java_art_Test924_nativeWaiterStructWaitForNative(
    JNIEnv* env ATTRIBUTE_UNUSED, jclass TestClass ATTRIBUTE_UNUSED, jlong waiter_struct) {
  WaiterStruct* s = reinterpret_cast<WaiterStruct*>(static_cast<intptr_t>(waiter_struct));
  while (!s->started) { }
}
 
extern "C" JNIEXPORT void JNICALL Java_art_Test924_nativeWaiterStructFinish(
    JNIEnv* env ATTRIBUTE_UNUSED, jclass TestClass ATTRIBUTE_UNUSED, jlong waiter_struct) {
  WaiterStruct* s = reinterpret_cast<WaiterStruct*>(static_cast<intptr_t>(waiter_struct));
  s->finish = true;
}
 
extern "C" JNIEXPORT void JNICALL Java_art_Test924_nativeLoop(JNIEnv* env,
                                                              jclass TestClass ATTRIBUTE_UNUSED,
                                                              jlong waiter_struct) {
  WaiterStruct* s = reinterpret_cast<WaiterStruct*>(static_cast<intptr_t>(waiter_struct));
  s->started = true;
  while (!s->finish) { }
  JvmtiErrorToException(env, jvmti_env, jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(s)));
}
 
// private static native Thread getCurrentThread();
// private static native Object[] getThreadInfo(Thread t);
 
extern "C" JNIEXPORT jthread JNICALL Java_art_Test924_getCurrentThread(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
  jthread thread = nullptr;
  jvmtiError result = jvmti_env->GetCurrentThread(&thread);
  if (JvmtiErrorToException(env, jvmti_env, result)) {
    return nullptr;
  }
  return thread;
}
 
extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test924_getThreadInfo(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) {
  jvmtiThreadInfo info;
  memset(&info, 0, sizeof(jvmtiThreadInfo));
 
  jvmtiError result = jvmti_env->GetThreadInfo(thread, &info);
  if (JvmtiErrorToException(env, jvmti_env, result)) {
    return nullptr;
  }
 
  auto callback = [&](jint component_index) -> jobject {
    switch (component_index) {
      // The name.
      case 0:
        return (info.name == nullptr) ? nullptr : env->NewStringUTF(info.name);
 
      // The priority. Use a string for simplicity of construction.
      case 1:
        return env->NewStringUTF(android::base::StringPrintf("%d", info.priority).c_str());
 
      // Whether it's a daemon. Use a string for simplicity of construction.
      case 2:
        return env->NewStringUTF(info.is_daemon == JNI_TRUE ? "true" : "false");
 
      // The thread group;
      case 3:
        return env->NewLocalRef(info.thread_group);
 
      // The context classloader.
      case 4:
        return env->NewLocalRef(info.context_class_loader);
    }
    LOG(FATAL) << "Should not reach here";
    UNREACHABLE();
  };
  jobjectArray ret = CreateObjectArray(env, 5, "java/lang/Object", callback);
 
  jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(info.name));
  if (info.thread_group != nullptr) {
    env->DeleteLocalRef(info.thread_group);
  }
  if (info.context_class_loader != nullptr) {
    env->DeleteLocalRef(info.context_class_loader);
  }
 
  return ret;
}
 
extern "C" JNIEXPORT jint JNICALL Java_art_Test924_getThreadState(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) {
  jint state;
  jvmtiError result = jvmti_env->GetThreadState(thread, &state);
  if (JvmtiErrorToException(env, jvmti_env, result)) {
    return 0;
  }
  return state;
}
 
extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test924_getAllThreads(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
  jint thread_count;
  jthread* threads;
 
  jvmtiError result = jvmti_env->GetAllThreads(&thread_count, &threads);
  if (JvmtiErrorToException(env, jvmti_env, result)) {
    return nullptr;
  }
 
  auto callback = [&](jint index) {
    return threads[index];
  };
  jobjectArray ret = CreateObjectArray(env, thread_count, "java/lang/Thread", callback);
 
  jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(threads));
 
  return ret;
}
 
extern "C" JNIEXPORT jlong JNICALL Java_art_Test924_getTLS(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) {
  void* tls;
  jvmtiError result = jvmti_env->GetThreadLocalStorage(thread, &tls);
  if (JvmtiErrorToException(env, jvmti_env, result)) {
    return 0;
  }
  return static_cast<jlong>(reinterpret_cast<uintptr_t>(tls));
}
 
extern "C" JNIEXPORT void JNICALL Java_art_Test924_setTLS(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread, jlong val) {
  const void* tls = reinterpret_cast<void*>(static_cast<uintptr_t>(val));
  jvmtiError result = jvmti_env->SetThreadLocalStorage(thread, tls);
  JvmtiErrorToException(env, jvmti_env, result);
}
 
static std::mutex gEventsMutex;
static std::vector<std::string> gEvents;
 
static void JNICALL ThreadEvent(jvmtiEnv* jvmti_env,
                                JNIEnv* jni_env,
                                jthread thread,
                                bool is_start) {
  jvmtiThreadInfo info;
  {
    std::lock_guard<std::mutex> guard(gEventsMutex);
 
    jvmtiError result = jvmti_env->GetThreadInfo(thread, &info);
    if (result != JVMTI_ERROR_NONE) {
      gEvents.push_back("Error getting thread info");
      return;
    }
 
    gEvents.push_back(android::base::StringPrintf("Thread(%s): %s",
                                                  info.name,
                                                  is_start ? "start" : "end"));
  }
 
  jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(info.name));
  jni_env->DeleteLocalRef(info.thread_group);
  jni_env->DeleteLocalRef(info.context_class_loader);
}
 
static void JNICALL ThreadStart(jvmtiEnv* jvmti_env,
                                JNIEnv* jni_env,
                                jthread thread) {
  ThreadEvent(jvmti_env, jni_env, thread, true);
}
 
static void JNICALL ThreadEnd(jvmtiEnv* jvmti_env,
                              JNIEnv* jni_env,
                              jthread thread) {
  ThreadEvent(jvmti_env, jni_env, thread, false);
}
 
extern "C" JNIEXPORT void JNICALL Java_art_Test924_enableThreadEvents(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jboolean b) {
  if (b == JNI_FALSE) {
    jvmtiError ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
                                                         JVMTI_EVENT_THREAD_START,
                                                         nullptr);
    if (JvmtiErrorToException(env, jvmti_env, ret)) {
      return;
    }
    ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
                                              JVMTI_EVENT_THREAD_END,
                                              nullptr);
    JvmtiErrorToException(env, jvmti_env, ret);
    return;
  }
 
  jvmtiEventCallbacks callbacks;
  memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
  callbacks.ThreadStart = ThreadStart;
  callbacks.ThreadEnd = ThreadEnd;
  jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
  if (JvmtiErrorToException(env, jvmti_env, ret)) {
    return;
  }
 
  ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
                                            JVMTI_EVENT_THREAD_START,
                                            nullptr);
  if (JvmtiErrorToException(env, jvmti_env, ret)) {
    return;
  }
  ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
                                            JVMTI_EVENT_THREAD_END,
                                            nullptr);
  JvmtiErrorToException(env, jvmti_env, ret);
}
 
extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test924_getThreadEventMessages(
    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
  std::lock_guard<std::mutex> guard(gEventsMutex);
  jobjectArray ret = CreateObjectArray(env,
                                       static_cast<jint>(gEvents.size()),
                                       "java/lang/String",
                                       [&](jint i) {
    return env->NewStringUTF(gEvents[i].c_str());
  });
  gEvents.clear();
  return ret;
}
 
}  // namespace Test924Threads
}  // namespace art