blob: 5516eab4f8541cfe58f350d22d60d86116777154 [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include "common_compiler_test.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070020#include "java_vm_ext.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "mirror/art_method-inl.h"
Fred Shih56890e22014-06-02 11:11:52 -070022#include "mirror/string-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "scoped_thread_state_change.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070024#include "ScopedLocalRef.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -070025
26namespace art {
27
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028// TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used.
29class JniInternalTest : public CommonCompilerTest {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070030 protected:
31 virtual void SetUp() {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032 CommonCompilerTest::SetUp();
Elliott Hughes5174fe62011-08-23 15:12:35 -070033
Elliott Hughesa2501992011-08-26 19:39:54 -070034 vm_ = Runtime::Current()->GetJavaVM();
35
Elliott Hughes5174fe62011-08-23 15:12:35 -070036 // Turn on -verbose:jni for the JNI tests.
Ian Rogers79713632013-08-21 19:06:15 -070037 // gLogVerbosity.jni = true;
Elliott Hughes5174fe62011-08-23 15:12:35 -070038
Ian Rogers2d10b202014-05-12 19:15:18 -070039 vm_->AttachCurrentThread(&env_, nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -070040
Brian Carlstromea46f952013-07-30 01:26:50 -070041 ScopedLocalRef<jclass> aioobe(env_,
42 env_->FindClass("java/lang/ArrayIndexOutOfBoundsException"));
Ian Rogers2d10b202014-05-12 19:15:18 -070043 CHECK(aioobe.get() != nullptr);
Elliott Hughes726079d2011-10-07 18:43:44 -070044 aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get()));
Elliott Hughesb465ab02011-08-24 11:21:21 -070045
Elliott Hughesb264f082012-04-06 17:10:10 -070046 ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException"));
Ian Rogers2d10b202014-05-12 19:15:18 -070047 CHECK(ase.get() != nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -070048 ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get()));
49
Brian Carlstromea46f952013-07-30 01:26:50 -070050 ScopedLocalRef<jclass> sioobe(env_,
51 env_->FindClass("java/lang/StringIndexOutOfBoundsException"));
Ian Rogers2d10b202014-05-12 19:15:18 -070052 CHECK(sioobe.get() != nullptr);
Elliott Hughes726079d2011-10-07 18:43:44 -070053 sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get()));
54 }
55
Ian Rogers2d10b202014-05-12 19:15:18 -070056 void ExpectException(jclass exception_class) {
Ian Rogers68d8b422014-07-17 11:09:10 -070057 ScopedObjectAccess soa(env_);
58 EXPECT_TRUE(env_->ExceptionCheck())
59 << PrettyDescriptor(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers2d10b202014-05-12 19:15:18 -070060 jthrowable exception = env_->ExceptionOccurred();
61 EXPECT_NE(nullptr, exception);
62 env_->ExceptionClear();
63 EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class));
64 }
65
Brian Carlstrom4d571432012-05-16 00:21:41 -070066 void CleanUpJniEnv() {
Ian Rogers2d10b202014-05-12 19:15:18 -070067 if (aioobe_ != nullptr) {
Brian Carlstrom4d571432012-05-16 00:21:41 -070068 env_->DeleteGlobalRef(aioobe_);
Ian Rogers2d10b202014-05-12 19:15:18 -070069 aioobe_ = nullptr;
Brian Carlstrom4d571432012-05-16 00:21:41 -070070 }
Ian Rogers2d10b202014-05-12 19:15:18 -070071 if (ase_ != nullptr) {
Brian Carlstrom4d571432012-05-16 00:21:41 -070072 env_->DeleteGlobalRef(ase_);
Ian Rogers2d10b202014-05-12 19:15:18 -070073 ase_ = nullptr;
Brian Carlstrom4d571432012-05-16 00:21:41 -070074 }
Ian Rogers2d10b202014-05-12 19:15:18 -070075 if (sioobe_ != nullptr) {
Brian Carlstrom4d571432012-05-16 00:21:41 -070076 env_->DeleteGlobalRef(sioobe_);
Ian Rogers2d10b202014-05-12 19:15:18 -070077 sioobe_ = nullptr;
Brian Carlstrom4d571432012-05-16 00:21:41 -070078 }
79 }
80
Ian Rogers53b8b092014-03-13 23:45:53 -070081 virtual void TearDown() OVERRIDE {
Brian Carlstrom4d571432012-05-16 00:21:41 -070082 CleanUpJniEnv();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080083 CommonCompilerTest::TearDown();
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070084 }
Elliott Hughesb465ab02011-08-24 11:21:21 -070085
Ian Rogers1d99e452014-01-02 17:36:41 -080086 jclass GetPrimitiveClass(char descriptor) {
87 ScopedObjectAccess soa(env_);
88 mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor);
89 CHECK(c != nullptr);
90 return soa.AddLocalReference<jclass>(c);
91 }
92
Ian Rogers68d8b422014-07-17 11:09:10 -070093 void ExpectClassFound(const char* name) {
94 EXPECT_NE(env_->FindClass(name), nullptr) << name;
95 EXPECT_FALSE(env_->ExceptionCheck()) << name;
96 }
97
98 void ExpectClassNotFound(const char* name, bool check_jni, const char* check_jni_msg,
99 CheckJniAbortCatcher* abort_catcher) {
100 EXPECT_EQ(env_->FindClass(name), nullptr) << name;
101 if (!check_jni || check_jni_msg == nullptr) {
102 EXPECT_TRUE(env_->ExceptionCheck()) << name;
103 env_->ExceptionClear();
104 } else {
105 abort_catcher->Check(check_jni_msg);
106 }
107 }
108
109 void FindClassTest(bool check_jni) {
110 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
111 CheckJniAbortCatcher check_jni_abort_catcher;
112
113 // Null argument is always an abort.
114 env_->FindClass(nullptr);
115 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
116 : "name == null");
117
118 // Reference types...
119 ExpectClassFound("java/lang/String");
120 // ...for arrays too, where you must include "L;".
121 ExpectClassFound("[Ljava/lang/String;");
122 // Primitive arrays are okay too, if the primitive type is valid.
123 ExpectClassFound("[C");
124
125 // But primitive types aren't allowed...
126 ExpectClassNotFound("C", check_jni, nullptr, &check_jni_abort_catcher);
127 ExpectClassNotFound("V", check_jni, nullptr, &check_jni_abort_catcher);
128 ExpectClassNotFound("K", check_jni, nullptr, &check_jni_abort_catcher);
129
130 if (check_jni) {
131 // Check JNI will reject invalid class names as aborts but without pending exceptions.
132 EXPECT_EQ(env_->FindClass("java.lang.String"), nullptr);
133 EXPECT_FALSE(env_->ExceptionCheck());
134 check_jni_abort_catcher.Check("illegal class name 'java.lang.String'");
135
136 EXPECT_EQ(env_->FindClass("[Ljava.lang.String;"), nullptr);
137 EXPECT_FALSE(env_->ExceptionCheck());
138 check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'");
139 } else {
140 // Without check JNI we're tolerant and replace '.' with '/'.
141 ExpectClassFound("java.lang.String");
142 ExpectClassFound("[Ljava.lang.String;");
143 }
144
145 ExpectClassNotFound("Ljava.lang.String;", check_jni, "illegal class name 'Ljava.lang.String;'",
146 &check_jni_abort_catcher);
147 ExpectClassNotFound("[java.lang.String", check_jni, "illegal class name '[java.lang.String'",
148 &check_jni_abort_catcher);
149
150 // You can't include the "L;" in a JNI class descriptor.
151 ExpectClassNotFound("Ljava/lang/String;", check_jni, "illegal class name 'Ljava/lang/String;'",
152 &check_jni_abort_catcher);
153
154 // But you must include it for an array of any reference type.
155 ExpectClassNotFound("[java/lang/String", check_jni, "illegal class name '[java/lang/String'",
156 &check_jni_abort_catcher);
157
158 ExpectClassNotFound("[K", check_jni, "illegal class name '[K'", &check_jni_abort_catcher);
159
160 // Void arrays aren't allowed.
161 ExpectClassNotFound("[V", check_jni, "illegal class name '[V'", &check_jni_abort_catcher);
162
163 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
164 }
165
166 void GetFieldIdBadArgumentTest(bool check_jni) {
167 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
168 CheckJniAbortCatcher check_jni_abort_catcher;
169
170 jclass c = env_->FindClass("java/lang/String");
171 ASSERT_NE(c, nullptr);
172
173 jfieldID fid = env_->GetFieldID(nullptr, "count", "I");
174 EXPECT_EQ(nullptr, fid);
175 check_jni_abort_catcher.Check(check_jni ? "GetFieldID received NULL jclass"
176 : "java_class == null");
177 fid = env_->GetFieldID(c, nullptr, "I");
178 EXPECT_EQ(nullptr, fid);
179 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
180 : "name == null");
181 fid = env_->GetFieldID(c, "count", nullptr);
182 EXPECT_EQ(nullptr, fid);
183 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
184 : "sig == null");
185
186 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
187 }
188
189 void GetStaticFieldIdBadArgumentTest(bool check_jni) {
190 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
191 CheckJniAbortCatcher check_jni_abort_catcher;
192
193 jclass c = env_->FindClass("java/lang/String");
194 ASSERT_NE(c, nullptr);
195
196 jfieldID fid = env_->GetStaticFieldID(nullptr, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
197 EXPECT_EQ(nullptr, fid);
198 check_jni_abort_catcher.Check(check_jni ? "GetStaticFieldID received NULL jclass"
199 : "java_class == null");
200 fid = env_->GetStaticFieldID(c, nullptr, "Ljava/util/Comparator;");
201 EXPECT_EQ(nullptr, fid);
202 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
203 : "name == null");
204 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", nullptr);
205 EXPECT_EQ(nullptr, fid);
206 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
207 : "sig == null");
208
209 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
210 }
211
212 void GetMethodIdBadArgumentTest(bool check_jni) {
213 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
214 CheckJniAbortCatcher check_jni_abort_catcher;
215
216 jmethodID method = env_->GetMethodID(nullptr, "<init>", "(Ljava/lang/String;)V");
217 EXPECT_EQ(nullptr, method);
218 check_jni_abort_catcher.Check(check_jni ? "GetMethodID received NULL jclass"
219 : "java_class == null");
220 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
221 ASSERT_TRUE(jlnsme != nullptr);
222 method = env_->GetMethodID(jlnsme, nullptr, "(Ljava/lang/String;)V");
223 EXPECT_EQ(nullptr, method);
224 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
225 : "name == null");
226 method = env_->GetMethodID(jlnsme, "<init>", nullptr);
227 EXPECT_EQ(nullptr, method);
228 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
229 : "sig == null");
230
231 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
232 }
233
234 void GetStaticMethodIdBadArgumentTest(bool check_jni) {
235 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
236 CheckJniAbortCatcher check_jni_abort_catcher;
237
238 jmethodID method = env_->GetStaticMethodID(nullptr, "valueOf", "(I)Ljava/lang/String;");
239 EXPECT_EQ(nullptr, method);
240 check_jni_abort_catcher.Check(check_jni ? "GetStaticMethodID received NULL jclass"
241 : "java_class == null");
242 jclass jlstring = env_->FindClass("java/lang/String");
243 method = env_->GetStaticMethodID(jlstring, nullptr, "(I)Ljava/lang/String;");
244 EXPECT_EQ(nullptr, method);
245 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
246 : "name == null");
247 method = env_->GetStaticMethodID(jlstring, "valueOf", nullptr);
248 EXPECT_EQ(nullptr, method);
249 check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL"
250 : "sig == null");
251
252 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
253 }
254
255 void GetFromReflectedField_ToReflectedFieldBadArgumentTest(bool check_jni) {
256 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
257 CheckJniAbortCatcher check_jni_abort_catcher;
258
259 jclass c = env_->FindClass("java/lang/String");
260 ASSERT_NE(c, nullptr);
261 jfieldID fid = env_->GetFieldID(c, "count", "I");
262 ASSERT_NE(fid, nullptr);
263
264 // Check class argument for null argument, not checked in non-check JNI.
265 jobject field = env_->ToReflectedField(nullptr, fid, JNI_FALSE);
266 if (check_jni) {
267 EXPECT_EQ(field, nullptr);
268 check_jni_abort_catcher.Check("ToReflectedField received NULL jclass");
269 } else {
270 EXPECT_NE(field, nullptr);
271 }
272
273 field = env_->ToReflectedField(c, nullptr, JNI_FALSE);
274 EXPECT_EQ(field, nullptr);
275 check_jni_abort_catcher.Check(check_jni ? "jfieldID was NULL"
276 : "fid == null");
277
278 fid = env_->FromReflectedField(nullptr);
279 ASSERT_EQ(fid, nullptr);
280 check_jni_abort_catcher.Check(check_jni ? "expected non-null java.lang.reflect.Field"
281 : "jlr_field == null");
282
283 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
284 }
285
286 void GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(bool check_jni) {
287 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
288 CheckJniAbortCatcher check_jni_abort_catcher;
289
290 jclass c = env_->FindClass("java/lang/String");
291 ASSERT_NE(c, nullptr);
292 jmethodID mid = env_->GetMethodID(c, "<init>", "()V");
293 ASSERT_NE(mid, nullptr);
294
295 // Check class argument for null argument, not checked in non-check JNI.
296 jobject method = env_->ToReflectedMethod(nullptr, mid, JNI_FALSE);
297 if (check_jni) {
298 EXPECT_EQ(method, nullptr);
299 check_jni_abort_catcher.Check("ToReflectedMethod received NULL jclass");
300 } else {
301 EXPECT_NE(method, nullptr);
302 }
303
304 method = env_->ToReflectedMethod(c, nullptr, JNI_FALSE);
305 EXPECT_EQ(method, nullptr);
306 check_jni_abort_catcher.Check(check_jni ? "jmethodID was NULL"
307 : "mid == null");
308 mid = env_->FromReflectedMethod(method);
309 ASSERT_EQ(mid, nullptr);
310 check_jni_abort_catcher.Check(check_jni ? "expected non-null method" : "jlr_method == null");
311
312 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
313 }
314
315 void RegisterAndUnregisterNativesBadArguments(bool check_jni,
316 CheckJniAbortCatcher* check_jni_abort_catcher) {
317 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
318 // Passing a class of null is a failure.
319 {
320 JNINativeMethod methods[] = { };
321 EXPECT_EQ(env_->RegisterNatives(nullptr, methods, 0), JNI_ERR);
322 check_jni_abort_catcher->Check(check_jni ? "RegisterNatives received NULL jclass"
323 : "java_class == null");
324 }
325
326 // Passing methods as null is a failure.
327 jclass jlobject = env_->FindClass("java/lang/Object");
328 EXPECT_EQ(env_->RegisterNatives(jlobject, nullptr, 1), JNI_ERR);
329 check_jni_abort_catcher->Check("methods == null");
330
331 // Unregisters null is a failure.
332 EXPECT_EQ(env_->UnregisterNatives(nullptr), JNI_ERR);
333 check_jni_abort_catcher->Check(check_jni ? "UnregisterNatives received NULL jclass"
334 : "java_class == null");
335
336 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
337 }
338
339
340 void GetPrimitiveArrayElementsOfWrongType(bool check_jni) {
341 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
342 CheckJniAbortCatcher jni_abort_catcher;
343
344 jbooleanArray array = env_->NewBooleanArray(10);
345 jboolean is_copy;
346 EXPECT_EQ(env_->GetByteArrayElements(reinterpret_cast<jbyteArray>(array), &is_copy), nullptr);
347 jni_abort_catcher.Check(
348 check_jni ? "incompatible array type boolean[] expected byte[]"
349 : "attempt to get byte primitive array elements with an object of type boolean[]");
350 EXPECT_EQ(env_->GetShortArrayElements(reinterpret_cast<jshortArray>(array), &is_copy), nullptr);
351 jni_abort_catcher.Check(
352 check_jni ? "incompatible array type boolean[] expected short[]"
353 : "attempt to get short primitive array elements with an object of type boolean[]");
354 EXPECT_EQ(env_->GetCharArrayElements(reinterpret_cast<jcharArray>(array), &is_copy), nullptr);
355 jni_abort_catcher.Check(
356 check_jni ? "incompatible array type boolean[] expected char[]"
357 : "attempt to get char primitive array elements with an object of type boolean[]");
358 EXPECT_EQ(env_->GetIntArrayElements(reinterpret_cast<jintArray>(array), &is_copy), nullptr);
359 jni_abort_catcher.Check(
360 check_jni ? "incompatible array type boolean[] expected int[]"
361 : "attempt to get int primitive array elements with an object of type boolean[]");
362 EXPECT_EQ(env_->GetLongArrayElements(reinterpret_cast<jlongArray>(array), &is_copy), nullptr);
363 jni_abort_catcher.Check(
364 check_jni ? "incompatible array type boolean[] expected long[]"
365 : "attempt to get long primitive array elements with an object of type boolean[]");
366 EXPECT_EQ(env_->GetFloatArrayElements(reinterpret_cast<jfloatArray>(array), &is_copy), nullptr);
367 jni_abort_catcher.Check(
368 check_jni ? "incompatible array type boolean[] expected float[]"
369 : "attempt to get float primitive array elements with an object of type boolean[]");
370 EXPECT_EQ(env_->GetDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), &is_copy), nullptr);
371 jni_abort_catcher.Check(
372 check_jni ? "incompatible array type boolean[] expected double[]"
373 : "attempt to get double primitive array elements with an object of type boolean[]");
374 jbyteArray array2 = env_->NewByteArray(10);
375 EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), &is_copy),
376 nullptr);
377 jni_abort_catcher.Check(
378 check_jni ? "incompatible array type byte[] expected boolean[]"
379 : "attempt to get boolean primitive array elements with an object of type byte[]");
380 jobject object = env_->NewStringUTF("Test String");
381 EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), &is_copy),
382 nullptr);
383 jni_abort_catcher.Check(
384 check_jni ? "jarray argument has non-array type: java.lang.String"
385 : "attempt to get boolean primitive array elements with an object of type java.lang.String");
386
387 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
388 }
389
390 void ReleasePrimitiveArrayElementsOfWrongType(bool check_jni) {
391 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
392 CheckJniAbortCatcher jni_abort_catcher;
Ian Rogersdd11d2a2014-11-19 10:06:46 -0800393 {
394 jbooleanArray array = env_->NewBooleanArray(10);
395 ASSERT_TRUE(array != nullptr);
396 jboolean is_copy;
397 jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy);
398 ASSERT_TRUE(elements != nullptr);
399 env_->ReleaseByteArrayElements(reinterpret_cast<jbyteArray>(array),
400 reinterpret_cast<jbyte*>(elements), 0);
401 jni_abort_catcher.Check(
402 check_jni ? "incompatible array type boolean[] expected byte[]"
403 : "attempt to release byte primitive array elements with an object of type boolean[]");
404 env_->ReleaseShortArrayElements(reinterpret_cast<jshortArray>(array),
405 reinterpret_cast<jshort*>(elements), 0);
406 jni_abort_catcher.Check(
407 check_jni ? "incompatible array type boolean[] expected short[]"
408 : "attempt to release short primitive array elements with an object of type boolean[]");
409 env_->ReleaseCharArrayElements(reinterpret_cast<jcharArray>(array),
410 reinterpret_cast<jchar*>(elements), 0);
411 jni_abort_catcher.Check(
412 check_jni ? "incompatible array type boolean[] expected char[]"
413 : "attempt to release char primitive array elements with an object of type boolean[]");
414 env_->ReleaseIntArrayElements(reinterpret_cast<jintArray>(array),
415 reinterpret_cast<jint*>(elements), 0);
416 jni_abort_catcher.Check(
417 check_jni ? "incompatible array type boolean[] expected int[]"
418 : "attempt to release int primitive array elements with an object of type boolean[]");
419 env_->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array),
420 reinterpret_cast<jlong*>(elements), 0);
421 jni_abort_catcher.Check(
422 check_jni ? "incompatible array type boolean[] expected long[]"
423 : "attempt to release long primitive array elements with an object of type boolean[]");
424 env_->ReleaseFloatArrayElements(reinterpret_cast<jfloatArray>(array),
425 reinterpret_cast<jfloat*>(elements), 0);
426 jni_abort_catcher.Check(
427 check_jni ? "incompatible array type boolean[] expected float[]"
428 : "attempt to release float primitive array elements with an object of type boolean[]");
429 env_->ReleaseDoubleArrayElements(reinterpret_cast<jdoubleArray>(array),
430 reinterpret_cast<jdouble*>(elements), 0);
431 jni_abort_catcher.Check(
432 check_jni ? "incompatible array type boolean[] expected double[]"
433 : "attempt to release double primitive array elements with an object of type boolean[]");
Ian Rogers68d8b422014-07-17 11:09:10 -0700434
Ian Rogersdd11d2a2014-11-19 10:06:46 -0800435 // Don't leak the elements array.
436 env_->ReleaseBooleanArrayElements(array, elements, 0);
437 }
438 {
439 jbyteArray array = env_->NewByteArray(10);
440 jboolean is_copy;
441 jbyte* elements = env_->GetByteArrayElements(array, &is_copy);
442
443 env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(array),
444 reinterpret_cast<jboolean*>(elements), 0);
445 jni_abort_catcher.Check(
446 check_jni ? "incompatible array type byte[] expected boolean[]"
447 : "attempt to release boolean primitive array elements with an object of type byte[]");
448 jobject object = env_->NewStringUTF("Test String");
449 env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(object),
450 reinterpret_cast<jboolean*>(elements), 0);
451 jni_abort_catcher.Check(
452 check_jni ? "jarray argument has non-array type: java.lang.String"
453 : "attempt to release boolean primitive array elements with an object of type "
Ian Rogers68d8b422014-07-17 11:09:10 -0700454 "java.lang.String");
455
Ian Rogersdd11d2a2014-11-19 10:06:46 -0800456 // Don't leak the elements array.
457 env_->ReleaseByteArrayElements(array, elements, 0);
458 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700459 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
460 }
461
462 void GetReleasePrimitiveArrayCriticalOfWrongType(bool check_jni) {
463 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
464 CheckJniAbortCatcher jni_abort_catcher;
465
466 jobject object = env_->NewStringUTF("Test String");
467 jboolean is_copy;
468 void* elements = env_->GetPrimitiveArrayCritical(reinterpret_cast<jarray>(object), &is_copy);
469 jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String"
470 : "expected primitive array, given java.lang.String");
471 env_->ReleasePrimitiveArrayCritical(reinterpret_cast<jarray>(object), elements, 0);
472 jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String"
473 : "expected primitive array, given java.lang.String");
474
475 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
476 }
477
478 void GetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) {
479 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
480 CheckJniAbortCatcher jni_abort_catcher;
481 constexpr size_t kLength = 10;
482 jbooleanArray array = env_->NewBooleanArray(kLength);
483 ASSERT_TRUE(array != nullptr);
484 jboolean elements[kLength];
485 env_->GetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength,
486 reinterpret_cast<jbyte*>(elements));
487 jni_abort_catcher.Check(
488 check_jni ? "incompatible array type boolean[] expected byte[]"
489 : "attempt to get region of byte primitive array elements with an object of type boolean[]");
490 env_->GetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength,
491 reinterpret_cast<jshort*>(elements));
492 jni_abort_catcher.Check(
493 check_jni ? "incompatible array type boolean[] expected short[]"
494 : "attempt to get region of short primitive array elements with an object of type boolean[]");
495 env_->GetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength,
496 reinterpret_cast<jchar*>(elements));
497 jni_abort_catcher.Check(
498 check_jni ? "incompatible array type boolean[] expected char[]"
499 : "attempt to get region of char primitive array elements with an object of type boolean[]");
500 env_->GetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength,
501 reinterpret_cast<jint*>(elements));
502 jni_abort_catcher.Check(
503 check_jni ? "incompatible array type boolean[] expected int[]"
504 : "attempt to get region of int primitive array elements with an object of type boolean[]");
505 env_->GetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength,
506 reinterpret_cast<jlong*>(elements));
507 jni_abort_catcher.Check(
508 check_jni ? "incompatible array type boolean[] expected long[]"
509 : "attempt to get region of long primitive array elements with an object of type boolean[]");
510 env_->GetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength,
511 reinterpret_cast<jfloat*>(elements));
512 jni_abort_catcher.Check(
513 check_jni ? "incompatible array type boolean[] expected float[]"
514 : "attempt to get region of float primitive array elements with an object of type boolean[]");
515 env_->GetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength,
516 reinterpret_cast<jdouble*>(elements));
517 jni_abort_catcher.Check(
518 check_jni ? "incompatible array type boolean[] expected double[]"
519 : "attempt to get region of double primitive array elements with an object of type boolean[]");
520 jbyteArray array2 = env_->NewByteArray(10);
521 env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength,
522 reinterpret_cast<jboolean*>(elements));
523 jni_abort_catcher.Check(
524 check_jni ? "incompatible array type byte[] expected boolean[]"
525 : "attempt to get region of boolean primitive array elements with an object of type byte[]");
526 jobject object = env_->NewStringUTF("Test String");
527 env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength,
528 reinterpret_cast<jboolean*>(elements));
529 jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String"
530 : "attempt to get region of boolean primitive array elements with an object of type "
531 "java.lang.String");
532
533 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
534 }
535
536 void SetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) {
537 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
538 CheckJniAbortCatcher jni_abort_catcher;
539 constexpr size_t kLength = 10;
540 jbooleanArray array = env_->NewBooleanArray(kLength);
541 ASSERT_TRUE(array != nullptr);
542 jboolean elements[kLength];
543 env_->SetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength,
544 reinterpret_cast<jbyte*>(elements));
545 jni_abort_catcher.Check(
546 check_jni ? "incompatible array type boolean[] expected byte[]"
547 : "attempt to set region of byte primitive array elements with an object of type boolean[]");
548 env_->SetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength,
549 reinterpret_cast<jshort*>(elements));
550 jni_abort_catcher.Check(
551 check_jni ? "incompatible array type boolean[] expected short[]"
552 : "attempt to set region of short primitive array elements with an object of type boolean[]");
553 env_->SetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength,
554 reinterpret_cast<jchar*>(elements));
555 jni_abort_catcher.Check(
556 check_jni ? "incompatible array type boolean[] expected char[]"
557 : "attempt to set region of char primitive array elements with an object of type boolean[]");
558 env_->SetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength,
559 reinterpret_cast<jint*>(elements));
560 jni_abort_catcher.Check(
561 check_jni ? "incompatible array type boolean[] expected int[]"
562 : "attempt to set region of int primitive array elements with an object of type boolean[]");
563 env_->SetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength,
564 reinterpret_cast<jlong*>(elements));
565 jni_abort_catcher.Check(
566 check_jni ? "incompatible array type boolean[] expected long[]"
567 : "attempt to set region of long primitive array elements with an object of type boolean[]");
568 env_->SetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength,
569 reinterpret_cast<jfloat*>(elements));
570 jni_abort_catcher.Check(
571 check_jni ? "incompatible array type boolean[] expected float[]"
572 : "attempt to set region of float primitive array elements with an object of type boolean[]");
573 env_->SetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength,
574 reinterpret_cast<jdouble*>(elements));
575 jni_abort_catcher.Check(
576 check_jni ? "incompatible array type boolean[] expected double[]"
577 : "attempt to set region of double primitive array elements with an object of type boolean[]");
578 jbyteArray array2 = env_->NewByteArray(10);
579 env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength,
580 reinterpret_cast<jboolean*>(elements));
581 jni_abort_catcher.Check(
582 check_jni ? "incompatible array type byte[] expected boolean[]"
583 : "attempt to set region of boolean primitive array elements with an object of type byte[]");
584 jobject object = env_->NewStringUTF("Test String");
585 env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength,
586 reinterpret_cast<jboolean*>(elements));
587 jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String"
588 : "attempt to set region of boolean primitive array elements with an object of type "
589 "java.lang.String");
590 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
591 }
592
593 void NewObjectArrayBadArguments(bool check_jni) {
594 bool old_check_jni = vm_->SetCheckJniEnabled(check_jni);
595 CheckJniAbortCatcher jni_abort_catcher;
596
597 jclass element_class = env_->FindClass("java/lang/String");
598 ASSERT_NE(element_class, nullptr);
599
600 env_->NewObjectArray(-1, element_class, nullptr);
601 jni_abort_catcher.Check(check_jni ? "negative jsize: -1" : "negative array length: -1");
602
603 env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr);
604 jni_abort_catcher.Check(check_jni ? "negative jsize: -2147483648"
605 : "negative array length: -2147483648");
606
607 EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni));
608 }
609
Elliott Hughesa2501992011-08-26 19:39:54 -0700610 JavaVMExt* vm_;
Brian Carlstrom4d571432012-05-16 00:21:41 -0700611 JNIEnv* env_;
Elliott Hughes814e4032011-08-23 12:07:56 -0700612 jclass aioobe_;
Elliott Hughesb264f082012-04-06 17:10:10 -0700613 jclass ase_;
Elliott Hughesb465ab02011-08-24 11:21:21 -0700614 jclass sioobe_;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700615};
616
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700617TEST_F(JniInternalTest, AllocObject) {
618 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700619 ASSERT_NE(c, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700620 jobject o = env_->AllocObject(c);
Ian Rogers2d10b202014-05-12 19:15:18 -0700621 ASSERT_NE(o, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700622
623 // We have an instance of the class we asked for...
624 ASSERT_TRUE(env_->IsInstanceOf(o, c));
625 // ...whose fields haven't been initialized because
626 // we didn't call a constructor.
627 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I")));
628 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I")));
Ian Rogers2d10b202014-05-12 19:15:18 -0700629 ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700630}
631
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700632TEST_F(JniInternalTest, GetVersion) {
633 ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
634}
635
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700636TEST_F(JniInternalTest, FindClass) {
Andreas Gampe369810a2015-01-14 19:53:31 -0800637 // This tests leads to warnings in the log.
638 ScopedLogSeverity sls(LogSeverity::ERROR);
639
Ian Rogers68d8b422014-07-17 11:09:10 -0700640 FindClassTest(false);
641 FindClassTest(true);
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700642}
643
Elliott Hughescdf53122011-08-19 15:46:09 -0700644TEST_F(JniInternalTest, GetFieldID) {
645 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
Ian Rogers2d10b202014-05-12 19:15:18 -0700646 ASSERT_NE(jlnsfe, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700648 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700649
650 // Wrong type.
651 jfieldID fid = env_->GetFieldID(c, "count", "J");
Ian Rogers2d10b202014-05-12 19:15:18 -0700652 EXPECT_EQ(nullptr, fid);
653 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700654
Ian Rogersb17d08b2011-09-02 16:16:49 -0700655 // Wrong type where type doesn't exist.
656 fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700657 EXPECT_EQ(nullptr, fid);
658 ExpectException(jlnsfe);
Ian Rogersb17d08b2011-09-02 16:16:49 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 // Wrong name.
661 fid = env_->GetFieldID(c, "Count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700662 EXPECT_EQ(nullptr, fid);
663 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700664
665 // Good declared field lookup.
666 fid = env_->GetFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700667 EXPECT_NE(nullptr, fid);
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 EXPECT_FALSE(env_->ExceptionCheck());
669
670 // Good superclass field lookup.
671 c = env_->FindClass("java/lang/StringBuilder");
672 fid = env_->GetFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700673 EXPECT_NE(nullptr, fid);
674 EXPECT_NE(fid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 EXPECT_FALSE(env_->ExceptionCheck());
676
677 // Not instance.
678 fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700679 EXPECT_EQ(nullptr, fid);
680 ExpectException(jlnsfe);
681
682 // Bad arguments.
Ian Rogers68d8b422014-07-17 11:09:10 -0700683 GetFieldIdBadArgumentTest(false);
684 GetFieldIdBadArgumentTest(true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700685}
686
687TEST_F(JniInternalTest, GetStaticFieldID) {
688 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
Ian Rogers2d10b202014-05-12 19:15:18 -0700689 ASSERT_NE(jlnsfe, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700691 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700692
693 // Wrong type.
694 jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J");
Ian Rogers2d10b202014-05-12 19:15:18 -0700695 EXPECT_EQ(nullptr, fid);
696 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700697
Ian Rogersb17d08b2011-09-02 16:16:49 -0700698 // Wrong type where type doesn't exist.
699 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700700 EXPECT_EQ(nullptr, fid);
701 ExpectException(jlnsfe);
Ian Rogersb17d08b2011-09-02 16:16:49 -0700702
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 // Wrong name.
704 fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700705 EXPECT_EQ(nullptr, fid);
706 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700707
708 // Good declared field lookup.
709 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700710 EXPECT_NE(nullptr, fid);
711 EXPECT_NE(fid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 EXPECT_FALSE(env_->ExceptionCheck());
713
714 // Not static.
715 fid = env_->GetStaticFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700716 EXPECT_EQ(nullptr, fid);
717 ExpectException(jlnsfe);
718
719 // Bad arguments.
Ian Rogers68d8b422014-07-17 11:09:10 -0700720 GetStaticFieldIdBadArgumentTest(false);
721 GetStaticFieldIdBadArgumentTest(true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700722}
723
Ian Rogers4dd71f12011-08-16 14:16:02 -0700724TEST_F(JniInternalTest, GetMethodID) {
725 jclass jlobject = env_->FindClass("java/lang/Object");
726 jclass jlstring = env_->FindClass("java/lang/String");
727 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
Brian Carlstrom004644f2014-06-18 08:34:01 -0700728 jclass jncrbc = env_->FindClass("java/nio/channels/ReadableByteChannel");
Ian Rogers4dd71f12011-08-16 14:16:02 -0700729
Brian Carlstrom004644f2014-06-18 08:34:01 -0700730 // Sanity check that no exceptions are pending.
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700732
733 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
Brian Carlstrom004644f2014-06-18 08:34:01 -0700734 // a pending exception.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700735 jmethodID method = env_->GetMethodID(jlobject, "foo", "()V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700736 EXPECT_EQ(nullptr, method);
737 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700738
Brian Carlstrom004644f2014-06-18 08:34:01 -0700739 // Check that java.lang.Object.equals() does exist.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700740 method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
Ian Rogers2d10b202014-05-12 19:15:18 -0700741 EXPECT_NE(nullptr, method);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700742 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700743
744 // Check that GetMethodID for java.lang.String.valueOf(int) fails as the
Brian Carlstrom004644f2014-06-18 08:34:01 -0700745 // method is static.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700746 method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700747 EXPECT_EQ(nullptr, method);
748 ExpectException(jlnsme);
Brian Carlstromea46f952013-07-30 01:26:50 -0700749
Brian Carlstrom004644f2014-06-18 08:34:01 -0700750 // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor.
Brian Carlstromea46f952013-07-30 01:26:50 -0700751 method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700752 EXPECT_NE(nullptr, method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700753 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers2d10b202014-05-12 19:15:18 -0700754
Brian Carlstrom004644f2014-06-18 08:34:01 -0700755 // Check that GetMethodID can find a interface method inherited from another interface.
756 method = env_->GetMethodID(jncrbc, "close", "()V");
757 EXPECT_NE(nullptr, method);
758 EXPECT_FALSE(env_->ExceptionCheck());
759
Ian Rogers2d10b202014-05-12 19:15:18 -0700760 // Bad arguments.
Ian Rogers68d8b422014-07-17 11:09:10 -0700761 GetMethodIdBadArgumentTest(false);
762 GetMethodIdBadArgumentTest(true);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700763}
764
Mathieu Chartiera1e78fa2014-08-20 12:09:38 -0700765TEST_F(JniInternalTest, CallVoidMethodNullReceiver) {
766 jclass jlobject = env_->FindClass("java/lang/Object");
767 jmethodID method;
768
769 // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor.
770 method = env_->GetMethodID(jlobject, "<init>", "()V");
771 EXPECT_NE(nullptr, method);
772 EXPECT_FALSE(env_->ExceptionCheck());
773
774 // Null object to CallVoidMethod.
775 CheckJniAbortCatcher check_jni_abort_catcher;
776 env_->CallVoidMethod(nullptr, method);
777 check_jni_abort_catcher.Check("null");
778}
779
Ian Rogers4dd71f12011-08-16 14:16:02 -0700780TEST_F(JniInternalTest, GetStaticMethodID) {
781 jclass jlobject = env_->FindClass("java/lang/Object");
782 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
783
784 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700786
787 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
788 // a pending exception
789 jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700790 EXPECT_EQ(nullptr, method);
791 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700792
793 // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as
794 // the method is not static
795 method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
Ian Rogers2d10b202014-05-12 19:15:18 -0700796 EXPECT_EQ(nullptr, method);
797 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700798
799 // Check that java.lang.String.valueOf(int) does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700800 jclass jlstring = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700801 method = env_->GetStaticMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
802 EXPECT_NE(nullptr, method);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700803 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers2d10b202014-05-12 19:15:18 -0700804
805 // Bad arguments.
Ian Rogers68d8b422014-07-17 11:09:10 -0700806 GetStaticMethodIdBadArgumentTest(false);
807 GetStaticMethodIdBadArgumentTest(true);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700808}
809
Elliott Hughescdf53122011-08-19 15:46:09 -0700810TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) {
811 jclass jlrField = env_->FindClass("java/lang/reflect/Field");
812 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700813 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 jfieldID fid = env_->GetFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700815 ASSERT_NE(fid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 // Turn the fid into a java.lang.reflect.Field...
817 jobject field = env_->ToReflectedField(c, fid, JNI_FALSE);
Mathieu Chartier78959462014-11-17 10:24:04 -0800818 for (size_t i = 0; i <= kLocalsMax; ++i) {
Mathieu Chartier41da5962014-11-15 13:07:39 -0800819 // Regression test for b/18396311, ToReflectedField leaking local refs causing a local
820 // reference table overflows with 512 references to ArtField
821 env_->DeleteLocalRef(env_->ToReflectedField(c, fid, JNI_FALSE));
822 }
Ian Rogers2d10b202014-05-12 19:15:18 -0700823 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 ASSERT_TRUE(env_->IsInstanceOf(field, jlrField));
825 // ...and back again.
826 jfieldID fid2 = env_->FromReflectedField(field);
Ian Rogers2d10b202014-05-12 19:15:18 -0700827 ASSERT_NE(fid2, nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700828 // Make sure we can actually use it.
829 jstring s = env_->NewStringUTF("poop");
830 ASSERT_EQ(4, env_->GetIntField(s, fid2));
Ian Rogers2d10b202014-05-12 19:15:18 -0700831
832 // Bad arguments.
Ian Rogers68d8b422014-07-17 11:09:10 -0700833 GetFromReflectedField_ToReflectedFieldBadArgumentTest(false);
834 GetFromReflectedField_ToReflectedFieldBadArgumentTest(true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700835}
836
837TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) {
838 jclass jlrMethod = env_->FindClass("java/lang/reflect/Method");
Sebastien Hertzd3333762014-06-26 14:45:07 +0200839 ASSERT_NE(jlrMethod, nullptr);
840 jclass jlrConstructor = env_->FindClass("java/lang/reflect/Constructor");
841 ASSERT_NE(jlrConstructor, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700843 ASSERT_NE(c, nullptr);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200844
845 jmethodID mid = env_->GetMethodID(c, "<init>", "()V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700846 ASSERT_NE(mid, nullptr);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200847 // Turn the mid into a java.lang.reflect.Constructor...
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
Mathieu Chartier78959462014-11-17 10:24:04 -0800849 for (size_t i = 0; i <= kLocalsMax; ++i) {
Mathieu Chartier41da5962014-11-15 13:07:39 -0800850 // Regression test for b/18396311, ToReflectedMethod leaking local refs causing a local
851 // reference table overflows with 512 references to ArtMethod
852 env_->DeleteLocalRef(env_->ToReflectedMethod(c, mid, JNI_FALSE));
853 }
Sebastien Hertzd3333762014-06-26 14:45:07 +0200854 ASSERT_NE(method, nullptr);
855 ASSERT_TRUE(env_->IsInstanceOf(method, jlrConstructor));
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 // ...and back again.
857 jmethodID mid2 = env_->FromReflectedMethod(method);
Ian Rogers2d10b202014-05-12 19:15:18 -0700858 ASSERT_NE(mid2, nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700859 // Make sure we can actually use it.
Sebastien Hertzd3333762014-06-26 14:45:07 +0200860 jstring s = reinterpret_cast<jstring>(env_->AllocObject(c));
861 ASSERT_NE(s, nullptr);
862 env_->CallVoidMethod(s, mid2);
863 ASSERT_EQ(JNI_FALSE, env_->ExceptionCheck());
864
865 mid = env_->GetMethodID(c, "length", "()I");
866 ASSERT_NE(mid, nullptr);
867 // Turn the mid into a java.lang.reflect.Method...
868 method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
869 ASSERT_NE(method, nullptr);
870 ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod));
871 // ...and back again.
872 mid2 = env_->FromReflectedMethod(method);
873 ASSERT_NE(mid2, nullptr);
874 // Make sure we can actually use it.
875 s = env_->NewStringUTF("poop");
876 ASSERT_NE(s, nullptr);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700877 ASSERT_EQ(4, env_->CallIntMethod(s, mid2));
Ian Rogers2d10b202014-05-12 19:15:18 -0700878
879 // Bad arguments.
Ian Rogers68d8b422014-07-17 11:09:10 -0700880 GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(false);
881 GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700882}
883
Ian Rogers2d10b202014-05-12 19:15:18 -0700884static void BogusMethod() {
885 // You can't pass nullptr function pointers to RegisterNatives.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700886}
887
Ian Rogers2d10b202014-05-12 19:15:18 -0700888TEST_F(JniInternalTest, RegisterAndUnregisterNatives) {
Ian Rogers4dd71f12011-08-16 14:16:02 -0700889 jclass jlobject = env_->FindClass("java/lang/Object");
890 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200891 void* native_function = reinterpret_cast<void*>(BogusMethod);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700892
Ian Rogers2d10b202014-05-12 19:15:18 -0700893 // Sanity check that no exceptions are pending.
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700895
Andreas Gampe369810a2015-01-14 19:53:31 -0800896 // The following can print errors to the log we'd like to ignore.
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200897 {
Andreas Gampe369810a2015-01-14 19:53:31 -0800898 ScopedLogSeverity sls(LogSeverity::FATAL);
899 // Check that registering method without name causes a NoSuchMethodError.
900 {
901 JNINativeMethod methods[] = { { nullptr, "()V", native_function } };
902 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
903 }
904 ExpectException(jlnsme);
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200905
Andreas Gampe369810a2015-01-14 19:53:31 -0800906 // Check that registering method without signature causes a NoSuchMethodError.
907 {
908 JNINativeMethod methods[] = { { "notify", nullptr, native_function } };
909 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
910 }
911 ExpectException(jlnsme);
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200912
Andreas Gampe369810a2015-01-14 19:53:31 -0800913 // Check that registering method without function causes a NoSuchMethodError.
914 {
915 JNINativeMethod methods[] = { { "notify", "()V", nullptr } };
916 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
917 }
918 ExpectException(jlnsme);
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200919
Andreas Gampe369810a2015-01-14 19:53:31 -0800920 // Check that registering to a non-existent java.lang.Object.foo() causes a NoSuchMethodError.
921 {
922 JNINativeMethod methods[] = { { "foo", "()V", native_function } };
923 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
924 }
925 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700926
Andreas Gampe369810a2015-01-14 19:53:31 -0800927 // Check that registering non-native methods causes a NoSuchMethodError.
928 {
929 JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", native_function } };
930 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
931 }
932 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700933 }
Ian Rogers4dd71f12011-08-16 14:16:02 -0700934
Ian Rogers2d10b202014-05-12 19:15:18 -0700935 // Check that registering native methods is successful.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700936 {
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200937 JNINativeMethod methods[] = { { "notify", "()V", native_function } };
Ian Rogers2d10b202014-05-12 19:15:18 -0700938 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_OK);
939 }
940 EXPECT_FALSE(env_->ExceptionCheck());
941 EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK);
942
943 // Check that registering no methods isn't a failure.
944 {
945 JNINativeMethod methods[] = { };
946 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 0), JNI_OK);
947 }
948 EXPECT_FALSE(env_->ExceptionCheck());
949 EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK);
950
951 // Check that registering a -ve number of methods is a failure.
952 CheckJniAbortCatcher check_jni_abort_catcher;
953 for (int i = -10; i < 0; ++i) {
954 JNINativeMethod methods[] = { };
955 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, i), JNI_ERR);
956 check_jni_abort_catcher.Check("negative method count: ");
Ian Rogers4dd71f12011-08-16 14:16:02 -0700957 }
958 EXPECT_FALSE(env_->ExceptionCheck());
Elliott Hughes5174fe62011-08-23 15:12:35 -0700959
Ian Rogers2d10b202014-05-12 19:15:18 -0700960 // Unregistering a class with no natives is a warning.
961 EXPECT_EQ(env_->UnregisterNatives(jlnsme), JNI_OK);
Ian Rogers68d8b422014-07-17 11:09:10 -0700962
963 RegisterAndUnregisterNativesBadArguments(false, &check_jni_abort_catcher);
964 RegisterAndUnregisterNativesBadArguments(true, &check_jni_abort_catcher);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700965}
966
Brian Carlstromea46f952013-07-30 01:26:50 -0700967#define EXPECT_PRIMITIVE_ARRAY(new_fn, \
968 get_region_fn, \
969 set_region_fn, \
970 get_elements_fn, \
971 release_elements_fn, \
972 scalar_type, \
973 expected_class_descriptor) \
Ian Rogers2d10b202014-05-12 19:15:18 -0700974 jsize size = 4; \
975 \
Ian Rogers1d99e452014-01-02 17:36:41 -0800976 { \
977 CheckJniAbortCatcher jni_abort_catcher; \
Ian Rogers68d8b422014-07-17 11:09:10 -0700978 down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(false); \
Ian Rogers1d99e452014-01-02 17:36:41 -0800979 /* Allocate an negative sized array and check it has the right failure type. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700980 EXPECT_EQ(env_->new_fn(-1), nullptr); \
Ian Rogers1d99e452014-01-02 17:36:41 -0800981 jni_abort_catcher.Check("negative array length: -1"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700982 EXPECT_EQ(env_->new_fn(std::numeric_limits<jint>::min()), nullptr); \
Ian Rogers1d99e452014-01-02 17:36:41 -0800983 jni_abort_catcher.Check("negative array length: -2147483648"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700984 /* Pass the array as null. */ \
985 EXPECT_EQ(0, env_->GetArrayLength(nullptr)); \
986 jni_abort_catcher.Check("java_array == null"); \
987 env_->get_region_fn(nullptr, 0, 0, nullptr); \
988 jni_abort_catcher.Check("java_array == null"); \
989 env_->set_region_fn(nullptr, 0, 0, nullptr); \
990 jni_abort_catcher.Check("java_array == null"); \
991 env_->get_elements_fn(nullptr, nullptr); \
992 jni_abort_catcher.Check("java_array == null"); \
993 env_->release_elements_fn(nullptr, nullptr, 0); \
994 jni_abort_catcher.Check("java_array == null"); \
995 /* Pass the elements for region as null. */ \
996 scalar_type ## Array a = env_->new_fn(size); \
997 env_->get_region_fn(a, 0, size, nullptr); \
998 jni_abort_catcher.Check("buf == null"); \
999 env_->set_region_fn(a, 0, size, nullptr); \
1000 jni_abort_catcher.Check("buf == null"); \
Ian Rogers68d8b422014-07-17 11:09:10 -07001001 down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(true); \
Ian Rogers1d99e452014-01-02 17:36:41 -08001002 } \
Elliott Hughes814e4032011-08-23 12:07:56 -07001003 /* Allocate an array and check it has the right type and length. */ \
1004 scalar_type ## Array a = env_->new_fn(size); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001005 EXPECT_NE(a, nullptr); \
Elliott Hughes814e4032011-08-23 12:07:56 -07001006 EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \
1007 EXPECT_EQ(size, env_->GetArrayLength(a)); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001008 \
1009 /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \
Elliott Hughes814e4032011-08-23 12:07:56 -07001010 /* AIOOBE for negative start offset. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001011 env_->get_region_fn(a, -1, 1, nullptr); \
1012 ExpectException(aioobe_); \
1013 env_->set_region_fn(a, -1, 1, nullptr); \
1014 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001015 \
Elliott Hughes814e4032011-08-23 12:07:56 -07001016 /* AIOOBE for negative length. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001017 env_->get_region_fn(a, 0, -1, nullptr); \
1018 ExpectException(aioobe_); \
1019 env_->set_region_fn(a, 0, -1, nullptr); \
1020 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001021 \
Elliott Hughes814e4032011-08-23 12:07:56 -07001022 /* AIOOBE for buffer overrun. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001023 env_->get_region_fn(a, size - 1, size, nullptr); \
1024 ExpectException(aioobe_); \
1025 env_->set_region_fn(a, size - 1, size, nullptr); \
1026 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001027 \
Ian Rogers2d10b202014-05-12 19:15:18 -07001028 /* It's okay for the buffer to be nullptr as long as the length is 0. */ \
1029 env_->get_region_fn(a, 2, 0, nullptr); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001030 /* Even if the offset is invalid... */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001031 env_->get_region_fn(a, 123, 0, nullptr); \
1032 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001033 \
Ian Rogers2d10b202014-05-12 19:15:18 -07001034 /* It's okay for the buffer to be nullptr as long as the length is 0. */ \
1035 env_->set_region_fn(a, 2, 0, nullptr); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001036 /* Even if the offset is invalid... */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001037 env_->set_region_fn(a, 123, 0, nullptr); \
1038 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001039 \
Elliott Hughes814e4032011-08-23 12:07:56 -07001040 /* Prepare a couple of buffers. */ \
Ian Rogers700a4022014-05-19 16:49:03 -07001041 std::unique_ptr<scalar_type[]> src_buf(new scalar_type[size]); \
1042 std::unique_ptr<scalar_type[]> dst_buf(new scalar_type[size]); \
Elliott Hughes814e4032011-08-23 12:07:56 -07001043 for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \
1044 for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001045 \
Elliott Hughes814e4032011-08-23 12:07:56 -07001046 /* Copy all of src_buf onto the heap. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -07001047 env_->set_region_fn(a, 0, size, &src_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -07001048 /* Copy back only part. */ \
1049 env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001050 EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
1051 << "short copy equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -07001052 /* Copy the missing pieces. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -07001053 env_->get_region_fn(a, 0, 1, &dst_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -07001054 env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001055 EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
1056 << "fixed copy not equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -07001057 /* Copy back the whole array. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -07001058 env_->get_region_fn(a, 0, size, &dst_buf[0]); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001059 EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
1060 << "full copy not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -07001061 /* GetPrimitiveArrayCritical */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001062 void* v = env_->GetPrimitiveArrayCritical(a, nullptr); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001063 EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \
1064 << "GetPrimitiveArrayCritical not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -07001065 env_->ReleasePrimitiveArrayCritical(a, v, 0); \
1066 /* GetXArrayElements */ \
Ian Rogers2d10b202014-05-12 19:15:18 -07001067 scalar_type* xs = env_->get_elements_fn(a, nullptr); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001068 EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \
1069 << # get_elements_fn " not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -07001070 env_->release_elements_fn(a, xs, 0); \
Elliott Hughesbd935992011-08-22 11:59:34 -07001071
Elliott Hughes814e4032011-08-23 12:07:56 -07001072TEST_F(JniInternalTest, BooleanArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001073 EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion,
1074 GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z");
Elliott Hughes814e4032011-08-23 12:07:56 -07001075}
1076TEST_F(JniInternalTest, ByteArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001077 EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion,
1078 GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B");
Elliott Hughes814e4032011-08-23 12:07:56 -07001079}
1080TEST_F(JniInternalTest, CharArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001081 EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion,
1082 GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C");
Elliott Hughes814e4032011-08-23 12:07:56 -07001083}
1084TEST_F(JniInternalTest, DoubleArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001085 EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion,
1086 GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D");
Elliott Hughes814e4032011-08-23 12:07:56 -07001087}
1088TEST_F(JniInternalTest, FloatArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001089 EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion,
1090 GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F");
Elliott Hughes814e4032011-08-23 12:07:56 -07001091}
1092TEST_F(JniInternalTest, IntArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001093 EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion,
1094 GetIntArrayElements, ReleaseIntArrayElements, jint, "[I");
Elliott Hughes814e4032011-08-23 12:07:56 -07001095}
1096TEST_F(JniInternalTest, LongArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001097 EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion,
1098 GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J");
Elliott Hughes814e4032011-08-23 12:07:56 -07001099}
1100TEST_F(JniInternalTest, ShortArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001101 EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion,
1102 GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001103}
1104
Ian Rogers2d10b202014-05-12 19:15:18 -07001105TEST_F(JniInternalTest, GetPrimitiveArrayElementsOfWrongType) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001106 GetPrimitiveArrayElementsOfWrongType(false);
1107 GetPrimitiveArrayElementsOfWrongType(true);
Ian Rogers2d10b202014-05-12 19:15:18 -07001108}
1109
1110TEST_F(JniInternalTest, ReleasePrimitiveArrayElementsOfWrongType) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001111 ReleasePrimitiveArrayElementsOfWrongType(false);
1112 ReleasePrimitiveArrayElementsOfWrongType(true);
Ian Rogers2d10b202014-05-12 19:15:18 -07001113}
Ian Rogers68d8b422014-07-17 11:09:10 -07001114
Ian Rogers2d10b202014-05-12 19:15:18 -07001115TEST_F(JniInternalTest, GetReleasePrimitiveArrayCriticalOfWrongType) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001116 GetReleasePrimitiveArrayCriticalOfWrongType(false);
1117 GetReleasePrimitiveArrayCriticalOfWrongType(true);
Ian Rogers2d10b202014-05-12 19:15:18 -07001118}
1119
1120TEST_F(JniInternalTest, GetPrimitiveArrayRegionElementsOfWrongType) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001121 GetPrimitiveArrayRegionElementsOfWrongType(false);
1122 GetPrimitiveArrayRegionElementsOfWrongType(true);
Ian Rogers2d10b202014-05-12 19:15:18 -07001123}
1124
1125TEST_F(JniInternalTest, SetPrimitiveArrayRegionElementsOfWrongType) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001126 SetPrimitiveArrayRegionElementsOfWrongType(false);
1127 SetPrimitiveArrayRegionElementsOfWrongType(true);
Ian Rogers2d10b202014-05-12 19:15:18 -07001128}
1129
Elliott Hughesf2682d52011-08-15 16:37:04 -07001130TEST_F(JniInternalTest, NewObjectArray) {
Elliott Hughesbd935992011-08-22 11:59:34 -07001131 jclass element_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -07001132 ASSERT_NE(element_class, nullptr);
Elliott Hughesbd935992011-08-22 11:59:34 -07001133 jclass array_class = env_->FindClass("[Ljava/lang/String;");
Ian Rogers2d10b202014-05-12 19:15:18 -07001134 ASSERT_NE(array_class, nullptr);
Elliott Hughesf2682d52011-08-15 16:37:04 -07001135
Ian Rogers1d99e452014-01-02 17:36:41 -08001136 jobjectArray a = env_->NewObjectArray(0, element_class, nullptr);
Ian Rogers2d10b202014-05-12 19:15:18 -07001137 EXPECT_NE(a, nullptr);
Elliott Hughesbd935992011-08-22 11:59:34 -07001138 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
1139 EXPECT_EQ(0, env_->GetArrayLength(a));
1140
Ian Rogers1d99e452014-01-02 17:36:41 -08001141 a = env_->NewObjectArray(1, element_class, nullptr);
Ian Rogers2d10b202014-05-12 19:15:18 -07001142 EXPECT_NE(a, nullptr);
Elliott Hughesbd935992011-08-22 11:59:34 -07001143 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
1144 EXPECT_EQ(1, env_->GetArrayLength(a));
Ian Rogers1d99e452014-01-02 17:36:41 -08001145 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr));
Ian Rogers1d99e452014-01-02 17:36:41 -08001146
Ian Rogers2d10b202014-05-12 19:15:18 -07001147 // Negative array length checks.
Ian Rogers68d8b422014-07-17 11:09:10 -07001148 NewObjectArrayBadArguments(false);
1149 NewObjectArrayBadArguments(true);
Ian Rogers1d99e452014-01-02 17:36:41 -08001150}
1151
1152TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) {
1153 const char* primitive_descriptors = "VZBSCIJFD";
1154 const char* primitive_names[] = {
1155 "void", "boolean", "byte", "short", "char", "int", "long", "float", "double"
1156 };
1157 ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names));
1158
Ian Rogers68d8b422014-07-17 11:09:10 -07001159 bool old_check_jni = vm_->SetCheckJniEnabled(false);
Ian Rogers1d99e452014-01-02 17:36:41 -08001160 CheckJniAbortCatcher jni_abort_catcher;
1161 for (size_t i = 0; i < strlen(primitive_descriptors); ++i) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001162 env_->NewObjectArray(0, nullptr, nullptr);
1163 jni_abort_catcher.Check("element_jclass == null");
Ian Rogers1d99e452014-01-02 17:36:41 -08001164 jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]);
1165 env_->NewObjectArray(1, primitive_class, nullptr);
1166 std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i]));
1167 jni_abort_catcher.Check(error_msg.c_str());
1168 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001169 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1170 for (size_t i = 0; i < strlen(primitive_descriptors); ++i) {
1171 env_->NewObjectArray(0, nullptr, nullptr);
1172 jni_abort_catcher.Check("NewObjectArray received NULL jclass");
1173 jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]);
1174 env_->NewObjectArray(1, primitive_class, nullptr);
1175 std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i]));
1176 jni_abort_catcher.Check(error_msg.c_str());
1177 }
1178 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Ian Rogers1d99e452014-01-02 17:36:41 -08001179}
1180
1181TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) {
1182 jclass element_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -07001183 ASSERT_NE(element_class, nullptr);
Ian Rogers1d99e452014-01-02 17:36:41 -08001184 jclass array_class = env_->FindClass("[Ljava/lang/String;");
Ian Rogers2d10b202014-05-12 19:15:18 -07001185 ASSERT_NE(array_class, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001186
1187 jstring s = env_->NewStringUTF("poop");
Ian Rogers1d99e452014-01-02 17:36:41 -08001188 jobjectArray a = env_->NewObjectArray(2, element_class, s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001189 EXPECT_NE(a, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001190 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
1191 EXPECT_EQ(2, env_->GetArrayLength(a));
1192 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s));
1193 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s));
Ian Rogers1d99e452014-01-02 17:36:41 -08001194
1195 // Attempt to incorrect create an array of strings with initial value of string arrays.
1196 CheckJniAbortCatcher jni_abort_catcher;
1197 env_->NewObjectArray(2, element_class, a);
1198 jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element "
1199 "type of 'java.lang.String'");
Elliott Hughesbd935992011-08-22 11:59:34 -07001200}
1201
1202TEST_F(JniInternalTest, GetArrayLength) {
Ian Rogers2f022bd2014-11-11 08:43:05 -08001203 // Already tested in NewObjectArray/NewPrimitiveArray except for NULL.
1204 CheckJniAbortCatcher jni_abort_catcher;
1205 bool old_check_jni = vm_->SetCheckJniEnabled(false);
1206 EXPECT_EQ(0, env_->GetArrayLength(nullptr));
1207 jni_abort_catcher.Check("java_array == null");
1208 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1209 EXPECT_EQ(JNI_ERR, env_->GetArrayLength(nullptr));
1210 jni_abort_catcher.Check("jarray was NULL");
1211 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -07001212}
1213
Elliott Hughes37f7a402011-08-22 18:56:01 -07001214TEST_F(JniInternalTest, GetObjectClass) {
1215 jclass string_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -07001216 ASSERT_NE(string_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001217 jclass class_class = env_->FindClass("java/lang/Class");
Ian Rogers2d10b202014-05-12 19:15:18 -07001218 ASSERT_NE(class_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001219
1220 jstring s = env_->NewStringUTF("poop");
1221 jclass c = env_->GetObjectClass(s);
1222 ASSERT_TRUE(env_->IsSameObject(string_class, c));
1223
1224 jclass c2 = env_->GetObjectClass(c);
1225 ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2)));
Ian Rogers2d10b202014-05-12 19:15:18 -07001226
1227 // Null as object should fail.
1228 CheckJniAbortCatcher jni_abort_catcher;
1229 EXPECT_EQ(env_->GetObjectClass(nullptr), nullptr);
1230 jni_abort_catcher.Check("java_object == null");
Elliott Hughes37f7a402011-08-22 18:56:01 -07001231}
1232
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001233TEST_F(JniInternalTest, GetSuperclass) {
1234 jclass object_class = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -07001235 ASSERT_NE(object_class, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001236 jclass string_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -07001237 ASSERT_NE(string_class, nullptr);
Ian Rogersdc180202012-01-29 14:47:29 -08001238 jclass runnable_interface = env_->FindClass("java/lang/Runnable");
Ian Rogers2d10b202014-05-12 19:15:18 -07001239 ASSERT_NE(runnable_interface, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001240 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class)));
Ian Rogers2d10b202014-05-12 19:15:18 -07001241 ASSERT_EQ(env_->GetSuperclass(object_class), nullptr);
Ian Rogersdc180202012-01-29 14:47:29 -08001242 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface)));
Ian Rogers2d10b202014-05-12 19:15:18 -07001243
1244 // Null as class should fail.
1245 CheckJniAbortCatcher jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001246 bool old_check_jni = vm_->SetCheckJniEnabled(false);
Ian Rogers2d10b202014-05-12 19:15:18 -07001247 EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr);
1248 jni_abort_catcher.Check("java_class == null");
Ian Rogers68d8b422014-07-17 11:09:10 -07001249 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1250 EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr);
1251 jni_abort_catcher.Check("GetSuperclass received NULL jclass");
1252 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001253}
1254
Elliott Hughes37f7a402011-08-22 18:56:01 -07001255TEST_F(JniInternalTest, IsAssignableFrom) {
1256 jclass object_class = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -07001257 ASSERT_NE(object_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001258 jclass string_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -07001259 ASSERT_NE(string_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001260
Narayan Kamath1268b742014-07-11 19:15:11 +01001261 // A superclass is assignable from an instance of its
1262 // subclass but not vice versa.
1263 ASSERT_TRUE(env_->IsAssignableFrom(string_class, object_class));
1264 ASSERT_FALSE(env_->IsAssignableFrom(object_class, string_class));
1265
1266 jclass charsequence_interface = env_->FindClass("java/lang/CharSequence");
1267 ASSERT_NE(charsequence_interface, nullptr);
1268
1269 // An interface is assignable from an instance of an implementing
1270 // class but not vice versa.
1271 ASSERT_TRUE(env_->IsAssignableFrom(string_class, charsequence_interface));
1272 ASSERT_FALSE(env_->IsAssignableFrom(charsequence_interface, string_class));
1273
1274 // Check that arrays are covariant.
1275 jclass string_array_class = env_->FindClass("[Ljava/lang/String;");
1276 ASSERT_NE(string_array_class, nullptr);
1277 jclass object_array_class = env_->FindClass("[Ljava/lang/Object;");
1278 ASSERT_NE(object_array_class, nullptr);
1279 ASSERT_TRUE(env_->IsAssignableFrom(string_array_class, object_array_class));
1280 ASSERT_FALSE(env_->IsAssignableFrom(object_array_class, string_array_class));
1281
1282 // Primitive types are tested in 004-JniTest.
Ian Rogers2d10b202014-05-12 19:15:18 -07001283
1284 // Null as either class should fail.
1285 CheckJniAbortCatcher jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001286 bool old_check_jni = vm_->SetCheckJniEnabled(false);
Ian Rogers2d10b202014-05-12 19:15:18 -07001287 EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE);
1288 jni_abort_catcher.Check("java_class1 == null");
1289 EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE);
1290 jni_abort_catcher.Check("java_class2 == null");
Ian Rogers68d8b422014-07-17 11:09:10 -07001291 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1292 EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE);
1293 jni_abort_catcher.Check("IsAssignableFrom received NULL jclass");
1294 EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE);
1295 jni_abort_catcher.Check("IsAssignableFrom received NULL jclass");
1296 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001297}
1298
Elliott Hughesb465ab02011-08-24 11:21:21 -07001299TEST_F(JniInternalTest, GetObjectRefType) {
1300 jclass local = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -07001301 ASSERT_TRUE(local != nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001302 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local));
1303
1304 jobject global = env_->NewGlobalRef(local);
1305 EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global));
1306
1307 jweak weak_global = env_->NewWeakGlobalRef(local);
1308 EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global));
1309
Andreas Gampea8763072014-12-20 00:08:35 -08001310 {
1311 CheckJniAbortCatcher jni_abort_catcher;
1312 jobject invalid = reinterpret_cast<jobject>(this);
1313 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid));
1314 jni_abort_catcher.Check("use of invalid jobject");
1315 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07001316
1317 // TODO: invoke a native method and test that its arguments are considered local references.
Ian Rogers2d10b202014-05-12 19:15:18 -07001318
Andreas Gampea8763072014-12-20 00:08:35 -08001319 // Null as pointer should not fail and return invalid-ref. b/18820997
Ian Rogers2d10b202014-05-12 19:15:18 -07001320 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(nullptr));
Andreas Gampea8763072014-12-20 00:08:35 -08001321
1322 // TODO: Null as reference should return the original type.
1323 // This requires running a GC so a non-null object gets freed.
Elliott Hughesb465ab02011-08-24 11:21:21 -07001324}
1325
Mathieu Chartier08599992013-12-20 17:17:55 -08001326TEST_F(JniInternalTest, StaleWeakGlobal) {
1327 jclass java_lang_Class = env_->FindClass("java/lang/Class");
Ian Rogers2d10b202014-05-12 19:15:18 -07001328 ASSERT_NE(java_lang_Class, nullptr);
1329 jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, nullptr);
1330 ASSERT_NE(local_ref, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -08001331 jweak weak_global = env_->NewWeakGlobalRef(local_ref);
Ian Rogers2d10b202014-05-12 19:15:18 -07001332 ASSERT_NE(weak_global, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -08001333 env_->DeleteLocalRef(local_ref);
1334 Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global.
1335 jobject new_global_ref = env_->NewGlobalRef(weak_global);
Ian Rogers2d10b202014-05-12 19:15:18 -07001336 EXPECT_EQ(new_global_ref, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -08001337 jobject new_local_ref = env_->NewLocalRef(weak_global);
Ian Rogers2d10b202014-05-12 19:15:18 -07001338 EXPECT_EQ(new_local_ref, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -08001339}
1340
Elliott Hughes8a26c5c2011-08-15 18:35:43 -07001341TEST_F(JniInternalTest, NewStringUTF) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001342 EXPECT_EQ(env_->NewStringUTF(nullptr), nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -07001343 jstring s;
1344
1345 s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001346 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -07001347 EXPECT_EQ(0, env_->GetStringLength(s));
1348 EXPECT_EQ(0, env_->GetStringUTFLength(s));
1349 s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001350 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -07001351 EXPECT_EQ(5, env_->GetStringLength(s));
1352 EXPECT_EQ(5, env_->GetStringUTFLength(s));
1353
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001354 // Encoded surrogate pair.
1355 s = env_->NewStringUTF("\xed\xa0\x81\xed\xb0\x80");
1356 EXPECT_NE(s, nullptr);
1357 EXPECT_EQ(2, env_->GetStringLength(s));
Narayan Kamathe16dad12015-02-13 11:49:22 +00001358
1359 // The surrogate pair gets encoded into a 4 byte UTF sequence..
1360 EXPECT_EQ(4, env_->GetStringUTFLength(s));
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001361 const char* chars = env_->GetStringUTFChars(s, nullptr);
Narayan Kamathe16dad12015-02-13 11:49:22 +00001362 EXPECT_STREQ("\xf0\x90\x90\x80", chars);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001363 env_->ReleaseStringUTFChars(s, chars);
1364
Narayan Kamathe16dad12015-02-13 11:49:22 +00001365 // .. but is stored as is in the utf-16 representation.
1366 const jchar* jchars = env_->GetStringChars(s, nullptr);
1367 EXPECT_EQ(0xd801, jchars[0]);
1368 EXPECT_EQ(0xdc00, jchars[1]);
1369 env_->ReleaseStringChars(s, jchars);
1370
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001371 // 4 byte UTF sequence appended to an encoded surrogate pair.
1372 s = env_->NewStringUTF("\xed\xa0\x81\xed\xb0\x80 \xf0\x9f\x8f\xa0");
1373 EXPECT_NE(s, nullptr);
Narayan Kamathe16dad12015-02-13 11:49:22 +00001374
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001375 // The 4 byte sequence {0xf0, 0x9f, 0x8f, 0xa0} is converted into a surrogate
Narayan Kamathe16dad12015-02-13 11:49:22 +00001376 // pair {0xd83c, 0xdfe0}.
1377 EXPECT_EQ(5, env_->GetStringLength(s));
1378 jchars = env_->GetStringChars(s, nullptr);
1379 // The first surrogate pair, encoded as such in the input.
1380 EXPECT_EQ(0xd801, jchars[0]);
1381 EXPECT_EQ(0xdc00, jchars[1]);
1382 // The second surrogate pair, from the 4 byte UTF sequence in the input.
1383 EXPECT_EQ(0xd83c, jchars[3]);
1384 EXPECT_EQ(0xdfe0, jchars[4]);
1385 env_->ReleaseStringChars(s, jchars);
1386
1387 EXPECT_EQ(9, env_->GetStringUTFLength(s));
1388 chars = env_->GetStringUTFChars(s, nullptr);
1389 EXPECT_STREQ("\xf0\x90\x90\x80 \xf0\x9f\x8f\xa0", chars);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001390 env_->ReleaseStringUTFChars(s, chars);
1391
1392 // A string with 1, 2, 3 and 4 byte UTF sequences with spaces
1393 // between them
1394 s = env_->NewStringUTF("\x24 \xc2\xa2 \xe2\x82\xac \xf0\x9f\x8f\xa0");
1395 EXPECT_NE(s, nullptr);
1396 EXPECT_EQ(8, env_->GetStringLength(s));
Narayan Kamathe16dad12015-02-13 11:49:22 +00001397 EXPECT_EQ(13, env_->GetStringUTFLength(s));
Elliott Hughesf2682d52011-08-15 16:37:04 -07001398}
1399
Elliott Hughes814e4032011-08-23 12:07:56 -07001400TEST_F(JniInternalTest, NewString) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001401 jchar chars[] = { 'h', 'i' };
1402 jstring s;
1403 s = env_->NewString(chars, 0);
Ian Rogers2d10b202014-05-12 19:15:18 -07001404 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -07001405 EXPECT_EQ(0, env_->GetStringLength(s));
1406 EXPECT_EQ(0, env_->GetStringUTFLength(s));
1407 s = env_->NewString(chars, 2);
Ian Rogers2d10b202014-05-12 19:15:18 -07001408 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -07001409 EXPECT_EQ(2, env_->GetStringLength(s));
1410 EXPECT_EQ(2, env_->GetStringUTFLength(s));
1411
1412 // TODO: check some non-ASCII strings.
1413}
1414
Jesse Wilson25e79a52011-11-18 15:31:58 -05001415TEST_F(JniInternalTest, NewStringNullCharsZeroLength) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001416 jstring s = env_->NewString(nullptr, 0);
Ian Rogers2d10b202014-05-12 19:15:18 -07001417 EXPECT_NE(s, nullptr);
Jesse Wilson25e79a52011-11-18 15:31:58 -05001418 EXPECT_EQ(0, env_->GetStringLength(s));
1419}
1420
Ian Rogers1d99e452014-01-02 17:36:41 -08001421TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) {
1422 CheckJniAbortCatcher jni_abort_catcher;
1423 env_->NewString(nullptr, 1);
1424 jni_abort_catcher.Check("chars == null && char_count > 0");
1425}
1426
1427TEST_F(JniInternalTest, NewStringNegativeLength) {
1428 CheckJniAbortCatcher jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001429 bool old_check_jni = vm_->SetCheckJniEnabled(false);
Ian Rogers1d99e452014-01-02 17:36:41 -08001430 env_->NewString(nullptr, -1);
1431 jni_abort_catcher.Check("char_count < 0: -1");
1432 env_->NewString(nullptr, std::numeric_limits<jint>::min());
1433 jni_abort_catcher.Check("char_count < 0: -2147483648");
Ian Rogers68d8b422014-07-17 11:09:10 -07001434 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1435 env_->NewString(nullptr, -1);
1436 jni_abort_catcher.Check("negative jsize: -1");
1437 env_->NewString(nullptr, std::numeric_limits<jint>::min());
1438 jni_abort_catcher.Check("negative jsize: -2147483648");
1439 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Jesse Wilson25e79a52011-11-18 15:31:58 -05001440}
1441
Elliott Hughesb465ab02011-08-24 11:21:21 -07001442TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
1443 // Already tested in the NewString/NewStringUTF tests.
1444}
1445
1446TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) {
1447 jstring s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001448 ASSERT_TRUE(s != nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001449
Ian Rogers2d10b202014-05-12 19:15:18 -07001450 env_->GetStringRegion(s, -1, 0, nullptr);
1451 ExpectException(sioobe_);
1452 env_->GetStringRegion(s, 0, -1, nullptr);
1453 ExpectException(sioobe_);
1454 env_->GetStringRegion(s, 0, 10, nullptr);
1455 ExpectException(sioobe_);
1456 env_->GetStringRegion(s, 10, 1, nullptr);
1457 ExpectException(sioobe_);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001458
1459 jchar chars[4] = { 'x', 'x', 'x', 'x' };
1460 env_->GetStringRegion(s, 1, 2, &chars[1]);
1461 EXPECT_EQ('x', chars[0]);
1462 EXPECT_EQ('e', chars[1]);
1463 EXPECT_EQ('l', chars[2]);
1464 EXPECT_EQ('x', chars[3]);
1465
Ian Rogers2d10b202014-05-12 19:15:18 -07001466 // It's okay for the buffer to be nullptr as long as the length is 0.
1467 env_->GetStringRegion(s, 2, 0, nullptr);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001468 // Even if the offset is invalid...
Ian Rogers2d10b202014-05-12 19:15:18 -07001469 env_->GetStringRegion(s, 123, 0, nullptr);
1470 ExpectException(sioobe_);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001471
Ian Rogers2d10b202014-05-12 19:15:18 -07001472 env_->GetStringUTFRegion(s, -1, 0, nullptr);
1473 ExpectException(sioobe_);
1474 env_->GetStringUTFRegion(s, 0, -1, nullptr);
1475 ExpectException(sioobe_);
1476 env_->GetStringUTFRegion(s, 0, 10, nullptr);
1477 ExpectException(sioobe_);
1478 env_->GetStringUTFRegion(s, 10, 1, nullptr);
1479 ExpectException(sioobe_);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001480
1481 char bytes[4] = { 'x', 'x', 'x', 'x' };
1482 env_->GetStringUTFRegion(s, 1, 2, &bytes[1]);
1483 EXPECT_EQ('x', bytes[0]);
1484 EXPECT_EQ('e', bytes[1]);
1485 EXPECT_EQ('l', bytes[2]);
1486 EXPECT_EQ('x', bytes[3]);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001487
Ian Rogers2d10b202014-05-12 19:15:18 -07001488 // It's okay for the buffer to be nullptr as long as the length is 0.
1489 env_->GetStringUTFRegion(s, 2, 0, nullptr);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001490 // Even if the offset is invalid...
Ian Rogers2d10b202014-05-12 19:15:18 -07001491 env_->GetStringUTFRegion(s, 123, 0, nullptr);
1492 ExpectException(sioobe_);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001493}
1494
Elliott Hughes75770752011-08-24 17:52:38 -07001495TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001496 // Passing in a nullptr jstring is ignored normally, but caught by -Xcheck:jni.
Ian Rogers68d8b422014-07-17 11:09:10 -07001497 bool old_check_jni = vm_->SetCheckJniEnabled(false);
Elliott Hughesb264f082012-04-06 17:10:10 -07001498 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001499 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers2d10b202014-05-12 19:15:18 -07001500 EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -07001501 }
1502 {
1503 CheckJniAbortCatcher check_jni_abort_catcher;
1504 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1505 EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr);
1506 check_jni_abort_catcher.Check("GetStringUTFChars received NULL jstring");
1507 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughesb264f082012-04-06 17:10:10 -07001508 }
Elliott Hughes75770752011-08-24 17:52:38 -07001509
1510 jstring s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001511 ASSERT_TRUE(s != nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001512
Ian Rogers2d10b202014-05-12 19:15:18 -07001513 const char* utf = env_->GetStringUTFChars(s, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001514 EXPECT_STREQ("hello", utf);
1515 env_->ReleaseStringUTFChars(s, utf);
1516
1517 jboolean is_copy = JNI_FALSE;
1518 utf = env_->GetStringUTFChars(s, &is_copy);
1519 EXPECT_EQ(JNI_TRUE, is_copy);
1520 EXPECT_STREQ("hello", utf);
1521 env_->ReleaseStringUTFChars(s, utf);
1522}
1523
1524TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) {
1525 jstring s = env_->NewStringUTF("hello");
Fred Shih56890e22014-06-02 11:11:52 -07001526 ScopedObjectAccess soa(env_);
1527 mirror::String* s_m = soa.Decode<mirror::String*>(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001528 ASSERT_TRUE(s != nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001529
1530 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
Ian Rogers2d10b202014-05-12 19:15:18 -07001531 const jchar* chars = env_->GetStringChars(s, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001532 EXPECT_EQ(expected[0], chars[0]);
1533 EXPECT_EQ(expected[1], chars[1]);
1534 EXPECT_EQ(expected[2], chars[2]);
1535 EXPECT_EQ(expected[3], chars[3]);
1536 EXPECT_EQ(expected[4], chars[4]);
1537 env_->ReleaseStringChars(s, chars);
1538
1539 jboolean is_copy = JNI_FALSE;
1540 chars = env_->GetStringChars(s, &is_copy);
Fred Shih56890e22014-06-02 11:11:52 -07001541 if (Runtime::Current()->GetHeap()->IsMovableObject(s_m->GetCharArray())) {
1542 EXPECT_EQ(JNI_TRUE, is_copy);
1543 } else {
1544 EXPECT_EQ(JNI_FALSE, is_copy);
1545 }
Elliott Hughes75770752011-08-24 17:52:38 -07001546 EXPECT_EQ(expected[0], chars[0]);
1547 EXPECT_EQ(expected[1], chars[1]);
1548 EXPECT_EQ(expected[2], chars[2]);
1549 EXPECT_EQ(expected[3], chars[3]);
1550 EXPECT_EQ(expected[4], chars[4]);
1551 env_->ReleaseStringChars(s, chars);
1552}
1553
1554TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) {
1555 jstring s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001556 ASSERT_TRUE(s != nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001557
1558 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
Ian Rogers2d10b202014-05-12 19:15:18 -07001559 const jchar* chars = env_->GetStringCritical(s, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001560 EXPECT_EQ(expected[0], chars[0]);
1561 EXPECT_EQ(expected[1], chars[1]);
1562 EXPECT_EQ(expected[2], chars[2]);
1563 EXPECT_EQ(expected[3], chars[3]);
1564 EXPECT_EQ(expected[4], chars[4]);
1565 env_->ReleaseStringCritical(s, chars);
1566
Fred Shih56890e22014-06-02 11:11:52 -07001567 jboolean is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001568 chars = env_->GetStringCritical(s, &is_copy);
Fred Shih56890e22014-06-02 11:11:52 -07001569 EXPECT_EQ(JNI_FALSE, is_copy);
Elliott Hughes75770752011-08-24 17:52:38 -07001570 EXPECT_EQ(expected[0], chars[0]);
1571 EXPECT_EQ(expected[1], chars[1]);
1572 EXPECT_EQ(expected[2], chars[2]);
1573 EXPECT_EQ(expected[3], chars[3]);
1574 EXPECT_EQ(expected[4], chars[4]);
1575 env_->ReleaseStringCritical(s, chars);
1576}
1577
Elliott Hughes814e4032011-08-23 12:07:56 -07001578TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) {
Elliott Hughesb264f082012-04-06 17:10:10 -07001579 jclass java_lang_Class = env_->FindClass("java/lang/Class");
Ian Rogers2d10b202014-05-12 19:15:18 -07001580 ASSERT_TRUE(java_lang_Class != nullptr);
Elliott Hughes289da822011-08-16 10:11:20 -07001581
Ian Rogers2d10b202014-05-12 19:15:18 -07001582 jobjectArray array = env_->NewObjectArray(1, java_lang_Class, nullptr);
1583 EXPECT_NE(array, nullptr);
1584 EXPECT_EQ(env_->GetObjectArrayElement(array, 0), nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -07001585 env_->SetObjectArrayElement(array, 0, java_lang_Class);
1586 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class));
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001587
1588 // ArrayIndexOutOfBounds for negative index.
Elliott Hughesb264f082012-04-06 17:10:10 -07001589 env_->SetObjectArrayElement(array, -1, java_lang_Class);
Ian Rogers2d10b202014-05-12 19:15:18 -07001590 ExpectException(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001591
1592 // ArrayIndexOutOfBounds for too-large index.
Elliott Hughesb264f082012-04-06 17:10:10 -07001593 env_->SetObjectArrayElement(array, 1, java_lang_Class);
Ian Rogers2d10b202014-05-12 19:15:18 -07001594 ExpectException(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001595
Elliott Hughesb264f082012-04-06 17:10:10 -07001596 // ArrayStoreException thrown for bad types.
1597 env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!"));
Ian Rogers2d10b202014-05-12 19:15:18 -07001598 ExpectException(ase_);
1599
1600 // Null as array should fail.
1601 CheckJniAbortCatcher jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001602 bool old_check_jni = vm_->SetCheckJniEnabled(false);
Ian Rogers2d10b202014-05-12 19:15:18 -07001603 EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0));
1604 jni_abort_catcher.Check("java_array == null");
1605 env_->SetObjectArrayElement(nullptr, 0, nullptr);
1606 jni_abort_catcher.Check("java_array == null");
Ian Rogers68d8b422014-07-17 11:09:10 -07001607 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1608 EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0));
1609 jni_abort_catcher.Check("jarray was NULL");
1610 env_->SetObjectArrayElement(nullptr, 0, nullptr);
1611 jni_abort_catcher.Check("jarray was NULL");
1612 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughes289da822011-08-16 10:11:20 -07001613}
1614
Ian Rogers647b1a82014-10-10 11:02:11 -07001615#define EXPECT_STATIC_PRIMITIVE_FIELD(expect_eq, type, field_name, sig, value1, value2) \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 do { \
1617 jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001618 EXPECT_NE(fid, nullptr); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001619 env_->SetStatic ## type ## Field(c, fid, value1); \
Ian Rogers647b1a82014-10-10 11:02:11 -07001620 expect_eq(value1, env_->GetStatic ## type ## Field(c, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 env_->SetStatic ## type ## Field(c, fid, value2); \
Ian Rogers647b1a82014-10-10 11:02:11 -07001622 expect_eq(value2, env_->GetStatic ## type ## Field(c, fid)); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001623 \
Ian Rogers68d8b422014-07-17 11:09:10 -07001624 bool old_check_jni = vm_->SetCheckJniEnabled(false); \
1625 { \
1626 CheckJniAbortCatcher jni_abort_catcher; \
1627 env_->GetStatic ## type ## Field(nullptr, fid); \
1628 env_->SetStatic ## type ## Field(nullptr, fid, value1); \
1629 } \
Ian Rogers2d10b202014-05-12 19:15:18 -07001630 CheckJniAbortCatcher jni_abort_catcher; \
Ian Rogers2d10b202014-05-12 19:15:18 -07001631 env_->GetStatic ## type ## Field(c, nullptr); \
1632 jni_abort_catcher.Check("fid == null"); \
1633 env_->SetStatic ## type ## Field(c, nullptr, value1); \
1634 jni_abort_catcher.Check("fid == null"); \
Ian Rogers68d8b422014-07-17 11:09:10 -07001635 \
1636 EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \
1637 env_->GetStatic ## type ## Field(nullptr, fid); \
1638 jni_abort_catcher.Check("received NULL jclass"); \
1639 env_->SetStatic ## type ## Field(nullptr, fid, value1); \
1640 jni_abort_catcher.Check("received NULL jclass"); \
1641 env_->GetStatic ## type ## Field(c, nullptr); \
1642 jni_abort_catcher.Check("jfieldID was NULL"); \
1643 env_->SetStatic ## type ## Field(c, nullptr, value1); \
1644 jni_abort_catcher.Check("jfieldID was NULL"); \
1645 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 } while (false)
1647
Ian Rogers647b1a82014-10-10 11:02:11 -07001648#define EXPECT_PRIMITIVE_FIELD(expect_eq, instance, type, field_name, sig, value1, value2) \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001649 do { \
1650 jfieldID fid = env_->GetFieldID(c, field_name, sig); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001651 EXPECT_NE(fid, nullptr); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001652 env_->Set ## type ## Field(instance, fid, value1); \
Ian Rogers647b1a82014-10-10 11:02:11 -07001653 expect_eq(value1, env_->Get ## type ## Field(instance, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 env_->Set ## type ## Field(instance, fid, value2); \
Ian Rogers647b1a82014-10-10 11:02:11 -07001655 expect_eq(value2, env_->Get ## type ## Field(instance, fid)); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001656 \
Ian Rogers68d8b422014-07-17 11:09:10 -07001657 bool old_check_jni = vm_->SetCheckJniEnabled(false); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001658 CheckJniAbortCatcher jni_abort_catcher; \
1659 env_->Get ## type ## Field(nullptr, fid); \
1660 jni_abort_catcher.Check("obj == null"); \
1661 env_->Set ## type ## Field(nullptr, fid, value1); \
1662 jni_abort_catcher.Check("obj == null"); \
1663 env_->Get ## type ## Field(instance, nullptr); \
1664 jni_abort_catcher.Check("fid == null"); \
1665 env_->Set ## type ## Field(instance, nullptr, value1); \
1666 jni_abort_catcher.Check("fid == null"); \
Ian Rogers68d8b422014-07-17 11:09:10 -07001667 EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \
1668 env_->Get ## type ## Field(nullptr, fid); \
1669 jni_abort_catcher.Check("field operation on NULL object:"); \
1670 env_->Set ## type ## Field(nullptr, fid, value1); \
1671 jni_abort_catcher.Check("field operation on NULL object:"); \
1672 env_->Get ## type ## Field(instance, nullptr); \
1673 jni_abort_catcher.Check("jfieldID was NULL"); \
1674 env_->Set ## type ## Field(instance, nullptr, value1); \
1675 jni_abort_catcher.Check("jfieldID was NULL"); \
1676 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001677 } while (false)
1678
1679
1680TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 Thread::Current()->TransitionFromSuspendedToRunnable();
1682 LoadDex("AllFields");
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07001683 bool started = runtime_->Start();
Ian Rogers2d10b202014-05-12 19:15:18 -07001684 ASSERT_TRUE(started);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001685
1686 jclass c = env_->FindClass("AllFields");
Ian Rogers2d10b202014-05-12 19:15:18 -07001687 ASSERT_NE(c, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001688 jobject o = env_->AllocObject(c);
Ian Rogers2d10b202014-05-12 19:15:18 -07001689 ASSERT_NE(o, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001690
Ian Rogers647b1a82014-10-10 11:02:11 -07001691 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Boolean, "sZ", "Z", JNI_TRUE, JNI_FALSE);
1692 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Byte, "sB", "B", 1, 2);
1693 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Char, "sC", "C", 'a', 'b');
1694 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_DOUBLE_EQ, Double, "sD", "D", 1.0, 2.0);
1695 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_FLOAT_EQ, Float, "sF", "F", 1.0, 2.0);
1696 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Int, "sI", "I", 1, 2);
1697 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Long, "sJ", "J", 1, 2);
1698 EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Short, "sS", "S", 1, 2);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001699
Ian Rogers647b1a82014-10-10 11:02:11 -07001700 EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Boolean, "iZ", "Z", JNI_TRUE, JNI_FALSE);
1701 EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Byte, "iB", "B", 1, 2);
1702 EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Char, "iC", "C", 'a', 'b');
1703 EXPECT_PRIMITIVE_FIELD(EXPECT_DOUBLE_EQ, o, Double, "iD", "D", 1.0, 2.0);
1704 EXPECT_PRIMITIVE_FIELD(EXPECT_FLOAT_EQ, o, Float, "iF", "F", 1.0, 2.0);
1705 EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Int, "iI", "I", 1, 2);
1706 EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Long, "iJ", "J", 1, 2);
1707 EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Short, "iS", "S", 1, 2);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001708}
1709
1710TEST_F(JniInternalTest, GetObjectField_SetObjectField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001711 Thread::Current()->TransitionFromSuspendedToRunnable();
1712 LoadDex("AllFields");
Brian Carlstrom25c33252011-09-18 15:58:35 -07001713 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001714
1715 jclass c = env_->FindClass("AllFields");
Ian Rogers2d10b202014-05-12 19:15:18 -07001716 ASSERT_NE(c, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001717 jobject o = env_->AllocObject(c);
Ian Rogers2d10b202014-05-12 19:15:18 -07001718 ASSERT_NE(o, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001719
1720 jstring s1 = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001721 ASSERT_NE(s1, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001722 jstring s2 = env_->NewStringUTF("world");
Ian Rogers2d10b202014-05-12 19:15:18 -07001723 ASSERT_NE(s2, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001724
1725 jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;");
Ian Rogers2d10b202014-05-12 19:15:18 -07001726 ASSERT_NE(s_fid, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001727 jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;");
Ian Rogers2d10b202014-05-12 19:15:18 -07001728 ASSERT_NE(i_fid, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001729
1730 env_->SetStaticObjectField(c, s_fid, s1);
1731 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid)));
1732 env_->SetStaticObjectField(c, s_fid, s2);
1733 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid)));
1734
1735 env_->SetObjectField(o, i_fid, s1);
1736 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid)));
1737 env_->SetObjectField(o, i_fid, s2);
1738 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid)));
1739}
1740
Ian Rogers2d10b202014-05-12 19:15:18 -07001741TEST_F(JniInternalTest, NewLocalRef_nullptr) {
1742 EXPECT_EQ(env_->NewLocalRef(nullptr), nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001743}
1744
1745TEST_F(JniInternalTest, NewLocalRef) {
1746 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001747 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001748 jobject o = env_->NewLocalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001749 EXPECT_NE(o, nullptr);
1750 EXPECT_NE(o, s);
Elliott Hughes18c07532011-08-18 15:50:51 -07001751
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001752 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o));
Elliott Hughes18c07532011-08-18 15:50:51 -07001753}
1754
Ian Rogers2d10b202014-05-12 19:15:18 -07001755TEST_F(JniInternalTest, DeleteLocalRef_nullptr) {
1756 env_->DeleteLocalRef(nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001757}
1758
1759TEST_F(JniInternalTest, DeleteLocalRef) {
Andreas Gampe369810a2015-01-14 19:53:31 -08001760 // This tests leads to warnings and errors in the log.
1761 ScopedLogSeverity sls(LogSeverity::FATAL);
1762
Elliott Hughes18c07532011-08-18 15:50:51 -07001763 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001764 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001765 env_->DeleteLocalRef(s);
1766
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001767 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001768 {
Ian Rogers68d8b422014-07-17 11:09:10 -07001769 bool old_check_jni = vm_->SetCheckJniEnabled(false);
1770 {
1771 CheckJniAbortCatcher check_jni_abort_catcher;
1772 env_->DeleteLocalRef(s);
1773 }
Elliott Hughesb264f082012-04-06 17:10:10 -07001774 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001775 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
Elliott Hughesb264f082012-04-06 17:10:10 -07001776 env_->DeleteLocalRef(s);
Ian Rogersc0542af2014-09-03 16:16:56 -07001777 std::string expected(StringPrintf("use of deleted local reference %p", s));
Elliott Hughesa9137c62013-01-09 10:55:21 -08001778 check_jni_abort_catcher.Check(expected.c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001779 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughesb264f082012-04-06 17:10:10 -07001780 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001781
1782 s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001783 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001784 jobject o = env_->NewLocalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001785 ASSERT_NE(o, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001786
1787 env_->DeleteLocalRef(s);
1788 env_->DeleteLocalRef(o);
1789}
1790
Elliott Hughesaa836f72013-08-20 16:57:23 -07001791TEST_F(JniInternalTest, PushLocalFrame_10395422) {
1792 // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a
1793 // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how
1794 // Android historically treated it, and it's how the RI treats it. It's also the more useful
1795 // interpretation!
1796 ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0));
Ian Rogers2d10b202014-05-12 19:15:18 -07001797 env_->PopLocalFrame(nullptr);
Elliott Hughesaa836f72013-08-20 16:57:23 -07001798
Andreas Gampe369810a2015-01-14 19:53:31 -08001799 // The following two tests will print errors to the log.
1800 ScopedLogSeverity sls(LogSeverity::FATAL);
1801
Elliott Hughesaa836f72013-08-20 16:57:23 -07001802 // Negative capacities are not allowed.
1803 ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1));
1804
1805 // And it's okay to have an upper limit. Ours is currently 512.
1806 ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192));
1807}
1808
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001809TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) {
Andreas Gampe369810a2015-01-14 19:53:31 -08001810 // This tests leads to errors in the log.
1811 ScopedLogSeverity sls(LogSeverity::FATAL);
1812
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001813 jobject original = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001814 ASSERT_NE(original, nullptr);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001815
1816 jobject outer;
1817 jobject inner1, inner2;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env_);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001819 {
Elliott Hughesaa836f72013-08-20 16:57:23 -07001820 ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001821 outer = env_->NewLocalRef(original);
1822
1823 {
Elliott Hughesaa836f72013-08-20 16:57:23 -07001824 ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001825 inner1 = env_->NewLocalRef(outer);
1826 inner2 = env_->NewStringUTF("survivor");
Ian Rogersc0542af2014-09-03 16:16:56 -07001827 EXPECT_NE(env_->PopLocalFrame(inner2), nullptr);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001828 }
1829
1830 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
1831 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer));
Ian Rogersc0542af2014-09-03 16:16:56 -07001832 {
1833 CheckJniAbortCatcher check_jni_abort_catcher;
1834 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
1835 check_jni_abort_catcher.Check("use of deleted local reference");
1836 }
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001837
1838 // Our local reference for the survivor is invalid because the survivor
1839 // gets a new local reference...
Ian Rogersc0542af2014-09-03 16:16:56 -07001840 {
1841 CheckJniAbortCatcher check_jni_abort_catcher;
1842 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
1843 check_jni_abort_catcher.Check("use of deleted local reference");
1844 }
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001845
Ian Rogersc0542af2014-09-03 16:16:56 -07001846 EXPECT_EQ(env_->PopLocalFrame(nullptr), nullptr);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001847 }
1848 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
Ian Rogersc0542af2014-09-03 16:16:56 -07001849 CheckJniAbortCatcher check_jni_abort_catcher;
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001850 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer));
Ian Rogersc0542af2014-09-03 16:16:56 -07001851 check_jni_abort_catcher.Check("use of deleted local reference");
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001852 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
Ian Rogersc0542af2014-09-03 16:16:56 -07001853 check_jni_abort_catcher.Check("use of deleted local reference");
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001854 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
Ian Rogersc0542af2014-09-03 16:16:56 -07001855 check_jni_abort_catcher.Check("use of deleted local reference");
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001856}
1857
Ian Rogers2d10b202014-05-12 19:15:18 -07001858TEST_F(JniInternalTest, NewGlobalRef_nullptr) {
1859 EXPECT_EQ(env_->NewGlobalRef(nullptr), nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001860}
1861
1862TEST_F(JniInternalTest, NewGlobalRef) {
1863 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001864 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001865 jobject o = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001866 EXPECT_NE(o, nullptr);
1867 EXPECT_NE(o, s);
Elliott Hughes18c07532011-08-18 15:50:51 -07001868
Ian Rogers2d10b202014-05-12 19:15:18 -07001869 EXPECT_EQ(env_->GetObjectRefType(o), JNIGlobalRefType);
Elliott Hughes18c07532011-08-18 15:50:51 -07001870}
1871
Ian Rogers2d10b202014-05-12 19:15:18 -07001872TEST_F(JniInternalTest, DeleteGlobalRef_nullptr) {
1873 env_->DeleteGlobalRef(nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001874}
1875
1876TEST_F(JniInternalTest, DeleteGlobalRef) {
Andreas Gampe369810a2015-01-14 19:53:31 -08001877 // This tests leads to warnings and errors in the log.
1878 ScopedLogSeverity sls(LogSeverity::FATAL);
1879
Elliott Hughes18c07532011-08-18 15:50:51 -07001880 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001881 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001882
1883 jobject o = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001884 ASSERT_NE(o, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001885 env_->DeleteGlobalRef(o);
1886
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001887 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001888 {
Ian Rogers68d8b422014-07-17 11:09:10 -07001889 bool old_check_jni = vm_->SetCheckJniEnabled(false);
1890 {
1891 CheckJniAbortCatcher check_jni_abort_catcher;
1892 env_->DeleteGlobalRef(o);
1893 }
Elliott Hughesb264f082012-04-06 17:10:10 -07001894 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001895 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
Elliott Hughesb264f082012-04-06 17:10:10 -07001896 env_->DeleteGlobalRef(o);
Ian Rogersc0542af2014-09-03 16:16:56 -07001897 std::string expected(StringPrintf("use of deleted global reference %p", o));
Elliott Hughesa9137c62013-01-09 10:55:21 -08001898 check_jni_abort_catcher.Check(expected.c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001899 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughesb264f082012-04-06 17:10:10 -07001900 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001901
1902 jobject o1 = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001903 ASSERT_NE(o1, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001904 jobject o2 = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001905 ASSERT_NE(o2, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001906
1907 env_->DeleteGlobalRef(o1);
1908 env_->DeleteGlobalRef(o2);
1909}
1910
Ian Rogers2d10b202014-05-12 19:15:18 -07001911TEST_F(JniInternalTest, NewWeakGlobalRef_nullptr) {
1912 EXPECT_EQ(env_->NewWeakGlobalRef(nullptr), nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001913}
1914
1915TEST_F(JniInternalTest, NewWeakGlobalRef) {
1916 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001917 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001918 jobject o = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001919 EXPECT_NE(o, nullptr);
1920 EXPECT_NE(o, s);
Elliott Hughes18c07532011-08-18 15:50:51 -07001921
Ian Rogers2d10b202014-05-12 19:15:18 -07001922 EXPECT_EQ(env_->GetObjectRefType(o), JNIWeakGlobalRefType);
Elliott Hughes18c07532011-08-18 15:50:51 -07001923}
1924
Ian Rogers2d10b202014-05-12 19:15:18 -07001925TEST_F(JniInternalTest, DeleteWeakGlobalRef_nullptr) {
1926 env_->DeleteWeakGlobalRef(nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001927}
1928
1929TEST_F(JniInternalTest, DeleteWeakGlobalRef) {
Andreas Gampe369810a2015-01-14 19:53:31 -08001930 // This tests leads to warnings and errors in the log.
1931 ScopedLogSeverity sls(LogSeverity::FATAL);
1932
Elliott Hughes18c07532011-08-18 15:50:51 -07001933 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001934 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001935
1936 jobject o = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001937 ASSERT_NE(o, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001938 env_->DeleteWeakGlobalRef(o);
1939
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001940 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001941 {
Ian Rogers68d8b422014-07-17 11:09:10 -07001942 bool old_check_jni = vm_->SetCheckJniEnabled(false);
1943 {
1944 CheckJniAbortCatcher check_jni_abort_catcher;
1945 env_->DeleteWeakGlobalRef(o);
1946 }
Elliott Hughesb264f082012-04-06 17:10:10 -07001947 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers68d8b422014-07-17 11:09:10 -07001948 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
Elliott Hughesb264f082012-04-06 17:10:10 -07001949 env_->DeleteWeakGlobalRef(o);
Ian Rogersc0542af2014-09-03 16:16:56 -07001950 std::string expected(StringPrintf("use of deleted weak global reference %p", o));
Elliott Hughesa9137c62013-01-09 10:55:21 -08001951 check_jni_abort_catcher.Check(expected.c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001952 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughesb264f082012-04-06 17:10:10 -07001953 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001954
1955 jobject o1 = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 ASSERT_NE(o1, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001957 jobject o2 = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001958 ASSERT_NE(o2, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001959
1960 env_->DeleteWeakGlobalRef(o1);
1961 env_->DeleteWeakGlobalRef(o2);
1962}
1963
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +07001964TEST_F(JniInternalTest, ExceptionDescribe) {
1965 // This checks how ExceptionDescribe handles call without exception.
1966 env_->ExceptionClear();
1967 env_->ExceptionDescribe();
1968}
1969
Elliott Hughes37f7a402011-08-22 18:56:01 -07001970TEST_F(JniInternalTest, Throw) {
Elliott Hughes37f7a402011-08-22 18:56:01 -07001971 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
Ian Rogers2d10b202014-05-12 19:15:18 -07001972 ASSERT_TRUE(exception_class != nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001973 jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class));
Ian Rogers2d10b202014-05-12 19:15:18 -07001974 ASSERT_TRUE(exception != nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001975
1976 EXPECT_EQ(JNI_OK, env_->Throw(exception));
1977 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughesa2501992011-08-26 19:39:54 -07001978 jthrowable thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001979 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001980 EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception));
Ian Rogers68d8b422014-07-17 11:09:10 -07001981
1982 // Bad argument.
1983 bool old_check_jni = vm_->SetCheckJniEnabled(false);
1984 EXPECT_EQ(JNI_ERR, env_->Throw(nullptr));
1985 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
1986 CheckJniAbortCatcher check_jni_abort_catcher;
1987 EXPECT_EQ(JNI_ERR, env_->Throw(nullptr));
1988 check_jni_abort_catcher.Check("Throw received NULL jthrowable");
1989 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001990}
1991
1992TEST_F(JniInternalTest, ThrowNew) {
Elliott Hughes37f7a402011-08-22 18:56:01 -07001993 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
Ian Rogers2d10b202014-05-12 19:15:18 -07001994 ASSERT_TRUE(exception_class != nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001995
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001996 jthrowable thrown_exception;
1997
Elliott Hughes37f7a402011-08-22 18:56:01 -07001998 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world"));
1999 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002000 thrown_exception = env_->ExceptionOccurred();
2001 env_->ExceptionClear();
2002 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
2003
Ian Rogers2d10b202014-05-12 19:15:18 -07002004 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, nullptr));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002005 EXPECT_TRUE(env_->ExceptionCheck());
2006 thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07002007 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07002008 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
Ian Rogers68d8b422014-07-17 11:09:10 -07002009
2010 // Bad argument.
2011 bool old_check_jni = vm_->SetCheckJniEnabled(false);
2012 CheckJniAbortCatcher check_jni_abort_catcher;
2013 EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr));
2014 check_jni_abort_catcher.Check("c == null");
2015 EXPECT_FALSE(vm_->SetCheckJniEnabled(true));
2016 EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr));
2017 check_jni_abort_catcher.Check("ThrowNew received NULL jclass");
2018 EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni));
Elliott Hughes37f7a402011-08-22 18:56:01 -07002019}
2020
Ian Rogers1d99e452014-01-02 17:36:41 -08002021TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) {
2022 // Start runtime.
2023 Thread* self = Thread::Current();
2024 self->TransitionFromSuspendedToRunnable();
2025 MakeExecutable(nullptr, "java.lang.Class");
2026 MakeExecutable(nullptr, "java.lang.Object");
2027 MakeExecutable(nullptr, "java.nio.DirectByteBuffer");
2028 MakeExecutable(nullptr, "java.nio.MemoryBlock");
2029 MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock");
2030 MakeExecutable(nullptr, "java.nio.MappedByteBuffer");
2031 MakeExecutable(nullptr, "java.nio.ByteBuffer");
2032 MakeExecutable(nullptr, "java.nio.Buffer");
2033 // TODO: we only load a dex file here as starting the runtime relies upon it.
2034 const char* class_name = "StaticLeafMethods";
2035 LoadDex(class_name);
2036 bool started = runtime_->Start();
2037 ASSERT_TRUE(started);
2038
Elliott Hughesb465ab02011-08-24 11:21:21 -07002039 jclass buffer_class = env_->FindClass("java/nio/Buffer");
Ian Rogers2d10b202014-05-12 19:15:18 -07002040 ASSERT_NE(buffer_class, nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002041
2042 char bytes[1024];
2043 jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes));
Ian Rogers2d10b202014-05-12 19:15:18 -07002044 ASSERT_NE(buffer, nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002045 ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class));
Ian Rogers2d10b202014-05-12 19:15:18 -07002046 ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes);
Ian Rogers1ee99352014-05-14 14:38:16 -07002047 ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes)));
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002048
2049 {
2050 CheckJniAbortCatcher check_jni_abort_catcher;
Brian Carlstrom85a93362014-06-25 09:30:52 -07002051 env_->NewDirectByteBuffer(bytes, static_cast<jlong>(INT_MAX) + 1);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002052 check_jni_abort_catcher.Check("in call to NewDirectByteBuffer");
2053 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002054}
2055
Ian Rogers6d0b13e2012-02-07 09:25:29 -08002056TEST_F(JniInternalTest, MonitorEnterExit) {
Andreas Gampe369810a2015-01-14 19:53:31 -08002057 // This will print some error messages. Suppress.
2058 ScopedLogSeverity sls(LogSeverity::FATAL);
2059
Ian Rogers2d10b202014-05-12 19:15:18 -07002060 // Create an object to torture.
Ian Rogers6d0b13e2012-02-07 09:25:29 -08002061 jclass object_class = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -07002062 ASSERT_NE(object_class, nullptr);
Ian Rogers6d0b13e2012-02-07 09:25:29 -08002063 jobject object = env_->AllocObject(object_class);
Ian Rogers2d10b202014-05-12 19:15:18 -07002064 ASSERT_NE(object, nullptr);
Ian Rogers6d0b13e2012-02-07 09:25:29 -08002065
2066 // Expected class of exceptions
2067 jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException");
Ian Rogers2d10b202014-05-12 19:15:18 -07002068 ASSERT_NE(imse_class, nullptr);
Ian Rogers6d0b13e2012-02-07 09:25:29 -08002069
2070 jthrowable thrown_exception;
2071
2072 // Unlock of unowned monitor
2073 env_->MonitorExit(object);
2074 EXPECT_TRUE(env_->ExceptionCheck());
2075 thrown_exception = env_->ExceptionOccurred();
2076 env_->ExceptionClear();
2077 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
2078
2079 // Lock of unowned monitor
2080 env_->MonitorEnter(object);
2081 EXPECT_FALSE(env_->ExceptionCheck());
2082 // Regular unlock
2083 env_->MonitorExit(object);
2084 EXPECT_FALSE(env_->ExceptionCheck());
2085
2086 // Recursively lock a lot
2087 size_t max_recursive_lock = 1024;
2088 for (size_t i = 0; i < max_recursive_lock; i++) {
2089 env_->MonitorEnter(object);
2090 EXPECT_FALSE(env_->ExceptionCheck());
2091 }
2092 // Recursively unlock a lot
2093 for (size_t i = 0; i < max_recursive_lock; i++) {
2094 env_->MonitorExit(object);
2095 EXPECT_FALSE(env_->ExceptionCheck());
2096 }
2097
2098 // Unlock of unowned monitor
2099 env_->MonitorExit(object);
2100 EXPECT_TRUE(env_->ExceptionCheck());
2101 thrown_exception = env_->ExceptionOccurred();
2102 env_->ExceptionClear();
2103 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
Elliott Hughesa92853e2012-02-07 16:09:27 -08002104
Ian Rogers2d10b202014-05-12 19:15:18 -07002105 // It's an error to call MonitorEnter or MonitorExit on nullptr.
Elliott Hughesb264f082012-04-06 17:10:10 -07002106 {
2107 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers2d10b202014-05-12 19:15:18 -07002108 env_->MonitorEnter(nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -07002109 check_jni_abort_catcher.Check("in call to MonitorEnter");
Ian Rogers2d10b202014-05-12 19:15:18 -07002110 env_->MonitorExit(nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -07002111 check_jni_abort_catcher.Check("in call to MonitorExit");
2112 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -08002113}
2114
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002115} // namespace art