Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 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 <assert.h> |
| 18 | #include <stdio.h> |
| 19 | #include <pthread.h> |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 20 | #include <vector> |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include "jni.h" |
| 23 | |
| 24 | #if defined(NDEBUG) |
| 25 | #error test code compiled without NDEBUG |
| 26 | #endif |
| 27 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 28 | static JavaVM* jvm = nullptr; |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 29 | |
| 30 | extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 31 | assert(vm != nullptr); |
| 32 | assert(jvm == nullptr); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 33 | jvm = vm; |
| 34 | return JNI_VERSION_1_6; |
| 35 | } |
| 36 | |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 37 | static void* AttachHelper(void* arg) { |
| 38 | assert(jvm != nullptr); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 39 | |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 40 | JNIEnv* env = nullptr; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 41 | JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, nullptr }; |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 42 | int attach_result = jvm->AttachCurrentThread(&env, &args); |
| 43 | assert(attach_result == 0); |
| 44 | |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 45 | typedef void (*Fn)(JNIEnv*); |
| 46 | Fn fn = reinterpret_cast<Fn>(arg); |
| 47 | fn(env); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 48 | |
| 49 | int detach_result = jvm->DetachCurrentThread(); |
| 50 | assert(detach_result == 0); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 51 | return nullptr; |
| 52 | } |
| 53 | |
| 54 | static void PthreadHelper(void (*fn)(JNIEnv*)) { |
| 55 | pthread_t pthread; |
| 56 | int pthread_create_result = pthread_create(&pthread, nullptr, AttachHelper, |
| 57 | reinterpret_cast<void*>(fn)); |
| 58 | assert(pthread_create_result == 0); |
| 59 | int pthread_join_result = pthread_join(pthread, nullptr); |
| 60 | assert(pthread_join_result == 0); |
| 61 | } |
| 62 | |
| 63 | static void testFindClassOnAttachedNativeThread(JNIEnv* env) { |
| 64 | jclass clazz = env->FindClass("Main"); |
| 65 | assert(clazz != nullptr); |
| 66 | assert(!env->ExceptionCheck()); |
| 67 | |
| 68 | jobjectArray array = env->NewObjectArray(0, clazz, nullptr); |
| 69 | assert(array != nullptr); |
| 70 | assert(!env->ExceptionCheck()); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 73 | // http://b/10994325 |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 74 | extern "C" JNIEXPORT void JNICALL Java_Main_testFindClassOnAttachedNativeThread(JNIEnv*, jclass) { |
| 75 | PthreadHelper(&testFindClassOnAttachedNativeThread); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 76 | } |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 77 | |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 78 | static void testFindFieldOnAttachedNativeThread(JNIEnv* env) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 79 | jclass clazz = env->FindClass("Main"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 80 | assert(clazz != nullptr); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 81 | assert(!env->ExceptionCheck()); |
| 82 | |
| 83 | jfieldID field = env->GetStaticFieldID(clazz, "testFindFieldOnAttachedNativeThreadField", "Z"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 84 | assert(field != nullptr); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 85 | assert(!env->ExceptionCheck()); |
| 86 | |
| 87 | env->SetStaticBooleanField(clazz, field, JNI_TRUE); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 90 | extern "C" JNIEXPORT void JNICALL Java_Main_testFindFieldOnAttachedNativeThreadNative(JNIEnv*, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 91 | jclass) { |
| 92 | PthreadHelper(&testFindFieldOnAttachedNativeThread); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 95 | static void testReflectFieldGetFromAttachedNativeThread(JNIEnv* env) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 96 | jclass clazz = env->FindClass("Main"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 97 | assert(clazz != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 98 | assert(!env->ExceptionCheck()); |
| 99 | |
| 100 | jclass class_clazz = env->FindClass("java/lang/Class"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 101 | assert(class_clazz != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 102 | assert(!env->ExceptionCheck()); |
| 103 | |
| 104 | jmethodID getFieldMetodId = env->GetMethodID(class_clazz, "getField", |
| 105 | "(Ljava/lang/String;)Ljava/lang/reflect/Field;"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 106 | assert(getFieldMetodId != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 107 | assert(!env->ExceptionCheck()); |
| 108 | |
| 109 | jstring field_name = env->NewStringUTF("testReflectFieldGetFromAttachedNativeThreadField"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 110 | assert(field_name != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 111 | assert(!env->ExceptionCheck()); |
| 112 | |
| 113 | jobject field = env->CallObjectMethod(clazz, getFieldMetodId, field_name); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 114 | assert(field != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 115 | assert(!env->ExceptionCheck()); |
| 116 | |
| 117 | jclass field_clazz = env->FindClass("java/lang/reflect/Field"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 118 | assert(field_clazz != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 119 | assert(!env->ExceptionCheck()); |
| 120 | |
| 121 | jmethodID getBooleanMetodId = env->GetMethodID(field_clazz, "getBoolean", |
| 122 | "(Ljava/lang/Object;)Z"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 123 | assert(getBooleanMetodId != nullptr); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 124 | assert(!env->ExceptionCheck()); |
| 125 | |
| 126 | jboolean value = env->CallBooleanMethod(field, getBooleanMetodId, /* ignored */ clazz); |
| 127 | assert(value == false); |
| 128 | assert(!env->ExceptionCheck()); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // http://b/15539150 |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 132 | extern "C" JNIEXPORT void JNICALL Java_Main_testReflectFieldGetFromAttachedNativeThreadNative( |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 133 | JNIEnv*, jclass) { |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 134 | PthreadHelper(&testReflectFieldGetFromAttachedNativeThread); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 137 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 138 | // http://b/11243757 |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 139 | extern "C" JNIEXPORT void JNICALL Java_Main_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 140 | jclass) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 141 | jclass super_class = env->FindClass("Main$testCallStaticVoidMethodOnSubClass_SuperClass"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 142 | assert(super_class != nullptr); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 143 | |
| 144 | jmethodID execute = env->GetStaticMethodID(super_class, "execute", "()V"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 145 | assert(execute != nullptr); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 146 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 147 | jclass sub_class = env->FindClass("Main$testCallStaticVoidMethodOnSubClass_SubClass"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 148 | assert(sub_class != nullptr); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 149 | |
| 150 | env->CallStaticVoidMethod(sub_class, execute); |
| 151 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 152 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 153 | extern "C" JNIEXPORT jobject JNICALL Java_Main_testGetMirandaMethodNative(JNIEnv* env, jclass) { |
| 154 | jclass abstract_class = env->FindClass("Main$testGetMirandaMethod_MirandaAbstract"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 155 | assert(abstract_class != nullptr); |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 156 | jmethodID miranda_method = env->GetMethodID(abstract_class, "inInterface", "()Z"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 157 | assert(miranda_method != nullptr); |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 158 | return env->ToReflectedMethod(abstract_class, miranda_method, JNI_FALSE); |
| 159 | } |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 160 | |
| 161 | // https://code.google.com/p/android/issues/detail?id=63055 |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 162 | extern "C" void JNICALL Java_Main_testZeroLengthByteBuffers(JNIEnv* env, jclass) { |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 163 | std::vector<uint8_t> buffer(1); |
| 164 | jobject byte_buffer = env->NewDirectByteBuffer(&buffer[0], 0); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 165 | assert(byte_buffer != nullptr); |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 166 | assert(!env->ExceptionCheck()); |
| 167 | |
| 168 | assert(env->GetDirectBufferAddress(byte_buffer) == &buffer[0]); |
| 169 | assert(env->GetDirectBufferCapacity(byte_buffer) == 0); |
| 170 | } |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 171 | |
| 172 | constexpr size_t kByteReturnSize = 7; |
| 173 | jbyte byte_returns[kByteReturnSize] = { 0, 1, 2, 127, -1, -2, -128 }; |
| 174 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 175 | extern "C" jbyte JNICALL Java_Main_byteMethod(JNIEnv*, jclass, jbyte b1, jbyte b2, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 176 | jbyte b3, jbyte b4, jbyte b5, jbyte b6, |
| 177 | jbyte b7, jbyte b8, jbyte b9, jbyte b10) { |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 178 | // We use b1 to drive the output. |
| 179 | assert(b2 == 2); |
| 180 | assert(b3 == -3); |
| 181 | assert(b4 == 4); |
| 182 | assert(b5 == -5); |
| 183 | assert(b6 == 6); |
| 184 | assert(b7 == -7); |
| 185 | assert(b8 == 8); |
| 186 | assert(b9 == -9); |
| 187 | assert(b10 == 10); |
| 188 | |
| 189 | assert(0 <= b1); |
| 190 | assert(b1 < static_cast<jbyte>(kByteReturnSize)); |
| 191 | |
| 192 | return byte_returns[b1]; |
| 193 | } |
| 194 | |
| 195 | constexpr size_t kShortReturnSize = 9; |
| 196 | jshort short_returns[kShortReturnSize] = { 0, 1, 2, 127, 32767, -1, -2, -128, |
| 197 | static_cast<jshort>(0x8000) }; |
| 198 | // The weird static_cast is because short int is only guaranteed down to -32767, not Java's -32768. |
| 199 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 200 | extern "C" jshort JNICALL Java_Main_shortMethod(JNIEnv*, jclass, jshort s1, jshort s2, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 201 | jshort s3, jshort s4, jshort s5, jshort s6, |
| 202 | jshort s7, jshort s8, jshort s9, jshort s10) { |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 203 | // We use s1 to drive the output. |
| 204 | assert(s2 == 2); |
| 205 | assert(s3 == -3); |
| 206 | assert(s4 == 4); |
| 207 | assert(s5 == -5); |
| 208 | assert(s6 == 6); |
| 209 | assert(s7 == -7); |
| 210 | assert(s8 == 8); |
| 211 | assert(s9 == -9); |
| 212 | assert(s10 == 10); |
| 213 | |
| 214 | assert(0 <= s1); |
| 215 | assert(s1 < static_cast<jshort>(kShortReturnSize)); |
| 216 | |
| 217 | return short_returns[s1]; |
| 218 | } |
| 219 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 220 | extern "C" jboolean JNICALL Java_Main_booleanMethod(JNIEnv*, jclass, jboolean b1, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 221 | jboolean b2, jboolean b3, jboolean b4, |
| 222 | jboolean b5, jboolean b6, jboolean b7, |
| 223 | jboolean b8, jboolean b9, jboolean b10) { |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 224 | // We use b1 to drive the output. |
| 225 | assert(b2 == JNI_TRUE); |
| 226 | assert(b3 == JNI_FALSE); |
| 227 | assert(b4 == JNI_TRUE); |
| 228 | assert(b5 == JNI_FALSE); |
| 229 | assert(b6 == JNI_TRUE); |
| 230 | assert(b7 == JNI_FALSE); |
| 231 | assert(b8 == JNI_TRUE); |
| 232 | assert(b9 == JNI_FALSE); |
| 233 | assert(b10 == JNI_TRUE); |
| 234 | |
| 235 | assert(b1 == JNI_TRUE || b1 == JNI_FALSE); |
| 236 | return b1; |
| 237 | } |
| 238 | |
| 239 | constexpr size_t kCharReturnSize = 8; |
| 240 | jchar char_returns[kCharReturnSize] = { 0, 1, 2, 127, 255, 256, 15000, 34000 }; |
| 241 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 242 | extern "C" jchar JNICALL Java_Main_charMethod(JNIEnv*, jclass, jchar c1, jchar c2, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 243 | jchar c3, jchar c4, jchar c5, jchar c6, jchar c7, |
| 244 | jchar c8, jchar c9, jchar c10) { |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 245 | // We use c1 to drive the output. |
| 246 | assert(c2 == 'a'); |
| 247 | assert(c3 == 'b'); |
| 248 | assert(c4 == 'c'); |
| 249 | assert(c5 == '0'); |
| 250 | assert(c6 == '1'); |
| 251 | assert(c7 == '2'); |
| 252 | assert(c8 == 1234); |
| 253 | assert(c9 == 2345); |
| 254 | assert(c10 == 3456); |
| 255 | |
| 256 | assert(c1 < static_cast<jchar>(kCharReturnSize)); |
| 257 | |
| 258 | return char_returns[c1]; |
| 259 | } |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 260 | |
Mathieu Chartier | 22c1caa | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 261 | extern "C" JNIEXPORT void JNICALL Java_Main_removeLocalObject(JNIEnv* env, jclass, jclass o) { |
| 262 | // Delete the arg to see if it crashes. |
| 263 | env->DeleteLocalRef(o); |
| 264 | } |
| 265 | |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 266 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_nativeIsAssignableFrom(JNIEnv* env, jclass, |
| 267 | jclass from, jclass to) { |
| 268 | return env->IsAssignableFrom(from, to); |
| 269 | } |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 270 | |
| 271 | static void testShallowGetCallingClassLoader(JNIEnv* env) { |
| 272 | // Test direct call. |
| 273 | { |
| 274 | jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack"); |
| 275 | assert(vmstack_clazz != nullptr); |
| 276 | assert(!env->ExceptionCheck()); |
| 277 | |
| 278 | jmethodID getCallingClassLoaderMethodId = env->GetStaticMethodID(vmstack_clazz, |
| 279 | "getCallingClassLoader", |
| 280 | "()Ljava/lang/ClassLoader;"); |
| 281 | assert(getCallingClassLoaderMethodId != nullptr); |
| 282 | assert(!env->ExceptionCheck()); |
| 283 | |
| 284 | jobject class_loader = env->CallStaticObjectMethod(vmstack_clazz, |
| 285 | getCallingClassLoaderMethodId); |
| 286 | assert(class_loader == nullptr); |
| 287 | assert(!env->ExceptionCheck()); |
| 288 | } |
| 289 | |
| 290 | // Test one-level call. Use System.loadLibrary(). |
| 291 | { |
| 292 | jclass system_clazz = env->FindClass("java/lang/System"); |
| 293 | assert(system_clazz != nullptr); |
| 294 | assert(!env->ExceptionCheck()); |
| 295 | |
| 296 | jmethodID loadLibraryMethodId = env->GetStaticMethodID(system_clazz, "loadLibrary", |
| 297 | "(Ljava/lang/String;)V"); |
| 298 | assert(loadLibraryMethodId != nullptr); |
| 299 | assert(!env->ExceptionCheck()); |
| 300 | |
| 301 | // Create a string object. |
Nicolas Geoffray | 005f697 | 2014-12-03 18:10:39 +0000 | [diff] [blame] | 302 | jobject library_string = env->NewStringUTF("non_existing_library"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 303 | assert(library_string != nullptr); |
| 304 | assert(!env->ExceptionCheck()); |
| 305 | |
| 306 | env->CallStaticVoidMethod(system_clazz, loadLibraryMethodId, library_string); |
Nicolas Geoffray | 005f697 | 2014-12-03 18:10:39 +0000 | [diff] [blame] | 307 | assert(env->ExceptionCheck()); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 308 | |
Nicolas Geoffray | 005f697 | 2014-12-03 18:10:39 +0000 | [diff] [blame] | 309 | // We expect UnsatisfiedLinkError. |
| 310 | jthrowable thrown = env->ExceptionOccurred(); |
| 311 | env->ExceptionClear(); |
| 312 | |
| 313 | jclass unsatisfied_link_error_clazz = env->FindClass("java/lang/UnsatisfiedLinkError"); |
| 314 | jclass thrown_class = env->GetObjectClass(thrown); |
| 315 | assert(env->IsSameObject(unsatisfied_link_error_clazz, thrown_class)); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | // http://b/16867274 |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 320 | extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetCallingClassLoader(JNIEnv*, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 321 | jclass) { |
| 322 | PthreadHelper(&testShallowGetCallingClassLoader); |
| 323 | } |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 324 | |
| 325 | static void testShallowGetStackClass2(JNIEnv* env) { |
| 326 | jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack"); |
| 327 | assert(vmstack_clazz != nullptr); |
| 328 | assert(!env->ExceptionCheck()); |
| 329 | |
| 330 | // Test direct call. |
| 331 | { |
| 332 | jmethodID getStackClass2MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass2", |
| 333 | "()Ljava/lang/Class;"); |
| 334 | assert(getStackClass2MethodId != nullptr); |
| 335 | assert(!env->ExceptionCheck()); |
| 336 | |
| 337 | jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass2MethodId); |
| 338 | assert(caller_class == nullptr); |
| 339 | assert(!env->ExceptionCheck()); |
| 340 | } |
| 341 | |
| 342 | // Test one-level call. Use VMStack.getStackClass1(). |
| 343 | { |
| 344 | jmethodID getStackClass1MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass1", |
| 345 | "()Ljava/lang/Class;"); |
| 346 | assert(getStackClass1MethodId != nullptr); |
| 347 | assert(!env->ExceptionCheck()); |
| 348 | |
| 349 | jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass1MethodId); |
| 350 | assert(caller_class == nullptr); |
| 351 | assert(!env->ExceptionCheck()); |
| 352 | } |
| 353 | |
| 354 | // For better testing we would need to compile against libcore and have a two-deep stack |
| 355 | // ourselves. |
| 356 | } |
| 357 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 358 | extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetStackClass2(JNIEnv*, jclass) { |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 359 | PthreadHelper(&testShallowGetStackClass2); |
| 360 | } |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 361 | |
| 362 | class JniCallNonvirtualVoidMethodTest { |
| 363 | public: |
| 364 | explicit JniCallNonvirtualVoidMethodTest(JNIEnv* env) |
| 365 | : env_(env), |
| 366 | check_jni_ri_(true), |
| 367 | check_jni_android_(true), |
| 368 | super_(GetClass("JniCallNonvirtualTest")), |
| 369 | sub_(GetClass("JniCallNonvirtualTestSubclass")), |
| 370 | super_constructor_(GetMethodID(super_, true, "<init>")), |
| 371 | super_static_(GetMethodID(super_, false, "staticMethod")), |
| 372 | super_nonstatic_(GetMethodID(super_, true, "nonstaticMethod")), |
| 373 | sub_constructor_(GetMethodID(sub_, true, "<init>")), |
| 374 | sub_static_(GetMethodID(sub_, false, "staticMethod")), |
| 375 | sub_nonstatic_(GetMethodID(sub_, true, "nonstaticMethod")), |
| 376 | super_field_(GetFieldID(super_, "nonstaticMethodSuperCalled")), |
| 377 | sub_field_(GetFieldID(super_, "nonstaticMethodSubCalled")) {} |
| 378 | |
| 379 | void Test() { |
| 380 | TestStaticCallNonvirtualMethod(); |
| 381 | TestNewObject(); |
| 382 | TestnonstaticCallNonvirtualMethod(); |
| 383 | } |
| 384 | |
| 385 | JNIEnv* const env_; |
| 386 | |
| 387 | bool const check_jni_ri_; |
| 388 | bool const check_jni_android_; |
| 389 | |
| 390 | jclass const super_; |
| 391 | jclass const sub_; |
| 392 | |
| 393 | jmethodID const super_constructor_; |
| 394 | jmethodID const super_static_; |
| 395 | jmethodID const super_nonstatic_; |
| 396 | jmethodID const sub_constructor_; |
| 397 | jmethodID const sub_static_; |
| 398 | jmethodID const sub_nonstatic_; |
| 399 | |
| 400 | jfieldID const super_field_; |
| 401 | jfieldID const sub_field_; |
| 402 | |
| 403 | private: |
| 404 | jclass GetClass(const char* class_name) { |
| 405 | jclass c = env_->FindClass(class_name); |
| 406 | if (env_->ExceptionCheck()) { |
| 407 | env_->ExceptionDescribe(); |
| 408 | env_->FatalError(__FUNCTION__); |
| 409 | } |
| 410 | assert(!env_->ExceptionCheck()); |
| 411 | assert(c != nullptr); |
| 412 | return c; |
| 413 | } |
| 414 | |
| 415 | jmethodID GetMethodID(jclass c, bool nonstatic, const char* method_name) { |
| 416 | jmethodID m = ((nonstatic) ? |
| 417 | env_->GetMethodID(c, method_name, "()V") : |
| 418 | env_->GetStaticMethodID(c, method_name, "()V")); |
| 419 | if (env_->ExceptionCheck()) { |
| 420 | env_->ExceptionDescribe(); |
| 421 | env_->FatalError(__FUNCTION__); |
| 422 | } |
| 423 | assert(m != nullptr); |
| 424 | return m; |
| 425 | } |
| 426 | |
| 427 | jobject CallConstructor(jclass c, jmethodID m) { |
| 428 | jobject o = env_->NewObject(c, m); |
| 429 | if (env_->ExceptionCheck()) { |
| 430 | env_->ExceptionDescribe(); |
| 431 | env_->FatalError(__FUNCTION__); |
| 432 | } |
| 433 | assert(o != nullptr); |
| 434 | return o; |
| 435 | } |
| 436 | |
| 437 | void CallMethod(jobject o, jclass c, jmethodID m, bool nonstatic, const char* test_case) { |
| 438 | printf("RUNNING %s\n", test_case); |
| 439 | env_->CallNonvirtualVoidMethod(o, c, m); |
| 440 | bool exception_check = env_->ExceptionCheck(); |
| 441 | if (c == nullptr || !nonstatic) { |
| 442 | if (!exception_check) { |
| 443 | printf("FAILED %s due to missing exception\n", test_case); |
| 444 | env_->FatalError("Expected NullPointerException with null jclass"); |
| 445 | } |
| 446 | env_->ExceptionClear(); |
| 447 | } else if (exception_check) { |
| 448 | printf("FAILED %s due to pending exception\n", test_case); |
| 449 | env_->ExceptionDescribe(); |
| 450 | env_->FatalError(test_case); |
| 451 | } |
| 452 | printf("PASSED %s\n", test_case); |
| 453 | } |
| 454 | |
| 455 | jfieldID GetFieldID(jclass c, const char* field_name) { |
| 456 | jfieldID m = env_->GetFieldID(c, field_name, "Z"); |
| 457 | if (env_->ExceptionCheck()) { |
| 458 | env_->ExceptionDescribe(); |
| 459 | env_->FatalError(__FUNCTION__); |
| 460 | } |
| 461 | assert(m != nullptr); |
| 462 | return m; |
| 463 | } |
| 464 | |
| 465 | jboolean GetBooleanField(jobject o, jfieldID f) { |
| 466 | jboolean b = env_->GetBooleanField(o, f); |
| 467 | if (env_->ExceptionCheck()) { |
| 468 | env_->ExceptionDescribe(); |
| 469 | env_->FatalError(__FUNCTION__); |
| 470 | } |
| 471 | return b; |
| 472 | } |
| 473 | |
| 474 | void TestStaticCallNonvirtualMethod() { |
| 475 | if (!check_jni_ri_&& !check_jni_android_) { |
| 476 | CallMethod(nullptr, nullptr, super_static_, false, "null object, null class, super static"); |
| 477 | } |
| 478 | if (!check_jni_android_) { |
| 479 | CallMethod(nullptr, super_, super_static_, false, "null object, super class, super static"); |
| 480 | } |
| 481 | if (!check_jni_android_) { |
| 482 | CallMethod(nullptr, sub_, super_static_, false, "null object, sub class, super static"); |
| 483 | } |
| 484 | |
| 485 | if (!check_jni_ri_ && !check_jni_android_) { |
| 486 | CallMethod(nullptr, nullptr, sub_static_, false, "null object, null class, sub static"); |
| 487 | } |
| 488 | if (!check_jni_android_) { |
| 489 | CallMethod(nullptr, sub_, sub_static_, false, "null object, super class, sub static"); |
| 490 | } |
| 491 | if (!check_jni_android_) { |
| 492 | CallMethod(nullptr, super_, sub_static_, false, "null object, super class, sub static"); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void TestNewObject() { |
| 497 | jobject super_super = CallConstructor(super_, super_constructor_); |
| 498 | jobject super_sub = CallConstructor(super_, sub_constructor_); |
| 499 | jobject sub_super = CallConstructor(sub_, super_constructor_); |
| 500 | jobject sub_sub = CallConstructor(sub_, sub_constructor_); |
| 501 | |
| 502 | assert(env_->IsInstanceOf(super_super, super_)); |
| 503 | assert(!env_->IsInstanceOf(super_super, sub_)); |
| 504 | |
| 505 | // Note that even though we called (and ran) the subclass |
| 506 | // constructor, we are not the subclass. |
| 507 | assert(env_->IsInstanceOf(super_sub, super_)); |
| 508 | assert(!env_->IsInstanceOf(super_sub, sub_)); |
| 509 | |
| 510 | // Note that even though we called the superclass constructor, we |
| 511 | // are still the subclass. |
| 512 | assert(env_->IsInstanceOf(sub_super, super_)); |
| 513 | assert(env_->IsInstanceOf(sub_super, sub_)); |
| 514 | |
| 515 | assert(env_->IsInstanceOf(sub_sub, super_)); |
| 516 | assert(env_->IsInstanceOf(sub_sub, sub_)); |
| 517 | } |
| 518 | |
| 519 | void TestnonstaticCallNonvirtualMethod(bool super_object, bool super_class, bool super_method, const char* test_case) { |
| 520 | if (check_jni_android_) { |
| 521 | if (super_object && !super_method) { |
| 522 | return; // We don't allow a call with sub class method on the super class instance. |
| 523 | } |
| 524 | if (super_class && !super_method) { |
| 525 | return; // We don't allow a call with the sub class method with the super class argument. |
| 526 | } |
| 527 | } |
| 528 | jobject o = ((super_object) ? |
| 529 | CallConstructor(super_, super_constructor_) : |
| 530 | CallConstructor(sub_, sub_constructor_)); |
| 531 | jclass c = (super_class) ? super_ : sub_; |
| 532 | jmethodID m = (super_method) ? super_nonstatic_ : sub_nonstatic_; |
| 533 | CallMethod(o, c, m, true, test_case); |
| 534 | jboolean super_field = GetBooleanField(o, super_field_); |
| 535 | jboolean sub_field = GetBooleanField(o, sub_field_); |
| 536 | assert(super_field == super_method); |
| 537 | assert(sub_field != super_method); |
| 538 | } |
| 539 | |
| 540 | void TestnonstaticCallNonvirtualMethod() { |
| 541 | TestnonstaticCallNonvirtualMethod(true, true, true, "super object, super class, super nonstatic"); |
| 542 | TestnonstaticCallNonvirtualMethod(true, false, true, "super object, sub class, super nonstatic"); |
| 543 | TestnonstaticCallNonvirtualMethod(true, false, false, "super object, sub class, sub nonstatic"); |
| 544 | TestnonstaticCallNonvirtualMethod(true, true, false, "super object, super class, sub nonstatic"); |
| 545 | |
| 546 | TestnonstaticCallNonvirtualMethod(false, true, true, "sub object, super class, super nonstatic"); |
| 547 | TestnonstaticCallNonvirtualMethod(false, false, true, "sub object, sub class, super nonstatic"); |
| 548 | TestnonstaticCallNonvirtualMethod(false, false, false, "sub object, sub class, sub nonstatic"); |
| 549 | TestnonstaticCallNonvirtualMethod(false, true, false, "sub object, super class, sub nonstatic"); |
| 550 | } |
| 551 | }; |
| 552 | |
| 553 | extern "C" void JNICALL Java_Main_testCallNonvirtual(JNIEnv* env, jclass) { |
| 554 | JniCallNonvirtualVoidMethodTest(env).Test(); |
| 555 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 556 | |
| 557 | extern "C" JNIEXPORT void JNICALL Java_Main_testNewStringObject(JNIEnv* env, jclass) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 558 | jclass c = env->FindClass("java/lang/String"); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 559 | assert(c != nullptr); |
| 560 | |
| 561 | jmethodID mid1 = env->GetMethodID(c, "<init>", "()V"); |
| 562 | assert(mid1 != nullptr); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 563 | assert(!env->ExceptionCheck()); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 564 | jmethodID mid2 = env->GetMethodID(c, "<init>", "([B)V"); |
| 565 | assert(mid2 != nullptr); |
| 566 | assert(!env->ExceptionCheck()); |
| 567 | jmethodID mid3 = env->GetMethodID(c, "<init>", "([C)V"); |
| 568 | assert(mid3 != nullptr); |
| 569 | assert(!env->ExceptionCheck()); |
| 570 | jmethodID mid4 = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V"); |
| 571 | assert(mid4 != nullptr); |
| 572 | assert(!env->ExceptionCheck()); |
| 573 | |
| 574 | const char* test_array = "Test"; |
| 575 | int byte_array_length = strlen(test_array); |
| 576 | jbyteArray byte_array = env->NewByteArray(byte_array_length); |
| 577 | env->SetByteArrayRegion(byte_array, 0, byte_array_length, reinterpret_cast<const jbyte*>(test_array)); |
| 578 | |
| 579 | // Test NewObject |
| 580 | jstring s = reinterpret_cast<jstring>(env->NewObject(c, mid2, byte_array)); |
| 581 | assert(s != nullptr); |
| 582 | assert(env->GetStringLength(s) == byte_array_length); |
| 583 | assert(env->GetStringUTFLength(s) == byte_array_length); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 584 | const char* chars = env->GetStringUTFChars(s, nullptr); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 585 | assert(strcmp(test_array, chars) == 0); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 586 | env->ReleaseStringUTFChars(s, chars); |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 587 | |
| 588 | // Test AllocObject and Call(Nonvirtual)VoidMethod |
| 589 | jstring s1 = reinterpret_cast<jstring>(env->AllocObject(c)); |
| 590 | assert(s1 != nullptr); |
| 591 | jstring s2 = reinterpret_cast<jstring>(env->AllocObject(c)); |
| 592 | assert(s2 != nullptr); |
| 593 | jstring s3 = reinterpret_cast<jstring>(env->AllocObject(c)); |
| 594 | assert(s3 != nullptr); |
| 595 | jstring s4 = reinterpret_cast<jstring>(env->AllocObject(c)); |
| 596 | assert(s4 != nullptr); |
| 597 | |
| 598 | jcharArray char_array = env->NewCharArray(5); |
| 599 | jstring string_arg = env->NewStringUTF("helloworld"); |
| 600 | |
| 601 | // With Var Args |
| 602 | env->CallVoidMethod(s1, mid1); |
| 603 | env->CallNonvirtualVoidMethod(s2, c, mid2, byte_array); |
| 604 | |
| 605 | // With JValues |
| 606 | jvalue args3[1]; |
| 607 | args3[0].l = char_array; |
| 608 | jvalue args4[1]; |
| 609 | args4[0].l = string_arg; |
| 610 | env->CallVoidMethodA(s3, mid3, args3); |
| 611 | env->CallNonvirtualVoidMethodA(s4, c, mid4, args4); |
Jeff Hao | 450c62b | 2015-05-28 14:32:07 -0700 | [diff] [blame] | 612 | |
| 613 | // Test with global and weak global references |
| 614 | jstring s5 = reinterpret_cast<jstring>(env->AllocObject(c)); |
| 615 | assert(s5 != nullptr); |
| 616 | s5 = reinterpret_cast<jstring>(env->NewGlobalRef(s5)); |
| 617 | jstring s6 = reinterpret_cast<jstring>(env->AllocObject(c)); |
| 618 | assert(s6 != nullptr); |
| 619 | s6 = reinterpret_cast<jstring>(env->NewWeakGlobalRef(s6)); |
| 620 | |
| 621 | env->CallVoidMethod(s5, mid1); |
| 622 | env->CallNonvirtualVoidMethod(s6, c, mid2, byte_array); |
| 623 | assert(env->GetStringLength(s5) == 0); |
| 624 | assert(env->GetStringLength(s6) == byte_array_length); |
| 625 | const char* chars6 = env->GetStringUTFChars(s6, nullptr); |
| 626 | assert(strcmp(test_array, chars6) == 0); |
| 627 | env->ReleaseStringUTFChars(s6, chars6); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 628 | } |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 629 | |
| 630 | extern "C" JNIEXPORT jlong JNICALL Java_Main_testGetMethodID(JNIEnv* env, jclass, jclass c) { |
| 631 | return reinterpret_cast<jlong>(env->GetMethodID(c, "a", "()V")); |
| 632 | } |