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 
17 #include <iostream>
18 #include <pthread.h>
19 #include <stdio.h>
20 #include <vector>
21 
22 #include "android-base/logging.h"
23 #include "jni.h"
24 #include "jvmti.h"
25 #include "scoped_local_ref.h"
26 #include "scoped_utf_chars.h"
27 
28 // Test infrastructure
29 #include "jvmti_helper.h"
30 #include "test_env.h"
31 
32 namespace art {
33 namespace Test905ObjectFree {
34 
35 static std::vector<jlong> collected_tags1;
36 static std::vector<jlong> collected_tags2;
37 
38 jvmtiEnv* jvmti_env2;
39 
ObjectFree1(jvmtiEnv * ti_env,jlong tag)40 static void JNICALL ObjectFree1(jvmtiEnv* ti_env, jlong tag) {
41   CHECK_EQ(ti_env, jvmti_env);
42   collected_tags1.push_back(tag);
43 }
44 
ObjectFree2(jvmtiEnv * ti_env,jlong tag)45 static void JNICALL ObjectFree2(jvmtiEnv* ti_env, jlong tag) {
46   CHECK_EQ(ti_env, jvmti_env2);
47   collected_tags2.push_back(tag);
48 }
49 
setupObjectFreeCallback(JNIEnv * env,jvmtiEnv * jenv,jvmtiEventObjectFree callback)50 static void setupObjectFreeCallback(JNIEnv* env, jvmtiEnv* jenv, jvmtiEventObjectFree callback) {
51   jvmtiEventCallbacks callbacks;
52   memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
53   callbacks.ObjectFree = callback;
54   jvmtiError ret = jenv->SetEventCallbacks(&callbacks, sizeof(callbacks));
55   JvmtiErrorToException(env, jenv, ret);
56 }
57 
Java_art_Test905_setupObjectFreeCallback(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED)58 extern "C" JNIEXPORT void JNICALL Java_art_Test905_setupObjectFreeCallback(
59     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
60   setupObjectFreeCallback(env, jvmti_env, ObjectFree1);
61   JavaVM* jvm = nullptr;
62   env->GetJavaVM(&jvm);
63   CHECK_EQ(jvm->GetEnv(reinterpret_cast<void**>(&jvmti_env2), JVMTI_VERSION_1_2), 0);
64   SetAllCapabilities(jvmti_env2);
65   setupObjectFreeCallback(env, jvmti_env2, ObjectFree2);
66 }
67 
Java_art_Test905_enableFreeTracking(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jboolean enable)68 extern "C" JNIEXPORT void JNICALL Java_art_Test905_enableFreeTracking(
69     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) {
70   jvmtiError ret = jvmti_env->SetEventNotificationMode(
71       enable ? JVMTI_ENABLE : JVMTI_DISABLE,
72       JVMTI_EVENT_OBJECT_FREE,
73       nullptr);
74   if (JvmtiErrorToException(env, jvmti_env, ret)) {
75     return;
76   }
77   ret = jvmti_env2->SetEventNotificationMode(
78       enable ? JVMTI_ENABLE : JVMTI_DISABLE,
79       JVMTI_EVENT_OBJECT_FREE,
80       nullptr);
81   JvmtiErrorToException(env, jvmti_env, ret);
82 }
83 
Java_art_Test905_getCollectedTags(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jint index)84 extern "C" JNIEXPORT jlongArray JNICALL Java_art_Test905_getCollectedTags(
85     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jint index) {
86   std::vector<jlong>& tags = (index == 0) ? collected_tags1 : collected_tags2;
87   jlongArray ret = env->NewLongArray(tags.size());
88   if (ret == nullptr) {
89     return ret;
90   }
91 
92   env->SetLongArrayRegion(ret, 0, tags.size(), tags.data());
93   tags.clear();
94 
95   return ret;
96 }
97 
Java_art_Test905_setTag2(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject obj,jlong tag)98 extern "C" JNIEXPORT void JNICALL Java_art_Test905_setTag2(
99     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject obj, jlong tag) {
100   jvmtiError ret = jvmti_env2->SetTag(obj, tag);
101   JvmtiErrorToException(env, jvmti_env, ret);
102 }
103 
104 }  // namespace Test905ObjectFree
105 }  // namespace art
106