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