Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 17 | #include <pthread.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 18 | |
| 19 | #include <cstdio> |
| 20 | #include <iostream> |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 21 | #include <mutex> |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 25 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 26 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 27 | #include "scoped_local_ref.h" |
| 28 | #include "scoped_utf_chars.h" |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 29 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 30 | // Test infrastructure |
| 31 | #include "jvmti_helper.h" |
| 32 | #include "test_env.h" |
| 33 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | namespace Test905ObjectFree { |
| 36 | |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 37 | // The ObjectFree functions aren't required to be called on any particular thread so use these |
| 38 | // mutexs to control access to the collected_tags lists. |
| 39 | std::mutex ct1_mutex; |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 40 | static std::vector<jlong> collected_tags1; |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 41 | std::mutex ct2_mutex; |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 42 | static std::vector<jlong> collected_tags2; |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 43 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 44 | jvmtiEnv* jvmti_env2; |
| 45 | |
| 46 | static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) { |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 47 | std::lock_guard<std::mutex> mu(ct1_mutex); |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 48 | CHECK_EQ(ti_env, jvmti_env); |
| 49 | collected_tags1.push_back(tag); |
| 50 | } |
| 51 | |
| 52 | static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) { |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 53 | std::lock_guard<std::mutex> mu(ct2_mutex); |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 54 | CHECK_EQ(ti_env, jvmti_env2); |
| 55 | collected_tags2.push_back(tag); |
| 56 | } |
| 57 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 58 | static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) { |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 59 | jvmtiEventCallbacks callbacks; |
| 60 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 61 | callbacks.ObjectFree = callback; |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 62 | jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 63 | JvmtiErrorToException(env, jenv, ret); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 66 | extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback( |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 67 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 68 | setupObjectFreeCallback(env, jvmti_env, ObjectFree1); |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 69 | JavaVM* jvm = nullptr; |
| 70 | env->GetJavaVM(&jvm); |
| 71 | CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0); |
Alex Light | 3d324fd | 2017-07-20 15:38:52 -0700 | [diff] [blame] | 72 | SetStandardCapabilities(jvmti_env2); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 73 | setupObjectFreeCallback(env, jvmti_env2, ObjectFree2); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 76 | extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking( |
| 77 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 78 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 79 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 80 | JVMTI_EVENT_OBJECT_FREE, |
| 81 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 82 | if (JvmtiErrorToException(env, jvmti_env, ret)) { |
| 83 | return; |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 84 | } |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 85 | ret = jvmti_env2->SetEventNotificationMode( |
| 86 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 87 | JVMTI_EVENT_OBJECT_FREE, |
| 88 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 89 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 92 | extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags( |
| 93 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint index) { |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 94 | std::lock_guard<std::mutex> mu((index == 0) ? ct1_mutex : ct2_mutex); |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 95 | std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2; |
| 96 | jlongArray ret = env->NewLongArray(tags.size()); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 97 | if (ret == nullptr) { |
| 98 | return ret; |
| 99 | } |
| 100 | |
Mathieu Chartier | f169e27 | 2017-03-28 12:59:38 -0700 | [diff] [blame] | 101 | env->SetLongArrayRegion(ret, 0, tags.size(), tags.data()); |
| 102 | tags.clear(); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
Alex Light | 35a760d | 2019-02-12 14:24:38 -0800 | [diff] [blame] | 107 | extern "C" JNIEXPORT jlong JNICALL Java_art_Test905_getTag2( |
| 108 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj) { |
| 109 | jlong tag; |
| 110 | jvmtiError ret = jvmti_env2->GetTag(obj, &tag); |
| 111 | JvmtiErrorToException(env, jvmti_env, ret); |
| 112 | return tag; |
| 113 | } |
| 114 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 115 | extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2( |
| 116 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jlong tag) { |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 117 | jvmtiError ret = jvmti_env2->SetTag(obj, tag); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 118 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 121 | } // namespace Test905ObjectFree |
| 122 | } // namespace art |