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