Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 19 | #include "common_compiler_test.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 20 | #include "java_vm_ext.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "mirror/art_method-inl.h" |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 22 | #include "mirror/string-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | // TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used. |
| 29 | class JniInternalTest : public CommonCompilerTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 30 | protected: |
| 31 | virtual void SetUp() { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 32 | CommonCompilerTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 34 | vm_ = Runtime::Current()->GetJavaVM(); |
| 35 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 36 | // Turn on -verbose:jni for the JNI tests. |
Ian Rogers | 7971363 | 2013-08-21 19:06:15 -0700 | [diff] [blame] | 37 | // gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 38 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 39 | vm_->AttachCurrentThread(&env_, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 40 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 41 | ScopedLocalRef<jclass> aioobe(env_, |
| 42 | env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 43 | CHECK(aioobe.get() != nullptr); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 44 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 46 | ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 47 | CHECK(ase.get() != nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 48 | ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get())); |
| 49 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 50 | ScopedLocalRef<jclass> sioobe(env_, |
| 51 | env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 52 | CHECK(sioobe.get() != nullptr); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 53 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 54 | } |
| 55 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 56 | void ExpectException(jclass exception_class) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 57 | ScopedObjectAccess soa(env_); |
| 58 | EXPECT_TRUE(env_->ExceptionCheck()) |
| 59 | << PrettyDescriptor(soa.Decode<mirror::Class*>(exception_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 60 | jthrowable exception = env_->ExceptionOccurred(); |
| 61 | EXPECT_NE(nullptr, exception); |
| 62 | env_->ExceptionClear(); |
| 63 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); |
| 64 | } |
| 65 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 66 | void CleanUpJniEnv() { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 67 | if (aioobe_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 68 | env_->DeleteGlobalRef(aioobe_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 69 | aioobe_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 70 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 71 | if (ase_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 72 | env_->DeleteGlobalRef(ase_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 73 | ase_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 74 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 75 | if (sioobe_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 76 | env_->DeleteGlobalRef(sioobe_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 77 | sioobe_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 81 | virtual void TearDown() OVERRIDE { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 82 | CleanUpJniEnv(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 83 | CommonCompilerTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 84 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 85 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 86 | jclass GetPrimitiveClass(char descriptor) { |
| 87 | ScopedObjectAccess soa(env_); |
| 88 | mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor); |
| 89 | CHECK(c != nullptr); |
| 90 | return soa.AddLocalReference<jclass>(c); |
| 91 | } |
| 92 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 93 | void ExpectClassFound(const char* name) { |
| 94 | EXPECT_NE(env_->FindClass(name), nullptr) << name; |
| 95 | EXPECT_FALSE(env_->ExceptionCheck()) << name; |
| 96 | } |
| 97 | |
| 98 | void ExpectClassNotFound(const char* name, bool check_jni, const char* check_jni_msg, |
| 99 | CheckJniAbortCatcher* abort_catcher) { |
| 100 | EXPECT_EQ(env_->FindClass(name), nullptr) << name; |
| 101 | if (!check_jni || check_jni_msg == nullptr) { |
| 102 | EXPECT_TRUE(env_->ExceptionCheck()) << name; |
| 103 | env_->ExceptionClear(); |
| 104 | } else { |
| 105 | abort_catcher->Check(check_jni_msg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void FindClassTest(bool check_jni) { |
| 110 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 111 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 112 | |
| 113 | // Null argument is always an abort. |
| 114 | env_->FindClass(nullptr); |
| 115 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 116 | : "name == null"); |
| 117 | |
| 118 | // Reference types... |
| 119 | ExpectClassFound("java/lang/String"); |
| 120 | // ...for arrays too, where you must include "L;". |
| 121 | ExpectClassFound("[Ljava/lang/String;"); |
| 122 | // Primitive arrays are okay too, if the primitive type is valid. |
| 123 | ExpectClassFound("[C"); |
| 124 | |
| 125 | // But primitive types aren't allowed... |
| 126 | ExpectClassNotFound("C", check_jni, nullptr, &check_jni_abort_catcher); |
| 127 | ExpectClassNotFound("V", check_jni, nullptr, &check_jni_abort_catcher); |
| 128 | ExpectClassNotFound("K", check_jni, nullptr, &check_jni_abort_catcher); |
| 129 | |
| 130 | if (check_jni) { |
| 131 | // Check JNI will reject invalid class names as aborts but without pending exceptions. |
| 132 | EXPECT_EQ(env_->FindClass("java.lang.String"), nullptr); |
| 133 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 134 | check_jni_abort_catcher.Check("illegal class name 'java.lang.String'"); |
| 135 | |
| 136 | EXPECT_EQ(env_->FindClass("[Ljava.lang.String;"), nullptr); |
| 137 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 138 | check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'"); |
| 139 | } else { |
| 140 | // Without check JNI we're tolerant and replace '.' with '/'. |
| 141 | ExpectClassFound("java.lang.String"); |
| 142 | ExpectClassFound("[Ljava.lang.String;"); |
| 143 | } |
| 144 | |
| 145 | ExpectClassNotFound("Ljava.lang.String;", check_jni, "illegal class name 'Ljava.lang.String;'", |
| 146 | &check_jni_abort_catcher); |
| 147 | ExpectClassNotFound("[java.lang.String", check_jni, "illegal class name '[java.lang.String'", |
| 148 | &check_jni_abort_catcher); |
| 149 | |
| 150 | // You can't include the "L;" in a JNI class descriptor. |
| 151 | ExpectClassNotFound("Ljava/lang/String;", check_jni, "illegal class name 'Ljava/lang/String;'", |
| 152 | &check_jni_abort_catcher); |
| 153 | |
| 154 | // But you must include it for an array of any reference type. |
| 155 | ExpectClassNotFound("[java/lang/String", check_jni, "illegal class name '[java/lang/String'", |
| 156 | &check_jni_abort_catcher); |
| 157 | |
| 158 | ExpectClassNotFound("[K", check_jni, "illegal class name '[K'", &check_jni_abort_catcher); |
| 159 | |
| 160 | // Void arrays aren't allowed. |
| 161 | ExpectClassNotFound("[V", check_jni, "illegal class name '[V'", &check_jni_abort_catcher); |
| 162 | |
| 163 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 164 | } |
| 165 | |
| 166 | void GetFieldIdBadArgumentTest(bool check_jni) { |
| 167 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 168 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 169 | |
| 170 | jclass c = env_->FindClass("java/lang/String"); |
| 171 | ASSERT_NE(c, nullptr); |
| 172 | |
| 173 | jfieldID fid = env_->GetFieldID(nullptr, "count", "I"); |
| 174 | EXPECT_EQ(nullptr, fid); |
| 175 | check_jni_abort_catcher.Check(check_jni ? "GetFieldID received NULL jclass" |
| 176 | : "java_class == null"); |
| 177 | fid = env_->GetFieldID(c, nullptr, "I"); |
| 178 | EXPECT_EQ(nullptr, fid); |
| 179 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 180 | : "name == null"); |
| 181 | fid = env_->GetFieldID(c, "count", nullptr); |
| 182 | EXPECT_EQ(nullptr, fid); |
| 183 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 184 | : "sig == null"); |
| 185 | |
| 186 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 187 | } |
| 188 | |
| 189 | void GetStaticFieldIdBadArgumentTest(bool check_jni) { |
| 190 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 191 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 192 | |
| 193 | jclass c = env_->FindClass("java/lang/String"); |
| 194 | ASSERT_NE(c, nullptr); |
| 195 | |
| 196 | jfieldID fid = env_->GetStaticFieldID(nullptr, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 197 | EXPECT_EQ(nullptr, fid); |
| 198 | check_jni_abort_catcher.Check(check_jni ? "GetStaticFieldID received NULL jclass" |
| 199 | : "java_class == null"); |
| 200 | fid = env_->GetStaticFieldID(c, nullptr, "Ljava/util/Comparator;"); |
| 201 | EXPECT_EQ(nullptr, fid); |
| 202 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 203 | : "name == null"); |
| 204 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", nullptr); |
| 205 | EXPECT_EQ(nullptr, fid); |
| 206 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 207 | : "sig == null"); |
| 208 | |
| 209 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 210 | } |
| 211 | |
| 212 | void GetMethodIdBadArgumentTest(bool check_jni) { |
| 213 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 214 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 215 | |
| 216 | jmethodID method = env_->GetMethodID(nullptr, "<init>", "(Ljava/lang/String;)V"); |
| 217 | EXPECT_EQ(nullptr, method); |
| 218 | check_jni_abort_catcher.Check(check_jni ? "GetMethodID received NULL jclass" |
| 219 | : "java_class == null"); |
| 220 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 221 | ASSERT_TRUE(jlnsme != nullptr); |
| 222 | method = env_->GetMethodID(jlnsme, nullptr, "(Ljava/lang/String;)V"); |
| 223 | EXPECT_EQ(nullptr, method); |
| 224 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 225 | : "name == null"); |
| 226 | method = env_->GetMethodID(jlnsme, "<init>", nullptr); |
| 227 | EXPECT_EQ(nullptr, method); |
| 228 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 229 | : "sig == null"); |
| 230 | |
| 231 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 232 | } |
| 233 | |
| 234 | void GetStaticMethodIdBadArgumentTest(bool check_jni) { |
| 235 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 236 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 237 | |
| 238 | jmethodID method = env_->GetStaticMethodID(nullptr, "valueOf", "(I)Ljava/lang/String;"); |
| 239 | EXPECT_EQ(nullptr, method); |
| 240 | check_jni_abort_catcher.Check(check_jni ? "GetStaticMethodID received NULL jclass" |
| 241 | : "java_class == null"); |
| 242 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 243 | method = env_->GetStaticMethodID(jlstring, nullptr, "(I)Ljava/lang/String;"); |
| 244 | EXPECT_EQ(nullptr, method); |
| 245 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 246 | : "name == null"); |
| 247 | method = env_->GetStaticMethodID(jlstring, "valueOf", nullptr); |
| 248 | EXPECT_EQ(nullptr, method); |
| 249 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 250 | : "sig == null"); |
| 251 | |
| 252 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 253 | } |
| 254 | |
| 255 | void GetFromReflectedField_ToReflectedFieldBadArgumentTest(bool check_jni) { |
| 256 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 257 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 258 | |
| 259 | jclass c = env_->FindClass("java/lang/String"); |
| 260 | ASSERT_NE(c, nullptr); |
| 261 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 262 | ASSERT_NE(fid, nullptr); |
| 263 | |
| 264 | // Check class argument for null argument, not checked in non-check JNI. |
| 265 | jobject field = env_->ToReflectedField(nullptr, fid, JNI_FALSE); |
| 266 | if (check_jni) { |
| 267 | EXPECT_EQ(field, nullptr); |
| 268 | check_jni_abort_catcher.Check("ToReflectedField received NULL jclass"); |
| 269 | } else { |
| 270 | EXPECT_NE(field, nullptr); |
| 271 | } |
| 272 | |
| 273 | field = env_->ToReflectedField(c, nullptr, JNI_FALSE); |
| 274 | EXPECT_EQ(field, nullptr); |
| 275 | check_jni_abort_catcher.Check(check_jni ? "jfieldID was NULL" |
| 276 | : "fid == null"); |
| 277 | |
| 278 | fid = env_->FromReflectedField(nullptr); |
| 279 | ASSERT_EQ(fid, nullptr); |
| 280 | check_jni_abort_catcher.Check(check_jni ? "expected non-null java.lang.reflect.Field" |
| 281 | : "jlr_field == null"); |
| 282 | |
| 283 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 284 | } |
| 285 | |
| 286 | void GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(bool check_jni) { |
| 287 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 288 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 289 | |
| 290 | jclass c = env_->FindClass("java/lang/String"); |
| 291 | ASSERT_NE(c, nullptr); |
| 292 | jmethodID mid = env_->GetMethodID(c, "<init>", "()V"); |
| 293 | ASSERT_NE(mid, nullptr); |
| 294 | |
| 295 | // Check class argument for null argument, not checked in non-check JNI. |
| 296 | jobject method = env_->ToReflectedMethod(nullptr, mid, JNI_FALSE); |
| 297 | if (check_jni) { |
| 298 | EXPECT_EQ(method, nullptr); |
| 299 | check_jni_abort_catcher.Check("ToReflectedMethod received NULL jclass"); |
| 300 | } else { |
| 301 | EXPECT_NE(method, nullptr); |
| 302 | } |
| 303 | |
| 304 | method = env_->ToReflectedMethod(c, nullptr, JNI_FALSE); |
| 305 | EXPECT_EQ(method, nullptr); |
| 306 | check_jni_abort_catcher.Check(check_jni ? "jmethodID was NULL" |
| 307 | : "mid == null"); |
| 308 | mid = env_->FromReflectedMethod(method); |
| 309 | ASSERT_EQ(mid, nullptr); |
| 310 | check_jni_abort_catcher.Check(check_jni ? "expected non-null method" : "jlr_method == null"); |
| 311 | |
| 312 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 313 | } |
| 314 | |
| 315 | void RegisterAndUnregisterNativesBadArguments(bool check_jni, |
| 316 | CheckJniAbortCatcher* check_jni_abort_catcher) { |
| 317 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 318 | // Passing a class of null is a failure. |
| 319 | { |
| 320 | JNINativeMethod methods[] = { }; |
| 321 | EXPECT_EQ(env_->RegisterNatives(nullptr, methods, 0), JNI_ERR); |
| 322 | check_jni_abort_catcher->Check(check_jni ? "RegisterNatives received NULL jclass" |
| 323 | : "java_class == null"); |
| 324 | } |
| 325 | |
| 326 | // Passing methods as null is a failure. |
| 327 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 328 | EXPECT_EQ(env_->RegisterNatives(jlobject, nullptr, 1), JNI_ERR); |
| 329 | check_jni_abort_catcher->Check("methods == null"); |
| 330 | |
| 331 | // Unregisters null is a failure. |
| 332 | EXPECT_EQ(env_->UnregisterNatives(nullptr), JNI_ERR); |
| 333 | check_jni_abort_catcher->Check(check_jni ? "UnregisterNatives received NULL jclass" |
| 334 | : "java_class == null"); |
| 335 | |
| 336 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | void GetPrimitiveArrayElementsOfWrongType(bool check_jni) { |
| 341 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 342 | CheckJniAbortCatcher jni_abort_catcher; |
| 343 | |
| 344 | jbooleanArray array = env_->NewBooleanArray(10); |
| 345 | jboolean is_copy; |
| 346 | EXPECT_EQ(env_->GetByteArrayElements(reinterpret_cast<jbyteArray>(array), &is_copy), nullptr); |
| 347 | jni_abort_catcher.Check( |
| 348 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 349 | : "attempt to get byte primitive array elements with an object of type boolean[]"); |
| 350 | EXPECT_EQ(env_->GetShortArrayElements(reinterpret_cast<jshortArray>(array), &is_copy), nullptr); |
| 351 | jni_abort_catcher.Check( |
| 352 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 353 | : "attempt to get short primitive array elements with an object of type boolean[]"); |
| 354 | EXPECT_EQ(env_->GetCharArrayElements(reinterpret_cast<jcharArray>(array), &is_copy), nullptr); |
| 355 | jni_abort_catcher.Check( |
| 356 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 357 | : "attempt to get char primitive array elements with an object of type boolean[]"); |
| 358 | EXPECT_EQ(env_->GetIntArrayElements(reinterpret_cast<jintArray>(array), &is_copy), nullptr); |
| 359 | jni_abort_catcher.Check( |
| 360 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 361 | : "attempt to get int primitive array elements with an object of type boolean[]"); |
| 362 | EXPECT_EQ(env_->GetLongArrayElements(reinterpret_cast<jlongArray>(array), &is_copy), nullptr); |
| 363 | jni_abort_catcher.Check( |
| 364 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 365 | : "attempt to get long primitive array elements with an object of type boolean[]"); |
| 366 | EXPECT_EQ(env_->GetFloatArrayElements(reinterpret_cast<jfloatArray>(array), &is_copy), nullptr); |
| 367 | jni_abort_catcher.Check( |
| 368 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 369 | : "attempt to get float primitive array elements with an object of type boolean[]"); |
| 370 | EXPECT_EQ(env_->GetDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), &is_copy), nullptr); |
| 371 | jni_abort_catcher.Check( |
| 372 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 373 | : "attempt to get double primitive array elements with an object of type boolean[]"); |
| 374 | jbyteArray array2 = env_->NewByteArray(10); |
| 375 | EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), &is_copy), |
| 376 | nullptr); |
| 377 | jni_abort_catcher.Check( |
| 378 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 379 | : "attempt to get boolean primitive array elements with an object of type byte[]"); |
| 380 | jobject object = env_->NewStringUTF("Test String"); |
| 381 | EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), &is_copy), |
| 382 | nullptr); |
| 383 | jni_abort_catcher.Check( |
| 384 | check_jni ? "jarray argument has non-array type: java.lang.String" |
| 385 | : "attempt to get boolean primitive array elements with an object of type java.lang.String"); |
| 386 | |
| 387 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 388 | } |
| 389 | |
| 390 | void ReleasePrimitiveArrayElementsOfWrongType(bool check_jni) { |
| 391 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 392 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | dd11d2a | 2014-11-19 10:06:46 -0800 | [diff] [blame^] | 393 | { |
| 394 | jbooleanArray array = env_->NewBooleanArray(10); |
| 395 | ASSERT_TRUE(array != nullptr); |
| 396 | jboolean is_copy; |
| 397 | jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy); |
| 398 | ASSERT_TRUE(elements != nullptr); |
| 399 | env_->ReleaseByteArrayElements(reinterpret_cast<jbyteArray>(array), |
| 400 | reinterpret_cast<jbyte*>(elements), 0); |
| 401 | jni_abort_catcher.Check( |
| 402 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 403 | : "attempt to release byte primitive array elements with an object of type boolean[]"); |
| 404 | env_->ReleaseShortArrayElements(reinterpret_cast<jshortArray>(array), |
| 405 | reinterpret_cast<jshort*>(elements), 0); |
| 406 | jni_abort_catcher.Check( |
| 407 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 408 | : "attempt to release short primitive array elements with an object of type boolean[]"); |
| 409 | env_->ReleaseCharArrayElements(reinterpret_cast<jcharArray>(array), |
| 410 | reinterpret_cast<jchar*>(elements), 0); |
| 411 | jni_abort_catcher.Check( |
| 412 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 413 | : "attempt to release char primitive array elements with an object of type boolean[]"); |
| 414 | env_->ReleaseIntArrayElements(reinterpret_cast<jintArray>(array), |
| 415 | reinterpret_cast<jint*>(elements), 0); |
| 416 | jni_abort_catcher.Check( |
| 417 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 418 | : "attempt to release int primitive array elements with an object of type boolean[]"); |
| 419 | env_->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 420 | reinterpret_cast<jlong*>(elements), 0); |
| 421 | jni_abort_catcher.Check( |
| 422 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 423 | : "attempt to release long primitive array elements with an object of type boolean[]"); |
| 424 | env_->ReleaseFloatArrayElements(reinterpret_cast<jfloatArray>(array), |
| 425 | reinterpret_cast<jfloat*>(elements), 0); |
| 426 | jni_abort_catcher.Check( |
| 427 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 428 | : "attempt to release float primitive array elements with an object of type boolean[]"); |
| 429 | env_->ReleaseDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), |
| 430 | reinterpret_cast<jdouble*>(elements), 0); |
| 431 | jni_abort_catcher.Check( |
| 432 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 433 | : "attempt to release double primitive array elements with an object of type boolean[]"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 434 | |
Ian Rogers | dd11d2a | 2014-11-19 10:06:46 -0800 | [diff] [blame^] | 435 | // Don't leak the elements array. |
| 436 | env_->ReleaseBooleanArrayElements(array, elements, 0); |
| 437 | } |
| 438 | { |
| 439 | jbyteArray array = env_->NewByteArray(10); |
| 440 | jboolean is_copy; |
| 441 | jbyte* elements = env_->GetByteArrayElements(array, &is_copy); |
| 442 | |
| 443 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(array), |
| 444 | reinterpret_cast<jboolean*>(elements), 0); |
| 445 | jni_abort_catcher.Check( |
| 446 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 447 | : "attempt to release boolean primitive array elements with an object of type byte[]"); |
| 448 | jobject object = env_->NewStringUTF("Test String"); |
| 449 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), |
| 450 | reinterpret_cast<jboolean*>(elements), 0); |
| 451 | jni_abort_catcher.Check( |
| 452 | check_jni ? "jarray argument has non-array type: java.lang.String" |
| 453 | : "attempt to release boolean primitive array elements with an object of type " |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 454 | "java.lang.String"); |
| 455 | |
Ian Rogers | dd11d2a | 2014-11-19 10:06:46 -0800 | [diff] [blame^] | 456 | // Don't leak the elements array. |
| 457 | env_->ReleaseByteArrayElements(array, elements, 0); |
| 458 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 459 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 460 | } |
| 461 | |
| 462 | void GetReleasePrimitiveArrayCriticalOfWrongType(bool check_jni) { |
| 463 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 464 | CheckJniAbortCatcher jni_abort_catcher; |
| 465 | |
| 466 | jobject object = env_->NewStringUTF("Test String"); |
| 467 | jboolean is_copy; |
| 468 | void* elements = env_->GetPrimitiveArrayCritical(reinterpret_cast<jarray>(object), &is_copy); |
| 469 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 470 | : "expected primitive array, given java.lang.String"); |
| 471 | env_->ReleasePrimitiveArrayCritical(reinterpret_cast<jarray>(object), elements, 0); |
| 472 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 473 | : "expected primitive array, given java.lang.String"); |
| 474 | |
| 475 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 476 | } |
| 477 | |
| 478 | void GetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) { |
| 479 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 480 | CheckJniAbortCatcher jni_abort_catcher; |
| 481 | constexpr size_t kLength = 10; |
| 482 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 483 | ASSERT_TRUE(array != nullptr); |
| 484 | jboolean elements[kLength]; |
| 485 | env_->GetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 486 | reinterpret_cast<jbyte*>(elements)); |
| 487 | jni_abort_catcher.Check( |
| 488 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 489 | : "attempt to get region of byte primitive array elements with an object of type boolean[]"); |
| 490 | env_->GetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 491 | reinterpret_cast<jshort*>(elements)); |
| 492 | jni_abort_catcher.Check( |
| 493 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 494 | : "attempt to get region of short primitive array elements with an object of type boolean[]"); |
| 495 | env_->GetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 496 | reinterpret_cast<jchar*>(elements)); |
| 497 | jni_abort_catcher.Check( |
| 498 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 499 | : "attempt to get region of char primitive array elements with an object of type boolean[]"); |
| 500 | env_->GetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 501 | reinterpret_cast<jint*>(elements)); |
| 502 | jni_abort_catcher.Check( |
| 503 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 504 | : "attempt to get region of int primitive array elements with an object of type boolean[]"); |
| 505 | env_->GetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 506 | reinterpret_cast<jlong*>(elements)); |
| 507 | jni_abort_catcher.Check( |
| 508 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 509 | : "attempt to get region of long primitive array elements with an object of type boolean[]"); |
| 510 | env_->GetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 511 | reinterpret_cast<jfloat*>(elements)); |
| 512 | jni_abort_catcher.Check( |
| 513 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 514 | : "attempt to get region of float primitive array elements with an object of type boolean[]"); |
| 515 | env_->GetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 516 | reinterpret_cast<jdouble*>(elements)); |
| 517 | jni_abort_catcher.Check( |
| 518 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 519 | : "attempt to get region of double primitive array elements with an object of type boolean[]"); |
| 520 | jbyteArray array2 = env_->NewByteArray(10); |
| 521 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 522 | reinterpret_cast<jboolean*>(elements)); |
| 523 | jni_abort_catcher.Check( |
| 524 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 525 | : "attempt to get region of boolean primitive array elements with an object of type byte[]"); |
| 526 | jobject object = env_->NewStringUTF("Test String"); |
| 527 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 528 | reinterpret_cast<jboolean*>(elements)); |
| 529 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 530 | : "attempt to get region of boolean primitive array elements with an object of type " |
| 531 | "java.lang.String"); |
| 532 | |
| 533 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 534 | } |
| 535 | |
| 536 | void SetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) { |
| 537 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 538 | CheckJniAbortCatcher jni_abort_catcher; |
| 539 | constexpr size_t kLength = 10; |
| 540 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 541 | ASSERT_TRUE(array != nullptr); |
| 542 | jboolean elements[kLength]; |
| 543 | env_->SetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 544 | reinterpret_cast<jbyte*>(elements)); |
| 545 | jni_abort_catcher.Check( |
| 546 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 547 | : "attempt to set region of byte primitive array elements with an object of type boolean[]"); |
| 548 | env_->SetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 549 | reinterpret_cast<jshort*>(elements)); |
| 550 | jni_abort_catcher.Check( |
| 551 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 552 | : "attempt to set region of short primitive array elements with an object of type boolean[]"); |
| 553 | env_->SetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 554 | reinterpret_cast<jchar*>(elements)); |
| 555 | jni_abort_catcher.Check( |
| 556 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 557 | : "attempt to set region of char primitive array elements with an object of type boolean[]"); |
| 558 | env_->SetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 559 | reinterpret_cast<jint*>(elements)); |
| 560 | jni_abort_catcher.Check( |
| 561 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 562 | : "attempt to set region of int primitive array elements with an object of type boolean[]"); |
| 563 | env_->SetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 564 | reinterpret_cast<jlong*>(elements)); |
| 565 | jni_abort_catcher.Check( |
| 566 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 567 | : "attempt to set region of long primitive array elements with an object of type boolean[]"); |
| 568 | env_->SetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 569 | reinterpret_cast<jfloat*>(elements)); |
| 570 | jni_abort_catcher.Check( |
| 571 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 572 | : "attempt to set region of float primitive array elements with an object of type boolean[]"); |
| 573 | env_->SetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 574 | reinterpret_cast<jdouble*>(elements)); |
| 575 | jni_abort_catcher.Check( |
| 576 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 577 | : "attempt to set region of double primitive array elements with an object of type boolean[]"); |
| 578 | jbyteArray array2 = env_->NewByteArray(10); |
| 579 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 580 | reinterpret_cast<jboolean*>(elements)); |
| 581 | jni_abort_catcher.Check( |
| 582 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 583 | : "attempt to set region of boolean primitive array elements with an object of type byte[]"); |
| 584 | jobject object = env_->NewStringUTF("Test String"); |
| 585 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 586 | reinterpret_cast<jboolean*>(elements)); |
| 587 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 588 | : "attempt to set region of boolean primitive array elements with an object of type " |
| 589 | "java.lang.String"); |
| 590 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 591 | } |
| 592 | |
| 593 | void NewObjectArrayBadArguments(bool check_jni) { |
| 594 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 595 | CheckJniAbortCatcher jni_abort_catcher; |
| 596 | |
| 597 | jclass element_class = env_->FindClass("java/lang/String"); |
| 598 | ASSERT_NE(element_class, nullptr); |
| 599 | |
| 600 | env_->NewObjectArray(-1, element_class, nullptr); |
| 601 | jni_abort_catcher.Check(check_jni ? "negative jsize: -1" : "negative array length: -1"); |
| 602 | |
| 603 | env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr); |
| 604 | jni_abort_catcher.Check(check_jni ? "negative jsize: -2147483648" |
| 605 | : "negative array length: -2147483648"); |
| 606 | |
| 607 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 608 | } |
| 609 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 610 | JavaVMExt* vm_; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 611 | JNIEnv* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 612 | jclass aioobe_; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 613 | jclass ase_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 614 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 615 | }; |
| 616 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 617 | TEST_F(JniInternalTest, AllocObject) { |
| 618 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 619 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 620 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 621 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 622 | |
| 623 | // We have an instance of the class we asked for... |
| 624 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 625 | // ...whose fields haven't been initialized because |
| 626 | // we didn't call a constructor. |
| 627 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 628 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 629 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 632 | TEST_F(JniInternalTest, GetVersion) { |
| 633 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 634 | } |
| 635 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 636 | TEST_F(JniInternalTest, FindClass) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 637 | FindClassTest(false); |
| 638 | FindClassTest(true); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 641 | TEST_F(JniInternalTest, GetFieldID) { |
| 642 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 643 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 644 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 645 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 646 | |
| 647 | // Wrong type. |
| 648 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 649 | EXPECT_EQ(nullptr, fid); |
| 650 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 651 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 652 | // Wrong type where type doesn't exist. |
| 653 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 654 | EXPECT_EQ(nullptr, fid); |
| 655 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 656 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 657 | // Wrong name. |
| 658 | fid = env_->GetFieldID(c, "Count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 659 | EXPECT_EQ(nullptr, fid); |
| 660 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 661 | |
| 662 | // Good declared field lookup. |
| 663 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 664 | EXPECT_NE(nullptr, fid); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 665 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 666 | |
| 667 | // Good superclass field lookup. |
| 668 | c = env_->FindClass("java/lang/StringBuilder"); |
| 669 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 670 | EXPECT_NE(nullptr, fid); |
| 671 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 672 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 673 | |
| 674 | // Not instance. |
| 675 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 676 | EXPECT_EQ(nullptr, fid); |
| 677 | ExpectException(jlnsfe); |
| 678 | |
| 679 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 680 | GetFieldIdBadArgumentTest(false); |
| 681 | GetFieldIdBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 685 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 686 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 687 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 688 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 689 | |
| 690 | // Wrong type. |
| 691 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 692 | EXPECT_EQ(nullptr, fid); |
| 693 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 694 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 695 | // Wrong type where type doesn't exist. |
| 696 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 697 | EXPECT_EQ(nullptr, fid); |
| 698 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 699 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 700 | // Wrong name. |
| 701 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 702 | EXPECT_EQ(nullptr, fid); |
| 703 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 704 | |
| 705 | // Good declared field lookup. |
| 706 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 707 | EXPECT_NE(nullptr, fid); |
| 708 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 709 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 710 | |
| 711 | // Not static. |
| 712 | fid = env_->GetStaticFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 713 | EXPECT_EQ(nullptr, fid); |
| 714 | ExpectException(jlnsfe); |
| 715 | |
| 716 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 717 | GetStaticFieldIdBadArgumentTest(false); |
| 718 | GetStaticFieldIdBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 721 | TEST_F(JniInternalTest, GetMethodID) { |
| 722 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 723 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 724 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 725 | jclass jncrbc = env_->FindClass("java/nio/channels/ReadableByteChannel"); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 726 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 727 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 728 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 729 | |
| 730 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 731 | // a pending exception. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 732 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 733 | EXPECT_EQ(nullptr, method); |
| 734 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 735 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 736 | // Check that java.lang.Object.equals() does exist. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 737 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 738 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 739 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 740 | |
| 741 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 742 | // method is static. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 743 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 744 | EXPECT_EQ(nullptr, method); |
| 745 | ExpectException(jlnsme); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 746 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 747 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 748 | method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 749 | EXPECT_NE(nullptr, method); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 750 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 751 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 752 | // Check that GetMethodID can find a interface method inherited from another interface. |
| 753 | method = env_->GetMethodID(jncrbc, "close", "()V"); |
| 754 | EXPECT_NE(nullptr, method); |
| 755 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 756 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 757 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 758 | GetMethodIdBadArgumentTest(false); |
| 759 | GetMethodIdBadArgumentTest(true); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 762 | TEST_F(JniInternalTest, CallVoidMethodNullReceiver) { |
| 763 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 764 | jmethodID method; |
| 765 | |
| 766 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
| 767 | method = env_->GetMethodID(jlobject, "<init>", "()V"); |
| 768 | EXPECT_NE(nullptr, method); |
| 769 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 770 | |
| 771 | // Null object to CallVoidMethod. |
| 772 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 773 | env_->CallVoidMethod(nullptr, method); |
| 774 | check_jni_abort_catcher.Check("null"); |
| 775 | } |
| 776 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 777 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 778 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 779 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 780 | |
| 781 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 782 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 783 | |
| 784 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 785 | // a pending exception |
| 786 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 787 | EXPECT_EQ(nullptr, method); |
| 788 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 789 | |
| 790 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 791 | // the method is not static |
| 792 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 793 | EXPECT_EQ(nullptr, method); |
| 794 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 795 | |
| 796 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 797 | jclass jlstring = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 798 | method = env_->GetStaticMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 799 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 800 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 801 | |
| 802 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 803 | GetStaticMethodIdBadArgumentTest(false); |
| 804 | GetStaticMethodIdBadArgumentTest(true); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 807 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 808 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 809 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 810 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 811 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 812 | ASSERT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 813 | // Turn the fid into a java.lang.reflect.Field... |
| 814 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
Mathieu Chartier | 7895946 | 2014-11-17 10:24:04 -0800 | [diff] [blame] | 815 | for (size_t i = 0; i <= kLocalsMax; ++i) { |
Mathieu Chartier | 41da596 | 2014-11-15 13:07:39 -0800 | [diff] [blame] | 816 | // Regression test for b/18396311, ToReflectedField leaking local refs causing a local |
| 817 | // reference table overflows with 512 references to ArtField |
| 818 | env_->DeleteLocalRef(env_->ToReflectedField(c, fid, JNI_FALSE)); |
| 819 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 820 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 821 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 822 | // ...and back again. |
| 823 | jfieldID fid2 = env_->FromReflectedField(field); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 824 | ASSERT_NE(fid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 825 | // Make sure we can actually use it. |
| 826 | jstring s = env_->NewStringUTF("poop"); |
| 827 | ASSERT_EQ(4, env_->GetIntField(s, fid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 828 | |
| 829 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 830 | GetFromReflectedField_ToReflectedFieldBadArgumentTest(false); |
| 831 | GetFromReflectedField_ToReflectedFieldBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 835 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 836 | ASSERT_NE(jlrMethod, nullptr); |
| 837 | jclass jlrConstructor = env_->FindClass("java/lang/reflect/Constructor"); |
| 838 | ASSERT_NE(jlrConstructor, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 839 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 840 | ASSERT_NE(c, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 841 | |
| 842 | jmethodID mid = env_->GetMethodID(c, "<init>", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 843 | ASSERT_NE(mid, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 844 | // Turn the mid into a java.lang.reflect.Constructor... |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 845 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
Mathieu Chartier | 7895946 | 2014-11-17 10:24:04 -0800 | [diff] [blame] | 846 | for (size_t i = 0; i <= kLocalsMax; ++i) { |
Mathieu Chartier | 41da596 | 2014-11-15 13:07:39 -0800 | [diff] [blame] | 847 | // Regression test for b/18396311, ToReflectedMethod leaking local refs causing a local |
| 848 | // reference table overflows with 512 references to ArtMethod |
| 849 | env_->DeleteLocalRef(env_->ToReflectedMethod(c, mid, JNI_FALSE)); |
| 850 | } |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 851 | ASSERT_NE(method, nullptr); |
| 852 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrConstructor)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 853 | // ...and back again. |
| 854 | jmethodID mid2 = env_->FromReflectedMethod(method); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 855 | ASSERT_NE(mid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 856 | // Make sure we can actually use it. |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 857 | jstring s = reinterpret_cast<jstring>(env_->AllocObject(c)); |
| 858 | ASSERT_NE(s, nullptr); |
| 859 | env_->CallVoidMethod(s, mid2); |
| 860 | ASSERT_EQ(JNI_FALSE, env_->ExceptionCheck()); |
| 861 | |
| 862 | mid = env_->GetMethodID(c, "length", "()I"); |
| 863 | ASSERT_NE(mid, nullptr); |
| 864 | // Turn the mid into a java.lang.reflect.Method... |
| 865 | method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 866 | ASSERT_NE(method, nullptr); |
| 867 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 868 | // ...and back again. |
| 869 | mid2 = env_->FromReflectedMethod(method); |
| 870 | ASSERT_NE(mid2, nullptr); |
| 871 | // Make sure we can actually use it. |
| 872 | s = env_->NewStringUTF("poop"); |
| 873 | ASSERT_NE(s, nullptr); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 874 | ASSERT_EQ(4, env_->CallIntMethod(s, mid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 875 | |
| 876 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 877 | GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(false); |
| 878 | GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 881 | static void BogusMethod() { |
| 882 | // You can't pass nullptr function pointers to RegisterNatives. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 885 | TEST_F(JniInternalTest, RegisterAndUnregisterNatives) { |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 886 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 887 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 888 | void* native_function = reinterpret_cast<void*>(BogusMethod); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 889 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 890 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 891 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 892 | |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 893 | // Check that registering method without name causes a NoSuchMethodError. |
| 894 | { |
| 895 | JNINativeMethod methods[] = { { nullptr, "()V", native_function } }; |
| 896 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 897 | } |
| 898 | ExpectException(jlnsme); |
| 899 | |
| 900 | // Check that registering method without signature causes a NoSuchMethodError. |
| 901 | { |
| 902 | JNINativeMethod methods[] = { { "notify", nullptr, native_function } }; |
| 903 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 904 | } |
| 905 | ExpectException(jlnsme); |
| 906 | |
| 907 | // Check that registering method without function causes a NoSuchMethodError. |
| 908 | { |
| 909 | JNINativeMethod methods[] = { { "notify", "()V", nullptr } }; |
| 910 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 911 | } |
| 912 | ExpectException(jlnsme); |
| 913 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 914 | // Check that registering to a non-existent java.lang.Object.foo() causes a NoSuchMethodError. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 915 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 916 | JNINativeMethod methods[] = { { "foo", "()V", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 917 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 918 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 919 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 920 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 921 | // Check that registering non-native methods causes a NoSuchMethodError. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 922 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 923 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 924 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 925 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 926 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 927 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 928 | // Check that registering native methods is successful. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 929 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 930 | JNINativeMethod methods[] = { { "notify", "()V", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 931 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_OK); |
| 932 | } |
| 933 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 934 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 935 | |
| 936 | // Check that registering no methods isn't a failure. |
| 937 | { |
| 938 | JNINativeMethod methods[] = { }; |
| 939 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 0), JNI_OK); |
| 940 | } |
| 941 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 942 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 943 | |
| 944 | // Check that registering a -ve number of methods is a failure. |
| 945 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 946 | for (int i = -10; i < 0; ++i) { |
| 947 | JNINativeMethod methods[] = { }; |
| 948 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, i), JNI_ERR); |
| 949 | check_jni_abort_catcher.Check("negative method count: "); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 950 | } |
| 951 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 952 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 953 | // Unregistering a class with no natives is a warning. |
| 954 | EXPECT_EQ(env_->UnregisterNatives(jlnsme), JNI_OK); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 955 | |
| 956 | RegisterAndUnregisterNativesBadArguments(false, &check_jni_abort_catcher); |
| 957 | RegisterAndUnregisterNativesBadArguments(true, &check_jni_abort_catcher); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 960 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, \ |
| 961 | get_region_fn, \ |
| 962 | set_region_fn, \ |
| 963 | get_elements_fn, \ |
| 964 | release_elements_fn, \ |
| 965 | scalar_type, \ |
| 966 | expected_class_descriptor) \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 967 | jsize size = 4; \ |
| 968 | \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 969 | { \ |
| 970 | CheckJniAbortCatcher jni_abort_catcher; \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 971 | down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(false); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 972 | /* Allocate an negative sized array and check it has the right failure type. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 973 | EXPECT_EQ(env_->new_fn(-1), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 974 | jni_abort_catcher.Check("negative array length: -1"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 975 | EXPECT_EQ(env_->new_fn(std::numeric_limits<jint>::min()), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 976 | jni_abort_catcher.Check("negative array length: -2147483648"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 977 | /* Pass the array as null. */ \ |
| 978 | EXPECT_EQ(0, env_->GetArrayLength(nullptr)); \ |
| 979 | jni_abort_catcher.Check("java_array == null"); \ |
| 980 | env_->get_region_fn(nullptr, 0, 0, nullptr); \ |
| 981 | jni_abort_catcher.Check("java_array == null"); \ |
| 982 | env_->set_region_fn(nullptr, 0, 0, nullptr); \ |
| 983 | jni_abort_catcher.Check("java_array == null"); \ |
| 984 | env_->get_elements_fn(nullptr, nullptr); \ |
| 985 | jni_abort_catcher.Check("java_array == null"); \ |
| 986 | env_->release_elements_fn(nullptr, nullptr, 0); \ |
| 987 | jni_abort_catcher.Check("java_array == null"); \ |
| 988 | /* Pass the elements for region as null. */ \ |
| 989 | scalar_type ## Array a = env_->new_fn(size); \ |
| 990 | env_->get_region_fn(a, 0, size, nullptr); \ |
| 991 | jni_abort_catcher.Check("buf == null"); \ |
| 992 | env_->set_region_fn(a, 0, size, nullptr); \ |
| 993 | jni_abort_catcher.Check("buf == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 994 | down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(true); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 995 | } \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 996 | /* Allocate an array and check it has the right type and length. */ \ |
| 997 | scalar_type ## Array a = env_->new_fn(size); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 998 | EXPECT_NE(a, nullptr); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 999 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 1000 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1001 | \ |
| 1002 | /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1003 | /* AIOOBE for negative start offset. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1004 | env_->get_region_fn(a, -1, 1, nullptr); \ |
| 1005 | ExpectException(aioobe_); \ |
| 1006 | env_->set_region_fn(a, -1, 1, nullptr); \ |
| 1007 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1008 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1009 | /* AIOOBE for negative length. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1010 | env_->get_region_fn(a, 0, -1, nullptr); \ |
| 1011 | ExpectException(aioobe_); \ |
| 1012 | env_->set_region_fn(a, 0, -1, nullptr); \ |
| 1013 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1014 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1015 | /* AIOOBE for buffer overrun. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1016 | env_->get_region_fn(a, size - 1, size, nullptr); \ |
| 1017 | ExpectException(aioobe_); \ |
| 1018 | env_->set_region_fn(a, size - 1, size, nullptr); \ |
| 1019 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1020 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1021 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 1022 | env_->get_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1023 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1024 | env_->get_region_fn(a, 123, 0, nullptr); \ |
| 1025 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1026 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1027 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 1028 | env_->set_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1029 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1030 | env_->set_region_fn(a, 123, 0, nullptr); \ |
| 1031 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1032 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1033 | /* Prepare a couple of buffers. */ \ |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1034 | std::unique_ptr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 1035 | std::unique_ptr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1036 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 1037 | for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1038 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1039 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1040 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1041 | /* Copy back only part. */ \ |
| 1042 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1043 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1044 | << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1045 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1046 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1047 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1048 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1049 | << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1050 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1051 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1052 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1053 | << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1054 | /* GetPrimitiveArrayCritical */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1055 | void* v = env_->GetPrimitiveArrayCritical(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1056 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \ |
| 1057 | << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1058 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 1059 | /* GetXArrayElements */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1060 | scalar_type* xs = env_->get_elements_fn(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1061 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \ |
| 1062 | << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1063 | env_->release_elements_fn(a, xs, 0); \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1064 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1065 | TEST_F(JniInternalTest, BooleanArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1066 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, |
| 1067 | GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1068 | } |
| 1069 | TEST_F(JniInternalTest, ByteArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1070 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, |
| 1071 | GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1072 | } |
| 1073 | TEST_F(JniInternalTest, CharArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1074 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, |
| 1075 | GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1076 | } |
| 1077 | TEST_F(JniInternalTest, DoubleArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1078 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, |
| 1079 | GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1080 | } |
| 1081 | TEST_F(JniInternalTest, FloatArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1082 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, |
| 1083 | GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1084 | } |
| 1085 | TEST_F(JniInternalTest, IntArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1086 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, |
| 1087 | GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1088 | } |
| 1089 | TEST_F(JniInternalTest, LongArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1090 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, |
| 1091 | GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1092 | } |
| 1093 | TEST_F(JniInternalTest, ShortArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1094 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, |
| 1095 | GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1098 | TEST_F(JniInternalTest, GetPrimitiveArrayElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1099 | GetPrimitiveArrayElementsOfWrongType(false); |
| 1100 | GetPrimitiveArrayElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | TEST_F(JniInternalTest, ReleasePrimitiveArrayElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1104 | ReleasePrimitiveArrayElementsOfWrongType(false); |
| 1105 | ReleasePrimitiveArrayElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1106 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1107 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1108 | TEST_F(JniInternalTest, GetReleasePrimitiveArrayCriticalOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1109 | GetReleasePrimitiveArrayCriticalOfWrongType(false); |
| 1110 | GetReleasePrimitiveArrayCriticalOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | TEST_F(JniInternalTest, GetPrimitiveArrayRegionElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1114 | GetPrimitiveArrayRegionElementsOfWrongType(false); |
| 1115 | GetPrimitiveArrayRegionElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | TEST_F(JniInternalTest, SetPrimitiveArrayRegionElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1119 | SetPrimitiveArrayRegionElementsOfWrongType(false); |
| 1120 | SetPrimitiveArrayRegionElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1123 | TEST_F(JniInternalTest, NewObjectArray) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1124 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1125 | ASSERT_NE(element_class, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1126 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1127 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1128 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1129 | jobjectArray a = env_->NewObjectArray(0, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1130 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1131 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1132 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 1133 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1134 | a = env_->NewObjectArray(1, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1135 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1136 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1137 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1138 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1139 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1140 | // Negative array length checks. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1141 | NewObjectArrayBadArguments(false); |
| 1142 | NewObjectArrayBadArguments(true); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) { |
| 1146 | const char* primitive_descriptors = "VZBSCIJFD"; |
| 1147 | const char* primitive_names[] = { |
| 1148 | "void", "boolean", "byte", "short", "char", "int", "long", "float", "double" |
| 1149 | }; |
| 1150 | ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names)); |
| 1151 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1152 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1153 | CheckJniAbortCatcher jni_abort_catcher; |
| 1154 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1155 | env_->NewObjectArray(0, nullptr, nullptr); |
| 1156 | jni_abort_catcher.Check("element_jclass == null"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1157 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 1158 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 1159 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 1160 | jni_abort_catcher.Check(error_msg.c_str()); |
| 1161 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1162 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1163 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
| 1164 | env_->NewObjectArray(0, nullptr, nullptr); |
| 1165 | jni_abort_catcher.Check("NewObjectArray received NULL jclass"); |
| 1166 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 1167 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 1168 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 1169 | jni_abort_catcher.Check(error_msg.c_str()); |
| 1170 | } |
| 1171 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) { |
| 1175 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1176 | ASSERT_NE(element_class, nullptr); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1177 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1178 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1179 | |
| 1180 | jstring s = env_->NewStringUTF("poop"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1181 | jobjectArray a = env_->NewObjectArray(2, element_class, s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1182 | EXPECT_NE(a, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1183 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1184 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 1185 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 1186 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1187 | |
| 1188 | // Attempt to incorrect create an array of strings with initial value of string arrays. |
| 1189 | CheckJniAbortCatcher jni_abort_catcher; |
| 1190 | env_->NewObjectArray(2, element_class, a); |
| 1191 | jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element " |
| 1192 | "type of 'java.lang.String'"); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | TEST_F(JniInternalTest, GetArrayLength) { |
Ian Rogers | 2f022bd | 2014-11-11 08:43:05 -0800 | [diff] [blame] | 1196 | // Already tested in NewObjectArray/NewPrimitiveArray except for NULL. |
| 1197 | CheckJniAbortCatcher jni_abort_catcher; |
| 1198 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1199 | EXPECT_EQ(0, env_->GetArrayLength(nullptr)); |
| 1200 | jni_abort_catcher.Check("java_array == null"); |
| 1201 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1202 | EXPECT_EQ(JNI_ERR, env_->GetArrayLength(nullptr)); |
| 1203 | jni_abort_catcher.Check("jarray was NULL"); |
| 1204 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1207 | TEST_F(JniInternalTest, GetObjectClass) { |
| 1208 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1209 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1210 | jclass class_class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1211 | ASSERT_NE(class_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1212 | |
| 1213 | jstring s = env_->NewStringUTF("poop"); |
| 1214 | jclass c = env_->GetObjectClass(s); |
| 1215 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 1216 | |
| 1217 | jclass c2 = env_->GetObjectClass(c); |
| 1218 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1219 | |
| 1220 | // Null as object should fail. |
| 1221 | CheckJniAbortCatcher jni_abort_catcher; |
| 1222 | EXPECT_EQ(env_->GetObjectClass(nullptr), nullptr); |
| 1223 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1226 | TEST_F(JniInternalTest, GetSuperclass) { |
| 1227 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1228 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1229 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1230 | ASSERT_NE(string_class, nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 1231 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1232 | ASSERT_NE(runnable_interface, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1233 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1234 | ASSERT_EQ(env_->GetSuperclass(object_class), nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 1235 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1236 | |
| 1237 | // Null as class should fail. |
| 1238 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1239 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1240 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 1241 | jni_abort_catcher.Check("java_class == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1242 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1243 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 1244 | jni_abort_catcher.Check("GetSuperclass received NULL jclass"); |
| 1245 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1248 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 1249 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1250 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1251 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1252 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1253 | |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 1254 | // A superclass is assignable from an instance of its |
| 1255 | // subclass but not vice versa. |
| 1256 | ASSERT_TRUE(env_->IsAssignableFrom(string_class, object_class)); |
| 1257 | ASSERT_FALSE(env_->IsAssignableFrom(object_class, string_class)); |
| 1258 | |
| 1259 | jclass charsequence_interface = env_->FindClass("java/lang/CharSequence"); |
| 1260 | ASSERT_NE(charsequence_interface, nullptr); |
| 1261 | |
| 1262 | // An interface is assignable from an instance of an implementing |
| 1263 | // class but not vice versa. |
| 1264 | ASSERT_TRUE(env_->IsAssignableFrom(string_class, charsequence_interface)); |
| 1265 | ASSERT_FALSE(env_->IsAssignableFrom(charsequence_interface, string_class)); |
| 1266 | |
| 1267 | // Check that arrays are covariant. |
| 1268 | jclass string_array_class = env_->FindClass("[Ljava/lang/String;"); |
| 1269 | ASSERT_NE(string_array_class, nullptr); |
| 1270 | jclass object_array_class = env_->FindClass("[Ljava/lang/Object;"); |
| 1271 | ASSERT_NE(object_array_class, nullptr); |
| 1272 | ASSERT_TRUE(env_->IsAssignableFrom(string_array_class, object_array_class)); |
| 1273 | ASSERT_FALSE(env_->IsAssignableFrom(object_array_class, string_array_class)); |
| 1274 | |
| 1275 | // Primitive types are tested in 004-JniTest. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1276 | |
| 1277 | // Null as either class should fail. |
| 1278 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1279 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1280 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 1281 | jni_abort_catcher.Check("java_class1 == null"); |
| 1282 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 1283 | jni_abort_catcher.Check("java_class2 == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1284 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1285 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 1286 | jni_abort_catcher.Check("IsAssignableFrom received NULL jclass"); |
| 1287 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 1288 | jni_abort_catcher.Check("IsAssignableFrom received NULL jclass"); |
| 1289 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1292 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 1293 | jclass local = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1294 | ASSERT_TRUE(local != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1295 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 1296 | |
| 1297 | jobject global = env_->NewGlobalRef(local); |
| 1298 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 1299 | |
| 1300 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 1301 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 1302 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1303 | CheckJniAbortCatcher jni_abort_catcher; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1304 | jobject invalid = reinterpret_cast<jobject>(this); |
| 1305 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1306 | jni_abort_catcher.Check("use of invalid jobject"); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1307 | |
| 1308 | // TODO: invoke a native method and test that its arguments are considered local references. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1309 | |
| 1310 | // Null as object should fail. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1311 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(nullptr)); |
| 1312 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1315 | TEST_F(JniInternalTest, StaleWeakGlobal) { |
| 1316 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1317 | ASSERT_NE(java_lang_Class, nullptr); |
| 1318 | jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1319 | ASSERT_NE(local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1320 | jweak weak_global = env_->NewWeakGlobalRef(local_ref); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1321 | ASSERT_NE(weak_global, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1322 | env_->DeleteLocalRef(local_ref); |
| 1323 | Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global. |
| 1324 | jobject new_global_ref = env_->NewGlobalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1325 | EXPECT_EQ(new_global_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1326 | jobject new_local_ref = env_->NewLocalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1327 | EXPECT_EQ(new_local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1330 | TEST_F(JniInternalTest, NewStringUTF) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1331 | EXPECT_EQ(env_->NewStringUTF(nullptr), nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1332 | jstring s; |
| 1333 | |
| 1334 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1335 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1336 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1337 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1338 | s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1339 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1340 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 1341 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 1342 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1343 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1346 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1347 | jchar chars[] = { 'h', 'i' }; |
| 1348 | jstring s; |
| 1349 | s = env_->NewString(chars, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1350 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1351 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1352 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1353 | s = env_->NewString(chars, 2); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1354 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1355 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 1356 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 1357 | |
| 1358 | // TODO: check some non-ASCII strings. |
| 1359 | } |
| 1360 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1361 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1362 | jstring s = env_->NewString(nullptr, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1363 | EXPECT_NE(s, nullptr); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1364 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1365 | } |
| 1366 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1367 | TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) { |
| 1368 | CheckJniAbortCatcher jni_abort_catcher; |
| 1369 | env_->NewString(nullptr, 1); |
| 1370 | jni_abort_catcher.Check("chars == null && char_count > 0"); |
| 1371 | } |
| 1372 | |
| 1373 | TEST_F(JniInternalTest, NewStringNegativeLength) { |
| 1374 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1375 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1376 | env_->NewString(nullptr, -1); |
| 1377 | jni_abort_catcher.Check("char_count < 0: -1"); |
| 1378 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1379 | jni_abort_catcher.Check("char_count < 0: -2147483648"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1380 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1381 | env_->NewString(nullptr, -1); |
| 1382 | jni_abort_catcher.Check("negative jsize: -1"); |
| 1383 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1384 | jni_abort_catcher.Check("negative jsize: -2147483648"); |
| 1385 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1386 | } |
| 1387 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1388 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 1389 | // Already tested in the NewString/NewStringUTF tests. |
| 1390 | } |
| 1391 | |
| 1392 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 1393 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1394 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1395 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1396 | env_->GetStringRegion(s, -1, 0, nullptr); |
| 1397 | ExpectException(sioobe_); |
| 1398 | env_->GetStringRegion(s, 0, -1, nullptr); |
| 1399 | ExpectException(sioobe_); |
| 1400 | env_->GetStringRegion(s, 0, 10, nullptr); |
| 1401 | ExpectException(sioobe_); |
| 1402 | env_->GetStringRegion(s, 10, 1, nullptr); |
| 1403 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1404 | |
| 1405 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 1406 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 1407 | EXPECT_EQ('x', chars[0]); |
| 1408 | EXPECT_EQ('e', chars[1]); |
| 1409 | EXPECT_EQ('l', chars[2]); |
| 1410 | EXPECT_EQ('x', chars[3]); |
| 1411 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1412 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1413 | env_->GetStringRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1414 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1415 | env_->GetStringRegion(s, 123, 0, nullptr); |
| 1416 | ExpectException(sioobe_); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1417 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1418 | env_->GetStringUTFRegion(s, -1, 0, nullptr); |
| 1419 | ExpectException(sioobe_); |
| 1420 | env_->GetStringUTFRegion(s, 0, -1, nullptr); |
| 1421 | ExpectException(sioobe_); |
| 1422 | env_->GetStringUTFRegion(s, 0, 10, nullptr); |
| 1423 | ExpectException(sioobe_); |
| 1424 | env_->GetStringUTFRegion(s, 10, 1, nullptr); |
| 1425 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1426 | |
| 1427 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 1428 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 1429 | EXPECT_EQ('x', bytes[0]); |
| 1430 | EXPECT_EQ('e', bytes[1]); |
| 1431 | EXPECT_EQ('l', bytes[2]); |
| 1432 | EXPECT_EQ('x', bytes[3]); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1433 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1434 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1435 | env_->GetStringUTFRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1436 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1437 | env_->GetStringUTFRegion(s, 123, 0, nullptr); |
| 1438 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1441 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1442 | // Passing in a nullptr jstring is ignored normally, but caught by -Xcheck:jni. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1443 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1444 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1445 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1446 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1447 | } |
| 1448 | { |
| 1449 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1450 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1451 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
| 1452 | check_jni_abort_catcher.Check("GetStringUTFChars received NULL jstring"); |
| 1453 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1454 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1455 | |
| 1456 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1457 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1458 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1459 | const char* utf = env_->GetStringUTFChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1460 | EXPECT_STREQ("hello", utf); |
| 1461 | env_->ReleaseStringUTFChars(s, utf); |
| 1462 | |
| 1463 | jboolean is_copy = JNI_FALSE; |
| 1464 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 1465 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1466 | EXPECT_STREQ("hello", utf); |
| 1467 | env_->ReleaseStringUTFChars(s, utf); |
| 1468 | } |
| 1469 | |
| 1470 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 1471 | jstring s = env_->NewStringUTF("hello"); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1472 | ScopedObjectAccess soa(env_); |
| 1473 | mirror::String* s_m = soa.Decode<mirror::String*>(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1474 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1475 | |
| 1476 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1477 | const jchar* chars = env_->GetStringChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1478 | EXPECT_EQ(expected[0], chars[0]); |
| 1479 | EXPECT_EQ(expected[1], chars[1]); |
| 1480 | EXPECT_EQ(expected[2], chars[2]); |
| 1481 | EXPECT_EQ(expected[3], chars[3]); |
| 1482 | EXPECT_EQ(expected[4], chars[4]); |
| 1483 | env_->ReleaseStringChars(s, chars); |
| 1484 | |
| 1485 | jboolean is_copy = JNI_FALSE; |
| 1486 | chars = env_->GetStringChars(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1487 | if (Runtime::Current()->GetHeap()->IsMovableObject(s_m->GetCharArray())) { |
| 1488 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1489 | } else { |
| 1490 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1491 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1492 | EXPECT_EQ(expected[0], chars[0]); |
| 1493 | EXPECT_EQ(expected[1], chars[1]); |
| 1494 | EXPECT_EQ(expected[2], chars[2]); |
| 1495 | EXPECT_EQ(expected[3], chars[3]); |
| 1496 | EXPECT_EQ(expected[4], chars[4]); |
| 1497 | env_->ReleaseStringChars(s, chars); |
| 1498 | } |
| 1499 | |
| 1500 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 1501 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1502 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1503 | |
| 1504 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1505 | const jchar* chars = env_->GetStringCritical(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1506 | EXPECT_EQ(expected[0], chars[0]); |
| 1507 | EXPECT_EQ(expected[1], chars[1]); |
| 1508 | EXPECT_EQ(expected[2], chars[2]); |
| 1509 | EXPECT_EQ(expected[3], chars[3]); |
| 1510 | EXPECT_EQ(expected[4], chars[4]); |
| 1511 | env_->ReleaseStringCritical(s, chars); |
| 1512 | |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1513 | jboolean is_copy = JNI_TRUE; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1514 | chars = env_->GetStringCritical(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1515 | EXPECT_EQ(JNI_FALSE, is_copy); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1516 | EXPECT_EQ(expected[0], chars[0]); |
| 1517 | EXPECT_EQ(expected[1], chars[1]); |
| 1518 | EXPECT_EQ(expected[2], chars[2]); |
| 1519 | EXPECT_EQ(expected[3], chars[3]); |
| 1520 | EXPECT_EQ(expected[4], chars[4]); |
| 1521 | env_->ReleaseStringCritical(s, chars); |
| 1522 | } |
| 1523 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1524 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1525 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1526 | ASSERT_TRUE(java_lang_Class != nullptr); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1527 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1528 | jobjectArray array = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1529 | EXPECT_NE(array, nullptr); |
| 1530 | EXPECT_EQ(env_->GetObjectArrayElement(array, 0), nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1531 | env_->SetObjectArrayElement(array, 0, java_lang_Class); |
| 1532 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1533 | |
| 1534 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1535 | env_->SetObjectArrayElement(array, -1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1536 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1537 | |
| 1538 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1539 | env_->SetObjectArrayElement(array, 1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1540 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1541 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1542 | // ArrayStoreException thrown for bad types. |
| 1543 | env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1544 | ExpectException(ase_); |
| 1545 | |
| 1546 | // Null as array should fail. |
| 1547 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1548 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1549 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1550 | jni_abort_catcher.Check("java_array == null"); |
| 1551 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1552 | jni_abort_catcher.Check("java_array == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1553 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1554 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1555 | jni_abort_catcher.Check("jarray was NULL"); |
| 1556 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1557 | jni_abort_catcher.Check("jarray was NULL"); |
| 1558 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1561 | #define EXPECT_STATIC_PRIMITIVE_FIELD(expect_eq, type, field_name, sig, value1, value2) \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1562 | do { \ |
| 1563 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1564 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1565 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1566 | expect_eq(value1, env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1567 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1568 | expect_eq(value2, env_->GetStatic ## type ## Field(c, fid)); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1569 | \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1570 | bool old_check_jni = vm_->SetCheckJniEnabled(false); \ |
| 1571 | { \ |
| 1572 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1573 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1574 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1575 | } \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1576 | CheckJniAbortCatcher jni_abort_catcher; \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1577 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1578 | jni_abort_catcher.Check("fid == null"); \ |
| 1579 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1580 | jni_abort_catcher.Check("fid == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1581 | \ |
| 1582 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \ |
| 1583 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1584 | jni_abort_catcher.Check("received NULL jclass"); \ |
| 1585 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1586 | jni_abort_catcher.Check("received NULL jclass"); \ |
| 1587 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1588 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1589 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1590 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1591 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1592 | } while (false) |
| 1593 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1594 | #define EXPECT_PRIMITIVE_FIELD(expect_eq, instance, type, field_name, sig, value1, value2) \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1595 | do { \ |
| 1596 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1597 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1598 | env_->Set ## type ## Field(instance, fid, value1); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1599 | expect_eq(value1, env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1600 | env_->Set ## type ## Field(instance, fid, value2); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1601 | expect_eq(value2, env_->Get ## type ## Field(instance, fid)); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1602 | \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1603 | bool old_check_jni = vm_->SetCheckJniEnabled(false); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1604 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1605 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1606 | jni_abort_catcher.Check("obj == null"); \ |
| 1607 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1608 | jni_abort_catcher.Check("obj == null"); \ |
| 1609 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1610 | jni_abort_catcher.Check("fid == null"); \ |
| 1611 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1612 | jni_abort_catcher.Check("fid == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1613 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \ |
| 1614 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1615 | jni_abort_catcher.Check("field operation on NULL object:"); \ |
| 1616 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1617 | jni_abort_catcher.Check("field operation on NULL object:"); \ |
| 1618 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1619 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1620 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1621 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1622 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1623 | } while (false) |
| 1624 | |
| 1625 | |
| 1626 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1627 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1628 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1629 | LoadDex("AllFields"); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 1630 | bool started = runtime_->Start(); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1631 | ASSERT_TRUE(started); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1632 | |
| 1633 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1634 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1635 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1636 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1637 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1638 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Boolean, "sZ", "Z", JNI_TRUE, JNI_FALSE); |
| 1639 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Byte, "sB", "B", 1, 2); |
| 1640 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Char, "sC", "C", 'a', 'b'); |
| 1641 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_DOUBLE_EQ, Double, "sD", "D", 1.0, 2.0); |
| 1642 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_FLOAT_EQ, Float, "sF", "F", 1.0, 2.0); |
| 1643 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Int, "sI", "I", 1, 2); |
| 1644 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Long, "sJ", "J", 1, 2); |
| 1645 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Short, "sS", "S", 1, 2); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1646 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1647 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Boolean, "iZ", "Z", JNI_TRUE, JNI_FALSE); |
| 1648 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Byte, "iB", "B", 1, 2); |
| 1649 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Char, "iC", "C", 'a', 'b'); |
| 1650 | EXPECT_PRIMITIVE_FIELD(EXPECT_DOUBLE_EQ, o, Double, "iD", "D", 1.0, 2.0); |
| 1651 | EXPECT_PRIMITIVE_FIELD(EXPECT_FLOAT_EQ, o, Float, "iF", "F", 1.0, 2.0); |
| 1652 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Int, "iI", "I", 1, 2); |
| 1653 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Long, "iJ", "J", 1, 2); |
| 1654 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Short, "iS", "S", 1, 2); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1658 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1659 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1660 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1661 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1662 | |
| 1663 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1664 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1665 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1666 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1667 | |
| 1668 | jstring s1 = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1669 | ASSERT_NE(s1, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1670 | jstring s2 = env_->NewStringUTF("world"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1671 | ASSERT_NE(s2, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1672 | |
| 1673 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1674 | ASSERT_NE(s_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1675 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1676 | ASSERT_NE(i_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1677 | |
| 1678 | env_->SetStaticObjectField(c, s_fid, s1); |
| 1679 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 1680 | env_->SetStaticObjectField(c, s_fid, s2); |
| 1681 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 1682 | |
| 1683 | env_->SetObjectField(o, i_fid, s1); |
| 1684 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 1685 | env_->SetObjectField(o, i_fid, s2); |
| 1686 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 1687 | } |
| 1688 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1689 | TEST_F(JniInternalTest, NewLocalRef_nullptr) { |
| 1690 | EXPECT_EQ(env_->NewLocalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | TEST_F(JniInternalTest, NewLocalRef) { |
| 1694 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1695 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1696 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1697 | EXPECT_NE(o, nullptr); |
| 1698 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1699 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1700 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1703 | TEST_F(JniInternalTest, DeleteLocalRef_nullptr) { |
| 1704 | env_->DeleteLocalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 1708 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1709 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1710 | env_->DeleteLocalRef(s); |
| 1711 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1712 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1713 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1714 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1715 | { |
| 1716 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1717 | env_->DeleteLocalRef(s); |
| 1718 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1719 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1720 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1721 | env_->DeleteLocalRef(s); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1722 | std::string expected(StringPrintf("use of deleted local reference %p", s)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1723 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1724 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1725 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1726 | |
| 1727 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1728 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1729 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1730 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1731 | |
| 1732 | env_->DeleteLocalRef(s); |
| 1733 | env_->DeleteLocalRef(o); |
| 1734 | } |
| 1735 | |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1736 | TEST_F(JniInternalTest, PushLocalFrame_10395422) { |
| 1737 | // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a |
| 1738 | // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how |
| 1739 | // Android historically treated it, and it's how the RI treats it. It's also the more useful |
| 1740 | // interpretation! |
| 1741 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1742 | env_->PopLocalFrame(nullptr); |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1743 | |
| 1744 | // Negative capacities are not allowed. |
| 1745 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1)); |
| 1746 | |
| 1747 | // And it's okay to have an upper limit. Ours is currently 512. |
| 1748 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192)); |
| 1749 | } |
| 1750 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1751 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 1752 | jobject original = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1753 | ASSERT_NE(original, nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1754 | |
| 1755 | jobject outer; |
| 1756 | jobject inner1, inner2; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1757 | ScopedObjectAccess soa(env_); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1758 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1759 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1760 | outer = env_->NewLocalRef(original); |
| 1761 | |
| 1762 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1763 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1764 | inner1 = env_->NewLocalRef(outer); |
| 1765 | inner2 = env_->NewStringUTF("survivor"); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1766 | EXPECT_NE(env_->PopLocalFrame(inner2), nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1770 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1771 | { |
| 1772 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1773 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1774 | check_jni_abort_catcher.Check("use of deleted local reference"); |
| 1775 | } |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1776 | |
| 1777 | // Our local reference for the survivor is invalid because the survivor |
| 1778 | // gets a new local reference... |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1779 | { |
| 1780 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1781 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1782 | check_jni_abort_catcher.Check("use of deleted local reference"); |
| 1783 | } |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1784 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1785 | EXPECT_EQ(env_->PopLocalFrame(nullptr), nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1786 | } |
| 1787 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1788 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1789 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1790 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1791 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1792 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1793 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1794 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1797 | TEST_F(JniInternalTest, NewGlobalRef_nullptr) { |
| 1798 | EXPECT_EQ(env_->NewGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1802 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1803 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1804 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1805 | EXPECT_NE(o, nullptr); |
| 1806 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1807 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1808 | EXPECT_EQ(env_->GetObjectRefType(o), JNIGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1811 | TEST_F(JniInternalTest, DeleteGlobalRef_nullptr) { |
| 1812 | env_->DeleteGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 1816 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1817 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1818 | |
| 1819 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1820 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1821 | env_->DeleteGlobalRef(o); |
| 1822 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1823 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1824 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1825 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1826 | { |
| 1827 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1828 | env_->DeleteGlobalRef(o); |
| 1829 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1830 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1831 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1832 | env_->DeleteGlobalRef(o); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1833 | std::string expected(StringPrintf("use of deleted global reference %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1834 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1835 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1836 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1837 | |
| 1838 | jobject o1 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1839 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1840 | jobject o2 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1841 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1842 | |
| 1843 | env_->DeleteGlobalRef(o1); |
| 1844 | env_->DeleteGlobalRef(o2); |
| 1845 | } |
| 1846 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1847 | TEST_F(JniInternalTest, NewWeakGlobalRef_nullptr) { |
| 1848 | EXPECT_EQ(env_->NewWeakGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1852 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1853 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1854 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1855 | EXPECT_NE(o, nullptr); |
| 1856 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1857 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1858 | EXPECT_EQ(env_->GetObjectRefType(o), JNIWeakGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1861 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_nullptr) { |
| 1862 | env_->DeleteWeakGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 1866 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1867 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1868 | |
| 1869 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1870 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1871 | env_->DeleteWeakGlobalRef(o); |
| 1872 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1873 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1874 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1875 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1876 | { |
| 1877 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1878 | env_->DeleteWeakGlobalRef(o); |
| 1879 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1880 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1881 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1882 | env_->DeleteWeakGlobalRef(o); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1883 | std::string expected(StringPrintf("use of deleted weak global reference %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1884 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1885 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1886 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1887 | |
| 1888 | jobject o1 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1889 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1890 | jobject o2 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1891 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1892 | |
| 1893 | env_->DeleteWeakGlobalRef(o1); |
| 1894 | env_->DeleteWeakGlobalRef(o2); |
| 1895 | } |
| 1896 | |
Alexei Zavjalov | 3a1444c | 2014-06-25 16:04:55 +0700 | [diff] [blame] | 1897 | TEST_F(JniInternalTest, ExceptionDescribe) { |
| 1898 | // This checks how ExceptionDescribe handles call without exception. |
| 1899 | env_->ExceptionClear(); |
| 1900 | env_->ExceptionDescribe(); |
| 1901 | } |
| 1902 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1903 | TEST_F(JniInternalTest, Throw) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1904 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1905 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1906 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1907 | ASSERT_TRUE(exception != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1908 | |
| 1909 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1910 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1911 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1912 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1913 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1914 | |
| 1915 | // Bad argument. |
| 1916 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1917 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
| 1918 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1919 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1920 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
| 1921 | check_jni_abort_catcher.Check("Throw received NULL jthrowable"); |
| 1922 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | TEST_F(JniInternalTest, ThrowNew) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1926 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1927 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1928 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1929 | jthrowable thrown_exception; |
| 1930 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1931 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1932 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1933 | thrown_exception = env_->ExceptionOccurred(); |
| 1934 | env_->ExceptionClear(); |
| 1935 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1936 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1937 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, nullptr)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1938 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1939 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1940 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1941 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1942 | |
| 1943 | // Bad argument. |
| 1944 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1945 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1946 | EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr)); |
| 1947 | check_jni_abort_catcher.Check("c == null"); |
| 1948 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1949 | EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr)); |
| 1950 | check_jni_abort_catcher.Check("ThrowNew received NULL jclass"); |
| 1951 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1954 | TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1955 | // Start runtime. |
| 1956 | Thread* self = Thread::Current(); |
| 1957 | self->TransitionFromSuspendedToRunnable(); |
| 1958 | MakeExecutable(nullptr, "java.lang.Class"); |
| 1959 | MakeExecutable(nullptr, "java.lang.Object"); |
| 1960 | MakeExecutable(nullptr, "java.nio.DirectByteBuffer"); |
| 1961 | MakeExecutable(nullptr, "java.nio.MemoryBlock"); |
| 1962 | MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock"); |
| 1963 | MakeExecutable(nullptr, "java.nio.MappedByteBuffer"); |
| 1964 | MakeExecutable(nullptr, "java.nio.ByteBuffer"); |
| 1965 | MakeExecutable(nullptr, "java.nio.Buffer"); |
| 1966 | // TODO: we only load a dex file here as starting the runtime relies upon it. |
| 1967 | const char* class_name = "StaticLeafMethods"; |
| 1968 | LoadDex(class_name); |
| 1969 | bool started = runtime_->Start(); |
| 1970 | ASSERT_TRUE(started); |
| 1971 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1972 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1973 | ASSERT_NE(buffer_class, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1974 | |
| 1975 | char bytes[1024]; |
| 1976 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1977 | ASSERT_NE(buffer, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1978 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1979 | ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes); |
Ian Rogers | 1ee9935 | 2014-05-14 14:38:16 -0700 | [diff] [blame] | 1980 | ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes))); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 1981 | |
| 1982 | { |
| 1983 | CheckJniAbortCatcher check_jni_abort_catcher; |
Brian Carlstrom | 85a9336 | 2014-06-25 09:30:52 -0700 | [diff] [blame] | 1984 | env_->NewDirectByteBuffer(bytes, static_cast<jlong>(INT_MAX) + 1); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 1985 | check_jni_abort_catcher.Check("in call to NewDirectByteBuffer"); |
| 1986 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1989 | TEST_F(JniInternalTest, MonitorEnterExit) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1990 | // Create an object to torture. |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1991 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1992 | ASSERT_NE(object_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1993 | jobject object = env_->AllocObject(object_class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1994 | ASSERT_NE(object, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1995 | |
| 1996 | // Expected class of exceptions |
| 1997 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1998 | ASSERT_NE(imse_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1999 | |
| 2000 | jthrowable thrown_exception; |
| 2001 | |
| 2002 | // Unlock of unowned monitor |
| 2003 | env_->MonitorExit(object); |
| 2004 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 2005 | thrown_exception = env_->ExceptionOccurred(); |
| 2006 | env_->ExceptionClear(); |
| 2007 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 2008 | |
| 2009 | // Lock of unowned monitor |
| 2010 | env_->MonitorEnter(object); |
| 2011 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2012 | // Regular unlock |
| 2013 | env_->MonitorExit(object); |
| 2014 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2015 | |
| 2016 | // Recursively lock a lot |
| 2017 | size_t max_recursive_lock = 1024; |
| 2018 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 2019 | env_->MonitorEnter(object); |
| 2020 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2021 | } |
| 2022 | // Recursively unlock a lot |
| 2023 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 2024 | env_->MonitorExit(object); |
| 2025 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2026 | } |
| 2027 | |
| 2028 | // Unlock of unowned monitor |
| 2029 | env_->MonitorExit(object); |
| 2030 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 2031 | thrown_exception = env_->ExceptionOccurred(); |
| 2032 | env_->ExceptionClear(); |
| 2033 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 2034 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2035 | // It's an error to call MonitorEnter or MonitorExit on nullptr. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2036 | { |
| 2037 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2038 | env_->MonitorEnter(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2039 | check_jni_abort_catcher.Check("in call to MonitorEnter"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2040 | env_->MonitorExit(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2041 | check_jni_abort_catcher.Check("in call to MonitorExit"); |
| 2042 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2045 | } // namespace art |