• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 #include <type_traits>
19 
20 #include <math.h>
21 
22 #include "art_method-inl.h"
23 #include "base/bit_utils.h"
24 #include "base/casts.h"
25 #include "base/mem_map.h"
26 #include "class_linker.h"
27 #include "common_compiler_test.h"
28 #include "compiler.h"
29 #include "dex/dex_file.h"
30 #include "entrypoints/entrypoint_utils-inl.h"
31 #include "gtest/gtest.h"
32 #include "indirect_reference_table.h"
33 #include "java_frame_root_info.h"
34 #include "jni/java_vm_ext.h"
35 #include "jni/jni_internal.h"
36 #include "mirror/class-inl.h"
37 #include "mirror/class_loader.h"
38 #include "mirror/object-inl.h"
39 #include "mirror/object_array-inl.h"
40 #include "mirror/stack_trace_element-inl.h"
41 #include "nativehelper/ScopedLocalRef.h"
42 #include "nativeloader/native_loader.h"
43 #include "oat_quick_method_header.h"
44 #include "runtime.h"
45 #include "scoped_thread_state_change-inl.h"
46 #include "thread.h"
47 
Java_MyClassNatives_bar(JNIEnv *,jobject,jint count)48 extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_bar(JNIEnv*, jobject, jint count) {
49   return count + 1;
50 }
51 
52 // Note: JNI name mangling "_" -> "_1".
Java_MyClassNatives_bar_1Fast(JNIEnv *,jobject,jint count)53 extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_bar_1Fast(JNIEnv*, jobject, jint count) {
54   return count + 1;
55 }
56 
Java_MyClassNatives_sbar(JNIEnv *,jclass,jint count)57 extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_sbar(JNIEnv*, jclass, jint count) {
58   return count + 1;
59 }
60 
61 // Note: JNI name mangling "_" -> "_1".
Java_MyClassNatives_sbar_1Fast(JNIEnv *,jclass,jint count)62 extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_sbar_1Fast(JNIEnv*, jclass, jint count) {
63   return count + 1;
64 }
65 
66 // Note: JNI name mangling "_" -> "_1".
Java_MyClassNatives_sbar_1Critical(jint count)67 extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_sbar_1Critical(jint count) {
68   return count + 1;
69 }
70 
71 // TODO: In the Baker read barrier configuration, add checks to ensure
72 // the Marking Register's value is correct.
73 
74 namespace art {
75 
76 enum class JniKind {
77   kNormal,      // Regular kind of un-annotated natives.
78   kFast,        // Native method annotated with @FastNative.
79   kCritical,    // Native method annotated with @CriticalNative.
80   kCount        // How many different types of JNIs we can have.
81 };
82 
83 // Used to initialize array sizes that want to have different state per current jni.
84 static constexpr size_t kJniKindCount = static_cast<size_t>(JniKind::kCount);
85 // Do not use directly, use the helpers instead.
86 uint32_t gCurrentJni = static_cast<uint32_t>(JniKind::kNormal);
87 
88 // Is the current native method under test @CriticalNative?
IsCurrentJniCritical()89 static bool IsCurrentJniCritical() {
90   return gCurrentJni == static_cast<uint32_t>(JniKind::kCritical);
91 }
92 
93 // Is the current native method under test @FastNative?
IsCurrentJniFast()94 static bool IsCurrentJniFast() {
95   return gCurrentJni == static_cast<uint32_t>(JniKind::kFast);
96 }
97 
98 // Is the current native method a plain-old non-annotated native?
IsCurrentJniNormal()99 static bool IsCurrentJniNormal() {
100   return gCurrentJni == static_cast<uint32_t>(JniKind::kNormal);
101 }
102 
103 // Signify that a different kind of JNI is about to be tested.
UpdateCurrentJni(JniKind kind)104 static void UpdateCurrentJni(JniKind kind) {
105   gCurrentJni = static_cast<uint32_t>(kind);
106 }
107 
108 // (Match the name suffixes of native methods in MyClassNatives.java)
CurrentJniStringSuffix()109 static std::string CurrentJniStringSuffix() {
110   switch (gCurrentJni) {
111     case static_cast<uint32_t>(JniKind::kNormal): {
112       return "";
113     }
114     case static_cast<uint32_t>(JniKind::kFast): {
115       return "_Fast";
116     }
117     case static_cast<uint32_t>(JniKind::kCritical): {
118       return "_Critical";
119     }
120     default:
121       LOG(FATAL) << "Invalid current JNI value: " << gCurrentJni;
122       UNREACHABLE();
123   }
124 }
125 
126 // Fake values passed to our JNI handlers when we enter @CriticalNative.
127 // Normally @CriticalNative calling convention strips out the "JNIEnv*, jclass" parameters.
128 // However to avoid duplicating every single test method we have a templated handler
129 // that inserts fake parameters (0,1) to make it compatible with a regular JNI handler.
130 static JNIEnv* const kCriticalFakeJniEnv = reinterpret_cast<JNIEnv*>(0xDEADFEAD);
131 static jclass const kCriticalFakeJniClass = reinterpret_cast<jclass>(0xBEAFBEEF);
132 
133 // Type trait. Returns true if "T" is the same type as one of the types in Args...
134 //
135 // Logically equal to OR(std::same_type<T, U> for all U in Args).
136 template <typename T, typename ... Args>
137 struct is_any_of;
138 
139 template <typename T, typename U, typename ... Args>
140 struct is_any_of<T, U, Args ...> {
141   using value_type = bool;
142   static constexpr const bool value = std::is_same<T, U>::value || is_any_of<T, Args ...>::value;
143 };
144 
145 template <typename T, typename U>
146 struct is_any_of<T, U> {
147   using value_type = bool;
148   static constexpr const bool value = std::is_same<T, U>::value;
149 };
150 
151 // Type traits for JNI types.
152 template <typename T>
153 struct jni_type_traits {
154   // True if type T ends up holding an object reference. False otherwise.
155   // (Non-JNI types will also be false).
156   static constexpr const bool is_ref =
157       is_any_of<T, jclass, jobject, jstring, jobjectArray, jintArray,
158                 jcharArray, jfloatArray, jshortArray, jdoubleArray, jlongArray>::value;
159 };
160 
161 // Base case: No parameters = 0 refs.
count_nonnull_refs_helper()162 size_t count_nonnull_refs_helper() {
163   return 0;
164 }
165 
166 // SFINAE for ref types. 1 if non-null, 0 otherwise.
167 template <typename T>
count_nonnull_refs_single_helper(T arg,typename std::enable_if<jni_type_traits<T>::is_ref>::type * =nullptr)168 size_t count_nonnull_refs_single_helper(T arg,
169                                         typename std::enable_if<jni_type_traits<T>::is_ref>::type*
170                                             = nullptr) {
171   return ((arg == NULL) ? 0 : 1);
172 }
173 
174 // SFINAE for non-ref-types. Always 0.
175 template <typename T>
count_nonnull_refs_single_helper(T arg ATTRIBUTE_UNUSED,typename std::enable_if<!jni_type_traits<T>::is_ref>::type * =nullptr)176 size_t count_nonnull_refs_single_helper(T arg ATTRIBUTE_UNUSED,
177                                         typename std::enable_if<!jni_type_traits<T>::is_ref>::type*
178                                             = nullptr) {
179   return 0;
180 }
181 
182 // Recursive case.
183 template <typename T, typename ... Args>
count_nonnull_refs_helper(T arg,Args...args)184 size_t count_nonnull_refs_helper(T arg, Args ... args) {
185   return count_nonnull_refs_single_helper(arg) + count_nonnull_refs_helper(args...);
186 }
187 
188 // Given any list of parameters, check how many object refs there are and only count
189 // them if their runtime value is non-null.
190 //
191 // For example given (jobject, jint, jclass) we can get (2) if both #0/#2 are non-null,
192 // (1) if either #0/#2 are null but not both, and (0) if all parameters are null.
193 // Primitive parameters (including JNIEnv*, if present) are ignored.
194 template <typename ... Args>
count_nonnull_refs(Args...args)195 size_t count_nonnull_refs(Args ... args) {
196   return count_nonnull_refs_helper(args...);
197 }
198 
199 template <typename T, T* fn>
200 struct remove_extra_parameters_helper;
201 
202 template <typename R, typename Arg1, typename Arg2, typename ... Args, R (*fn)(Arg1, Arg2, Args...)>
203 struct remove_extra_parameters_helper<R(Arg1, Arg2, Args...), fn> {
204   // Note: Do not use Args&& here to maintain C-style parameter types.
applyart::remove_extra_parameters_helper205   static R apply(Args... args) {
206     JNIEnv* env = kCriticalFakeJniEnv;
207     jclass kls = kCriticalFakeJniClass;
208     return fn(env, kls, args...);
209   }
210 };
211 
212 // Given a function 'fn' create a function 'apply' which will omit the JNIEnv/jklass parameters
213 //
214 // i.e. if fn(JNIEnv*,jklass,a,b,c,d,e...) then apply(a,b,c,d,e,...)
215 template <typename T, T* fn>
216 struct jni_remove_extra_parameters : public remove_extra_parameters_helper<T, fn> {};
217 
218 class JniCompilerTest : public CommonCompilerTest {
219  protected:
SetUp()220   void SetUp() override {
221     CommonCompilerTest::SetUp();
222     check_generic_jni_ = false;
223   }
224 
TearDown()225   void TearDown() override {
226     android::ResetNativeLoader();
227     CommonCompilerTest::TearDown();
228   }
229 
SetCheckGenericJni(bool generic)230   void SetCheckGenericJni(bool generic) {
231     check_generic_jni_ = generic;
232   }
233 
234  private:
CompileForTest(jobject class_loader,bool direct,const char * method_name,const char * method_sig)235   void CompileForTest(jobject class_loader,
236                       bool direct,
237                       const char* method_name,
238                       const char* method_sig) {
239     ScopedObjectAccess soa(Thread::Current());
240     StackHandleScope<2> hs(soa.Self());
241     Handle<mirror::ClassLoader> loader(
242         hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
243     // Compile the native method before starting the runtime
244     Handle<mirror::Class> c =
245         hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMyClassNatives;", loader));
246     const auto pointer_size = class_linker_->GetImagePointerSize();
247     ArtMethod* method = c->FindClassMethod(method_name, method_sig, pointer_size);
248     ASSERT_TRUE(method != nullptr) << method_name << " " << method_sig;
249     ASSERT_EQ(direct, method->IsDirect()) << method_name << " " << method_sig;
250     if (direct) {
251       // Class initialization could replace the entrypoint, so force
252       // the initialization before we set up the entrypoint below.
253       class_linker_->EnsureInitialized(
254           soa.Self(), c, /*can_init_fields=*/ true, /*can_init_parents=*/ true);
255       class_linker_->MakeInitializedClassesVisiblyInitialized(soa.Self(), /*wait=*/ true);
256     }
257     if (check_generic_jni_) {
258       method->SetEntryPointFromQuickCompiledCode(class_linker_->GetRuntimeQuickGenericJniStub());
259     } else {
260       const void* code = method->GetEntryPointFromQuickCompiledCode();
261       if (code == nullptr || class_linker_->IsQuickGenericJniStub(code)) {
262         CompileMethod(method);
263         ASSERT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr)
264             << method_name << " " << method_sig;
265       }
266     }
267   }
268 
269  protected:
CompileForTestWithCurrentJni(jobject class_loader,bool direct,const char * method_name_orig,const char * method_sig)270   void CompileForTestWithCurrentJni(jobject class_loader,
271                                     bool direct,
272                                     const char* method_name_orig,
273                                     const char* method_sig) {
274     // Append the JNI kind to the method name, so that we automatically get the
275     // fast or critical versions of the same method.
276     std::string method_name_str = std::string(method_name_orig) + CurrentJniStringSuffix();
277     const char* method_name = method_name_str.c_str();
278 
279     CompileForTest(class_loader, direct, method_name, method_sig);
280   }
281 
SetUpForTest(bool direct,const char * method_name_orig,const char * method_sig,void * native_fnptr)282   void SetUpForTest(bool direct,
283                     const char* method_name_orig,
284                     const char* method_sig,
285                     void* native_fnptr) {
286     // Append the JNI kind to the method name, so that we automatically get the
287     // fast or critical versions of the same method.
288     std::string method_name_str = std::string(method_name_orig) + CurrentJniStringSuffix();
289     const char* method_name = method_name_str.c_str();
290 
291     // Initialize class loader and compile method when runtime not started.
292     if (!runtime_->IsStarted()) {
293       {
294         ScopedObjectAccess soa(Thread::Current());
295         class_loader_ = LoadDex("MyClassNatives");
296       }
297       CompileForTest(class_loader_, direct, method_name, method_sig);
298       // Start runtime.
299       Thread::Current()->TransitionFromSuspendedToRunnable();
300       android::InitializeNativeLoader();
301       bool started = runtime_->Start();
302       CHECK(started);
303     }
304     // JNI operations after runtime start.
305     env_ = Thread::Current()->GetJniEnv();
306     jklass_ = env_->FindClass("MyClassNatives");
307     ASSERT_TRUE(jklass_ != nullptr) << method_name << " " << method_sig;
308 
309     if (direct) {
310       jmethod_ = env_->GetStaticMethodID(jklass_, method_name, method_sig);
311     } else {
312       jmethod_ = env_->GetMethodID(jklass_, method_name, method_sig);
313     }
314     ASSERT_TRUE(jmethod_ != nullptr) << method_name << " " << method_sig;
315 
316     // Make sure the test class is visibly initialized so that the RegisterNatives() below
317     // sets the JNI entrypoint rather than leaving it as null (this test pretends to be an
318     // AOT compiler and therefore the ClassLinker skips entrypoint initialization). Even
319     // if the ClassLinker initialized it with a stub, we would not want to test that here.
320     class_linker_->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
321 
322     if (native_fnptr != nullptr) {
323       JNINativeMethod methods[] = { { method_name, method_sig, native_fnptr } };
324       ASSERT_EQ(JNI_OK, env_->RegisterNatives(jklass_, methods, 1))
325               << method_name << " " << method_sig;
326     } else {
327       env_->UnregisterNatives(jklass_);
328     }
329 
330     jmethodID constructor = env_->GetMethodID(jklass_, "<init>", "()V");
331     jobj_ = env_->NewObject(jklass_, constructor);
332     ASSERT_TRUE(jobj_ != nullptr) << method_name << " " << method_sig;
333   }
334 
335  public:
336   // Available as statics so our JNI handlers can access these.
337   static jclass jklass_;
338   static jobject jobj_;
339   static jobject class_loader_;
340 
341   static void AssertCallerObjectLocked(JNIEnv* env);
342 
343   static LockWord GetLockWord(jobject obj);
344 
345  protected:
346   // We have to list the methods here so we can share them between default and generic JNI.
347   void CompileAndRunNoArgMethodImpl();
348   void CompileAndRunIntMethodThroughStubImpl();
349   void CompileAndRunStaticIntMethodThroughStubImpl();
350   void CompileAndRunIntMethodImpl();
351   void CompileAndRunIntIntMethodImpl();
352   void CompileAndRunLongLongMethodImpl();
353   void CompileAndRunDoubleDoubleMethodImpl();
354   void CompileAndRun_fooJJ_synchronizedImpl();
355   void CompileAndRunIntObjectObjectMethodImpl();
356   void CompileAndRunStaticIntIntMethodImpl();
357   void CompileAndRunStaticDoubleDoubleMethodImpl();
358   void RunStaticLogDoubleMethodImpl();
359   void RunStaticLogFloatMethodImpl();
360   void RunStaticReturnTrueImpl();
361   void RunStaticReturnFalseImpl();
362   void RunGenericStaticReturnIntImpl();
363   void RunGenericStaticReturnDoubleImpl();
364   void RunGenericStaticReturnLongImpl();
365   void CompileAndRunStaticIntObjectObjectMethodImpl();
366   void CompileAndRunStaticSynchronizedIntObjectObjectMethodImpl();
367   void ExceptionHandlingImpl();
368   void NativeStackTraceElementImpl();
369   void ReturnGlobalRefImpl();
370   void LocalReferenceTableClearingTestImpl();
371   void JavaLangSystemArrayCopyImpl();
372   void CompareAndSwapIntImpl();
373   void GetTextImpl();
374   void GetSinkPropertiesNativeImpl();
375   void UpcallReturnTypeChecking_InstanceImpl();
376   void UpcallReturnTypeChecking_StaticImpl();
377   void UpcallArgumentTypeChecking_InstanceImpl();
378   void UpcallArgumentTypeChecking_StaticImpl();
379   void CompileAndRunFloatFloatMethodImpl();
380   void CheckParameterAlignImpl();
381   void MaxParamNumberImpl();
382   void WithoutImplementationImpl();
383   void WithoutImplementationRefReturnImpl();
384   void StaticWithoutImplementationImpl();
385   void StackArgsIntsFirstImpl();
386   void StackArgsFloatsFirstImpl();
387   void StackArgsMixedImpl();
388 
389   void NormalNativeImpl();
390   void FastNativeImpl();
391   void CriticalNativeImpl();
392 
393   JNIEnv* env_;
394   jmethodID jmethod_;
395 
396  private:
397   bool check_generic_jni_;
398 };
399 
400 jclass JniCompilerTest::jklass_;
401 jobject JniCompilerTest::jobj_;
402 jobject JniCompilerTest::class_loader_;
403 
AssertCallerObjectLocked(JNIEnv * env)404 void JniCompilerTest::AssertCallerObjectLocked(JNIEnv* env) {
405   Thread* self = down_cast<JNIEnvExt*>(env)->GetSelf();
406   CHECK_EQ(self, Thread::Current());
407   ScopedObjectAccess soa(self);
408   ArtMethod** caller_frame = self->GetManagedStack()->GetTopQuickFrame();
409   CHECK(caller_frame != nullptr);
410   ArtMethod* caller = *caller_frame;
411   CHECK(caller != nullptr);
412   CHECK(caller->IsNative());
413   CHECK(!caller->IsFastNative());
414   CHECK(!caller->IsCriticalNative());
415   CHECK(caller->IsSynchronized());
416   ObjPtr<mirror::Object> lock;
417   if (self->GetManagedStack()->GetTopQuickFrameTag()) {
418     // Generic JNI.
419     lock = GetGenericJniSynchronizationObject(self, caller);
420   } else if (caller->IsStatic()) {
421     lock = caller->GetDeclaringClass();
422   } else {
423     uint8_t* sp = reinterpret_cast<uint8_t*>(caller_frame);
424     const void* code_ptr = EntryPointToCodePointer(caller->GetEntryPointFromQuickCompiledCode());
425     OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
426     size_t frame_size = method_header->GetFrameSizeInBytes();
427     StackReference<mirror::Object>* this_ref = reinterpret_cast<StackReference<mirror::Object>*>(
428         sp + frame_size + static_cast<size_t>(kRuntimePointerSize));
429     lock = this_ref->AsMirrorPtr();
430   }
431   CHECK_EQ(Monitor::GetLockOwnerThreadId(lock), self->GetThreadId());
432 }
433 
GetLockWord(jobject obj)434 LockWord JniCompilerTest::GetLockWord(jobject obj) {
435   ScopedObjectAccess soa(Thread::Current());
436   return soa.Decode<mirror::Object>(obj)->GetLockWord(/*as_volatile=*/ false);
437 }
438 
439 // Test the normal compiler and normal generic JNI only.
440 // The following features are unsupported in @FastNative:
441 // 1) synchronized keyword
442 # define JNI_TEST_NORMAL_ONLY(TestName)          \
443   TEST_F(JniCompilerTest, TestName ## NormalCompiler) { \
444     ScopedCheckHandleScope top_handle_scope_check;  \
445     SCOPED_TRACE("Normal JNI with compiler");    \
446     gCurrentJni = static_cast<uint32_t>(JniKind::kNormal); \
447     TestName ## Impl();                          \
448   }                                              \
449   TEST_F(JniCompilerTest, TestName ## NormalGeneric) { \
450     ScopedCheckHandleScope top_handle_scope_check;  \
451     SCOPED_TRACE("Normal JNI with generic");     \
452     gCurrentJni = static_cast<uint32_t>(JniKind::kNormal); \
453     SetCheckGenericJni(true);                    \
454     TestName ## Impl();                          \
455   }
456 
457 // Test (normal, @FastNative) x (compiler, generic).
458 #define JNI_TEST(TestName) \
459   JNI_TEST_NORMAL_ONLY(TestName)                 \
460   TEST_F(JniCompilerTest, TestName ## FastCompiler) {    \
461     ScopedCheckHandleScope top_handle_scope_check;  \
462     SCOPED_TRACE("@FastNative JNI with compiler");  \
463     gCurrentJni = static_cast<uint32_t>(JniKind::kFast); \
464     TestName ## Impl();                          \
465   }                                              \
466                                                  \
467   TEST_F(JniCompilerTest, TestName ## FastGeneric) { \
468     ScopedCheckHandleScope top_handle_scope_check;  \
469     SCOPED_TRACE("@FastNative JNI with generic");  \
470     gCurrentJni = static_cast<uint32_t>(JniKind::kFast); \
471     SetCheckGenericJni(true);                    \
472     TestName ## Impl();                          \
473   }
474 
475 // Test (@CriticalNative) x (compiler, generic) only.
476 #define JNI_TEST_CRITICAL_ONLY(TestName) \
477   TEST_F(JniCompilerTest, TestName ## CriticalCompiler) { \
478     ScopedCheckHandleScope top_handle_scope_check;  \
479     SCOPED_TRACE("@CriticalNative JNI with compiler");  \
480     gCurrentJni = static_cast<uint32_t>(JniKind::kCritical); \
481     TestName ## Impl();                          \
482   }                                              \
483   TEST_F(JniCompilerTest, TestName ## CriticalGeneric) { \
484     ScopedCheckHandleScope top_handle_scope_check;  \
485     SCOPED_TRACE("@CriticalNative JNI with generic");  \
486     gCurrentJni = static_cast<uint32_t>(JniKind::kCritical); \
487     SetCheckGenericJni(true);                    \
488     TestName ## Impl();                          \
489   }
490 
491 // Test everything: (normal, @FastNative, @CriticalNative) x (compiler, generic).
492 #define JNI_TEST_CRITICAL(TestName)              \
493   JNI_TEST(TestName)                             \
494   JNI_TEST_CRITICAL_ONLY(TestName)               \
495 
expectValidThreadState()496 static void expectValidThreadState() {
497   // Normal JNI always transitions to "Native". Other JNIs stay in the "Runnable" state.
498   if (IsCurrentJniNormal()) {
499     EXPECT_EQ(ThreadState::kNative, Thread::Current()->GetState());
500   } else {
501     EXPECT_EQ(ThreadState::kRunnable, Thread::Current()->GetState());
502   }
503 }
504 
505 #define EXPECT_THREAD_STATE_FOR_CURRENT_JNI() expectValidThreadState()
506 
expectValidMutatorLockHeld()507 static void expectValidMutatorLockHeld() {
508   if (IsCurrentJniNormal()) {
509     Locks::mutator_lock_->AssertNotHeld(Thread::Current());
510   } else {
511     Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
512   }
513 }
514 
515 #define EXPECT_MUTATOR_LOCK_FOR_CURRENT_JNI() expectValidMutatorLockHeld()
516 
expectValidJniEnvAndObject(JNIEnv * env,jobject thisObj)517 static void expectValidJniEnvAndObject(JNIEnv* env, jobject thisObj) {
518   if (!IsCurrentJniCritical()) {
519     EXPECT_EQ(Thread::Current()->GetJniEnv(), env);
520     ASSERT_TRUE(thisObj != nullptr);
521     EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_));
522   } else {
523     LOG(FATAL) << "Objects are not supported for @CriticalNative, why is this being tested?";
524     UNREACHABLE();
525   }
526 }
527 
528 // Validates the JNIEnv to be the same as the current thread's JNIEnv, and makes sure
529 // that the object here is an instance of the class we registered the method with.
530 //
531 // Hard-fails if this somehow gets invoked for @CriticalNative since objects are unsupported.
532 #define EXPECT_JNI_ENV_AND_OBJECT_FOR_CURRENT_JNI(env, thisObj) \
533     expectValidJniEnvAndObject(env, thisObj)
534 
expectValidJniEnvAndClass(JNIEnv * env,jclass kls)535 static void expectValidJniEnvAndClass(JNIEnv* env, jclass kls) {
536   if (!IsCurrentJniCritical()) {
537     EXPECT_EQ(Thread::Current()->GetJniEnv(), env);
538     ASSERT_TRUE(kls != nullptr);
539     EXPECT_TRUE(env->IsSameObject(static_cast<jobject>(JniCompilerTest::jklass_),
540                                   static_cast<jobject>(kls)));
541   } else {
542     // This is pretty much vacuously true but catch any testing setup mistakes.
543     EXPECT_EQ(env, kCriticalFakeJniEnv);
544     EXPECT_EQ(kls, kCriticalFakeJniClass);
545   }
546 }
547 
548 // Validates the JNIEnv is the same as the current thread's JNIenv, and makes sure
549 // that the jclass we got in the JNI handler is the same one as the class the method was looked
550 // up for.
551 //
552 // (Checks are skipped for @CriticalNative since the two values are fake).
553 #define EXPECT_JNI_ENV_AND_CLASS_FOR_CURRENT_JNI(env, kls) expectValidJniEnvAndClass(env, kls)
554 
555 // Temporarily disable the EXPECT_NUM_STACK_REFERENCES check (for a single test).
556 struct ScopedDisableCheckNumStackReferences {
ScopedDisableCheckNumStackReferencesart::ScopedDisableCheckNumStackReferences557   ScopedDisableCheckNumStackReferences() {
558     CHECK(sCheckNumStackReferences);  // No nested support.
559     sCheckNumStackReferences = false;
560   }
561 
~ScopedDisableCheckNumStackReferencesart::ScopedDisableCheckNumStackReferences562   ~ScopedDisableCheckNumStackReferences() {
563     sCheckNumStackReferences = true;
564   }
565 
566   static bool sCheckNumStackReferences;
567 };
568 
569 bool ScopedDisableCheckNumStackReferences::sCheckNumStackReferences = true;
570 
571 // Check that the handle scope at the start of this block is the same
572 // as the handle scope at the end of the block.
573 struct ScopedCheckHandleScope {
ScopedCheckHandleScopeart::ScopedCheckHandleScope574   ScopedCheckHandleScope() : handle_scope_(Thread::Current()->GetTopHandleScope()) {
575   }
576 
~ScopedCheckHandleScopeart::ScopedCheckHandleScope577   ~ScopedCheckHandleScope() {
578     EXPECT_EQ(handle_scope_, Thread::Current()->GetTopHandleScope())
579         << "Top-most handle scope must be the same after all the JNI "
580         << "invocations have finished (as before they were invoked).";
581   }
582 
583   BaseHandleScope* const handle_scope_;
584 };
585 
586 class CountReferencesVisitor : public RootVisitor {
587  public:
VisitRoots(mirror::Object *** roots ATTRIBUTE_UNUSED,size_t count,const RootInfo & info)588   void VisitRoots(mirror::Object*** roots ATTRIBUTE_UNUSED,
589                   size_t count,
590                   const RootInfo& info) override
591       REQUIRES_SHARED(Locks::mutator_lock_) {
592     if (info.GetType() == art::RootType::kRootJavaFrame) {
593       const JavaFrameRootInfo& jrfi = static_cast<const JavaFrameRootInfo&>(info);
594       if (jrfi.GetVReg() == JavaFrameRootInfo::kNativeReferenceArgument) {
595         DCHECK_EQ(count, 1u);
596         num_references_ += count;
597       }
598     }
599   }
600 
VisitRoots(mirror::CompressedReference<mirror::Object> ** roots ATTRIBUTE_UNUSED,size_t count ATTRIBUTE_UNUSED,const RootInfo & info)601   void VisitRoots(mirror::CompressedReference<mirror::Object>** roots ATTRIBUTE_UNUSED,
602                   size_t count ATTRIBUTE_UNUSED,
603                   const RootInfo& info) override
604       REQUIRES_SHARED(Locks::mutator_lock_) {
605     CHECK_NE(info.GetType(), art::RootType::kRootJavaFrame);
606   }
607 
NumReferences() const608   size_t NumReferences() const {
609     return num_references_;
610   }
611 
612  private:
613   size_t num_references_ = 0u;
614 };
615 
616 // Number of references allocated in handle scopes & JNI shadow frames on this thread.
NumStackReferences(Thread * self)617 static size_t NumStackReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
618   CountReferencesVisitor visitor;
619   self->VisitRoots(&visitor, kVisitRootFlagAllRoots);
620   return visitor.NumReferences();
621 }
622 
expectNumStackReferences(size_t expected)623 static void expectNumStackReferences(size_t expected) {
624   // In rare cases when JNI functions call themselves recursively,
625   // disable this test because it will have a false negative.
626   if (!IsCurrentJniCritical() && ScopedDisableCheckNumStackReferences::sCheckNumStackReferences) {
627     /* @CriticalNative doesn't build a HandleScope, so this test is meaningless then. */
628     ScopedObjectAccess soa(Thread::Current());
629 
630     size_t num_references = NumStackReferences(Thread::Current());
631     EXPECT_EQ(expected, num_references);
632   }
633 }
634 
635 #define EXPECT_NUM_STACK_REFERENCES(expected) expectNumStackReferences(expected)
636 
637 template <typename T, T* fn>
638 struct make_jni_test_decorator;
639 
640 // Decorator for "static" JNI callbacks.
641 template <typename R, typename ... Args, R (*fn)(JNIEnv*, jclass, Args...)>
642 struct make_jni_test_decorator<R(JNIEnv*, jclass kls, Args...), fn> {
applyart::make_jni_test_decorator643   static R apply(JNIEnv* env, jclass kls, Args ... args) {
644     EXPECT_THREAD_STATE_FOR_CURRENT_JNI();
645     EXPECT_MUTATOR_LOCK_FOR_CURRENT_JNI();
646     EXPECT_JNI_ENV_AND_CLASS_FOR_CURRENT_JNI(env, kls);
647     // All incoming parameters get spilled into the JNI transition frame.
648     // The `jclass` is just a reference to the method's declaring class field.
649     EXPECT_NUM_STACK_REFERENCES(count_nonnull_refs(args...));
650 
651     return fn(env, kls, args...);
652   }
653 };
654 
655 // Decorator for instance JNI callbacks.
656 template <typename R, typename ... Args, R (*fn)(JNIEnv*, jobject, Args...)>
657 struct make_jni_test_decorator<R(JNIEnv*, jobject, Args...), fn> {
applyart::make_jni_test_decorator658   static R apply(JNIEnv* env, jobject thisObj, Args ... args) {
659     EXPECT_THREAD_STATE_FOR_CURRENT_JNI();
660     EXPECT_MUTATOR_LOCK_FOR_CURRENT_JNI();
661     EXPECT_JNI_ENV_AND_OBJECT_FOR_CURRENT_JNI(env, thisObj);
662     // All incoming parameters + the implicit 'this' get spilled into the JNI transition frame.
663     EXPECT_NUM_STACK_REFERENCES(count_nonnull_refs(thisObj, args...));
664 
665     return fn(env, thisObj, args...);
666   }
667 };
668 
669 // Decorate the regular JNI callee with the extra gtest checks.
670 // This way we can have common test logic for everything generic like checking if a lock is held,
671 // checking handle scope state, etc.
672 #define MAKE_JNI_TEST_DECORATOR(fn) make_jni_test_decorator<decltype(fn), (fn)>::apply
673 
674 // Convert function f(JNIEnv*,jclass,a,b,c,d...) into f2(a,b,c,d...)
675 // -- This way we don't have to write out each implementation twice for @CriticalNative.
676 #define JNI_CRITICAL_WRAPPER(func) jni_remove_extra_parameters<decltype(func), (func)>::apply
677 // Get a function pointer whose calling convention either matches a regular native
678 // or a critical native depending on which kind of jni is currently under test.
679 // -- This also has the benefit of genering a compile time error if the 'func' doesn't properly
680 //    have JNIEnv and jclass parameters first.
681 #define CURRENT_JNI_WRAPPER(func)                                                         \
682     (IsCurrentJniCritical()                                                               \
683          ? reinterpret_cast<void*>(&JNI_CRITICAL_WRAPPER(MAKE_JNI_TEST_DECORATOR(func)))  \
684          : reinterpret_cast<void*>(&MAKE_JNI_TEST_DECORATOR(func)))
685 
686 // Do the opposite of the above. Do *not* wrap the function, instead just cast it to a void*.
687 // Only for "TEST_JNI_NORMAL_ONLY" configs, and it inserts a test assert to ensure this is the case.
688 #define NORMAL_JNI_ONLY_NOWRAP(func) \
689     ({ ASSERT_TRUE(IsCurrentJniNormal()); reinterpret_cast<void*>(&(func)); })
690 // Same as above, but with nullptr. When we want to test the stub functionality.
691 #define NORMAL_OR_FAST_JNI_ONLY_NULLPTR \
692     ({ ASSERT_TRUE(IsCurrentJniNormal() || IsCurrentJniFast()); nullptr; })
693 
694 
695 int gJava_MyClassNatives_foo_calls[kJniKindCount] = {};
Java_MyClassNatives_foo(JNIEnv *,jobject)696 void Java_MyClassNatives_foo(JNIEnv*, jobject) {
697   gJava_MyClassNatives_foo_calls[gCurrentJni]++;
698 }
699 
CompileAndRunNoArgMethodImpl()700 void JniCompilerTest::CompileAndRunNoArgMethodImpl() {
701   SetUpForTest(false, "foo", "()V", CURRENT_JNI_WRAPPER(Java_MyClassNatives_foo));
702 
703   EXPECT_EQ(0, gJava_MyClassNatives_foo_calls[gCurrentJni]);
704   env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
705   EXPECT_EQ(1, gJava_MyClassNatives_foo_calls[gCurrentJni]);
706   env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
707   EXPECT_EQ(2, gJava_MyClassNatives_foo_calls[gCurrentJni]);
708 
709   gJava_MyClassNatives_foo_calls[gCurrentJni] = 0;
710 }
711 
JNI_TEST(CompileAndRunNoArgMethod)712 JNI_TEST(CompileAndRunNoArgMethod)
713 
714 void JniCompilerTest::CompileAndRunIntMethodThroughStubImpl() {
715   SetUpForTest(false, "bar", "(I)I", NORMAL_OR_FAST_JNI_ONLY_NULLPTR);
716   // calling through stub will link with &Java_MyClassNatives_bar{,_1Fast}
717 
718   std::string reason;
719   ASSERT_TRUE(Runtime::Current()->GetJavaVM()->
720                   LoadNativeLibrary(env_, "", class_loader_, nullptr, &reason))
721       << reason;
722 
723   jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 24);
724   EXPECT_EQ(25, result);
725 }
726 
727 // Note: @CriticalNative is only for static methods.
JNI_TEST(CompileAndRunIntMethodThroughStub)728 JNI_TEST(CompileAndRunIntMethodThroughStub)
729 
730 void JniCompilerTest::CompileAndRunStaticIntMethodThroughStubImpl() {
731   SetUpForTest(true, "sbar", "(I)I", nullptr);
732   // calling through stub will link with &Java_MyClassNatives_sbar{,_1Fast,_1Critical}
733 
734   std::string reason;
735   ASSERT_TRUE(Runtime::Current()->GetJavaVM()->
736                   LoadNativeLibrary(env_, "", class_loader_, nullptr, &reason))
737       << reason;
738 
739   jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 42);
740   EXPECT_EQ(43, result);
741 }
742 
743 JNI_TEST_CRITICAL(CompileAndRunStaticIntMethodThroughStub)
744 
745 int gJava_MyClassNatives_fooI_calls[kJniKindCount] = {};
Java_MyClassNatives_fooI(JNIEnv *,jobject,jint x)746 jint Java_MyClassNatives_fooI(JNIEnv*, jobject, jint x) {
747   gJava_MyClassNatives_fooI_calls[gCurrentJni]++;
748   return x;
749 }
750 
CompileAndRunIntMethodImpl()751 void JniCompilerTest::CompileAndRunIntMethodImpl() {
752   SetUpForTest(false, "fooI", "(I)I",
753                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooI));
754 
755   EXPECT_EQ(0, gJava_MyClassNatives_fooI_calls[gCurrentJni]);
756   jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 42);
757   EXPECT_EQ(42, result);
758   EXPECT_EQ(1, gJava_MyClassNatives_fooI_calls[gCurrentJni]);
759   result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 0xCAFED00D);
760   EXPECT_EQ(static_cast<jint>(0xCAFED00D), result);
761   EXPECT_EQ(2, gJava_MyClassNatives_fooI_calls[gCurrentJni]);
762 
763   gJava_MyClassNatives_fooI_calls[gCurrentJni] = 0;
764 }
765 
766 JNI_TEST(CompileAndRunIntMethod)
767 
768 int gJava_MyClassNatives_fooII_calls[kJniKindCount] = {};
Java_MyClassNatives_fooII(JNIEnv *,jobject,jint x,jint y)769 jint Java_MyClassNatives_fooII(JNIEnv*, jobject, jint x, jint y) {
770   gJava_MyClassNatives_fooII_calls[gCurrentJni]++;
771   return x - y;  // non-commutative operator
772 }
773 
CompileAndRunIntIntMethodImpl()774 void JniCompilerTest::CompileAndRunIntIntMethodImpl() {
775   SetUpForTest(false, "fooII", "(II)I",
776                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooII));
777 
778   EXPECT_EQ(0, gJava_MyClassNatives_fooII_calls[gCurrentJni]);
779   jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 99, 10);
780   EXPECT_EQ(99 - 10, result);
781   EXPECT_EQ(1, gJava_MyClassNatives_fooII_calls[gCurrentJni]);
782   result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 0xCAFEBABE,
783                                          0xCAFED00D);
784   EXPECT_EQ(static_cast<jint>(0xCAFEBABE - 0xCAFED00D), result);
785   EXPECT_EQ(2, gJava_MyClassNatives_fooII_calls[gCurrentJni]);
786 
787   gJava_MyClassNatives_fooII_calls[gCurrentJni] = 0;
788 }
789 
790 JNI_TEST(CompileAndRunIntIntMethod)
791 
792 int gJava_MyClassNatives_fooJJ_calls[kJniKindCount] = {};
Java_MyClassNatives_fooJJ(JNIEnv *,jobject,jlong x,jlong y)793 jlong Java_MyClassNatives_fooJJ(JNIEnv*, jobject, jlong x, jlong y) {
794   gJava_MyClassNatives_fooJJ_calls[gCurrentJni]++;
795   return x - y;  // non-commutative operator
796 }
797 
CompileAndRunLongLongMethodImpl()798 void JniCompilerTest::CompileAndRunLongLongMethodImpl() {
799   SetUpForTest(false, "fooJJ", "(JJ)J",
800                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooJJ));
801 
802   EXPECT_EQ(0, gJava_MyClassNatives_fooJJ_calls[gCurrentJni]);
803   jlong a = INT64_C(0x1234567890ABCDEF);
804   jlong b = INT64_C(0xFEDCBA0987654321);
805   jlong result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
806   EXPECT_EQ(a - b, result);
807   EXPECT_EQ(1, gJava_MyClassNatives_fooJJ_calls[gCurrentJni]);
808   result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, b, a);
809   EXPECT_EQ(b - a, result);
810   EXPECT_EQ(2, gJava_MyClassNatives_fooJJ_calls[gCurrentJni]);
811 
812   gJava_MyClassNatives_fooJJ_calls[gCurrentJni] = 0;
813 }
814 
815 JNI_TEST(CompileAndRunLongLongMethod)
816 
817 int gJava_MyClassNatives_fooDD_calls[kJniKindCount] = {};
Java_MyClassNatives_fooDD(JNIEnv *,jobject,jdouble x,jdouble y)818 jdouble Java_MyClassNatives_fooDD(JNIEnv*, jobject, jdouble x, jdouble y) {
819   gJava_MyClassNatives_fooDD_calls[gCurrentJni]++;
820   return x - y;  // non-commutative operator
821 }
822 
CompileAndRunDoubleDoubleMethodImpl()823 void JniCompilerTest::CompileAndRunDoubleDoubleMethodImpl() {
824   SetUpForTest(false, "fooDD", "(DD)D",
825                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooDD));
826 
827   EXPECT_EQ(0, gJava_MyClassNatives_fooDD_calls[gCurrentJni]);
828   jdouble result = env_->CallNonvirtualDoubleMethod(jobj_, jklass_, jmethod_,
829                                                     99.0, 10.0);
830   EXPECT_DOUBLE_EQ(99.0 - 10.0, result);
831   EXPECT_EQ(1, gJava_MyClassNatives_fooDD_calls[gCurrentJni]);
832   jdouble a = 3.14159265358979323846;
833   jdouble b = 0.69314718055994530942;
834   result = env_->CallNonvirtualDoubleMethod(jobj_, jklass_, jmethod_, a, b);
835   EXPECT_DOUBLE_EQ(a - b, result);
836   EXPECT_EQ(2, gJava_MyClassNatives_fooDD_calls[gCurrentJni]);
837 
838   gJava_MyClassNatives_fooDD_calls[gCurrentJni] = 0;
839 }
840 
841 int gJava_MyClassNatives_fooJJ_synchronized_calls[kJniKindCount] = {};
Java_MyClassNatives_fooJJ_synchronized(JNIEnv * env,jobject,jlong x,jlong y)842 jlong Java_MyClassNatives_fooJJ_synchronized(JNIEnv* env, jobject, jlong x, jlong y) {
843   JniCompilerTest::AssertCallerObjectLocked(env);
844   gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]++;
845   return x | y;
846 }
847 
848 void InitEntryPoints(JniEntryPoints* jpoints,
849                      QuickEntryPoints* qpoints,
850                      bool monitor_jni_entry_exit);
851 
CompileAndRun_fooJJ_synchronizedImpl()852 void JniCompilerTest::CompileAndRun_fooJJ_synchronizedImpl() {
853   SetUpForTest(false, "fooJJ_synchronized", "(JJ)J",
854                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooJJ_synchronized));
855 
856   EXPECT_EQ(0, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
857   jlong a = 0x1000000020000000ULL;
858   jlong b = 0x00ff000000aa0000ULL;
859   jlong result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
860   EXPECT_EQ(a | b, result);
861   EXPECT_EQ(1, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
862 
863   // Exercise recursive thin locking/unlocking.
864   // Note: Thin lock count 0 means locked once.
865   env_->MonitorEnter(jobj_);
866   LockWord lock_word = GetLockWord(jobj_);
867   ASSERT_EQ(lock_word.GetState(), LockWord::kThinLocked);
868   ASSERT_EQ(lock_word.ThinLockCount(), 0u);
869   result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
870   EXPECT_EQ(a | b, result);
871   EXPECT_EQ(2, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
872   lock_word = GetLockWord(jobj_);
873   ASSERT_EQ(lock_word.GetState(), LockWord::kThinLocked);
874   ASSERT_EQ(lock_word.ThinLockCount(), 0u);
875   env_->MonitorExit(jobj_);
876   lock_word = GetLockWord(jobj_);
877   ASSERT_EQ(lock_word.GetState(), LockWord::kUnlocked);
878 
879   // Exercise lock inflation due to thin lock count overflow.
880   constexpr uint32_t kMaxThinLockRecursiveLocks = 1u << LockWord::kThinLockCountSize;
881   for (uint32_t i = 0; i != kMaxThinLockRecursiveLocks; ++i) {
882     env_->MonitorEnter(jobj_);
883     lock_word = GetLockWord(jobj_);
884     ASSERT_EQ(lock_word.GetState(), LockWord::kThinLocked);
885     ASSERT_EQ(lock_word.ThinLockCount(), i);
886   }
887   result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
888   EXPECT_EQ(a | b, result);
889   EXPECT_EQ(3, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
890   lock_word = GetLockWord(jobj_);
891   ASSERT_EQ(lock_word.GetState(), LockWord::kFatLocked);
892   for (uint32_t i = 0; i != kMaxThinLockRecursiveLocks; ++i) {
893     env_->MonitorExit(jobj_);  // Remains "fat-locked" even if actually unlocked.
894   }
895 
896   // Exercise locking for "fat-locked".
897   lock_word = GetLockWord(jobj_);
898   ASSERT_EQ(lock_word.GetState(), LockWord::kFatLocked);
899   result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
900   EXPECT_EQ(a | b, result);
901   EXPECT_EQ(4, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
902   lock_word = GetLockWord(jobj_);
903   ASSERT_EQ(lock_word.GetState(), LockWord::kFatLocked);
904 
905   // Exercise locking/unocking for "fat-locked" through the "no_inline" path.
906   // These entrypoints are selected with verbose "systrace_lock_logging".
907   Thread* self = Thread::Current();
908   ASSERT_FALSE(gLogVerbosity.systrace_lock_logging);
909   gLogVerbosity.systrace_lock_logging = true;
910   InitEntryPoints(&self->tlsPtr_.jni_entrypoints,
911                   &self->tlsPtr_.quick_entrypoints,
912                   self->ReadFlag(ThreadFlag::kMonitorJniEntryExit));
913   result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
914   EXPECT_EQ(a | b, result);
915   EXPECT_EQ(5, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
916   gLogVerbosity.systrace_lock_logging = false;
917   InitEntryPoints(&self->tlsPtr_.jni_entrypoints,
918                   &self->tlsPtr_.quick_entrypoints,
919                   self->ReadFlag(ThreadFlag::kMonitorJniEntryExit));
920 
921   gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni] = 0;
922 }
923 
924 JNI_TEST_NORMAL_ONLY(CompileAndRun_fooJJ_synchronized)
925 
926 int gJava_MyClassNatives_fooIOO_calls[kJniKindCount] = {};
Java_MyClassNatives_fooIOO(JNIEnv *,jobject thisObj,jint x,jobject y,jobject z)927 jobject Java_MyClassNatives_fooIOO(JNIEnv*, jobject thisObj, jint x, jobject y,
928                             jobject z) {
929   gJava_MyClassNatives_fooIOO_calls[gCurrentJni]++;
930   switch (x) {
931     case 1:
932       return y;
933     case 2:
934       return z;
935     default:
936       return thisObj;
937   }
938 }
939 
CompileAndRunIntObjectObjectMethodImpl()940 void JniCompilerTest::CompileAndRunIntObjectObjectMethodImpl() {
941   SetUpForTest(false, "fooIOO",
942                "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
943                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooIOO));
944 
945   EXPECT_EQ(0, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
946   jobject result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, nullptr, nullptr);
947   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
948   EXPECT_EQ(1, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
949 
950   result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, nullptr, jklass_);
951   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
952   EXPECT_EQ(2, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
953   result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 1, nullptr, jklass_);
954   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
955   EXPECT_EQ(3, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
956   result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 2, nullptr, jklass_);
957   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
958   EXPECT_EQ(4, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
959 
960   result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, jklass_, nullptr);
961   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
962   EXPECT_EQ(5, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
963   result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 1, jklass_, nullptr);
964   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
965   EXPECT_EQ(6, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
966   result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 2, jklass_, nullptr);
967   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
968   EXPECT_EQ(7, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
969 
970   gJava_MyClassNatives_fooIOO_calls[gCurrentJni] = 0;
971 }
972 
973 JNI_TEST(CompileAndRunIntObjectObjectMethod)
974 
975 int gJava_MyClassNatives_fooSII_calls[kJniKindCount] = {};
Java_MyClassNatives_fooSII(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED,jint x,jint y)976 jint Java_MyClassNatives_fooSII(JNIEnv* env ATTRIBUTE_UNUSED,
977                                 jclass klass ATTRIBUTE_UNUSED,
978                                 jint x,
979                                 jint y) {
980   gJava_MyClassNatives_fooSII_calls[gCurrentJni]++;
981   return x + y;
982 }
983 
CompileAndRunStaticIntIntMethodImpl()984 void JniCompilerTest::CompileAndRunStaticIntIntMethodImpl() {
985   SetUpForTest(true, "fooSII", "(II)I",
986                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSII));
987 
988   EXPECT_EQ(0, gJava_MyClassNatives_fooSII_calls[gCurrentJni]);
989   jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 20, 30);
990   EXPECT_EQ(50, result);
991   EXPECT_EQ(1, gJava_MyClassNatives_fooSII_calls[gCurrentJni]);
992 
993   gJava_MyClassNatives_fooSII_calls[gCurrentJni] = 0;
994 }
995 
996 JNI_TEST_CRITICAL(CompileAndRunStaticIntIntMethod)
997 
998 int gJava_MyClassNatives_fooSDD_calls[kJniKindCount] = {};
Java_MyClassNatives_fooSDD(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED,jdouble x,jdouble y)999 jdouble Java_MyClassNatives_fooSDD(JNIEnv* env ATTRIBUTE_UNUSED,
1000                                    jclass klass ATTRIBUTE_UNUSED,
1001                                    jdouble x,
1002                                    jdouble y) {
1003   gJava_MyClassNatives_fooSDD_calls[gCurrentJni]++;
1004   return x - y;  // non-commutative operator
1005 }
1006 
CompileAndRunStaticDoubleDoubleMethodImpl()1007 void JniCompilerTest::CompileAndRunStaticDoubleDoubleMethodImpl() {
1008   SetUpForTest(true, "fooSDD", "(DD)D",
1009                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSDD));
1010 
1011   EXPECT_EQ(0, gJava_MyClassNatives_fooSDD_calls[gCurrentJni]);
1012   jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_, 99.0, 10.0);
1013   EXPECT_DOUBLE_EQ(99.0 - 10.0, result);
1014   EXPECT_EQ(1, gJava_MyClassNatives_fooSDD_calls[gCurrentJni]);
1015   jdouble a = 3.14159265358979323846;
1016   jdouble b = 0.69314718055994530942;
1017   result = env_->CallStaticDoubleMethod(jklass_, jmethod_, a, b);
1018   EXPECT_DOUBLE_EQ(a - b, result);
1019   EXPECT_DOUBLE_EQ(2, gJava_MyClassNatives_fooSDD_calls[gCurrentJni]);
1020 
1021   gJava_MyClassNatives_fooSDD_calls[gCurrentJni] = 0;
1022 }
1023 
JNI_TEST_CRITICAL(CompileAndRunStaticDoubleDoubleMethod)1024 JNI_TEST_CRITICAL(CompileAndRunStaticDoubleDoubleMethod)
1025 
1026 // The x86 generic JNI code had a bug where it assumed a floating
1027 // point return value would be in xmm0. We use log, to somehow ensure
1028 // the compiler will use the floating point stack.
1029 
1030 jdouble Java_MyClassNatives_logD(JNIEnv*, jclass, jdouble x) {
1031   return log(x);
1032 }
1033 
Java_MyClassNatives_logD_notNormal(JNIEnv *,jclass,jdouble x)1034 jdouble Java_MyClassNatives_logD_notNormal(JNIEnv*, jclass, jdouble x) {
1035   EXPECT_DOUBLE_EQ(2.0, x);
1036   return log(x);
1037 }
1038 
RunStaticLogDoubleMethodImpl()1039 void JniCompilerTest::RunStaticLogDoubleMethodImpl() {
1040   void* jni_handler;
1041   if (IsCurrentJniNormal()) {
1042     // This test seems a bit special, don't use a JNI wrapper here.
1043     jni_handler = NORMAL_JNI_ONLY_NOWRAP(Java_MyClassNatives_logD);
1044   } else {
1045     jni_handler = CURRENT_JNI_WRAPPER(Java_MyClassNatives_logD_notNormal);
1046   }
1047   SetUpForTest(true, "logD", "(D)D", jni_handler);
1048 
1049   jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_, 2.0);
1050   EXPECT_DOUBLE_EQ(log(2.0), result);
1051 }
1052 
JNI_TEST_CRITICAL(RunStaticLogDoubleMethod)1053 JNI_TEST_CRITICAL(RunStaticLogDoubleMethod)
1054 
1055 jfloat Java_MyClassNatives_logF(JNIEnv*, jclass, jfloat x) {
1056   return logf(x);
1057 }
1058 
RunStaticLogFloatMethodImpl()1059 void JniCompilerTest::RunStaticLogFloatMethodImpl() {
1060   void* jni_handler;
1061   if (IsCurrentJniNormal()) {
1062     // This test seems a bit special, don't use a JNI wrapper here.
1063     jni_handler = NORMAL_JNI_ONLY_NOWRAP(Java_MyClassNatives_logF);
1064   } else {
1065     jni_handler = CURRENT_JNI_WRAPPER(Java_MyClassNatives_logF);
1066   }
1067 
1068   SetUpForTest(true, "logF", "(F)F", jni_handler);
1069 
1070   jfloat result = env_->CallStaticFloatMethod(jklass_, jmethod_, 2.0);
1071   EXPECT_FLOAT_EQ(logf(2.0), result);
1072 }
1073 
JNI_TEST_CRITICAL(RunStaticLogFloatMethod)1074 JNI_TEST_CRITICAL(RunStaticLogFloatMethod)
1075 
1076 jboolean Java_MyClassNatives_returnTrue(JNIEnv*, jclass) {
1077   return JNI_TRUE;
1078 }
1079 
Java_MyClassNatives_returnFalse(JNIEnv *,jclass)1080 jboolean Java_MyClassNatives_returnFalse(JNIEnv*, jclass) {
1081   return JNI_FALSE;
1082 }
1083 
Java_MyClassNatives_returnInt(JNIEnv *,jclass)1084 jint Java_MyClassNatives_returnInt(JNIEnv*, jclass) {
1085   return 42;
1086 }
1087 
RunStaticReturnTrueImpl()1088 void JniCompilerTest::RunStaticReturnTrueImpl() {
1089   SetUpForTest(true, "returnTrue", "()Z", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnTrue));
1090 
1091   jboolean result = env_->CallStaticBooleanMethod(jklass_, jmethod_);
1092   EXPECT_TRUE(result);
1093 }
1094 
JNI_TEST_CRITICAL(RunStaticReturnTrue)1095 JNI_TEST_CRITICAL(RunStaticReturnTrue)
1096 
1097 void JniCompilerTest::RunStaticReturnFalseImpl() {
1098   SetUpForTest(true, "returnFalse", "()Z",
1099                CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnFalse));
1100 
1101   jboolean result = env_->CallStaticBooleanMethod(jklass_, jmethod_);
1102   EXPECT_FALSE(result);
1103 }
1104 
JNI_TEST_CRITICAL(RunStaticReturnFalse)1105 JNI_TEST_CRITICAL(RunStaticReturnFalse)
1106 
1107 void JniCompilerTest::RunGenericStaticReturnIntImpl() {
1108   SetUpForTest(true, "returnInt", "()I", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnInt));
1109 
1110   jint result = env_->CallStaticIntMethod(jklass_, jmethod_);
1111   EXPECT_EQ(42, result);
1112 }
1113 
1114 JNI_TEST_CRITICAL(RunGenericStaticReturnInt)
1115 
1116 int gJava_MyClassNatives_returnDouble_calls[kJniKindCount] = {};
Java_MyClassNatives_returnDouble(JNIEnv *,jclass)1117 jdouble Java_MyClassNatives_returnDouble(JNIEnv*, jclass) {
1118   gJava_MyClassNatives_returnDouble_calls[gCurrentJni]++;
1119   return 4.0;
1120 }
1121 
RunGenericStaticReturnDoubleImpl()1122 void JniCompilerTest::RunGenericStaticReturnDoubleImpl() {
1123   SetUpForTest(true, "returnDouble", "()D", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnDouble));
1124 
1125   jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_);
1126   EXPECT_DOUBLE_EQ(4.0, result);
1127   EXPECT_EQ(1, gJava_MyClassNatives_returnDouble_calls[gCurrentJni]);
1128 
1129   gJava_MyClassNatives_returnDouble_calls[gCurrentJni] = 0;
1130 }
1131 
JNI_TEST_CRITICAL(RunGenericStaticReturnDouble)1132 JNI_TEST_CRITICAL(RunGenericStaticReturnDouble)
1133 
1134 jlong Java_MyClassNatives_returnLong(JNIEnv*, jclass) {
1135   return 0xFEEDDEADFEEDL;
1136 }
1137 
RunGenericStaticReturnLongImpl()1138 void JniCompilerTest::RunGenericStaticReturnLongImpl() {
1139   SetUpForTest(true, "returnLong", "()J", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnLong));
1140 
1141   jlong result = env_->CallStaticLongMethod(jklass_, jmethod_);
1142   EXPECT_EQ(0xFEEDDEADFEEDL, result);
1143 }
1144 
1145 JNI_TEST_CRITICAL(RunGenericStaticReturnLong)
1146 
1147 int gJava_MyClassNatives_fooSIOO_calls[kJniKindCount] = {};
Java_MyClassNatives_fooSIOO(JNIEnv *,jclass klass,jint x,jobject y,jobject z)1148 jobject Java_MyClassNatives_fooSIOO(JNIEnv*, jclass klass, jint x, jobject y, jobject z) {
1149   gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]++;
1150   switch (x) {
1151     case 1:
1152       return y;
1153     case 2:
1154       return z;
1155     default:
1156       return klass;
1157   }
1158 }
1159 
CompileAndRunStaticIntObjectObjectMethodImpl()1160 void JniCompilerTest::CompileAndRunStaticIntObjectObjectMethodImpl() {
1161   SetUpForTest(true, "fooSIOO",
1162                "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
1163                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSIOO));
1164 
1165   EXPECT_EQ(0, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1166   jobject result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, nullptr);
1167   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
1168   EXPECT_EQ(1, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1169 
1170   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, jobj_);
1171   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
1172   EXPECT_EQ(2, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1173   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, nullptr, jobj_);
1174   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1175   EXPECT_EQ(3, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1176   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, nullptr, jobj_);
1177   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
1178   EXPECT_EQ(4, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1179 
1180   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, jobj_, nullptr);
1181   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
1182   EXPECT_EQ(5, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1183   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, jobj_, nullptr);
1184   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
1185   EXPECT_EQ(6, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1186   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1187   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1188   EXPECT_EQ(7, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
1189 
1190   gJava_MyClassNatives_fooSIOO_calls[gCurrentJni] = 0;
1191 }
1192 
1193 JNI_TEST(CompileAndRunStaticIntObjectObjectMethod)
1194 
1195 int gJava_MyClassNatives_fooSSIOO_calls[kJniKindCount] = {};
Java_MyClassNatives_fooSSIOO(JNIEnv * env,jclass klass,jint x,jobject y,jobject z)1196 jobject Java_MyClassNatives_fooSSIOO(JNIEnv* env, jclass klass, jint x, jobject y, jobject z) {
1197   JniCompilerTest::AssertCallerObjectLocked(env);
1198   gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]++;
1199   switch (x) {
1200     case 1:
1201       return y;
1202     case 2:
1203       return z;
1204     default:
1205       return klass;
1206   }
1207 }
1208 
CompileAndRunStaticSynchronizedIntObjectObjectMethodImpl()1209 void JniCompilerTest::CompileAndRunStaticSynchronizedIntObjectObjectMethodImpl() {
1210   SetUpForTest(true, "fooSSIOO",
1211                "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
1212                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSSIOO));
1213 
1214   EXPECT_EQ(0, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1215   jobject result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, nullptr);
1216   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
1217   EXPECT_EQ(1, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1218 
1219   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, jobj_);
1220   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
1221   EXPECT_EQ(2, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1222   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, nullptr, jobj_);
1223   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1224   EXPECT_EQ(3, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1225   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, nullptr, jobj_);
1226   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
1227   EXPECT_EQ(4, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1228 
1229   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, jobj_, nullptr);
1230   EXPECT_TRUE(env_->IsSameObject(jklass_, result));
1231   EXPECT_EQ(5, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1232   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, jobj_, nullptr);
1233   EXPECT_TRUE(env_->IsSameObject(jobj_, result));
1234   EXPECT_EQ(6, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1235   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1236   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1237   EXPECT_EQ(7, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1238 
1239   // Exercise recursive thin locking/unlocking.
1240   // Note: Thin lock count 0 means locked once.
1241   env_->MonitorEnter(jklass_);
1242   LockWord lock_word = GetLockWord(jklass_);
1243   ASSERT_EQ(lock_word.GetState(), LockWord::kThinLocked);
1244   ASSERT_EQ(lock_word.ThinLockCount(), 0u);
1245   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1246   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1247   EXPECT_EQ(8, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1248   lock_word = GetLockWord(jklass_);
1249   ASSERT_EQ(lock_word.GetState(), LockWord::kThinLocked);
1250   ASSERT_EQ(lock_word.ThinLockCount(), 0u);
1251   env_->MonitorExit(jklass_);
1252   lock_word = GetLockWord(jklass_);
1253   ASSERT_EQ(lock_word.GetState(), LockWord::kUnlocked);
1254 
1255   // Exercise lock inflation due to thin lock count overflow.
1256   constexpr uint32_t kMaxThinLockRecursiveLocks = 1u << LockWord::kThinLockCountSize;
1257   for (uint32_t i = 0; i != kMaxThinLockRecursiveLocks; ++i) {
1258     env_->MonitorEnter(jklass_);
1259     lock_word = GetLockWord(jklass_);
1260     ASSERT_EQ(lock_word.GetState(), LockWord::kThinLocked);
1261     ASSERT_EQ(lock_word.ThinLockCount(), i);
1262   }
1263   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1264   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1265   EXPECT_EQ(9, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1266   lock_word = GetLockWord(jklass_);
1267   ASSERT_EQ(lock_word.GetState(), LockWord::kFatLocked);
1268   for (uint32_t i = 0; i != kMaxThinLockRecursiveLocks; ++i) {
1269     env_->MonitorExit(jklass_);  // Remains "fat-locked" even if actually unlocked.
1270   }
1271 
1272   // Exercise locking for "fat-locked".
1273   lock_word = GetLockWord(jklass_);
1274   result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1275   EXPECT_TRUE(env_->IsSameObject(nullptr, result));
1276   EXPECT_EQ(10, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
1277   lock_word = GetLockWord(jklass_);
1278   ASSERT_EQ(lock_word.GetState(), LockWord::kFatLocked);
1279 
1280   gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni] = 0;
1281 }
1282 
1283 // TODO: Maybe. @FastNative support for returning Objects?
JNI_TEST_NORMAL_ONLY(CompileAndRunStaticSynchronizedIntObjectObjectMethod)1284 JNI_TEST_NORMAL_ONLY(CompileAndRunStaticSynchronizedIntObjectObjectMethod)
1285 
1286 void Java_MyClassNatives_throwException(JNIEnv* env, jobject) {
1287   jclass c = env->FindClass("java/lang/RuntimeException");
1288   env->ThrowNew(c, "hello");
1289 }
1290 
Java_MyClassNatives_synchronizedThrowException(JNIEnv * env,jobject)1291 void Java_MyClassNatives_synchronizedThrowException(JNIEnv* env, jobject) {
1292   JniCompilerTest::AssertCallerObjectLocked(env);
1293   jclass c = env->FindClass("java/lang/RuntimeException");
1294   env->ThrowNew(c, "hello");
1295 }
1296 
ExceptionHandlingImpl()1297 void JniCompilerTest::ExceptionHandlingImpl() {
1298   {
1299     ASSERT_FALSE(runtime_->IsStarted());
1300     ScopedObjectAccess soa(Thread::Current());
1301     class_loader_ = LoadDex("MyClassNatives");
1302 
1303     // all compilation needs to happen before Runtime::Start
1304     CompileForTestWithCurrentJni(class_loader_, false, "foo", "()V");
1305     CompileForTestWithCurrentJni(class_loader_, false, "throwException", "()V");
1306     if (gCurrentJni == enum_cast<uint32_t>(JniKind::kNormal)) {
1307       CompileForTestWithCurrentJni(class_loader_, false, "synchronizedThrowException", "()V");
1308     }
1309   }
1310   // Start runtime to avoid re-initialization in SetupForTest.
1311   Thread::Current()->TransitionFromSuspendedToRunnable();
1312   bool started = runtime_->Start();
1313   CHECK(started);
1314 
1315   gJava_MyClassNatives_foo_calls[gCurrentJni] = 0;
1316 
1317   // Check a single call of a JNI method is ok
1318   SetUpForTest(false, "foo", "()V", CURRENT_JNI_WRAPPER(Java_MyClassNatives_foo));
1319   env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
1320   EXPECT_EQ(1, gJava_MyClassNatives_foo_calls[gCurrentJni]);
1321   EXPECT_FALSE(Thread::Current()->IsExceptionPending());
1322 
1323   // Get class for exception we expect to be thrown
1324   ScopedLocalRef<jclass> jlre(env_, env_->FindClass("java/lang/RuntimeException"));
1325   SetUpForTest(false, "throwException", "()V",
1326                CURRENT_JNI_WRAPPER(Java_MyClassNatives_throwException));
1327   // Call Java_MyClassNatives_throwException (JNI method that throws exception)
1328   env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
1329   EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
1330   ScopedLocalRef<jthrowable> exception(env_, env_->ExceptionOccurred());
1331   env_->ExceptionClear();
1332   EXPECT_TRUE(env_->IsInstanceOf(exception.get(), jlre.get()));
1333 
1334   // Check a single call of a JNI method is ok
1335   EXPECT_EQ(1, gJava_MyClassNatives_foo_calls[gCurrentJni]);
1336   SetUpForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClassNatives_foo));
1337   env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
1338   EXPECT_EQ(2, gJava_MyClassNatives_foo_calls[gCurrentJni]);
1339 
1340   if (gCurrentJni == enum_cast<uint32_t>(JniKind::kNormal)) {
1341     SetUpForTest(false, "synchronizedThrowException", "()V",
1342                  CURRENT_JNI_WRAPPER(Java_MyClassNatives_synchronizedThrowException));
1343     LockWord lock_word = GetLockWord(jobj_);
1344     ASSERT_EQ(lock_word.GetState(), LockWord::kUnlocked);
1345     // Call Java_MyClassNatives_synchronizedThrowException (synchronized JNI method
1346     // that throws exception) to check that we correctly unlock the object.
1347     env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
1348     EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
1349     ScopedLocalRef<jthrowable> exception2(env_, env_->ExceptionOccurred());
1350     env_->ExceptionClear();
1351     EXPECT_TRUE(env_->IsInstanceOf(exception2.get(), jlre.get()));
1352     lock_word = GetLockWord(jobj_);
1353     EXPECT_EQ(lock_word.GetState(), LockWord::kUnlocked);
1354 
1355     // Check a single call of a JNI method is ok
1356     EXPECT_EQ(2, gJava_MyClassNatives_foo_calls[gCurrentJni]);
1357     SetUpForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClassNatives_foo));
1358     env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
1359     EXPECT_EQ(3, gJava_MyClassNatives_foo_calls[gCurrentJni]);
1360   }
1361 
1362   gJava_MyClassNatives_foo_calls[gCurrentJni] = 0;
1363 }
1364 
JNI_TEST(ExceptionHandling)1365 JNI_TEST(ExceptionHandling)
1366 
1367 jint Java_MyClassNatives_nativeUpCall(JNIEnv* env, jobject thisObj, jint i) {
1368   if (i <= 0) {
1369     // We want to check raw Object* / Array* below
1370     ScopedObjectAccess soa(env);
1371 
1372     // Build stack trace
1373     jobject internal = Thread::Current()->CreateInternalStackTrace(soa);
1374     jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(soa, internal);
1375     ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> trace_array =
1376         soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>>(ste_array);
1377     EXPECT_TRUE(trace_array != nullptr);
1378     EXPECT_EQ(11, trace_array->GetLength());
1379 
1380     // Check stack trace entries have expected values
1381     for (int32_t j = 0; j < trace_array->GetLength(); ++j) {
1382       EXPECT_EQ(-2, trace_array->Get(j)->GetLineNumber());
1383       ObjPtr<mirror::StackTraceElement> ste = trace_array->Get(j);
1384       EXPECT_STREQ("MyClassNatives.java", ste->GetFileName()->ToModifiedUtf8().c_str());
1385       EXPECT_STREQ("MyClassNatives", ste->GetDeclaringClass()->ToModifiedUtf8().c_str());
1386       EXPECT_EQ(("fooI" + CurrentJniStringSuffix()), ste->GetMethodName()->ToModifiedUtf8());
1387     }
1388 
1389     // end recursion
1390     return 0;
1391   } else {
1392     jclass jklass = env->FindClass("MyClassNatives");
1393     EXPECT_TRUE(jklass != nullptr);
1394     jmethodID jmethod = env->GetMethodID(jklass,
1395                                          ("fooI" + CurrentJniStringSuffix()).c_str(),
1396                                          "(I)I");
1397     EXPECT_TRUE(jmethod != nullptr);
1398 
1399     // Recurse with i - 1
1400     jint result = env->CallNonvirtualIntMethod(thisObj, jklass, jmethod, i - 1);
1401 
1402     // Return sum of all depths
1403     return i + result;
1404   }
1405 }
1406 
NativeStackTraceElementImpl()1407 void JniCompilerTest::NativeStackTraceElementImpl() {
1408   SetUpForTest(false, "fooI", "(I)I",
1409                CURRENT_JNI_WRAPPER(Java_MyClassNatives_nativeUpCall));
1410 
1411   // Usual # local references on stack check fails because nativeUpCall calls itself recursively,
1412   // each time the # of local references will therefore go up.
1413   ScopedDisableCheckNumStackReferences disable_num_stack_check;
1414   jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 10);
1415 
1416   EXPECT_EQ(10+9+8+7+6+5+4+3+2+1, result);
1417 }
1418 
JNI_TEST(NativeStackTraceElement)1419 JNI_TEST(NativeStackTraceElement)
1420 
1421 jobject Java_MyClassNatives_fooO(JNIEnv* env, jobject, jobject x) {
1422   return env->NewGlobalRef(x);
1423 }
1424 
ReturnGlobalRefImpl()1425 void JniCompilerTest::ReturnGlobalRefImpl() {
1426   SetUpForTest(false, "fooO", "(Ljava/lang/Object;)Ljava/lang/Object;",
1427                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooO));
1428   jobject result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, jobj_);
1429   EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(result));
1430   EXPECT_TRUE(env_->IsSameObject(result, jobj_));
1431 }
1432 
JNI_TEST(ReturnGlobalRef)1433 JNI_TEST(ReturnGlobalRef)
1434 
1435 jint local_ref_test(JNIEnv* env, jobject thisObj, jint x) {
1436   // Add 10 local references
1437   ScopedObjectAccess soa(env);
1438   for (int i = 0; i < 10; i++) {
1439     soa.AddLocalReference<jobject>(soa.Decode<mirror::Object>(thisObj));
1440   }
1441   return x+1;
1442 }
1443 
LocalReferenceTableClearingTestImpl()1444 void JniCompilerTest::LocalReferenceTableClearingTestImpl() {
1445   SetUpForTest(false, "fooI", "(I)I", CURRENT_JNI_WRAPPER(local_ref_test));
1446   // 1000 invocations of a method that adds 10 local references
1447   for (int i = 0; i < 1000; i++) {
1448     jint result = env_->CallIntMethod(jobj_, jmethod_, i);
1449     EXPECT_TRUE(result == i + 1);
1450   }
1451 }
1452 
JNI_TEST(LocalReferenceTableClearingTest)1453 JNI_TEST(LocalReferenceTableClearingTest)
1454 
1455 void my_arraycopy(JNIEnv* env, jclass klass, jobject src, jint src_pos, jobject dst, jint dst_pos, jint length) {
1456   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jklass_, klass));
1457   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jklass_, dst));
1458   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, src));
1459   EXPECT_EQ(1234, src_pos);
1460   EXPECT_EQ(5678, dst_pos);
1461   EXPECT_EQ(9876, length);
1462 }
1463 
JavaLangSystemArrayCopyImpl()1464 void JniCompilerTest::JavaLangSystemArrayCopyImpl() {
1465   SetUpForTest(true, "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V",
1466                CURRENT_JNI_WRAPPER(my_arraycopy));
1467   env_->CallStaticVoidMethod(jklass_, jmethod_, jobj_, 1234, jklass_, 5678, 9876);
1468 }
1469 
JNI_TEST(JavaLangSystemArrayCopy)1470 JNI_TEST(JavaLangSystemArrayCopy)
1471 
1472 jboolean my_casi(JNIEnv* env, jobject unsafe, jobject obj, jlong offset, jint expected, jint newval) {
1473   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, unsafe));
1474   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj));
1475   EXPECT_EQ(INT64_C(0x12345678ABCDEF88), offset);
1476   EXPECT_EQ(static_cast<jint>(0xCAFEF00D), expected);
1477   EXPECT_EQ(static_cast<jint>(0xEBADF00D), newval);
1478   return JNI_TRUE;
1479 }
1480 
CompareAndSwapIntImpl()1481 void JniCompilerTest::CompareAndSwapIntImpl() {
1482   SetUpForTest(false, "compareAndSwapInt", "(Ljava/lang/Object;JII)Z",
1483                CURRENT_JNI_WRAPPER(my_casi));
1484   jboolean result = env_->CallBooleanMethod(jobj_, jmethod_, jobj_, INT64_C(0x12345678ABCDEF88),
1485                                             0xCAFEF00D, 0xEBADF00D);
1486   EXPECT_EQ(result, JNI_TRUE);
1487 }
1488 
JNI_TEST(CompareAndSwapInt)1489 JNI_TEST(CompareAndSwapInt)
1490 
1491 jint my_gettext(JNIEnv* env, jclass klass, jlong val1, jobject obj1, jlong val2, jobject obj2) {
1492   EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass));
1493   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj1));
1494   EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj2));
1495   EXPECT_EQ(0x12345678ABCDEF88LL, val1);
1496   EXPECT_EQ(0x7FEDCBA987654321LL, val2);
1497   return 42;
1498 }
1499 
GetTextImpl()1500 void JniCompilerTest::GetTextImpl() {
1501   SetUpForTest(true, "getText", "(JLjava/lang/Object;JLjava/lang/Object;)I",
1502                CURRENT_JNI_WRAPPER(my_gettext));
1503   jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 0x12345678ABCDEF88LL, jobj_,
1504                                           INT64_C(0x7FEDCBA987654321), jobj_);
1505   EXPECT_EQ(result, 42);
1506 }
1507 
1508 JNI_TEST(GetText)
1509 
1510 int gJava_MyClassNatives_GetSinkProperties_calls[kJniKindCount] = {};
Java_MyClassNatives_GetSinkProperties(JNIEnv * env,jobject thisObj,jstring s)1511 jarray Java_MyClassNatives_GetSinkProperties(JNIEnv* env, jobject thisObj, jstring s) {
1512   JniCompilerTest::AssertCallerObjectLocked(env);
1513   EXPECT_EQ(s, nullptr);
1514   gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni]++;
1515 
1516   Thread* self = Thread::Current();
1517   ScopedObjectAccess soa(self);
1518   EXPECT_TRUE(self->HoldsLock(soa.Decode<mirror::Object>(thisObj)));
1519   return nullptr;
1520 }
1521 
GetSinkPropertiesNativeImpl()1522 void JniCompilerTest::GetSinkPropertiesNativeImpl() {
1523   SetUpForTest(false, "getSinkPropertiesNative", "(Ljava/lang/String;)[Ljava/lang/Object;",
1524                CURRENT_JNI_WRAPPER(Java_MyClassNatives_GetSinkProperties));
1525 
1526   EXPECT_EQ(0, gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni]);
1527   jarray result = down_cast<jarray>(
1528       env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, nullptr));
1529   EXPECT_EQ(nullptr, result);
1530   EXPECT_EQ(1, gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni]);
1531 
1532   gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni] = 0;
1533 }
1534 
1535 // @FastNative doesn't support 'synchronized' keyword and
1536 // never will -- locking functions aren't fast.
JNI_TEST_NORMAL_ONLY(GetSinkPropertiesNative)1537 JNI_TEST_NORMAL_ONLY(GetSinkPropertiesNative)
1538 
1539 // This should return jclass, but we're imitating a bug pattern.
1540 jobject Java_MyClassNatives_instanceMethodThatShouldReturnClass(JNIEnv* env, jobject) {
1541   return env->NewStringUTF("not a class!");
1542 }
1543 
1544 // This should return jclass, but we're imitating a bug pattern.
Java_MyClassNatives_staticMethodThatShouldReturnClass(JNIEnv * env,jclass)1545 jobject Java_MyClassNatives_staticMethodThatShouldReturnClass(JNIEnv* env, jclass) {
1546   return env->NewStringUTF("not a class!");
1547 }
1548 
UpcallReturnTypeChecking_InstanceImpl()1549 void JniCompilerTest::UpcallReturnTypeChecking_InstanceImpl() {
1550   SetUpForTest(false, "instanceMethodThatShouldReturnClass", "()Ljava/lang/Class;",
1551                CURRENT_JNI_WRAPPER(Java_MyClassNatives_instanceMethodThatShouldReturnClass));
1552 
1553   CheckJniAbortCatcher check_jni_abort_catcher;
1554   // This native method is bad, and tries to return a jstring as a jclass.
1555   env_->CallObjectMethod(jobj_, jmethod_);
1556   check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1557                                     "of java.lang.String from java.lang.Class " +
1558                                     "MyClassNatives.instanceMethodThatShouldReturnClass" +
1559                                     CurrentJniStringSuffix() + "()");
1560 
1561   // Here, we just call the method incorrectly; we should catch that too.
1562   env_->CallObjectMethod(jobj_, jmethod_);
1563   check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1564                                     "of java.lang.String from java.lang.Class " +
1565                                     "MyClassNatives.instanceMethodThatShouldReturnClass" +
1566                                     CurrentJniStringSuffix() + "()");
1567   env_->CallStaticObjectMethod(jklass_, jmethod_);
1568   check_jni_abort_catcher.Check(std::string() + "calling non-static method " +
1569                                     "java.lang.Class " +
1570                                     "MyClassNatives.instanceMethodThatShouldReturnClass" +
1571                                     CurrentJniStringSuffix() + "() with CallStaticObjectMethodV");
1572 }
1573 
JNI_TEST(UpcallReturnTypeChecking_Instance)1574 JNI_TEST(UpcallReturnTypeChecking_Instance)
1575 
1576 void JniCompilerTest::UpcallReturnTypeChecking_StaticImpl() {
1577   SetUpForTest(true, "staticMethodThatShouldReturnClass", "()Ljava/lang/Class;",
1578                CURRENT_JNI_WRAPPER(Java_MyClassNatives_staticMethodThatShouldReturnClass));
1579 
1580   CheckJniAbortCatcher check_jni_abort_catcher;
1581   // This native method is bad, and tries to return a jstring as a jclass.
1582   env_->CallStaticObjectMethod(jklass_, jmethod_);
1583   check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1584                                     "of java.lang.String from java.lang.Class " +
1585                                     "MyClassNatives.staticMethodThatShouldReturnClass" +
1586                                     CurrentJniStringSuffix() + "()");
1587 
1588   // Here, we just call the method incorrectly; we should catch that too.
1589   env_->CallStaticObjectMethod(jklass_, jmethod_);
1590   check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1591                                     "of java.lang.String from java.lang.Class " +
1592                                     "MyClassNatives.staticMethodThatShouldReturnClass" +
1593                                     CurrentJniStringSuffix() + "()");
1594   env_->CallObjectMethod(jobj_, jmethod_);
1595   check_jni_abort_catcher.Check(std::string() + "calling static method " +
1596                                     "java.lang.Class " +
1597                                     "MyClassNatives.staticMethodThatShouldReturnClass" +
1598                                     CurrentJniStringSuffix() + "() with CallObjectMethodV");
1599 }
1600 
JNI_TEST(UpcallReturnTypeChecking_Static)1601 JNI_TEST(UpcallReturnTypeChecking_Static)
1602 
1603 // This should take jclass, but we're imitating a bug pattern.
1604 void Java_MyClassNatives_instanceMethodThatShouldTakeClass(JNIEnv*, jobject, jclass) {
1605 }
1606 
1607 // This should take jclass, but we're imitating a bug pattern.
Java_MyClassNatives_staticMethodThatShouldTakeClass(JNIEnv *,jclass,jclass)1608 void Java_MyClassNatives_staticMethodThatShouldTakeClass(JNIEnv*, jclass, jclass) {
1609 }
1610 
UpcallArgumentTypeChecking_InstanceImpl()1611 void JniCompilerTest::UpcallArgumentTypeChecking_InstanceImpl() {
1612   // This will lead to error messages in the log.
1613   ScopedLogSeverity sls(LogSeverity::FATAL);
1614 
1615   SetUpForTest(false, "instanceMethodThatShouldTakeClass", "(ILjava/lang/Class;)V",
1616                CURRENT_JNI_WRAPPER(Java_MyClassNatives_instanceMethodThatShouldTakeClass));
1617 
1618   CheckJniAbortCatcher check_jni_abort_catcher;
1619   // We deliberately pass a bad second argument here.
1620   env_->CallVoidMethod(jobj_, jmethod_, 123, env_->NewStringUTF("not a class!"));
1621   check_jni_abort_catcher.Check(std::string() + "bad arguments passed to void " +
1622                                     "MyClassNatives.instanceMethodThatShouldTakeClass" +
1623                                     CurrentJniStringSuffix() + "(int, java.lang.Class)");
1624 }
1625 
JNI_TEST(UpcallArgumentTypeChecking_Instance)1626 JNI_TEST(UpcallArgumentTypeChecking_Instance)
1627 
1628 void JniCompilerTest::UpcallArgumentTypeChecking_StaticImpl() {
1629   // This will lead to error messages in the log.
1630   ScopedLogSeverity sls(LogSeverity::FATAL);
1631 
1632   SetUpForTest(true, "staticMethodThatShouldTakeClass", "(ILjava/lang/Class;)V",
1633                CURRENT_JNI_WRAPPER(Java_MyClassNatives_staticMethodThatShouldTakeClass));
1634 
1635   CheckJniAbortCatcher check_jni_abort_catcher;
1636   // We deliberately pass a bad second argument here.
1637   env_->CallStaticVoidMethod(jklass_, jmethod_, 123, env_->NewStringUTF("not a class!"));
1638   check_jni_abort_catcher.Check(std::string() + "bad arguments passed to void " +
1639                                     "MyClassNatives.staticMethodThatShouldTakeClass" +
1640                                     CurrentJniStringSuffix() + "(int, java.lang.Class)");
1641 }
1642 
JNI_TEST(UpcallArgumentTypeChecking_Static)1643 JNI_TEST(UpcallArgumentTypeChecking_Static)
1644 
1645 jfloat Java_MyClassNatives_checkFloats(JNIEnv*, jobject, jfloat f1, jfloat f2) {
1646   return f1 - f2;  // non-commutative operator
1647 }
1648 
CompileAndRunFloatFloatMethodImpl()1649 void JniCompilerTest::CompileAndRunFloatFloatMethodImpl() {
1650   SetUpForTest(false, "checkFloats", "(FF)F",
1651                CURRENT_JNI_WRAPPER(Java_MyClassNatives_checkFloats));
1652 
1653   jfloat result = env_->CallNonvirtualFloatMethod(jobj_, jklass_, jmethod_,
1654                                                     99.0F, 10.0F);
1655   EXPECT_FLOAT_EQ(99.0F - 10.0F, result);
1656   jfloat a = 3.14159F;
1657   jfloat b = 0.69314F;
1658   result = env_->CallNonvirtualFloatMethod(jobj_, jklass_, jmethod_, a, b);
1659   EXPECT_FLOAT_EQ(a - b, result);
1660 }
1661 
JNI_TEST(CompileAndRunFloatFloatMethod)1662 JNI_TEST(CompileAndRunFloatFloatMethod)
1663 
1664 void Java_MyClassNatives_checkParameterAlign(JNIEnv* env ATTRIBUTE_UNUSED,
1665                                              jobject thisObj ATTRIBUTE_UNUSED,
1666                                              jint i1,
1667                                              jlong l1) {
1668   EXPECT_EQ(i1, 1234);
1669   EXPECT_EQ(l1, INT64_C(0x12345678ABCDEF0));
1670 }
1671 
CheckParameterAlignImpl()1672 void JniCompilerTest::CheckParameterAlignImpl() {
1673   SetUpForTest(false, "checkParameterAlign", "(IJ)V",
1674                CURRENT_JNI_WRAPPER(Java_MyClassNatives_checkParameterAlign));
1675 
1676   env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_, 1234, INT64_C(0x12345678ABCDEF0));
1677 }
1678 
JNI_TEST(CheckParameterAlign)1679 JNI_TEST(CheckParameterAlign)
1680 
1681 void Java_MyClassNatives_maxParamNumber(JNIEnv* env, jobject,
1682     jobject o0, jobject o1, jobject o2, jobject o3, jobject o4, jobject o5, jobject o6, jobject o7,
1683     jobject o8, jobject o9, jobject o10, jobject o11, jobject o12, jobject o13, jobject o14, jobject o15,
1684     jobject o16, jobject o17, jobject o18, jobject o19, jobject o20, jobject o21, jobject o22, jobject o23,
1685     jobject o24, jobject o25, jobject o26, jobject o27, jobject o28, jobject o29, jobject o30, jobject o31,
1686     jobject o32, jobject o33, jobject o34, jobject o35, jobject o36, jobject o37, jobject o38, jobject o39,
1687     jobject o40, jobject o41, jobject o42, jobject o43, jobject o44, jobject o45, jobject o46, jobject o47,
1688     jobject o48, jobject o49, jobject o50, jobject o51, jobject o52, jobject o53, jobject o54, jobject o55,
1689     jobject o56, jobject o57, jobject o58, jobject o59, jobject o60, jobject o61, jobject o62, jobject o63,
1690     jobject o64, jobject o65, jobject o66, jobject o67, jobject o68, jobject o69, jobject o70, jobject o71,
1691     jobject o72, jobject o73, jobject o74, jobject o75, jobject o76, jobject o77, jobject o78, jobject o79,
1692     jobject o80, jobject o81, jobject o82, jobject o83, jobject o84, jobject o85, jobject o86, jobject o87,
1693     jobject o88, jobject o89, jobject o90, jobject o91, jobject o92, jobject o93, jobject o94, jobject o95,
1694     jobject o96, jobject o97, jobject o98, jobject o99, jobject o100, jobject o101, jobject o102, jobject o103,
1695     jobject o104, jobject o105, jobject o106, jobject o107, jobject o108, jobject o109, jobject o110, jobject o111,
1696     jobject o112, jobject o113, jobject o114, jobject o115, jobject o116, jobject o117, jobject o118, jobject o119,
1697     jobject o120, jobject o121, jobject o122, jobject o123, jobject o124, jobject o125, jobject o126, jobject o127,
1698     jobject o128, jobject o129, jobject o130, jobject o131, jobject o132, jobject o133, jobject o134, jobject o135,
1699     jobject o136, jobject o137, jobject o138, jobject o139, jobject o140, jobject o141, jobject o142, jobject o143,
1700     jobject o144, jobject o145, jobject o146, jobject o147, jobject o148, jobject o149, jobject o150, jobject o151,
1701     jobject o152, jobject o153, jobject o154, jobject o155, jobject o156, jobject o157, jobject o158, jobject o159,
1702     jobject o160, jobject o161, jobject o162, jobject o163, jobject o164, jobject o165, jobject o166, jobject o167,
1703     jobject o168, jobject o169, jobject o170, jobject o171, jobject o172, jobject o173, jobject o174, jobject o175,
1704     jobject o176, jobject o177, jobject o178, jobject o179, jobject o180, jobject o181, jobject o182, jobject o183,
1705     jobject o184, jobject o185, jobject o186, jobject o187, jobject o188, jobject o189, jobject o190, jobject o191,
1706     jobject o192, jobject o193, jobject o194, jobject o195, jobject o196, jobject o197, jobject o198, jobject o199,
1707     jobject o200, jobject o201, jobject o202, jobject o203, jobject o204, jobject o205, jobject o206, jobject o207,
1708     jobject o208, jobject o209, jobject o210, jobject o211, jobject o212, jobject o213, jobject o214, jobject o215,
1709     jobject o216, jobject o217, jobject o218, jobject o219, jobject o220, jobject o221, jobject o222, jobject o223,
1710     jobject o224, jobject o225, jobject o226, jobject o227, jobject o228, jobject o229, jobject o230, jobject o231,
1711     jobject o232, jobject o233, jobject o234, jobject o235, jobject o236, jobject o237, jobject o238, jobject o239,
1712     jobject o240, jobject o241, jobject o242, jobject o243, jobject o244, jobject o245, jobject o246, jobject o247,
1713     jobject o248, jobject o249, jobject o250, jobject o251, jobject o252, jobject o253) {
1714   // two tests possible
1715   if (o0 == nullptr) {
1716     // 1) everything is null
1717     EXPECT_TRUE(o0 == nullptr && o1 == nullptr && o2 == nullptr && o3 == nullptr && o4 == nullptr
1718         && o5 == nullptr && o6 == nullptr && o7 == nullptr && o8 == nullptr && o9 == nullptr
1719         && o10 == nullptr && o11 == nullptr && o12 == nullptr && o13 == nullptr && o14 == nullptr
1720         && o15 == nullptr && o16 == nullptr && o17 == nullptr && o18 == nullptr && o19 == nullptr
1721         && o20 == nullptr && o21 == nullptr && o22 == nullptr && o23 == nullptr && o24 == nullptr
1722         && o25 == nullptr && o26 == nullptr && o27 == nullptr && o28 == nullptr && o29 == nullptr
1723         && o30 == nullptr && o31 == nullptr && o32 == nullptr && o33 == nullptr && o34 == nullptr
1724         && o35 == nullptr && o36 == nullptr && o37 == nullptr && o38 == nullptr && o39 == nullptr
1725         && o40 == nullptr && o41 == nullptr && o42 == nullptr && o43 == nullptr && o44 == nullptr
1726         && o45 == nullptr && o46 == nullptr && o47 == nullptr && o48 == nullptr && o49 == nullptr
1727         && o50 == nullptr && o51 == nullptr && o52 == nullptr && o53 == nullptr && o54 == nullptr
1728         && o55 == nullptr && o56 == nullptr && o57 == nullptr && o58 == nullptr && o59 == nullptr
1729         && o60 == nullptr && o61 == nullptr && o62 == nullptr && o63 == nullptr && o64 == nullptr
1730         && o65 == nullptr && o66 == nullptr && o67 == nullptr && o68 == nullptr && o69 == nullptr
1731         && o70 == nullptr && o71 == nullptr && o72 == nullptr && o73 == nullptr && o74 == nullptr
1732         && o75 == nullptr && o76 == nullptr && o77 == nullptr && o78 == nullptr && o79 == nullptr
1733         && o80 == nullptr && o81 == nullptr && o82 == nullptr && o83 == nullptr && o84 == nullptr
1734         && o85 == nullptr && o86 == nullptr && o87 == nullptr && o88 == nullptr && o89 == nullptr
1735         && o90 == nullptr && o91 == nullptr && o92 == nullptr && o93 == nullptr && o94 == nullptr
1736         && o95 == nullptr && o96 == nullptr && o97 == nullptr && o98 == nullptr && o99 == nullptr
1737         && o100 == nullptr && o101 == nullptr && o102 == nullptr && o103 == nullptr && o104 == nullptr
1738         && o105 == nullptr && o106 == nullptr && o107 == nullptr && o108 == nullptr && o109 == nullptr
1739         && o110 == nullptr && o111 == nullptr && o112 == nullptr && o113 == nullptr && o114 == nullptr
1740         && o115 == nullptr && o116 == nullptr && o117 == nullptr && o118 == nullptr && o119 == nullptr
1741         && o120 == nullptr && o121 == nullptr && o122 == nullptr && o123 == nullptr && o124 == nullptr
1742         && o125 == nullptr && o126 == nullptr && o127 == nullptr && o128 == nullptr && o129 == nullptr
1743         && o130 == nullptr && o131 == nullptr && o132 == nullptr && o133 == nullptr && o134 == nullptr
1744         && o135 == nullptr && o136 == nullptr && o137 == nullptr && o138 == nullptr && o139 == nullptr
1745         && o140 == nullptr && o141 == nullptr && o142 == nullptr && o143 == nullptr && o144 == nullptr
1746         && o145 == nullptr && o146 == nullptr && o147 == nullptr && o148 == nullptr && o149 == nullptr
1747         && o150 == nullptr && o151 == nullptr && o152 == nullptr && o153 == nullptr && o154 == nullptr
1748         && o155 == nullptr && o156 == nullptr && o157 == nullptr && o158 == nullptr && o159 == nullptr
1749         && o160 == nullptr && o161 == nullptr && o162 == nullptr && o163 == nullptr && o164 == nullptr
1750         && o165 == nullptr && o166 == nullptr && o167 == nullptr && o168 == nullptr && o169 == nullptr
1751         && o170 == nullptr && o171 == nullptr && o172 == nullptr && o173 == nullptr && o174 == nullptr
1752         && o175 == nullptr && o176 == nullptr && o177 == nullptr && o178 == nullptr && o179 == nullptr
1753         && o180 == nullptr && o181 == nullptr && o182 == nullptr && o183 == nullptr && o184 == nullptr
1754         && o185 == nullptr && o186 == nullptr && o187 == nullptr && o188 == nullptr && o189 == nullptr
1755         && o190 == nullptr && o191 == nullptr && o192 == nullptr && o193 == nullptr && o194 == nullptr
1756         && o195 == nullptr && o196 == nullptr && o197 == nullptr && o198 == nullptr && o199 == nullptr
1757         && o200 == nullptr && o201 == nullptr && o202 == nullptr && o203 == nullptr && o204 == nullptr
1758         && o205 == nullptr && o206 == nullptr && o207 == nullptr && o208 == nullptr && o209 == nullptr
1759         && o210 == nullptr && o211 == nullptr && o212 == nullptr && o213 == nullptr && o214 == nullptr
1760         && o215 == nullptr && o216 == nullptr && o217 == nullptr && o218 == nullptr && o219 == nullptr
1761         && o220 == nullptr && o221 == nullptr && o222 == nullptr && o223 == nullptr && o224 == nullptr
1762         && o225 == nullptr && o226 == nullptr && o227 == nullptr && o228 == nullptr && o229 == nullptr
1763         && o230 == nullptr && o231 == nullptr && o232 == nullptr && o233 == nullptr && o234 == nullptr
1764         && o235 == nullptr && o236 == nullptr && o237 == nullptr && o238 == nullptr && o239 == nullptr
1765         && o240 == nullptr && o241 == nullptr && o242 == nullptr && o243 == nullptr && o244 == nullptr
1766         && o245 == nullptr && o246 == nullptr && o247 == nullptr && o248 == nullptr && o249 == nullptr
1767         && o250 == nullptr && o251 == nullptr && o252 == nullptr && o253 == nullptr);
1768   } else {
1769     EXPECT_EQ(0, env->GetArrayLength(reinterpret_cast<jarray>(o0)));
1770     EXPECT_EQ(1, env->GetArrayLength(reinterpret_cast<jarray>(o1)));
1771     EXPECT_EQ(2, env->GetArrayLength(reinterpret_cast<jarray>(o2)));
1772     EXPECT_EQ(3, env->GetArrayLength(reinterpret_cast<jarray>(o3)));
1773     EXPECT_EQ(4, env->GetArrayLength(reinterpret_cast<jarray>(o4)));
1774     EXPECT_EQ(5, env->GetArrayLength(reinterpret_cast<jarray>(o5)));
1775     EXPECT_EQ(6, env->GetArrayLength(reinterpret_cast<jarray>(o6)));
1776     EXPECT_EQ(7, env->GetArrayLength(reinterpret_cast<jarray>(o7)));
1777     EXPECT_EQ(8, env->GetArrayLength(reinterpret_cast<jarray>(o8)));
1778     EXPECT_EQ(9, env->GetArrayLength(reinterpret_cast<jarray>(o9)));
1779     EXPECT_EQ(10, env->GetArrayLength(reinterpret_cast<jarray>(o10)));
1780     EXPECT_EQ(11, env->GetArrayLength(reinterpret_cast<jarray>(o11)));
1781     EXPECT_EQ(12, env->GetArrayLength(reinterpret_cast<jarray>(o12)));
1782     EXPECT_EQ(13, env->GetArrayLength(reinterpret_cast<jarray>(o13)));
1783     EXPECT_EQ(14, env->GetArrayLength(reinterpret_cast<jarray>(o14)));
1784     EXPECT_EQ(15, env->GetArrayLength(reinterpret_cast<jarray>(o15)));
1785     EXPECT_EQ(16, env->GetArrayLength(reinterpret_cast<jarray>(o16)));
1786     EXPECT_EQ(17, env->GetArrayLength(reinterpret_cast<jarray>(o17)));
1787     EXPECT_EQ(18, env->GetArrayLength(reinterpret_cast<jarray>(o18)));
1788     EXPECT_EQ(19, env->GetArrayLength(reinterpret_cast<jarray>(o19)));
1789     EXPECT_EQ(20, env->GetArrayLength(reinterpret_cast<jarray>(o20)));
1790     EXPECT_EQ(21, env->GetArrayLength(reinterpret_cast<jarray>(o21)));
1791     EXPECT_EQ(22, env->GetArrayLength(reinterpret_cast<jarray>(o22)));
1792     EXPECT_EQ(23, env->GetArrayLength(reinterpret_cast<jarray>(o23)));
1793     EXPECT_EQ(24, env->GetArrayLength(reinterpret_cast<jarray>(o24)));
1794     EXPECT_EQ(25, env->GetArrayLength(reinterpret_cast<jarray>(o25)));
1795     EXPECT_EQ(26, env->GetArrayLength(reinterpret_cast<jarray>(o26)));
1796     EXPECT_EQ(27, env->GetArrayLength(reinterpret_cast<jarray>(o27)));
1797     EXPECT_EQ(28, env->GetArrayLength(reinterpret_cast<jarray>(o28)));
1798     EXPECT_EQ(29, env->GetArrayLength(reinterpret_cast<jarray>(o29)));
1799     EXPECT_EQ(30, env->GetArrayLength(reinterpret_cast<jarray>(o30)));
1800     EXPECT_EQ(31, env->GetArrayLength(reinterpret_cast<jarray>(o31)));
1801     EXPECT_EQ(32, env->GetArrayLength(reinterpret_cast<jarray>(o32)));
1802     EXPECT_EQ(33, env->GetArrayLength(reinterpret_cast<jarray>(o33)));
1803     EXPECT_EQ(34, env->GetArrayLength(reinterpret_cast<jarray>(o34)));
1804     EXPECT_EQ(35, env->GetArrayLength(reinterpret_cast<jarray>(o35)));
1805     EXPECT_EQ(36, env->GetArrayLength(reinterpret_cast<jarray>(o36)));
1806     EXPECT_EQ(37, env->GetArrayLength(reinterpret_cast<jarray>(o37)));
1807     EXPECT_EQ(38, env->GetArrayLength(reinterpret_cast<jarray>(o38)));
1808     EXPECT_EQ(39, env->GetArrayLength(reinterpret_cast<jarray>(o39)));
1809     EXPECT_EQ(40, env->GetArrayLength(reinterpret_cast<jarray>(o40)));
1810     EXPECT_EQ(41, env->GetArrayLength(reinterpret_cast<jarray>(o41)));
1811     EXPECT_EQ(42, env->GetArrayLength(reinterpret_cast<jarray>(o42)));
1812     EXPECT_EQ(43, env->GetArrayLength(reinterpret_cast<jarray>(o43)));
1813     EXPECT_EQ(44, env->GetArrayLength(reinterpret_cast<jarray>(o44)));
1814     EXPECT_EQ(45, env->GetArrayLength(reinterpret_cast<jarray>(o45)));
1815     EXPECT_EQ(46, env->GetArrayLength(reinterpret_cast<jarray>(o46)));
1816     EXPECT_EQ(47, env->GetArrayLength(reinterpret_cast<jarray>(o47)));
1817     EXPECT_EQ(48, env->GetArrayLength(reinterpret_cast<jarray>(o48)));
1818     EXPECT_EQ(49, env->GetArrayLength(reinterpret_cast<jarray>(o49)));
1819     EXPECT_EQ(50, env->GetArrayLength(reinterpret_cast<jarray>(o50)));
1820     EXPECT_EQ(51, env->GetArrayLength(reinterpret_cast<jarray>(o51)));
1821     EXPECT_EQ(52, env->GetArrayLength(reinterpret_cast<jarray>(o52)));
1822     EXPECT_EQ(53, env->GetArrayLength(reinterpret_cast<jarray>(o53)));
1823     EXPECT_EQ(54, env->GetArrayLength(reinterpret_cast<jarray>(o54)));
1824     EXPECT_EQ(55, env->GetArrayLength(reinterpret_cast<jarray>(o55)));
1825     EXPECT_EQ(56, env->GetArrayLength(reinterpret_cast<jarray>(o56)));
1826     EXPECT_EQ(57, env->GetArrayLength(reinterpret_cast<jarray>(o57)));
1827     EXPECT_EQ(58, env->GetArrayLength(reinterpret_cast<jarray>(o58)));
1828     EXPECT_EQ(59, env->GetArrayLength(reinterpret_cast<jarray>(o59)));
1829     EXPECT_EQ(60, env->GetArrayLength(reinterpret_cast<jarray>(o60)));
1830     EXPECT_EQ(61, env->GetArrayLength(reinterpret_cast<jarray>(o61)));
1831     EXPECT_EQ(62, env->GetArrayLength(reinterpret_cast<jarray>(o62)));
1832     EXPECT_EQ(63, env->GetArrayLength(reinterpret_cast<jarray>(o63)));
1833     EXPECT_EQ(64, env->GetArrayLength(reinterpret_cast<jarray>(o64)));
1834     EXPECT_EQ(65, env->GetArrayLength(reinterpret_cast<jarray>(o65)));
1835     EXPECT_EQ(66, env->GetArrayLength(reinterpret_cast<jarray>(o66)));
1836     EXPECT_EQ(67, env->GetArrayLength(reinterpret_cast<jarray>(o67)));
1837     EXPECT_EQ(68, env->GetArrayLength(reinterpret_cast<jarray>(o68)));
1838     EXPECT_EQ(69, env->GetArrayLength(reinterpret_cast<jarray>(o69)));
1839     EXPECT_EQ(70, env->GetArrayLength(reinterpret_cast<jarray>(o70)));
1840     EXPECT_EQ(71, env->GetArrayLength(reinterpret_cast<jarray>(o71)));
1841     EXPECT_EQ(72, env->GetArrayLength(reinterpret_cast<jarray>(o72)));
1842     EXPECT_EQ(73, env->GetArrayLength(reinterpret_cast<jarray>(o73)));
1843     EXPECT_EQ(74, env->GetArrayLength(reinterpret_cast<jarray>(o74)));
1844     EXPECT_EQ(75, env->GetArrayLength(reinterpret_cast<jarray>(o75)));
1845     EXPECT_EQ(76, env->GetArrayLength(reinterpret_cast<jarray>(o76)));
1846     EXPECT_EQ(77, env->GetArrayLength(reinterpret_cast<jarray>(o77)));
1847     EXPECT_EQ(78, env->GetArrayLength(reinterpret_cast<jarray>(o78)));
1848     EXPECT_EQ(79, env->GetArrayLength(reinterpret_cast<jarray>(o79)));
1849     EXPECT_EQ(80, env->GetArrayLength(reinterpret_cast<jarray>(o80)));
1850     EXPECT_EQ(81, env->GetArrayLength(reinterpret_cast<jarray>(o81)));
1851     EXPECT_EQ(82, env->GetArrayLength(reinterpret_cast<jarray>(o82)));
1852     EXPECT_EQ(83, env->GetArrayLength(reinterpret_cast<jarray>(o83)));
1853     EXPECT_EQ(84, env->GetArrayLength(reinterpret_cast<jarray>(o84)));
1854     EXPECT_EQ(85, env->GetArrayLength(reinterpret_cast<jarray>(o85)));
1855     EXPECT_EQ(86, env->GetArrayLength(reinterpret_cast<jarray>(o86)));
1856     EXPECT_EQ(87, env->GetArrayLength(reinterpret_cast<jarray>(o87)));
1857     EXPECT_EQ(88, env->GetArrayLength(reinterpret_cast<jarray>(o88)));
1858     EXPECT_EQ(89, env->GetArrayLength(reinterpret_cast<jarray>(o89)));
1859     EXPECT_EQ(90, env->GetArrayLength(reinterpret_cast<jarray>(o90)));
1860     EXPECT_EQ(91, env->GetArrayLength(reinterpret_cast<jarray>(o91)));
1861     EXPECT_EQ(92, env->GetArrayLength(reinterpret_cast<jarray>(o92)));
1862     EXPECT_EQ(93, env->GetArrayLength(reinterpret_cast<jarray>(o93)));
1863     EXPECT_EQ(94, env->GetArrayLength(reinterpret_cast<jarray>(o94)));
1864     EXPECT_EQ(95, env->GetArrayLength(reinterpret_cast<jarray>(o95)));
1865     EXPECT_EQ(96, env->GetArrayLength(reinterpret_cast<jarray>(o96)));
1866     EXPECT_EQ(97, env->GetArrayLength(reinterpret_cast<jarray>(o97)));
1867     EXPECT_EQ(98, env->GetArrayLength(reinterpret_cast<jarray>(o98)));
1868     EXPECT_EQ(99, env->GetArrayLength(reinterpret_cast<jarray>(o99)));
1869     EXPECT_EQ(100, env->GetArrayLength(reinterpret_cast<jarray>(o100)));
1870     EXPECT_EQ(101, env->GetArrayLength(reinterpret_cast<jarray>(o101)));
1871     EXPECT_EQ(102, env->GetArrayLength(reinterpret_cast<jarray>(o102)));
1872     EXPECT_EQ(103, env->GetArrayLength(reinterpret_cast<jarray>(o103)));
1873     EXPECT_EQ(104, env->GetArrayLength(reinterpret_cast<jarray>(o104)));
1874     EXPECT_EQ(105, env->GetArrayLength(reinterpret_cast<jarray>(o105)));
1875     EXPECT_EQ(106, env->GetArrayLength(reinterpret_cast<jarray>(o106)));
1876     EXPECT_EQ(107, env->GetArrayLength(reinterpret_cast<jarray>(o107)));
1877     EXPECT_EQ(108, env->GetArrayLength(reinterpret_cast<jarray>(o108)));
1878     EXPECT_EQ(109, env->GetArrayLength(reinterpret_cast<jarray>(o109)));
1879     EXPECT_EQ(110, env->GetArrayLength(reinterpret_cast<jarray>(o110)));
1880     EXPECT_EQ(111, env->GetArrayLength(reinterpret_cast<jarray>(o111)));
1881     EXPECT_EQ(112, env->GetArrayLength(reinterpret_cast<jarray>(o112)));
1882     EXPECT_EQ(113, env->GetArrayLength(reinterpret_cast<jarray>(o113)));
1883     EXPECT_EQ(114, env->GetArrayLength(reinterpret_cast<jarray>(o114)));
1884     EXPECT_EQ(115, env->GetArrayLength(reinterpret_cast<jarray>(o115)));
1885     EXPECT_EQ(116, env->GetArrayLength(reinterpret_cast<jarray>(o116)));
1886     EXPECT_EQ(117, env->GetArrayLength(reinterpret_cast<jarray>(o117)));
1887     EXPECT_EQ(118, env->GetArrayLength(reinterpret_cast<jarray>(o118)));
1888     EXPECT_EQ(119, env->GetArrayLength(reinterpret_cast<jarray>(o119)));
1889     EXPECT_EQ(120, env->GetArrayLength(reinterpret_cast<jarray>(o120)));
1890     EXPECT_EQ(121, env->GetArrayLength(reinterpret_cast<jarray>(o121)));
1891     EXPECT_EQ(122, env->GetArrayLength(reinterpret_cast<jarray>(o122)));
1892     EXPECT_EQ(123, env->GetArrayLength(reinterpret_cast<jarray>(o123)));
1893     EXPECT_EQ(124, env->GetArrayLength(reinterpret_cast<jarray>(o124)));
1894     EXPECT_EQ(125, env->GetArrayLength(reinterpret_cast<jarray>(o125)));
1895     EXPECT_EQ(126, env->GetArrayLength(reinterpret_cast<jarray>(o126)));
1896     EXPECT_EQ(127, env->GetArrayLength(reinterpret_cast<jarray>(o127)));
1897     EXPECT_EQ(128, env->GetArrayLength(reinterpret_cast<jarray>(o128)));
1898     EXPECT_EQ(129, env->GetArrayLength(reinterpret_cast<jarray>(o129)));
1899     EXPECT_EQ(130, env->GetArrayLength(reinterpret_cast<jarray>(o130)));
1900     EXPECT_EQ(131, env->GetArrayLength(reinterpret_cast<jarray>(o131)));
1901     EXPECT_EQ(132, env->GetArrayLength(reinterpret_cast<jarray>(o132)));
1902     EXPECT_EQ(133, env->GetArrayLength(reinterpret_cast<jarray>(o133)));
1903     EXPECT_EQ(134, env->GetArrayLength(reinterpret_cast<jarray>(o134)));
1904     EXPECT_EQ(135, env->GetArrayLength(reinterpret_cast<jarray>(o135)));
1905     EXPECT_EQ(136, env->GetArrayLength(reinterpret_cast<jarray>(o136)));
1906     EXPECT_EQ(137, env->GetArrayLength(reinterpret_cast<jarray>(o137)));
1907     EXPECT_EQ(138, env->GetArrayLength(reinterpret_cast<jarray>(o138)));
1908     EXPECT_EQ(139, env->GetArrayLength(reinterpret_cast<jarray>(o139)));
1909     EXPECT_EQ(140, env->GetArrayLength(reinterpret_cast<jarray>(o140)));
1910     EXPECT_EQ(141, env->GetArrayLength(reinterpret_cast<jarray>(o141)));
1911     EXPECT_EQ(142, env->GetArrayLength(reinterpret_cast<jarray>(o142)));
1912     EXPECT_EQ(143, env->GetArrayLength(reinterpret_cast<jarray>(o143)));
1913     EXPECT_EQ(144, env->GetArrayLength(reinterpret_cast<jarray>(o144)));
1914     EXPECT_EQ(145, env->GetArrayLength(reinterpret_cast<jarray>(o145)));
1915     EXPECT_EQ(146, env->GetArrayLength(reinterpret_cast<jarray>(o146)));
1916     EXPECT_EQ(147, env->GetArrayLength(reinterpret_cast<jarray>(o147)));
1917     EXPECT_EQ(148, env->GetArrayLength(reinterpret_cast<jarray>(o148)));
1918     EXPECT_EQ(149, env->GetArrayLength(reinterpret_cast<jarray>(o149)));
1919     EXPECT_EQ(150, env->GetArrayLength(reinterpret_cast<jarray>(o150)));
1920     EXPECT_EQ(151, env->GetArrayLength(reinterpret_cast<jarray>(o151)));
1921     EXPECT_EQ(152, env->GetArrayLength(reinterpret_cast<jarray>(o152)));
1922     EXPECT_EQ(153, env->GetArrayLength(reinterpret_cast<jarray>(o153)));
1923     EXPECT_EQ(154, env->GetArrayLength(reinterpret_cast<jarray>(o154)));
1924     EXPECT_EQ(155, env->GetArrayLength(reinterpret_cast<jarray>(o155)));
1925     EXPECT_EQ(156, env->GetArrayLength(reinterpret_cast<jarray>(o156)));
1926     EXPECT_EQ(157, env->GetArrayLength(reinterpret_cast<jarray>(o157)));
1927     EXPECT_EQ(158, env->GetArrayLength(reinterpret_cast<jarray>(o158)));
1928     EXPECT_EQ(159, env->GetArrayLength(reinterpret_cast<jarray>(o159)));
1929     EXPECT_EQ(160, env->GetArrayLength(reinterpret_cast<jarray>(o160)));
1930     EXPECT_EQ(161, env->GetArrayLength(reinterpret_cast<jarray>(o161)));
1931     EXPECT_EQ(162, env->GetArrayLength(reinterpret_cast<jarray>(o162)));
1932     EXPECT_EQ(163, env->GetArrayLength(reinterpret_cast<jarray>(o163)));
1933     EXPECT_EQ(164, env->GetArrayLength(reinterpret_cast<jarray>(o164)));
1934     EXPECT_EQ(165, env->GetArrayLength(reinterpret_cast<jarray>(o165)));
1935     EXPECT_EQ(166, env->GetArrayLength(reinterpret_cast<jarray>(o166)));
1936     EXPECT_EQ(167, env->GetArrayLength(reinterpret_cast<jarray>(o167)));
1937     EXPECT_EQ(168, env->GetArrayLength(reinterpret_cast<jarray>(o168)));
1938     EXPECT_EQ(169, env->GetArrayLength(reinterpret_cast<jarray>(o169)));
1939     EXPECT_EQ(170, env->GetArrayLength(reinterpret_cast<jarray>(o170)));
1940     EXPECT_EQ(171, env->GetArrayLength(reinterpret_cast<jarray>(o171)));
1941     EXPECT_EQ(172, env->GetArrayLength(reinterpret_cast<jarray>(o172)));
1942     EXPECT_EQ(173, env->GetArrayLength(reinterpret_cast<jarray>(o173)));
1943     EXPECT_EQ(174, env->GetArrayLength(reinterpret_cast<jarray>(o174)));
1944     EXPECT_EQ(175, env->GetArrayLength(reinterpret_cast<jarray>(o175)));
1945     EXPECT_EQ(176, env->GetArrayLength(reinterpret_cast<jarray>(o176)));
1946     EXPECT_EQ(177, env->GetArrayLength(reinterpret_cast<jarray>(o177)));
1947     EXPECT_EQ(178, env->GetArrayLength(reinterpret_cast<jarray>(o178)));
1948     EXPECT_EQ(179, env->GetArrayLength(reinterpret_cast<jarray>(o179)));
1949     EXPECT_EQ(180, env->GetArrayLength(reinterpret_cast<jarray>(o180)));
1950     EXPECT_EQ(181, env->GetArrayLength(reinterpret_cast<jarray>(o181)));
1951     EXPECT_EQ(182, env->GetArrayLength(reinterpret_cast<jarray>(o182)));
1952     EXPECT_EQ(183, env->GetArrayLength(reinterpret_cast<jarray>(o183)));
1953     EXPECT_EQ(184, env->GetArrayLength(reinterpret_cast<jarray>(o184)));
1954     EXPECT_EQ(185, env->GetArrayLength(reinterpret_cast<jarray>(o185)));
1955     EXPECT_EQ(186, env->GetArrayLength(reinterpret_cast<jarray>(o186)));
1956     EXPECT_EQ(187, env->GetArrayLength(reinterpret_cast<jarray>(o187)));
1957     EXPECT_EQ(188, env->GetArrayLength(reinterpret_cast<jarray>(o188)));
1958     EXPECT_EQ(189, env->GetArrayLength(reinterpret_cast<jarray>(o189)));
1959     EXPECT_EQ(190, env->GetArrayLength(reinterpret_cast<jarray>(o190)));
1960     EXPECT_EQ(191, env->GetArrayLength(reinterpret_cast<jarray>(o191)));
1961     EXPECT_EQ(192, env->GetArrayLength(reinterpret_cast<jarray>(o192)));
1962     EXPECT_EQ(193, env->GetArrayLength(reinterpret_cast<jarray>(o193)));
1963     EXPECT_EQ(194, env->GetArrayLength(reinterpret_cast<jarray>(o194)));
1964     EXPECT_EQ(195, env->GetArrayLength(reinterpret_cast<jarray>(o195)));
1965     EXPECT_EQ(196, env->GetArrayLength(reinterpret_cast<jarray>(o196)));
1966     EXPECT_EQ(197, env->GetArrayLength(reinterpret_cast<jarray>(o197)));
1967     EXPECT_EQ(198, env->GetArrayLength(reinterpret_cast<jarray>(o198)));
1968     EXPECT_EQ(199, env->GetArrayLength(reinterpret_cast<jarray>(o199)));
1969     EXPECT_EQ(200, env->GetArrayLength(reinterpret_cast<jarray>(o200)));
1970     EXPECT_EQ(201, env->GetArrayLength(reinterpret_cast<jarray>(o201)));
1971     EXPECT_EQ(202, env->GetArrayLength(reinterpret_cast<jarray>(o202)));
1972     EXPECT_EQ(203, env->GetArrayLength(reinterpret_cast<jarray>(o203)));
1973     EXPECT_EQ(204, env->GetArrayLength(reinterpret_cast<jarray>(o204)));
1974     EXPECT_EQ(205, env->GetArrayLength(reinterpret_cast<jarray>(o205)));
1975     EXPECT_EQ(206, env->GetArrayLength(reinterpret_cast<jarray>(o206)));
1976     EXPECT_EQ(207, env->GetArrayLength(reinterpret_cast<jarray>(o207)));
1977     EXPECT_EQ(208, env->GetArrayLength(reinterpret_cast<jarray>(o208)));
1978     EXPECT_EQ(209, env->GetArrayLength(reinterpret_cast<jarray>(o209)));
1979     EXPECT_EQ(210, env->GetArrayLength(reinterpret_cast<jarray>(o210)));
1980     EXPECT_EQ(211, env->GetArrayLength(reinterpret_cast<jarray>(o211)));
1981     EXPECT_EQ(212, env->GetArrayLength(reinterpret_cast<jarray>(o212)));
1982     EXPECT_EQ(213, env->GetArrayLength(reinterpret_cast<jarray>(o213)));
1983     EXPECT_EQ(214, env->GetArrayLength(reinterpret_cast<jarray>(o214)));
1984     EXPECT_EQ(215, env->GetArrayLength(reinterpret_cast<jarray>(o215)));
1985     EXPECT_EQ(216, env->GetArrayLength(reinterpret_cast<jarray>(o216)));
1986     EXPECT_EQ(217, env->GetArrayLength(reinterpret_cast<jarray>(o217)));
1987     EXPECT_EQ(218, env->GetArrayLength(reinterpret_cast<jarray>(o218)));
1988     EXPECT_EQ(219, env->GetArrayLength(reinterpret_cast<jarray>(o219)));
1989     EXPECT_EQ(220, env->GetArrayLength(reinterpret_cast<jarray>(o220)));
1990     EXPECT_EQ(221, env->GetArrayLength(reinterpret_cast<jarray>(o221)));
1991     EXPECT_EQ(222, env->GetArrayLength(reinterpret_cast<jarray>(o222)));
1992     EXPECT_EQ(223, env->GetArrayLength(reinterpret_cast<jarray>(o223)));
1993     EXPECT_EQ(224, env->GetArrayLength(reinterpret_cast<jarray>(o224)));
1994     EXPECT_EQ(225, env->GetArrayLength(reinterpret_cast<jarray>(o225)));
1995     EXPECT_EQ(226, env->GetArrayLength(reinterpret_cast<jarray>(o226)));
1996     EXPECT_EQ(227, env->GetArrayLength(reinterpret_cast<jarray>(o227)));
1997     EXPECT_EQ(228, env->GetArrayLength(reinterpret_cast<jarray>(o228)));
1998     EXPECT_EQ(229, env->GetArrayLength(reinterpret_cast<jarray>(o229)));
1999     EXPECT_EQ(230, env->GetArrayLength(reinterpret_cast<jarray>(o230)));
2000     EXPECT_EQ(231, env->GetArrayLength(reinterpret_cast<jarray>(o231)));
2001     EXPECT_EQ(232, env->GetArrayLength(reinterpret_cast<jarray>(o232)));
2002     EXPECT_EQ(233, env->GetArrayLength(reinterpret_cast<jarray>(o233)));
2003     EXPECT_EQ(234, env->GetArrayLength(reinterpret_cast<jarray>(o234)));
2004     EXPECT_EQ(235, env->GetArrayLength(reinterpret_cast<jarray>(o235)));
2005     EXPECT_EQ(236, env->GetArrayLength(reinterpret_cast<jarray>(o236)));
2006     EXPECT_EQ(237, env->GetArrayLength(reinterpret_cast<jarray>(o237)));
2007     EXPECT_EQ(238, env->GetArrayLength(reinterpret_cast<jarray>(o238)));
2008     EXPECT_EQ(239, env->GetArrayLength(reinterpret_cast<jarray>(o239)));
2009     EXPECT_EQ(240, env->GetArrayLength(reinterpret_cast<jarray>(o240)));
2010     EXPECT_EQ(241, env->GetArrayLength(reinterpret_cast<jarray>(o241)));
2011     EXPECT_EQ(242, env->GetArrayLength(reinterpret_cast<jarray>(o242)));
2012     EXPECT_EQ(243, env->GetArrayLength(reinterpret_cast<jarray>(o243)));
2013     EXPECT_EQ(244, env->GetArrayLength(reinterpret_cast<jarray>(o244)));
2014     EXPECT_EQ(245, env->GetArrayLength(reinterpret_cast<jarray>(o245)));
2015     EXPECT_EQ(246, env->GetArrayLength(reinterpret_cast<jarray>(o246)));
2016     EXPECT_EQ(247, env->GetArrayLength(reinterpret_cast<jarray>(o247)));
2017     EXPECT_EQ(248, env->GetArrayLength(reinterpret_cast<jarray>(o248)));
2018     EXPECT_EQ(249, env->GetArrayLength(reinterpret_cast<jarray>(o249)));
2019     EXPECT_EQ(250, env->GetArrayLength(reinterpret_cast<jarray>(o250)));
2020     EXPECT_EQ(251, env->GetArrayLength(reinterpret_cast<jarray>(o251)));
2021     EXPECT_EQ(252, env->GetArrayLength(reinterpret_cast<jarray>(o252)));
2022     EXPECT_EQ(253, env->GetArrayLength(reinterpret_cast<jarray>(o253)));
2023   }
2024 }
2025 
2026 const char* longSig =
2027     "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2028     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2029     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2030     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2031     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2032     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2033     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2034     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2035     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2036     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2037     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2038     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2039     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2040     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2041     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2042     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2043     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2044     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2045     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2046     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2047     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2048     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2049     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2050     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2051     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2052     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2053     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2054     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2055     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2056     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2057     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2058     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2059     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2060     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2061     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2062     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2063     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2064     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2065     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2066     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2067     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2068     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2069     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2070     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2071     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2072     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2073     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2074     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2075     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2076     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
2077     "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V";
2078 
MaxParamNumberImpl()2079 void JniCompilerTest::MaxParamNumberImpl() {
2080   SetUpForTest(false, "maxParamNumber", longSig,
2081                CURRENT_JNI_WRAPPER(Java_MyClassNatives_maxParamNumber));
2082 
2083   jvalue args[254];
2084 
2085   // First test: test with all arguments null.
2086   for (int i = 0; i < 254; ++i) {
2087     args[i].l = nullptr;
2088   }
2089 
2090   env_->CallNonvirtualVoidMethodA(jobj_, jklass_, jmethod_, args);
2091 
2092   // Second test: test with int[] objects with increasing lengths
2093   for (int i = 0; i < 254; ++i) {
2094     jintArray tmp = env_->NewIntArray(i);
2095     args[i].l = tmp;
2096     EXPECT_NE(args[i].l, nullptr);
2097   }
2098 
2099   env_->CallNonvirtualVoidMethodA(jobj_, jklass_, jmethod_, args);
2100 }
2101 
JNI_TEST(MaxParamNumber)2102 JNI_TEST(MaxParamNumber)
2103 
2104 void JniCompilerTest::WithoutImplementationImpl() {
2105   // This will lead to error messages in the log.
2106   ScopedLogSeverity sls(LogSeverity::FATAL);
2107 
2108   SetUpForTest(false, "withoutImplementation", "()V", NORMAL_OR_FAST_JNI_ONLY_NULLPTR);
2109 
2110   env_->CallVoidMethod(jobj_, jmethod_);
2111 
2112   EXPECT_TRUE(Thread::Current()->IsExceptionPending());
2113   EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
2114 }
2115 
JNI_TEST(WithoutImplementation)2116 JNI_TEST(WithoutImplementation)
2117 
2118 void JniCompilerTest::WithoutImplementationRefReturnImpl() {
2119   // This will lead to error messages in the log.
2120   ScopedLogSeverity sls(LogSeverity::FATAL);
2121 
2122   SetUpForTest(false,
2123                "withoutImplementationRefReturn",
2124                "()Ljava/lang/Object;",
2125                NORMAL_OR_FAST_JNI_ONLY_NULLPTR);
2126 
2127   env_->CallObjectMethod(jobj_, jmethod_);
2128 
2129   EXPECT_TRUE(Thread::Current()->IsExceptionPending());
2130   EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
2131 }
2132 
JNI_TEST(WithoutImplementationRefReturn)2133 JNI_TEST(WithoutImplementationRefReturn)
2134 
2135 void JniCompilerTest::StaticWithoutImplementationImpl() {
2136   // This will lead to error messages in the log.
2137   ScopedLogSeverity sls(LogSeverity::FATAL);
2138 
2139   SetUpForTest(true, "staticWithoutImplementation", "()V", nullptr);
2140 
2141   env_->CallStaticVoidMethod(jklass_, jmethod_);
2142 
2143   EXPECT_TRUE(Thread::Current()->IsExceptionPending());
2144   EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
2145 }
2146 
JNI_TEST_CRITICAL(StaticWithoutImplementation)2147 JNI_TEST_CRITICAL(StaticWithoutImplementation)
2148 
2149 void Java_MyClassNatives_stackArgsIntsFirst(JNIEnv*, jclass, jint i1, jint i2, jint i3,
2150                                             jint i4, jint i5, jint i6, jint i7, jint i8, jint i9,
2151                                             jint i10, jfloat f1, jfloat f2, jfloat f3, jfloat f4,
2152                                             jfloat f5, jfloat f6, jfloat f7, jfloat f8, jfloat f9,
2153                                             jfloat f10) {
2154   EXPECT_EQ(i1, 1);
2155   EXPECT_EQ(i2, 2);
2156   EXPECT_EQ(i3, 3);
2157   EXPECT_EQ(i4, 4);
2158   EXPECT_EQ(i5, 5);
2159   EXPECT_EQ(i6, 6);
2160   EXPECT_EQ(i7, 7);
2161   EXPECT_EQ(i8, 8);
2162   EXPECT_EQ(i9, 9);
2163   EXPECT_EQ(i10, 10);
2164 
2165   jint i11 = bit_cast<jint, jfloat>(f1);
2166   EXPECT_EQ(i11, 11);
2167   jint i12 = bit_cast<jint, jfloat>(f2);
2168   EXPECT_EQ(i12, 12);
2169   jint i13 = bit_cast<jint, jfloat>(f3);
2170   EXPECT_EQ(i13, 13);
2171   jint i14 = bit_cast<jint, jfloat>(f4);
2172   EXPECT_EQ(i14, 14);
2173   jint i15 = bit_cast<jint, jfloat>(f5);
2174   EXPECT_EQ(i15, 15);
2175   jint i16 = bit_cast<jint, jfloat>(f6);
2176   EXPECT_EQ(i16, 16);
2177   jint i17 = bit_cast<jint, jfloat>(f7);
2178   EXPECT_EQ(i17, 17);
2179   jint i18 = bit_cast<jint, jfloat>(f8);
2180   EXPECT_EQ(i18, 18);
2181   jint i19 = bit_cast<jint, jfloat>(f9);
2182   EXPECT_EQ(i19, 19);
2183   jint i20 = bit_cast<jint, jfloat>(f10);
2184   EXPECT_EQ(i20, 20);
2185 }
2186 
StackArgsIntsFirstImpl()2187 void JniCompilerTest::StackArgsIntsFirstImpl() {
2188   SetUpForTest(true, "stackArgsIntsFirst", "(IIIIIIIIIIFFFFFFFFFF)V",
2189                CURRENT_JNI_WRAPPER(Java_MyClassNatives_stackArgsIntsFirst));
2190 
2191   jint i1 = 1;
2192   jint i2 = 2;
2193   jint i3 = 3;
2194   jint i4 = 4;
2195   jint i5 = 5;
2196   jint i6 = 6;
2197   jint i7 = 7;
2198   jint i8 = 8;
2199   jint i9 = 9;
2200   jint i10 = 10;
2201 
2202   jfloat f1 = bit_cast<jfloat, jint>(11);
2203   jfloat f2 = bit_cast<jfloat, jint>(12);
2204   jfloat f3 = bit_cast<jfloat, jint>(13);
2205   jfloat f4 = bit_cast<jfloat, jint>(14);
2206   jfloat f5 = bit_cast<jfloat, jint>(15);
2207   jfloat f6 = bit_cast<jfloat, jint>(16);
2208   jfloat f7 = bit_cast<jfloat, jint>(17);
2209   jfloat f8 = bit_cast<jfloat, jint>(18);
2210   jfloat f9 = bit_cast<jfloat, jint>(19);
2211   jfloat f10 = bit_cast<jfloat, jint>(20);
2212 
2213   env_->CallStaticVoidMethod(jklass_, jmethod_, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, f1, f2,
2214                              f3, f4, f5, f6, f7, f8, f9, f10);
2215 }
2216 
JNI_TEST_CRITICAL(StackArgsIntsFirst)2217 JNI_TEST_CRITICAL(StackArgsIntsFirst)
2218 
2219 void Java_MyClassNatives_stackArgsFloatsFirst(JNIEnv*, jclass, jfloat f1, jfloat f2,
2220                                               jfloat f3, jfloat f4, jfloat f5, jfloat f6, jfloat f7,
2221                                               jfloat f8, jfloat f9, jfloat f10, jint i1, jint i2,
2222                                               jint i3, jint i4, jint i5, jint i6, jint i7, jint i8,
2223                                               jint i9, jint i10) {
2224   EXPECT_EQ(i1, 1);
2225   EXPECT_EQ(i2, 2);
2226   EXPECT_EQ(i3, 3);
2227   EXPECT_EQ(i4, 4);
2228   EXPECT_EQ(i5, 5);
2229   EXPECT_EQ(i6, 6);
2230   EXPECT_EQ(i7, 7);
2231   EXPECT_EQ(i8, 8);
2232   EXPECT_EQ(i9, 9);
2233   EXPECT_EQ(i10, 10);
2234 
2235   jint i11 = bit_cast<jint, jfloat>(f1);
2236   EXPECT_EQ(i11, 11);
2237   jint i12 = bit_cast<jint, jfloat>(f2);
2238   EXPECT_EQ(i12, 12);
2239   jint i13 = bit_cast<jint, jfloat>(f3);
2240   EXPECT_EQ(i13, 13);
2241   jint i14 = bit_cast<jint, jfloat>(f4);
2242   EXPECT_EQ(i14, 14);
2243   jint i15 = bit_cast<jint, jfloat>(f5);
2244   EXPECT_EQ(i15, 15);
2245   jint i16 = bit_cast<jint, jfloat>(f6);
2246   EXPECT_EQ(i16, 16);
2247   jint i17 = bit_cast<jint, jfloat>(f7);
2248   EXPECT_EQ(i17, 17);
2249   jint i18 = bit_cast<jint, jfloat>(f8);
2250   EXPECT_EQ(i18, 18);
2251   jint i19 = bit_cast<jint, jfloat>(f9);
2252   EXPECT_EQ(i19, 19);
2253   jint i20 = bit_cast<jint, jfloat>(f10);
2254   EXPECT_EQ(i20, 20);
2255 }
2256 
StackArgsFloatsFirstImpl()2257 void JniCompilerTest::StackArgsFloatsFirstImpl() {
2258   SetUpForTest(true, "stackArgsFloatsFirst", "(FFFFFFFFFFIIIIIIIIII)V",
2259                CURRENT_JNI_WRAPPER(Java_MyClassNatives_stackArgsFloatsFirst));
2260 
2261   jint i1 = 1;
2262   jint i2 = 2;
2263   jint i3 = 3;
2264   jint i4 = 4;
2265   jint i5 = 5;
2266   jint i6 = 6;
2267   jint i7 = 7;
2268   jint i8 = 8;
2269   jint i9 = 9;
2270   jint i10 = 10;
2271 
2272   jfloat f1 = bit_cast<jfloat, jint>(11);
2273   jfloat f2 = bit_cast<jfloat, jint>(12);
2274   jfloat f3 = bit_cast<jfloat, jint>(13);
2275   jfloat f4 = bit_cast<jfloat, jint>(14);
2276   jfloat f5 = bit_cast<jfloat, jint>(15);
2277   jfloat f6 = bit_cast<jfloat, jint>(16);
2278   jfloat f7 = bit_cast<jfloat, jint>(17);
2279   jfloat f8 = bit_cast<jfloat, jint>(18);
2280   jfloat f9 = bit_cast<jfloat, jint>(19);
2281   jfloat f10 = bit_cast<jfloat, jint>(20);
2282 
2283   env_->CallStaticVoidMethod(jklass_, jmethod_, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, i1, i2, i3,
2284                              i4, i5, i6, i7, i8, i9, i10);
2285 }
2286 
JNI_TEST_CRITICAL(StackArgsFloatsFirst)2287 JNI_TEST_CRITICAL(StackArgsFloatsFirst)
2288 
2289 void Java_MyClassNatives_stackArgsMixed(JNIEnv*, jclass, jint i1, jfloat f1, jint i2,
2290                                         jfloat f2, jint i3, jfloat f3, jint i4, jfloat f4, jint i5,
2291                                         jfloat f5, jint i6, jfloat f6, jint i7, jfloat f7, jint i8,
2292                                         jfloat f8, jint i9, jfloat f9, jint i10, jfloat f10) {
2293   EXPECT_EQ(i1, 1);
2294   EXPECT_EQ(i2, 2);
2295   EXPECT_EQ(i3, 3);
2296   EXPECT_EQ(i4, 4);
2297   EXPECT_EQ(i5, 5);
2298   EXPECT_EQ(i6, 6);
2299   EXPECT_EQ(i7, 7);
2300   EXPECT_EQ(i8, 8);
2301   EXPECT_EQ(i9, 9);
2302   EXPECT_EQ(i10, 10);
2303 
2304   jint i11 = bit_cast<jint, jfloat>(f1);
2305   EXPECT_EQ(i11, 11);
2306   jint i12 = bit_cast<jint, jfloat>(f2);
2307   EXPECT_EQ(i12, 12);
2308   jint i13 = bit_cast<jint, jfloat>(f3);
2309   EXPECT_EQ(i13, 13);
2310   jint i14 = bit_cast<jint, jfloat>(f4);
2311   EXPECT_EQ(i14, 14);
2312   jint i15 = bit_cast<jint, jfloat>(f5);
2313   EXPECT_EQ(i15, 15);
2314   jint i16 = bit_cast<jint, jfloat>(f6);
2315   EXPECT_EQ(i16, 16);
2316   jint i17 = bit_cast<jint, jfloat>(f7);
2317   EXPECT_EQ(i17, 17);
2318   jint i18 = bit_cast<jint, jfloat>(f8);
2319   EXPECT_EQ(i18, 18);
2320   jint i19 = bit_cast<jint, jfloat>(f9);
2321   EXPECT_EQ(i19, 19);
2322   jint i20 = bit_cast<jint, jfloat>(f10);
2323   EXPECT_EQ(i20, 20);
2324 }
2325 
StackArgsMixedImpl()2326 void JniCompilerTest::StackArgsMixedImpl() {
2327   SetUpForTest(true, "stackArgsMixed", "(IFIFIFIFIFIFIFIFIFIF)V",
2328                CURRENT_JNI_WRAPPER(Java_MyClassNatives_stackArgsMixed));
2329 
2330   jint i1 = 1;
2331   jint i2 = 2;
2332   jint i3 = 3;
2333   jint i4 = 4;
2334   jint i5 = 5;
2335   jint i6 = 6;
2336   jint i7 = 7;
2337   jint i8 = 8;
2338   jint i9 = 9;
2339   jint i10 = 10;
2340 
2341   jfloat f1 = bit_cast<jfloat, jint>(11);
2342   jfloat f2 = bit_cast<jfloat, jint>(12);
2343   jfloat f3 = bit_cast<jfloat, jint>(13);
2344   jfloat f4 = bit_cast<jfloat, jint>(14);
2345   jfloat f5 = bit_cast<jfloat, jint>(15);
2346   jfloat f6 = bit_cast<jfloat, jint>(16);
2347   jfloat f7 = bit_cast<jfloat, jint>(17);
2348   jfloat f8 = bit_cast<jfloat, jint>(18);
2349   jfloat f9 = bit_cast<jfloat, jint>(19);
2350   jfloat f10 = bit_cast<jfloat, jint>(20);
2351 
2352   env_->CallStaticVoidMethod(jklass_, jmethod_, i1, f1, i2, f2, i3, f3, i4, f4, i5, f5, i6, f6, i7,
2353                              f7, i8, f8, i9, f9, i10, f10);
2354 }
2355 
JNI_TEST_CRITICAL(StackArgsMixed)2356 JNI_TEST_CRITICAL(StackArgsMixed)
2357 
2358 void Java_MyClassNatives_normalNative(JNIEnv*, jclass) {
2359   // Intentionally left empty.
2360 }
2361 
2362 // Methods not annotated with anything are not considered "fast native"
2363 // -- Check that the annotation lookup does not find it.
NormalNativeImpl()2364 void JniCompilerTest::NormalNativeImpl() {
2365   SetUpForTest(/* direct= */ true,
2366                "normalNative",
2367                "()V",
2368                CURRENT_JNI_WRAPPER(Java_MyClassNatives_normalNative));
2369 
2370   ArtMethod* method = jni::DecodeArtMethod(jmethod_);
2371   ASSERT_TRUE(method != nullptr);
2372 
2373   EXPECT_FALSE(method->IsCriticalNative());
2374   EXPECT_FALSE(method->IsFastNative());
2375 }
2376 
2377 // TODO: just rename the java functions  to the standard convention and remove duplicated tests
JNI_TEST_NORMAL_ONLY(NormalNative)2378 JNI_TEST_NORMAL_ONLY(NormalNative)
2379 
2380 // Methods annotated with @FastNative are considered "fast native"
2381 // -- Check that the annotation lookup succeeds.
2382 void Java_MyClassNatives_fastNative(JNIEnv*, jclass) {
2383   // Intentionally left empty.
2384 }
2385 
FastNativeImpl()2386 void JniCompilerTest::FastNativeImpl() {
2387   SetUpForTest(/* direct= */ true,
2388                "fastNative",
2389                "()V",
2390                CURRENT_JNI_WRAPPER(Java_MyClassNatives_fastNative));
2391 
2392   ArtMethod* method = jni::DecodeArtMethod(jmethod_);
2393   ASSERT_TRUE(method != nullptr);
2394 
2395   EXPECT_FALSE(method->IsCriticalNative());
2396   EXPECT_TRUE(method->IsFastNative());
2397 }
2398 
2399 // TODO: just rename the java functions  to the standard convention and remove duplicated tests
2400 JNI_TEST_NORMAL_ONLY(FastNative)
2401 
2402 int gJava_myClassNatives_criticalNative_calls[kJniKindCount] = {};
2403 // Methods annotated with @CriticalNative are considered "critical native"
2404 // -- Check that the annotation lookup succeeds.
Java_MyClassNatives_criticalNative()2405 void Java_MyClassNatives_criticalNative() {
2406   gJava_myClassNatives_criticalNative_calls[gCurrentJni]++;
2407 }
2408 
CriticalNativeImpl()2409 void JniCompilerTest::CriticalNativeImpl() {
2410   SetUpForTest(/* direct= */ true,
2411                // Important: Don't change the "current jni" yet to avoid a method name suffix.
2412                "criticalNative",
2413                "()V",
2414                // TODO: Use CURRENT_JNI_WRAPPER instead which is more generic.
2415                reinterpret_cast<void*>(&Java_MyClassNatives_criticalNative));
2416 
2417   // TODO: remove this manual updating of the current JNI. Merge with the other tests.
2418   UpdateCurrentJni(JniKind::kCritical);
2419   ASSERT_TRUE(IsCurrentJniCritical());
2420 
2421   ArtMethod* method = jni::DecodeArtMethod(jmethod_);
2422   ASSERT_TRUE(method != nullptr);
2423 
2424   EXPECT_TRUE(method->IsCriticalNative());
2425   EXPECT_FALSE(method->IsFastNative());
2426 
2427   EXPECT_EQ(0, gJava_myClassNatives_criticalNative_calls[gCurrentJni]);
2428   env_->CallStaticVoidMethod(jklass_, jmethod_);
2429   EXPECT_EQ(1, gJava_myClassNatives_criticalNative_calls[gCurrentJni]);
2430 
2431   gJava_myClassNatives_criticalNative_calls[gCurrentJni] = 0;
2432 }
2433 
2434 // TODO: just rename the java functions  to the standard convention and remove duplicated tests
2435 JNI_TEST_NORMAL_ONLY(CriticalNative)
2436 
2437 }  // namespace art
2438