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 Test1970ForceEarlyReturnLong {
43 
44 extern "C" JNIEXPORT
Java_art_Test1970_00024NativeCalledObject_calledFunction(JNIEnv * env,jobject thiz)45 jlong JNICALL Java_art_Test1970_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   jlong res = static_cast<jlong>(env->GetIntField(thiz, cnt));
52   env->SetIntField(thiz, cnt, env->GetIntField(thiz, cnt) + 1);
53   void *data;
54   if (JvmtiErrorToException(env,
55                             jvmti_env,
56                             jvmti_env->GetThreadLocalStorage(/* thread */ nullptr,
57                                                              reinterpret_cast<void**>(&data)))) {
58     env->PopLocalFrame(nullptr);
59     return -1;
60   }
61   if (data != nullptr) {
62     art::common_suspend_event::PerformSuspension(jvmti_env, env);
63   }
64   env->PopLocalFrame(nullptr);
65   return res;
66 }
67 
68 extern "C" JNIEXPORT
Java_art_Test1970_00024NativeCallerObject_run(JNIEnv * env,jobject thiz)69 void JNICALL Java_art_Test1970_00024NativeCallerObject_run(
70     JNIEnv* env, jobject thiz) {
71   env->PushLocalFrame(1);
72   jclass klass = env->GetObjectClass(thiz);
73   jfieldID ret = env->GetFieldID(klass, "returnValue", "J");
74   jmethodID called = env->GetMethodID(klass, "calledFunction", "()J");
75   env->SetLongField(thiz, ret, env->CallLongMethod(thiz, called));
76   env->PopLocalFrame(nullptr);
77 }
78 
79 }  // namespace Test1970ForceEarlyReturnLong
80 }  // namespace art
81 
82