blob: 7925aea97ca9dd1c10b62df60f6c69d08edb45e6 [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
Elliott Hughesf66330a2012-12-12 17:27:00 -080019#include <cfloat>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <cmath>
21
Carl Shapiro9b9ba282011-08-14 15:30:39 -070022#include "common_test.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070023#include "ScopedLocalRef.h"
Ian Rogers1f539342012-10-03 21:09:42 -070024#include "sirt_ref.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
Brian Carlstrom4d571432012-05-16 00:21:41 -070038 vm_->AttachCurrentThread(&env_, NULL);
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 Hughesb264f082012-04-06 17:10:10 -070044 ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException"));
45 CHECK(ase.get() != NULL);
46 ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get()));
47
Elliott Hughes726079d2011-10-07 18:43:44 -070048 ScopedLocalRef<jclass> sioobe(env_, env_->FindClass("java/lang/StringIndexOutOfBoundsException"));
49 CHECK(sioobe.get() != NULL);
50 sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get()));
51 }
52
Brian Carlstrom4d571432012-05-16 00:21:41 -070053 void CleanUpJniEnv() {
54 if (aioobe_ != NULL) {
55 env_->DeleteGlobalRef(aioobe_);
56 aioobe_ = NULL;
57 }
58 if (ase_ != NULL) {
59 env_->DeleteGlobalRef(ase_);
60 ase_ = NULL;
61 }
62 if (sioobe_ != NULL) {
63 env_->DeleteGlobalRef(sioobe_);
64 sioobe_ = NULL;
65 }
66 }
67
Elliott Hughes726079d2011-10-07 18:43:44 -070068 virtual void TearDown() {
Brian Carlstrom4d571432012-05-16 00:21:41 -070069 CleanUpJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -070070 CommonTest::TearDown();
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070071 }
Elliott Hughesb465ab02011-08-24 11:21:21 -070072
Mathieu Chartier66f19252012-09-18 08:57:04 -070073 AbstractMethod::InvokeStub* DoCompile(AbstractMethod*& method, Object*& receiver, bool is_static,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 const char* method_name, const char* method_signature)
Ian Rogersb726dcb2012-09-05 08:57:23 -070075 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -070076 const char* class_name = is_static ? "StaticLeafMethods" : "NonStaticLeafMethods";
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077 jobject jclass_loader(LoadDex(class_name));
Ian Rogers1f539342012-10-03 21:09:42 -070078 Thread* self = Thread::Current();
79 SirtRef<ClassLoader>
80 class_loader(self,
81 ScopedObjectAccessUnchecked(self).Decode<ClassLoader*>(jclass_loader));
Elliott Hughes77405792012-03-15 15:22:12 -070082 if (is_static) {
83 CompileDirectMethod(class_loader.get(), class_name, method_name, method_signature);
84 } else {
85 CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z");
86 CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V");
87 CompileVirtualMethod(class_loader.get(), class_name, method_name, method_signature);
88 }
89
90 Class* c = class_linker_->FindClass(DotToDescriptor(class_name).c_str(), class_loader.get());
91 CHECK(c != NULL);
92
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 method = is_static ? c->FindDirectMethod(method_name, method_signature)
94 : c->FindVirtualMethod(method_name, method_signature);
Elliott Hughes77405792012-03-15 15:22:12 -070095 CHECK(method != NULL);
96
Ian Rogers50b35e22012-10-04 10:09:15 -070097 receiver = (is_static ? NULL : c->AllocObject(self));
Elliott Hughes77405792012-03-15 15:22:12 -070098
Mathieu Chartier66f19252012-09-18 08:57:04 -070099 AbstractMethod::InvokeStub* stub = method->GetInvokeStub();
Elliott Hughes77405792012-03-15 15:22:12 -0700100 CHECK(stub != NULL);
101
102 return stub;
103 }
104
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 void InvokeNopMethod(bool is_static) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700106 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700107 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700108 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "nop", "()V");
Elliott Hughes77405792012-03-15 15:22:12 -0700109 (*stub)(method, receiver, Thread::Current(), NULL, NULL);
110 }
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 void InvokeIdentityByteMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700114 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700115 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700116 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(B)B");
Elliott Hughes77405792012-03-15 15:22:12 -0700117
118 JValue args[1];
119 JValue result;
120
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700121 args[0].SetB(0);
122 result.SetB(-1);
Elliott Hughes77405792012-03-15 15:22:12 -0700123 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700124 EXPECT_EQ(0, result.GetB());
Elliott Hughes77405792012-03-15 15:22:12 -0700125
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 args[0].SetB(-1);
127 result.SetB(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700128 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700129 EXPECT_EQ(-1, result.GetB());
Elliott Hughes77405792012-03-15 15:22:12 -0700130
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700131 args[0].SetB(SCHAR_MAX);
132 result.SetB(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700133 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700134 EXPECT_EQ(SCHAR_MAX, result.GetB());
Elliott Hughes77405792012-03-15 15:22:12 -0700135
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700136 args[0].SetB(SCHAR_MIN);
137 result.SetB(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700138 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700139 EXPECT_EQ(SCHAR_MIN, result.GetB());
Elliott Hughes77405792012-03-15 15:22:12 -0700140 }
141
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 void InvokeIdentityIntMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700144 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700145 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700146 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(I)I");
Elliott Hughes77405792012-03-15 15:22:12 -0700147
148 JValue args[1];
149 JValue result;
150
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700151 args[0].SetI(0);
152 result.SetI(-1);
Elliott Hughes77405792012-03-15 15:22:12 -0700153 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700154 EXPECT_EQ(0, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700155
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700156 args[0].SetI(-1);
157 result.SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700158 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700159 EXPECT_EQ(-1, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700160
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 args[0].SetI(INT_MAX);
162 result.SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700163 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 EXPECT_EQ(INT_MAX, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700165
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700166 args[0].SetI(INT_MIN);
167 result.SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700168 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700169 EXPECT_EQ(INT_MIN, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700170 }
171
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 void InvokeIdentityDoubleMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700173 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700174 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700175 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700176 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(D)D");
Elliott Hughes77405792012-03-15 15:22:12 -0700177
178 JValue args[1];
179 JValue result;
180
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700181 args[0].SetD(0.0);
182 result.SetD(-1.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700183 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700184 EXPECT_EQ(0.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700185
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700186 args[0].SetD(-1.0);
187 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700188 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700189 EXPECT_EQ(-1.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700190
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700191 args[0].SetD(DBL_MAX);
192 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700193 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700194 EXPECT_EQ(DBL_MAX, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700195
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700196 args[0].SetD(DBL_MIN);
197 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700198 (*stub)(method, receiver, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700199 EXPECT_EQ(DBL_MIN, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700200 }
201
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 void InvokeSumIntIntMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700204 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700205 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700206 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(II)I");
Elliott Hughes77405792012-03-15 15:22:12 -0700207
208 JValue result;
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700209 result.SetI(-1);
Elliott Hughes77405792012-03-15 15:22:12 -0700210 JValue args[2];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700211 args[0].SetI(0);
212 args[1].SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700213 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700214 EXPECT_EQ(0, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700215
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700216 result.SetI(0);
217 args[0].SetI(1);
218 args[1].SetI(2);
Elliott Hughes77405792012-03-15 15:22:12 -0700219 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700220 EXPECT_EQ(3, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700221
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700222 result.SetI(0);
223 args[0].SetI(-2);
224 args[1].SetI(5);
Elliott Hughes77405792012-03-15 15:22:12 -0700225 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700226 EXPECT_EQ(3, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700227
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700228 result.SetI(1234);
229 args[0].SetI(INT_MAX);
230 args[1].SetI(INT_MIN);
Elliott Hughes77405792012-03-15 15:22:12 -0700231 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700232 EXPECT_EQ(-1, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700233
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700234 result.SetI(INT_MIN);
235 args[0].SetI(INT_MAX);
236 args[1].SetI(INT_MAX);
Elliott Hughes77405792012-03-15 15:22:12 -0700237 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700238 EXPECT_EQ(-2, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700239 }
240
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 void InvokeSumIntIntIntMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700243 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700244 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700245 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(III)I");
Elliott Hughes77405792012-03-15 15:22:12 -0700246
247 JValue result;
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700248 result.SetI(-1);
Elliott Hughes77405792012-03-15 15:22:12 -0700249 JValue args[3];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700250 args[0].SetI(0);
251 args[1].SetI(0);
252 args[2].SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700253 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700254 EXPECT_EQ(0, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700255
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700256 result.SetI(0);
257 args[0].SetI(1);
258 args[1].SetI(2);
259 args[2].SetI(3);
Elliott Hughes77405792012-03-15 15:22:12 -0700260 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700261 EXPECT_EQ(6, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700262
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700263 result.SetI(0);
264 args[0].SetI(-1);
265 args[1].SetI(2);
266 args[2].SetI(-3);
Elliott Hughes77405792012-03-15 15:22:12 -0700267 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700268 EXPECT_EQ(-2, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700269
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700270 result.SetI(1234);
271 args[0].SetI(INT_MAX);
272 args[1].SetI(INT_MIN);
273 args[2].SetI(INT_MAX);
Elliott Hughes77405792012-03-15 15:22:12 -0700274 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700275 EXPECT_EQ(2147483646, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700276
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700277 result.SetI(INT_MIN);
278 args[0].SetI(INT_MAX);
279 args[1].SetI(INT_MAX);
280 args[2].SetI(INT_MAX);
Elliott Hughes77405792012-03-15 15:22:12 -0700281 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700282 EXPECT_EQ(2147483645, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700283 }
284
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 void InvokeSumIntIntIntIntMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700287 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700288 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700289 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(IIII)I");
Elliott Hughes77405792012-03-15 15:22:12 -0700290
291 JValue result;
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700292 result.SetI(-1);
Elliott Hughes77405792012-03-15 15:22:12 -0700293 JValue args[4];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700294 args[0].SetI(0);
295 args[1].SetI(0);
296 args[2].SetI(0);
297 args[3].SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700298 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700299 EXPECT_EQ(0, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700300
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700301 result.SetI(0);
302 args[0].SetI(1);
303 args[1].SetI(2);
304 args[2].SetI(3);
305 args[3].SetI(4);
Elliott Hughes77405792012-03-15 15:22:12 -0700306 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700307 EXPECT_EQ(10, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700308
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700309 result.SetI(0);
310 args[0].SetI(-1);
311 args[1].SetI(2);
312 args[2].SetI(-3);
313 args[3].SetI(4);
Elliott Hughes77405792012-03-15 15:22:12 -0700314 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700315 EXPECT_EQ(2, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700316
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700317 result.SetI(1234);
318 args[0].SetI(INT_MAX);
319 args[1].SetI(INT_MIN);
320 args[2].SetI(INT_MAX);
321 args[3].SetI(INT_MIN);
Elliott Hughes77405792012-03-15 15:22:12 -0700322 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700323 EXPECT_EQ(-2, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700324
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700325 result.SetI(INT_MIN);
326 args[0].SetI(INT_MAX);
327 args[1].SetI(INT_MAX);
328 args[2].SetI(INT_MAX);
329 args[3].SetI(INT_MAX);
Elliott Hughes77405792012-03-15 15:22:12 -0700330 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700331 EXPECT_EQ(-4, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700332 }
333
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 void InvokeSumIntIntIntIntIntMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700336 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700337 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700338 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(IIIII)I");
Elliott Hughes77405792012-03-15 15:22:12 -0700339
340 JValue result;
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700341 result.SetI(-1.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700342 JValue args[5];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700343 args[0].SetI(0);
344 args[1].SetI(0);
345 args[2].SetI(0);
346 args[3].SetI(0);
347 args[4].SetI(0);
Elliott Hughes77405792012-03-15 15:22:12 -0700348 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700349 EXPECT_EQ(0, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700350
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700351 result.SetI(0);
352 args[0].SetI(1);
353 args[1].SetI(2);
354 args[2].SetI(3);
355 args[3].SetI(4);
356 args[4].SetI(5);
Elliott Hughes77405792012-03-15 15:22:12 -0700357 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700358 EXPECT_EQ(15, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700359
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700360 result.SetI(0);
361 args[0].SetI(-1);
362 args[1].SetI(2);
363 args[2].SetI(-3);
364 args[3].SetI(4);
365 args[4].SetI(-5);
Elliott Hughes77405792012-03-15 15:22:12 -0700366 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700367 EXPECT_EQ(-3, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700368
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700369 result.SetI(1234);
370 args[0].SetI(INT_MAX);
371 args[1].SetI(INT_MIN);
372 args[2].SetI(INT_MAX);
373 args[3].SetI(INT_MIN);
374 args[4].SetI(INT_MAX);
Elliott Hughes77405792012-03-15 15:22:12 -0700375 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700376 EXPECT_EQ(2147483645, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700377
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700378 result.SetI(INT_MIN);
379 args[0].SetI(INT_MAX);
380 args[1].SetI(INT_MAX);
381 args[2].SetI(INT_MAX);
382 args[3].SetI(INT_MAX);
383 args[4].SetI(INT_MAX);
Elliott Hughes77405792012-03-15 15:22:12 -0700384 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700385 EXPECT_EQ(2147483643, result.GetI());
Elliott Hughes77405792012-03-15 15:22:12 -0700386 }
387
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 void InvokeSumDoubleDoubleMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700390 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700391 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700392 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DD)D");
Elliott Hughes77405792012-03-15 15:22:12 -0700393
394 JValue args[2];
395 JValue result;
396
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700397 args[0].SetD(0.0);
398 args[1].SetD(0.0);
399 result.SetD(-1.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700400 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700401 EXPECT_EQ(0.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700402
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700403 args[0].SetD(1.0);
404 args[1].SetD(2.0);
405 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700406 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700407 EXPECT_EQ(3.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700408
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700409 args[0].SetD(1.0);
410 args[1].SetD(-2.0);
411 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700412 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700413 EXPECT_EQ(-1.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700414
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700415 args[0].SetD(DBL_MAX);
416 args[1].SetD(DBL_MIN);
417 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700418 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700419 EXPECT_EQ(1.7976931348623157e308, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700420
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700421 args[0].SetD(DBL_MAX);
422 args[1].SetD(DBL_MAX);
423 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700424 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700425 EXPECT_EQ(INFINITY, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700426 }
427
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428 void InvokeSumDoubleDoubleDoubleMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700430 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700431 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700432 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDD)D");
Elliott Hughes77405792012-03-15 15:22:12 -0700433
434 JValue args[3];
435 JValue result;
436
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700437 args[0].SetD(0.0);
438 args[1].SetD(0.0);
439 args[2].SetD(0.0);
440 result.SetD(-1.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700441 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700442 EXPECT_EQ(0.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700443
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700444 args[0].SetD(1.0);
445 args[1].SetD(2.0);
446 args[2].SetD(3.0);
447 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700448 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700449 EXPECT_EQ(6.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700450
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700451 args[0].SetD(1.0);
452 args[1].SetD(-2.0);
453 args[2].SetD(3.0);
454 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700455 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700456 EXPECT_EQ(2.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700457 }
458
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 void InvokeSumDoubleDoubleDoubleDoubleMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700461 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700462 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700463 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDDD)D");
Elliott Hughes77405792012-03-15 15:22:12 -0700464
465 JValue args[4];
466 JValue result;
467
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700468 args[0].SetD(0.0);
469 args[1].SetD(0.0);
470 args[2].SetD(0.0);
471 args[3].SetD(0.0);
472 result.SetD(-1.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700473 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700474 EXPECT_EQ(0.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700475
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700476 args[0].SetD(1.0);
477 args[1].SetD(2.0);
478 args[2].SetD(3.0);
479 args[3].SetD(4.0);
480 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700481 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700482 EXPECT_EQ(10.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700483
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700484 args[0].SetD(1.0);
485 args[1].SetD(-2.0);
486 args[2].SetD(3.0);
487 args[3].SetD(-4.0);
488 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700489 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700490 EXPECT_EQ(-2.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700491 }
492
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 void InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700495 AbstractMethod* method;
Elliott Hughes77405792012-03-15 15:22:12 -0700496 Object* receiver;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700497 AbstractMethod::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDDDD)D");
Elliott Hughes77405792012-03-15 15:22:12 -0700498
499 JValue args[5];
500 JValue result;
501
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700502 args[0].SetD(0.0);
503 args[1].SetD(0.0);
504 args[2].SetD(0.0);
505 args[3].SetD(0.0);
506 args[4].SetD(0.0);
507 result.SetD(-1.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700508 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700509 EXPECT_EQ(0.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700510
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700511 args[0].SetD(1.0);
512 args[1].SetD(2.0);
513 args[2].SetD(3.0);
514 args[3].SetD(4.0);
515 args[4].SetD(5.0);
516 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700517 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700518 EXPECT_EQ(15.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700519
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700520 args[0].SetD(1.0);
521 args[1].SetD(-2.0);
522 args[2].SetD(3.0);
523 args[3].SetD(-4.0);
524 args[4].SetD(5.0);
525 result.SetD(0.0);
Elliott Hughes77405792012-03-15 15:22:12 -0700526 (*stub)(method, NULL, Thread::Current(), args, &result);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700527 EXPECT_EQ(3.0, result.GetD());
Elliott Hughes77405792012-03-15 15:22:12 -0700528 }
529
Elliott Hughesa2501992011-08-26 19:39:54 -0700530 JavaVMExt* vm_;
Brian Carlstrom4d571432012-05-16 00:21:41 -0700531 JNIEnv* env_;
Elliott Hughes814e4032011-08-23 12:07:56 -0700532 jclass aioobe_;
Elliott Hughesb264f082012-04-06 17:10:10 -0700533 jclass ase_;
Elliott Hughesb465ab02011-08-24 11:21:21 -0700534 jclass sioobe_;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700535};
536
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700537TEST_F(JniInternalTest, AllocObject) {
538 jclass c = env_->FindClass("java/lang/String");
539 ASSERT_TRUE(c != NULL);
540 jobject o = env_->AllocObject(c);
541 ASSERT_TRUE(o != NULL);
542
543 // We have an instance of the class we asked for...
544 ASSERT_TRUE(env_->IsInstanceOf(o, c));
545 // ...whose fields haven't been initialized because
546 // we didn't call a constructor.
547 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I")));
548 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I")));
549 ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL);
550}
551
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700552TEST_F(JniInternalTest, GetVersion) {
553 ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
554}
555
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700556#define EXPECT_CLASS_FOUND(NAME) \
Elliott Hughesbd935992011-08-22 11:59:34 -0700557 EXPECT_TRUE(env_->FindClass(NAME) != NULL); \
558 EXPECT_FALSE(env_->ExceptionCheck())
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700559
560#define EXPECT_CLASS_NOT_FOUND(NAME) \
Elliott Hughesbd935992011-08-22 11:59:34 -0700561 EXPECT_TRUE(env_->FindClass(NAME) == NULL); \
562 EXPECT_TRUE(env_->ExceptionCheck()); \
563 env_->ExceptionClear()
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700564
565TEST_F(JniInternalTest, FindClass) {
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700566 // Reference types...
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700567 EXPECT_CLASS_FOUND("java/lang/String");
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700568 // ...for arrays too, where you must include "L;".
569 EXPECT_CLASS_FOUND("[Ljava/lang/String;");
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700570 // Primitive arrays are okay too, if the primitive type is valid.
571 EXPECT_CLASS_FOUND("[C");
Elliott Hughesa2501992011-08-26 19:39:54 -0700572
Elliott Hughesb264f082012-04-06 17:10:10 -0700573 {
Elliott Hughesb264f082012-04-06 17:10:10 -0700574 // We support . as well as / for compatibility, if -Xcheck:jni is off.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700575 CheckJniAbortCatcher check_jni_abort_catcher;
Elliott Hughesb264f082012-04-06 17:10:10 -0700576 EXPECT_CLASS_FOUND("java.lang.String");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700577 check_jni_abort_catcher.Check("illegal class name 'java.lang.String'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700578 EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700579 check_jni_abort_catcher.Check("illegal class name 'Ljava.lang.String;'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700580 EXPECT_CLASS_FOUND("[Ljava.lang.String;");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700581 check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700582 EXPECT_CLASS_NOT_FOUND("[java.lang.String");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700583 check_jni_abort_catcher.Check("illegal class name '[java.lang.String'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700584
585 // You can't include the "L;" in a JNI class descriptor.
586 EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700587 check_jni_abort_catcher.Check("illegal class name 'Ljava/lang/String;'");
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700588
Elliott Hughesb264f082012-04-06 17:10:10 -0700589 // But you must include it for an array of any reference type.
590 EXPECT_CLASS_NOT_FOUND("[java/lang/String");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700591 check_jni_abort_catcher.Check("illegal class name '[java/lang/String'");
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700592
593 EXPECT_CLASS_NOT_FOUND("[K");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700594 check_jni_abort_catcher.Check("illegal class name '[K'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700595 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700596
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700597 // But primitive types aren't allowed...
598 EXPECT_CLASS_NOT_FOUND("C");
599 EXPECT_CLASS_NOT_FOUND("K");
600}
601
Elliott Hughescdf53122011-08-19 15:46:09 -0700602#define EXPECT_EXCEPTION(exception_class) \
603 do { \
604 EXPECT_TRUE(env_->ExceptionCheck()); \
605 jthrowable exception = env_->ExceptionOccurred(); \
606 EXPECT_NE(static_cast<jthrowable>(NULL), exception); \
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 env_->ExceptionClear(); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700608 EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 } while (false)
610
611TEST_F(JniInternalTest, GetFieldID) {
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_->GetFieldID(c, "count", "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_->GetFieldID(c, "count", "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_->GetFieldID(c, "Count", "I");
629 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
630 EXPECT_EXCEPTION(jlnsfe);
631
632 // Good declared field lookup.
633 fid = env_->GetFieldID(c, "count", "I");
634 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
635 EXPECT_TRUE(fid != NULL);
636 EXPECT_FALSE(env_->ExceptionCheck());
637
638 // Good superclass field lookup.
639 c = env_->FindClass("java/lang/StringBuilder");
640 fid = env_->GetFieldID(c, "count", "I");
641 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
642 EXPECT_TRUE(fid != NULL);
643 EXPECT_FALSE(env_->ExceptionCheck());
644
645 // Not instance.
646 fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
647 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
648 EXPECT_EXCEPTION(jlnsfe);
649}
650
651TEST_F(JniInternalTest, GetStaticFieldID) {
652 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
653 ASSERT_TRUE(jlnsfe != NULL);
654 jclass c = env_->FindClass("java/lang/String");
655 ASSERT_TRUE(c != NULL);
656
657 // Wrong type.
658 jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J");
659 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
660 EXPECT_EXCEPTION(jlnsfe);
661
Ian Rogersb17d08b2011-09-02 16:16:49 -0700662 // Wrong type where type doesn't exist.
663 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;");
664 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
665 EXPECT_EXCEPTION(jlnsfe);
666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 // Wrong name.
668 fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
669 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
670 EXPECT_EXCEPTION(jlnsfe);
671
672 // Good declared field lookup.
673 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
674 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
675 EXPECT_TRUE(fid != NULL);
676 EXPECT_FALSE(env_->ExceptionCheck());
677
678 // Not static.
679 fid = env_->GetStaticFieldID(c, "count", "I");
680 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
681 EXPECT_EXCEPTION(jlnsfe);
682}
683
Ian Rogers4dd71f12011-08-16 14:16:02 -0700684TEST_F(JniInternalTest, GetMethodID) {
685 jclass jlobject = env_->FindClass("java/lang/Object");
686 jclass jlstring = env_->FindClass("java/lang/String");
687 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
688
689 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700691
692 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
693 // a pending exception
694 jmethodID method = env_->GetMethodID(jlobject, "foo", "()V");
695 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700697
698 // Check that java.lang.Object.equals() does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700699 method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
700 EXPECT_NE(static_cast<jmethodID>(NULL), method);
701 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700702
703 // Check that GetMethodID for java.lang.String.valueOf(int) fails as the
704 // method is static
705 method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
706 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700708}
709
710TEST_F(JniInternalTest, GetStaticMethodID) {
711 jclass jlobject = env_->FindClass("java/lang/Object");
712 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
713
714 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700716
717 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
718 // a pending exception
719 jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V");
720 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700722
723 // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as
724 // the method is not static
725 method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
726 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700728
729 // Check that java.lang.String.valueOf(int) does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700730 jclass jlstring = env_->FindClass("java/lang/String");
731 method = env_->GetStaticMethodID(jlstring, "valueOf",
732 "(I)Ljava/lang/String;");
733 EXPECT_NE(static_cast<jmethodID>(NULL), method);
734 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700735}
736
Elliott Hughescdf53122011-08-19 15:46:09 -0700737TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) {
738 jclass jlrField = env_->FindClass("java/lang/reflect/Field");
739 jclass c = env_->FindClass("java/lang/String");
740 ASSERT_TRUE(c != NULL);
741 jfieldID fid = env_->GetFieldID(c, "count", "I");
742 ASSERT_TRUE(fid != NULL);
743 // Turn the fid into a java.lang.reflect.Field...
744 jobject field = env_->ToReflectedField(c, fid, JNI_FALSE);
745 ASSERT_TRUE(c != NULL);
746 ASSERT_TRUE(env_->IsInstanceOf(field, jlrField));
747 // ...and back again.
748 jfieldID fid2 = env_->FromReflectedField(field);
749 ASSERT_TRUE(fid2 != NULL);
750}
751
752TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) {
753 jclass jlrMethod = env_->FindClass("java/lang/reflect/Method");
754 jclass c = env_->FindClass("java/lang/String");
755 ASSERT_TRUE(c != NULL);
756 jmethodID mid = env_->GetMethodID(c, "length", "()I");
757 ASSERT_TRUE(mid != NULL);
758 // Turn the mid into a java.lang.reflect.Method...
759 jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
760 ASSERT_TRUE(c != NULL);
761 ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod));
762 // ...and back again.
763 jmethodID mid2 = env_->FromReflectedMethod(method);
764 ASSERT_TRUE(mid2 != NULL);
765}
766
Elliott Hughes5174fe62011-08-23 15:12:35 -0700767void BogusMethod() {
768 // You can't pass NULL function pointers to RegisterNatives.
769}
770
Ian Rogers4dd71f12011-08-16 14:16:02 -0700771TEST_F(JniInternalTest, RegisterNatives) {
772 jclass jlobject = env_->FindClass("java/lang/Object");
773 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
774
775 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700777
778 // Check that registering to a non-existent java.lang.Object.foo() causes a
779 // NoSuchMethodError
780 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700781 JNINativeMethod methods[] = { { "foo", "()V", NULL } };
Ian Rogers4dd71f12011-08-16 14:16:02 -0700782 env_->RegisterNatives(jlobject, methods, 1);
783 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700785
786 // Check that registering non-native methods causes a NoSuchMethodError
787 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700788 JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", NULL } };
Ian Rogers4dd71f12011-08-16 14:16:02 -0700789 env_->RegisterNatives(jlobject, methods, 1);
790 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700792
793 // Check that registering native methods is successful
794 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700795 JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } };
Ian Rogers4dd71f12011-08-16 14:16:02 -0700796 env_->RegisterNatives(jlobject, methods, 1);
797 }
798 EXPECT_FALSE(env_->ExceptionCheck());
Elliott Hughes5174fe62011-08-23 15:12:35 -0700799
800 env_->UnregisterNatives(jlobject);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700801}
802
Elliott Hughes75770752011-08-24 17:52:38 -0700803#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 -0700804 jsize size = 4; \
805 /* Allocate an array and check it has the right type and length. */ \
806 scalar_type ## Array a = env_->new_fn(size); \
807 EXPECT_TRUE(a != NULL); \
808 EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \
809 EXPECT_EQ(size, env_->GetArrayLength(a)); \
810 /* AIOOBE for negative start offset. */ \
811 env_->get_region_fn(a, -1, 1, NULL); \
812 EXPECT_EXCEPTION(aioobe_); \
813 env_->set_region_fn(a, -1, 1, NULL); \
814 EXPECT_EXCEPTION(aioobe_); \
815 /* AIOOBE for negative length. */ \
816 env_->get_region_fn(a, 0, -1, NULL); \
817 EXPECT_EXCEPTION(aioobe_); \
818 env_->set_region_fn(a, 0, -1, NULL); \
819 EXPECT_EXCEPTION(aioobe_); \
820 /* AIOOBE for buffer overrun. */ \
821 env_->get_region_fn(a, size - 1, size, NULL); \
822 EXPECT_EXCEPTION(aioobe_); \
823 env_->set_region_fn(a, size - 1, size, NULL); \
824 EXPECT_EXCEPTION(aioobe_); \
825 /* Prepare a couple of buffers. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700826 UniquePtr<scalar_type[]> src_buf(new scalar_type[size]); \
827 UniquePtr<scalar_type[]> dst_buf(new scalar_type[size]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700828 for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \
829 for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \
830 /* Copy all of src_buf onto the heap. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700831 env_->set_region_fn(a, 0, size, &src_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700832 /* Copy back only part. */ \
833 env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700834 EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "short copy equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -0700835 /* Copy the missing pieces. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700836 env_->get_region_fn(a, 0, 1, &dst_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700837 env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700838 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 -0700839 /* Copy back the whole array. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700840 env_->get_region_fn(a, 0, size, &dst_buf[0]); \
841 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 -0700842 /* GetPrimitiveArrayCritical */ \
843 void* v = env_->GetPrimitiveArrayCritical(a, NULL); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700844 EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) << "GetPrimitiveArrayCritical not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700845 env_->ReleasePrimitiveArrayCritical(a, v, 0); \
846 /* GetXArrayElements */ \
847 scalar_type* xs = env_->get_elements_fn(a, NULL); \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700848 EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) << # get_elements_fn " not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700849 env_->release_elements_fn(a, xs, 0); \
850 EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs))
Elliott Hughesbd935992011-08-22 11:59:34 -0700851
Elliott Hughes814e4032011-08-23 12:07:56 -0700852TEST_F(JniInternalTest, BooleanArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700853 EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z");
Elliott Hughes814e4032011-08-23 12:07:56 -0700854}
855TEST_F(JniInternalTest, ByteArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700856 EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B");
Elliott Hughes814e4032011-08-23 12:07:56 -0700857}
858TEST_F(JniInternalTest, CharArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700859 EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C");
Elliott Hughes814e4032011-08-23 12:07:56 -0700860}
861TEST_F(JniInternalTest, DoubleArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700862 EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D");
Elliott Hughes814e4032011-08-23 12:07:56 -0700863}
864TEST_F(JniInternalTest, FloatArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700865 EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F");
Elliott Hughes814e4032011-08-23 12:07:56 -0700866}
867TEST_F(JniInternalTest, IntArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700868 EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I");
Elliott Hughes814e4032011-08-23 12:07:56 -0700869}
870TEST_F(JniInternalTest, LongArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700871 EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J");
Elliott Hughes814e4032011-08-23 12:07:56 -0700872}
873TEST_F(JniInternalTest, ShortArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700874 EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700875}
876
Elliott Hughesf2682d52011-08-15 16:37:04 -0700877TEST_F(JniInternalTest, NewObjectArray) {
878 // TODO: death tests for negative array sizes.
879
Elliott Hughesf2682d52011-08-15 16:37:04 -0700880 // TODO: check non-NULL initial elements.
881
Elliott Hughesbd935992011-08-22 11:59:34 -0700882 jclass element_class = env_->FindClass("java/lang/String");
883 ASSERT_TRUE(element_class != NULL);
884 jclass array_class = env_->FindClass("[Ljava/lang/String;");
885 ASSERT_TRUE(array_class != NULL);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700886
Elliott Hughesbd935992011-08-22 11:59:34 -0700887 jobjectArray a;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700888
Elliott Hughesbd935992011-08-22 11:59:34 -0700889 a = env_->NewObjectArray(0, element_class, NULL);
890 EXPECT_TRUE(a != NULL);
891 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
892 EXPECT_EQ(0, env_->GetArrayLength(a));
893
894 a = env_->NewObjectArray(1, element_class, NULL);
895 EXPECT_TRUE(a != NULL);
896 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
897 EXPECT_EQ(1, env_->GetArrayLength(a));
Elliott Hughes75770752011-08-24 17:52:38 -0700898 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), NULL));
899
900 jstring s = env_->NewStringUTF("poop");
901 a = env_->NewObjectArray(2, element_class, s);
902 EXPECT_TRUE(a != NULL);
903 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
904 EXPECT_EQ(2, env_->GetArrayLength(a));
905 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s));
906 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s));
Elliott Hughesbd935992011-08-22 11:59:34 -0700907}
908
909TEST_F(JniInternalTest, GetArrayLength) {
910 // Already tested in NewObjectArray/NewPrimitiveArray.
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700911}
912
Elliott Hughes37f7a402011-08-22 18:56:01 -0700913TEST_F(JniInternalTest, GetObjectClass) {
914 jclass string_class = env_->FindClass("java/lang/String");
915 ASSERT_TRUE(string_class != NULL);
916 jclass class_class = env_->FindClass("java/lang/Class");
917 ASSERT_TRUE(class_class != NULL);
918
919 jstring s = env_->NewStringUTF("poop");
920 jclass c = env_->GetObjectClass(s);
921 ASSERT_TRUE(env_->IsSameObject(string_class, c));
922
923 jclass c2 = env_->GetObjectClass(c);
924 ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2)));
925}
926
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700927TEST_F(JniInternalTest, GetSuperclass) {
928 jclass object_class = env_->FindClass("java/lang/Object");
929 ASSERT_TRUE(object_class != NULL);
930 jclass string_class = env_->FindClass("java/lang/String");
931 ASSERT_TRUE(string_class != NULL);
Ian Rogersdc180202012-01-29 14:47:29 -0800932 jclass runnable_interface = env_->FindClass("java/lang/Runnable");
933 ASSERT_TRUE(runnable_interface != NULL);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class)));
935 ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL);
Ian Rogersdc180202012-01-29 14:47:29 -0800936 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface)));
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700937}
938
Elliott Hughes37f7a402011-08-22 18:56:01 -0700939TEST_F(JniInternalTest, IsAssignableFrom) {
940 jclass object_class = env_->FindClass("java/lang/Object");
941 ASSERT_TRUE(object_class != NULL);
942 jclass string_class = env_->FindClass("java/lang/String");
943 ASSERT_TRUE(string_class != NULL);
944
945 ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class));
946 ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class));
947}
948
Elliott Hughesb465ab02011-08-24 11:21:21 -0700949TEST_F(JniInternalTest, GetObjectRefType) {
950 jclass local = env_->FindClass("java/lang/Object");
951 ASSERT_TRUE(local != NULL);
952 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local));
953
954 jobject global = env_->NewGlobalRef(local);
955 EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global));
956
957 jweak weak_global = env_->NewWeakGlobalRef(local);
958 EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global));
959
960 jobject invalid = reinterpret_cast<jobject>(this);
961 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid));
962
963 // TODO: invoke a native method and test that its arguments are considered local references.
964}
965
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700966TEST_F(JniInternalTest, NewStringUTF) {
967 EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL);
Elliott Hughes814e4032011-08-23 12:07:56 -0700968 jstring s;
969
970 s = env_->NewStringUTF("");
971 EXPECT_TRUE(s != NULL);
972 EXPECT_EQ(0, env_->GetStringLength(s));
973 EXPECT_EQ(0, env_->GetStringUTFLength(s));
974 s = env_->NewStringUTF("hello");
975 EXPECT_TRUE(s != NULL);
976 EXPECT_EQ(5, env_->GetStringLength(s));
977 EXPECT_EQ(5, env_->GetStringUTFLength(s));
978
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700979 // TODO: check some non-ASCII strings.
Elliott Hughesf2682d52011-08-15 16:37:04 -0700980}
981
Elliott Hughes814e4032011-08-23 12:07:56 -0700982TEST_F(JniInternalTest, NewString) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700983 jchar chars[] = { 'h', 'i' };
984 jstring s;
985 s = env_->NewString(chars, 0);
986 EXPECT_TRUE(s != NULL);
987 EXPECT_EQ(0, env_->GetStringLength(s));
988 EXPECT_EQ(0, env_->GetStringUTFLength(s));
989 s = env_->NewString(chars, 2);
990 EXPECT_TRUE(s != NULL);
991 EXPECT_EQ(2, env_->GetStringLength(s));
992 EXPECT_EQ(2, env_->GetStringUTFLength(s));
993
994 // TODO: check some non-ASCII strings.
995}
996
Jesse Wilson25e79a52011-11-18 15:31:58 -0500997TEST_F(JniInternalTest, NewStringNullCharsZeroLength) {
998 jstring s = env_->NewString(NULL, 0);
999 EXPECT_TRUE(s != NULL);
1000 EXPECT_EQ(0, env_->GetStringLength(s));
1001}
1002
Brian Carlstrom36258122011-12-09 12:55:51 -08001003// TODO: fix gtest death tests on host http://b/5690440 (and target)
1004TEST_F(JniInternalTest, DISABLED_NewStringNullCharsNonzeroLength) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001005 ASSERT_DEATH(env_->NewString(NULL, 1), "");
1006}
1007
Elliott Hughesb465ab02011-08-24 11:21:21 -07001008TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
1009 // Already tested in the NewString/NewStringUTF tests.
1010}
1011
1012TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) {
1013 jstring s = env_->NewStringUTF("hello");
1014 ASSERT_TRUE(s != NULL);
1015
1016 env_->GetStringRegion(s, -1, 0, NULL);
1017 EXPECT_EXCEPTION(sioobe_);
1018 env_->GetStringRegion(s, 0, -1, NULL);
1019 EXPECT_EXCEPTION(sioobe_);
1020 env_->GetStringRegion(s, 0, 10, NULL);
1021 EXPECT_EXCEPTION(sioobe_);
1022 env_->GetStringRegion(s, 10, 1, NULL);
1023 EXPECT_EXCEPTION(sioobe_);
1024
1025 jchar chars[4] = { 'x', 'x', 'x', 'x' };
1026 env_->GetStringRegion(s, 1, 2, &chars[1]);
1027 EXPECT_EQ('x', chars[0]);
1028 EXPECT_EQ('e', chars[1]);
1029 EXPECT_EQ('l', chars[2]);
1030 EXPECT_EQ('x', chars[3]);
1031
1032 env_->GetStringUTFRegion(s, -1, 0, NULL);
1033 EXPECT_EXCEPTION(sioobe_);
1034 env_->GetStringUTFRegion(s, 0, -1, NULL);
1035 EXPECT_EXCEPTION(sioobe_);
1036 env_->GetStringUTFRegion(s, 0, 10, NULL);
1037 EXPECT_EXCEPTION(sioobe_);
1038 env_->GetStringUTFRegion(s, 10, 1, NULL);
1039 EXPECT_EXCEPTION(sioobe_);
1040
1041 char bytes[4] = { 'x', 'x', 'x', 'x' };
1042 env_->GetStringUTFRegion(s, 1, 2, &bytes[1]);
1043 EXPECT_EQ('x', bytes[0]);
1044 EXPECT_EQ('e', bytes[1]);
1045 EXPECT_EQ('l', bytes[2]);
1046 EXPECT_EQ('x', bytes[3]);
1047}
1048
Elliott Hughes75770752011-08-24 17:52:38 -07001049TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001050 // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni.
Elliott Hughesb264f082012-04-06 17:10:10 -07001051 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001052 CheckJniAbortCatcher check_jni_abort_catcher;
1053 EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL);
Elliott Hughes56ef0422012-06-19 14:35:04 -07001054 check_jni_abort_catcher.Check("GetStringUTFChars received null jstring");
Elliott Hughesb264f082012-04-06 17:10:10 -07001055 }
Elliott Hughes75770752011-08-24 17:52:38 -07001056
1057 jstring s = env_->NewStringUTF("hello");
1058 ASSERT_TRUE(s != NULL);
1059
1060 const char* utf = env_->GetStringUTFChars(s, NULL);
1061 EXPECT_STREQ("hello", utf);
1062 env_->ReleaseStringUTFChars(s, utf);
1063
1064 jboolean is_copy = JNI_FALSE;
1065 utf = env_->GetStringUTFChars(s, &is_copy);
1066 EXPECT_EQ(JNI_TRUE, is_copy);
1067 EXPECT_STREQ("hello", utf);
1068 env_->ReleaseStringUTFChars(s, utf);
1069}
1070
1071TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) {
1072 jstring s = env_->NewStringUTF("hello");
1073 ASSERT_TRUE(s != NULL);
1074
1075 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
1076 const jchar* chars = env_->GetStringChars(s, NULL);
1077 EXPECT_EQ(expected[0], chars[0]);
1078 EXPECT_EQ(expected[1], chars[1]);
1079 EXPECT_EQ(expected[2], chars[2]);
1080 EXPECT_EQ(expected[3], chars[3]);
1081 EXPECT_EQ(expected[4], chars[4]);
1082 env_->ReleaseStringChars(s, chars);
1083
1084 jboolean is_copy = JNI_FALSE;
1085 chars = env_->GetStringChars(s, &is_copy);
1086 EXPECT_EQ(JNI_FALSE, is_copy);
1087 EXPECT_EQ(expected[0], chars[0]);
1088 EXPECT_EQ(expected[1], chars[1]);
1089 EXPECT_EQ(expected[2], chars[2]);
1090 EXPECT_EQ(expected[3], chars[3]);
1091 EXPECT_EQ(expected[4], chars[4]);
1092 env_->ReleaseStringChars(s, chars);
1093}
1094
1095TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) {
1096 jstring s = env_->NewStringUTF("hello");
1097 ASSERT_TRUE(s != NULL);
1098
1099 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
1100 const jchar* chars = env_->GetStringCritical(s, NULL);
1101 EXPECT_EQ(expected[0], chars[0]);
1102 EXPECT_EQ(expected[1], chars[1]);
1103 EXPECT_EQ(expected[2], chars[2]);
1104 EXPECT_EQ(expected[3], chars[3]);
1105 EXPECT_EQ(expected[4], chars[4]);
1106 env_->ReleaseStringCritical(s, chars);
1107
1108 jboolean is_copy = JNI_FALSE;
1109 chars = env_->GetStringCritical(s, &is_copy);
1110 EXPECT_EQ(JNI_FALSE, is_copy);
1111 EXPECT_EQ(expected[0], chars[0]);
1112 EXPECT_EQ(expected[1], chars[1]);
1113 EXPECT_EQ(expected[2], chars[2]);
1114 EXPECT_EQ(expected[3], chars[3]);
1115 EXPECT_EQ(expected[4], chars[4]);
1116 env_->ReleaseStringCritical(s, chars);
1117}
1118
Elliott Hughes814e4032011-08-23 12:07:56 -07001119TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) {
Elliott Hughesb264f082012-04-06 17:10:10 -07001120 jclass java_lang_Class = env_->FindClass("java/lang/Class");
1121 ASSERT_TRUE(java_lang_Class != NULL);
Elliott Hughes289da822011-08-16 10:11:20 -07001122
Elliott Hughesb264f082012-04-06 17:10:10 -07001123 jobjectArray array = env_->NewObjectArray(1, java_lang_Class, NULL);
Elliott Hughes289da822011-08-16 10:11:20 -07001124 EXPECT_TRUE(array != NULL);
Elliott Hughes814e4032011-08-23 12:07:56 -07001125 EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL);
Elliott Hughesb264f082012-04-06 17:10:10 -07001126 env_->SetObjectArrayElement(array, 0, java_lang_Class);
1127 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class));
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001128
1129 // ArrayIndexOutOfBounds for negative index.
Elliott Hughesb264f082012-04-06 17:10:10 -07001130 env_->SetObjectArrayElement(array, -1, java_lang_Class);
Elliott Hughes814e4032011-08-23 12:07:56 -07001131 EXPECT_EXCEPTION(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001132
1133 // ArrayIndexOutOfBounds for too-large index.
Elliott Hughesb264f082012-04-06 17:10:10 -07001134 env_->SetObjectArrayElement(array, 1, java_lang_Class);
Elliott Hughes814e4032011-08-23 12:07:56 -07001135 EXPECT_EXCEPTION(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001136
Elliott Hughesb264f082012-04-06 17:10:10 -07001137 // ArrayStoreException thrown for bad types.
1138 env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!"));
1139 EXPECT_EXCEPTION(ase_);
Elliott Hughes289da822011-08-16 10:11:20 -07001140}
1141
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001142#define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \
1143 do { \
1144 jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \
1145 EXPECT_TRUE(fid != NULL); \
1146 env_->SetStatic ## type ## Field(c, fid, value1); \
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001147 EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001148 env_->SetStatic ## type ## Field(c, fid, value2); \
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001149 EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001150 } while (false)
1151
1152#define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \
1153 do { \
1154 jfieldID fid = env_->GetFieldID(c, field_name, sig); \
1155 EXPECT_TRUE(fid != NULL); \
1156 env_->Set ## type ## Field(instance, fid, value1); \
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001157 EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001158 env_->Set ## type ## Field(instance, fid, value2); \
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08001159 EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001160 } while (false)
1161
1162
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -08001163#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001164TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 Thread::Current()->TransitionFromSuspendedToRunnable();
1166 LoadDex("AllFields");
Brian Carlstrom25c33252011-09-18 15:58:35 -07001167 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001168
1169 jclass c = env_->FindClass("AllFields");
1170 ASSERT_TRUE(c != NULL);
1171 jobject o = env_->AllocObject(c);
1172 ASSERT_TRUE(o != NULL);
1173
1174 EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false);
1175 EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2);
1176 EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b');
1177 EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0);
1178 EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0);
1179 EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2);
1180 EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2);
1181 EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2);
1182
1183 EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false);
1184 EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2);
1185 EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b');
1186 EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0);
1187 EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0);
1188 EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2);
1189 EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2);
1190 EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2);
1191}
1192
1193TEST_F(JniInternalTest, GetObjectField_SetObjectField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 Thread::Current()->TransitionFromSuspendedToRunnable();
1195 LoadDex("AllFields");
Brian Carlstrom25c33252011-09-18 15:58:35 -07001196 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001197
1198 jclass c = env_->FindClass("AllFields");
1199 ASSERT_TRUE(c != NULL);
1200 jobject o = env_->AllocObject(c);
1201 ASSERT_TRUE(o != NULL);
1202
1203 jstring s1 = env_->NewStringUTF("hello");
1204 ASSERT_TRUE(s1 != NULL);
1205 jstring s2 = env_->NewStringUTF("world");
1206 ASSERT_TRUE(s2 != NULL);
1207
1208 jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;");
1209 ASSERT_TRUE(s_fid != NULL);
1210 jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;");
1211 ASSERT_TRUE(i_fid != NULL);
1212
1213 env_->SetStaticObjectField(c, s_fid, s1);
1214 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid)));
1215 env_->SetStaticObjectField(c, s_fid, s2);
1216 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid)));
1217
1218 env_->SetObjectField(o, i_fid, s1);
1219 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid)));
1220 env_->SetObjectField(o, i_fid, s2);
1221 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid)));
1222}
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -08001223#endif
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001224
Elliott Hughes18c07532011-08-18 15:50:51 -07001225TEST_F(JniInternalTest, NewLocalRef_NULL) {
1226 EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL);
1227}
1228
1229TEST_F(JniInternalTest, NewLocalRef) {
1230 jstring s = env_->NewStringUTF("");
1231 ASSERT_TRUE(s != NULL);
1232 jobject o = env_->NewLocalRef(s);
1233 EXPECT_TRUE(o != NULL);
1234 EXPECT_TRUE(o != s);
1235
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001236 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o));
Elliott Hughes18c07532011-08-18 15:50:51 -07001237}
1238
1239TEST_F(JniInternalTest, DeleteLocalRef_NULL) {
1240 env_->DeleteLocalRef(NULL);
1241}
1242
1243TEST_F(JniInternalTest, DeleteLocalRef) {
1244 jstring s = env_->NewStringUTF("");
1245 ASSERT_TRUE(s != NULL);
1246 env_->DeleteLocalRef(s);
1247
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001248 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001249 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001250 CheckJniAbortCatcher check_jni_abort_catcher;
1251 env_->DeleteLocalRef(s);
Ian Rogers120f1c72012-09-28 17:17:10 -07001252 check_jni_abort_catcher.Check("native code passing in reference to invalid local reference: 0x1500001");
Elliott Hughesb264f082012-04-06 17:10:10 -07001253 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001254
1255 s = env_->NewStringUTF("");
1256 ASSERT_TRUE(s != NULL);
1257 jobject o = env_->NewLocalRef(s);
1258 ASSERT_TRUE(o != NULL);
1259
1260 env_->DeleteLocalRef(s);
1261 env_->DeleteLocalRef(o);
1262}
1263
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001264TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) {
1265 jobject original = env_->NewStringUTF("");
1266 ASSERT_TRUE(original != NULL);
1267
1268 jobject outer;
1269 jobject inner1, inner2;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env_);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001271 Object* inner2_direct_pointer;
1272 {
1273 env_->PushLocalFrame(4);
1274 outer = env_->NewLocalRef(original);
1275
1276 {
1277 env_->PushLocalFrame(4);
1278 inner1 = env_->NewLocalRef(outer);
1279 inner2 = env_->NewStringUTF("survivor");
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 inner2_direct_pointer = soa.Decode<Object*>(inner2);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001281 env_->PopLocalFrame(inner2);
1282 }
1283
1284 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
1285 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer));
1286 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
1287
1288 // Our local reference for the survivor is invalid because the survivor
1289 // gets a new local reference...
1290 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
1291 // ...but the survivor should be in the local reference table.
Brian Carlstrom4d571432012-05-16 00:21:41 -07001292 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(env_);
1293 EXPECT_TRUE(env->locals.ContainsDirectPointer(inner2_direct_pointer));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001294
1295 env_->PopLocalFrame(NULL);
1296 }
1297 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
1298 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer));
1299 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
1300 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
1301}
1302
Elliott Hughes18c07532011-08-18 15:50:51 -07001303TEST_F(JniInternalTest, NewGlobalRef_NULL) {
1304 EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL);
1305}
1306
1307TEST_F(JniInternalTest, NewGlobalRef) {
1308 jstring s = env_->NewStringUTF("");
1309 ASSERT_TRUE(s != NULL);
1310 jobject o = env_->NewGlobalRef(s);
1311 EXPECT_TRUE(o != NULL);
1312 EXPECT_TRUE(o != s);
1313
1314 // TODO: check that o is a global reference.
1315}
1316
1317TEST_F(JniInternalTest, DeleteGlobalRef_NULL) {
1318 env_->DeleteGlobalRef(NULL);
1319}
1320
1321TEST_F(JniInternalTest, DeleteGlobalRef) {
1322 jstring s = env_->NewStringUTF("");
1323 ASSERT_TRUE(s != NULL);
1324
1325 jobject o = env_->NewGlobalRef(s);
1326 ASSERT_TRUE(o != NULL);
1327 env_->DeleteGlobalRef(o);
1328
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001329 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001330 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001331 CheckJniAbortCatcher check_jni_abort_catcher;
1332 env_->DeleteGlobalRef(o);
Ian Rogers120f1c72012-09-28 17:17:10 -07001333 check_jni_abort_catcher.Check("native code passing in reference to invalid global reference: 0x10005a");
Elliott Hughesb264f082012-04-06 17:10:10 -07001334 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001335
1336 jobject o1 = env_->NewGlobalRef(s);
1337 ASSERT_TRUE(o1 != NULL);
1338 jobject o2 = env_->NewGlobalRef(s);
1339 ASSERT_TRUE(o2 != NULL);
1340
1341 env_->DeleteGlobalRef(o1);
1342 env_->DeleteGlobalRef(o2);
1343}
1344
1345TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) {
1346 EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL);
1347}
1348
1349TEST_F(JniInternalTest, NewWeakGlobalRef) {
1350 jstring s = env_->NewStringUTF("");
1351 ASSERT_TRUE(s != NULL);
1352 jobject o = env_->NewWeakGlobalRef(s);
1353 EXPECT_TRUE(o != NULL);
1354 EXPECT_TRUE(o != s);
1355
1356 // TODO: check that o is a weak global reference.
1357}
1358
1359TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) {
1360 env_->DeleteWeakGlobalRef(NULL);
1361}
1362
1363TEST_F(JniInternalTest, DeleteWeakGlobalRef) {
1364 jstring s = env_->NewStringUTF("");
1365 ASSERT_TRUE(s != NULL);
1366
1367 jobject o = env_->NewWeakGlobalRef(s);
1368 ASSERT_TRUE(o != NULL);
1369 env_->DeleteWeakGlobalRef(o);
1370
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001371 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001372 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001373 CheckJniAbortCatcher check_jni_abort_catcher;
1374 env_->DeleteWeakGlobalRef(o);
Elliott Hughes56ef0422012-06-19 14:35:04 -07001375 check_jni_abort_catcher.Check("native code passing in reference to invalid weak global reference: 0x100003");
Elliott Hughesb264f082012-04-06 17:10:10 -07001376 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001377
1378 jobject o1 = env_->NewWeakGlobalRef(s);
1379 ASSERT_TRUE(o1 != NULL);
1380 jobject o2 = env_->NewWeakGlobalRef(s);
1381 ASSERT_TRUE(o2 != NULL);
1382
1383 env_->DeleteWeakGlobalRef(o1);
1384 env_->DeleteWeakGlobalRef(o2);
1385}
1386
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001387TEST_F(JniInternalTest, StaticMainMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001388 ScopedObjectAccess soa(Thread::Current());
1389 jobject jclass_loader = LoadDex("Main");
Ian Rogers1f539342012-10-03 21:09:42 -07001390 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001391 CompileDirectMethod(class_loader.get(), "Main", "main", "([Ljava/lang/String;)V");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001392
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001393 Class* klass = class_linker_->FindClass("LMain;", class_loader.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001394 ASSERT_TRUE(klass != NULL);
1395
Mathieu Chartier66f19252012-09-18 08:57:04 -07001396 AbstractMethod* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001397 ASSERT_TRUE(method != NULL);
1398
Mathieu Chartier66f19252012-09-18 08:57:04 -07001399 AbstractMethod::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001400
Elliott Hughes77405792012-03-15 15:22:12 -07001401 JValue args[1];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001402 args[0].SetL(NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001403
Elliott Hughes77405792012-03-15 15:22:12 -07001404 (*stub)(method, NULL, Thread::Current(), args, NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001405}
1406
1407TEST_F(JniInternalTest, StaticNopMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001409 InvokeNopMethod(true);
1410}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001411
Elliott Hughes77405792012-03-15 15:22:12 -07001412TEST_F(JniInternalTest, NonStaticNopMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001414 InvokeNopMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001415}
1416
1417TEST_F(JniInternalTest, StaticIdentityByteMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001418 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001419 InvokeIdentityByteMethod(true);
1420}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001421
Elliott Hughes77405792012-03-15 15:22:12 -07001422TEST_F(JniInternalTest, NonStaticIdentityByteMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001423 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001424 InvokeIdentityByteMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001425}
1426
1427TEST_F(JniInternalTest, StaticIdentityIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001429 InvokeIdentityIntMethod(true);
1430}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001431
Elliott Hughes77405792012-03-15 15:22:12 -07001432TEST_F(JniInternalTest, NonStaticIdentityIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001433 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001434 InvokeIdentityIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001435}
1436
1437TEST_F(JniInternalTest, StaticIdentityDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001439 InvokeIdentityDoubleMethod(true);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001440}
1441
Elliott Hughes77405792012-03-15 15:22:12 -07001442TEST_F(JniInternalTest, NonStaticIdentityDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001443 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001444 InvokeIdentityDoubleMethod(false);
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -08001445}
1446
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001447TEST_F(JniInternalTest, StaticSumIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001449 InvokeSumIntIntMethod(true);
1450}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001451
Elliott Hughes77405792012-03-15 15:22:12 -07001452TEST_F(JniInternalTest, NonStaticSumIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001454 InvokeSumIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001455}
1456
1457TEST_F(JniInternalTest, StaticSumIntIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001458 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001459 InvokeSumIntIntIntMethod(true);
1460}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001461
Elliott Hughes77405792012-03-15 15:22:12 -07001462TEST_F(JniInternalTest, NonStaticSumIntIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001463 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001464 InvokeSumIntIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001465}
1466
1467TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001468 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001469 InvokeSumIntIntIntIntMethod(true);
1470}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001471
Elliott Hughes77405792012-03-15 15:22:12 -07001472TEST_F(JniInternalTest, NonStaticSumIntIntIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001474 InvokeSumIntIntIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001475}
1476
1477TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001478 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001479 InvokeSumIntIntIntIntIntMethod(true);
1480}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001481
Elliott Hughes77405792012-03-15 15:22:12 -07001482TEST_F(JniInternalTest, NonStaticSumIntIntIntIntIntMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001483 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001484 InvokeSumIntIntIntIntIntMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001485}
1486
1487TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001488 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001489 InvokeSumDoubleDoubleMethod(true);
1490}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001491
Elliott Hughes77405792012-03-15 15:22:12 -07001492TEST_F(JniInternalTest, NonStaticSumDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001493 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001494 InvokeSumDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001495}
1496
1497TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001499 InvokeSumDoubleDoubleDoubleMethod(true);
1500}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001501
Elliott Hughes77405792012-03-15 15:22:12 -07001502TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001503 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001504 InvokeSumDoubleDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001505}
1506
1507TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001509 InvokeSumDoubleDoubleDoubleDoubleMethod(true);
1510}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001511
Elliott Hughes77405792012-03-15 15:22:12 -07001512TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001513 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001514 InvokeSumDoubleDoubleDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001515}
1516
1517TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001519 InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(true);
1520}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001521
Elliott Hughes77405792012-03-15 15:22:12 -07001522TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001523 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001524 InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001525}
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001526
Elliott Hughes37f7a402011-08-22 18:56:01 -07001527TEST_F(JniInternalTest, Throw) {
1528 EXPECT_EQ(JNI_ERR, env_->Throw(NULL));
1529
1530 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
1531 ASSERT_TRUE(exception_class != NULL);
1532 jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class));
1533 ASSERT_TRUE(exception != NULL);
1534
1535 EXPECT_EQ(JNI_OK, env_->Throw(exception));
1536 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughesa2501992011-08-26 19:39:54 -07001537 jthrowable thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001538 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001539 EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001540}
1541
1542TEST_F(JniInternalTest, ThrowNew) {
1543 EXPECT_EQ(JNI_ERR, env_->Throw(NULL));
1544
1545 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
1546 ASSERT_TRUE(exception_class != NULL);
1547
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001548 jthrowable thrown_exception;
1549
Elliott Hughes37f7a402011-08-22 18:56:01 -07001550 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world"));
1551 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001552 thrown_exception = env_->ExceptionOccurred();
1553 env_->ExceptionClear();
1554 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
1555
1556 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL));
1557 EXPECT_TRUE(env_->ExceptionCheck());
1558 thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001559 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001560 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001561}
1562
Elliott Hughesb465ab02011-08-24 11:21:21 -07001563// TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>.
1564TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) {
1565 jclass buffer_class = env_->FindClass("java/nio/Buffer");
1566 ASSERT_TRUE(buffer_class != NULL);
1567
1568 char bytes[1024];
1569 jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes));
1570 ASSERT_TRUE(buffer != NULL);
1571 ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class));
1572 ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes);
1573 ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes));
1574}
1575
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001576TEST_F(JniInternalTest, MonitorEnterExit) {
1577 // Create an object to torture
1578 jclass object_class = env_->FindClass("java/lang/Object");
1579 ASSERT_TRUE(object_class != NULL);
1580 jobject object = env_->AllocObject(object_class);
1581 ASSERT_TRUE(object != NULL);
1582
1583 // Expected class of exceptions
1584 jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException");
1585 ASSERT_TRUE(imse_class != NULL);
1586
1587 jthrowable thrown_exception;
1588
1589 // Unlock of unowned monitor
1590 env_->MonitorExit(object);
1591 EXPECT_TRUE(env_->ExceptionCheck());
1592 thrown_exception = env_->ExceptionOccurred();
1593 env_->ExceptionClear();
1594 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
1595
1596 // Lock of unowned monitor
1597 env_->MonitorEnter(object);
1598 EXPECT_FALSE(env_->ExceptionCheck());
1599 // Regular unlock
1600 env_->MonitorExit(object);
1601 EXPECT_FALSE(env_->ExceptionCheck());
1602
1603 // Recursively lock a lot
1604 size_t max_recursive_lock = 1024;
1605 for (size_t i = 0; i < max_recursive_lock; i++) {
1606 env_->MonitorEnter(object);
1607 EXPECT_FALSE(env_->ExceptionCheck());
1608 }
1609 // Recursively unlock a lot
1610 for (size_t i = 0; i < max_recursive_lock; i++) {
1611 env_->MonitorExit(object);
1612 EXPECT_FALSE(env_->ExceptionCheck());
1613 }
1614
1615 // Unlock of unowned monitor
1616 env_->MonitorExit(object);
1617 EXPECT_TRUE(env_->ExceptionCheck());
1618 thrown_exception = env_->ExceptionOccurred();
1619 env_->ExceptionClear();
1620 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
Elliott Hughesa92853e2012-02-07 16:09:27 -08001621
1622 // It's an error to call MonitorEnter or MonitorExit on NULL.
Elliott Hughesb264f082012-04-06 17:10:10 -07001623 {
1624 CheckJniAbortCatcher check_jni_abort_catcher;
1625 env_->MonitorEnter(NULL);
1626 check_jni_abort_catcher.Check("in call to MonitorEnter");
Elliott Hughesb264f082012-04-06 17:10:10 -07001627
Elliott Hughesb264f082012-04-06 17:10:10 -07001628 env_->MonitorExit(NULL);
1629 check_jni_abort_catcher.Check("in call to MonitorExit");
1630 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001631}
1632
Brian Carlstrom4d571432012-05-16 00:21:41 -07001633TEST_F(JniInternalTest, DetachCurrentThread) {
1634 CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv
1635 jint ok = vm_->DetachCurrentThread();
1636 EXPECT_EQ(JNI_OK, ok);
1637
1638 jint err = vm_->DetachCurrentThread();
1639 EXPECT_EQ(JNI_ERR, err);
1640 vm_->AttachCurrentThread(&env_, NULL); // need attached thread for CommonTest::TearDown
1641}
1642
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001643} // namespace art