blob: d85d9d34ae0924c3f4ece2336486961f06503939 [file] [log] [blame]
Andreas Gampecc13b222016-10-10 19:09:09 -07001/*
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 Gampecc13b222016-10-10 19:09:09 -070017#include <pthread.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include <cstdio>
20#include <iostream>
Alex Light35a760d2019-02-12 14:24:38 -080021#include <mutex>
Andreas Gampecc13b222016-10-10 19:09:09 -070022#include <vector>
23
Andreas Gampe027444b2017-03-31 12:49:07 -070024#include "android-base/logging.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070025#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070026#include "jvmti.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070027#include "scoped_local_ref.h"
28#include "scoped_utf_chars.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070029
Andreas Gampe3f46c962017-03-30 10:26:59 -070030// Test infrastructure
31#include "jvmti_helper.h"
32#include "test_env.h"
33
Andreas Gampecc13b222016-10-10 19:09:09 -070034namespace art {
35namespace Test905ObjectFree {
36
Alex Light35a760d2019-02-12 14:24:38 -080037// 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.
39std::mutex ct1_mutex;
Mathieu Chartierf169e272017-03-28 12:59:38 -070040static std::vector<jlong> collected_tags1;
Alex Light35a760d2019-02-12 14:24:38 -080041std::mutex ct2_mutex;
Mathieu Chartierf169e272017-03-28 12:59:38 -070042static std::vector<jlong> collected_tags2;
Andreas Gampee54eee12016-10-20 19:03:58 -070043
Mathieu Chartierf169e272017-03-28 12:59:38 -070044jvmtiEnv* jvmti_env2;
45
46static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) {
Alex Light35a760d2019-02-12 14:24:38 -080047 std::lock_guard<std::mutex> mu(ct1_mutex);
Mathieu Chartierf169e272017-03-28 12:59:38 -070048 CHECK_EQ(ti_env, jvmti_env);
49 collected_tags1.push_back(tag);
50}
51
52static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) {
Alex Light35a760d2019-02-12 14:24:38 -080053 std::lock_guard<std::mutex> mu(ct2_mutex);
Mathieu Chartierf169e272017-03-28 12:59:38 -070054 CHECK_EQ(ti_env, jvmti_env2);
55 collected_tags2.push_back(tag);
56}
57
Andreas Gampe3f46c962017-03-30 10:26:59 -070058static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) {
Mathieu Chartierf169e272017-03-28 12:59:38 -070059 jvmtiEventCallbacks callbacks;
60 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
61 callbacks.ObjectFree = callback;
Andreas Gampe3f46c962017-03-30 10:26:59 -070062 jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks));
63 JvmtiErrorToException(env, jenv, ret);
Andreas Gampecc13b222016-10-10 19:09:09 -070064}
65
Andreas Gampe46651672017-04-07 09:00:04 -070066extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback(
Mathieu Chartierf169e272017-03-28 12:59:38 -070067 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe3f46c962017-03-30 10:26:59 -070068 setupObjectFreeCallback(env, jvmti_env, ObjectFree1);
Mathieu Chartierf169e272017-03-28 12:59:38 -070069 JavaVM* jvm = nullptr;
70 env->GetJavaVM(&jvm);
71 CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0);
Alex Light3d324fd2017-07-20 15:38:52 -070072 SetStandardCapabilities(jvmti_env2);
Andreas Gampe3f46c962017-03-30 10:26:59 -070073 setupObjectFreeCallback(env, jvmti_env2, ObjectFree2);
Andreas Gampecc13b222016-10-10 19:09:09 -070074}
75
Andreas Gampe46651672017-04-07 09:00:04 -070076extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking(
77 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) {
Andreas Gampecc13b222016-10-10 19:09:09 -070078 jvmtiError ret = jvmti_env->SetEventNotificationMode(
79 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
80 JVMTI_EVENT_OBJECT_FREE,
81 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070082 if (JvmtiErrorToException(env, jvmti_env, ret)) {
83 return;
Andreas Gampecc13b222016-10-10 19:09:09 -070084 }
Mathieu Chartierf169e272017-03-28 12:59:38 -070085 ret = jvmti_env2->SetEventNotificationMode(
86 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
87 JVMTI_EVENT_OBJECT_FREE,
88 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070089 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampecc13b222016-10-10 19:09:09 -070090}
91
Andreas Gampe46651672017-04-07 09:00:04 -070092extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags(
93 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint index) {
Alex Light35a760d2019-02-12 14:24:38 -080094 std::lock_guard<std::mutex> mu((index == 0) ? ct1_mutex : ct2_mutex);
Mathieu Chartierf169e272017-03-28 12:59:38 -070095 std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2;
96 jlongArray ret = env->NewLongArray(tags.size());
Andreas Gampee54eee12016-10-20 19:03:58 -070097 if (ret == nullptr) {
98 return ret;
99 }
100
Mathieu Chartierf169e272017-03-28 12:59:38 -0700101 env->SetLongArrayRegion(ret, 0, tags.size(), tags.data());
102 tags.clear();
Andreas Gampee54eee12016-10-20 19:03:58 -0700103
104 return ret;
105}
106
Alex Light35a760d2019-02-12 14:24:38 -0800107extern "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 Gampe46651672017-04-07 09:00:04 -0700115extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2(
116 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jlong tag) {
Andreas Gampea1705ea2017-03-28 20:12:13 -0700117 jvmtiError ret = jvmti_env2->SetTag(obj, tag);
Andreas Gampe3f46c962017-03-30 10:26:59 -0700118 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampea1705ea2017-03-28 20:12:13 -0700119}
120
Andreas Gampecc13b222016-10-10 19:09:09 -0700121} // namespace Test905ObjectFree
122} // namespace art