blob: 8bdc8c6ec90b42bc2313ee610c3c95a38e55db5e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Hughes0c9cd562011-08-12 10:59:29 -070016
Carl Shapiro9b9ba282011-08-14 15:30:39 -070017#include "jni_internal.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -070018
Carl Shapiro9b9ba282011-08-14 15:30:39 -070019#include <sys/mman.h>
20
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070021#include <cmath>
22
Carl Shapiro9b9ba282011-08-14 15:30:39 -070023#include "common_test.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070024#include "ScopedLocalRef.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -070025
26namespace art {
27
Brian Carlstromf734cf52011-08-17 16:28:14 -070028class JniInternalTest : public CommonTest {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070029 protected:
30 virtual void SetUp() {
Brian Carlstromf734cf52011-08-17 16:28:14 -070031 CommonTest::SetUp();
Elliott Hughes5174fe62011-08-23 15:12:35 -070032
Elliott Hughesa2501992011-08-26 19:39:54 -070033 vm_ = Runtime::Current()->GetJavaVM();
34
Elliott Hughes5174fe62011-08-23 15:12:35 -070035 // Turn on -verbose:jni for the JNI tests.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080036 gLogVerbosity.jni = true;
Elliott Hughes5174fe62011-08-23 15:12:35 -070037
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070038 env_ = Thread::Current()->GetJniEnv();
Elliott Hughesb465ab02011-08-24 11:21:21 -070039
Elliott Hughes726079d2011-10-07 18:43:44 -070040 ScopedLocalRef<jclass> aioobe(env_, env_->FindClass("java/lang/ArrayIndexOutOfBoundsException"));
41 CHECK(aioobe.get() != NULL);
42 aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get()));
Elliott Hughesb465ab02011-08-24 11:21:21 -070043
Elliott Hughes726079d2011-10-07 18:43:44 -070044 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 Hughesc7ac37f2011-08-12 12:21:58 -070053 }
Elliott Hughesb465ab02011-08-24 11:21:21 -070054
Elliott Hughes77405792012-03-15 15:22:12 -070055 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 Hughes77405792012-03-15 15:22:12 -070081 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 Hughes77405792012-03-15 15:22:12 -070088 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 Hughes77405792012-03-15 15:22:12 -0700117 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 Hughes77405792012-03-15 15:22:12 -0700146 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 Hughes77405792012-03-15 15:22:12 -0700175 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 Hughes77405792012-03-15 15:22:12 -0700213 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 Hughes77405792012-03-15 15:22:12 -0700256 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 Hughes77405792012-03-15 15:22:12 -0700304 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 Hughes77405792012-03-15 15:22:12 -0700357 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 Hughes77405792012-03-15 15:22:12 -0700396 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 Hughes77405792012-03-15 15:22:12 -0700426 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 Hughes77405792012-03-15 15:22:12 -0700459 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 Hughesa2501992011-08-26 19:39:54 -0700494 JavaVMExt* vm_;
495 JNIEnvExt* env_;
Elliott Hughes814e4032011-08-23 12:07:56 -0700496 jclass aioobe_;
Elliott Hughesb465ab02011-08-24 11:21:21 -0700497 jclass sioobe_;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700498};
499
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700500TEST_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 Hughesc7ac37f2011-08-12 12:21:58 -0700515TEST_F(JniInternalTest, GetVersion) {
516 ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
517}
518
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700519#define EXPECT_CLASS_FOUND(NAME) \
Elliott Hughesbd935992011-08-22 11:59:34 -0700520 EXPECT_TRUE(env_->FindClass(NAME) != NULL); \
521 EXPECT_FALSE(env_->ExceptionCheck())
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700522
523#define EXPECT_CLASS_NOT_FOUND(NAME) \
Elliott Hughesbd935992011-08-22 11:59:34 -0700524 EXPECT_TRUE(env_->FindClass(NAME) == NULL); \
525 EXPECT_TRUE(env_->ExceptionCheck()); \
526 env_->ExceptionClear()
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700527
Elliott Hughesa2501992011-08-26 19:39:54 -0700528std::string gCheckJniAbortMessage;
529void TestCheckJniAbortHook(const std::string& reason) {
530 gCheckJniAbortMessage = reason;
531}
532
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700533TEST_F(JniInternalTest, FindClass) {
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700534 // Reference types...
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700535 EXPECT_CLASS_FOUND("java/lang/String");
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700536 // ...for arrays too, where you must include "L;".
537 EXPECT_CLASS_FOUND("[Ljava/lang/String;");
Elliott Hughesa2501992011-08-26 19:39:54 -0700538
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 Hughes0c9cd562011-08-12 10:59:29 -0700543 EXPECT_CLASS_FOUND("[Ljava.lang.String;");
544 EXPECT_CLASS_NOT_FOUND("[java.lang.String");
545
Elliott Hughesa2501992011-08-26 19:39:54 -0700546 // 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 Hughes0c9cd562011-08-12 10:59:29 -0700552 // Primitive arrays are okay (if the primitive type is valid)...
553 EXPECT_CLASS_FOUND("[C");
Elliott Hughesa2501992011-08-26 19:39:54 -0700554 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700555 EXPECT_CLASS_NOT_FOUND("[K");
Elliott Hughesa2501992011-08-26 19:39:54 -0700556 vm_->check_jni_abort_hook = NULL;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700557 // But primitive types aren't allowed...
558 EXPECT_CLASS_NOT_FOUND("C");
559 EXPECT_CLASS_NOT_FOUND("K");
560}
561
Elliott Hughescdf53122011-08-19 15:46:09 -0700562#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 Hughescdf53122011-08-19 15:46:09 -0700567 env_->ExceptionClear(); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700568 EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \
Elliott Hughescdf53122011-08-19 15:46:09 -0700569 } while (false)
570
571TEST_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 Rogersb17d08b2011-09-02 16:16:49 -0700582 // 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 Hughescdf53122011-08-19 15:46:09 -0700587 // 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
611TEST_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 Rogersb17d08b2011-09-02 16:16:49 -0700622 // 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 Hughescdf53122011-08-19 15:46:09 -0700627 // 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 Rogers4dd71f12011-08-16 14:16:02 -0700644TEST_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 Hughescdf53122011-08-19 15:46:09 -0700650 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700651
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 Hughescdf53122011-08-19 15:46:09 -0700656 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700657
658 // Check that java.lang.Object.equals() does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700659 method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
660 EXPECT_NE(static_cast<jmethodID>(NULL), method);
661 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700662
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 Hughescdf53122011-08-19 15:46:09 -0700667 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700668}
669
670TEST_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 Hughescdf53122011-08-19 15:46:09 -0700675 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700676
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 Hughescdf53122011-08-19 15:46:09 -0700681 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700682
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 Hughescdf53122011-08-19 15:46:09 -0700687 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700688
689 // Check that java.lang.String.valueOf(int) does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700690 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 Rogers4dd71f12011-08-16 14:16:02 -0700695}
696
Elliott Hughescdf53122011-08-19 15:46:09 -0700697TEST_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
712TEST_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 Hughes5174fe62011-08-23 15:12:35 -0700727void BogusMethod() {
728 // You can't pass NULL function pointers to RegisterNatives.
729}
730
Ian Rogers4dd71f12011-08-16 14:16:02 -0700731TEST_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 Hughescdf53122011-08-19 15:46:09 -0700736 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700737
738 // Check that registering to a non-existent java.lang.Object.foo() causes a
739 // NoSuchMethodError
740 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700741 JNINativeMethod methods[] = { { "foo", "()V", NULL } };
Ian Rogers4dd71f12011-08-16 14:16:02 -0700742 env_->RegisterNatives(jlobject, methods, 1);
743 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700745
746 // Check that registering non-native methods causes a NoSuchMethodError
747 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700748 JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", NULL } };
Ian Rogers4dd71f12011-08-16 14:16:02 -0700749 env_->RegisterNatives(jlobject, methods, 1);
750 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700752
753 // Check that registering native methods is successful
754 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700755 JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } };
Ian Rogers4dd71f12011-08-16 14:16:02 -0700756 env_->RegisterNatives(jlobject, methods, 1);
757 }
758 EXPECT_FALSE(env_->ExceptionCheck());
Elliott Hughes5174fe62011-08-23 15:12:35 -0700759
760 env_->UnregisterNatives(jlobject);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700761}
762
Elliott Hughes75770752011-08-24 17:52:38 -0700763#define EXPECT_PRIMITIVE_ARRAY(new_fn, get_region_fn, set_region_fn, get_elements_fn, release_elements_fn, scalar_type, expected_class_descriptor) \
Elliott Hughes814e4032011-08-23 12:07:56 -0700764 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 Hughesee0fa762012-03-26 17:12:41 -0700786 UniquePtr<scalar_type[]> src_buf(new scalar_type[size]); \
787 UniquePtr<scalar_type[]> dst_buf(new scalar_type[size]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700788 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 Hughesee0fa762012-03-26 17:12:41 -0700791 env_->set_region_fn(a, 0, size, &src_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700792 /* Copy back only part. */ \
793 env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700794 EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "short copy equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -0700795 /* Copy the missing pieces. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700796 env_->get_region_fn(a, 0, 1, &dst_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700797 env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700798 EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "fixed copy not equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -0700799 /* Copy back the whole array. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700800 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 Hughes75770752011-08-24 17:52:38 -0700802 /* GetPrimitiveArrayCritical */ \
803 void* v = env_->GetPrimitiveArrayCritical(a, NULL); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700804 EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) << "GetPrimitiveArrayCritical not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700805 env_->ReleasePrimitiveArrayCritical(a, v, 0); \
806 /* GetXArrayElements */ \
807 scalar_type* xs = env_->get_elements_fn(a, NULL); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700808 EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) << # get_elements_fn " not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700809 env_->release_elements_fn(a, xs, 0); \
810 EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs))
Elliott Hughesbd935992011-08-22 11:59:34 -0700811
Elliott Hughes814e4032011-08-23 12:07:56 -0700812TEST_F(JniInternalTest, BooleanArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700813 EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z");
Elliott Hughes814e4032011-08-23 12:07:56 -0700814}
815TEST_F(JniInternalTest, ByteArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700816 EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B");
Elliott Hughes814e4032011-08-23 12:07:56 -0700817}
818TEST_F(JniInternalTest, CharArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700819 EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C");
Elliott Hughes814e4032011-08-23 12:07:56 -0700820}
821TEST_F(JniInternalTest, DoubleArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700822 EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D");
Elliott Hughes814e4032011-08-23 12:07:56 -0700823}
824TEST_F(JniInternalTest, FloatArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700825 EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F");
Elliott Hughes814e4032011-08-23 12:07:56 -0700826}
827TEST_F(JniInternalTest, IntArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700828 EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I");
Elliott Hughes814e4032011-08-23 12:07:56 -0700829}
830TEST_F(JniInternalTest, LongArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700831 EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J");
Elliott Hughes814e4032011-08-23 12:07:56 -0700832}
833TEST_F(JniInternalTest, ShortArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700834 EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700835}
836
Elliott Hughesf2682d52011-08-15 16:37:04 -0700837TEST_F(JniInternalTest, NewObjectArray) {
838 // TODO: death tests for negative array sizes.
839
Elliott Hughesf2682d52011-08-15 16:37:04 -0700840 // TODO: check non-NULL initial elements.
841
Elliott Hughesbd935992011-08-22 11:59:34 -0700842 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 Hughesf2682d52011-08-15 16:37:04 -0700846
Elliott Hughesbd935992011-08-22 11:59:34 -0700847 jobjectArray a;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700848
Elliott Hughesbd935992011-08-22 11:59:34 -0700849 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 Hughes75770752011-08-24 17:52:38 -0700858 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 Hughesbd935992011-08-22 11:59:34 -0700867}
868
869TEST_F(JniInternalTest, GetArrayLength) {
870 // Already tested in NewObjectArray/NewPrimitiveArray.
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700871}
872
Elliott Hughes37f7a402011-08-22 18:56:01 -0700873TEST_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 Hughes885c3bd2011-08-22 16:59:20 -0700887TEST_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 Rogersdc180202012-01-29 14:47:29 -0800892 jclass runnable_interface = env_->FindClass("java/lang/Runnable");
893 ASSERT_TRUE(runnable_interface != NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700894 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class)));
895 ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL);
Ian Rogersdc180202012-01-29 14:47:29 -0800896 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface)));
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700897}
898
Elliott Hughes37f7a402011-08-22 18:56:01 -0700899TEST_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 Hughesb465ab02011-08-24 11:21:21 -0700909TEST_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 Hughes8a26c5c2011-08-15 18:35:43 -0700926TEST_F(JniInternalTest, NewStringUTF) {
927 EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL);
Elliott Hughes814e4032011-08-23 12:07:56 -0700928 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 Hughes8a26c5c2011-08-15 18:35:43 -0700939 // TODO: check some non-ASCII strings.
Elliott Hughesf2682d52011-08-15 16:37:04 -0700940}
941
Elliott Hughes814e4032011-08-23 12:07:56 -0700942TEST_F(JniInternalTest, NewString) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700943 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 Wilson25e79a52011-11-18 15:31:58 -0500957TEST_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 Carlstrom36258122011-12-09 12:55:51 -0800963// TODO: fix gtest death tests on host http://b/5690440 (and target)
964TEST_F(JniInternalTest, DISABLED_NewStringNullCharsNonzeroLength) {
Jesse Wilson25e79a52011-11-18 15:31:58 -0500965 ASSERT_DEATH(env_->NewString(NULL, 1), "");
966}
967
Elliott Hughesb465ab02011-08-24 11:21:21 -0700968TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
969 // Already tested in the NewString/NewStringUTF tests.
970}
971
972TEST_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 Hughes75770752011-08-24 17:52:38 -07001009TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001010 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
1011 // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni.
Elliott Hughes75770752011-08-24 17:52:38 -07001012 EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL);
Elliott Hughesa2501992011-08-26 19:39:54 -07001013 vm_->check_jni_abort_hook = NULL;
Elliott Hughes75770752011-08-24 17:52:38 -07001014
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
1029TEST_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
1053TEST_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 Hughes814e4032011-08-23 12:07:56 -07001077TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001078 jclass c = env_->FindClass("java/lang/Object");
Elliott Hughes289da822011-08-16 10:11:20 -07001079 ASSERT_TRUE(c != NULL);
1080
1081 jobjectArray array = env_->NewObjectArray(1, c, NULL);
1082 EXPECT_TRUE(array != NULL);
Elliott Hughes814e4032011-08-23 12:07:56 -07001083 EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL);
Elliott Hughes289da822011-08-16 10:11:20 -07001084 env_->SetObjectArrayElement(array, 0, c);
Elliott Hughes814e4032011-08-23 12:07:56 -07001085 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), c));
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001086
1087 // ArrayIndexOutOfBounds for negative index.
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001088 env_->SetObjectArrayElement(array, -1, c);
Elliott Hughes814e4032011-08-23 12:07:56 -07001089 EXPECT_EXCEPTION(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001090
1091 // ArrayIndexOutOfBounds for too-large index.
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001092 env_->SetObjectArrayElement(array, 1, c);
Elliott Hughes814e4032011-08-23 12:07:56 -07001093 EXPECT_EXCEPTION(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001094
Elliott Hughes289da822011-08-16 10:11:20 -07001095 // TODO: check ArrayStoreException thrown for bad types.
1096}
1097
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001098#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 Carlstrom2e3d1b22012-01-09 18:01:56 -08001103 EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001104 env_->SetStatic ## type ## Field(c, fid, value2); \
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001105 EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001106 } 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 Carlstrom2e3d1b22012-01-09 18:01:56 -08001113 EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001114 env_->Set ## type ## Field(instance, fid, value2); \
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001115 EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001116 } while (false)
1117
1118
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -08001119#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001120TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001121 SirtRef<ClassLoader> class_loader(LoadDex("AllFields"));
Brian Carlstrom25c33252011-09-18 15:58:35 -07001122 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001123
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
1148TEST_F(JniInternalTest, GetObjectField_SetObjectField) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001149 SirtRef<ClassLoader> class_loader(LoadDex("AllFields"));
Brian Carlstrom25c33252011-09-18 15:58:35 -07001150 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001151
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 Liao5b8b1ed2012-02-23 23:48:21 -08001177#endif
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001178
Elliott Hughes18c07532011-08-18 15:50:51 -07001179TEST_F(JniInternalTest, NewLocalRef_NULL) {
1180 EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL);
1181}
1182
1183TEST_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 Hughes2ced6a52011-10-16 18:44:48 -07001190 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o));
Elliott Hughes18c07532011-08-18 15:50:51 -07001191}
1192
1193TEST_F(JniInternalTest, DeleteLocalRef_NULL) {
1194 env_->DeleteLocalRef(NULL);
1195}
1196
1197TEST_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 Hughesa2501992011-08-26 19:39:54 -07001203 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes18c07532011-08-18 15:50:51 -07001204 env_->DeleteLocalRef(s);
Elliott Hughesa2501992011-08-26 19:39:54 -07001205 vm_->check_jni_abort_hook = NULL;
Elliott Hughes18c07532011-08-18 15:50:51 -07001206
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 Hughes2ced6a52011-10-16 18:44:48 -07001216TEST_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 Hughes18c07532011-08-18 15:50:51 -07001253TEST_F(JniInternalTest, NewGlobalRef_NULL) {
1254 EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL);
1255}
1256
1257TEST_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
1267TEST_F(JniInternalTest, DeleteGlobalRef_NULL) {
1268 env_->DeleteGlobalRef(NULL);
1269}
1270
1271TEST_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 Hughesa2501992011-08-26 19:39:54 -07001279 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes18c07532011-08-18 15:50:51 -07001280 // Currently, deleting an already-deleted reference is just a warning.
1281 env_->DeleteGlobalRef(o);
Elliott Hughesa2501992011-08-26 19:39:54 -07001282 vm_->check_jni_abort_hook = NULL;
Elliott Hughes18c07532011-08-18 15:50:51 -07001283
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
1293TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) {
1294 EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL);
1295}
1296
1297TEST_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
1307TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) {
1308 env_->DeleteWeakGlobalRef(NULL);
1309}
1310
1311TEST_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 Hughesa2501992011-08-26 19:39:54 -07001319 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes18c07532011-08-18 15:50:51 -07001320 // Currently, deleting an already-deleted reference is just a warning.
1321 env_->DeleteWeakGlobalRef(o);
Elliott Hughesa2501992011-08-26 19:39:54 -07001322 vm_->check_jni_abort_hook = NULL;
Elliott Hughes18c07532011-08-18 15:50:51 -07001323
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 Shapiro9b9ba282011-08-14 15:30:39 -07001333TEST_F(JniInternalTest, StaticMainMethod) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001334 SirtRef<ClassLoader> class_loader(LoadDex("Main"));
1335 CompileDirectMethod(class_loader.get(), "Main", "main", "([Ljava/lang/String;)V");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001336
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001337 Class* klass = class_linker_->FindClass("LMain;", class_loader.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001338 ASSERT_TRUE(klass != NULL);
1339
1340 Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V");
1341 ASSERT_TRUE(method != NULL);
1342
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001343 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001344
Elliott Hughes77405792012-03-15 15:22:12 -07001345 JValue args[1];
1346 args[0].l = NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001347
Elliott Hughes77405792012-03-15 15:22:12 -07001348 (*stub)(method, NULL, Thread::Current(), args, NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001349}
1350
1351TEST_F(JniInternalTest, StaticNopMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001352 InvokeNopMethod(true);
1353}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001354
Elliott Hughes77405792012-03-15 15:22:12 -07001355TEST_F(JniInternalTest, NonStaticNopMethod) {
1356 InvokeNopMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001357}
1358
1359TEST_F(JniInternalTest, StaticIdentityByteMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001360 InvokeIdentityByteMethod(true);
1361}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001362
Elliott Hughes77405792012-03-15 15:22:12 -07001363TEST_F(JniInternalTest, NonStaticIdentityByteMethod) {
1364 InvokeIdentityByteMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001365}
1366
1367TEST_F(JniInternalTest, StaticIdentityIntMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001368 InvokeIdentityIntMethod(true);
1369}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001370
Elliott Hughes77405792012-03-15 15:22:12 -07001371TEST_F(JniInternalTest, NonStaticIdentityIntMethod) {
1372 InvokeIdentityIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001373}
1374
1375TEST_F(JniInternalTest, StaticIdentityDoubleMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001376 InvokeIdentityDoubleMethod(true);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001377}
1378
Elliott Hughes77405792012-03-15 15:22:12 -07001379TEST_F(JniInternalTest, NonStaticIdentityDoubleMethod) {
1380 InvokeIdentityDoubleMethod(false);
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -08001381}
1382
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001383TEST_F(JniInternalTest, StaticSumIntIntMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001384 InvokeSumIntIntMethod(true);
1385}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001386
Elliott Hughes77405792012-03-15 15:22:12 -07001387TEST_F(JniInternalTest, NonStaticSumIntIntMethod) {
1388 InvokeSumIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001389}
1390
1391TEST_F(JniInternalTest, StaticSumIntIntIntMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001392 InvokeSumIntIntIntMethod(true);
1393}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001394
Elliott Hughes77405792012-03-15 15:22:12 -07001395TEST_F(JniInternalTest, NonStaticSumIntIntIntMethod) {
1396 InvokeSumIntIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001397}
1398
1399TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001400 InvokeSumIntIntIntIntMethod(true);
1401}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001402
Elliott Hughes77405792012-03-15 15:22:12 -07001403TEST_F(JniInternalTest, NonStaticSumIntIntIntIntMethod) {
1404 InvokeSumIntIntIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001405}
1406
1407TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001408 InvokeSumIntIntIntIntIntMethod(true);
1409}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001410
Elliott Hughes77405792012-03-15 15:22:12 -07001411TEST_F(JniInternalTest, NonStaticSumIntIntIntIntIntMethod) {
1412 InvokeSumIntIntIntIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001413}
1414
1415TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001416 InvokeSumDoubleDoubleMethod(true);
1417}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001418
Elliott Hughes77405792012-03-15 15:22:12 -07001419TEST_F(JniInternalTest, NonStaticSumDoubleDoubleMethod) {
1420 InvokeSumDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001421}
1422
1423TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001424 InvokeSumDoubleDoubleDoubleMethod(true);
1425}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001426
Elliott Hughes77405792012-03-15 15:22:12 -07001427TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleMethod) {
1428 InvokeSumDoubleDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001429}
1430
1431TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001432 InvokeSumDoubleDoubleDoubleDoubleMethod(true);
1433}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001434
Elliott Hughes77405792012-03-15 15:22:12 -07001435TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleMethod) {
1436 InvokeSumDoubleDoubleDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001437}
1438
1439TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
Elliott Hughes77405792012-03-15 15:22:12 -07001440 InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(true);
1441}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001442
Elliott Hughes77405792012-03-15 15:22:12 -07001443TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
1444 InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001445}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001446
Elliott Hughes37f7a402011-08-22 18:56:01 -07001447TEST_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 Hughesa2501992011-08-26 19:39:54 -07001457 jthrowable thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001458 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001459 EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001460}
1461
1462TEST_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 Hughes5cb5ad22011-10-02 12:13:39 -07001468 jthrowable thrown_exception;
1469
Elliott Hughes37f7a402011-08-22 18:56:01 -07001470 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world"));
1471 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001472 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 Hughes37f7a402011-08-22 18:56:01 -07001479 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001480 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001481}
1482
Elliott Hughesb465ab02011-08-24 11:21:21 -07001483// TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>.
1484TEST_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 Rogers6d0b13e2012-02-07 09:25:29 -08001496TEST_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 Hughesa92853e2012-02-07 16:09:27 -08001541
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 Rogers6d0b13e2012-02-07 09:25:29 -08001549}
1550
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001551} // namespace art