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 <inttypes.h>
18 
19 #include <cstdio>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "android-base/logging.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_binder.h"
34 #include "jni_helper.h"
35 #include "jvmti_helper.h"
36 #include "test_env.h"
37 #include "ti_macros.h"
38 
39 #include "suspend_event_helper.h"
40 
41 namespace art {
42 namespace Test1953PopFrame {
43 
44 
45 extern "C" JNIEXPORT
Java_art_Test1953_popFrame(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jthread thr)46 void JNICALL Java_art_Test1953_popFrame(JNIEnv* env,
47                                         jclass klass ATTRIBUTE_UNUSED,
48                                         jthread thr) {
49   JvmtiErrorToException(env, jvmti_env, jvmti_env->PopFrame(thr));
50 }
51 
52 extern "C" JNIEXPORT
Java_art_Test1953_00024NativeCalledObject_calledFunction(JNIEnv * env,jobject thiz)53 void JNICALL Java_art_Test1953_00024NativeCalledObject_calledFunction(
54     JNIEnv* env, jobject thiz) {
55   env->PushLocalFrame(1);
56   jclass klass = env->GetObjectClass(thiz);
57   jfieldID cnt = env->GetFieldID(klass, "cnt", "I");
58   env->SetIntField(thiz, cnt, env->GetIntField(thiz, cnt) + 1);
59   env->PopLocalFrame(nullptr);
60   art::common_suspend_event::PerformSuspension(jvmti_env, env);
61 }
62 
63 extern "C" JNIEXPORT
Java_art_Test1953_00024NativeCallerObject_run(JNIEnv * env,jobject thiz)64 void JNICALL Java_art_Test1953_00024NativeCallerObject_run(
65     JNIEnv* env, jobject thiz) {
66   env->PushLocalFrame(1);
67   jclass klass = env->GetObjectClass(thiz);
68   jfieldID baseCnt = env->GetFieldID(klass, "baseCnt", "I");
69   env->SetIntField(thiz, baseCnt, env->GetIntField(thiz, baseCnt) + 1);
70   jmethodID called = env->GetMethodID(klass, "calledFunction", "()V");
71   env->CallVoidMethod(thiz, called);
72   env->PopLocalFrame(nullptr);
73 }
74 
75 extern "C" JNIEXPORT
Java_art_Test1953_isClassLoaded(JNIEnv * env,jclass,jstring name)76 jboolean JNICALL Java_art_Test1953_isClassLoaded(JNIEnv* env, jclass, jstring name) {
77   ScopedUtfChars chr(env, name);
78   if (env->ExceptionCheck()) {
79     return false;
80   }
81   jint cnt = 0;
82   jclass* klasses = nullptr;
83   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetLoadedClasses(&cnt, &klasses))) {
84     return false;
85   }
86   bool res = false;
87   for (jint i = 0; !res && i < cnt; i++) {
88     char* sig;
89     if (JvmtiErrorToException(env,
90                               jvmti_env,
91                               jvmti_env->GetClassSignature(klasses[i], &sig, nullptr))) {
92       return false;
93     }
94     res = (strcmp(sig, chr.c_str()) == 0);
95     jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
96   }
97   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(klasses));
98   return res;
99 }
100 
101 }  // namespace Test1953PopFrame
102 }  // namespace art
103 
104