blob: dc66234c6f88fd0c65954102a57beb584d201b20 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersb033c752011-07-20 12:22:35 -070016
Ian Rogers700a4022014-05-19 16:49:03 -070017#include <memory>
Igor Murashkin367f3dd2016-09-01 17:00:24 -070018#include <type_traits>
Ian Rogers700a4022014-05-19 16:49:03 -070019
Nicolas Geoffray54accbc2014-08-13 03:40:45 +010020#include <math.h>
21
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Pavle Batuta837e72a2016-03-16 11:31:46 +010023#include "base/bit_utils.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_compiler_test.h"
Igor Murashkin367f3dd2016-09-01 17:00:24 -070026#include "compiler.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "gtest/gtest.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070029#include "indirect_reference_table.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070030#include "java_vm_ext.h"
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070031#include "jni_internal.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032#include "mem_map.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class_loader.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070035#include "mirror/object-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070036#include "mirror/object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/stack_trace_element.h"
Steven Morelande431e272017-07-18 16:53:49 -070038#include "nativehelper/ScopedLocalRef.h"
Dimitry Ivanov5edb0632016-04-29 11:14:25 -070039#include "nativeloader/native_loader.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070041#include "scoped_thread_state_change-inl.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042#include "thread.h"
Ian Rogersb033c752011-07-20 12:22:35 -070043
Elliott Hughesb264f082012-04-06 17:10:10 -070044extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_bar(JNIEnv*, jobject, jint count) {
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -080045 return count + 1;
46}
47
Elliott Hughesb264f082012-04-06 17:10:10 -070048extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_sbar(JNIEnv*, jclass, jint count) {
Ian Rogers1cefdbd2012-02-29 09:34:50 -080049 return count + 1;
50}
51
Roland Levillain97c46462017-05-11 14:04:03 +010052// TODO: In the Baker read barrier configuration, add checks to ensure
53// the Marking Register's value is correct.
54
Ian Rogersb033c752011-07-20 12:22:35 -070055namespace art {
56
Igor Murashkin367f3dd2016-09-01 17:00:24 -070057enum class JniKind {
Vladimir Markob0a6aee2017-10-27 10:34:04 +010058 kNormal, // Regular kind of un-annotated natives.
59 kFast, // Native method annotated with @FastNative.
60 kCritical, // Native method annotated with @CriticalNative.
61 kCount // How many different types of JNIs we can have.
Igor Murashkin367f3dd2016-09-01 17:00:24 -070062};
63
64// Used to initialize array sizes that want to have different state per current jni.
65static constexpr size_t kJniKindCount = static_cast<size_t>(JniKind::kCount);
66// Do not use directly, use the helpers instead.
67uint32_t gCurrentJni = static_cast<uint32_t>(JniKind::kNormal);
68
69// Is the current native method under test @CriticalNative?
70static 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?
75static bool IsCurrentJniNormal() {
76 return gCurrentJni == static_cast<uint32_t>(JniKind::kNormal);
77}
78
Roland Levillainef012222017-06-21 16:28:06 +010079// Signify that a different kind of JNI is about to be tested.
Igor Murashkin367f3dd2016-09-01 17:00:24 -070080static void UpdateCurrentJni(JniKind kind) {
81 gCurrentJni = static_cast<uint32_t>(kind);
82}
83
84// (Match the name suffixes of native methods in MyClassNatives.java)
85static 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.
106static JNIEnv* const kCriticalDummyJniEnv = reinterpret_cast<JNIEnv*>(0xDEADFEAD);
107static 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).
112template <typename T, typename ... Args>
113struct is_any_of;
114
115template <typename T, typename U, typename ... Args>
116struct 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
121template <typename T, typename U>
122struct 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.
128template <typename T>
129struct 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
137template <typename ... Args>
138struct count_refs_helper {
139 using value_type = size_t;
140 static constexpr const size_t value = 0;
141};
142
143template <typename Arg, typename ... Args>
144struct 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
150template <typename T, T fn>
151struct count_refs_fn_helper;
152
153template <typename R, typename ... Args, R fn(Args...)>
154struct 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)
162template <typename T, T fn>
163struct count_refs : public count_refs_fn_helper<T, fn> {};
164
165// Base case: No parameters = 0 refs.
166size_t count_nonnull_refs_helper() {
167 return 0;
168}
169
170// SFINAE for ref types. 1 if non-null, 0 otherwise.
171template <typename T>
172size_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.
179template <typename T>
180size_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.
187template <typename T, typename ... Args>
188size_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.
198template <typename ... Args>
199size_t count_nonnull_refs(Args ... args) {
200 return count_nonnull_refs_helper(args...);
201}
202
203template <typename T, T fn>
204struct remove_extra_parameters_helper;
205
206template <typename R, typename Arg1, typename Arg2, typename ... Args, R fn(Arg1, Arg2, Args...)>
207struct remove_extra_parameters_helper<R(Arg1, Arg2, Args...), fn> {
208 // Note: Do not use Args&& here to maintain C-style parameter types.
209 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,...)
219template <typename T, T fn>
220struct jni_remove_extra_parameters : public remove_extra_parameters_helper<T, fn> {};
221
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800222class JniCompilerTest : public CommonCompilerTest {
Ian Rogersb033c752011-07-20 12:22:35 -0700223 protected:
Andreas Gampe6e498692014-08-18 16:43:12 -0700224 void SetUp() OVERRIDE {
225 CommonCompilerTest::SetUp();
226 check_generic_jni_ = false;
227 }
228
Dimitry Ivanov5edb0632016-04-29 11:14:25 -0700229 void TearDown() OVERRIDE {
230 android::ResetNativeLoader();
231 CommonCompilerTest::TearDown();
232 }
233
Andreas Gampe6e498692014-08-18 16:43:12 -0700234 void SetCheckGenericJni(bool generic) {
235 check_generic_jni_ = generic;
236 }
237
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700238 private:
239 void CompileForTest(jobject class_loader,
240 bool direct,
241 const char* method_name,
242 const char* method_sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700244 StackHandleScope<1> hs(soa.Self());
245 Handle<mirror::ClassLoader> loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700246 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Brian Carlstrom25c33252011-09-18 15:58:35 -0700247 // Compile the native method before starting the runtime
Ian Rogers98379392014-02-24 16:53:16 -0800248 mirror::Class* c = class_linker_->FindClass(soa.Self(), "LMyClassNatives;", loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 const auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100250 ArtMethod* method = c->FindClassMethod(method_name, method_sig, pointer_size);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700251 ASSERT_TRUE(method != nullptr) << method_name << " " << method_sig;
Vladimir Markoba118822017-06-12 15:41:56 +0100252 ASSERT_EQ(direct, method->IsDirect()) << method_name << " " << method_sig;
Andreas Gampe6e498692014-08-18 16:43:12 -0700253 if (check_generic_jni_) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700254 method->SetEntryPointFromQuickCompiledCode(class_linker_->GetRuntimeQuickGenericJniStub());
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100255 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700256 const void* code = method->GetEntryPointFromQuickCompiledCode();
257 if (code == nullptr || class_linker_->IsQuickGenericJniStub(code)) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100258 CompileMethod(method);
259 ASSERT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr)
260 << method_name << " " << method_sig;
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100261 }
Brian Carlstrom25c33252011-09-18 15:58:35 -0700262 }
Brian Carlstrom25c33252011-09-18 15:58:35 -0700263 }
264
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700265 protected:
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
278 void SetUpForTest(bool direct,
279 const char* method_name_orig,
280 const char* method_sig,
Andreas Gampe6e498692014-08-18 16:43:12 -0700281 void* native_fnptr) {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700282 // 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 // Initialize class loader and compile method when runtime not started.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700288 if (!runtime_->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 {
290 ScopedObjectAccess soa(Thread::Current());
291 class_loader_ = LoadDex("MyClassNatives");
292 }
Andreas Gampe6e498692014-08-18 16:43:12 -0700293 CompileForTest(class_loader_, direct, method_name, method_sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 // Start runtime.
295 Thread::Current()->TransitionFromSuspendedToRunnable();
Dimitry Ivanov5edb0632016-04-29 11:14:25 -0700296 android::InitializeNativeLoader();
Dimitry Ivanovc544f342016-05-09 16:26:13 -0700297 bool started = runtime_->Start();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700298 CHECK(started);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700299 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300 // JNI operations after runtime start.
Brian Carlstrom25c33252011-09-18 15:58:35 -0700301 env_ = Thread::Current()->GetJniEnv();
Elliott Hughesb264f082012-04-06 17:10:10 -0700302 jklass_ = env_->FindClass("MyClassNatives");
Andreas Gampecf4035a2014-05-28 22:43:01 -0700303 ASSERT_TRUE(jklass_ != nullptr) << method_name << " " << method_sig;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700304
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700305 if (direct) {
306 jmethod_ = env_->GetStaticMethodID(jklass_, method_name, method_sig);
307 } else {
308 jmethod_ = env_->GetMethodID(jklass_, method_name, method_sig);
309 }
Andreas Gampecf4035a2014-05-28 22:43:01 -0700310 ASSERT_TRUE(jmethod_ != nullptr) << method_name << " " << method_sig;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700311
Andreas Gampecf4035a2014-05-28 22:43:01 -0700312 if (native_fnptr != nullptr) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700313 JNINativeMethod methods[] = { { method_name, method_sig, native_fnptr } };
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700314 ASSERT_EQ(JNI_OK, env_->RegisterNatives(jklass_, methods, 1))
315 << method_name << " " << method_sig;
Ian Rogersbdb03912011-09-14 00:55:44 -0700316 } else {
317 env_->UnregisterNatives(jklass_);
Shih-wei Liao31384c52011-09-06 15:27:45 -0700318 }
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700319
320 jmethodID constructor = env_->GetMethodID(jklass_, "<init>", "()V");
321 jobj_ = env_->NewObject(jklass_, constructor);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700322 ASSERT_TRUE(jobj_ != nullptr) << method_name << " " << method_sig;
Ian Rogersb033c752011-07-20 12:22:35 -0700323 }
324
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700325 public:
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700326 // Available as statics so our JNI handlers can access these.
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700327 static jclass jklass_;
328 static jobject jobj_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329 static jobject class_loader_;
330
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700331 protected:
Andreas Gampe6e498692014-08-18 16:43:12 -0700332 // We have to list the methods here so we can share them between default and generic JNI.
333 void CompileAndRunNoArgMethodImpl();
334 void CompileAndRunIntMethodThroughStubImpl();
335 void CompileAndRunStaticIntMethodThroughStubImpl();
336 void CompileAndRunIntMethodImpl();
337 void CompileAndRunIntIntMethodImpl();
338 void CompileAndRunLongLongMethodImpl();
339 void CompileAndRunDoubleDoubleMethodImpl();
340 void CompileAndRun_fooJJ_synchronizedImpl();
341 void CompileAndRunIntObjectObjectMethodImpl();
342 void CompileAndRunStaticIntIntMethodImpl();
343 void CompileAndRunStaticDoubleDoubleMethodImpl();
344 void RunStaticLogDoubleMethodImpl();
345 void RunStaticLogFloatMethodImpl();
346 void RunStaticReturnTrueImpl();
347 void RunStaticReturnFalseImpl();
348 void RunGenericStaticReturnIntImpl();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700349 void RunGenericStaticReturnDoubleImpl();
350 void RunGenericStaticReturnLongImpl();
Andreas Gampe6e498692014-08-18 16:43:12 -0700351 void CompileAndRunStaticIntObjectObjectMethodImpl();
352 void CompileAndRunStaticSynchronizedIntObjectObjectMethodImpl();
353 void ExceptionHandlingImpl();
354 void NativeStackTraceElementImpl();
355 void ReturnGlobalRefImpl();
356 void LocalReferenceTableClearingTestImpl();
357 void JavaLangSystemArrayCopyImpl();
358 void CompareAndSwapIntImpl();
359 void GetTextImpl();
360 void GetSinkPropertiesNativeImpl();
361 void UpcallReturnTypeChecking_InstanceImpl();
362 void UpcallReturnTypeChecking_StaticImpl();
363 void UpcallArgumentTypeChecking_InstanceImpl();
364 void UpcallArgumentTypeChecking_StaticImpl();
365 void CompileAndRunFloatFloatMethodImpl();
366 void CheckParameterAlignImpl();
367 void MaxParamNumberImpl();
368 void WithoutImplementationImpl();
Andreas Gampe48ee3562015-04-10 19:57:29 -0700369 void WithoutImplementationRefReturnImpl();
Andreas Gampe6e498692014-08-18 16:43:12 -0700370 void StackArgsIntsFirstImpl();
371 void StackArgsFloatsFirstImpl();
372 void StackArgsMixedImpl();
Pavle Batuta837e72a2016-03-16 11:31:46 +0100373#if defined(__mips__) && defined(__LP64__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
Lazar Trsicf652d602015-06-24 16:30:21 +0200374 void StackArgsSignExtendedMips64Impl();
Pavle Batuta837e72a2016-03-16 11:31:46 +0100375#endif
Andreas Gampe6e498692014-08-18 16:43:12 -0700376
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700377 void NormalNativeImpl();
378 void FastNativeImpl();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700379 void CriticalNativeImpl();
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700380
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700381 JNIEnv* env_;
382 jmethodID jmethod_;
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700383
384 private:
Andreas Gampe6e498692014-08-18 16:43:12 -0700385 bool check_generic_jni_;
Ian Rogersb033c752011-07-20 12:22:35 -0700386};
387
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700388jclass JniCompilerTest::jklass_;
389jobject JniCompilerTest::jobj_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390jobject JniCompilerTest::class_loader_;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700391
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700392// Test the normal compiler and normal generic JNI only.
393// The following features are unsupported in @FastNative:
394// 1) JNI stubs (lookup via dlsym) when methods aren't explicitly registered
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700395// 2) synchronized keyword
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700396// -- TODO: We can support (1) if we remove the mutator lock assert during stub lookup.
397# define JNI_TEST_NORMAL_ONLY(TestName) \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700398 TEST_F(JniCompilerTest, TestName ## NormalCompiler) { \
Igor Murashkina51d8b72016-10-05 14:33:30 -0700399 ScopedCheckHandleScope top_handle_scope_check; \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700400 SCOPED_TRACE("Normal JNI with compiler"); \
401 gCurrentJni = static_cast<uint32_t>(JniKind::kNormal); \
Andreas Gampe6e498692014-08-18 16:43:12 -0700402 TestName ## Impl(); \
403 } \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700404 TEST_F(JniCompilerTest, TestName ## NormalGeneric) { \
Igor Murashkina51d8b72016-10-05 14:33:30 -0700405 ScopedCheckHandleScope top_handle_scope_check; \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700406 SCOPED_TRACE("Normal JNI with generic"); \
407 gCurrentJni = static_cast<uint32_t>(JniKind::kNormal); \
Andreas Gampe6e498692014-08-18 16:43:12 -0700408 SetCheckGenericJni(true); \
409 TestName ## Impl(); \
410 }
Andreas Gampecf4035a2014-05-28 22:43:01 -0700411
Igor Murashkin06a04e02016-09-13 15:57:37 -0700412// Test (normal, @FastNative) x (compiler, generic).
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700413#define JNI_TEST(TestName) \
414 JNI_TEST_NORMAL_ONLY(TestName) \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700415 TEST_F(JniCompilerTest, TestName ## FastCompiler) { \
Igor Murashkina51d8b72016-10-05 14:33:30 -0700416 ScopedCheckHandleScope top_handle_scope_check; \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700417 SCOPED_TRACE("@FastNative JNI with compiler"); \
418 gCurrentJni = static_cast<uint32_t>(JniKind::kFast); \
419 TestName ## Impl(); \
420 } \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700421 \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700422 TEST_F(JniCompilerTest, TestName ## FastGeneric) { \
Igor Murashkina51d8b72016-10-05 14:33:30 -0700423 ScopedCheckHandleScope top_handle_scope_check; \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700424 SCOPED_TRACE("@FastNative JNI with generic"); \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700425 gCurrentJni = static_cast<uint32_t>(JniKind::kFast); \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700426 SetCheckGenericJni(true); \
427 TestName ## Impl(); \
428 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700429
Igor Murashkin06a04e02016-09-13 15:57:37 -0700430// Test (@CriticalNative) x (compiler, generic) only.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700431#define JNI_TEST_CRITICAL_ONLY(TestName) \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700432 TEST_F(JniCompilerTest, TestName ## CriticalCompiler) { \
Igor Murashkina51d8b72016-10-05 14:33:30 -0700433 ScopedCheckHandleScope top_handle_scope_check; \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700434 SCOPED_TRACE("@CriticalNative JNI with compiler"); \
435 gCurrentJni = static_cast<uint32_t>(JniKind::kCritical); \
436 TestName ## Impl(); \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700437 } \
438 TEST_F(JniCompilerTest, TestName ## CriticalGeneric) { \
Igor Murashkina51d8b72016-10-05 14:33:30 -0700439 ScopedCheckHandleScope top_handle_scope_check; \
Igor Murashkin06a04e02016-09-13 15:57:37 -0700440 SCOPED_TRACE("@CriticalNative JNI with generic"); \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700441 gCurrentJni = static_cast<uint32_t>(JniKind::kCritical); \
Igor Murashkin294a9152016-09-28 13:23:19 -0700442 SetCheckGenericJni(true); \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700443 TestName ## Impl(); \
444 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700445
446// Test everything: (normal, @FastNative, @CriticalNative) x (compiler, generic).
447#define JNI_TEST_CRITICAL(TestName) \
448 JNI_TEST(TestName) \
449 JNI_TEST_CRITICAL_ONLY(TestName) \
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700450
451static void expectValidThreadState() {
452 // Normal JNI always transitions to "Native". Other JNIs stay in the "Runnable" state.
453 if (IsCurrentJniNormal()) {
454 EXPECT_EQ(kNative, Thread::Current()->GetState());
455 } else {
456 EXPECT_EQ(kRunnable, Thread::Current()->GetState());
457 }
458}
459
460#define EXPECT_THREAD_STATE_FOR_CURRENT_JNI() expectValidThreadState()
461
462static void expectValidMutatorLockHeld() {
463 if (IsCurrentJniNormal()) {
464 Locks::mutator_lock_->AssertNotHeld(Thread::Current());
465 } else {
466 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
467 }
468}
469
470#define EXPECT_MUTATOR_LOCK_FOR_CURRENT_JNI() expectValidMutatorLockHeld()
471
472static void expectValidJniEnvAndObject(JNIEnv* env, jobject thisObj) {
473 if (!IsCurrentJniCritical()) {
474 EXPECT_EQ(Thread::Current()->GetJniEnv(), env);
475 ASSERT_TRUE(thisObj != nullptr);
476 EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_));
477 } else {
478 LOG(FATAL) << "Objects are not supported for @CriticalNative, why is this being tested?";
479 UNREACHABLE();
480 }
481}
482
483// Validates the JNIEnv to be the same as the current thread's JNIEnv, and makes sure
484// that the object here is an instance of the class we registered the method with.
485//
486// Hard-fails if this somehow gets invoked for @CriticalNative since objects are unsupported.
487#define EXPECT_JNI_ENV_AND_OBJECT_FOR_CURRENT_JNI(env, thisObj) \
488 expectValidJniEnvAndObject(env, thisObj)
489
490static void expectValidJniEnvAndClass(JNIEnv* env, jclass kls) {
491 if (!IsCurrentJniCritical()) {
492 EXPECT_EQ(Thread::Current()->GetJniEnv(), env);
493 ASSERT_TRUE(kls != nullptr);
494 EXPECT_TRUE(env->IsSameObject(static_cast<jobject>(JniCompilerTest::jklass_),
495 static_cast<jobject>(kls)));
496 } else {
497 // This is pretty much vacuously true but catch any testing setup mistakes.
498 EXPECT_EQ(env, kCriticalDummyJniEnv);
499 EXPECT_EQ(kls, kCriticalDummyJniClass);
500 }
501}
502
503// Validates the JNIEnv is the same as the current thread's JNIenv, and makes sure
504// that the jclass we got in the JNI handler is the same one as the class the method was looked
505// up for.
506//
507// (Checks are skipped for @CriticalNative since the two values are dummy).
508#define EXPECT_JNI_ENV_AND_CLASS_FOR_CURRENT_JNI(env, kls) expectValidJniEnvAndClass(env, kls)
509
510// Temporarily disable the EXPECT_NUM_STACK_REFERENCES check (for a single test).
511struct ScopedDisableCheckNumStackReferences {
512 ScopedDisableCheckNumStackReferences() {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700513 CHECK(sCheckNumStackReferences); // No nested support.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700514 sCheckNumStackReferences = false;
515 }
516
517 ~ScopedDisableCheckNumStackReferences() {
518 sCheckNumStackReferences = true;
519 }
520
521 static bool sCheckNumStackReferences;
522};
523
524bool ScopedDisableCheckNumStackReferences::sCheckNumStackReferences = true;
525
Igor Murashkina51d8b72016-10-05 14:33:30 -0700526// Check that the handle scope at the start of this block is the same as the handle scope at the end of the block.
527struct ScopedCheckHandleScope {
Igor Murashkin42298112016-10-06 10:51:11 -0700528 ScopedCheckHandleScope() : handle_scope_(Thread::Current()->GetTopHandleScope()) {
Igor Murashkina51d8b72016-10-05 14:33:30 -0700529 }
530
531 ~ScopedCheckHandleScope() {
532 EXPECT_EQ(handle_scope_, Thread::Current()->GetTopHandleScope())
533 << "Top-most handle scope must be the same after all the JNI "
534 << "invocations have finished (as before they were invoked).";
535 }
536
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700537 BaseHandleScope* const handle_scope_;
Igor Murashkina51d8b72016-10-05 14:33:30 -0700538};
539
Andreas Gampe0a855762016-10-26 13:43:14 -0700540// Number of references allocated in JNI ShadowFrames on the given thread.
541static size_t NumJniShadowFrameReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
542 return self->GetManagedStack()->NumJniShadowFrameReferences();
543}
544
545// Number of references in handle scope on the given thread.
546static size_t NumHandleReferences(Thread* self) {
547 size_t count = 0;
548 for (BaseHandleScope* cur = self->GetTopHandleScope(); cur != nullptr; cur = cur->GetLink()) {
549 count += cur->NumberOfReferences();
550 }
551 return count;
552}
553
554// Number of references allocated in handle scopes & JNI shadow frames on this thread.
555static size_t NumStackReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
556 return NumHandleReferences(self) + NumJniShadowFrameReferences(self);
557}
558
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700559static void expectNumStackReferences(size_t val1, size_t val2) {
560 // In rare cases when JNI functions call themselves recursively,
561 // disable this test because it will have a false negative.
562 if (!IsCurrentJniCritical() && ScopedDisableCheckNumStackReferences::sCheckNumStackReferences) {
563 /* @CriticalNative doesn't build a HandleScope, so this test is meaningless then. */
564 ScopedObjectAccess soa(Thread::Current());
565
Andreas Gampe0a855762016-10-26 13:43:14 -0700566 size_t actual_num = NumStackReferences(Thread::Current());
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700567 // XX: Not too sure what's going on.
568 // Sometimes null references get placed and sometimes they don't?
569 EXPECT_TRUE(val1 == actual_num || val2 == actual_num)
570 << "expected either " << val1 << " or " << val2
571 << " number of stack references, but got: " << actual_num;
572 }
573}
574
575#define EXPECT_NUM_STACK_REFERENCES(val1, val2) expectNumStackReferences(val1, val2)
576
577template <typename T, T fn>
578struct make_jni_test_decorator;
579
580// Decorator for "static" JNI callbacks.
581template <typename R, typename ... Args, R fn(JNIEnv*, jclass, Args...)>
582struct make_jni_test_decorator<R(JNIEnv*, jclass kls, Args...), fn> {
583 static R apply(JNIEnv* env, jclass kls, Args ... args) {
584 EXPECT_THREAD_STATE_FOR_CURRENT_JNI();
585 EXPECT_MUTATOR_LOCK_FOR_CURRENT_JNI();
586 EXPECT_JNI_ENV_AND_CLASS_FOR_CURRENT_JNI(env, kls);
587 // All incoming parameters + the jclass get put into the transition's StackHandleScope.
588 EXPECT_NUM_STACK_REFERENCES(count_nonnull_refs(kls, args...),
589 (count_refs_helper<jclass, Args...>::value));
590
591 return fn(env, kls, args...);
592 }
593};
594
595// Decorator for instance JNI callbacks.
596template <typename R, typename ... Args, R fn(JNIEnv*, jobject, Args...)>
597struct make_jni_test_decorator<R(JNIEnv*, jobject, Args...), fn> {
598 static R apply(JNIEnv* env, jobject thisObj, Args ... args) {
599 EXPECT_THREAD_STATE_FOR_CURRENT_JNI();
600 EXPECT_MUTATOR_LOCK_FOR_CURRENT_JNI();
601 EXPECT_JNI_ENV_AND_OBJECT_FOR_CURRENT_JNI(env, thisObj);
602 // All incoming parameters + the implicit 'this' get put into the transition's StackHandleScope.
603 EXPECT_NUM_STACK_REFERENCES(count_nonnull_refs(thisObj, args...),
604 (count_refs_helper<jobject, Args...>::value));
605
606 return fn(env, thisObj, args...);
607 }
608};
609
610// Decorate the regular JNI callee with the extra gtest checks.
611// This way we can have common test logic for everything generic like checking if a lock is held,
612// checking handle scope state, etc.
613#define MAKE_JNI_TEST_DECORATOR(fn) make_jni_test_decorator<decltype(fn), (fn)>::apply
614
615// Convert function f(JNIEnv*,jclass,a,b,c,d...) into f2(a,b,c,d...)
616// -- This way we don't have to write out each implementation twice for @CriticalNative.
617#define JNI_CRITICAL_WRAPPER(func) jni_remove_extra_parameters<decltype(func), (func)>::apply
618// Get a function pointer whose calling convention either matches a regular native
619// or a critical native depending on which kind of jni is currently under test.
620// -- This also has the benefit of genering a compile time error if the 'func' doesn't properly
621// have JNIEnv and jclass parameters first.
622#define CURRENT_JNI_WRAPPER(func) \
623 (IsCurrentJniCritical() \
624 ? reinterpret_cast<void*>(&JNI_CRITICAL_WRAPPER(MAKE_JNI_TEST_DECORATOR(func))) \
625 : reinterpret_cast<void*>(&MAKE_JNI_TEST_DECORATOR(func)))
626
627// Do the opposite of the above. Do *not* wrap the function, instead just cast it to a void*.
628// Only for "TEST_JNI_NORMAL_ONLY" configs, and it inserts a test assert to ensure this is the case.
629#define NORMAL_JNI_ONLY_NOWRAP(func) \
630 ({ ASSERT_TRUE(IsCurrentJniNormal()); reinterpret_cast<void*>(&(func)); })
631// Same as above, but with nullptr. When we want to test the stub functionality.
632#define NORMAL_JNI_ONLY_NULLPTR \
633 ({ ASSERT_TRUE(IsCurrentJniNormal()); nullptr; })
634
635
636int gJava_MyClassNatives_foo_calls[kJniKindCount] = {};
637void Java_MyClassNatives_foo(JNIEnv*, jobject) {
638 gJava_MyClassNatives_foo_calls[gCurrentJni]++;
Ian Rogersb033c752011-07-20 12:22:35 -0700639}
640
Andreas Gampe6e498692014-08-18 16:43:12 -0700641void JniCompilerTest::CompileAndRunNoArgMethodImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700642 SetUpForTest(false, "foo", "()V", CURRENT_JNI_WRAPPER(Java_MyClassNatives_foo));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700643
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700644 EXPECT_EQ(0, gJava_MyClassNatives_foo_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700645 env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700646 EXPECT_EQ(1, gJava_MyClassNatives_foo_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700647 env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700648 EXPECT_EQ(2, gJava_MyClassNatives_foo_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700649
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700650 gJava_MyClassNatives_foo_calls[gCurrentJni] = 0;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700651}
652
Andreas Gampe6e498692014-08-18 16:43:12 -0700653JNI_TEST(CompileAndRunNoArgMethod)
654
655void JniCompilerTest::CompileAndRunIntMethodThroughStubImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700656 SetUpForTest(false, "bar", "(I)I", NORMAL_JNI_ONLY_NULLPTR);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700657 // calling through stub will link with &Java_MyClassNatives_bar
Shih-wei Liao31384c52011-09-06 15:27:45 -0700658
Shih-wei Liao31384c52011-09-06 15:27:45 -0700659 std::string reason;
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -0800660 ASSERT_TRUE(Runtime::Current()->GetJavaVM()->
Andreas Gampe473191c2017-12-28 16:55:31 -0800661 LoadNativeLibrary(env_, "", class_loader_, &reason))
Ian Rogers68d8b422014-07-17 11:09:10 -0700662 << reason;
Shih-wei Liao31384c52011-09-06 15:27:45 -0700663
664 jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 24);
665 EXPECT_EQ(25, result);
666}
667
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700668// TODO: Support @FastNative and @CriticalNative through stubs.
669JNI_TEST_NORMAL_ONLY(CompileAndRunIntMethodThroughStub)
Andreas Gampe6e498692014-08-18 16:43:12 -0700670
671void JniCompilerTest::CompileAndRunStaticIntMethodThroughStubImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700672 SetUpForTest(true, "sbar", "(I)I", NORMAL_JNI_ONLY_NULLPTR);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700673 // calling through stub will link with &Java_MyClassNatives_sbar
Ian Rogers1cefdbd2012-02-29 09:34:50 -0800674
675 std::string reason;
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -0800676 ASSERT_TRUE(Runtime::Current()->GetJavaVM()->
Andreas Gampe473191c2017-12-28 16:55:31 -0800677 LoadNativeLibrary(env_, "", class_loader_, &reason))
Ian Rogers68d8b422014-07-17 11:09:10 -0700678 << reason;
Ian Rogers1cefdbd2012-02-29 09:34:50 -0800679
680 jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 42);
681 EXPECT_EQ(43, result);
682}
683
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700684// TODO: Support @FastNative and @CriticalNative through stubs.
685JNI_TEST_NORMAL_ONLY(CompileAndRunStaticIntMethodThroughStub)
Andreas Gampe6e498692014-08-18 16:43:12 -0700686
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700687int gJava_MyClassNatives_fooI_calls[kJniKindCount] = {};
688jint Java_MyClassNatives_fooI(JNIEnv*, jobject, jint x) {
689 gJava_MyClassNatives_fooI_calls[gCurrentJni]++;
Ian Rogersb033c752011-07-20 12:22:35 -0700690 return x;
691}
692
Andreas Gampe6e498692014-08-18 16:43:12 -0700693void JniCompilerTest::CompileAndRunIntMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 SetUpForTest(false, "fooI", "(I)I",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700695 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooI));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700696
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700697 EXPECT_EQ(0, gJava_MyClassNatives_fooI_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700698 jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 42);
699 EXPECT_EQ(42, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700700 EXPECT_EQ(1, gJava_MyClassNatives_fooI_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700701 result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 0xCAFED00D);
702 EXPECT_EQ(static_cast<jint>(0xCAFED00D), result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700703 EXPECT_EQ(2, gJava_MyClassNatives_fooI_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700704
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700705 gJava_MyClassNatives_fooI_calls[gCurrentJni] = 0;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700706}
707
Andreas Gampe6e498692014-08-18 16:43:12 -0700708JNI_TEST(CompileAndRunIntMethod)
709
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700710int gJava_MyClassNatives_fooII_calls[kJniKindCount] = {};
711jint Java_MyClassNatives_fooII(JNIEnv*, jobject, jint x, jint y) {
712 gJava_MyClassNatives_fooII_calls[gCurrentJni]++;
Ian Rogersb033c752011-07-20 12:22:35 -0700713 return x - y; // non-commutative operator
714}
715
Andreas Gampe6e498692014-08-18 16:43:12 -0700716void JniCompilerTest::CompileAndRunIntIntMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 SetUpForTest(false, "fooII", "(II)I",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700718 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooII));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700719
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700720 EXPECT_EQ(0, gJava_MyClassNatives_fooII_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700721 jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 99, 10);
722 EXPECT_EQ(99 - 10, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700723 EXPECT_EQ(1, gJava_MyClassNatives_fooII_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700724 result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 0xCAFEBABE,
725 0xCAFED00D);
726 EXPECT_EQ(static_cast<jint>(0xCAFEBABE - 0xCAFED00D), result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700727 EXPECT_EQ(2, gJava_MyClassNatives_fooII_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700728
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700729 gJava_MyClassNatives_fooII_calls[gCurrentJni] = 0;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700730}
731
Andreas Gampe6e498692014-08-18 16:43:12 -0700732JNI_TEST(CompileAndRunIntIntMethod)
733
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700734int gJava_MyClassNatives_fooJJ_calls[kJniKindCount] = {};
735jlong Java_MyClassNatives_fooJJ(JNIEnv*, jobject, jlong x, jlong y) {
736 gJava_MyClassNatives_fooJJ_calls[gCurrentJni]++;
Ian Rogers9b269d22011-09-04 14:06:05 -0700737 return x - y; // non-commutative operator
738}
739
Andreas Gampe6e498692014-08-18 16:43:12 -0700740void JniCompilerTest::CompileAndRunLongLongMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 SetUpForTest(false, "fooJJ", "(JJ)J",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700742 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooJJ));
Ian Rogers9b269d22011-09-04 14:06:05 -0700743
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700744 EXPECT_EQ(0, gJava_MyClassNatives_fooJJ_calls[gCurrentJni]);
Ian Rogers0f678472014-03-10 16:18:37 -0700745 jlong a = INT64_C(0x1234567890ABCDEF);
746 jlong b = INT64_C(0xFEDCBA0987654321);
Ian Rogers9b269d22011-09-04 14:06:05 -0700747 jlong result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
748 EXPECT_EQ(a - b, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700749 EXPECT_EQ(1, gJava_MyClassNatives_fooJJ_calls[gCurrentJni]);
Ian Rogers9b269d22011-09-04 14:06:05 -0700750 result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, b, a);
751 EXPECT_EQ(b - a, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700752 EXPECT_EQ(2, gJava_MyClassNatives_fooJJ_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700753
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700754 gJava_MyClassNatives_fooJJ_calls[gCurrentJni] = 0;
Ian Rogers9b269d22011-09-04 14:06:05 -0700755}
756
Andreas Gampe6e498692014-08-18 16:43:12 -0700757JNI_TEST(CompileAndRunLongLongMethod)
758
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700759int gJava_MyClassNatives_fooDD_calls[kJniKindCount] = {};
760jdouble Java_MyClassNatives_fooDD(JNIEnv*, jobject, jdouble x, jdouble y) {
761 gJava_MyClassNatives_fooDD_calls[gCurrentJni]++;
Ian Rogersb033c752011-07-20 12:22:35 -0700762 return x - y; // non-commutative operator
763}
764
Andreas Gampe6e498692014-08-18 16:43:12 -0700765void JniCompilerTest::CompileAndRunDoubleDoubleMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 SetUpForTest(false, "fooDD", "(DD)D",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700767 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooDD));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700768
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700769 EXPECT_EQ(0, gJava_MyClassNatives_fooDD_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700770 jdouble result = env_->CallNonvirtualDoubleMethod(jobj_, jklass_, jmethod_,
771 99.0, 10.0);
Ian Rogers647b1a82014-10-10 11:02:11 -0700772 EXPECT_DOUBLE_EQ(99.0 - 10.0, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700773 EXPECT_EQ(1, gJava_MyClassNatives_fooDD_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700774 jdouble a = 3.14159265358979323846;
775 jdouble b = 0.69314718055994530942;
776 result = env_->CallNonvirtualDoubleMethod(jobj_, jklass_, jmethod_, a, b);
Ian Rogers647b1a82014-10-10 11:02:11 -0700777 EXPECT_DOUBLE_EQ(a - b, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700778 EXPECT_EQ(2, gJava_MyClassNatives_fooDD_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700779
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700780 gJava_MyClassNatives_fooDD_calls[gCurrentJni] = 0;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700781}
782
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700783int gJava_MyClassNatives_fooJJ_synchronized_calls[kJniKindCount] = {};
784jlong Java_MyClassNatives_fooJJ_synchronized(JNIEnv*, jobject, jlong x, jlong y) {
785 gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]++;
Elliott Hughes3e778f72012-05-21 15:29:52 -0700786 return x | y;
787}
788
Andreas Gampe6e498692014-08-18 16:43:12 -0700789void JniCompilerTest::CompileAndRun_fooJJ_synchronizedImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 SetUpForTest(false, "fooJJ_synchronized", "(JJ)J",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700791 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooJJ_synchronized));
Elliott Hughes3e778f72012-05-21 15:29:52 -0700792
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700793 EXPECT_EQ(0, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
Elliott Hughes3e778f72012-05-21 15:29:52 -0700794 jlong a = 0x1000000020000000ULL;
795 jlong b = 0x00ff000000aa0000ULL;
796 jlong result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b);
797 EXPECT_EQ(a | b, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700798 EXPECT_EQ(1, gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700799
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700800 gJava_MyClassNatives_fooJJ_synchronized_calls[gCurrentJni] = 0;
Elliott Hughes3e778f72012-05-21 15:29:52 -0700801}
802
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700803JNI_TEST_NORMAL_ONLY(CompileAndRun_fooJJ_synchronized)
Andreas Gampe6e498692014-08-18 16:43:12 -0700804
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700805int gJava_MyClassNatives_fooIOO_calls[kJniKindCount] = {};
806jobject Java_MyClassNatives_fooIOO(JNIEnv*, jobject thisObj, jint x, jobject y,
Ian Rogersb033c752011-07-20 12:22:35 -0700807 jobject z) {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700808 gJava_MyClassNatives_fooIOO_calls[gCurrentJni]++;
Ian Rogersb033c752011-07-20 12:22:35 -0700809 switch (x) {
810 case 1:
811 return y;
812 case 2:
813 return z;
814 default:
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700815 return thisObj;
Ian Rogersb033c752011-07-20 12:22:35 -0700816 }
817}
818
Andreas Gampe6e498692014-08-18 16:43:12 -0700819void JniCompilerTest::CompileAndRunIntObjectObjectMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 SetUpForTest(false, "fooIOO",
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700821 "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700822 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooIOO));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700823
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700824 EXPECT_EQ(0, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700825 jobject result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, nullptr, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700826 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700827 EXPECT_EQ(1, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700828
Andreas Gampecf4035a2014-05-28 22:43:01 -0700829 result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, nullptr, jklass_);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700830 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700831 EXPECT_EQ(2, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700832 result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 1, nullptr, jklass_);
833 EXPECT_TRUE(env_->IsSameObject(nullptr, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700834 EXPECT_EQ(3, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700835 result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 2, nullptr, jklass_);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700836 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700837 EXPECT_EQ(4, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700838
Andreas Gampecf4035a2014-05-28 22:43:01 -0700839 result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, jklass_, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700840 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700841 EXPECT_EQ(5, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700842 result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 1, jklass_, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700843 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700844 EXPECT_EQ(6, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -0700845 result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 2, jklass_, nullptr);
846 EXPECT_TRUE(env_->IsSameObject(nullptr, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700847 EXPECT_EQ(7, gJava_MyClassNatives_fooIOO_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700848
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700849 gJava_MyClassNatives_fooIOO_calls[gCurrentJni] = 0;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700850}
851
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700852JNI_TEST(CompileAndRunIntObjectObjectMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -0700853
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700854int gJava_MyClassNatives_fooSII_calls[kJniKindCount] = {};
855jint Java_MyClassNatives_fooSII(JNIEnv* env ATTRIBUTE_UNUSED,
856 jclass klass ATTRIBUTE_UNUSED,
857 jint x,
858 jint y) {
859 gJava_MyClassNatives_fooSII_calls[gCurrentJni]++;
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700860 return x + y;
861}
862
Andreas Gampe6e498692014-08-18 16:43:12 -0700863void JniCompilerTest::CompileAndRunStaticIntIntMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 SetUpForTest(true, "fooSII", "(II)I",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700865 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSII));
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700866
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700867 EXPECT_EQ(0, gJava_MyClassNatives_fooSII_calls[gCurrentJni]);
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700868 jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 20, 30);
869 EXPECT_EQ(50, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700870 EXPECT_EQ(1, gJava_MyClassNatives_fooSII_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700871
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700872 gJava_MyClassNatives_fooSII_calls[gCurrentJni] = 0;
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700873}
874
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700875JNI_TEST_CRITICAL(CompileAndRunStaticIntIntMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -0700876
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700877int gJava_MyClassNatives_fooSDD_calls[kJniKindCount] = {};
878jdouble Java_MyClassNatives_fooSDD(JNIEnv* env ATTRIBUTE_UNUSED,
879 jclass klass ATTRIBUTE_UNUSED,
880 jdouble x,
881 jdouble y) {
882 gJava_MyClassNatives_fooSDD_calls[gCurrentJni]++;
Ian Rogers7a99c112011-09-07 12:48:27 -0700883 return x - y; // non-commutative operator
884}
885
Andreas Gampe6e498692014-08-18 16:43:12 -0700886void JniCompilerTest::CompileAndRunStaticDoubleDoubleMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 SetUpForTest(true, "fooSDD", "(DD)D",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700888 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSDD));
Ian Rogers7a99c112011-09-07 12:48:27 -0700889
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700890 EXPECT_EQ(0, gJava_MyClassNatives_fooSDD_calls[gCurrentJni]);
Ian Rogers7a99c112011-09-07 12:48:27 -0700891 jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_, 99.0, 10.0);
Ian Rogers647b1a82014-10-10 11:02:11 -0700892 EXPECT_DOUBLE_EQ(99.0 - 10.0, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700893 EXPECT_EQ(1, gJava_MyClassNatives_fooSDD_calls[gCurrentJni]);
Ian Rogers7a99c112011-09-07 12:48:27 -0700894 jdouble a = 3.14159265358979323846;
895 jdouble b = 0.69314718055994530942;
896 result = env_->CallStaticDoubleMethod(jklass_, jmethod_, a, b);
Ian Rogers647b1a82014-10-10 11:02:11 -0700897 EXPECT_DOUBLE_EQ(a - b, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700898 EXPECT_DOUBLE_EQ(2, gJava_MyClassNatives_fooSDD_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -0700899
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700900 gJava_MyClassNatives_fooSDD_calls[gCurrentJni] = 0;
Ian Rogers7a99c112011-09-07 12:48:27 -0700901}
902
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700903JNI_TEST_CRITICAL(CompileAndRunStaticDoubleDoubleMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -0700904
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100905// The x86 generic JNI code had a bug where it assumed a floating
906// point return value would be in xmm0. We use log, to somehow ensure
907// the compiler will use the floating point stack.
908
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700909jdouble Java_MyClassNatives_logD(JNIEnv*, jclass, jdouble x) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100910 return log(x);
911}
912
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700913jdouble Java_MyClassNatives_logD_notNormal(JNIEnv*, jclass, jdouble x) {
914 EXPECT_DOUBLE_EQ(2.0, x);
915 return log(x);
916}
917
Andreas Gampe6e498692014-08-18 16:43:12 -0700918void JniCompilerTest::RunStaticLogDoubleMethodImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700919 void* jni_handler;
920 if (IsCurrentJniNormal()) {
921 // This test seems a bit special, don't use a JNI wrapper here.
922 jni_handler = NORMAL_JNI_ONLY_NOWRAP(Java_MyClassNatives_logD);
923 } else {
924 jni_handler = CURRENT_JNI_WRAPPER(Java_MyClassNatives_logD_notNormal);
925 }
926 SetUpForTest(true, "logD", "(D)D", jni_handler);
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100927
928 jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_, 2.0);
Ian Rogers647b1a82014-10-10 11:02:11 -0700929 EXPECT_DOUBLE_EQ(log(2.0), result);
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100930}
931
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700932JNI_TEST_CRITICAL(RunStaticLogDoubleMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -0700933
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700934jfloat Java_MyClassNatives_logF(JNIEnv*, jclass, jfloat x) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100935 return logf(x);
936}
937
Andreas Gampe6e498692014-08-18 16:43:12 -0700938void JniCompilerTest::RunStaticLogFloatMethodImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700939 void* jni_handler;
940 if (IsCurrentJniNormal()) {
941 // This test seems a bit special, don't use a JNI wrapper here.
942 jni_handler = NORMAL_JNI_ONLY_NOWRAP(Java_MyClassNatives_logF);
943 } else {
944 jni_handler = CURRENT_JNI_WRAPPER(Java_MyClassNatives_logF);
945 }
946
947 SetUpForTest(true, "logF", "(F)F", jni_handler);
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100948
949 jfloat result = env_->CallStaticFloatMethod(jklass_, jmethod_, 2.0);
Ian Rogers647b1a82014-10-10 11:02:11 -0700950 EXPECT_FLOAT_EQ(logf(2.0), result);
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100951}
952
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700953JNI_TEST_CRITICAL(RunStaticLogFloatMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -0700954
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700955jboolean Java_MyClassNatives_returnTrue(JNIEnv*, jclass) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100956 return JNI_TRUE;
957}
958
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700959jboolean Java_MyClassNatives_returnFalse(JNIEnv*, jclass) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100960 return JNI_FALSE;
961}
962
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700963jint Java_MyClassNatives_returnInt(JNIEnv*, jclass) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100964 return 42;
965}
966
Andreas Gampe6e498692014-08-18 16:43:12 -0700967void JniCompilerTest::RunStaticReturnTrueImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700968 SetUpForTest(true, "returnTrue", "()Z", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnTrue));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100969
970 jboolean result = env_->CallStaticBooleanMethod(jklass_, jmethod_);
971 EXPECT_TRUE(result);
972}
973
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700974JNI_TEST_CRITICAL(RunStaticReturnTrue)
Andreas Gampe6e498692014-08-18 16:43:12 -0700975
976void JniCompilerTest::RunStaticReturnFalseImpl() {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100977 SetUpForTest(true, "returnFalse", "()Z",
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700978 CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnFalse));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100979
980 jboolean result = env_->CallStaticBooleanMethod(jklass_, jmethod_);
981 EXPECT_FALSE(result);
982}
983
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700984JNI_TEST_CRITICAL(RunStaticReturnFalse)
Andreas Gampe6e498692014-08-18 16:43:12 -0700985
986void JniCompilerTest::RunGenericStaticReturnIntImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700987 SetUpForTest(true, "returnInt", "()I", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnInt));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100988
989 jint result = env_->CallStaticIntMethod(jklass_, jmethod_);
990 EXPECT_EQ(42, result);
991}
992
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700993JNI_TEST_CRITICAL(RunGenericStaticReturnInt)
Andreas Gampe6e498692014-08-18 16:43:12 -0700994
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700995int gJava_MyClassNatives_returnDouble_calls[kJniKindCount] = {};
996jdouble Java_MyClassNatives_returnDouble(JNIEnv*, jclass) {
997 gJava_MyClassNatives_returnDouble_calls[gCurrentJni]++;
998 return 4.0;
999}
1000
1001void JniCompilerTest::RunGenericStaticReturnDoubleImpl() {
1002 SetUpForTest(true, "returnDouble", "()D", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnDouble));
1003
1004 jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_);
1005 EXPECT_DOUBLE_EQ(4.0, result);
1006 EXPECT_EQ(1, gJava_MyClassNatives_returnDouble_calls[gCurrentJni]);
1007
1008 gJava_MyClassNatives_returnDouble_calls[gCurrentJni] = 0;
1009}
1010
1011JNI_TEST_CRITICAL(RunGenericStaticReturnDouble)
1012
1013jlong Java_MyClassNatives_returnLong(JNIEnv*, jclass) {
1014 return 0xFEEDDEADFEEDL;
1015}
1016
1017void JniCompilerTest::RunGenericStaticReturnLongImpl() {
1018 SetUpForTest(true, "returnLong", "()J", CURRENT_JNI_WRAPPER(Java_MyClassNatives_returnLong));
1019
1020 jlong result = env_->CallStaticLongMethod(jklass_, jmethod_);
1021 EXPECT_EQ(0xFEEDDEADFEEDL, result);
1022}
1023
1024JNI_TEST_CRITICAL(RunGenericStaticReturnLong)
1025
1026int gJava_MyClassNatives_fooSIOO_calls[kJniKindCount] = {};
1027jobject Java_MyClassNatives_fooSIOO(JNIEnv*, jclass klass, jint x, jobject y, jobject z) {
1028 gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]++;
Ian Rogersb033c752011-07-20 12:22:35 -07001029 switch (x) {
1030 case 1:
1031 return y;
1032 case 2:
1033 return z;
1034 default:
1035 return klass;
1036 }
1037}
1038
Andreas Gampe6e498692014-08-18 16:43:12 -07001039void JniCompilerTest::CompileAndRunStaticIntObjectObjectMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001040 SetUpForTest(true, "fooSIOO",
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001041 "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001042 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSIOO));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001043
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001044 EXPECT_EQ(0, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001045 jobject result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001046 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001047 EXPECT_EQ(1, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001048
Andreas Gampecf4035a2014-05-28 22:43:01 -07001049 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, jobj_);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001050 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001051 EXPECT_EQ(2, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001052 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, nullptr, jobj_);
1053 EXPECT_TRUE(env_->IsSameObject(nullptr, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001054 EXPECT_EQ(3, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001055 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, nullptr, jobj_);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001056 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001057 EXPECT_EQ(4, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001058
Andreas Gampecf4035a2014-05-28 22:43:01 -07001059 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, jobj_, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001060 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001061 EXPECT_EQ(5, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001062 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, jobj_, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001063 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001064 EXPECT_EQ(6, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001065 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1066 EXPECT_TRUE(env_->IsSameObject(nullptr, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001067 EXPECT_EQ(7, gJava_MyClassNatives_fooSIOO_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -07001068
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001069 gJava_MyClassNatives_fooSIOO_calls[gCurrentJni] = 0;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001070}
1071
Igor Murashkinaf1e2992016-10-12 17:44:50 -07001072JNI_TEST(CompileAndRunStaticIntObjectObjectMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -07001073
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001074int gJava_MyClassNatives_fooSSIOO_calls[kJniKindCount] = {};
1075jobject Java_MyClassNatives_fooSSIOO(JNIEnv*, jclass klass, jint x, jobject y, jobject z) {
1076 gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]++;
Ian Rogersdf20fe02011-07-20 20:34:16 -07001077 switch (x) {
1078 case 1:
1079 return y;
1080 case 2:
1081 return z;
1082 default:
1083 return klass;
1084 }
1085}
1086
Andreas Gampe6e498692014-08-18 16:43:12 -07001087void JniCompilerTest::CompileAndRunStaticSynchronizedIntObjectObjectMethodImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 SetUpForTest(true, "fooSSIOO",
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001089 "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001090 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooSSIOO));
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001091
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001092 EXPECT_EQ(0, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001093 jobject result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001094 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001095 EXPECT_EQ(1, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001096
Andreas Gampecf4035a2014-05-28 22:43:01 -07001097 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, nullptr, jobj_);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001098 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001099 EXPECT_EQ(2, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001100 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, nullptr, jobj_);
1101 EXPECT_TRUE(env_->IsSameObject(nullptr, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001102 EXPECT_EQ(3, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001103 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, nullptr, jobj_);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001104 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001105 EXPECT_EQ(4, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001106
Andreas Gampecf4035a2014-05-28 22:43:01 -07001107 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, jobj_, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001108 EXPECT_TRUE(env_->IsSameObject(jklass_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001109 EXPECT_EQ(5, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001110 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, jobj_, nullptr);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001111 EXPECT_TRUE(env_->IsSameObject(jobj_, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001112 EXPECT_EQ(6, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001113 result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, nullptr);
1114 EXPECT_TRUE(env_->IsSameObject(nullptr, result));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001115 EXPECT_EQ(7, gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -07001116
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001117 gJava_MyClassNatives_fooSSIOO_calls[gCurrentJni] = 0;
Ian Rogersdf20fe02011-07-20 20:34:16 -07001118}
1119
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001120// TODO: Maybe. @FastNative support for returning Objects?
1121JNI_TEST_NORMAL_ONLY(CompileAndRunStaticSynchronizedIntObjectObjectMethod)
Andreas Gampe6e498692014-08-18 16:43:12 -07001122
Elliott Hughesb264f082012-04-06 17:10:10 -07001123void Java_MyClassNatives_throwException(JNIEnv* env, jobject) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001124 jclass c = env->FindClass("java/lang/RuntimeException");
1125 env->ThrowNew(c, "hello");
1126}
Ian Rogers45a76cb2011-07-21 22:00:15 -07001127
Andreas Gampe6e498692014-08-18 16:43:12 -07001128void JniCompilerTest::ExceptionHandlingImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 {
1130 ASSERT_FALSE(runtime_->IsStarted());
1131 ScopedObjectAccess soa(Thread::Current());
1132 class_loader_ = LoadDex("MyClassNatives");
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001133
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 // all compilation needs to happen before Runtime::Start
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001135 CompileForTestWithCurrentJni(class_loader_, false, "foo", "()V");
1136 CompileForTestWithCurrentJni(class_loader_, false, "throwException", "()V");
1137 CompileForTestWithCurrentJni(class_loader_, false, "foo", "()V");
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 }
1139 // Start runtime to avoid re-initialization in SetupForTest.
1140 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07001141 bool started = runtime_->Start();
1142 CHECK(started);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001143
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001144 gJava_MyClassNatives_foo_calls[gCurrentJni] = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -07001145
Ian Rogers67375ac2011-09-14 00:55:44 -07001146 // Check a single call of a JNI method is ok
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001147 SetUpForTest(false, "foo", "()V", CURRENT_JNI_WRAPPER(Java_MyClassNatives_foo));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001148 env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001149 EXPECT_EQ(1, gJava_MyClassNatives_foo_calls[gCurrentJni]);
Ian Rogers67375ac2011-09-14 00:55:44 -07001150 EXPECT_FALSE(Thread::Current()->IsExceptionPending());
Elliott Hughesa2501992011-08-26 19:39:54 -07001151
Ian Rogers67375ac2011-09-14 00:55:44 -07001152 // Get class for exception we expect to be thrown
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedLocalRef<jclass> jlre(env_, env_->FindClass("java/lang/RuntimeException"));
1154 SetUpForTest(false, "throwException", "()V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001155 CURRENT_JNI_WRAPPER(Java_MyClassNatives_throwException));
Elliott Hughesb264f082012-04-06 17:10:10 -07001156 // Call Java_MyClassNatives_throwException (JNI method that throws exception)
Elliott Hughesa2501992011-08-26 19:39:54 -07001157 env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001158 EXPECT_EQ(1, gJava_MyClassNatives_foo_calls[gCurrentJni]);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
1160 ScopedLocalRef<jthrowable> exception(env_, env_->ExceptionOccurred());
1161 env_->ExceptionClear();
1162 EXPECT_TRUE(env_->IsInstanceOf(exception.get(), jlre.get()));
Elliott Hughesa2501992011-08-26 19:39:54 -07001163
Ian Rogers67375ac2011-09-14 00:55:44 -07001164 // Check a single call of a JNI method is ok
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 SetUpForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClassNatives_foo));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -07001166 env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001167 EXPECT_EQ(2, gJava_MyClassNatives_foo_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -07001168
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001169 gJava_MyClassNatives_foo_calls[gCurrentJni] = 0;
Ian Rogers45a76cb2011-07-21 22:00:15 -07001170}
1171
Andreas Gampe6e498692014-08-18 16:43:12 -07001172JNI_TEST(ExceptionHandling)
1173
Elliott Hughesb264f082012-04-06 17:10:10 -07001174jint Java_MyClassNatives_nativeUpCall(JNIEnv* env, jobject thisObj, jint i) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001175 if (i <= 0) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001176 // We want to check raw Object* / Array* below
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
Ian Rogersaaa20802011-09-11 21:47:37 -07001178
1179 // Build stack trace
Sebastien Hertzee1d79a2014-02-21 15:46:30 +01001180 jobject internal = Thread::Current()->CreateInternalStackTrace<false>(soa);
Ian Rogers53b8b092014-03-13 23:45:53 -07001181 jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(soa, internal);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001182 ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> trace_array =
1183 soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>>(ste_array);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001184 EXPECT_TRUE(trace_array != nullptr);
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001185 EXPECT_EQ(11, trace_array->GetLength());
1186
Ian Rogersaaa20802011-09-11 21:47:37 -07001187 // Check stack trace entries have expected values
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001188 for (int32_t j = 0; j < trace_array->GetLength(); ++j) {
1189 EXPECT_EQ(-2, trace_array->Get(j)->GetLineNumber());
1190 mirror::StackTraceElement* ste = trace_array->Get(j);
Ian Rogersaaa20802011-09-11 21:47:37 -07001191 EXPECT_STREQ("MyClassNatives.java", ste->GetFileName()->ToModifiedUtf8().c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -07001192 EXPECT_STREQ("MyClassNatives", ste->GetDeclaringClass()->ToModifiedUtf8().c_str());
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001193 EXPECT_EQ(("fooI" + CurrentJniStringSuffix()), ste->GetMethodName()->ToModifiedUtf8());
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001194 }
Ian Rogersaaa20802011-09-11 21:47:37 -07001195
1196 // end recursion
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001197 return 0;
1198 } else {
Elliott Hughesb264f082012-04-06 17:10:10 -07001199 jclass jklass = env->FindClass("MyClassNatives");
Andreas Gampecf4035a2014-05-28 22:43:01 -07001200 EXPECT_TRUE(jklass != nullptr);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001201 jmethodID jmethod = env->GetMethodID(jklass,
1202 ("fooI" + CurrentJniStringSuffix()).c_str(),
1203 "(I)I");
Andreas Gampecf4035a2014-05-28 22:43:01 -07001204 EXPECT_TRUE(jmethod != nullptr);
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001205
Ian Rogersaaa20802011-09-11 21:47:37 -07001206 // Recurse with i - 1
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001207 jint result = env->CallNonvirtualIntMethod(thisObj, jklass, jmethod, i - 1);
Ian Rogersaaa20802011-09-11 21:47:37 -07001208
1209 // Return sum of all depths
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001210 return i + result;
1211 }
1212}
1213
Andreas Gampe6e498692014-08-18 16:43:12 -07001214void JniCompilerTest::NativeStackTraceElementImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 SetUpForTest(false, "fooI", "(I)I",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001216 CURRENT_JNI_WRAPPER(Java_MyClassNatives_nativeUpCall));
1217
1218 // Usual # local references on stack check fails because nativeUpCall calls itself recursively,
1219 // each time the # of local references will therefore go up.
1220 ScopedDisableCheckNumStackReferences disable_num_stack_check;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001221 jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 10);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001222
Ian Rogersaaa20802011-09-11 21:47:37 -07001223 EXPECT_EQ(10+9+8+7+6+5+4+3+2+1, result);
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001224}
1225
Andreas Gampe6e498692014-08-18 16:43:12 -07001226JNI_TEST(NativeStackTraceElement)
1227
Elliott Hughesb264f082012-04-06 17:10:10 -07001228jobject Java_MyClassNatives_fooO(JNIEnv* env, jobject, jobject x) {
Shih-wei Liao558788e2011-09-01 02:39:11 -07001229 return env->NewGlobalRef(x);
1230}
1231
Andreas Gampe6e498692014-08-18 16:43:12 -07001232void JniCompilerTest::ReturnGlobalRefImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 SetUpForTest(false, "fooO", "(Ljava/lang/Object;)Ljava/lang/Object;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001234 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fooO));
Shih-wei Liao558788e2011-09-01 02:39:11 -07001235 jobject result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, jobj_);
1236 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(result));
1237 EXPECT_TRUE(env_->IsSameObject(result, jobj_));
1238}
1239
Igor Murashkinaf1e2992016-10-12 17:44:50 -07001240JNI_TEST(ReturnGlobalRef)
Andreas Gampe6e498692014-08-18 16:43:12 -07001241
Ian Rogersdc51b792011-09-22 20:41:37 -07001242jint local_ref_test(JNIEnv* env, jobject thisObj, jint x) {
1243 // Add 10 local references
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001245 for (int i = 0; i < 10; i++) {
Mathieu Chartier0795f232016-09-27 18:43:30 -07001246 soa.AddLocalReference<jobject>(soa.Decode<mirror::Object>(thisObj));
Ian Rogersdc51b792011-09-22 20:41:37 -07001247 }
1248 return x+1;
1249}
1250
Andreas Gampe6e498692014-08-18 16:43:12 -07001251void JniCompilerTest::LocalReferenceTableClearingTestImpl() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001252 SetUpForTest(false, "fooI", "(I)I", CURRENT_JNI_WRAPPER(local_ref_test));
Ian Rogersdc51b792011-09-22 20:41:37 -07001253 // 1000 invocations of a method that adds 10 local references
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001254 for (int i = 0; i < 1000; i++) {
Ian Rogersdc51b792011-09-22 20:41:37 -07001255 jint result = env_->CallIntMethod(jobj_, jmethod_, i);
1256 EXPECT_TRUE(result == i + 1);
1257 }
1258}
1259
Andreas Gampe6e498692014-08-18 16:43:12 -07001260JNI_TEST(LocalReferenceTableClearingTest)
1261
Ian Rogersb9231c82011-09-05 22:13:19 -07001262void my_arraycopy(JNIEnv* env, jclass klass, jobject src, jint src_pos, jobject dst, jint dst_pos, jint length) {
1263 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jklass_, klass));
1264 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jklass_, dst));
Ian Rogers82f3e092011-09-05 22:54:45 -07001265 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, src));
Ian Rogersb9231c82011-09-05 22:13:19 -07001266 EXPECT_EQ(1234, src_pos);
1267 EXPECT_EQ(5678, dst_pos);
1268 EXPECT_EQ(9876, length);
1269}
1270
Andreas Gampe6e498692014-08-18 16:43:12 -07001271void JniCompilerTest::JavaLangSystemArrayCopyImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 SetUpForTest(true, "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001273 CURRENT_JNI_WRAPPER(my_arraycopy));
Ian Rogers82f3e092011-09-05 22:54:45 -07001274 env_->CallStaticVoidMethod(jklass_, jmethod_, jobj_, 1234, jklass_, 5678, 9876);
Ian Rogersb9231c82011-09-05 22:13:19 -07001275}
1276
Andreas Gampe6e498692014-08-18 16:43:12 -07001277JNI_TEST(JavaLangSystemArrayCopy)
1278
Ian Rogers67375ac2011-09-14 00:55:44 -07001279jboolean my_casi(JNIEnv* env, jobject unsafe, jobject obj, jlong offset, jint expected, jint newval) {
1280 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, unsafe));
1281 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj));
Ian Rogers0f678472014-03-10 16:18:37 -07001282 EXPECT_EQ(INT64_C(0x12345678ABCDEF88), offset);
Ian Rogers67375ac2011-09-14 00:55:44 -07001283 EXPECT_EQ(static_cast<jint>(0xCAFEF00D), expected);
1284 EXPECT_EQ(static_cast<jint>(0xEBADF00D), newval);
1285 return JNI_TRUE;
1286}
1287
Andreas Gampe6e498692014-08-18 16:43:12 -07001288void JniCompilerTest::CompareAndSwapIntImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 SetUpForTest(false, "compareAndSwapInt", "(Ljava/lang/Object;JII)Z",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001290 CURRENT_JNI_WRAPPER(my_casi));
Ian Rogers0f678472014-03-10 16:18:37 -07001291 jboolean result = env_->CallBooleanMethod(jobj_, jmethod_, jobj_, INT64_C(0x12345678ABCDEF88),
1292 0xCAFEF00D, 0xEBADF00D);
Ian Rogers67375ac2011-09-14 00:55:44 -07001293 EXPECT_EQ(result, JNI_TRUE);
1294}
1295
Andreas Gampe6e498692014-08-18 16:43:12 -07001296JNI_TEST(CompareAndSwapInt)
1297
Ian Rogersc7792842012-03-03 15:36:20 -08001298jint my_gettext(JNIEnv* env, jclass klass, jlong val1, jobject obj1, jlong val2, jobject obj2) {
1299 EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass));
1300 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj1));
1301 EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj2));
1302 EXPECT_EQ(0x12345678ABCDEF88ll, val1);
1303 EXPECT_EQ(0x7FEDCBA987654321ll, val2);
1304 return 42;
1305}
1306
Andreas Gampe6e498692014-08-18 16:43:12 -07001307void JniCompilerTest::GetTextImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 SetUpForTest(true, "getText", "(JLjava/lang/Object;JLjava/lang/Object;)I",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001309 CURRENT_JNI_WRAPPER(my_gettext));
Ian Rogersc7792842012-03-03 15:36:20 -08001310 jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 0x12345678ABCDEF88ll, jobj_,
Ian Rogers0f678472014-03-10 16:18:37 -07001311 INT64_C(0x7FEDCBA987654321), jobj_);
Ian Rogersc7792842012-03-03 15:36:20 -08001312 EXPECT_EQ(result, 42);
1313}
1314
Andreas Gampe6e498692014-08-18 16:43:12 -07001315JNI_TEST(GetText)
1316
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001317int gJava_MyClassNatives_GetSinkProperties_calls[kJniKindCount] = {};
1318jarray Java_MyClassNatives_GetSinkProperties(JNIEnv*, jobject thisObj, jstring s) {
Vladimir Marko4e24b9d2014-07-24 17:01:58 +01001319 EXPECT_EQ(s, nullptr);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001320 gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni]++;
1321
1322 Thread* self = Thread::Current();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +01001323 ScopedObjectAccess soa(self);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001324 EXPECT_TRUE(self->HoldsLock(soa.Decode<mirror::Object>(thisObj).Ptr()));
Vladimir Marko4e24b9d2014-07-24 17:01:58 +01001325 return nullptr;
1326}
1327
Andreas Gampe6e498692014-08-18 16:43:12 -07001328void JniCompilerTest::GetSinkPropertiesNativeImpl() {
Vladimir Marko4e24b9d2014-07-24 17:01:58 +01001329 SetUpForTest(false, "getSinkPropertiesNative", "(Ljava/lang/String;)[Ljava/lang/Object;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001330 CURRENT_JNI_WRAPPER(Java_MyClassNatives_GetSinkProperties));
Vladimir Marko4e24b9d2014-07-24 17:01:58 +01001331
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001332 EXPECT_EQ(0, gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni]);
Vladimir Marko4e24b9d2014-07-24 17:01:58 +01001333 jarray result = down_cast<jarray>(
1334 env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, nullptr));
1335 EXPECT_EQ(nullptr, result);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001336 EXPECT_EQ(1, gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni]);
Andreas Gampe6e498692014-08-18 16:43:12 -07001337
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001338 gJava_MyClassNatives_GetSinkProperties_calls[gCurrentJni] = 0;
Brian Carlstromfc7120c2012-08-27 13:43:25 -07001339}
1340
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001341// @FastNative doesn't support 'synchronized' keyword and
1342// never will -- locking functions aren't fast.
1343JNI_TEST_NORMAL_ONLY(GetSinkPropertiesNative)
Andreas Gampe6e498692014-08-18 16:43:12 -07001344
Elliott Hughesb264f082012-04-06 17:10:10 -07001345// This should return jclass, but we're imitating a bug pattern.
1346jobject Java_MyClassNatives_instanceMethodThatShouldReturnClass(JNIEnv* env, jobject) {
1347 return env->NewStringUTF("not a class!");
1348}
1349
1350// This should return jclass, but we're imitating a bug pattern.
1351jobject Java_MyClassNatives_staticMethodThatShouldReturnClass(JNIEnv* env, jclass) {
1352 return env->NewStringUTF("not a class!");
1353}
1354
Andreas Gampe6e498692014-08-18 16:43:12 -07001355void JniCompilerTest::UpcallReturnTypeChecking_InstanceImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001356 SetUpForTest(false, "instanceMethodThatShouldReturnClass", "()Ljava/lang/Class;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001357 CURRENT_JNI_WRAPPER(Java_MyClassNatives_instanceMethodThatShouldReturnClass));
Elliott Hughesb264f082012-04-06 17:10:10 -07001358
1359 CheckJniAbortCatcher check_jni_abort_catcher;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001360 // This native method is bad, and tries to return a jstring as a jclass.
1361 env_->CallObjectMethod(jobj_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001362 check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1363 "of java.lang.String from java.lang.Class " +
1364 "MyClassNatives.instanceMethodThatShouldReturnClass" +
1365 CurrentJniStringSuffix() + "()");
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001366
1367 // Here, we just call the method incorrectly; we should catch that too.
Ian Rogers68d8b422014-07-17 11:09:10 -07001368 env_->CallObjectMethod(jobj_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001369 check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1370 "of java.lang.String from java.lang.Class " +
1371 "MyClassNatives.instanceMethodThatShouldReturnClass" +
1372 CurrentJniStringSuffix() + "()");
Ian Rogers68d8b422014-07-17 11:09:10 -07001373 env_->CallStaticObjectMethod(jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001374 check_jni_abort_catcher.Check(std::string() + "calling non-static method " +
1375 "java.lang.Class " +
1376 "MyClassNatives.instanceMethodThatShouldReturnClass" +
1377 CurrentJniStringSuffix() + "() with CallStaticObjectMethodV");
Elliott Hughesb264f082012-04-06 17:10:10 -07001378}
1379
Igor Murashkinaf1e2992016-10-12 17:44:50 -07001380JNI_TEST(UpcallReturnTypeChecking_Instance)
Andreas Gampe6e498692014-08-18 16:43:12 -07001381
1382void JniCompilerTest::UpcallReturnTypeChecking_StaticImpl() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001383 SetUpForTest(true, "staticMethodThatShouldReturnClass", "()Ljava/lang/Class;",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001384 CURRENT_JNI_WRAPPER(Java_MyClassNatives_staticMethodThatShouldReturnClass));
Elliott Hughesb264f082012-04-06 17:10:10 -07001385
1386 CheckJniAbortCatcher check_jni_abort_catcher;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001387 // This native method is bad, and tries to return a jstring as a jclass.
1388 env_->CallStaticObjectMethod(jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001389 check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1390 "of java.lang.String from java.lang.Class " +
1391 "MyClassNatives.staticMethodThatShouldReturnClass" +
1392 CurrentJniStringSuffix() + "()");
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001393
1394 // Here, we just call the method incorrectly; we should catch that too.
Ian Rogers68d8b422014-07-17 11:09:10 -07001395 env_->CallStaticObjectMethod(jklass_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001396 check_jni_abort_catcher.Check(std::string() + "attempt to return an instance " +
1397 "of java.lang.String from java.lang.Class " +
1398 "MyClassNatives.staticMethodThatShouldReturnClass" +
1399 CurrentJniStringSuffix() + "()");
Ian Rogers68d8b422014-07-17 11:09:10 -07001400 env_->CallObjectMethod(jobj_, jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001401 check_jni_abort_catcher.Check(std::string() + "calling static method " +
1402 "java.lang.Class " +
1403 "MyClassNatives.staticMethodThatShouldReturnClass" +
1404 CurrentJniStringSuffix() + "() with CallObjectMethodV");
Elliott Hughesb264f082012-04-06 17:10:10 -07001405}
1406
Igor Murashkinaf1e2992016-10-12 17:44:50 -07001407JNI_TEST(UpcallReturnTypeChecking_Static)
Andreas Gampe6e498692014-08-18 16:43:12 -07001408
Elliott Hughesb264f082012-04-06 17:10:10 -07001409// This should take jclass, but we're imitating a bug pattern.
1410void Java_MyClassNatives_instanceMethodThatShouldTakeClass(JNIEnv*, jobject, jclass) {
1411}
1412
1413// This should take jclass, but we're imitating a bug pattern.
1414void Java_MyClassNatives_staticMethodThatShouldTakeClass(JNIEnv*, jclass, jclass) {
1415}
1416
Andreas Gampe6e498692014-08-18 16:43:12 -07001417void JniCompilerTest::UpcallArgumentTypeChecking_InstanceImpl() {
Andreas Gampe369810a2015-01-14 19:53:31 -08001418 // This will lead to error messages in the log.
1419 ScopedLogSeverity sls(LogSeverity::FATAL);
1420
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 SetUpForTest(false, "instanceMethodThatShouldTakeClass", "(ILjava/lang/Class;)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001422 CURRENT_JNI_WRAPPER(Java_MyClassNatives_instanceMethodThatShouldTakeClass));
Elliott Hughesb264f082012-04-06 17:10:10 -07001423
1424 CheckJniAbortCatcher check_jni_abort_catcher;
1425 // We deliberately pass a bad second argument here.
1426 env_->CallVoidMethod(jobj_, jmethod_, 123, env_->NewStringUTF("not a class!"));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001427 check_jni_abort_catcher.Check(std::string() + "bad arguments passed to void " +
1428 "MyClassNatives.instanceMethodThatShouldTakeClass" +
1429 CurrentJniStringSuffix() + "(int, java.lang.Class)");
Elliott Hughesb264f082012-04-06 17:10:10 -07001430}
1431
Andreas Gampe6e498692014-08-18 16:43:12 -07001432JNI_TEST(UpcallArgumentTypeChecking_Instance)
1433
1434void JniCompilerTest::UpcallArgumentTypeChecking_StaticImpl() {
Andreas Gampe369810a2015-01-14 19:53:31 -08001435 // This will lead to error messages in the log.
1436 ScopedLogSeverity sls(LogSeverity::FATAL);
1437
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 SetUpForTest(true, "staticMethodThatShouldTakeClass", "(ILjava/lang/Class;)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001439 CURRENT_JNI_WRAPPER(Java_MyClassNatives_staticMethodThatShouldTakeClass));
Elliott Hughesb264f082012-04-06 17:10:10 -07001440
1441 CheckJniAbortCatcher check_jni_abort_catcher;
1442 // We deliberately pass a bad second argument here.
1443 env_->CallStaticVoidMethod(jklass_, jmethod_, 123, env_->NewStringUTF("not a class!"));
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001444 check_jni_abort_catcher.Check(std::string() + "bad arguments passed to void " +
1445 "MyClassNatives.staticMethodThatShouldTakeClass" +
1446 CurrentJniStringSuffix() + "(int, java.lang.Class)");
Elliott Hughesb264f082012-04-06 17:10:10 -07001447}
1448
Andreas Gampe6e498692014-08-18 16:43:12 -07001449JNI_TEST(UpcallArgumentTypeChecking_Static)
1450
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001451jfloat Java_MyClassNatives_checkFloats(JNIEnv*, jobject, jfloat f1, jfloat f2) {
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001452 return f1 - f2; // non-commutative operator
1453}
1454
Andreas Gampe6e498692014-08-18 16:43:12 -07001455void JniCompilerTest::CompileAndRunFloatFloatMethodImpl() {
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001456 SetUpForTest(false, "checkFloats", "(FF)F",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001457 CURRENT_JNI_WRAPPER(Java_MyClassNatives_checkFloats));
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001458
1459 jfloat result = env_->CallNonvirtualFloatMethod(jobj_, jklass_, jmethod_,
1460 99.0F, 10.0F);
Ian Rogers647b1a82014-10-10 11:02:11 -07001461 EXPECT_FLOAT_EQ(99.0F - 10.0F, result);
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001462 jfloat a = 3.14159F;
1463 jfloat b = 0.69314F;
1464 result = env_->CallNonvirtualFloatMethod(jobj_, jklass_, jmethod_, a, b);
Ian Rogers647b1a82014-10-10 11:02:11 -07001465 EXPECT_FLOAT_EQ(a - b, result);
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001466}
1467
Andreas Gampe6e498692014-08-18 16:43:12 -07001468JNI_TEST(CompileAndRunFloatFloatMethod)
1469
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001470void Java_MyClassNatives_checkParameterAlign(JNIEnv* env ATTRIBUTE_UNUSED,
1471 jobject thisObj ATTRIBUTE_UNUSED,
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001472 jint i1,
1473 jlong l1) {
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001474 EXPECT_EQ(i1, 1234);
Ian Rogers0f678472014-03-10 16:18:37 -07001475 EXPECT_EQ(l1, INT64_C(0x12345678ABCDEF0));
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001476}
1477
Andreas Gampe6e498692014-08-18 16:43:12 -07001478void JniCompilerTest::CheckParameterAlignImpl() {
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001479 SetUpForTest(false, "checkParameterAlign", "(IJ)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001480 CURRENT_JNI_WRAPPER(Java_MyClassNatives_checkParameterAlign));
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001481
Ian Rogers0f678472014-03-10 16:18:37 -07001482 env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_, 1234, INT64_C(0x12345678ABCDEF0));
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001483}
1484
Andreas Gampe6e498692014-08-18 16:43:12 -07001485JNI_TEST(CheckParameterAlign)
1486
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001487void Java_MyClassNatives_maxParamNumber(JNIEnv* env, jobject,
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001488 jobject o0, jobject o1, jobject o2, jobject o3, jobject o4, jobject o5, jobject o6, jobject o7,
1489 jobject o8, jobject o9, jobject o10, jobject o11, jobject o12, jobject o13, jobject o14, jobject o15,
1490 jobject o16, jobject o17, jobject o18, jobject o19, jobject o20, jobject o21, jobject o22, jobject o23,
1491 jobject o24, jobject o25, jobject o26, jobject o27, jobject o28, jobject o29, jobject o30, jobject o31,
1492 jobject o32, jobject o33, jobject o34, jobject o35, jobject o36, jobject o37, jobject o38, jobject o39,
1493 jobject o40, jobject o41, jobject o42, jobject o43, jobject o44, jobject o45, jobject o46, jobject o47,
1494 jobject o48, jobject o49, jobject o50, jobject o51, jobject o52, jobject o53, jobject o54, jobject o55,
1495 jobject o56, jobject o57, jobject o58, jobject o59, jobject o60, jobject o61, jobject o62, jobject o63,
1496 jobject o64, jobject o65, jobject o66, jobject o67, jobject o68, jobject o69, jobject o70, jobject o71,
1497 jobject o72, jobject o73, jobject o74, jobject o75, jobject o76, jobject o77, jobject o78, jobject o79,
1498 jobject o80, jobject o81, jobject o82, jobject o83, jobject o84, jobject o85, jobject o86, jobject o87,
1499 jobject o88, jobject o89, jobject o90, jobject o91, jobject o92, jobject o93, jobject o94, jobject o95,
1500 jobject o96, jobject o97, jobject o98, jobject o99, jobject o100, jobject o101, jobject o102, jobject o103,
1501 jobject o104, jobject o105, jobject o106, jobject o107, jobject o108, jobject o109, jobject o110, jobject o111,
1502 jobject o112, jobject o113, jobject o114, jobject o115, jobject o116, jobject o117, jobject o118, jobject o119,
1503 jobject o120, jobject o121, jobject o122, jobject o123, jobject o124, jobject o125, jobject o126, jobject o127,
1504 jobject o128, jobject o129, jobject o130, jobject o131, jobject o132, jobject o133, jobject o134, jobject o135,
1505 jobject o136, jobject o137, jobject o138, jobject o139, jobject o140, jobject o141, jobject o142, jobject o143,
1506 jobject o144, jobject o145, jobject o146, jobject o147, jobject o148, jobject o149, jobject o150, jobject o151,
1507 jobject o152, jobject o153, jobject o154, jobject o155, jobject o156, jobject o157, jobject o158, jobject o159,
1508 jobject o160, jobject o161, jobject o162, jobject o163, jobject o164, jobject o165, jobject o166, jobject o167,
1509 jobject o168, jobject o169, jobject o170, jobject o171, jobject o172, jobject o173, jobject o174, jobject o175,
1510 jobject o176, jobject o177, jobject o178, jobject o179, jobject o180, jobject o181, jobject o182, jobject o183,
1511 jobject o184, jobject o185, jobject o186, jobject o187, jobject o188, jobject o189, jobject o190, jobject o191,
1512 jobject o192, jobject o193, jobject o194, jobject o195, jobject o196, jobject o197, jobject o198, jobject o199,
1513 jobject o200, jobject o201, jobject o202, jobject o203, jobject o204, jobject o205, jobject o206, jobject o207,
1514 jobject o208, jobject o209, jobject o210, jobject o211, jobject o212, jobject o213, jobject o214, jobject o215,
1515 jobject o216, jobject o217, jobject o218, jobject o219, jobject o220, jobject o221, jobject o222, jobject o223,
1516 jobject o224, jobject o225, jobject o226, jobject o227, jobject o228, jobject o229, jobject o230, jobject o231,
1517 jobject o232, jobject o233, jobject o234, jobject o235, jobject o236, jobject o237, jobject o238, jobject o239,
1518 jobject o240, jobject o241, jobject o242, jobject o243, jobject o244, jobject o245, jobject o246, jobject o247,
1519 jobject o248, jobject o249, jobject o250, jobject o251, jobject o252, jobject o253) {
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001520 // two tests possible
1521 if (o0 == nullptr) {
1522 // 1) everything is null
1523 EXPECT_TRUE(o0 == nullptr && o1 == nullptr && o2 == nullptr && o3 == nullptr && o4 == nullptr
1524 && o5 == nullptr && o6 == nullptr && o7 == nullptr && o8 == nullptr && o9 == nullptr
1525 && o10 == nullptr && o11 == nullptr && o12 == nullptr && o13 == nullptr && o14 == nullptr
1526 && o15 == nullptr && o16 == nullptr && o17 == nullptr && o18 == nullptr && o19 == nullptr
1527 && o20 == nullptr && o21 == nullptr && o22 == nullptr && o23 == nullptr && o24 == nullptr
1528 && o25 == nullptr && o26 == nullptr && o27 == nullptr && o28 == nullptr && o29 == nullptr
1529 && o30 == nullptr && o31 == nullptr && o32 == nullptr && o33 == nullptr && o34 == nullptr
1530 && o35 == nullptr && o36 == nullptr && o37 == nullptr && o38 == nullptr && o39 == nullptr
1531 && o40 == nullptr && o41 == nullptr && o42 == nullptr && o43 == nullptr && o44 == nullptr
1532 && o45 == nullptr && o46 == nullptr && o47 == nullptr && o48 == nullptr && o49 == nullptr
1533 && o50 == nullptr && o51 == nullptr && o52 == nullptr && o53 == nullptr && o54 == nullptr
1534 && o55 == nullptr && o56 == nullptr && o57 == nullptr && o58 == nullptr && o59 == nullptr
1535 && o60 == nullptr && o61 == nullptr && o62 == nullptr && o63 == nullptr && o64 == nullptr
1536 && o65 == nullptr && o66 == nullptr && o67 == nullptr && o68 == nullptr && o69 == nullptr
1537 && o70 == nullptr && o71 == nullptr && o72 == nullptr && o73 == nullptr && o74 == nullptr
1538 && o75 == nullptr && o76 == nullptr && o77 == nullptr && o78 == nullptr && o79 == nullptr
1539 && o80 == nullptr && o81 == nullptr && o82 == nullptr && o83 == nullptr && o84 == nullptr
1540 && o85 == nullptr && o86 == nullptr && o87 == nullptr && o88 == nullptr && o89 == nullptr
1541 && o90 == nullptr && o91 == nullptr && o92 == nullptr && o93 == nullptr && o94 == nullptr
1542 && o95 == nullptr && o96 == nullptr && o97 == nullptr && o98 == nullptr && o99 == nullptr
1543 && o100 == nullptr && o101 == nullptr && o102 == nullptr && o103 == nullptr && o104 == nullptr
1544 && o105 == nullptr && o106 == nullptr && o107 == nullptr && o108 == nullptr && o109 == nullptr
1545 && o110 == nullptr && o111 == nullptr && o112 == nullptr && o113 == nullptr && o114 == nullptr
1546 && o115 == nullptr && o116 == nullptr && o117 == nullptr && o118 == nullptr && o119 == nullptr
1547 && o120 == nullptr && o121 == nullptr && o122 == nullptr && o123 == nullptr && o124 == nullptr
1548 && o125 == nullptr && o126 == nullptr && o127 == nullptr && o128 == nullptr && o129 == nullptr
1549 && o130 == nullptr && o131 == nullptr && o132 == nullptr && o133 == nullptr && o134 == nullptr
1550 && o135 == nullptr && o136 == nullptr && o137 == nullptr && o138 == nullptr && o139 == nullptr
1551 && o140 == nullptr && o141 == nullptr && o142 == nullptr && o143 == nullptr && o144 == nullptr
1552 && o145 == nullptr && o146 == nullptr && o147 == nullptr && o148 == nullptr && o149 == nullptr
1553 && o150 == nullptr && o151 == nullptr && o152 == nullptr && o153 == nullptr && o154 == nullptr
1554 && o155 == nullptr && o156 == nullptr && o157 == nullptr && o158 == nullptr && o159 == nullptr
1555 && o160 == nullptr && o161 == nullptr && o162 == nullptr && o163 == nullptr && o164 == nullptr
1556 && o165 == nullptr && o166 == nullptr && o167 == nullptr && o168 == nullptr && o169 == nullptr
1557 && o170 == nullptr && o171 == nullptr && o172 == nullptr && o173 == nullptr && o174 == nullptr
1558 && o175 == nullptr && o176 == nullptr && o177 == nullptr && o178 == nullptr && o179 == nullptr
1559 && o180 == nullptr && o181 == nullptr && o182 == nullptr && o183 == nullptr && o184 == nullptr
1560 && o185 == nullptr && o186 == nullptr && o187 == nullptr && o188 == nullptr && o189 == nullptr
1561 && o190 == nullptr && o191 == nullptr && o192 == nullptr && o193 == nullptr && o194 == nullptr
1562 && o195 == nullptr && o196 == nullptr && o197 == nullptr && o198 == nullptr && o199 == nullptr
1563 && o200 == nullptr && o201 == nullptr && o202 == nullptr && o203 == nullptr && o204 == nullptr
1564 && o205 == nullptr && o206 == nullptr && o207 == nullptr && o208 == nullptr && o209 == nullptr
1565 && o210 == nullptr && o211 == nullptr && o212 == nullptr && o213 == nullptr && o214 == nullptr
1566 && o215 == nullptr && o216 == nullptr && o217 == nullptr && o218 == nullptr && o219 == nullptr
1567 && o220 == nullptr && o221 == nullptr && o222 == nullptr && o223 == nullptr && o224 == nullptr
1568 && o225 == nullptr && o226 == nullptr && o227 == nullptr && o228 == nullptr && o229 == nullptr
1569 && o230 == nullptr && o231 == nullptr && o232 == nullptr && o233 == nullptr && o234 == nullptr
1570 && o235 == nullptr && o236 == nullptr && o237 == nullptr && o238 == nullptr && o239 == nullptr
1571 && o240 == nullptr && o241 == nullptr && o242 == nullptr && o243 == nullptr && o244 == nullptr
1572 && o245 == nullptr && o246 == nullptr && o247 == nullptr && o248 == nullptr && o249 == nullptr
1573 && o250 == nullptr && o251 == nullptr && o252 == nullptr && o253 == nullptr);
1574 } else {
1575 EXPECT_EQ(0, env->GetArrayLength(reinterpret_cast<jarray>(o0)));
1576 EXPECT_EQ(1, env->GetArrayLength(reinterpret_cast<jarray>(o1)));
1577 EXPECT_EQ(2, env->GetArrayLength(reinterpret_cast<jarray>(o2)));
1578 EXPECT_EQ(3, env->GetArrayLength(reinterpret_cast<jarray>(o3)));
1579 EXPECT_EQ(4, env->GetArrayLength(reinterpret_cast<jarray>(o4)));
1580 EXPECT_EQ(5, env->GetArrayLength(reinterpret_cast<jarray>(o5)));
1581 EXPECT_EQ(6, env->GetArrayLength(reinterpret_cast<jarray>(o6)));
1582 EXPECT_EQ(7, env->GetArrayLength(reinterpret_cast<jarray>(o7)));
1583 EXPECT_EQ(8, env->GetArrayLength(reinterpret_cast<jarray>(o8)));
1584 EXPECT_EQ(9, env->GetArrayLength(reinterpret_cast<jarray>(o9)));
1585 EXPECT_EQ(10, env->GetArrayLength(reinterpret_cast<jarray>(o10)));
1586 EXPECT_EQ(11, env->GetArrayLength(reinterpret_cast<jarray>(o11)));
1587 EXPECT_EQ(12, env->GetArrayLength(reinterpret_cast<jarray>(o12)));
1588 EXPECT_EQ(13, env->GetArrayLength(reinterpret_cast<jarray>(o13)));
1589 EXPECT_EQ(14, env->GetArrayLength(reinterpret_cast<jarray>(o14)));
1590 EXPECT_EQ(15, env->GetArrayLength(reinterpret_cast<jarray>(o15)));
1591 EXPECT_EQ(16, env->GetArrayLength(reinterpret_cast<jarray>(o16)));
1592 EXPECT_EQ(17, env->GetArrayLength(reinterpret_cast<jarray>(o17)));
1593 EXPECT_EQ(18, env->GetArrayLength(reinterpret_cast<jarray>(o18)));
1594 EXPECT_EQ(19, env->GetArrayLength(reinterpret_cast<jarray>(o19)));
1595 EXPECT_EQ(20, env->GetArrayLength(reinterpret_cast<jarray>(o20)));
1596 EXPECT_EQ(21, env->GetArrayLength(reinterpret_cast<jarray>(o21)));
1597 EXPECT_EQ(22, env->GetArrayLength(reinterpret_cast<jarray>(o22)));
1598 EXPECT_EQ(23, env->GetArrayLength(reinterpret_cast<jarray>(o23)));
1599 EXPECT_EQ(24, env->GetArrayLength(reinterpret_cast<jarray>(o24)));
1600 EXPECT_EQ(25, env->GetArrayLength(reinterpret_cast<jarray>(o25)));
1601 EXPECT_EQ(26, env->GetArrayLength(reinterpret_cast<jarray>(o26)));
1602 EXPECT_EQ(27, env->GetArrayLength(reinterpret_cast<jarray>(o27)));
1603 EXPECT_EQ(28, env->GetArrayLength(reinterpret_cast<jarray>(o28)));
1604 EXPECT_EQ(29, env->GetArrayLength(reinterpret_cast<jarray>(o29)));
1605 EXPECT_EQ(30, env->GetArrayLength(reinterpret_cast<jarray>(o30)));
1606 EXPECT_EQ(31, env->GetArrayLength(reinterpret_cast<jarray>(o31)));
1607 EXPECT_EQ(32, env->GetArrayLength(reinterpret_cast<jarray>(o32)));
1608 EXPECT_EQ(33, env->GetArrayLength(reinterpret_cast<jarray>(o33)));
1609 EXPECT_EQ(34, env->GetArrayLength(reinterpret_cast<jarray>(o34)));
1610 EXPECT_EQ(35, env->GetArrayLength(reinterpret_cast<jarray>(o35)));
1611 EXPECT_EQ(36, env->GetArrayLength(reinterpret_cast<jarray>(o36)));
1612 EXPECT_EQ(37, env->GetArrayLength(reinterpret_cast<jarray>(o37)));
1613 EXPECT_EQ(38, env->GetArrayLength(reinterpret_cast<jarray>(o38)));
1614 EXPECT_EQ(39, env->GetArrayLength(reinterpret_cast<jarray>(o39)));
1615 EXPECT_EQ(40, env->GetArrayLength(reinterpret_cast<jarray>(o40)));
1616 EXPECT_EQ(41, env->GetArrayLength(reinterpret_cast<jarray>(o41)));
1617 EXPECT_EQ(42, env->GetArrayLength(reinterpret_cast<jarray>(o42)));
1618 EXPECT_EQ(43, env->GetArrayLength(reinterpret_cast<jarray>(o43)));
1619 EXPECT_EQ(44, env->GetArrayLength(reinterpret_cast<jarray>(o44)));
1620 EXPECT_EQ(45, env->GetArrayLength(reinterpret_cast<jarray>(o45)));
1621 EXPECT_EQ(46, env->GetArrayLength(reinterpret_cast<jarray>(o46)));
1622 EXPECT_EQ(47, env->GetArrayLength(reinterpret_cast<jarray>(o47)));
1623 EXPECT_EQ(48, env->GetArrayLength(reinterpret_cast<jarray>(o48)));
1624 EXPECT_EQ(49, env->GetArrayLength(reinterpret_cast<jarray>(o49)));
1625 EXPECT_EQ(50, env->GetArrayLength(reinterpret_cast<jarray>(o50)));
1626 EXPECT_EQ(51, env->GetArrayLength(reinterpret_cast<jarray>(o51)));
1627 EXPECT_EQ(52, env->GetArrayLength(reinterpret_cast<jarray>(o52)));
1628 EXPECT_EQ(53, env->GetArrayLength(reinterpret_cast<jarray>(o53)));
1629 EXPECT_EQ(54, env->GetArrayLength(reinterpret_cast<jarray>(o54)));
1630 EXPECT_EQ(55, env->GetArrayLength(reinterpret_cast<jarray>(o55)));
1631 EXPECT_EQ(56, env->GetArrayLength(reinterpret_cast<jarray>(o56)));
1632 EXPECT_EQ(57, env->GetArrayLength(reinterpret_cast<jarray>(o57)));
1633 EXPECT_EQ(58, env->GetArrayLength(reinterpret_cast<jarray>(o58)));
1634 EXPECT_EQ(59, env->GetArrayLength(reinterpret_cast<jarray>(o59)));
1635 EXPECT_EQ(60, env->GetArrayLength(reinterpret_cast<jarray>(o60)));
1636 EXPECT_EQ(61, env->GetArrayLength(reinterpret_cast<jarray>(o61)));
1637 EXPECT_EQ(62, env->GetArrayLength(reinterpret_cast<jarray>(o62)));
1638 EXPECT_EQ(63, env->GetArrayLength(reinterpret_cast<jarray>(o63)));
1639 EXPECT_EQ(64, env->GetArrayLength(reinterpret_cast<jarray>(o64)));
1640 EXPECT_EQ(65, env->GetArrayLength(reinterpret_cast<jarray>(o65)));
1641 EXPECT_EQ(66, env->GetArrayLength(reinterpret_cast<jarray>(o66)));
1642 EXPECT_EQ(67, env->GetArrayLength(reinterpret_cast<jarray>(o67)));
1643 EXPECT_EQ(68, env->GetArrayLength(reinterpret_cast<jarray>(o68)));
1644 EXPECT_EQ(69, env->GetArrayLength(reinterpret_cast<jarray>(o69)));
1645 EXPECT_EQ(70, env->GetArrayLength(reinterpret_cast<jarray>(o70)));
1646 EXPECT_EQ(71, env->GetArrayLength(reinterpret_cast<jarray>(o71)));
1647 EXPECT_EQ(72, env->GetArrayLength(reinterpret_cast<jarray>(o72)));
1648 EXPECT_EQ(73, env->GetArrayLength(reinterpret_cast<jarray>(o73)));
1649 EXPECT_EQ(74, env->GetArrayLength(reinterpret_cast<jarray>(o74)));
1650 EXPECT_EQ(75, env->GetArrayLength(reinterpret_cast<jarray>(o75)));
1651 EXPECT_EQ(76, env->GetArrayLength(reinterpret_cast<jarray>(o76)));
1652 EXPECT_EQ(77, env->GetArrayLength(reinterpret_cast<jarray>(o77)));
1653 EXPECT_EQ(78, env->GetArrayLength(reinterpret_cast<jarray>(o78)));
1654 EXPECT_EQ(79, env->GetArrayLength(reinterpret_cast<jarray>(o79)));
1655 EXPECT_EQ(80, env->GetArrayLength(reinterpret_cast<jarray>(o80)));
1656 EXPECT_EQ(81, env->GetArrayLength(reinterpret_cast<jarray>(o81)));
1657 EXPECT_EQ(82, env->GetArrayLength(reinterpret_cast<jarray>(o82)));
1658 EXPECT_EQ(83, env->GetArrayLength(reinterpret_cast<jarray>(o83)));
1659 EXPECT_EQ(84, env->GetArrayLength(reinterpret_cast<jarray>(o84)));
1660 EXPECT_EQ(85, env->GetArrayLength(reinterpret_cast<jarray>(o85)));
1661 EXPECT_EQ(86, env->GetArrayLength(reinterpret_cast<jarray>(o86)));
1662 EXPECT_EQ(87, env->GetArrayLength(reinterpret_cast<jarray>(o87)));
1663 EXPECT_EQ(88, env->GetArrayLength(reinterpret_cast<jarray>(o88)));
1664 EXPECT_EQ(89, env->GetArrayLength(reinterpret_cast<jarray>(o89)));
1665 EXPECT_EQ(90, env->GetArrayLength(reinterpret_cast<jarray>(o90)));
1666 EXPECT_EQ(91, env->GetArrayLength(reinterpret_cast<jarray>(o91)));
1667 EXPECT_EQ(92, env->GetArrayLength(reinterpret_cast<jarray>(o92)));
1668 EXPECT_EQ(93, env->GetArrayLength(reinterpret_cast<jarray>(o93)));
1669 EXPECT_EQ(94, env->GetArrayLength(reinterpret_cast<jarray>(o94)));
1670 EXPECT_EQ(95, env->GetArrayLength(reinterpret_cast<jarray>(o95)));
1671 EXPECT_EQ(96, env->GetArrayLength(reinterpret_cast<jarray>(o96)));
1672 EXPECT_EQ(97, env->GetArrayLength(reinterpret_cast<jarray>(o97)));
1673 EXPECT_EQ(98, env->GetArrayLength(reinterpret_cast<jarray>(o98)));
1674 EXPECT_EQ(99, env->GetArrayLength(reinterpret_cast<jarray>(o99)));
1675 EXPECT_EQ(100, env->GetArrayLength(reinterpret_cast<jarray>(o100)));
1676 EXPECT_EQ(101, env->GetArrayLength(reinterpret_cast<jarray>(o101)));
1677 EXPECT_EQ(102, env->GetArrayLength(reinterpret_cast<jarray>(o102)));
1678 EXPECT_EQ(103, env->GetArrayLength(reinterpret_cast<jarray>(o103)));
1679 EXPECT_EQ(104, env->GetArrayLength(reinterpret_cast<jarray>(o104)));
1680 EXPECT_EQ(105, env->GetArrayLength(reinterpret_cast<jarray>(o105)));
1681 EXPECT_EQ(106, env->GetArrayLength(reinterpret_cast<jarray>(o106)));
1682 EXPECT_EQ(107, env->GetArrayLength(reinterpret_cast<jarray>(o107)));
1683 EXPECT_EQ(108, env->GetArrayLength(reinterpret_cast<jarray>(o108)));
1684 EXPECT_EQ(109, env->GetArrayLength(reinterpret_cast<jarray>(o109)));
1685 EXPECT_EQ(110, env->GetArrayLength(reinterpret_cast<jarray>(o110)));
1686 EXPECT_EQ(111, env->GetArrayLength(reinterpret_cast<jarray>(o111)));
1687 EXPECT_EQ(112, env->GetArrayLength(reinterpret_cast<jarray>(o112)));
1688 EXPECT_EQ(113, env->GetArrayLength(reinterpret_cast<jarray>(o113)));
1689 EXPECT_EQ(114, env->GetArrayLength(reinterpret_cast<jarray>(o114)));
1690 EXPECT_EQ(115, env->GetArrayLength(reinterpret_cast<jarray>(o115)));
1691 EXPECT_EQ(116, env->GetArrayLength(reinterpret_cast<jarray>(o116)));
1692 EXPECT_EQ(117, env->GetArrayLength(reinterpret_cast<jarray>(o117)));
1693 EXPECT_EQ(118, env->GetArrayLength(reinterpret_cast<jarray>(o118)));
1694 EXPECT_EQ(119, env->GetArrayLength(reinterpret_cast<jarray>(o119)));
1695 EXPECT_EQ(120, env->GetArrayLength(reinterpret_cast<jarray>(o120)));
1696 EXPECT_EQ(121, env->GetArrayLength(reinterpret_cast<jarray>(o121)));
1697 EXPECT_EQ(122, env->GetArrayLength(reinterpret_cast<jarray>(o122)));
1698 EXPECT_EQ(123, env->GetArrayLength(reinterpret_cast<jarray>(o123)));
1699 EXPECT_EQ(124, env->GetArrayLength(reinterpret_cast<jarray>(o124)));
1700 EXPECT_EQ(125, env->GetArrayLength(reinterpret_cast<jarray>(o125)));
1701 EXPECT_EQ(126, env->GetArrayLength(reinterpret_cast<jarray>(o126)));
1702 EXPECT_EQ(127, env->GetArrayLength(reinterpret_cast<jarray>(o127)));
1703 EXPECT_EQ(128, env->GetArrayLength(reinterpret_cast<jarray>(o128)));
1704 EXPECT_EQ(129, env->GetArrayLength(reinterpret_cast<jarray>(o129)));
1705 EXPECT_EQ(130, env->GetArrayLength(reinterpret_cast<jarray>(o130)));
1706 EXPECT_EQ(131, env->GetArrayLength(reinterpret_cast<jarray>(o131)));
1707 EXPECT_EQ(132, env->GetArrayLength(reinterpret_cast<jarray>(o132)));
1708 EXPECT_EQ(133, env->GetArrayLength(reinterpret_cast<jarray>(o133)));
1709 EXPECT_EQ(134, env->GetArrayLength(reinterpret_cast<jarray>(o134)));
1710 EXPECT_EQ(135, env->GetArrayLength(reinterpret_cast<jarray>(o135)));
1711 EXPECT_EQ(136, env->GetArrayLength(reinterpret_cast<jarray>(o136)));
1712 EXPECT_EQ(137, env->GetArrayLength(reinterpret_cast<jarray>(o137)));
1713 EXPECT_EQ(138, env->GetArrayLength(reinterpret_cast<jarray>(o138)));
1714 EXPECT_EQ(139, env->GetArrayLength(reinterpret_cast<jarray>(o139)));
1715 EXPECT_EQ(140, env->GetArrayLength(reinterpret_cast<jarray>(o140)));
1716 EXPECT_EQ(141, env->GetArrayLength(reinterpret_cast<jarray>(o141)));
1717 EXPECT_EQ(142, env->GetArrayLength(reinterpret_cast<jarray>(o142)));
1718 EXPECT_EQ(143, env->GetArrayLength(reinterpret_cast<jarray>(o143)));
1719 EXPECT_EQ(144, env->GetArrayLength(reinterpret_cast<jarray>(o144)));
1720 EXPECT_EQ(145, env->GetArrayLength(reinterpret_cast<jarray>(o145)));
1721 EXPECT_EQ(146, env->GetArrayLength(reinterpret_cast<jarray>(o146)));
1722 EXPECT_EQ(147, env->GetArrayLength(reinterpret_cast<jarray>(o147)));
1723 EXPECT_EQ(148, env->GetArrayLength(reinterpret_cast<jarray>(o148)));
1724 EXPECT_EQ(149, env->GetArrayLength(reinterpret_cast<jarray>(o149)));
1725 EXPECT_EQ(150, env->GetArrayLength(reinterpret_cast<jarray>(o150)));
1726 EXPECT_EQ(151, env->GetArrayLength(reinterpret_cast<jarray>(o151)));
1727 EXPECT_EQ(152, env->GetArrayLength(reinterpret_cast<jarray>(o152)));
1728 EXPECT_EQ(153, env->GetArrayLength(reinterpret_cast<jarray>(o153)));
1729 EXPECT_EQ(154, env->GetArrayLength(reinterpret_cast<jarray>(o154)));
1730 EXPECT_EQ(155, env->GetArrayLength(reinterpret_cast<jarray>(o155)));
1731 EXPECT_EQ(156, env->GetArrayLength(reinterpret_cast<jarray>(o156)));
1732 EXPECT_EQ(157, env->GetArrayLength(reinterpret_cast<jarray>(o157)));
1733 EXPECT_EQ(158, env->GetArrayLength(reinterpret_cast<jarray>(o158)));
1734 EXPECT_EQ(159, env->GetArrayLength(reinterpret_cast<jarray>(o159)));
1735 EXPECT_EQ(160, env->GetArrayLength(reinterpret_cast<jarray>(o160)));
1736 EXPECT_EQ(161, env->GetArrayLength(reinterpret_cast<jarray>(o161)));
1737 EXPECT_EQ(162, env->GetArrayLength(reinterpret_cast<jarray>(o162)));
1738 EXPECT_EQ(163, env->GetArrayLength(reinterpret_cast<jarray>(o163)));
1739 EXPECT_EQ(164, env->GetArrayLength(reinterpret_cast<jarray>(o164)));
1740 EXPECT_EQ(165, env->GetArrayLength(reinterpret_cast<jarray>(o165)));
1741 EXPECT_EQ(166, env->GetArrayLength(reinterpret_cast<jarray>(o166)));
1742 EXPECT_EQ(167, env->GetArrayLength(reinterpret_cast<jarray>(o167)));
1743 EXPECT_EQ(168, env->GetArrayLength(reinterpret_cast<jarray>(o168)));
1744 EXPECT_EQ(169, env->GetArrayLength(reinterpret_cast<jarray>(o169)));
1745 EXPECT_EQ(170, env->GetArrayLength(reinterpret_cast<jarray>(o170)));
1746 EXPECT_EQ(171, env->GetArrayLength(reinterpret_cast<jarray>(o171)));
1747 EXPECT_EQ(172, env->GetArrayLength(reinterpret_cast<jarray>(o172)));
1748 EXPECT_EQ(173, env->GetArrayLength(reinterpret_cast<jarray>(o173)));
1749 EXPECT_EQ(174, env->GetArrayLength(reinterpret_cast<jarray>(o174)));
1750 EXPECT_EQ(175, env->GetArrayLength(reinterpret_cast<jarray>(o175)));
1751 EXPECT_EQ(176, env->GetArrayLength(reinterpret_cast<jarray>(o176)));
1752 EXPECT_EQ(177, env->GetArrayLength(reinterpret_cast<jarray>(o177)));
1753 EXPECT_EQ(178, env->GetArrayLength(reinterpret_cast<jarray>(o178)));
1754 EXPECT_EQ(179, env->GetArrayLength(reinterpret_cast<jarray>(o179)));
1755 EXPECT_EQ(180, env->GetArrayLength(reinterpret_cast<jarray>(o180)));
1756 EXPECT_EQ(181, env->GetArrayLength(reinterpret_cast<jarray>(o181)));
1757 EXPECT_EQ(182, env->GetArrayLength(reinterpret_cast<jarray>(o182)));
1758 EXPECT_EQ(183, env->GetArrayLength(reinterpret_cast<jarray>(o183)));
1759 EXPECT_EQ(184, env->GetArrayLength(reinterpret_cast<jarray>(o184)));
1760 EXPECT_EQ(185, env->GetArrayLength(reinterpret_cast<jarray>(o185)));
1761 EXPECT_EQ(186, env->GetArrayLength(reinterpret_cast<jarray>(o186)));
1762 EXPECT_EQ(187, env->GetArrayLength(reinterpret_cast<jarray>(o187)));
1763 EXPECT_EQ(188, env->GetArrayLength(reinterpret_cast<jarray>(o188)));
1764 EXPECT_EQ(189, env->GetArrayLength(reinterpret_cast<jarray>(o189)));
1765 EXPECT_EQ(190, env->GetArrayLength(reinterpret_cast<jarray>(o190)));
1766 EXPECT_EQ(191, env->GetArrayLength(reinterpret_cast<jarray>(o191)));
1767 EXPECT_EQ(192, env->GetArrayLength(reinterpret_cast<jarray>(o192)));
1768 EXPECT_EQ(193, env->GetArrayLength(reinterpret_cast<jarray>(o193)));
1769 EXPECT_EQ(194, env->GetArrayLength(reinterpret_cast<jarray>(o194)));
1770 EXPECT_EQ(195, env->GetArrayLength(reinterpret_cast<jarray>(o195)));
1771 EXPECT_EQ(196, env->GetArrayLength(reinterpret_cast<jarray>(o196)));
1772 EXPECT_EQ(197, env->GetArrayLength(reinterpret_cast<jarray>(o197)));
1773 EXPECT_EQ(198, env->GetArrayLength(reinterpret_cast<jarray>(o198)));
1774 EXPECT_EQ(199, env->GetArrayLength(reinterpret_cast<jarray>(o199)));
1775 EXPECT_EQ(200, env->GetArrayLength(reinterpret_cast<jarray>(o200)));
1776 EXPECT_EQ(201, env->GetArrayLength(reinterpret_cast<jarray>(o201)));
1777 EXPECT_EQ(202, env->GetArrayLength(reinterpret_cast<jarray>(o202)));
1778 EXPECT_EQ(203, env->GetArrayLength(reinterpret_cast<jarray>(o203)));
1779 EXPECT_EQ(204, env->GetArrayLength(reinterpret_cast<jarray>(o204)));
1780 EXPECT_EQ(205, env->GetArrayLength(reinterpret_cast<jarray>(o205)));
1781 EXPECT_EQ(206, env->GetArrayLength(reinterpret_cast<jarray>(o206)));
1782 EXPECT_EQ(207, env->GetArrayLength(reinterpret_cast<jarray>(o207)));
1783 EXPECT_EQ(208, env->GetArrayLength(reinterpret_cast<jarray>(o208)));
1784 EXPECT_EQ(209, env->GetArrayLength(reinterpret_cast<jarray>(o209)));
1785 EXPECT_EQ(210, env->GetArrayLength(reinterpret_cast<jarray>(o210)));
1786 EXPECT_EQ(211, env->GetArrayLength(reinterpret_cast<jarray>(o211)));
1787 EXPECT_EQ(212, env->GetArrayLength(reinterpret_cast<jarray>(o212)));
1788 EXPECT_EQ(213, env->GetArrayLength(reinterpret_cast<jarray>(o213)));
1789 EXPECT_EQ(214, env->GetArrayLength(reinterpret_cast<jarray>(o214)));
1790 EXPECT_EQ(215, env->GetArrayLength(reinterpret_cast<jarray>(o215)));
1791 EXPECT_EQ(216, env->GetArrayLength(reinterpret_cast<jarray>(o216)));
1792 EXPECT_EQ(217, env->GetArrayLength(reinterpret_cast<jarray>(o217)));
1793 EXPECT_EQ(218, env->GetArrayLength(reinterpret_cast<jarray>(o218)));
1794 EXPECT_EQ(219, env->GetArrayLength(reinterpret_cast<jarray>(o219)));
1795 EXPECT_EQ(220, env->GetArrayLength(reinterpret_cast<jarray>(o220)));
1796 EXPECT_EQ(221, env->GetArrayLength(reinterpret_cast<jarray>(o221)));
1797 EXPECT_EQ(222, env->GetArrayLength(reinterpret_cast<jarray>(o222)));
1798 EXPECT_EQ(223, env->GetArrayLength(reinterpret_cast<jarray>(o223)));
1799 EXPECT_EQ(224, env->GetArrayLength(reinterpret_cast<jarray>(o224)));
1800 EXPECT_EQ(225, env->GetArrayLength(reinterpret_cast<jarray>(o225)));
1801 EXPECT_EQ(226, env->GetArrayLength(reinterpret_cast<jarray>(o226)));
1802 EXPECT_EQ(227, env->GetArrayLength(reinterpret_cast<jarray>(o227)));
1803 EXPECT_EQ(228, env->GetArrayLength(reinterpret_cast<jarray>(o228)));
1804 EXPECT_EQ(229, env->GetArrayLength(reinterpret_cast<jarray>(o229)));
1805 EXPECT_EQ(230, env->GetArrayLength(reinterpret_cast<jarray>(o230)));
1806 EXPECT_EQ(231, env->GetArrayLength(reinterpret_cast<jarray>(o231)));
1807 EXPECT_EQ(232, env->GetArrayLength(reinterpret_cast<jarray>(o232)));
1808 EXPECT_EQ(233, env->GetArrayLength(reinterpret_cast<jarray>(o233)));
1809 EXPECT_EQ(234, env->GetArrayLength(reinterpret_cast<jarray>(o234)));
1810 EXPECT_EQ(235, env->GetArrayLength(reinterpret_cast<jarray>(o235)));
1811 EXPECT_EQ(236, env->GetArrayLength(reinterpret_cast<jarray>(o236)));
1812 EXPECT_EQ(237, env->GetArrayLength(reinterpret_cast<jarray>(o237)));
1813 EXPECT_EQ(238, env->GetArrayLength(reinterpret_cast<jarray>(o238)));
1814 EXPECT_EQ(239, env->GetArrayLength(reinterpret_cast<jarray>(o239)));
1815 EXPECT_EQ(240, env->GetArrayLength(reinterpret_cast<jarray>(o240)));
1816 EXPECT_EQ(241, env->GetArrayLength(reinterpret_cast<jarray>(o241)));
1817 EXPECT_EQ(242, env->GetArrayLength(reinterpret_cast<jarray>(o242)));
1818 EXPECT_EQ(243, env->GetArrayLength(reinterpret_cast<jarray>(o243)));
1819 EXPECT_EQ(244, env->GetArrayLength(reinterpret_cast<jarray>(o244)));
1820 EXPECT_EQ(245, env->GetArrayLength(reinterpret_cast<jarray>(o245)));
1821 EXPECT_EQ(246, env->GetArrayLength(reinterpret_cast<jarray>(o246)));
1822 EXPECT_EQ(247, env->GetArrayLength(reinterpret_cast<jarray>(o247)));
1823 EXPECT_EQ(248, env->GetArrayLength(reinterpret_cast<jarray>(o248)));
1824 EXPECT_EQ(249, env->GetArrayLength(reinterpret_cast<jarray>(o249)));
1825 EXPECT_EQ(250, env->GetArrayLength(reinterpret_cast<jarray>(o250)));
1826 EXPECT_EQ(251, env->GetArrayLength(reinterpret_cast<jarray>(o251)));
1827 EXPECT_EQ(252, env->GetArrayLength(reinterpret_cast<jarray>(o252)));
1828 EXPECT_EQ(253, env->GetArrayLength(reinterpret_cast<jarray>(o253)));
1829 }
1830}
1831
1832const char* longSig =
1833 "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
1834 "Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;"
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;)V";
1884
Andreas Gampe6e498692014-08-18 16:43:12 -07001885void JniCompilerTest::MaxParamNumberImpl() {
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001886 SetUpForTest(false, "maxParamNumber", longSig,
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001887 CURRENT_JNI_WRAPPER(Java_MyClassNatives_maxParamNumber));
Andreas Gampe7a0e5042014-03-07 13:03:19 -08001888
1889 jvalue args[254];
1890
1891 // First test: test with all arguments null.
1892 for (int i = 0; i < 254; ++i) {
1893 args[i].l = nullptr;
1894 }
1895
1896 env_->CallNonvirtualVoidMethodA(jobj_, jklass_, jmethod_, args);
1897
1898 // Second test: test with int[] objects with increasing lengths
1899 for (int i = 0; i < 254; ++i) {
1900 jintArray tmp = env_->NewIntArray(i);
1901 args[i].l = tmp;
1902 EXPECT_NE(args[i].l, nullptr);
1903 }
1904
1905 env_->CallNonvirtualVoidMethodA(jobj_, jklass_, jmethod_, args);
1906}
1907
Andreas Gampe6e498692014-08-18 16:43:12 -07001908JNI_TEST(MaxParamNumber)
1909
1910void JniCompilerTest::WithoutImplementationImpl() {
Andreas Gampe369810a2015-01-14 19:53:31 -08001911 // This will lead to error messages in the log.
1912 ScopedLogSeverity sls(LogSeverity::FATAL);
1913
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001914 SetUpForTest(false, "withoutImplementation", "()V", NORMAL_JNI_ONLY_NULLPTR);
Andreas Gampead615172014-04-04 16:20:13 -07001915
1916 env_->CallVoidMethod(jobj_, jmethod_);
1917
1918 EXPECT_TRUE(Thread::Current()->IsExceptionPending());
1919 EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
1920}
1921
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001922// TODO: Don't test @FastNative here since it goes through a stub lookup (unsupported) which would
1923// normally fail with an exception, but fails with an assert.
1924JNI_TEST_NORMAL_ONLY(WithoutImplementation)
Andreas Gampe6e498692014-08-18 16:43:12 -07001925
Andreas Gampe48ee3562015-04-10 19:57:29 -07001926void JniCompilerTest::WithoutImplementationRefReturnImpl() {
1927 // This will lead to error messages in the log.
1928 ScopedLogSeverity sls(LogSeverity::FATAL);
1929
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001930 SetUpForTest(false,
1931 "withoutImplementationRefReturn",
1932 "()Ljava/lang/Object;",
1933 NORMAL_JNI_ONLY_NULLPTR);
Andreas Gampe48ee3562015-04-10 19:57:29 -07001934
1935 env_->CallObjectMethod(jobj_, jmethod_);
1936
1937 EXPECT_TRUE(Thread::Current()->IsExceptionPending());
1938 EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE);
1939}
1940
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001941// TODO: Should work for @FastNative too.
1942JNI_TEST_NORMAL_ONLY(WithoutImplementationRefReturn)
Andreas Gampe48ee3562015-04-10 19:57:29 -07001943
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001944void Java_MyClassNatives_stackArgsIntsFirst(JNIEnv*, jclass, jint i1, jint i2, jint i3,
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001945 jint i4, jint i5, jint i6, jint i7, jint i8, jint i9,
1946 jint i10, jfloat f1, jfloat f2, jfloat f3, jfloat f4,
1947 jfloat f5, jfloat f6, jfloat f7, jfloat f8, jfloat f9,
1948 jfloat f10) {
1949 EXPECT_EQ(i1, 1);
1950 EXPECT_EQ(i2, 2);
1951 EXPECT_EQ(i3, 3);
1952 EXPECT_EQ(i4, 4);
1953 EXPECT_EQ(i5, 5);
1954 EXPECT_EQ(i6, 6);
1955 EXPECT_EQ(i7, 7);
1956 EXPECT_EQ(i8, 8);
1957 EXPECT_EQ(i9, 9);
1958 EXPECT_EQ(i10, 10);
1959
Roland Levillainda4d79b2015-03-24 14:36:11 +00001960 jint i11 = bit_cast<jint, jfloat>(f1);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001961 EXPECT_EQ(i11, 11);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001962 jint i12 = bit_cast<jint, jfloat>(f2);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001963 EXPECT_EQ(i12, 12);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001964 jint i13 = bit_cast<jint, jfloat>(f3);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001965 EXPECT_EQ(i13, 13);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001966 jint i14 = bit_cast<jint, jfloat>(f4);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001967 EXPECT_EQ(i14, 14);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001968 jint i15 = bit_cast<jint, jfloat>(f5);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001969 EXPECT_EQ(i15, 15);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001970 jint i16 = bit_cast<jint, jfloat>(f6);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001971 EXPECT_EQ(i16, 16);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001972 jint i17 = bit_cast<jint, jfloat>(f7);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001973 EXPECT_EQ(i17, 17);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001974 jint i18 = bit_cast<jint, jfloat>(f8);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001975 EXPECT_EQ(i18, 18);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001976 jint i19 = bit_cast<jint, jfloat>(f9);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001977 EXPECT_EQ(i19, 19);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001978 jint i20 = bit_cast<jint, jfloat>(f10);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001979 EXPECT_EQ(i20, 20);
1980}
1981
Andreas Gampe6e498692014-08-18 16:43:12 -07001982void JniCompilerTest::StackArgsIntsFirstImpl() {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001983 SetUpForTest(true, "stackArgsIntsFirst", "(IIIIIIIIIIFFFFFFFFFF)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07001984 CURRENT_JNI_WRAPPER(Java_MyClassNatives_stackArgsIntsFirst));
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07001985
1986 jint i1 = 1;
1987 jint i2 = 2;
1988 jint i3 = 3;
1989 jint i4 = 4;
1990 jint i5 = 5;
1991 jint i6 = 6;
1992 jint i7 = 7;
1993 jint i8 = 8;
1994 jint i9 = 9;
1995 jint i10 = 10;
1996
Roland Levillainda4d79b2015-03-24 14:36:11 +00001997 jfloat f1 = bit_cast<jfloat, jint>(11);
1998 jfloat f2 = bit_cast<jfloat, jint>(12);
1999 jfloat f3 = bit_cast<jfloat, jint>(13);
2000 jfloat f4 = bit_cast<jfloat, jint>(14);
2001 jfloat f5 = bit_cast<jfloat, jint>(15);
2002 jfloat f6 = bit_cast<jfloat, jint>(16);
2003 jfloat f7 = bit_cast<jfloat, jint>(17);
2004 jfloat f8 = bit_cast<jfloat, jint>(18);
2005 jfloat f9 = bit_cast<jfloat, jint>(19);
2006 jfloat f10 = bit_cast<jfloat, jint>(20);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002007
2008 env_->CallStaticVoidMethod(jklass_, jmethod_, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, f1, f2,
2009 f3, f4, f5, f6, f7, f8, f9, f10);
2010}
2011
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002012JNI_TEST_CRITICAL(StackArgsIntsFirst)
Andreas Gampe6e498692014-08-18 16:43:12 -07002013
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002014void Java_MyClassNatives_stackArgsFloatsFirst(JNIEnv*, jclass, jfloat f1, jfloat f2,
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002015 jfloat f3, jfloat f4, jfloat f5, jfloat f6, jfloat f7,
2016 jfloat f8, jfloat f9, jfloat f10, jint i1, jint i2,
2017 jint i3, jint i4, jint i5, jint i6, jint i7, jint i8,
2018 jint i9, jint i10) {
2019 EXPECT_EQ(i1, 1);
2020 EXPECT_EQ(i2, 2);
2021 EXPECT_EQ(i3, 3);
2022 EXPECT_EQ(i4, 4);
2023 EXPECT_EQ(i5, 5);
2024 EXPECT_EQ(i6, 6);
2025 EXPECT_EQ(i7, 7);
2026 EXPECT_EQ(i8, 8);
2027 EXPECT_EQ(i9, 9);
2028 EXPECT_EQ(i10, 10);
2029
Roland Levillainda4d79b2015-03-24 14:36:11 +00002030 jint i11 = bit_cast<jint, jfloat>(f1);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002031 EXPECT_EQ(i11, 11);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002032 jint i12 = bit_cast<jint, jfloat>(f2);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002033 EXPECT_EQ(i12, 12);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002034 jint i13 = bit_cast<jint, jfloat>(f3);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002035 EXPECT_EQ(i13, 13);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002036 jint i14 = bit_cast<jint, jfloat>(f4);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002037 EXPECT_EQ(i14, 14);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002038 jint i15 = bit_cast<jint, jfloat>(f5);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002039 EXPECT_EQ(i15, 15);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002040 jint i16 = bit_cast<jint, jfloat>(f6);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002041 EXPECT_EQ(i16, 16);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002042 jint i17 = bit_cast<jint, jfloat>(f7);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002043 EXPECT_EQ(i17, 17);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002044 jint i18 = bit_cast<jint, jfloat>(f8);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002045 EXPECT_EQ(i18, 18);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002046 jint i19 = bit_cast<jint, jfloat>(f9);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002047 EXPECT_EQ(i19, 19);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002048 jint i20 = bit_cast<jint, jfloat>(f10);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002049 EXPECT_EQ(i20, 20);
2050}
2051
Andreas Gampe6e498692014-08-18 16:43:12 -07002052void JniCompilerTest::StackArgsFloatsFirstImpl() {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002053 SetUpForTest(true, "stackArgsFloatsFirst", "(FFFFFFFFFFIIIIIIIIII)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002054 CURRENT_JNI_WRAPPER(Java_MyClassNatives_stackArgsFloatsFirst));
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002055
2056 jint i1 = 1;
2057 jint i2 = 2;
2058 jint i3 = 3;
2059 jint i4 = 4;
2060 jint i5 = 5;
2061 jint i6 = 6;
2062 jint i7 = 7;
2063 jint i8 = 8;
2064 jint i9 = 9;
2065 jint i10 = 10;
2066
Roland Levillainda4d79b2015-03-24 14:36:11 +00002067 jfloat f1 = bit_cast<jfloat, jint>(11);
2068 jfloat f2 = bit_cast<jfloat, jint>(12);
2069 jfloat f3 = bit_cast<jfloat, jint>(13);
2070 jfloat f4 = bit_cast<jfloat, jint>(14);
2071 jfloat f5 = bit_cast<jfloat, jint>(15);
2072 jfloat f6 = bit_cast<jfloat, jint>(16);
2073 jfloat f7 = bit_cast<jfloat, jint>(17);
2074 jfloat f8 = bit_cast<jfloat, jint>(18);
2075 jfloat f9 = bit_cast<jfloat, jint>(19);
2076 jfloat f10 = bit_cast<jfloat, jint>(20);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002077
2078 env_->CallStaticVoidMethod(jklass_, jmethod_, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, i1, i2, i3,
2079 i4, i5, i6, i7, i8, i9, i10);
2080}
2081
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002082JNI_TEST_CRITICAL(StackArgsFloatsFirst)
Andreas Gampe6e498692014-08-18 16:43:12 -07002083
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002084void Java_MyClassNatives_stackArgsMixed(JNIEnv*, jclass, jint i1, jfloat f1, jint i2,
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002085 jfloat f2, jint i3, jfloat f3, jint i4, jfloat f4, jint i5,
2086 jfloat f5, jint i6, jfloat f6, jint i7, jfloat f7, jint i8,
2087 jfloat f8, jint i9, jfloat f9, jint i10, jfloat f10) {
2088 EXPECT_EQ(i1, 1);
2089 EXPECT_EQ(i2, 2);
2090 EXPECT_EQ(i3, 3);
2091 EXPECT_EQ(i4, 4);
2092 EXPECT_EQ(i5, 5);
2093 EXPECT_EQ(i6, 6);
2094 EXPECT_EQ(i7, 7);
2095 EXPECT_EQ(i8, 8);
2096 EXPECT_EQ(i9, 9);
2097 EXPECT_EQ(i10, 10);
2098
Roland Levillainda4d79b2015-03-24 14:36:11 +00002099 jint i11 = bit_cast<jint, jfloat>(f1);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002100 EXPECT_EQ(i11, 11);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002101 jint i12 = bit_cast<jint, jfloat>(f2);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002102 EXPECT_EQ(i12, 12);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002103 jint i13 = bit_cast<jint, jfloat>(f3);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002104 EXPECT_EQ(i13, 13);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002105 jint i14 = bit_cast<jint, jfloat>(f4);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002106 EXPECT_EQ(i14, 14);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002107 jint i15 = bit_cast<jint, jfloat>(f5);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002108 EXPECT_EQ(i15, 15);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002109 jint i16 = bit_cast<jint, jfloat>(f6);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002110 EXPECT_EQ(i16, 16);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002111 jint i17 = bit_cast<jint, jfloat>(f7);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002112 EXPECT_EQ(i17, 17);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002113 jint i18 = bit_cast<jint, jfloat>(f8);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002114 EXPECT_EQ(i18, 18);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002115 jint i19 = bit_cast<jint, jfloat>(f9);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002116 EXPECT_EQ(i19, 19);
Roland Levillainda4d79b2015-03-24 14:36:11 +00002117 jint i20 = bit_cast<jint, jfloat>(f10);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002118 EXPECT_EQ(i20, 20);
2119}
2120
Andreas Gampe6e498692014-08-18 16:43:12 -07002121void JniCompilerTest::StackArgsMixedImpl() {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002122 SetUpForTest(true, "stackArgsMixed", "(IFIFIFIFIFIFIFIFIFIF)V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002123 CURRENT_JNI_WRAPPER(Java_MyClassNatives_stackArgsMixed));
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002124
2125 jint i1 = 1;
2126 jint i2 = 2;
2127 jint i3 = 3;
2128 jint i4 = 4;
2129 jint i5 = 5;
2130 jint i6 = 6;
2131 jint i7 = 7;
2132 jint i8 = 8;
2133 jint i9 = 9;
2134 jint i10 = 10;
2135
Roland Levillainda4d79b2015-03-24 14:36:11 +00002136 jfloat f1 = bit_cast<jfloat, jint>(11);
2137 jfloat f2 = bit_cast<jfloat, jint>(12);
2138 jfloat f3 = bit_cast<jfloat, jint>(13);
2139 jfloat f4 = bit_cast<jfloat, jint>(14);
2140 jfloat f5 = bit_cast<jfloat, jint>(15);
2141 jfloat f6 = bit_cast<jfloat, jint>(16);
2142 jfloat f7 = bit_cast<jfloat, jint>(17);
2143 jfloat f8 = bit_cast<jfloat, jint>(18);
2144 jfloat f9 = bit_cast<jfloat, jint>(19);
2145 jfloat f10 = bit_cast<jfloat, jint>(20);
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +07002146
2147 env_->CallStaticVoidMethod(jklass_, jmethod_, i1, f1, i2, f2, i3, f3, i4, f4, i5, f5, i6, f6, i7,
2148 f7, i8, f8, i9, f9, i10, f10);
2149}
2150
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002151JNI_TEST_CRITICAL(StackArgsMixed)
Andreas Gampe6e498692014-08-18 16:43:12 -07002152
Lazar Trsicf652d602015-06-24 16:30:21 +02002153#if defined(__mips__) && defined(__LP64__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
Pavle Batuta837e72a2016-03-16 11:31:46 +01002154// Function will fetch the last argument passed from caller that is now on top of the stack and
2155// return it as a 8B long. That way we can test if the caller has properly sign-extended the
2156// value when placing it on the stack.
2157__attribute__((naked))
2158jlong Java_MyClassNatives_getStackArgSignExtendedMips64(
2159 JNIEnv*, jclass, // Arguments passed from caller
2160 jint, jint, jint, jint, jint, jint, // through regs a0 to a7.
2161 jint) { // The last argument will be passed on the stack.
2162 __asm__(
2163 ".set noreorder\n\t" // Just return and store 8 bytes from the top of the stack
2164 "jr $ra\n\t" // in v0 (in branch delay slot). This should be the last
2165 "ld $v0, 0($sp)\n\t"); // argument. It is a 32-bit int, but it should be sign
2166 // extended and it occupies 64-bit location.
Lazar Trsicf652d602015-06-24 16:30:21 +02002167}
2168
2169void JniCompilerTest::StackArgsSignExtendedMips64Impl() {
Pavle Batuta837e72a2016-03-16 11:31:46 +01002170 uint64_t ret;
2171 SetUpForTest(true,
2172 "getStackArgSignExtendedMips64",
2173 "(IIIIIII)J",
2174 // Don't use wrapper because this is raw assembly function.
2175 reinterpret_cast<void*>(&Java_MyClassNatives_getStackArgSignExtendedMips64));
Lazar Trsicf652d602015-06-24 16:30:21 +02002176
Pavle Batuta837e72a2016-03-16 11:31:46 +01002177 // Mips64 ABI requires that arguments passed through stack be sign-extended 8B slots.
2178 // First 8 arguments are passed through registers.
2179 // Final argument's value is 7. When sign-extended, higher stack bits should be 0.
2180 ret = env_->CallStaticLongMethod(jklass_, jmethod_, 1, 2, 3, 4, 5, 6, 7);
2181 EXPECT_EQ(High32Bits(ret), static_cast<uint32_t>(0));
2182
2183 // Final argument's value is -8. When sign-extended, higher stack bits should be 0xffffffff.
2184 ret = env_->CallStaticLongMethod(jklass_, jmethod_, 1, 2, 3, 4, 5, 6, -8);
2185 EXPECT_EQ(High32Bits(ret), static_cast<uint32_t>(0xffffffff));
Lazar Trsicf652d602015-06-24 16:30:21 +02002186}
2187
Pavle Batuta837e72a2016-03-16 11:31:46 +01002188JNI_TEST(StackArgsSignExtendedMips64)
2189#endif
Lazar Trsicf652d602015-06-24 16:30:21 +02002190
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002191void Java_MyClassNatives_normalNative(JNIEnv*, jclass) {
2192 // Intentionally left empty.
2193}
2194
2195// Methods not annotated with anything are not considered "fast native"
2196// -- Check that the annotation lookup does not find it.
2197void JniCompilerTest::NormalNativeImpl() {
2198 SetUpForTest(/* direct */ true,
2199 "normalNative",
2200 "()V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002201 CURRENT_JNI_WRAPPER(Java_MyClassNatives_normalNative));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002202
Andreas Gampe13b27842016-11-07 16:48:23 -08002203 ArtMethod* method = jni::DecodeArtMethod(jmethod_);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002204 ASSERT_TRUE(method != nullptr);
2205
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002206 EXPECT_FALSE(method->IsCriticalNative());
2207 EXPECT_FALSE(method->IsFastNative());
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002208}
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002209
2210// TODO: just rename the java functions to the standard convention and remove duplicated tests
2211JNI_TEST_NORMAL_ONLY(NormalNative)
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002212
2213// Methods annotated with @FastNative are considered "fast native"
2214// -- Check that the annotation lookup succeeds.
2215void Java_MyClassNatives_fastNative(JNIEnv*, jclass) {
2216 // Intentionally left empty.
2217}
2218
2219void JniCompilerTest::FastNativeImpl() {
2220 SetUpForTest(/* direct */ true,
2221 "fastNative",
2222 "()V",
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002223 CURRENT_JNI_WRAPPER(Java_MyClassNatives_fastNative));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002224
Andreas Gampe13b27842016-11-07 16:48:23 -08002225 ArtMethod* method = jni::DecodeArtMethod(jmethod_);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002226 ASSERT_TRUE(method != nullptr);
2227
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002228 EXPECT_FALSE(method->IsCriticalNative());
2229 EXPECT_TRUE(method->IsFastNative());
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002230}
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002231
2232// TODO: just rename the java functions to the standard convention and remove duplicated tests
2233JNI_TEST_NORMAL_ONLY(FastNative)
2234
2235int gJava_myClassNatives_criticalNative_calls[kJniKindCount] = {};
2236// Methods annotated with @CriticalNative are considered "critical native"
2237// -- Check that the annotation lookup succeeds.
2238void Java_MyClassNatives_criticalNative() {
2239 gJava_myClassNatives_criticalNative_calls[gCurrentJni]++;
2240}
2241
2242void JniCompilerTest::CriticalNativeImpl() {
2243 SetUpForTest(/* direct */ true,
2244 // Important: Don't change the "current jni" yet to avoid a method name suffix.
2245 "criticalNative",
2246 "()V",
2247 // TODO: Use CURRENT_JNI_WRAPPER instead which is more generic.
2248 reinterpret_cast<void*>(&Java_MyClassNatives_criticalNative));
2249
2250 // TODO: remove this manual updating of the current JNI. Merge with the other tests.
2251 UpdateCurrentJni(JniKind::kCritical);
2252 ASSERT_TRUE(IsCurrentJniCritical());
2253
Andreas Gampe13b27842016-11-07 16:48:23 -08002254 ArtMethod* method = jni::DecodeArtMethod(jmethod_);
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002255 ASSERT_TRUE(method != nullptr);
2256
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002257 EXPECT_TRUE(method->IsCriticalNative());
2258 EXPECT_FALSE(method->IsFastNative());
Igor Murashkin367f3dd2016-09-01 17:00:24 -07002259
2260 EXPECT_EQ(0, gJava_myClassNatives_criticalNative_calls[gCurrentJni]);
2261 env_->CallStaticVoidMethod(jklass_, jmethod_);
2262 EXPECT_EQ(1, gJava_myClassNatives_criticalNative_calls[gCurrentJni]);
2263
2264 gJava_myClassNatives_criticalNative_calls[gCurrentJni] = 0;
2265}
2266
2267// TODO: just rename the java functions to the standard convention and remove duplicated tests
2268JNI_TEST_NORMAL_ONLY(CriticalNative)
Igor Murashkin9d4b6da2016-07-29 09:51:58 -07002269
Ian Rogersb033c752011-07-20 12:22:35 -07002270} // namespace art