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 | |
| 261 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_nativeIsAssignableFrom(JNIEnv* env, jclass, |
| 262 | jclass from, jclass to) { |
| 263 | return env->IsAssignableFrom(from, to); |
| 264 | } |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 265 | |
| 266 | static void testShallowGetCallingClassLoader(JNIEnv* env) { |
| 267 | // Test direct call. |
| 268 | { |
| 269 | jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack"); |
| 270 | assert(vmstack_clazz != nullptr); |
| 271 | assert(!env->ExceptionCheck()); |
| 272 | |
| 273 | jmethodID getCallingClassLoaderMethodId = env->GetStaticMethodID(vmstack_clazz, |
| 274 | "getCallingClassLoader", |
| 275 | "()Ljava/lang/ClassLoader;"); |
| 276 | assert(getCallingClassLoaderMethodId != nullptr); |
| 277 | assert(!env->ExceptionCheck()); |
| 278 | |
| 279 | jobject class_loader = env->CallStaticObjectMethod(vmstack_clazz, |
| 280 | getCallingClassLoaderMethodId); |
| 281 | assert(class_loader == nullptr); |
| 282 | assert(!env->ExceptionCheck()); |
| 283 | } |
| 284 | |
| 285 | // Test one-level call. Use System.loadLibrary(). |
| 286 | { |
| 287 | jclass system_clazz = env->FindClass("java/lang/System"); |
| 288 | assert(system_clazz != nullptr); |
| 289 | assert(!env->ExceptionCheck()); |
| 290 | |
| 291 | jmethodID loadLibraryMethodId = env->GetStaticMethodID(system_clazz, "loadLibrary", |
| 292 | "(Ljava/lang/String;)V"); |
| 293 | assert(loadLibraryMethodId != nullptr); |
| 294 | assert(!env->ExceptionCheck()); |
| 295 | |
| 296 | // Create a string object. |
Nicolas Geoffray | 005f697 | 2014-12-03 18:10:39 +0000 | [diff] [blame] | 297 | jobject library_string = env->NewStringUTF("non_existing_library"); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 298 | assert(library_string != nullptr); |
| 299 | assert(!env->ExceptionCheck()); |
| 300 | |
| 301 | env->CallStaticVoidMethod(system_clazz, loadLibraryMethodId, library_string); |
Nicolas Geoffray | 005f697 | 2014-12-03 18:10:39 +0000 | [diff] [blame] | 302 | assert(env->ExceptionCheck()); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 303 | |
Nicolas Geoffray | 005f697 | 2014-12-03 18:10:39 +0000 | [diff] [blame] | 304 | // We expect UnsatisfiedLinkError. |
| 305 | jthrowable thrown = env->ExceptionOccurred(); |
| 306 | env->ExceptionClear(); |
| 307 | |
| 308 | jclass unsatisfied_link_error_clazz = env->FindClass("java/lang/UnsatisfiedLinkError"); |
| 309 | jclass thrown_class = env->GetObjectClass(thrown); |
| 310 | assert(env->IsSameObject(unsatisfied_link_error_clazz, thrown_class)); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | // http://b/16867274 |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 315 | extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetCallingClassLoader(JNIEnv*, |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 316 | jclass) { |
| 317 | PthreadHelper(&testShallowGetCallingClassLoader); |
| 318 | } |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 319 | |
| 320 | static void testShallowGetStackClass2(JNIEnv* env) { |
| 321 | jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack"); |
| 322 | assert(vmstack_clazz != nullptr); |
| 323 | assert(!env->ExceptionCheck()); |
| 324 | |
| 325 | // Test direct call. |
| 326 | { |
| 327 | jmethodID getStackClass2MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass2", |
| 328 | "()Ljava/lang/Class;"); |
| 329 | assert(getStackClass2MethodId != nullptr); |
| 330 | assert(!env->ExceptionCheck()); |
| 331 | |
| 332 | jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass2MethodId); |
| 333 | assert(caller_class == nullptr); |
| 334 | assert(!env->ExceptionCheck()); |
| 335 | } |
| 336 | |
| 337 | // Test one-level call. Use VMStack.getStackClass1(). |
| 338 | { |
| 339 | jmethodID getStackClass1MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass1", |
| 340 | "()Ljava/lang/Class;"); |
| 341 | assert(getStackClass1MethodId != nullptr); |
| 342 | assert(!env->ExceptionCheck()); |
| 343 | |
| 344 | jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass1MethodId); |
| 345 | assert(caller_class == nullptr); |
| 346 | assert(!env->ExceptionCheck()); |
| 347 | } |
| 348 | |
| 349 | // For better testing we would need to compile against libcore and have a two-deep stack |
| 350 | // ourselves. |
| 351 | } |
| 352 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 353 | extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetStackClass2(JNIEnv*, jclass) { |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 354 | PthreadHelper(&testShallowGetStackClass2); |
| 355 | } |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 356 | |
| 357 | class JniCallNonvirtualVoidMethodTest { |
| 358 | public: |
| 359 | explicit JniCallNonvirtualVoidMethodTest(JNIEnv* env) |
| 360 | : env_(env), |
| 361 | check_jni_ri_(true), |
| 362 | check_jni_android_(true), |
| 363 | super_(GetClass("JniCallNonvirtualTest")), |
| 364 | sub_(GetClass("JniCallNonvirtualTestSubclass")), |
| 365 | super_constructor_(GetMethodID(super_, true, "<init>")), |
| 366 | super_static_(GetMethodID(super_, false, "staticMethod")), |
| 367 | super_nonstatic_(GetMethodID(super_, true, "nonstaticMethod")), |
| 368 | sub_constructor_(GetMethodID(sub_, true, "<init>")), |
| 369 | sub_static_(GetMethodID(sub_, false, "staticMethod")), |
| 370 | sub_nonstatic_(GetMethodID(sub_, true, "nonstaticMethod")), |
| 371 | super_field_(GetFieldID(super_, "nonstaticMethodSuperCalled")), |
| 372 | sub_field_(GetFieldID(super_, "nonstaticMethodSubCalled")) {} |
| 373 | |
| 374 | void Test() { |
| 375 | TestStaticCallNonvirtualMethod(); |
| 376 | TestNewObject(); |
| 377 | TestnonstaticCallNonvirtualMethod(); |
| 378 | } |
| 379 | |
| 380 | JNIEnv* const env_; |
| 381 | |
| 382 | bool const check_jni_ri_; |
| 383 | bool const check_jni_android_; |
| 384 | |
| 385 | jclass const super_; |
| 386 | jclass const sub_; |
| 387 | |
| 388 | jmethodID const super_constructor_; |
| 389 | jmethodID const super_static_; |
| 390 | jmethodID const super_nonstatic_; |
| 391 | jmethodID const sub_constructor_; |
| 392 | jmethodID const sub_static_; |
| 393 | jmethodID const sub_nonstatic_; |
| 394 | |
| 395 | jfieldID const super_field_; |
| 396 | jfieldID const sub_field_; |
| 397 | |
| 398 | private: |
| 399 | jclass GetClass(const char* class_name) { |
| 400 | jclass c = env_->FindClass(class_name); |
| 401 | if (env_->ExceptionCheck()) { |
| 402 | env_->ExceptionDescribe(); |
| 403 | env_->FatalError(__FUNCTION__); |
| 404 | } |
| 405 | assert(!env_->ExceptionCheck()); |
| 406 | assert(c != nullptr); |
| 407 | return c; |
| 408 | } |
| 409 | |
| 410 | jmethodID GetMethodID(jclass c, bool nonstatic, const char* method_name) { |
| 411 | jmethodID m = ((nonstatic) ? |
| 412 | env_->GetMethodID(c, method_name, "()V") : |
| 413 | env_->GetStaticMethodID(c, method_name, "()V")); |
| 414 | if (env_->ExceptionCheck()) { |
| 415 | env_->ExceptionDescribe(); |
| 416 | env_->FatalError(__FUNCTION__); |
| 417 | } |
| 418 | assert(m != nullptr); |
| 419 | return m; |
| 420 | } |
| 421 | |
| 422 | jobject CallConstructor(jclass c, jmethodID m) { |
| 423 | jobject o = env_->NewObject(c, m); |
| 424 | if (env_->ExceptionCheck()) { |
| 425 | env_->ExceptionDescribe(); |
| 426 | env_->FatalError(__FUNCTION__); |
| 427 | } |
| 428 | assert(o != nullptr); |
| 429 | return o; |
| 430 | } |
| 431 | |
| 432 | void CallMethod(jobject o, jclass c, jmethodID m, bool nonstatic, const char* test_case) { |
| 433 | printf("RUNNING %s\n", test_case); |
| 434 | env_->CallNonvirtualVoidMethod(o, c, m); |
| 435 | bool exception_check = env_->ExceptionCheck(); |
| 436 | if (c == nullptr || !nonstatic) { |
| 437 | if (!exception_check) { |
| 438 | printf("FAILED %s due to missing exception\n", test_case); |
| 439 | env_->FatalError("Expected NullPointerException with null jclass"); |
| 440 | } |
| 441 | env_->ExceptionClear(); |
| 442 | } else if (exception_check) { |
| 443 | printf("FAILED %s due to pending exception\n", test_case); |
| 444 | env_->ExceptionDescribe(); |
| 445 | env_->FatalError(test_case); |
| 446 | } |
| 447 | printf("PASSED %s\n", test_case); |
| 448 | } |
| 449 | |
| 450 | jfieldID GetFieldID(jclass c, const char* field_name) { |
| 451 | jfieldID m = env_->GetFieldID(c, field_name, "Z"); |
| 452 | if (env_->ExceptionCheck()) { |
| 453 | env_->ExceptionDescribe(); |
| 454 | env_->FatalError(__FUNCTION__); |
| 455 | } |
| 456 | assert(m != nullptr); |
| 457 | return m; |
| 458 | } |
| 459 | |
| 460 | jboolean GetBooleanField(jobject o, jfieldID f) { |
| 461 | jboolean b = env_->GetBooleanField(o, f); |
| 462 | if (env_->ExceptionCheck()) { |
| 463 | env_->ExceptionDescribe(); |
| 464 | env_->FatalError(__FUNCTION__); |
| 465 | } |
| 466 | return b; |
| 467 | } |
| 468 | |
| 469 | void TestStaticCallNonvirtualMethod() { |
| 470 | if (!check_jni_ri_&& !check_jni_android_) { |
| 471 | CallMethod(nullptr, nullptr, super_static_, false, "null object, null class, super static"); |
| 472 | } |
| 473 | if (!check_jni_android_) { |
| 474 | CallMethod(nullptr, super_, super_static_, false, "null object, super class, super static"); |
| 475 | } |
| 476 | if (!check_jni_android_) { |
| 477 | CallMethod(nullptr, sub_, super_static_, false, "null object, sub class, super static"); |
| 478 | } |
| 479 | |
| 480 | if (!check_jni_ri_ && !check_jni_android_) { |
| 481 | CallMethod(nullptr, nullptr, sub_static_, false, "null object, null class, sub static"); |
| 482 | } |
| 483 | if (!check_jni_android_) { |
| 484 | CallMethod(nullptr, sub_, sub_static_, false, "null object, super class, sub static"); |
| 485 | } |
| 486 | if (!check_jni_android_) { |
| 487 | CallMethod(nullptr, super_, sub_static_, false, "null object, super class, sub static"); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void TestNewObject() { |
| 492 | jobject super_super = CallConstructor(super_, super_constructor_); |
| 493 | jobject super_sub = CallConstructor(super_, sub_constructor_); |
| 494 | jobject sub_super = CallConstructor(sub_, super_constructor_); |
| 495 | jobject sub_sub = CallConstructor(sub_, sub_constructor_); |
| 496 | |
| 497 | assert(env_->IsInstanceOf(super_super, super_)); |
| 498 | assert(!env_->IsInstanceOf(super_super, sub_)); |
| 499 | |
| 500 | // Note that even though we called (and ran) the subclass |
| 501 | // constructor, we are not the subclass. |
| 502 | assert(env_->IsInstanceOf(super_sub, super_)); |
| 503 | assert(!env_->IsInstanceOf(super_sub, sub_)); |
| 504 | |
| 505 | // Note that even though we called the superclass constructor, we |
| 506 | // are still the subclass. |
| 507 | assert(env_->IsInstanceOf(sub_super, super_)); |
| 508 | assert(env_->IsInstanceOf(sub_super, sub_)); |
| 509 | |
| 510 | assert(env_->IsInstanceOf(sub_sub, super_)); |
| 511 | assert(env_->IsInstanceOf(sub_sub, sub_)); |
| 512 | } |
| 513 | |
| 514 | void TestnonstaticCallNonvirtualMethod(bool super_object, bool super_class, bool super_method, const char* test_case) { |
| 515 | if (check_jni_android_) { |
| 516 | if (super_object && !super_method) { |
| 517 | return; // We don't allow a call with sub class method on the super class instance. |
| 518 | } |
| 519 | if (super_class && !super_method) { |
| 520 | return; // We don't allow a call with the sub class method with the super class argument. |
| 521 | } |
| 522 | } |
| 523 | jobject o = ((super_object) ? |
| 524 | CallConstructor(super_, super_constructor_) : |
| 525 | CallConstructor(sub_, sub_constructor_)); |
| 526 | jclass c = (super_class) ? super_ : sub_; |
| 527 | jmethodID m = (super_method) ? super_nonstatic_ : sub_nonstatic_; |
| 528 | CallMethod(o, c, m, true, test_case); |
| 529 | jboolean super_field = GetBooleanField(o, super_field_); |
| 530 | jboolean sub_field = GetBooleanField(o, sub_field_); |
| 531 | assert(super_field == super_method); |
| 532 | assert(sub_field != super_method); |
| 533 | } |
| 534 | |
| 535 | void TestnonstaticCallNonvirtualMethod() { |
| 536 | TestnonstaticCallNonvirtualMethod(true, true, true, "super object, super class, super nonstatic"); |
| 537 | TestnonstaticCallNonvirtualMethod(true, false, true, "super object, sub class, super nonstatic"); |
| 538 | TestnonstaticCallNonvirtualMethod(true, false, false, "super object, sub class, sub nonstatic"); |
| 539 | TestnonstaticCallNonvirtualMethod(true, true, false, "super object, super class, sub nonstatic"); |
| 540 | |
| 541 | TestnonstaticCallNonvirtualMethod(false, true, true, "sub object, super class, super nonstatic"); |
| 542 | TestnonstaticCallNonvirtualMethod(false, false, true, "sub object, sub class, super nonstatic"); |
| 543 | TestnonstaticCallNonvirtualMethod(false, false, false, "sub object, sub class, sub nonstatic"); |
| 544 | TestnonstaticCallNonvirtualMethod(false, true, false, "sub object, super class, sub nonstatic"); |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | extern "C" void JNICALL Java_Main_testCallNonvirtual(JNIEnv* env, jclass) { |
| 549 | JniCallNonvirtualVoidMethodTest(env).Test(); |
| 550 | } |