blob: 540e6ce357b86f0a8aa123c36070ecb0bc09d56c [file] [log] [blame]
Brian Carlstromce888532013-10-10 00:32:58 -07001/*
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
Brian Carlstromce888532013-10-10 00:32:58 -070017#include <pthread.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include <cstdio>
20#include <iostream>
Narayan Kamathef809d02013-12-19 17:52:47 +000021#include <vector>
Brian Carlstromce888532013-10-10 00:32:58 -070022
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070025#include "art_method-inl.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080026#include "base/runtime_debug.h"
Brian Carlstromce888532013-10-10 00:32:58 -070027#include "jni.h"
28
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070029namespace art {
Brian Carlstromce888532013-10-10 00:32:58 -070030
Mathieu Chartier2cebb242015-04-21 16:50:40 -070031static JavaVM* jvm = nullptr;
Brian Carlstromce888532013-10-10 00:32:58 -070032
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070033static jint Java_Main_intFastNativeMethod(JNIEnv*, jclass, jint a, jint b, jint c);
Igor Murashkin367f3dd2016-09-01 17:00:24 -070034static jint Java_Main_intCriticalNativeMethod(jint a, jint b, jint c);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070035
36static JNINativeMethod sMainMethods[] = {
Igor Murashkin367f3dd2016-09-01 17:00:24 -070037 {"intFastNativeMethod", "(III)I", reinterpret_cast<void*>(Java_Main_intFastNativeMethod) },
38 {"intCriticalNativeMethod", "(III)I", reinterpret_cast<void*>(Java_Main_intCriticalNativeMethod) },
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070039};
40
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070041extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void*) {
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070042 CHECK(vm != nullptr);
43 CHECK(jvm == nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -070044 jvm = vm;
Mathieu Chartier598302a2015-09-23 14:52:39 -070045 std::cout << "JNI_OnLoad called" << std::endl;
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070046
Brian Carlstromce888532013-10-10 00:32:58 -070047 return JNI_VERSION_1_6;
48}
49
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070050extern "C" JNIEXPORT void JNI_OnUnload(JavaVM*, void*) {
51 // std::cout since LOG(INFO) adds extra stuff like pid.
52 std::cout << "JNI_OnUnload called" << std::endl;
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070053 // Clear jvm for CHECK in test 004-JniTest.
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070054 jvm = nullptr;
55}
56
Andreas Gampe718ac652014-08-11 18:51:53 -070057static void* AttachHelper(void* arg) {
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070058 CHECK(jvm != nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -070059
Andreas Gampe718ac652014-08-11 18:51:53 -070060 JNIEnv* env = nullptr;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, nullptr };
Brian Carlstromce888532013-10-10 00:32:58 -070062 int attach_result = jvm->AttachCurrentThread(&env, &args);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070063 CHECK_EQ(attach_result, 0);
Brian Carlstromce888532013-10-10 00:32:58 -070064
Andreas Gampec55bb392018-09-21 00:02:02 +000065 using Fn = void(*)(JNIEnv*);
Andreas Gampe718ac652014-08-11 18:51:53 -070066 Fn fn = reinterpret_cast<Fn>(arg);
67 fn(env);
Brian Carlstromce888532013-10-10 00:32:58 -070068
69 int detach_result = jvm->DetachCurrentThread();
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070070 CHECK_EQ(detach_result, 0);
Andreas Gampe718ac652014-08-11 18:51:53 -070071 return nullptr;
72}
73
74static void PthreadHelper(void (*fn)(JNIEnv*)) {
75 pthread_t pthread;
76 int pthread_create_result = pthread_create(&pthread, nullptr, AttachHelper,
77 reinterpret_cast<void*>(fn));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070078 CHECK_EQ(pthread_create_result, 0);
Andreas Gampe718ac652014-08-11 18:51:53 -070079 int pthread_join_result = pthread_join(pthread, nullptr);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070080 CHECK_EQ(pthread_join_result, 0);
Andreas Gampe718ac652014-08-11 18:51:53 -070081}
82
83static void testFindClassOnAttachedNativeThread(JNIEnv* env) {
84 jclass clazz = env->FindClass("Main");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070085 CHECK(clazz != nullptr);
86 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -070087
88 jobjectArray array = env->NewObjectArray(0, clazz, nullptr);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070089 CHECK(array != nullptr);
90 CHECK(!env->ExceptionCheck());
Brian Carlstromce888532013-10-10 00:32:58 -070091}
92
Alex Lighte4033fa2017-12-13 10:32:59 -080093extern "C" JNIEXPORT jint JNICALL Java_Main_getFieldSubclass(JNIEnv* env,
94 jclass,
95 jobject f_obj,
96 jclass sub) {
97 jfieldID f = env->FromReflectedField(f_obj);
98 return env->GetStaticIntField(sub, f);
99}
100
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700101// http://b/10994325
Andreas Gampe718ac652014-08-11 18:51:53 -0700102extern "C" JNIEXPORT void JNICALL Java_Main_testFindClassOnAttachedNativeThread(JNIEnv*, jclass) {
103 PthreadHelper(&testFindClassOnAttachedNativeThread);
Brian Carlstromce888532013-10-10 00:32:58 -0700104}
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700105
Andreas Gampe718ac652014-08-11 18:51:53 -0700106static void testFindFieldOnAttachedNativeThread(JNIEnv* env) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700107 jclass clazz = env->FindClass("Main");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700108 CHECK(clazz != nullptr);
109 CHECK(!env->ExceptionCheck());
Jeff Hao62509b62013-12-10 17:44:56 -0800110
111 jfieldID field = env->GetStaticFieldID(clazz, "testFindFieldOnAttachedNativeThreadField", "Z");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700112 CHECK(field != nullptr);
113 CHECK(!env->ExceptionCheck());
Jeff Hao62509b62013-12-10 17:44:56 -0800114
115 env->SetStaticBooleanField(clazz, field, JNI_TRUE);
Jeff Hao62509b62013-12-10 17:44:56 -0800116}
117
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700118extern "C" JNIEXPORT void JNICALL Java_Main_testFindFieldOnAttachedNativeThreadNative(JNIEnv*,
Andreas Gampe718ac652014-08-11 18:51:53 -0700119 jclass) {
120 PthreadHelper(&testFindFieldOnAttachedNativeThread);
Jeff Hao62509b62013-12-10 17:44:56 -0800121}
122
Andreas Gampe718ac652014-08-11 18:51:53 -0700123static void testReflectFieldGetFromAttachedNativeThread(JNIEnv* env) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700124 jclass clazz = env->FindClass("Main");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700125 CHECK(clazz != nullptr);
126 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100127
128 jclass class_clazz = env->FindClass("java/lang/Class");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700129 CHECK(class_clazz != nullptr);
130 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100131
132 jmethodID getFieldMetodId = env->GetMethodID(class_clazz, "getField",
133 "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700134 CHECK(getFieldMetodId != nullptr);
135 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100136
137 jstring field_name = env->NewStringUTF("testReflectFieldGetFromAttachedNativeThreadField");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700138 CHECK(field_name != nullptr);
139 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100140
141 jobject field = env->CallObjectMethod(clazz, getFieldMetodId, field_name);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700142 CHECK(field != nullptr);
143 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100144
145 jclass field_clazz = env->FindClass("java/lang/reflect/Field");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700146 CHECK(field_clazz != nullptr);
147 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100148
149 jmethodID getBooleanMetodId = env->GetMethodID(field_clazz, "getBoolean",
150 "(Ljava/lang/Object;)Z");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700151 CHECK(getBooleanMetodId != nullptr);
152 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100153
154 jboolean value = env->CallBooleanMethod(field, getBooleanMetodId, /* ignored */ clazz);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700155 CHECK(value == false);
156 CHECK(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100157}
158
159// http://b/15539150
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700160extern "C" JNIEXPORT void JNICALL Java_Main_testReflectFieldGetFromAttachedNativeThreadNative(
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100161 JNIEnv*, jclass) {
Andreas Gampe718ac652014-08-11 18:51:53 -0700162 PthreadHelper(&testReflectFieldGetFromAttachedNativeThread);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100163}
164
Jeff Hao62509b62013-12-10 17:44:56 -0800165
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700166// http://b/11243757
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700167extern "C" JNIEXPORT void JNICALL Java_Main_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env,
Andreas Gampe718ac652014-08-11 18:51:53 -0700168 jclass) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700169 jclass super_class = env->FindClass("Main$testCallStaticVoidMethodOnSubClass_SuperClass");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700170 CHECK(super_class != nullptr);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700171
172 jmethodID execute = env->GetStaticMethodID(super_class, "execute", "()V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700173 CHECK(execute != nullptr);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700174
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700175 jclass sub_class = env->FindClass("Main$testCallStaticVoidMethodOnSubClass_SubClass");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700176 CHECK(sub_class != nullptr);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700177
178 env->CallStaticVoidMethod(sub_class, execute);
179}
Jeff Hao201803f2013-11-20 18:11:39 -0800180
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700181extern "C" JNIEXPORT jobject JNICALL Java_Main_testGetMirandaMethodNative(JNIEnv* env, jclass) {
182 jclass abstract_class = env->FindClass("Main$testGetMirandaMethod_MirandaAbstract");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700183 CHECK(abstract_class != nullptr);
Jeff Hao201803f2013-11-20 18:11:39 -0800184 jmethodID miranda_method = env->GetMethodID(abstract_class, "inInterface", "()Z");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700185 CHECK(miranda_method != nullptr);
Jeff Hao201803f2013-11-20 18:11:39 -0800186 return env->ToReflectedMethod(abstract_class, miranda_method, JNI_FALSE);
187}
Narayan Kamathef809d02013-12-19 17:52:47 +0000188
189// https://code.google.com/p/android/issues/detail?id=63055
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700190extern "C" void JNICALL Java_Main_testZeroLengthByteBuffers(JNIEnv* env, jclass) {
Narayan Kamathef809d02013-12-19 17:52:47 +0000191 std::vector<uint8_t> buffer(1);
192 jobject byte_buffer = env->NewDirectByteBuffer(&buffer[0], 0);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700193 CHECK(byte_buffer != nullptr);
194 CHECK(!env->ExceptionCheck());
Narayan Kamathef809d02013-12-19 17:52:47 +0000195
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700196 CHECK_EQ(env->GetDirectBufferAddress(byte_buffer), &buffer[0]);
197 CHECK_EQ(env->GetDirectBufferCapacity(byte_buffer), 0);
Narayan Kamathef809d02013-12-19 17:52:47 +0000198}
Andreas Gamped1104322014-05-01 14:38:56 -0700199
200constexpr size_t kByteReturnSize = 7;
201jbyte byte_returns[kByteReturnSize] = { 0, 1, 2, 127, -1, -2, -128 };
202
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700203extern "C" jbyte JNICALL Java_Main_byteMethod(JNIEnv*, jclass, jbyte b1, jbyte b2,
Andreas Gampe718ac652014-08-11 18:51:53 -0700204 jbyte b3, jbyte b4, jbyte b5, jbyte b6,
205 jbyte b7, jbyte b8, jbyte b9, jbyte b10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700206 // We use b1 to drive the output.
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700207 CHECK_EQ(b2, 2);
208 CHECK_EQ(b3, -3);
209 CHECK_EQ(b4, 4);
210 CHECK_EQ(b5, -5);
211 CHECK_EQ(b6, 6);
212 CHECK_EQ(b7, -7);
213 CHECK_EQ(b8, 8);
214 CHECK_EQ(b9, -9);
215 CHECK_EQ(b10, 10);
Andreas Gamped1104322014-05-01 14:38:56 -0700216
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700217 CHECK_LE(0, b1);
218 CHECK_LT(b1, static_cast<jbyte>(kByteReturnSize));
Andreas Gamped1104322014-05-01 14:38:56 -0700219
220 return byte_returns[b1];
221}
222
223constexpr size_t kShortReturnSize = 9;
224jshort short_returns[kShortReturnSize] = { 0, 1, 2, 127, 32767, -1, -2, -128,
225 static_cast<jshort>(0x8000) };
226// The weird static_cast is because short int is only guaranteed down to -32767, not Java's -32768.
227
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700228extern "C" jshort JNICALL Java_Main_shortMethod(JNIEnv*, jclass, jshort s1, jshort s2,
Andreas Gampe718ac652014-08-11 18:51:53 -0700229 jshort s3, jshort s4, jshort s5, jshort s6,
230 jshort s7, jshort s8, jshort s9, jshort s10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700231 // We use s1 to drive the output.
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700232 CHECK_EQ(s2, 2);
233 CHECK_EQ(s3, -3);
234 CHECK_EQ(s4, 4);
235 CHECK_EQ(s5, -5);
236 CHECK_EQ(s6, 6);
237 CHECK_EQ(s7, -7);
238 CHECK_EQ(s8, 8);
239 CHECK_EQ(s9, -9);
240 CHECK_EQ(s10, 10);
Andreas Gamped1104322014-05-01 14:38:56 -0700241
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700242 CHECK_LE(0, s1);
243 CHECK_LT(s1, static_cast<jshort>(kShortReturnSize));
Andreas Gamped1104322014-05-01 14:38:56 -0700244
245 return short_returns[s1];
246}
247
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700248extern "C" jboolean JNICALL Java_Main_booleanMethod(JNIEnv*, jclass, jboolean b1,
Andreas Gampe718ac652014-08-11 18:51:53 -0700249 jboolean b2, jboolean b3, jboolean b4,
250 jboolean b5, jboolean b6, jboolean b7,
251 jboolean b8, jboolean b9, jboolean b10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700252 // We use b1 to drive the output.
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700253 CHECK_EQ(b2, JNI_TRUE);
254 CHECK_EQ(b3, JNI_FALSE);
255 CHECK_EQ(b4, JNI_TRUE);
256 CHECK_EQ(b5, JNI_FALSE);
257 CHECK_EQ(b6, JNI_TRUE);
258 CHECK_EQ(b7, JNI_FALSE);
259 CHECK_EQ(b8, JNI_TRUE);
260 CHECK_EQ(b9, JNI_FALSE);
261 CHECK_EQ(b10, JNI_TRUE);
Andreas Gamped1104322014-05-01 14:38:56 -0700262
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700263 CHECK(b1 == JNI_TRUE || b1 == JNI_FALSE);
Andreas Gamped1104322014-05-01 14:38:56 -0700264 return b1;
265}
266
267constexpr size_t kCharReturnSize = 8;
268jchar char_returns[kCharReturnSize] = { 0, 1, 2, 127, 255, 256, 15000, 34000 };
269
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700270extern "C" jchar JNICALL Java_Main_charMethod(JNIEnv*, jclass, jchar c1, jchar c2,
Andreas Gampe718ac652014-08-11 18:51:53 -0700271 jchar c3, jchar c4, jchar c5, jchar c6, jchar c7,
272 jchar c8, jchar c9, jchar c10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700273 // We use c1 to drive the output.
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700274 CHECK_EQ(c2, 'a');
275 CHECK_EQ(c3, 'b');
276 CHECK_EQ(c4, 'c');
277 CHECK_EQ(c5, '0');
278 CHECK_EQ(c6, '1');
279 CHECK_EQ(c7, '2');
280 CHECK_EQ(c8, 1234);
281 CHECK_EQ(c9, 2345);
282 CHECK_EQ(c10, 3456);
Andreas Gamped1104322014-05-01 14:38:56 -0700283
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700284 CHECK_LT(c1, static_cast<jchar>(kCharReturnSize));
Andreas Gamped1104322014-05-01 14:38:56 -0700285
286 return char_returns[c1];
287}
Narayan Kamath1268b742014-07-11 19:15:11 +0100288
Mathieu Chartier22c1caa2015-06-02 13:40:12 -0700289extern "C" JNIEXPORT void JNICALL Java_Main_removeLocalObject(JNIEnv* env, jclass, jclass o) {
290 // Delete the arg to see if it crashes.
291 env->DeleteLocalRef(o);
292}
293
Narayan Kamath1268b742014-07-11 19:15:11 +0100294extern "C" JNIEXPORT jboolean JNICALL Java_Main_nativeIsAssignableFrom(JNIEnv* env, jclass,
295 jclass from, jclass to) {
296 return env->IsAssignableFrom(from, to);
297}
Andreas Gampe718ac652014-08-11 18:51:53 -0700298
299static void testShallowGetCallingClassLoader(JNIEnv* env) {
300 // Test direct call.
301 {
302 jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700303 CHECK(vmstack_clazz != nullptr);
304 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700305
306 jmethodID getCallingClassLoaderMethodId = env->GetStaticMethodID(vmstack_clazz,
307 "getCallingClassLoader",
308 "()Ljava/lang/ClassLoader;");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700309 CHECK(getCallingClassLoaderMethodId != nullptr);
310 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700311
312 jobject class_loader = env->CallStaticObjectMethod(vmstack_clazz,
313 getCallingClassLoaderMethodId);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700314 CHECK(class_loader == nullptr);
315 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700316 }
317
318 // Test one-level call. Use System.loadLibrary().
319 {
320 jclass system_clazz = env->FindClass("java/lang/System");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700321 CHECK(system_clazz != nullptr);
322 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700323
324 jmethodID loadLibraryMethodId = env->GetStaticMethodID(system_clazz, "loadLibrary",
325 "(Ljava/lang/String;)V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700326 CHECK(loadLibraryMethodId != nullptr);
327 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700328
329 // Create a string object.
Nicolas Geoffray005f6972014-12-03 18:10:39 +0000330 jobject library_string = env->NewStringUTF("non_existing_library");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700331 CHECK(library_string != nullptr);
332 CHECK(!env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700333
334 env->CallStaticVoidMethod(system_clazz, loadLibraryMethodId, library_string);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700335 CHECK(env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700336
Nicolas Geoffray005f6972014-12-03 18:10:39 +0000337 // We expect UnsatisfiedLinkError.
338 jthrowable thrown = env->ExceptionOccurred();
339 env->ExceptionClear();
340
341 jclass unsatisfied_link_error_clazz = env->FindClass("java/lang/UnsatisfiedLinkError");
342 jclass thrown_class = env->GetObjectClass(thrown);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700343 CHECK(env->IsSameObject(unsatisfied_link_error_clazz, thrown_class));
Andreas Gampe718ac652014-08-11 18:51:53 -0700344 }
345}
346
347// http://b/16867274
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700348extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetCallingClassLoader(JNIEnv*,
Andreas Gampe718ac652014-08-11 18:51:53 -0700349 jclass) {
350 PthreadHelper(&testShallowGetCallingClassLoader);
351}
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700352
353static void testShallowGetStackClass2(JNIEnv* env) {
354 jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700355 CHECK(vmstack_clazz != nullptr);
356 CHECK(!env->ExceptionCheck());
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700357
358 // Test direct call.
359 {
360 jmethodID getStackClass2MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass2",
361 "()Ljava/lang/Class;");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700362 CHECK(getStackClass2MethodId != nullptr);
363 CHECK(!env->ExceptionCheck());
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700364
365 jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass2MethodId);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700366 CHECK(caller_class == nullptr);
367 CHECK(!env->ExceptionCheck());
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700368 }
369
370 // Test one-level call. Use VMStack.getStackClass1().
371 {
372 jmethodID getStackClass1MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass1",
373 "()Ljava/lang/Class;");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700374 CHECK(getStackClass1MethodId != nullptr);
375 CHECK(!env->ExceptionCheck());
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700376
377 jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass1MethodId);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700378 CHECK(caller_class == nullptr);
379 CHECK(!env->ExceptionCheck());
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700380 }
381
382 // For better testing we would need to compile against libcore and have a two-deep stack
383 // ourselves.
384}
385
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700386extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetStackClass2(JNIEnv*, jclass) {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700387 PthreadHelper(&testShallowGetStackClass2);
388}
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700389
390class JniCallNonvirtualVoidMethodTest {
391 public:
392 explicit JniCallNonvirtualVoidMethodTest(JNIEnv* env)
393 : env_(env),
394 check_jni_ri_(true),
395 check_jni_android_(true),
396 super_(GetClass("JniCallNonvirtualTest")),
397 sub_(GetClass("JniCallNonvirtualTestSubclass")),
398 super_constructor_(GetMethodID(super_, true, "<init>")),
399 super_static_(GetMethodID(super_, false, "staticMethod")),
400 super_nonstatic_(GetMethodID(super_, true, "nonstaticMethod")),
401 sub_constructor_(GetMethodID(sub_, true, "<init>")),
402 sub_static_(GetMethodID(sub_, false, "staticMethod")),
403 sub_nonstatic_(GetMethodID(sub_, true, "nonstaticMethod")),
404 super_field_(GetFieldID(super_, "nonstaticMethodSuperCalled")),
405 sub_field_(GetFieldID(super_, "nonstaticMethodSubCalled")) {}
406
407 void Test() {
408 TestStaticCallNonvirtualMethod();
409 TestNewObject();
410 TestnonstaticCallNonvirtualMethod();
411 }
412
413 JNIEnv* const env_;
414
415 bool const check_jni_ri_;
416 bool const check_jni_android_;
417
418 jclass const super_;
419 jclass const sub_;
420
421 jmethodID const super_constructor_;
422 jmethodID const super_static_;
423 jmethodID const super_nonstatic_;
424 jmethodID const sub_constructor_;
425 jmethodID const sub_static_;
426 jmethodID const sub_nonstatic_;
427
428 jfieldID const super_field_;
429 jfieldID const sub_field_;
430
431 private:
432 jclass GetClass(const char* class_name) {
433 jclass c = env_->FindClass(class_name);
434 if (env_->ExceptionCheck()) {
435 env_->ExceptionDescribe();
436 env_->FatalError(__FUNCTION__);
437 }
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700438 CHECK(!env_->ExceptionCheck());
439 CHECK(c != nullptr);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700440 return c;
441 }
442
443 jmethodID GetMethodID(jclass c, bool nonstatic, const char* method_name) {
444 jmethodID m = ((nonstatic) ?
445 env_->GetMethodID(c, method_name, "()V") :
446 env_->GetStaticMethodID(c, method_name, "()V"));
447 if (env_->ExceptionCheck()) {
448 env_->ExceptionDescribe();
449 env_->FatalError(__FUNCTION__);
450 }
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700451 CHECK(m != nullptr);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700452 return m;
453 }
454
455 jobject CallConstructor(jclass c, jmethodID m) {
456 jobject o = env_->NewObject(c, m);
457 if (env_->ExceptionCheck()) {
458 env_->ExceptionDescribe();
459 env_->FatalError(__FUNCTION__);
460 }
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700461 CHECK(o != nullptr);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700462 return o;
463 }
464
465 void CallMethod(jobject o, jclass c, jmethodID m, bool nonstatic, const char* test_case) {
466 printf("RUNNING %s\n", test_case);
467 env_->CallNonvirtualVoidMethod(o, c, m);
468 bool exception_check = env_->ExceptionCheck();
469 if (c == nullptr || !nonstatic) {
470 if (!exception_check) {
471 printf("FAILED %s due to missing exception\n", test_case);
472 env_->FatalError("Expected NullPointerException with null jclass");
473 }
474 env_->ExceptionClear();
475 } else if (exception_check) {
476 printf("FAILED %s due to pending exception\n", test_case);
477 env_->ExceptionDescribe();
478 env_->FatalError(test_case);
479 }
480 printf("PASSED %s\n", test_case);
481 }
482
483 jfieldID GetFieldID(jclass c, const char* field_name) {
484 jfieldID m = env_->GetFieldID(c, field_name, "Z");
485 if (env_->ExceptionCheck()) {
486 env_->ExceptionDescribe();
487 env_->FatalError(__FUNCTION__);
488 }
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700489 CHECK(m != nullptr);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700490 return m;
491 }
492
493 jboolean GetBooleanField(jobject o, jfieldID f) {
494 jboolean b = env_->GetBooleanField(o, f);
495 if (env_->ExceptionCheck()) {
496 env_->ExceptionDescribe();
497 env_->FatalError(__FUNCTION__);
498 }
499 return b;
500 }
501
502 void TestStaticCallNonvirtualMethod() {
503 if (!check_jni_ri_&& !check_jni_android_) {
504 CallMethod(nullptr, nullptr, super_static_, false, "null object, null class, super static");
505 }
506 if (!check_jni_android_) {
507 CallMethod(nullptr, super_, super_static_, false, "null object, super class, super static");
508 }
509 if (!check_jni_android_) {
510 CallMethod(nullptr, sub_, super_static_, false, "null object, sub class, super static");
511 }
512
513 if (!check_jni_ri_ && !check_jni_android_) {
514 CallMethod(nullptr, nullptr, sub_static_, false, "null object, null class, sub static");
515 }
516 if (!check_jni_android_) {
517 CallMethod(nullptr, sub_, sub_static_, false, "null object, super class, sub static");
518 }
519 if (!check_jni_android_) {
520 CallMethod(nullptr, super_, sub_static_, false, "null object, super class, sub static");
521 }
522 }
523
524 void TestNewObject() {
525 jobject super_super = CallConstructor(super_, super_constructor_);
526 jobject super_sub = CallConstructor(super_, sub_constructor_);
527 jobject sub_super = CallConstructor(sub_, super_constructor_);
528 jobject sub_sub = CallConstructor(sub_, sub_constructor_);
529
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700530 CHECK(env_->IsInstanceOf(super_super, super_));
531 CHECK(!env_->IsInstanceOf(super_super, sub_));
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700532
533 // Note that even though we called (and ran) the subclass
534 // constructor, we are not the subclass.
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700535 CHECK(env_->IsInstanceOf(super_sub, super_));
536 CHECK(!env_->IsInstanceOf(super_sub, sub_));
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700537
538 // Note that even though we called the superclass constructor, we
539 // are still the subclass.
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700540 CHECK(env_->IsInstanceOf(sub_super, super_));
541 CHECK(env_->IsInstanceOf(sub_super, sub_));
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700542
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700543 CHECK(env_->IsInstanceOf(sub_sub, super_));
544 CHECK(env_->IsInstanceOf(sub_sub, sub_));
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700545 }
546
547 void TestnonstaticCallNonvirtualMethod(bool super_object, bool super_class, bool super_method, const char* test_case) {
548 if (check_jni_android_) {
549 if (super_object && !super_method) {
550 return; // We don't allow a call with sub class method on the super class instance.
551 }
552 if (super_class && !super_method) {
553 return; // We don't allow a call with the sub class method with the super class argument.
554 }
555 }
556 jobject o = ((super_object) ?
557 CallConstructor(super_, super_constructor_) :
558 CallConstructor(sub_, sub_constructor_));
559 jclass c = (super_class) ? super_ : sub_;
560 jmethodID m = (super_method) ? super_nonstatic_ : sub_nonstatic_;
561 CallMethod(o, c, m, true, test_case);
562 jboolean super_field = GetBooleanField(o, super_field_);
563 jboolean sub_field = GetBooleanField(o, sub_field_);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700564 CHECK_EQ(super_field, super_method);
565 CHECK_NE(sub_field, super_method);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700566 }
567
568 void TestnonstaticCallNonvirtualMethod() {
569 TestnonstaticCallNonvirtualMethod(true, true, true, "super object, super class, super nonstatic");
570 TestnonstaticCallNonvirtualMethod(true, false, true, "super object, sub class, super nonstatic");
571 TestnonstaticCallNonvirtualMethod(true, false, false, "super object, sub class, sub nonstatic");
572 TestnonstaticCallNonvirtualMethod(true, true, false, "super object, super class, sub nonstatic");
573
574 TestnonstaticCallNonvirtualMethod(false, true, true, "sub object, super class, super nonstatic");
575 TestnonstaticCallNonvirtualMethod(false, false, true, "sub object, sub class, super nonstatic");
576 TestnonstaticCallNonvirtualMethod(false, false, false, "sub object, sub class, sub nonstatic");
577 TestnonstaticCallNonvirtualMethod(false, true, false, "sub object, super class, sub nonstatic");
578 }
579};
580
581extern "C" void JNICALL Java_Main_testCallNonvirtual(JNIEnv* env, jclass) {
582 JniCallNonvirtualVoidMethodTest(env).Test();
583}
Jeff Hao848f70a2014-01-15 13:49:50 -0800584
585extern "C" JNIEXPORT void JNICALL Java_Main_testNewStringObject(JNIEnv* env, jclass) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800586 jclass c = env->FindClass("java/lang/String");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700587 CHECK(c != nullptr);
Jeff Hao39b6c242015-05-19 20:30:23 -0700588
589 jmethodID mid1 = env->GetMethodID(c, "<init>", "()V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700590 CHECK(mid1 != nullptr);
591 CHECK(!env->ExceptionCheck());
Jeff Hao39b6c242015-05-19 20:30:23 -0700592 jmethodID mid2 = env->GetMethodID(c, "<init>", "([B)V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700593 CHECK(mid2 != nullptr);
594 CHECK(!env->ExceptionCheck());
Jeff Hao39b6c242015-05-19 20:30:23 -0700595 jmethodID mid3 = env->GetMethodID(c, "<init>", "([C)V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700596 CHECK(mid3 != nullptr);
597 CHECK(!env->ExceptionCheck());
Jeff Hao39b6c242015-05-19 20:30:23 -0700598 jmethodID mid4 = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700599 CHECK(mid4 != nullptr);
600 CHECK(!env->ExceptionCheck());
Jeff Hao39b6c242015-05-19 20:30:23 -0700601
602 const char* test_array = "Test";
603 int byte_array_length = strlen(test_array);
604 jbyteArray byte_array = env->NewByteArray(byte_array_length);
605 env->SetByteArrayRegion(byte_array, 0, byte_array_length, reinterpret_cast<const jbyte*>(test_array));
606
607 // Test NewObject
608 jstring s = reinterpret_cast<jstring>(env->NewObject(c, mid2, byte_array));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700609 CHECK(s != nullptr);
610 CHECK_EQ(env->GetStringLength(s), byte_array_length);
611 CHECK_EQ(env->GetStringUTFLength(s), byte_array_length);
Jeff Hao848f70a2014-01-15 13:49:50 -0800612 const char* chars = env->GetStringUTFChars(s, nullptr);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700613 CHECK_EQ(strcmp(test_array, chars), 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800614 env->ReleaseStringUTFChars(s, chars);
Jeff Hao39b6c242015-05-19 20:30:23 -0700615
616 // Test AllocObject and Call(Nonvirtual)VoidMethod
617 jstring s1 = reinterpret_cast<jstring>(env->AllocObject(c));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700618 CHECK(s1 != nullptr);
Jeff Hao39b6c242015-05-19 20:30:23 -0700619 jstring s2 = reinterpret_cast<jstring>(env->AllocObject(c));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700620 CHECK(s2 != nullptr);
Jeff Hao39b6c242015-05-19 20:30:23 -0700621 jstring s3 = reinterpret_cast<jstring>(env->AllocObject(c));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700622 CHECK(s3 != nullptr);
Jeff Hao39b6c242015-05-19 20:30:23 -0700623 jstring s4 = reinterpret_cast<jstring>(env->AllocObject(c));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700624 CHECK(s4 != nullptr);
Jeff Hao39b6c242015-05-19 20:30:23 -0700625
626 jcharArray char_array = env->NewCharArray(5);
627 jstring string_arg = env->NewStringUTF("helloworld");
628
629 // With Var Args
630 env->CallVoidMethod(s1, mid1);
631 env->CallNonvirtualVoidMethod(s2, c, mid2, byte_array);
632
633 // With JValues
634 jvalue args3[1];
635 args3[0].l = char_array;
636 jvalue args4[1];
637 args4[0].l = string_arg;
638 env->CallVoidMethodA(s3, mid3, args3);
639 env->CallNonvirtualVoidMethodA(s4, c, mid4, args4);
Jeff Hao450c62b2015-05-28 14:32:07 -0700640
641 // Test with global and weak global references
642 jstring s5 = reinterpret_cast<jstring>(env->AllocObject(c));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700643 CHECK(s5 != nullptr);
Jeff Hao450c62b2015-05-28 14:32:07 -0700644 s5 = reinterpret_cast<jstring>(env->NewGlobalRef(s5));
645 jstring s6 = reinterpret_cast<jstring>(env->AllocObject(c));
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700646 CHECK(s6 != nullptr);
Jeff Hao450c62b2015-05-28 14:32:07 -0700647 s6 = reinterpret_cast<jstring>(env->NewWeakGlobalRef(s6));
648
649 env->CallVoidMethod(s5, mid1);
650 env->CallNonvirtualVoidMethod(s6, c, mid2, byte_array);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700651 CHECK_EQ(env->GetStringLength(s5), 0);
652 CHECK_EQ(env->GetStringLength(s6), byte_array_length);
Jeff Hao450c62b2015-05-28 14:32:07 -0700653 const char* chars6 = env->GetStringUTFChars(s6, nullptr);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700654 CHECK_EQ(strcmp(test_array, chars6), 0);
Jeff Hao450c62b2015-05-28 14:32:07 -0700655 env->ReleaseStringUTFChars(s6, chars6);
Jeff Hao848f70a2014-01-15 13:49:50 -0800656}
Mathieu Chartier72156e22015-07-10 18:26:41 -0700657
658extern "C" JNIEXPORT jlong JNICALL Java_Main_testGetMethodID(JNIEnv* env, jclass, jclass c) {
659 return reinterpret_cast<jlong>(env->GetMethodID(c, "a", "()V"));
660}
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -0800661
662extern "C" JNIEXPORT void JNICALL Java_Main_enterJniCriticalSection(JNIEnv* env, jclass,
663 jint arraySize,
664 jbyteArray array0,
665 jbyteArray array1) {
666 for (int i = 0; i < 50000; ++i) {
667 char* data0 = reinterpret_cast<char*>(env->GetPrimitiveArrayCritical(array0, nullptr));
668 char* data1 = reinterpret_cast<char*>(env->GetPrimitiveArrayCritical(array1, nullptr));
669 bool up = i % 2 == 0;
670 for (int j = 0; j < arraySize; ++j) {
671 if (up) {
672 data1[j] = data0[j] + 1;
673 } else {
674 data0[j] = data1[j] + 1;
675 }
676 }
677 env->ReleasePrimitiveArrayCritical(array1, data1, 0);
678 env->ReleasePrimitiveArrayCritical(array0, data0, 0);
679 }
680}
Alex Light36121492016-02-22 13:43:29 -0800681
682class JniCallDefaultMethodsTest {
683 public:
684 explicit JniCallDefaultMethodsTest(JNIEnv* env)
685 : env_(env), concrete_class_(env_->FindClass("ConcreteClass")) {
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700686 CHECK(!env_->ExceptionCheck());
687 CHECK(concrete_class_ != nullptr);
Alex Light36121492016-02-22 13:43:29 -0800688 }
689
690 void Test() {
691 TestCalls("ConcreteClass", { "JniCallNonOverridenDefaultMethod",
692 "JniCallOverridenDefaultMethod",
693 "JniCallOverridenDefaultMethodWithSuper",
694 "JniCallOverridenAbstractMethod",
695 "JniCallConflictDefaultMethod",
696 "JniCallSoftConflictMethod" });
697 TestCalls("DefaultInterface", { "JniCallNonOverridenDefaultMethod",
698 "JniCallOverridenDefaultMethod",
699 "JniCallOverridenAbstractMethod",
700 "JniCallConflictDefaultMethod",
701 "JniCallSoftConflictMethod" });
702 TestCalls("AbstractInterface", { "JniCallSoftConflictMethod" });
703 TestCalls("ConflictInterface", { "JniCallConflictDefaultMethod" });
704 }
705
706 private:
Stephen Hines48ba1972018-09-24 13:35:54 -0700707 void TestCalls(const char* declaring_class, const std::vector<const char*>& methods) {
Alex Light36121492016-02-22 13:43:29 -0800708 jmethodID new_method = env_->GetMethodID(concrete_class_, "<init>", "()V");
709 jobject obj = env_->NewObject(concrete_class_, new_method);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700710 CHECK(!env_->ExceptionCheck());
711 CHECK(obj != nullptr);
Alex Light36121492016-02-22 13:43:29 -0800712 jclass decl_class = env_->FindClass(declaring_class);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700713 CHECK(!env_->ExceptionCheck());
714 CHECK(decl_class != nullptr);
Alex Light36121492016-02-22 13:43:29 -0800715 for (const char* method : methods) {
716 jmethodID method_id = env_->GetMethodID(decl_class, method, "()V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700717 CHECK(!env_->ExceptionCheck());
Alex Light36121492016-02-22 13:43:29 -0800718 printf("Calling method %s->%s on object of type ConcreteClass\n", declaring_class, method);
719 env_->CallVoidMethod(obj, method_id);
720 if (env_->ExceptionCheck()) {
721 jthrowable thrown = env_->ExceptionOccurred();
722 env_->ExceptionClear();
723 jmethodID to_string = env_->GetMethodID(
724 env_->FindClass("java/lang/Object"), "toString", "()Ljava/lang/String;");
725 jstring exception_string = (jstring) env_->CallObjectMethod(thrown, to_string);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700726 CHECK(!env_->ExceptionCheck());
Alex Light36121492016-02-22 13:43:29 -0800727 const char* exception_string_utf8 = env_->GetStringUTFChars(exception_string, nullptr);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700728 CHECK(!env_->ExceptionCheck());
729 CHECK(exception_string_utf8 != nullptr);
Alex Light36121492016-02-22 13:43:29 -0800730 printf("EXCEPTION OCCURED: %s\n", exception_string_utf8);
731 env_->ReleaseStringUTFChars(exception_string, exception_string_utf8);
732 }
733 }
734 }
735
736 JNIEnv* env_;
737 jclass concrete_class_;
738};
739
740extern "C" JNIEXPORT void JNICALL Java_Main_testCallDefaultMethods(JNIEnv* env) {
741 JniCallDefaultMethodsTest(env).Test();
742}
Alex Lighte9d2ca22016-02-25 16:13:54 -0800743
744static void InvokeSpecificMethod(JNIEnv* env, jobject obj, const char* method) {
745 jclass lambda_class = env->FindClass("LambdaInterface");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700746 CHECK(!env->ExceptionCheck());
747 CHECK(lambda_class != nullptr);
Alex Lighte9d2ca22016-02-25 16:13:54 -0800748 jmethodID method_id = env->GetMethodID(lambda_class, method, "()V");
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700749 CHECK(!env->ExceptionCheck());
Alex Lighte9d2ca22016-02-25 16:13:54 -0800750 env->CallVoidMethod(obj, method_id);
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700751 CHECK(!env->ExceptionCheck());
Alex Lighte9d2ca22016-02-25 16:13:54 -0800752}
753
754extern "C" JNIEXPORT void JNICALL Java_Main_testInvokeLambdaDefaultMethod(
755 JNIEnv* e, jclass, jobject l) {
756 InvokeSpecificMethod(e, l, "sayHiTwice");
757}
758
759extern "C" JNIEXPORT void JNICALL Java_Main_testInvokeLambdaMethod(JNIEnv* e, jclass, jobject l) {
760 InvokeSpecificMethod(e, l, "sayHi");
761}
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700762
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700763// Register on-demand because many tests share this JNI library and
764// we can't unconditionally register them.
765extern "C" JNIEXPORT jboolean JNICALL Java_Main_registerNativesJniTest(JNIEnv* e, jclass kls) {
766 const size_t numMethods = sizeof(sMainMethods)/sizeof(JNINativeMethod);
767
768 if (e->RegisterNatives(kls, sMainMethods, numMethods) < 0) {
769 std::cerr << "RegisterNatives failed for 'Main'" << std::endl;
770 return JNI_FALSE;
771 }
772
773 return JNI_TRUE;
774}
775
776// Annotated with @FastNative in Java code. Doesn't need to be explicitly registered with "!".
777// NOTE: Has to be registered explicitly to avoid mutator lock check failures.
778static jint Java_Main_intFastNativeMethod(JNIEnv*, jclass, jint a, jint b, jint c) {
779 return a + b + c;
780}
781
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700782// Annotated with @CriticalNative in Java code. Doesn't need to be explicitly registered with "!".
783// NOTE: Has to be registered explicitly to avoid mutator lock check failures.
784static jint Java_Main_intCriticalNativeMethod(jint a, jint b, jint c) {
785 // Note that unlike a "Fast Native" method this excludes JNIEnv and the jclass parameters.
786 return a + b + c;
787}
788
Igor Murashkin35f1d082017-08-29 13:50:13 -0700789extern "C" JNIEXPORT jobject JNICALL Java_Main_lookupClinit(JNIEnv* env, jclass, jclass kls) {
790 jmethodID clinit_id = env->GetStaticMethodID(kls, "<clinit>", "()V");
791
792 if (clinit_id != nullptr) {
793 jobject obj = env->ToReflectedMethod(kls, clinit_id, /*isStatic*/ true);
794 CHECK(obj != nullptr);
795 return obj;
796 } else {
797 return nullptr;
798 }
799}
800
Andreas Gampe27aaf642017-06-21 21:51:33 -0700801extern "C" JNIEXPORT jboolean JNICALL Java_Main_isSlowDebug(JNIEnv*, jclass) {
802 // Return whether slow-debug is on. Only relevant for debug builds.
803 if (kIsDebugBuild) {
804 // Register a dummy flag and get the default value it should be initialized with.
805 static bool dummy_flag = false;
806 dummy_flag = RegisterRuntimeDebugFlag(&dummy_flag);
807
808 return dummy_flag ? JNI_TRUE : JNI_FALSE;
809 }
810 // To pass the Java-side test, just so "on" for release builds.
811 return JNI_TRUE;
812}
813
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -0700814} // namespace art
815