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