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 Test1968ForceEarlyReturn {
43 
44 extern "C" JNIEXPORT
Java_art_Test1968_00024NativeCalledObject_calledFunction(JNIEnv * env,jobject thiz)45 jobject JNICALL Java_art_Test1968_00024NativeCalledObject_calledFunction(
46     JNIEnv* env, jobject thiz) {
47   env->PushLocalFrame(4);
48   jclass klass = env->GetObjectClass(thiz);
49   jfieldID cnt = env->GetFieldID(klass, "cnt", "I");
50   env->SetIntField(thiz, cnt, env->GetIntField(thiz, cnt) + 1);
51   jclass int_container_klass = env->FindClass("art/Test1968$IntContainer");
52   jmethodID int_cont_new = env->GetMethodID(int_container_klass, "<init>", "(I)V");
53   jobject res = env->NewObject(int_container_klass, int_cont_new, env->GetIntField(thiz, cnt));
54   env->SetIntField(thiz, cnt, env->GetIntField(thiz, cnt) + 1);
55   void *data;
56   if (JvmtiErrorToException(env,
57                             jvmti_env,
58                             jvmti_env->GetThreadLocalStorage(/* thread */ nullptr,
59                                                              reinterpret_cast<void**>(&data)))) {
60     env->PopLocalFrame(nullptr);
61     return nullptr;
62   }
63   if (data != nullptr) {
64     art::common_suspend_event::PerformSuspension(jvmti_env, env);
65   }
66   return env->PopLocalFrame(res);
67 }
68 
69 extern "C" JNIEXPORT
Java_art_Test1968_00024NativeCallerObject_run(JNIEnv * env,jobject thiz)70 void JNICALL Java_art_Test1968_00024NativeCallerObject_run(
71     JNIEnv* env, jobject thiz) {
72   env->PushLocalFrame(1);
73   jclass klass = env->GetObjectClass(thiz);
74   jfieldID ret = env->GetFieldID(klass, "returnValue", "Ljava/lang/Object;");
75   jmethodID called = env->GetMethodID(klass, "calledFunction", "()Ljava/lang/Object;");
76   env->SetObjectField(thiz, ret, env->CallObjectMethod(thiz, called));
77   env->PopLocalFrame(nullptr);
78 }
79 
80 }  // namespace Test1968ForceEarlyReturn
81 }  // namespace art
82 
83