blob: 60037b80454d01ec5a40baadf28840f379122b25 [file] [log] [blame]
Alex Lightc14ec8f2019-07-18 16:08:41 -07001/*
2 * Copyright (C) 2019 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 <cstdio>
18#include <memory>
Alex Light72d7e942019-07-23 13:10:20 -070019#include <mutex>
Alex Lightc14ec8f2019-07-18 16:08:41 -070020#include <string>
21#include <vector>
22
23#include "android-base/logging.h"
24#include "android-base/macros.h"
25#include "android-base/stringprintf.h"
26
27#include "jni.h"
28#include "jvmti.h"
29#include "scoped_local_ref.h"
30#include "scoped_utf_chars.h"
31
32// Test infrastructure
33#include "jni_helper.h"
34#include "jvmti_helper.h"
35#include "test_env.h"
36#include "ti_macros.h"
37
38namespace art {
39namespace Test1974ResizeArray {
40
41using ChangeArraySize = jvmtiError (*)(jvmtiEnv* env, jobject arr, jint size);
42
43template <typename T> static void Dealloc(T* t) {
44 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
45}
46
47template <typename T, typename... Rest> static void Dealloc(T* t, Rest... rs) {
48 Dealloc(t);
49 Dealloc(rs...);
50}
51
52static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
53 for (jint i = 0; i < n_params; i++) {
54 Dealloc(params[i].name);
55 }
56}
57
Alex Light72d7e942019-07-23 13:10:20 -070058static jint FindExtensionEvent(JNIEnv* env, const std::string& name) {
59 jint n_ext;
60 jvmtiExtensionEventInfo* infos;
61 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionEvents(&n_ext, &infos))) {
62 return -1;
63 }
64 jint res = -1;
65 bool found = false;
66 for (jint i = 0; i < n_ext; i++) {
67 jvmtiExtensionEventInfo* cur_info = &infos[i];
68 if (strcmp(name.c_str(), cur_info->id) == 0) {
69 res = cur_info->extension_event_index;
70 found = true;
71 }
72 // Cleanup the cur_info
73 DeallocParams(cur_info->params, cur_info->param_count);
74 Dealloc(cur_info->id, cur_info->short_description, cur_info->params);
75 }
76 // Cleanup the array.
77 Dealloc(infos);
78 if (!found) {
79 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
80 env->ThrowNew(rt_exception.get(), (name + " extensions not found").c_str());
81 return -1;
82 }
83 return res;
84}
85
Alex Lightc14ec8f2019-07-18 16:08:41 -070086static jvmtiExtensionFunction FindExtensionMethod(JNIEnv* env, const std::string& name) {
87 jint n_ext;
88 jvmtiExtensionFunctionInfo* infos;
89 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
90 return nullptr;
91 }
92 jvmtiExtensionFunction res = nullptr;
93 for (jint i = 0; i < n_ext; i++) {
94 jvmtiExtensionFunctionInfo* cur_info = &infos[i];
95 if (strcmp(name.c_str(), cur_info->id) == 0) {
96 res = cur_info->func;
97 }
98 // Cleanup the cur_info
99 DeallocParams(cur_info->params, cur_info->param_count);
100 Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
101 }
102 // Cleanup the array.
103 Dealloc(infos);
104 if (res == nullptr) {
105 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
106 env->ThrowNew(rt_exception.get(), (name + " extensions not found").c_str());
107 return nullptr;
108 }
109 return res;
110}
111
112extern "C" JNIEXPORT void JNICALL Java_art_Test1974_ResizeArray(JNIEnv* env,
113 jclass klass ATTRIBUTE_UNUSED,
114 jobject ref_gen,
115 jint new_size) {
116 ChangeArraySize change_array_size = reinterpret_cast<ChangeArraySize>(
117 FindExtensionMethod(env, "com.android.art.heap.change_array_size"));
118 if (change_array_size == nullptr) {
119 return;
120 }
121 jmethodID getArr = env->GetMethodID(
122 env->FindClass("java/util/function/Supplier"), "get", "()Ljava/lang/Object;");
123 jobject arr = env->CallObjectMethod(ref_gen, getArr);
124 JvmtiErrorToException(env, jvmti_env, change_array_size(jvmti_env, arr, new_size));
125}
126
127extern "C" JNIEXPORT jobject JNICALL Java_art_Test1974_ReadJniRef(JNIEnv* env,
128 jclass klass ATTRIBUTE_UNUSED,
129 jlong r) {
130 return env->NewLocalRef(reinterpret_cast<jobject>(static_cast<intptr_t>(r)));
131}
132
133extern "C" JNIEXPORT jlong JNICALL
134Java_art_Test1974_GetWeakGlobalJniRef(JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject r) {
135 return static_cast<jlong>(reinterpret_cast<intptr_t>(env->NewWeakGlobalRef(r)));
136}
137
138extern "C" JNIEXPORT jlong JNICALL Java_art_Test1974_GetGlobalJniRef(JNIEnv* env,
139 jclass klass ATTRIBUTE_UNUSED,
140 jobject r) {
141 return static_cast<jlong>(reinterpret_cast<intptr_t>(env->NewGlobalRef(r)));
142}
143
144extern "C" JNIEXPORT jobjectArray JNICALL
145Java_art_Test1974_GetObjectsWithTag(JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jlong tag) {
146 jsize cnt = 0;
147 jobject* res = nullptr;
148 if (JvmtiErrorToException(
149 env, jvmti_env, jvmti_env->GetObjectsWithTags(1, &tag, &cnt, &res, nullptr))) {
150 return nullptr;
151 }
152 jobjectArray ret = env->NewObjectArray(cnt, env->FindClass("java/lang/Object"), nullptr);
153 if (ret == nullptr) {
154 return nullptr;
155 }
156 for (jsize i = 0; i < cnt; i++) {
157 env->SetObjectArrayElement(ret, i, res[i]);
158 }
159 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(res));
160 return ret;
161}
162
163extern "C" JNIEXPORT void JNICALL Java_art_Test1974_runNativeTest(JNIEnv* env,
164 jclass klass ATTRIBUTE_UNUSED,
165 jobjectArray arr,
166 jobject resize,
167 jobject print,
168 jobject check) {
169 jmethodID run = env->GetMethodID(env->FindClass("java/lang/Runnable"), "run", "()V");
170 jmethodID accept = env->GetMethodID(
171 env->FindClass("java/util/function/Consumer"), "accept", "(Ljava/lang/Object;)V");
172 env->CallVoidMethod(print, accept, arr);
173 env->CallVoidMethod(resize, run);
174 env->CallVoidMethod(print, accept, arr);
175 env->CallVoidMethod(check, accept, arr);
176}
Alex Light72d7e942019-07-23 13:10:20 -0700177
178struct JvmtiInfo {
179 std::mutex mu_;
180 std::vector<jlong> freed_tags_;
181};
182
183extern "C" JNIEXPORT void JNICALL Java_art_Test1974_StartCollectFrees(JNIEnv* env,
184 jclass k ATTRIBUTE_UNUSED) {
185 jvmtiEventCallbacks cb{
186 .ObjectFree =
187 [](jvmtiEnv* jvmti, jlong tag) {
188 JvmtiInfo* dat = nullptr;
189 CHECK_EQ(jvmti->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&dat)),
190 JVMTI_ERROR_NONE);
191 std::lock_guard<std::mutex> mu(dat->mu_);
192 dat->freed_tags_.push_back(tag);
193 },
194 };
195 JvmtiInfo* info = new JvmtiInfo;
196 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEnvironmentLocalStorage(info))) {
197 LOG(INFO) << "couldn't set env-local storage";
198 return;
199 }
200 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEventCallbacks(&cb, sizeof(cb)))) {
201 LOG(INFO) << "couldn't set event callback";
202 return;
203 }
204 JvmtiErrorToException(
205 env,
206 jvmti_env,
207 jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, nullptr));
208}
209
210extern "C" JNIEXPORT void JNICALL
211Java_art_Test1974_StartAssignObsoleteIncrementedId(JNIEnv* env, jclass k ATTRIBUTE_UNUSED) {
212 jint id = FindExtensionEvent(env, "com.android.art.heap.obsolete_object_created");
213 if (env->ExceptionCheck()) {
214 LOG(INFO) << "Could not find extension event!";
215 return;
216 }
217 using ObsoleteEvent = void (*)(jvmtiEnv * env, jlong * obsolete, jlong * non_obsolete);
218 ObsoleteEvent oe = [](jvmtiEnv* env ATTRIBUTE_UNUSED, jlong* obsolete, jlong* non_obsolete) {
219 *non_obsolete = *obsolete;
220 *obsolete = *obsolete + 1;
221 };
222 JvmtiErrorToException(
223 env,
224 jvmti_env,
225 jvmti_env->SetExtensionEventCallback(id, reinterpret_cast<jvmtiExtensionEvent>(oe)));
226}
227
228extern "C" JNIEXPORT void JNICALL
229Java_art_Test1974_EndAssignObsoleteIncrementedId(JNIEnv* env, jclass k ATTRIBUTE_UNUSED) {
230 jint id = FindExtensionEvent(env, "com.android.art.heap.obsolete_object_created");
231 if (env->ExceptionCheck()) {
232 LOG(INFO) << "Could not find extension event!";
233 return;
234 }
235 JvmtiErrorToException(env, jvmti_env, jvmti_env->SetExtensionEventCallback(id, nullptr));
236}
237
238extern "C" JNIEXPORT jlongArray JNICALL
239Java_art_Test1974_CollectFreedTags(JNIEnv* env, jclass k ATTRIBUTE_UNUSED) {
240 if (JvmtiErrorToException(
241 env,
242 jvmti_env,
243 jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_OBJECT_FREE, nullptr))) {
244 return nullptr;
245 }
246 JvmtiInfo* info_p = nullptr;
247 if (JvmtiErrorToException(
248 env,
249 jvmti_env,
250 jvmti_env->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&info_p)))) {
251 return nullptr;
252 }
253 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->SetEnvironmentLocalStorage(nullptr))) {
254 return nullptr;
255 }
256 std::unique_ptr<JvmtiInfo> info(info_p);
257 ScopedLocalRef<jlongArray> arr(env, env->NewLongArray(info->freed_tags_.size()));
258 if (env->ExceptionCheck()) {
259 return nullptr;
260 }
261 env->SetLongArrayRegion(arr.get(), 0, info->freed_tags_.size(), info->freed_tags_.data());
262 if (env->ExceptionCheck()) {
263 return nullptr;
264 }
265 return arr.release();
266}
Alex Lightc14ec8f2019-07-18 16:08:41 -0700267} // namespace Test1974ResizeArray
268} // namespace art