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 Test1969ForceEarlyReturnVoid {
43 
44 extern "C" JNIEXPORT
Java_art_Test1969_00024NativeCalledObject_calledFunction(JNIEnv * env,jobject thiz)45 void JNICALL Java_art_Test1969_00024NativeCalledObject_calledFunction(
46     JNIEnv* env, jobject thiz) {
47   jclass klass = env->GetObjectClass(thiz);
48   jfieldID cnt = env->GetFieldID(klass, "cnt", "I");
49   env->SetIntField(thiz, cnt, env->GetIntField(thiz, cnt) + 1);
50   env->SetIntField(thiz, cnt, env->GetIntField(thiz, cnt) + 1);
51   void *data;
52   if (JvmtiErrorToException(env,
53                             jvmti_env,
54                             jvmti_env->GetThreadLocalStorage(/* thread */ nullptr,
55                                                              reinterpret_cast<void**>(&data)))) {
56     return;
57   }
58   if (data != nullptr) {
59     art::common_suspend_event::PerformSuspension(jvmti_env, env);
60   }
61   return;
62 }
63 
64 extern "C" JNIEXPORT
Java_art_Test1969_00024NativeCallerObject_run(JNIEnv * env,jobject thiz)65 void JNICALL Java_art_Test1969_00024NativeCallerObject_run(
66     JNIEnv* env, jobject thiz) {
67   env->PushLocalFrame(1);
68   jclass klass = env->GetObjectClass(thiz);
69   jmethodID called = env->GetMethodID(klass, "calledFunction", "()V");
70   env->CallVoidMethod(thiz, called);
71   env->PopLocalFrame(nullptr);
72 }
73 
74 extern "C" JNIEXPORT
Java_art_Test1969_isClassLoaded(JNIEnv * env,jclass,jstring name)75 jboolean JNICALL Java_art_Test1969_isClassLoaded(JNIEnv* env, jclass, jstring name) {
76   ScopedUtfChars chr(env, name);
77   if (env->ExceptionCheck()) {
78     return false;
79   }
80   jint cnt = 0;
81   jclass* klasses = nullptr;
82   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetLoadedClasses(&cnt, &klasses))) {
83     return false;
84   }
85   bool res = false;
86   for (jint i = 0; !res && i < cnt; i++) {
87     char* sig;
88     if (JvmtiErrorToException(env,
89                               jvmti_env,
90                               jvmti_env->GetClassSignature(klasses[i], &sig, nullptr))) {
91       return false;
92     }
93     res = (strcmp(sig, chr.c_str()) == 0);
94     jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
95   }
96   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(klasses));
97   return res;
98 }
99 
100 }  // namespace Test1969ForceEarlyReturnVoid
101 }  // namespace art
102 
103