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 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 21 | #include <cmath> |
| 22 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 23 | #include "common_test.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 28 | class JniInternalTest : public CommonTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 29 | protected: |
| 30 | virtual void SetUp() { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 31 | CommonTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 33 | vm_ = Runtime::Current()->GetJavaVM(); |
| 34 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 35 | // Turn on -verbose:jni for the JNI tests. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 36 | gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 38 | env_ = Thread::Current()->GetJniEnv(); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 40 | ScopedLocalRef<jclass> aioobe(env_, env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
| 41 | CHECK(aioobe.get() != NULL); |
| 42 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 44 | ScopedLocalRef<jclass> sioobe(env_, env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
| 45 | CHECK(sioobe.get() != NULL); |
| 46 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 47 | } |
| 48 | |
| 49 | virtual void TearDown() { |
| 50 | env_->DeleteGlobalRef(aioobe_); |
| 51 | env_->DeleteGlobalRef(sioobe_); |
| 52 | CommonTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 53 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 55 | Method::InvokeStub* DoCompile(Method*& method, Object*& receiver, bool is_static, const char* method_name, const char* method_signature) { |
| 56 | const char* class_name = is_static ? "StaticLeafMethods" : "NonStaticLeafMethods"; |
| 57 | SirtRef<ClassLoader> class_loader(LoadDex(class_name)); |
| 58 | if (is_static) { |
| 59 | CompileDirectMethod(class_loader.get(), class_name, method_name, method_signature); |
| 60 | } else { |
| 61 | CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z"); |
| 62 | CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V"); |
| 63 | CompileVirtualMethod(class_loader.get(), class_name, method_name, method_signature); |
| 64 | } |
| 65 | |
| 66 | Class* c = class_linker_->FindClass(DotToDescriptor(class_name).c_str(), class_loader.get()); |
| 67 | CHECK(c != NULL); |
| 68 | |
| 69 | method = is_static ? c->FindDirectMethod(method_name, method_signature) : c->FindVirtualMethod(method_name, method_signature); |
| 70 | CHECK(method != NULL); |
| 71 | |
| 72 | receiver = (is_static ? NULL : c->AllocObject()); |
| 73 | |
| 74 | Method::InvokeStub* stub = method->GetInvokeStub(); |
| 75 | CHECK(stub != NULL); |
| 76 | |
| 77 | return stub; |
| 78 | } |
| 79 | |
| 80 | void InvokeNopMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 81 | Method* method; |
| 82 | Object* receiver; |
| 83 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "nop", "()V"); |
| 84 | (*stub)(method, receiver, Thread::Current(), NULL, NULL); |
| 85 | } |
| 86 | |
| 87 | void InvokeIdentityByteMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 88 | Method* method; |
| 89 | Object* receiver; |
| 90 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(B)B"); |
| 91 | |
| 92 | JValue args[1]; |
| 93 | JValue result; |
| 94 | |
| 95 | args[0].i = 0; |
| 96 | result.b = -1; |
| 97 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 98 | EXPECT_EQ(0, result.b); |
| 99 | |
| 100 | args[0].i = -1; |
| 101 | result.b = 0; |
| 102 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 103 | EXPECT_EQ(-1, result.b); |
| 104 | |
| 105 | args[0].i = SCHAR_MAX; |
| 106 | result.b = 0; |
| 107 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 108 | EXPECT_EQ(SCHAR_MAX, result.b); |
| 109 | |
| 110 | args[0].i = SCHAR_MIN; |
| 111 | result.b = 0; |
| 112 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 113 | EXPECT_EQ(SCHAR_MIN, result.b); |
| 114 | } |
| 115 | |
| 116 | void InvokeIdentityIntMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 117 | Method* method; |
| 118 | Object* receiver; |
| 119 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(I)I"); |
| 120 | |
| 121 | JValue args[1]; |
| 122 | JValue result; |
| 123 | |
| 124 | args[0].i = 0; |
| 125 | result.i = -1; |
| 126 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 127 | EXPECT_EQ(0, result.i); |
| 128 | |
| 129 | args[0].i = -1; |
| 130 | result.i = 0; |
| 131 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 132 | EXPECT_EQ(-1, result.i); |
| 133 | |
| 134 | args[0].i = INT_MAX; |
| 135 | result.i = 0; |
| 136 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 137 | EXPECT_EQ(INT_MAX, result.i); |
| 138 | |
| 139 | args[0].i = INT_MIN; |
| 140 | result.i = 0; |
| 141 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 142 | EXPECT_EQ(INT_MIN, result.i); |
| 143 | } |
| 144 | |
| 145 | void InvokeIdentityDoubleMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 146 | Method* method; |
| 147 | Object* receiver; |
| 148 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(D)D"); |
| 149 | |
| 150 | JValue args[1]; |
| 151 | JValue result; |
| 152 | |
| 153 | args[0].d = 0.0; |
| 154 | result.d = -1.0; |
| 155 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 156 | EXPECT_EQ(0.0, result.d); |
| 157 | |
| 158 | args[0].d = -1.0; |
| 159 | result.d = 0.0; |
| 160 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 161 | EXPECT_EQ(-1.0, result.d); |
| 162 | |
| 163 | args[0].d = DBL_MAX; |
| 164 | result.d = 0.0; |
| 165 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 166 | EXPECT_EQ(DBL_MAX, result.d); |
| 167 | |
| 168 | args[0].d = DBL_MIN; |
| 169 | result.d = 0.0; |
| 170 | (*stub)(method, receiver, Thread::Current(), args, &result); |
| 171 | EXPECT_EQ(DBL_MIN, result.d); |
| 172 | } |
| 173 | |
| 174 | void InvokeSumIntIntMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 175 | Method* method; |
| 176 | Object* receiver; |
| 177 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(II)I"); |
| 178 | |
| 179 | JValue result; |
| 180 | result.i = -1; |
| 181 | JValue args[2]; |
| 182 | args[0].i = 0; |
| 183 | args[1].i = 0; |
| 184 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 185 | EXPECT_EQ(0, result.i); |
| 186 | |
| 187 | result.i = 0; |
| 188 | args[0].i = 1; |
| 189 | args[1].i = 2; |
| 190 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 191 | EXPECT_EQ(3, result.i); |
| 192 | |
| 193 | result.i = 0; |
| 194 | args[0].i = -2; |
| 195 | args[1].i = 5; |
| 196 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 197 | EXPECT_EQ(3, result.i); |
| 198 | |
| 199 | result.i = 1234; |
| 200 | args[0].i = INT_MAX; |
| 201 | args[1].i = INT_MIN; |
| 202 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 203 | EXPECT_EQ(-1, result.i); |
| 204 | |
| 205 | result.i = INT_MIN; |
| 206 | args[0].i = INT_MAX; |
| 207 | args[1].i = INT_MAX; |
| 208 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 209 | EXPECT_EQ(-2, result.i); |
| 210 | } |
| 211 | |
| 212 | void InvokeSumIntIntIntMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 213 | Method* method; |
| 214 | Object* receiver; |
| 215 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(III)I"); |
| 216 | |
| 217 | JValue result; |
| 218 | result.i = -1; |
| 219 | JValue args[3]; |
| 220 | args[0].i = 0; |
| 221 | args[1].i = 0; |
| 222 | args[2].i = 0; |
| 223 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 224 | EXPECT_EQ(0, result.i); |
| 225 | |
| 226 | result.i = 0; |
| 227 | args[0].i = 1; |
| 228 | args[1].i = 2; |
| 229 | args[2].i = 3; |
| 230 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 231 | EXPECT_EQ(6, result.i); |
| 232 | |
| 233 | result.i = 0; |
| 234 | args[0].i = -1; |
| 235 | args[1].i = 2; |
| 236 | args[2].i = -3; |
| 237 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 238 | EXPECT_EQ(-2, result.i); |
| 239 | |
| 240 | result.i = 1234; |
| 241 | args[0].i = INT_MAX; |
| 242 | args[1].i = INT_MIN; |
| 243 | args[2].i = INT_MAX; |
| 244 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 245 | EXPECT_EQ(2147483646, result.i); |
| 246 | |
| 247 | result.i = INT_MIN; |
| 248 | args[0].i = INT_MAX; |
| 249 | args[1].i = INT_MAX; |
| 250 | args[2].i = INT_MAX; |
| 251 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 252 | EXPECT_EQ(2147483645, result.i); |
| 253 | } |
| 254 | |
| 255 | void InvokeSumIntIntIntIntMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 256 | Method* method; |
| 257 | Object* receiver; |
| 258 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(IIII)I"); |
| 259 | |
| 260 | JValue result; |
| 261 | result.i = -1; |
| 262 | JValue args[4]; |
| 263 | args[0].i = 0; |
| 264 | args[1].i = 0; |
| 265 | args[2].i = 0; |
| 266 | args[3].i = 0; |
| 267 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 268 | EXPECT_EQ(0, result.i); |
| 269 | |
| 270 | result.i = 0; |
| 271 | args[0].i = 1; |
| 272 | args[1].i = 2; |
| 273 | args[2].i = 3; |
| 274 | args[3].i = 4; |
| 275 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 276 | EXPECT_EQ(10, result.i); |
| 277 | |
| 278 | result.i = 0; |
| 279 | args[0].i = -1; |
| 280 | args[1].i = 2; |
| 281 | args[2].i = -3; |
| 282 | args[3].i = 4; |
| 283 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 284 | EXPECT_EQ(2, result.i); |
| 285 | |
| 286 | result.i = 1234; |
| 287 | args[0].i = INT_MAX; |
| 288 | args[1].i = INT_MIN; |
| 289 | args[2].i = INT_MAX; |
| 290 | args[3].i = INT_MIN; |
| 291 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 292 | EXPECT_EQ(-2, result.i); |
| 293 | |
| 294 | result.i = INT_MIN; |
| 295 | args[0].i = INT_MAX; |
| 296 | args[1].i = INT_MAX; |
| 297 | args[2].i = INT_MAX; |
| 298 | args[3].i = INT_MAX; |
| 299 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 300 | EXPECT_EQ(-4, result.i); |
| 301 | } |
| 302 | |
| 303 | void InvokeSumIntIntIntIntIntMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 304 | Method* method; |
| 305 | Object* receiver; |
| 306 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(IIIII)I"); |
| 307 | |
| 308 | JValue result; |
| 309 | result.i = -1.0; |
| 310 | JValue args[5]; |
| 311 | args[0].i = 0; |
| 312 | args[1].i = 0; |
| 313 | args[2].i = 0; |
| 314 | args[3].i = 0; |
| 315 | args[4].i = 0; |
| 316 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 317 | EXPECT_EQ(0, result.i); |
| 318 | |
| 319 | result.i = 0; |
| 320 | args[0].i = 1; |
| 321 | args[1].i = 2; |
| 322 | args[2].i = 3; |
| 323 | args[3].i = 4; |
| 324 | args[4].i = 5; |
| 325 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 326 | EXPECT_EQ(15, result.i); |
| 327 | |
| 328 | result.i = 0; |
| 329 | args[0].i = -1; |
| 330 | args[1].i = 2; |
| 331 | args[2].i = -3; |
| 332 | args[3].i = 4; |
| 333 | args[4].i = -5; |
| 334 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 335 | EXPECT_EQ(-3, result.i); |
| 336 | |
| 337 | result.i = 1234; |
| 338 | args[0].i = INT_MAX; |
| 339 | args[1].i = INT_MIN; |
| 340 | args[2].i = INT_MAX; |
| 341 | args[3].i = INT_MIN; |
| 342 | args[4].i = INT_MAX; |
| 343 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 344 | EXPECT_EQ(2147483645, result.i); |
| 345 | |
| 346 | result.i = INT_MIN; |
| 347 | args[0].i = INT_MAX; |
| 348 | args[1].i = INT_MAX; |
| 349 | args[2].i = INT_MAX; |
| 350 | args[3].i = INT_MAX; |
| 351 | args[4].i = INT_MAX; |
| 352 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 353 | EXPECT_EQ(2147483643, result.i); |
| 354 | } |
| 355 | |
| 356 | void InvokeSumDoubleDoubleMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 357 | Method* method; |
| 358 | Object* receiver; |
| 359 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DD)D"); |
| 360 | |
| 361 | JValue args[2]; |
| 362 | JValue result; |
| 363 | |
| 364 | args[0].d = 0.0; |
| 365 | args[1].d = 0.0; |
| 366 | result.d = -1.0; |
| 367 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 368 | EXPECT_EQ(0.0, result.d); |
| 369 | |
| 370 | args[0].d = 1.0; |
| 371 | args[1].d = 2.0; |
| 372 | result.d = 0.0; |
| 373 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 374 | EXPECT_EQ(3.0, result.d); |
| 375 | |
| 376 | args[0].d = 1.0; |
| 377 | args[1].d = -2.0; |
| 378 | result.d = 0.0; |
| 379 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 380 | EXPECT_EQ(-1.0, result.d); |
| 381 | |
| 382 | args[0].d = DBL_MAX; |
| 383 | args[1].d = DBL_MIN; |
| 384 | result.d = 0.0; |
| 385 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 386 | EXPECT_EQ(1.7976931348623157e308, result.d); |
| 387 | |
| 388 | args[0].d = DBL_MAX; |
| 389 | args[1].d = DBL_MAX; |
| 390 | result.d = 0.0; |
| 391 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 392 | EXPECT_EQ(INFINITY, result.d); |
| 393 | } |
| 394 | |
| 395 | void InvokeSumDoubleDoubleDoubleMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 396 | Method* method; |
| 397 | Object* receiver; |
| 398 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDD)D"); |
| 399 | |
| 400 | JValue args[3]; |
| 401 | JValue result; |
| 402 | |
| 403 | args[0].d = 0.0; |
| 404 | args[1].d = 0.0; |
| 405 | args[2].d = 0.0; |
| 406 | result.d = -1.0; |
| 407 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 408 | EXPECT_EQ(0.0, result.d); |
| 409 | |
| 410 | args[0].d = 1.0; |
| 411 | args[1].d = 2.0; |
| 412 | args[2].d = 3.0; |
| 413 | result.d = 0.0; |
| 414 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 415 | EXPECT_EQ(6.0, result.d); |
| 416 | |
| 417 | args[0].d = 1.0; |
| 418 | args[1].d = -2.0; |
| 419 | args[2].d = 3.0; |
| 420 | result.d = 0.0; |
| 421 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 422 | EXPECT_EQ(2.0, result.d); |
| 423 | } |
| 424 | |
| 425 | void InvokeSumDoubleDoubleDoubleDoubleMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 426 | Method* method; |
| 427 | Object* receiver; |
| 428 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDDD)D"); |
| 429 | |
| 430 | JValue args[4]; |
| 431 | JValue result; |
| 432 | |
| 433 | args[0].d = 0.0; |
| 434 | args[1].d = 0.0; |
| 435 | args[2].d = 0.0; |
| 436 | args[3].d = 0.0; |
| 437 | result.d = -1.0; |
| 438 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 439 | EXPECT_EQ(0.0, result.d); |
| 440 | |
| 441 | args[0].d = 1.0; |
| 442 | args[1].d = 2.0; |
| 443 | args[2].d = 3.0; |
| 444 | args[3].d = 4.0; |
| 445 | result.d = 0.0; |
| 446 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 447 | EXPECT_EQ(10.0, result.d); |
| 448 | |
| 449 | args[0].d = 1.0; |
| 450 | args[1].d = -2.0; |
| 451 | args[2].d = 3.0; |
| 452 | args[3].d = -4.0; |
| 453 | result.d = 0.0; |
| 454 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 455 | EXPECT_EQ(-2.0, result.d); |
| 456 | } |
| 457 | |
| 458 | void InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(bool is_static) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 459 | Method* method; |
| 460 | Object* receiver; |
| 461 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDDDD)D"); |
| 462 | |
| 463 | JValue args[5]; |
| 464 | JValue result; |
| 465 | |
| 466 | args[0].d = 0.0; |
| 467 | args[1].d = 0.0; |
| 468 | args[2].d = 0.0; |
| 469 | args[3].d = 0.0; |
| 470 | args[4].d = 0.0; |
| 471 | result.d = -1.0; |
| 472 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 473 | EXPECT_EQ(0.0, result.d); |
| 474 | |
| 475 | args[0].d = 1.0; |
| 476 | args[1].d = 2.0; |
| 477 | args[2].d = 3.0; |
| 478 | args[3].d = 4.0; |
| 479 | args[4].d = 5.0; |
| 480 | result.d = 0.0; |
| 481 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 482 | EXPECT_EQ(15.0, result.d); |
| 483 | |
| 484 | args[0].d = 1.0; |
| 485 | args[1].d = -2.0; |
| 486 | args[2].d = 3.0; |
| 487 | args[3].d = -4.0; |
| 488 | args[4].d = 5.0; |
| 489 | result.d = 0.0; |
| 490 | (*stub)(method, NULL, Thread::Current(), args, &result); |
| 491 | EXPECT_EQ(3.0, result.d); |
| 492 | } |
| 493 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 494 | JavaVMExt* vm_; |
| 495 | JNIEnvExt* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 496 | jclass aioobe_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 497 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 498 | }; |
| 499 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 500 | TEST_F(JniInternalTest, AllocObject) { |
| 501 | jclass c = env_->FindClass("java/lang/String"); |
| 502 | ASSERT_TRUE(c != NULL); |
| 503 | jobject o = env_->AllocObject(c); |
| 504 | ASSERT_TRUE(o != NULL); |
| 505 | |
| 506 | // We have an instance of the class we asked for... |
| 507 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 508 | // ...whose fields haven't been initialized because |
| 509 | // we didn't call a constructor. |
| 510 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 511 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
| 512 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL); |
| 513 | } |
| 514 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 515 | TEST_F(JniInternalTest, GetVersion) { |
| 516 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 517 | } |
| 518 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 519 | #define EXPECT_CLASS_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 520 | EXPECT_TRUE(env_->FindClass(NAME) != NULL); \ |
| 521 | EXPECT_FALSE(env_->ExceptionCheck()) |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 522 | |
| 523 | #define EXPECT_CLASS_NOT_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 524 | EXPECT_TRUE(env_->FindClass(NAME) == NULL); \ |
| 525 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 526 | env_->ExceptionClear() |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 527 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 528 | std::string gCheckJniAbortMessage; |
| 529 | void TestCheckJniAbortHook(const std::string& reason) { |
| 530 | gCheckJniAbortMessage = reason; |
| 531 | } |
| 532 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 533 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 534 | // Reference types... |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 535 | EXPECT_CLASS_FOUND("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 536 | // ...for arrays too, where you must include "L;". |
| 537 | EXPECT_CLASS_FOUND("[Ljava/lang/String;"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 538 | |
| 539 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 540 | // We support . as well as / for compatibility, if -Xcheck:jni is off. |
| 541 | EXPECT_CLASS_FOUND("java.lang.String"); |
| 542 | EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 543 | EXPECT_CLASS_FOUND("[Ljava.lang.String;"); |
| 544 | EXPECT_CLASS_NOT_FOUND("[java.lang.String"); |
| 545 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 546 | // You can't include the "L;" in a JNI class descriptor. |
| 547 | EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;"); |
| 548 | // But you must include it for an array of any reference type. |
| 549 | EXPECT_CLASS_NOT_FOUND("[java/lang/String"); |
| 550 | vm_->check_jni_abort_hook = NULL; |
| 551 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 552 | // Primitive arrays are okay (if the primitive type is valid)... |
| 553 | EXPECT_CLASS_FOUND("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 554 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 555 | EXPECT_CLASS_NOT_FOUND("[K"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 556 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 557 | // But primitive types aren't allowed... |
| 558 | EXPECT_CLASS_NOT_FOUND("C"); |
| 559 | EXPECT_CLASS_NOT_FOUND("K"); |
| 560 | } |
| 561 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 562 | #define EXPECT_EXCEPTION(exception_class) \ |
| 563 | do { \ |
| 564 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 565 | jthrowable exception = env_->ExceptionOccurred(); \ |
| 566 | EXPECT_NE(static_cast<jthrowable>(NULL), exception); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 567 | env_->ExceptionClear(); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 568 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 569 | } while (false) |
| 570 | |
| 571 | TEST_F(JniInternalTest, GetFieldID) { |
| 572 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 573 | ASSERT_TRUE(jlnsfe != NULL); |
| 574 | jclass c = env_->FindClass("java/lang/String"); |
| 575 | ASSERT_TRUE(c != NULL); |
| 576 | |
| 577 | // Wrong type. |
| 578 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
| 579 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 580 | EXPECT_EXCEPTION(jlnsfe); |
| 581 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 582 | // Wrong type where type doesn't exist. |
| 583 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
| 584 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 585 | EXPECT_EXCEPTION(jlnsfe); |
| 586 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 587 | // Wrong name. |
| 588 | fid = env_->GetFieldID(c, "Count", "I"); |
| 589 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 590 | EXPECT_EXCEPTION(jlnsfe); |
| 591 | |
| 592 | // Good declared field lookup. |
| 593 | fid = env_->GetFieldID(c, "count", "I"); |
| 594 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 595 | EXPECT_TRUE(fid != NULL); |
| 596 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 597 | |
| 598 | // Good superclass field lookup. |
| 599 | c = env_->FindClass("java/lang/StringBuilder"); |
| 600 | fid = env_->GetFieldID(c, "count", "I"); |
| 601 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 602 | EXPECT_TRUE(fid != NULL); |
| 603 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 604 | |
| 605 | // Not instance. |
| 606 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 607 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 608 | EXPECT_EXCEPTION(jlnsfe); |
| 609 | } |
| 610 | |
| 611 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 612 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 613 | ASSERT_TRUE(jlnsfe != NULL); |
| 614 | jclass c = env_->FindClass("java/lang/String"); |
| 615 | ASSERT_TRUE(c != NULL); |
| 616 | |
| 617 | // Wrong type. |
| 618 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
| 619 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 620 | EXPECT_EXCEPTION(jlnsfe); |
| 621 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 622 | // Wrong type where type doesn't exist. |
| 623 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
| 624 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 625 | EXPECT_EXCEPTION(jlnsfe); |
| 626 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 627 | // Wrong name. |
| 628 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 629 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 630 | EXPECT_EXCEPTION(jlnsfe); |
| 631 | |
| 632 | // Good declared field lookup. |
| 633 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 634 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 635 | EXPECT_TRUE(fid != NULL); |
| 636 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 637 | |
| 638 | // Not static. |
| 639 | fid = env_->GetStaticFieldID(c, "count", "I"); |
| 640 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 641 | EXPECT_EXCEPTION(jlnsfe); |
| 642 | } |
| 643 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 644 | TEST_F(JniInternalTest, GetMethodID) { |
| 645 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 646 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 647 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 648 | |
| 649 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 650 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 651 | |
| 652 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 653 | // a pending exception |
| 654 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
| 655 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 656 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 657 | |
| 658 | // Check that java.lang.Object.equals() does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 659 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 660 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 661 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 662 | |
| 663 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
| 664 | // method is static |
| 665 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 666 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 667 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 671 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 672 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 673 | |
| 674 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 675 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 676 | |
| 677 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 678 | // a pending exception |
| 679 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
| 680 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 681 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 682 | |
| 683 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 684 | // the method is not static |
| 685 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 686 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 687 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 688 | |
| 689 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 690 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 691 | method = env_->GetStaticMethodID(jlstring, "valueOf", |
| 692 | "(I)Ljava/lang/String;"); |
| 693 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 694 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 697 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 698 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 699 | jclass c = env_->FindClass("java/lang/String"); |
| 700 | ASSERT_TRUE(c != NULL); |
| 701 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 702 | ASSERT_TRUE(fid != NULL); |
| 703 | // Turn the fid into a java.lang.reflect.Field... |
| 704 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
| 705 | ASSERT_TRUE(c != NULL); |
| 706 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 707 | // ...and back again. |
| 708 | jfieldID fid2 = env_->FromReflectedField(field); |
| 709 | ASSERT_TRUE(fid2 != NULL); |
| 710 | } |
| 711 | |
| 712 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 713 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
| 714 | jclass c = env_->FindClass("java/lang/String"); |
| 715 | ASSERT_TRUE(c != NULL); |
| 716 | jmethodID mid = env_->GetMethodID(c, "length", "()I"); |
| 717 | ASSERT_TRUE(mid != NULL); |
| 718 | // Turn the mid into a java.lang.reflect.Method... |
| 719 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 720 | ASSERT_TRUE(c != NULL); |
| 721 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 722 | // ...and back again. |
| 723 | jmethodID mid2 = env_->FromReflectedMethod(method); |
| 724 | ASSERT_TRUE(mid2 != NULL); |
| 725 | } |
| 726 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 727 | void BogusMethod() { |
| 728 | // You can't pass NULL function pointers to RegisterNatives. |
| 729 | } |
| 730 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 731 | TEST_F(JniInternalTest, RegisterNatives) { |
| 732 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 733 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 734 | |
| 735 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 736 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 737 | |
| 738 | // Check that registering to a non-existent java.lang.Object.foo() causes a |
| 739 | // NoSuchMethodError |
| 740 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 741 | JNINativeMethod methods[] = { { "foo", "()V", NULL } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 742 | env_->RegisterNatives(jlobject, methods, 1); |
| 743 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 744 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 745 | |
| 746 | // Check that registering non-native methods causes a NoSuchMethodError |
| 747 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 748 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", NULL } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 749 | env_->RegisterNatives(jlobject, methods, 1); |
| 750 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 751 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 752 | |
| 753 | // Check that registering native methods is successful |
| 754 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 755 | JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 756 | env_->RegisterNatives(jlobject, methods, 1); |
| 757 | } |
| 758 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 759 | |
| 760 | env_->UnregisterNatives(jlobject); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 763 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, get_region_fn, set_region_fn, get_elements_fn, release_elements_fn, scalar_type, expected_class_descriptor) \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 764 | jsize size = 4; \ |
| 765 | /* Allocate an array and check it has the right type and length. */ \ |
| 766 | scalar_type ## Array a = env_->new_fn(size); \ |
| 767 | EXPECT_TRUE(a != NULL); \ |
| 768 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 769 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
| 770 | /* AIOOBE for negative start offset. */ \ |
| 771 | env_->get_region_fn(a, -1, 1, NULL); \ |
| 772 | EXPECT_EXCEPTION(aioobe_); \ |
| 773 | env_->set_region_fn(a, -1, 1, NULL); \ |
| 774 | EXPECT_EXCEPTION(aioobe_); \ |
| 775 | /* AIOOBE for negative length. */ \ |
| 776 | env_->get_region_fn(a, 0, -1, NULL); \ |
| 777 | EXPECT_EXCEPTION(aioobe_); \ |
| 778 | env_->set_region_fn(a, 0, -1, NULL); \ |
| 779 | EXPECT_EXCEPTION(aioobe_); \ |
| 780 | /* AIOOBE for buffer overrun. */ \ |
| 781 | env_->get_region_fn(a, size - 1, size, NULL); \ |
| 782 | EXPECT_EXCEPTION(aioobe_); \ |
| 783 | env_->set_region_fn(a, size - 1, size, NULL); \ |
| 784 | EXPECT_EXCEPTION(aioobe_); \ |
| 785 | /* Prepare a couple of buffers. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 786 | UniquePtr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 787 | UniquePtr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 788 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 789 | for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \ |
| 790 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 791 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 792 | /* Copy back only part. */ \ |
| 793 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 794 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 795 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 796 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 797 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 798 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 799 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 800 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
| 801 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 802 | /* GetPrimitiveArrayCritical */ \ |
| 803 | void* v = env_->GetPrimitiveArrayCritical(a, NULL); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 804 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 805 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 806 | /* GetXArrayElements */ \ |
| 807 | scalar_type* xs = env_->get_elements_fn(a, NULL); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 808 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 809 | env_->release_elements_fn(a, xs, 0); \ |
| 810 | EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs)) |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 811 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 812 | TEST_F(JniInternalTest, BooleanArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 813 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 814 | } |
| 815 | TEST_F(JniInternalTest, ByteArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 816 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 817 | } |
| 818 | TEST_F(JniInternalTest, CharArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 819 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 820 | } |
| 821 | TEST_F(JniInternalTest, DoubleArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 822 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 823 | } |
| 824 | TEST_F(JniInternalTest, FloatArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 825 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 826 | } |
| 827 | TEST_F(JniInternalTest, IntArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 828 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 829 | } |
| 830 | TEST_F(JniInternalTest, LongArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 831 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 832 | } |
| 833 | TEST_F(JniInternalTest, ShortArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 834 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 837 | TEST_F(JniInternalTest, NewObjectArray) { |
| 838 | // TODO: death tests for negative array sizes. |
| 839 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 840 | // TODO: check non-NULL initial elements. |
| 841 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 842 | jclass element_class = env_->FindClass("java/lang/String"); |
| 843 | ASSERT_TRUE(element_class != NULL); |
| 844 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
| 845 | ASSERT_TRUE(array_class != NULL); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 846 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 847 | jobjectArray a; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 848 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 849 | a = env_->NewObjectArray(0, element_class, NULL); |
| 850 | EXPECT_TRUE(a != NULL); |
| 851 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 852 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 853 | |
| 854 | a = env_->NewObjectArray(1, element_class, NULL); |
| 855 | EXPECT_TRUE(a != NULL); |
| 856 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 857 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 858 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), NULL)); |
| 859 | |
| 860 | jstring s = env_->NewStringUTF("poop"); |
| 861 | a = env_->NewObjectArray(2, element_class, s); |
| 862 | EXPECT_TRUE(a != NULL); |
| 863 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 864 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 865 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 866 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | TEST_F(JniInternalTest, GetArrayLength) { |
| 870 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 873 | TEST_F(JniInternalTest, GetObjectClass) { |
| 874 | jclass string_class = env_->FindClass("java/lang/String"); |
| 875 | ASSERT_TRUE(string_class != NULL); |
| 876 | jclass class_class = env_->FindClass("java/lang/Class"); |
| 877 | ASSERT_TRUE(class_class != NULL); |
| 878 | |
| 879 | jstring s = env_->NewStringUTF("poop"); |
| 880 | jclass c = env_->GetObjectClass(s); |
| 881 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 882 | |
| 883 | jclass c2 = env_->GetObjectClass(c); |
| 884 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
| 885 | } |
| 886 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 887 | TEST_F(JniInternalTest, GetSuperclass) { |
| 888 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 889 | ASSERT_TRUE(object_class != NULL); |
| 890 | jclass string_class = env_->FindClass("java/lang/String"); |
| 891 | ASSERT_TRUE(string_class != NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 892 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
| 893 | ASSERT_TRUE(runnable_interface != NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 894 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
| 895 | ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 896 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 899 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 900 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 901 | ASSERT_TRUE(object_class != NULL); |
| 902 | jclass string_class = env_->FindClass("java/lang/String"); |
| 903 | ASSERT_TRUE(string_class != NULL); |
| 904 | |
| 905 | ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class)); |
| 906 | ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class)); |
| 907 | } |
| 908 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 909 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 910 | jclass local = env_->FindClass("java/lang/Object"); |
| 911 | ASSERT_TRUE(local != NULL); |
| 912 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 913 | |
| 914 | jobject global = env_->NewGlobalRef(local); |
| 915 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 916 | |
| 917 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 918 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 919 | |
| 920 | jobject invalid = reinterpret_cast<jobject>(this); |
| 921 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 922 | |
| 923 | // TODO: invoke a native method and test that its arguments are considered local references. |
| 924 | } |
| 925 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 926 | TEST_F(JniInternalTest, NewStringUTF) { |
| 927 | EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 928 | jstring s; |
| 929 | |
| 930 | s = env_->NewStringUTF(""); |
| 931 | EXPECT_TRUE(s != NULL); |
| 932 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 933 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 934 | s = env_->NewStringUTF("hello"); |
| 935 | EXPECT_TRUE(s != NULL); |
| 936 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 937 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 938 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 939 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 942 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 943 | jchar chars[] = { 'h', 'i' }; |
| 944 | jstring s; |
| 945 | s = env_->NewString(chars, 0); |
| 946 | EXPECT_TRUE(s != NULL); |
| 947 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 948 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 949 | s = env_->NewString(chars, 2); |
| 950 | EXPECT_TRUE(s != NULL); |
| 951 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 952 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 953 | |
| 954 | // TODO: check some non-ASCII strings. |
| 955 | } |
| 956 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 957 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
| 958 | jstring s = env_->NewString(NULL, 0); |
| 959 | EXPECT_TRUE(s != NULL); |
| 960 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 961 | } |
| 962 | |
Brian Carlstrom | 3625812 | 2011-12-09 12:55:51 -0800 | [diff] [blame] | 963 | // TODO: fix gtest death tests on host http://b/5690440 (and target) |
| 964 | TEST_F(JniInternalTest, DISABLED_NewStringNullCharsNonzeroLength) { |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 965 | ASSERT_DEATH(env_->NewString(NULL, 1), ""); |
| 966 | } |
| 967 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 968 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 969 | // Already tested in the NewString/NewStringUTF tests. |
| 970 | } |
| 971 | |
| 972 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 973 | jstring s = env_->NewStringUTF("hello"); |
| 974 | ASSERT_TRUE(s != NULL); |
| 975 | |
| 976 | env_->GetStringRegion(s, -1, 0, NULL); |
| 977 | EXPECT_EXCEPTION(sioobe_); |
| 978 | env_->GetStringRegion(s, 0, -1, NULL); |
| 979 | EXPECT_EXCEPTION(sioobe_); |
| 980 | env_->GetStringRegion(s, 0, 10, NULL); |
| 981 | EXPECT_EXCEPTION(sioobe_); |
| 982 | env_->GetStringRegion(s, 10, 1, NULL); |
| 983 | EXPECT_EXCEPTION(sioobe_); |
| 984 | |
| 985 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 986 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 987 | EXPECT_EQ('x', chars[0]); |
| 988 | EXPECT_EQ('e', chars[1]); |
| 989 | EXPECT_EQ('l', chars[2]); |
| 990 | EXPECT_EQ('x', chars[3]); |
| 991 | |
| 992 | env_->GetStringUTFRegion(s, -1, 0, NULL); |
| 993 | EXPECT_EXCEPTION(sioobe_); |
| 994 | env_->GetStringUTFRegion(s, 0, -1, NULL); |
| 995 | EXPECT_EXCEPTION(sioobe_); |
| 996 | env_->GetStringUTFRegion(s, 0, 10, NULL); |
| 997 | EXPECT_EXCEPTION(sioobe_); |
| 998 | env_->GetStringUTFRegion(s, 10, 1, NULL); |
| 999 | EXPECT_EXCEPTION(sioobe_); |
| 1000 | |
| 1001 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 1002 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 1003 | EXPECT_EQ('x', bytes[0]); |
| 1004 | EXPECT_EQ('e', bytes[1]); |
| 1005 | EXPECT_EQ('l', bytes[2]); |
| 1006 | EXPECT_EQ('x', bytes[3]); |
| 1007 | } |
| 1008 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1009 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1010 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 1011 | // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni. |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1012 | EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1013 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1014 | |
| 1015 | jstring s = env_->NewStringUTF("hello"); |
| 1016 | ASSERT_TRUE(s != NULL); |
| 1017 | |
| 1018 | const char* utf = env_->GetStringUTFChars(s, NULL); |
| 1019 | EXPECT_STREQ("hello", utf); |
| 1020 | env_->ReleaseStringUTFChars(s, utf); |
| 1021 | |
| 1022 | jboolean is_copy = JNI_FALSE; |
| 1023 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 1024 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1025 | EXPECT_STREQ("hello", utf); |
| 1026 | env_->ReleaseStringUTFChars(s, utf); |
| 1027 | } |
| 1028 | |
| 1029 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 1030 | jstring s = env_->NewStringUTF("hello"); |
| 1031 | ASSERT_TRUE(s != NULL); |
| 1032 | |
| 1033 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 1034 | const jchar* chars = env_->GetStringChars(s, NULL); |
| 1035 | EXPECT_EQ(expected[0], chars[0]); |
| 1036 | EXPECT_EQ(expected[1], chars[1]); |
| 1037 | EXPECT_EQ(expected[2], chars[2]); |
| 1038 | EXPECT_EQ(expected[3], chars[3]); |
| 1039 | EXPECT_EQ(expected[4], chars[4]); |
| 1040 | env_->ReleaseStringChars(s, chars); |
| 1041 | |
| 1042 | jboolean is_copy = JNI_FALSE; |
| 1043 | chars = env_->GetStringChars(s, &is_copy); |
| 1044 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1045 | EXPECT_EQ(expected[0], chars[0]); |
| 1046 | EXPECT_EQ(expected[1], chars[1]); |
| 1047 | EXPECT_EQ(expected[2], chars[2]); |
| 1048 | EXPECT_EQ(expected[3], chars[3]); |
| 1049 | EXPECT_EQ(expected[4], chars[4]); |
| 1050 | env_->ReleaseStringChars(s, chars); |
| 1051 | } |
| 1052 | |
| 1053 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 1054 | jstring s = env_->NewStringUTF("hello"); |
| 1055 | ASSERT_TRUE(s != NULL); |
| 1056 | |
| 1057 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 1058 | const jchar* chars = env_->GetStringCritical(s, NULL); |
| 1059 | EXPECT_EQ(expected[0], chars[0]); |
| 1060 | EXPECT_EQ(expected[1], chars[1]); |
| 1061 | EXPECT_EQ(expected[2], chars[2]); |
| 1062 | EXPECT_EQ(expected[3], chars[3]); |
| 1063 | EXPECT_EQ(expected[4], chars[4]); |
| 1064 | env_->ReleaseStringCritical(s, chars); |
| 1065 | |
| 1066 | jboolean is_copy = JNI_FALSE; |
| 1067 | chars = env_->GetStringCritical(s, &is_copy); |
| 1068 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1069 | EXPECT_EQ(expected[0], chars[0]); |
| 1070 | EXPECT_EQ(expected[1], chars[1]); |
| 1071 | EXPECT_EQ(expected[2], chars[2]); |
| 1072 | EXPECT_EQ(expected[3], chars[3]); |
| 1073 | EXPECT_EQ(expected[4], chars[4]); |
| 1074 | env_->ReleaseStringCritical(s, chars); |
| 1075 | } |
| 1076 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1077 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1078 | jclass c = env_->FindClass("java/lang/Object"); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1079 | ASSERT_TRUE(c != NULL); |
| 1080 | |
| 1081 | jobjectArray array = env_->NewObjectArray(1, c, NULL); |
| 1082 | EXPECT_TRUE(array != NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1083 | EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1084 | env_->SetObjectArrayElement(array, 0, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1085 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), c)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1086 | |
| 1087 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1088 | env_->SetObjectArrayElement(array, -1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1089 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1090 | |
| 1091 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1092 | env_->SetObjectArrayElement(array, 1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1093 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1094 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1095 | // TODO: check ArrayStoreException thrown for bad types. |
| 1096 | } |
| 1097 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1098 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 1099 | do { \ |
| 1100 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
| 1101 | EXPECT_TRUE(fid != NULL); \ |
| 1102 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1103 | EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1104 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1105 | EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1106 | } while (false) |
| 1107 | |
| 1108 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 1109 | do { \ |
| 1110 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
| 1111 | EXPECT_TRUE(fid != NULL); \ |
| 1112 | env_->Set ## type ## Field(instance, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1113 | EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1114 | env_->Set ## type ## Field(instance, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1115 | EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1116 | } while (false) |
| 1117 | |
| 1118 | |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 1119 | #if !defined(ART_USE_LLVM_COMPILER) |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1120 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1121 | SirtRef<ClassLoader> class_loader(LoadDex("AllFields")); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1122 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1123 | |
| 1124 | jclass c = env_->FindClass("AllFields"); |
| 1125 | ASSERT_TRUE(c != NULL); |
| 1126 | jobject o = env_->AllocObject(c); |
| 1127 | ASSERT_TRUE(o != NULL); |
| 1128 | |
| 1129 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false); |
| 1130 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 1131 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 1132 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 1133 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 1134 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 1135 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 1136 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 1137 | |
| 1138 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false); |
| 1139 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 1140 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 1141 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 1142 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 1143 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 1144 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 1145 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 1146 | } |
| 1147 | |
| 1148 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1149 | SirtRef<ClassLoader> class_loader(LoadDex("AllFields")); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1150 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1151 | |
| 1152 | jclass c = env_->FindClass("AllFields"); |
| 1153 | ASSERT_TRUE(c != NULL); |
| 1154 | jobject o = env_->AllocObject(c); |
| 1155 | ASSERT_TRUE(o != NULL); |
| 1156 | |
| 1157 | jstring s1 = env_->NewStringUTF("hello"); |
| 1158 | ASSERT_TRUE(s1 != NULL); |
| 1159 | jstring s2 = env_->NewStringUTF("world"); |
| 1160 | ASSERT_TRUE(s2 != NULL); |
| 1161 | |
| 1162 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
| 1163 | ASSERT_TRUE(s_fid != NULL); |
| 1164 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
| 1165 | ASSERT_TRUE(i_fid != NULL); |
| 1166 | |
| 1167 | env_->SetStaticObjectField(c, s_fid, s1); |
| 1168 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 1169 | env_->SetStaticObjectField(c, s_fid, s2); |
| 1170 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 1171 | |
| 1172 | env_->SetObjectField(o, i_fid, s1); |
| 1173 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 1174 | env_->SetObjectField(o, i_fid, s2); |
| 1175 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 1176 | } |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 1177 | #endif |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1178 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1179 | TEST_F(JniInternalTest, NewLocalRef_NULL) { |
| 1180 | EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL); |
| 1181 | } |
| 1182 | |
| 1183 | TEST_F(JniInternalTest, NewLocalRef) { |
| 1184 | jstring s = env_->NewStringUTF(""); |
| 1185 | ASSERT_TRUE(s != NULL); |
| 1186 | jobject o = env_->NewLocalRef(s); |
| 1187 | EXPECT_TRUE(o != NULL); |
| 1188 | EXPECT_TRUE(o != s); |
| 1189 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1190 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | TEST_F(JniInternalTest, DeleteLocalRef_NULL) { |
| 1194 | env_->DeleteLocalRef(NULL); |
| 1195 | } |
| 1196 | |
| 1197 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 1198 | jstring s = env_->NewStringUTF(""); |
| 1199 | ASSERT_TRUE(s != NULL); |
| 1200 | env_->DeleteLocalRef(s); |
| 1201 | |
| 1202 | // Currently, deleting an already-deleted reference is just a warning. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1203 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1204 | env_->DeleteLocalRef(s); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1205 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1206 | |
| 1207 | s = env_->NewStringUTF(""); |
| 1208 | ASSERT_TRUE(s != NULL); |
| 1209 | jobject o = env_->NewLocalRef(s); |
| 1210 | ASSERT_TRUE(o != NULL); |
| 1211 | |
| 1212 | env_->DeleteLocalRef(s); |
| 1213 | env_->DeleteLocalRef(o); |
| 1214 | } |
| 1215 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1216 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 1217 | jobject original = env_->NewStringUTF(""); |
| 1218 | ASSERT_TRUE(original != NULL); |
| 1219 | |
| 1220 | jobject outer; |
| 1221 | jobject inner1, inner2; |
| 1222 | Object* inner2_direct_pointer; |
| 1223 | { |
| 1224 | env_->PushLocalFrame(4); |
| 1225 | outer = env_->NewLocalRef(original); |
| 1226 | |
| 1227 | { |
| 1228 | env_->PushLocalFrame(4); |
| 1229 | inner1 = env_->NewLocalRef(outer); |
| 1230 | inner2 = env_->NewStringUTF("survivor"); |
| 1231 | inner2_direct_pointer = Decode<Object*>(env_, inner2); |
| 1232 | env_->PopLocalFrame(inner2); |
| 1233 | } |
| 1234 | |
| 1235 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1236 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
| 1237 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1238 | |
| 1239 | // Our local reference for the survivor is invalid because the survivor |
| 1240 | // gets a new local reference... |
| 1241 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1242 | // ...but the survivor should be in the local reference table. |
| 1243 | EXPECT_TRUE(env_->locals.ContainsDirectPointer(inner2_direct_pointer)); |
| 1244 | |
| 1245 | env_->PopLocalFrame(NULL); |
| 1246 | } |
| 1247 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1248 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
| 1249 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1250 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1251 | } |
| 1252 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1253 | TEST_F(JniInternalTest, NewGlobalRef_NULL) { |
| 1254 | EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL); |
| 1255 | } |
| 1256 | |
| 1257 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1258 | jstring s = env_->NewStringUTF(""); |
| 1259 | ASSERT_TRUE(s != NULL); |
| 1260 | jobject o = env_->NewGlobalRef(s); |
| 1261 | EXPECT_TRUE(o != NULL); |
| 1262 | EXPECT_TRUE(o != s); |
| 1263 | |
| 1264 | // TODO: check that o is a global reference. |
| 1265 | } |
| 1266 | |
| 1267 | TEST_F(JniInternalTest, DeleteGlobalRef_NULL) { |
| 1268 | env_->DeleteGlobalRef(NULL); |
| 1269 | } |
| 1270 | |
| 1271 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 1272 | jstring s = env_->NewStringUTF(""); |
| 1273 | ASSERT_TRUE(s != NULL); |
| 1274 | |
| 1275 | jobject o = env_->NewGlobalRef(s); |
| 1276 | ASSERT_TRUE(o != NULL); |
| 1277 | env_->DeleteGlobalRef(o); |
| 1278 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1279 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1280 | // Currently, deleting an already-deleted reference is just a warning. |
| 1281 | env_->DeleteGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1282 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1283 | |
| 1284 | jobject o1 = env_->NewGlobalRef(s); |
| 1285 | ASSERT_TRUE(o1 != NULL); |
| 1286 | jobject o2 = env_->NewGlobalRef(s); |
| 1287 | ASSERT_TRUE(o2 != NULL); |
| 1288 | |
| 1289 | env_->DeleteGlobalRef(o1); |
| 1290 | env_->DeleteGlobalRef(o2); |
| 1291 | } |
| 1292 | |
| 1293 | TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) { |
| 1294 | EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL); |
| 1295 | } |
| 1296 | |
| 1297 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1298 | jstring s = env_->NewStringUTF(""); |
| 1299 | ASSERT_TRUE(s != NULL); |
| 1300 | jobject o = env_->NewWeakGlobalRef(s); |
| 1301 | EXPECT_TRUE(o != NULL); |
| 1302 | EXPECT_TRUE(o != s); |
| 1303 | |
| 1304 | // TODO: check that o is a weak global reference. |
| 1305 | } |
| 1306 | |
| 1307 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) { |
| 1308 | env_->DeleteWeakGlobalRef(NULL); |
| 1309 | } |
| 1310 | |
| 1311 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 1312 | jstring s = env_->NewStringUTF(""); |
| 1313 | ASSERT_TRUE(s != NULL); |
| 1314 | |
| 1315 | jobject o = env_->NewWeakGlobalRef(s); |
| 1316 | ASSERT_TRUE(o != NULL); |
| 1317 | env_->DeleteWeakGlobalRef(o); |
| 1318 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1319 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1320 | // Currently, deleting an already-deleted reference is just a warning. |
| 1321 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1322 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1323 | |
| 1324 | jobject o1 = env_->NewWeakGlobalRef(s); |
| 1325 | ASSERT_TRUE(o1 != NULL); |
| 1326 | jobject o2 = env_->NewWeakGlobalRef(s); |
| 1327 | ASSERT_TRUE(o2 != NULL); |
| 1328 | |
| 1329 | env_->DeleteWeakGlobalRef(o1); |
| 1330 | env_->DeleteWeakGlobalRef(o2); |
| 1331 | } |
| 1332 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1333 | TEST_F(JniInternalTest, StaticMainMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1334 | SirtRef<ClassLoader> class_loader(LoadDex("Main")); |
| 1335 | CompileDirectMethod(class_loader.get(), "Main", "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1336 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1337 | Class* klass = class_linker_->FindClass("LMain;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1338 | ASSERT_TRUE(klass != NULL); |
| 1339 | |
| 1340 | Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V"); |
| 1341 | ASSERT_TRUE(method != NULL); |
| 1342 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1343 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1344 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1345 | JValue args[1]; |
| 1346 | args[0].l = NULL; |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1347 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1348 | (*stub)(method, NULL, Thread::Current(), args, NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | TEST_F(JniInternalTest, StaticNopMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1352 | InvokeNopMethod(true); |
| 1353 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1354 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1355 | TEST_F(JniInternalTest, NonStaticNopMethod) { |
| 1356 | InvokeNopMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | TEST_F(JniInternalTest, StaticIdentityByteMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1360 | InvokeIdentityByteMethod(true); |
| 1361 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1362 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1363 | TEST_F(JniInternalTest, NonStaticIdentityByteMethod) { |
| 1364 | InvokeIdentityByteMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | TEST_F(JniInternalTest, StaticIdentityIntMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1368 | InvokeIdentityIntMethod(true); |
| 1369 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1370 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1371 | TEST_F(JniInternalTest, NonStaticIdentityIntMethod) { |
| 1372 | InvokeIdentityIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | TEST_F(JniInternalTest, StaticIdentityDoubleMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1376 | InvokeIdentityDoubleMethod(true); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1379 | TEST_F(JniInternalTest, NonStaticIdentityDoubleMethod) { |
| 1380 | InvokeIdentityDoubleMethod(false); |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1383 | TEST_F(JniInternalTest, StaticSumIntIntMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1384 | InvokeSumIntIntMethod(true); |
| 1385 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1386 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1387 | TEST_F(JniInternalTest, NonStaticSumIntIntMethod) { |
| 1388 | InvokeSumIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | TEST_F(JniInternalTest, StaticSumIntIntIntMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1392 | InvokeSumIntIntIntMethod(true); |
| 1393 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1394 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1395 | TEST_F(JniInternalTest, NonStaticSumIntIntIntMethod) { |
| 1396 | InvokeSumIntIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1400 | InvokeSumIntIntIntIntMethod(true); |
| 1401 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1402 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1403 | TEST_F(JniInternalTest, NonStaticSumIntIntIntIntMethod) { |
| 1404 | InvokeSumIntIntIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1408 | InvokeSumIntIntIntIntIntMethod(true); |
| 1409 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1410 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1411 | TEST_F(JniInternalTest, NonStaticSumIntIntIntIntIntMethod) { |
| 1412 | InvokeSumIntIntIntIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1416 | InvokeSumDoubleDoubleMethod(true); |
| 1417 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1418 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1419 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleMethod) { |
| 1420 | InvokeSumDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1424 | InvokeSumDoubleDoubleDoubleMethod(true); |
| 1425 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1426 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1427 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleMethod) { |
| 1428 | InvokeSumDoubleDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1432 | InvokeSumDoubleDoubleDoubleDoubleMethod(true); |
| 1433 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1434 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1435 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleMethod) { |
| 1436 | InvokeSumDoubleDoubleDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1440 | InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(true); |
| 1441 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1442 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1443 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
| 1444 | InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1445 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1446 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1447 | TEST_F(JniInternalTest, Throw) { |
| 1448 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1449 | |
| 1450 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1451 | ASSERT_TRUE(exception_class != NULL); |
| 1452 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
| 1453 | ASSERT_TRUE(exception != NULL); |
| 1454 | |
| 1455 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1456 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1457 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1458 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1459 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | TEST_F(JniInternalTest, ThrowNew) { |
| 1463 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1464 | |
| 1465 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1466 | ASSERT_TRUE(exception_class != NULL); |
| 1467 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1468 | jthrowable thrown_exception; |
| 1469 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1470 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1471 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1472 | thrown_exception = env_->ExceptionOccurred(); |
| 1473 | env_->ExceptionClear(); |
| 1474 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1475 | |
| 1476 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL)); |
| 1477 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1478 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1479 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1480 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1483 | // TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>. |
| 1484 | TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1485 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
| 1486 | ASSERT_TRUE(buffer_class != NULL); |
| 1487 | |
| 1488 | char bytes[1024]; |
| 1489 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
| 1490 | ASSERT_TRUE(buffer != NULL); |
| 1491 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
| 1492 | ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes); |
| 1493 | ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes)); |
| 1494 | } |
| 1495 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1496 | TEST_F(JniInternalTest, MonitorEnterExit) { |
| 1497 | // Create an object to torture |
| 1498 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 1499 | ASSERT_TRUE(object_class != NULL); |
| 1500 | jobject object = env_->AllocObject(object_class); |
| 1501 | ASSERT_TRUE(object != NULL); |
| 1502 | |
| 1503 | // Expected class of exceptions |
| 1504 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
| 1505 | ASSERT_TRUE(imse_class != NULL); |
| 1506 | |
| 1507 | jthrowable thrown_exception; |
| 1508 | |
| 1509 | // Unlock of unowned monitor |
| 1510 | env_->MonitorExit(object); |
| 1511 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1512 | thrown_exception = env_->ExceptionOccurred(); |
| 1513 | env_->ExceptionClear(); |
| 1514 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 1515 | |
| 1516 | // Lock of unowned monitor |
| 1517 | env_->MonitorEnter(object); |
| 1518 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1519 | // Regular unlock |
| 1520 | env_->MonitorExit(object); |
| 1521 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1522 | |
| 1523 | // Recursively lock a lot |
| 1524 | size_t max_recursive_lock = 1024; |
| 1525 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1526 | env_->MonitorEnter(object); |
| 1527 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1528 | } |
| 1529 | // Recursively unlock a lot |
| 1530 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1531 | env_->MonitorExit(object); |
| 1532 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1533 | } |
| 1534 | |
| 1535 | // Unlock of unowned monitor |
| 1536 | env_->MonitorExit(object); |
| 1537 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1538 | thrown_exception = env_->ExceptionOccurred(); |
| 1539 | env_->ExceptionClear(); |
| 1540 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 1541 | |
| 1542 | // It's an error to call MonitorEnter or MonitorExit on NULL. |
| 1543 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 1544 | env_->MonitorEnter(NULL); |
| 1545 | EXPECT_TRUE(gCheckJniAbortMessage.find("in call to MonitorEnter") != std::string::npos); |
| 1546 | env_->MonitorExit(NULL); |
| 1547 | EXPECT_TRUE(gCheckJniAbortMessage.find("in call to MonitorExit") != std::string::npos); |
| 1548 | vm_->check_jni_abort_hook = NULL; |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1551 | } // namespace art |