Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
| 18 | #include "common_test.h" |
| 19 | #include "dex_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 21 | #include "indirect_reference_table.h" |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 22 | #include "jni_internal.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 23 | #include "mem_map.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame^] | 24 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/object_array-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 28 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/stack_trace_element.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | #include "ScopedLocalRef.h" |
| 32 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "thread.h" |
Elliott Hughes | a168c83 | 2012-06-12 15:34:20 -0700 | [diff] [blame] | 34 | #include "UniquePtr.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 36 | extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_bar(JNIEnv*, jobject, jint count) { |
Brian Carlstrom | b9cc1ca | 2012-01-27 00:57:42 -0800 | [diff] [blame] | 37 | return count + 1; |
| 38 | } |
| 39 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 40 | extern "C" JNIEXPORT jint JNICALL Java_MyClassNatives_sbar(JNIEnv*, jclass, jint count) { |
Ian Rogers | 1cefdbd | 2012-02-29 09:34:50 -0800 | [diff] [blame] | 41 | return count + 1; |
| 42 | } |
| 43 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 44 | namespace art { |
| 45 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 46 | class JniCompilerTest : public CommonTest { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 47 | protected: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 48 | void CompileForTest(jobject class_loader, bool direct, |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 49 | const char* method_name, const char* method_sig) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 51 | // Compile the native method before starting the runtime |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | mirror::Class* c = class_linker_->FindClass("LMyClassNatives;", |
| 53 | soa.Decode<mirror::ClassLoader*>(class_loader)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame^] | 54 | mirror::ArtMethod* method; |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 55 | if (direct) { |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 56 | method = c->FindDirectMethod(method_name, method_sig); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 57 | } else { |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 58 | method = c->FindVirtualMethod(method_name, method_sig); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 59 | } |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 60 | ASSERT_TRUE(method != NULL) << method_name << " " << method_sig; |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 61 | if (method->GetEntryPointFromCompiledCode() != NULL) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 62 | return; |
| 63 | } |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 64 | CompileMethod(method); |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(method->GetEntryPointFromCompiledCode() != NULL) << method_name << " " << method_sig; |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | void SetUpForTest(bool direct, const char* method_name, const char* method_sig, |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 69 | void* native_fnptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | // Initialize class loader and compile method when runtime not started. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 71 | if (!runtime_->IsStarted()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | { |
| 73 | ScopedObjectAccess soa(Thread::Current()); |
| 74 | class_loader_ = LoadDex("MyClassNatives"); |
| 75 | } |
| 76 | CompileForTest(class_loader_, direct, method_name, method_sig); |
| 77 | // Start runtime. |
| 78 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 79 | bool started = runtime_->Start(); |
| 80 | CHECK(started); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 81 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 82 | // JNI operations after runtime start. |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 83 | env_ = Thread::Current()->GetJniEnv(); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 84 | jklass_ = env_->FindClass("MyClassNatives"); |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 85 | ASSERT_TRUE(jklass_ != NULL) << method_name << " " << method_sig; |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 86 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 87 | if (direct) { |
| 88 | jmethod_ = env_->GetStaticMethodID(jklass_, method_name, method_sig); |
| 89 | } else { |
| 90 | jmethod_ = env_->GetMethodID(jklass_, method_name, method_sig); |
| 91 | } |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(jmethod_ != NULL) << method_name << " " << method_sig; |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 93 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 94 | if (native_fnptr != NULL) { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 95 | JNINativeMethod methods[] = { { method_name, method_sig, native_fnptr } }; |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 96 | ASSERT_EQ(JNI_OK, env_->RegisterNatives(jklass_, methods, 1)) |
| 97 | << method_name << " " << method_sig; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 98 | } else { |
| 99 | env_->UnregisterNatives(jklass_); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 100 | } |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 101 | |
| 102 | jmethodID constructor = env_->GetMethodID(jklass_, "<init>", "()V"); |
| 103 | jobj_ = env_->NewObject(jklass_, constructor); |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 104 | ASSERT_TRUE(jobj_ != NULL) << method_name << " " << method_sig; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 107 | public: |
| 108 | static jclass jklass_; |
| 109 | static jobject jobj_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 110 | static jobject class_loader_; |
| 111 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 112 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 113 | protected: |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 114 | JNIEnv* env_; |
| 115 | jmethodID jmethod_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 118 | jclass JniCompilerTest::jklass_; |
| 119 | jobject JniCompilerTest::jobj_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 120 | jobject JniCompilerTest::class_loader_; |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 121 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 122 | int gJava_MyClassNatives_foo_calls = 0; |
| 123 | void Java_MyClassNatives_foo(JNIEnv* env, jobject thisObj) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | // 1 = thisObj |
| 125 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 126 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
| 127 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 128 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 129 | EXPECT_TRUE(thisObj != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 131 | gJava_MyClassNatives_foo_calls++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 134 | TEST_F(JniCompilerTest, CompileAndRunNoArgMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 135 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 136 | SetUpForTest(false, "foo", "()V", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 137 | reinterpret_cast<void*>(&Java_MyClassNatives_foo)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 138 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 139 | EXPECT_EQ(0, gJava_MyClassNatives_foo_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 140 | env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 141 | EXPECT_EQ(1, gJava_MyClassNatives_foo_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 142 | env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 143 | EXPECT_EQ(2, gJava_MyClassNatives_foo_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 146 | TEST_F(JniCompilerTest, CompileAndRunIntMethodThroughStub) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 147 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | SetUpForTest(false, "bar", "(I)I", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 149 | NULL /* calling through stub will link with &Java_MyClassNatives_bar */); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 150 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 151 | ScopedObjectAccess soa(Thread::Current()); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 152 | std::string reason; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 153 | ASSERT_TRUE( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 154 | Runtime::Current()->GetJavaVM()->LoadNativeLibrary("", soa.Decode<mirror::ClassLoader*>(class_loader_), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | reason)) << reason; |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 156 | |
| 157 | jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 24); |
| 158 | EXPECT_EQ(25, result); |
| 159 | } |
| 160 | |
Ian Rogers | 1cefdbd | 2012-02-29 09:34:50 -0800 | [diff] [blame] | 161 | TEST_F(JniCompilerTest, CompileAndRunStaticIntMethodThroughStub) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 162 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 163 | SetUpForTest(true, "sbar", "(I)I", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 164 | NULL /* calling through stub will link with &Java_MyClassNatives_sbar */); |
Ian Rogers | 1cefdbd | 2012-02-29 09:34:50 -0800 | [diff] [blame] | 165 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 166 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1cefdbd | 2012-02-29 09:34:50 -0800 | [diff] [blame] | 167 | std::string reason; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 168 | ASSERT_TRUE( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | Runtime::Current()->GetJavaVM()->LoadNativeLibrary("", soa.Decode<mirror::ClassLoader*>(class_loader_), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 170 | reason)) << reason; |
Ian Rogers | 1cefdbd | 2012-02-29 09:34:50 -0800 | [diff] [blame] | 171 | |
| 172 | jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 42); |
| 173 | EXPECT_EQ(43, result); |
| 174 | } |
| 175 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 176 | int gJava_MyClassNatives_fooI_calls = 0; |
| 177 | jint Java_MyClassNatives_fooI(JNIEnv* env, jobject thisObj, jint x) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 178 | // 1 = thisObj |
| 179 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 180 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 181 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 182 | EXPECT_TRUE(thisObj != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 183 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 184 | gJava_MyClassNatives_fooI_calls++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 185 | return x; |
| 186 | } |
| 187 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 188 | TEST_F(JniCompilerTest, CompileAndRunIntMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 189 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 190 | SetUpForTest(false, "fooI", "(I)I", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 191 | reinterpret_cast<void*>(&Java_MyClassNatives_fooI)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 192 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 193 | EXPECT_EQ(0, gJava_MyClassNatives_fooI_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 194 | jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 42); |
| 195 | EXPECT_EQ(42, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 196 | EXPECT_EQ(1, gJava_MyClassNatives_fooI_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 197 | result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 0xCAFED00D); |
| 198 | EXPECT_EQ(static_cast<jint>(0xCAFED00D), result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 199 | EXPECT_EQ(2, gJava_MyClassNatives_fooI_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 202 | int gJava_MyClassNatives_fooII_calls = 0; |
| 203 | jint Java_MyClassNatives_fooII(JNIEnv* env, jobject thisObj, jint x, jint y) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 204 | // 1 = thisObj |
| 205 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 206 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 207 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 208 | EXPECT_TRUE(thisObj != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 209 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 210 | gJava_MyClassNatives_fooII_calls++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 211 | return x - y; // non-commutative operator |
| 212 | } |
| 213 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 214 | TEST_F(JniCompilerTest, CompileAndRunIntIntMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 215 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 216 | SetUpForTest(false, "fooII", "(II)I", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 217 | reinterpret_cast<void*>(&Java_MyClassNatives_fooII)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 218 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 219 | EXPECT_EQ(0, gJava_MyClassNatives_fooII_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 220 | jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 99, 10); |
| 221 | EXPECT_EQ(99 - 10, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 222 | EXPECT_EQ(1, gJava_MyClassNatives_fooII_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 223 | result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 0xCAFEBABE, |
| 224 | 0xCAFED00D); |
| 225 | EXPECT_EQ(static_cast<jint>(0xCAFEBABE - 0xCAFED00D), result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 226 | EXPECT_EQ(2, gJava_MyClassNatives_fooII_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 229 | int gJava_MyClassNatives_fooJJ_calls = 0; |
| 230 | jlong Java_MyClassNatives_fooJJ(JNIEnv* env, jobject thisObj, jlong x, jlong y) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 231 | // 1 = thisObj |
| 232 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 233 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | 9b269d2 | 2011-09-04 14:06:05 -0700 | [diff] [blame] | 234 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 235 | EXPECT_TRUE(thisObj != NULL); |
| 236 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 237 | gJava_MyClassNatives_fooJJ_calls++; |
Ian Rogers | 9b269d2 | 2011-09-04 14:06:05 -0700 | [diff] [blame] | 238 | return x - y; // non-commutative operator |
| 239 | } |
| 240 | |
| 241 | TEST_F(JniCompilerTest, CompileAndRunLongLongMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 242 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 243 | SetUpForTest(false, "fooJJ", "(JJ)J", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 244 | reinterpret_cast<void*>(&Java_MyClassNatives_fooJJ)); |
Ian Rogers | 9b269d2 | 2011-09-04 14:06:05 -0700 | [diff] [blame] | 245 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 246 | EXPECT_EQ(0, gJava_MyClassNatives_fooJJ_calls); |
Ian Rogers | 9b269d2 | 2011-09-04 14:06:05 -0700 | [diff] [blame] | 247 | jlong a = 0x1234567890ABCDEFll; |
| 248 | jlong b = 0xFEDCBA0987654321ll; |
| 249 | jlong result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b); |
| 250 | EXPECT_EQ(a - b, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 251 | EXPECT_EQ(1, gJava_MyClassNatives_fooJJ_calls); |
Ian Rogers | 9b269d2 | 2011-09-04 14:06:05 -0700 | [diff] [blame] | 252 | result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, b, a); |
| 253 | EXPECT_EQ(b - a, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 254 | EXPECT_EQ(2, gJava_MyClassNatives_fooJJ_calls); |
Ian Rogers | 9b269d2 | 2011-09-04 14:06:05 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 257 | int gJava_MyClassNatives_fooDD_calls = 0; |
| 258 | jdouble Java_MyClassNatives_fooDD(JNIEnv* env, jobject thisObj, jdouble x, jdouble y) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 259 | // 1 = thisObj |
| 260 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 261 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 262 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 263 | EXPECT_TRUE(thisObj != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 264 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 265 | gJava_MyClassNatives_fooDD_calls++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 266 | return x - y; // non-commutative operator |
| 267 | } |
| 268 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 269 | TEST_F(JniCompilerTest, CompileAndRunDoubleDoubleMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 270 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 271 | SetUpForTest(false, "fooDD", "(DD)D", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 272 | reinterpret_cast<void*>(&Java_MyClassNatives_fooDD)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 273 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 274 | EXPECT_EQ(0, gJava_MyClassNatives_fooDD_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 275 | jdouble result = env_->CallNonvirtualDoubleMethod(jobj_, jklass_, jmethod_, |
| 276 | 99.0, 10.0); |
| 277 | EXPECT_EQ(99.0 - 10.0, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 278 | EXPECT_EQ(1, gJava_MyClassNatives_fooDD_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 279 | jdouble a = 3.14159265358979323846; |
| 280 | jdouble b = 0.69314718055994530942; |
| 281 | result = env_->CallNonvirtualDoubleMethod(jobj_, jklass_, jmethod_, a, b); |
| 282 | EXPECT_EQ(a - b, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 283 | EXPECT_EQ(2, gJava_MyClassNatives_fooDD_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Elliott Hughes | 3e778f7 | 2012-05-21 15:29:52 -0700 | [diff] [blame] | 286 | int gJava_MyClassNatives_fooJJ_synchronized_calls = 0; |
| 287 | jlong Java_MyClassNatives_fooJJ_synchronized(JNIEnv* env, jobject thisObj, jlong x, jlong y) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 288 | // 1 = thisObj |
| 289 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 290 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Elliott Hughes | 3e778f7 | 2012-05-21 15:29:52 -0700 | [diff] [blame] | 291 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 292 | EXPECT_TRUE(thisObj != NULL); |
| 293 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
| 294 | gJava_MyClassNatives_fooJJ_synchronized_calls++; |
| 295 | return x | y; |
| 296 | } |
| 297 | |
| 298 | TEST_F(JniCompilerTest, CompileAndRun_fooJJ_synchronized) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 299 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 300 | SetUpForTest(false, "fooJJ_synchronized", "(JJ)J", |
Elliott Hughes | 3e778f7 | 2012-05-21 15:29:52 -0700 | [diff] [blame] | 301 | reinterpret_cast<void*>(&Java_MyClassNatives_fooJJ_synchronized)); |
| 302 | |
| 303 | EXPECT_EQ(0, gJava_MyClassNatives_fooJJ_synchronized_calls); |
| 304 | jlong a = 0x1000000020000000ULL; |
| 305 | jlong b = 0x00ff000000aa0000ULL; |
| 306 | jlong result = env_->CallNonvirtualLongMethod(jobj_, jklass_, jmethod_, a, b); |
| 307 | EXPECT_EQ(a | b, result); |
| 308 | EXPECT_EQ(1, gJava_MyClassNatives_fooJJ_synchronized_calls); |
| 309 | } |
| 310 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 311 | int gJava_MyClassNatives_fooIOO_calls = 0; |
| 312 | jobject Java_MyClassNatives_fooIOO(JNIEnv* env, jobject thisObj, jint x, jobject y, |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 313 | jobject z) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 314 | // 3 = this + y + z |
| 315 | EXPECT_EQ(3U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 316 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 317 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 318 | EXPECT_TRUE(thisObj != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 319 | EXPECT_TRUE(env->IsInstanceOf(thisObj, JniCompilerTest::jklass_)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 320 | gJava_MyClassNatives_fooIOO_calls++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 321 | switch (x) { |
| 322 | case 1: |
| 323 | return y; |
| 324 | case 2: |
| 325 | return z; |
| 326 | default: |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 327 | return thisObj; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 331 | TEST_F(JniCompilerTest, CompileAndRunIntObjectObjectMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 332 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 333 | SetUpForTest(false, "fooIOO", |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 334 | "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 335 | reinterpret_cast<void*>(&Java_MyClassNatives_fooIOO)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 336 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 337 | EXPECT_EQ(0, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 338 | jobject result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, NULL, NULL); |
| 339 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 340 | EXPECT_EQ(1, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 341 | |
| 342 | result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, NULL, jklass_); |
| 343 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 344 | EXPECT_EQ(2, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 345 | result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 1, NULL, jklass_); |
| 346 | EXPECT_TRUE(env_->IsSameObject(NULL, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 347 | EXPECT_EQ(3, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 348 | result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 2, NULL, jklass_); |
| 349 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 350 | EXPECT_EQ(4, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 351 | |
| 352 | result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 0, jklass_, NULL); |
| 353 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 354 | EXPECT_EQ(5, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 355 | result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 1, jklass_, NULL); |
| 356 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 357 | EXPECT_EQ(6, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 358 | result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, 2, jklass_, NULL); |
| 359 | EXPECT_TRUE(env_->IsSameObject(NULL, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 360 | EXPECT_EQ(7, gJava_MyClassNatives_fooIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 363 | int gJava_MyClassNatives_fooSII_calls = 0; |
| 364 | jint Java_MyClassNatives_fooSII(JNIEnv* env, jclass klass, jint x, jint y) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 365 | // 1 = klass |
| 366 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 367 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 368 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 369 | EXPECT_TRUE(klass != NULL); |
| 370 | EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 371 | gJava_MyClassNatives_fooSII_calls++; |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 372 | return x + y; |
| 373 | } |
| 374 | |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 375 | TEST_F(JniCompilerTest, CompileAndRunStaticIntIntMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 376 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 377 | SetUpForTest(true, "fooSII", "(II)I", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 378 | reinterpret_cast<void*>(&Java_MyClassNatives_fooSII)); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 379 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 380 | EXPECT_EQ(0, gJava_MyClassNatives_fooSII_calls); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 381 | jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 20, 30); |
| 382 | EXPECT_EQ(50, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 383 | EXPECT_EQ(1, gJava_MyClassNatives_fooSII_calls); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 386 | int gJava_MyClassNatives_fooSDD_calls = 0; |
| 387 | jdouble Java_MyClassNatives_fooSDD(JNIEnv* env, jclass klass, jdouble x, jdouble y) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 388 | // 1 = klass |
| 389 | EXPECT_EQ(1U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 390 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 391 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 392 | EXPECT_TRUE(klass != NULL); |
| 393 | EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 394 | gJava_MyClassNatives_fooSDD_calls++; |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 395 | return x - y; // non-commutative operator |
| 396 | } |
| 397 | |
| 398 | TEST_F(JniCompilerTest, CompileAndRunStaticDoubleDoubleMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 399 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 400 | SetUpForTest(true, "fooSDD", "(DD)D", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 401 | reinterpret_cast<void*>(&Java_MyClassNatives_fooSDD)); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 402 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 403 | EXPECT_EQ(0, gJava_MyClassNatives_fooSDD_calls); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 404 | jdouble result = env_->CallStaticDoubleMethod(jklass_, jmethod_, 99.0, 10.0); |
| 405 | EXPECT_EQ(99.0 - 10.0, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 406 | EXPECT_EQ(1, gJava_MyClassNatives_fooSDD_calls); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 407 | jdouble a = 3.14159265358979323846; |
| 408 | jdouble b = 0.69314718055994530942; |
| 409 | result = env_->CallStaticDoubleMethod(jklass_, jmethod_, a, b); |
| 410 | EXPECT_EQ(a - b, result); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 411 | EXPECT_EQ(2, gJava_MyClassNatives_fooSDD_calls); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 414 | int gJava_MyClassNatives_fooSIOO_calls = 0; |
| 415 | jobject Java_MyClassNatives_fooSIOO(JNIEnv* env, jclass klass, jint x, jobject y, |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 416 | jobject z) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 417 | // 3 = klass + y + z |
| 418 | EXPECT_EQ(3U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 419 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 420 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 421 | EXPECT_TRUE(klass != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 422 | EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 423 | gJava_MyClassNatives_fooSIOO_calls++; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 424 | switch (x) { |
| 425 | case 1: |
| 426 | return y; |
| 427 | case 2: |
| 428 | return z; |
| 429 | default: |
| 430 | return klass; |
| 431 | } |
| 432 | } |
| 433 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 434 | |
| 435 | TEST_F(JniCompilerTest, CompileAndRunStaticIntObjectObjectMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 436 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 437 | SetUpForTest(true, "fooSIOO", |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 438 | "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 439 | reinterpret_cast<void*>(&Java_MyClassNatives_fooSIOO)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 440 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 441 | EXPECT_EQ(0, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 442 | jobject result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, NULL, NULL); |
| 443 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 444 | EXPECT_EQ(1, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 445 | |
| 446 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, NULL, jobj_); |
| 447 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 448 | EXPECT_EQ(2, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 449 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, NULL, jobj_); |
| 450 | EXPECT_TRUE(env_->IsSameObject(NULL, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 451 | EXPECT_EQ(3, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 452 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, NULL, jobj_); |
| 453 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 454 | EXPECT_EQ(4, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 455 | |
| 456 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, jobj_, NULL); |
| 457 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 458 | EXPECT_EQ(5, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 459 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, jobj_, NULL); |
| 460 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 461 | EXPECT_EQ(6, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 462 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, NULL); |
| 463 | EXPECT_TRUE(env_->IsSameObject(NULL, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 464 | EXPECT_EQ(7, gJava_MyClassNatives_fooSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 467 | int gJava_MyClassNatives_fooSSIOO_calls = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 468 | jobject Java_MyClassNatives_fooSSIOO(JNIEnv* env, jclass klass, jint x, jobject y, jobject z) { |
| 469 | // 3 = klass + y + z |
| 470 | EXPECT_EQ(3U, Thread::Current()->NumStackReferences()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 471 | EXPECT_EQ(kNative, Thread::Current()->GetState()); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 472 | EXPECT_EQ(Thread::Current()->GetJniEnv(), env); |
| 473 | EXPECT_TRUE(klass != NULL); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 474 | EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 475 | gJava_MyClassNatives_fooSSIOO_calls++; |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 476 | switch (x) { |
| 477 | case 1: |
| 478 | return y; |
| 479 | case 2: |
| 480 | return z; |
| 481 | default: |
| 482 | return klass; |
| 483 | } |
| 484 | } |
| 485 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 486 | TEST_F(JniCompilerTest, CompileAndRunStaticSynchronizedIntObjectObjectMethod) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 487 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 488 | SetUpForTest(true, "fooSSIOO", |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 489 | "(ILjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 490 | reinterpret_cast<void*>(&Java_MyClassNatives_fooSSIOO)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 491 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 492 | EXPECT_EQ(0, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 493 | jobject result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, NULL, NULL); |
| 494 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 495 | EXPECT_EQ(1, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 496 | |
| 497 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, NULL, jobj_); |
| 498 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 499 | EXPECT_EQ(2, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 500 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, NULL, jobj_); |
| 501 | EXPECT_TRUE(env_->IsSameObject(NULL, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 502 | EXPECT_EQ(3, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 503 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, NULL, jobj_); |
| 504 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 505 | EXPECT_EQ(4, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 506 | |
| 507 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 0, jobj_, NULL); |
| 508 | EXPECT_TRUE(env_->IsSameObject(jklass_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 509 | EXPECT_EQ(5, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 510 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 1, jobj_, NULL); |
| 511 | EXPECT_TRUE(env_->IsSameObject(jobj_, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 512 | EXPECT_EQ(6, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 513 | result = env_->CallStaticObjectMethod(jklass_, jmethod_, 2, jobj_, NULL); |
| 514 | EXPECT_TRUE(env_->IsSameObject(NULL, result)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 515 | EXPECT_EQ(7, gJava_MyClassNatives_fooSSIOO_calls); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 518 | void Java_MyClassNatives_throwException(JNIEnv* env, jobject) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 519 | jclass c = env->FindClass("java/lang/RuntimeException"); |
| 520 | env->ThrowNew(c, "hello"); |
| 521 | } |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 522 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 523 | TEST_F(JniCompilerTest, ExceptionHandling) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 524 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 525 | { |
| 526 | ASSERT_FALSE(runtime_->IsStarted()); |
| 527 | ScopedObjectAccess soa(Thread::Current()); |
| 528 | class_loader_ = LoadDex("MyClassNatives"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 529 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 530 | // all compilation needs to happen before Runtime::Start |
| 531 | CompileForTest(class_loader_, false, "foo", "()V"); |
| 532 | CompileForTest(class_loader_, false, "throwException", "()V"); |
| 533 | CompileForTest(class_loader_, false, "foo", "()V"); |
| 534 | } |
| 535 | // Start runtime to avoid re-initialization in SetupForTest. |
| 536 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 537 | bool started = runtime_->Start(); |
| 538 | CHECK(started); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 539 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 540 | gJava_MyClassNatives_foo_calls = 0; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 541 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 542 | // Check a single call of a JNI method is ok |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 543 | SetUpForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClassNatives_foo)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 544 | env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 545 | EXPECT_EQ(1, gJava_MyClassNatives_foo_calls); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 546 | EXPECT_FALSE(Thread::Current()->IsExceptionPending()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 547 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 548 | // Get class for exception we expect to be thrown |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 549 | ScopedLocalRef<jclass> jlre(env_, env_->FindClass("java/lang/RuntimeException")); |
| 550 | SetUpForTest(false, "throwException", "()V", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 551 | reinterpret_cast<void*>(&Java_MyClassNatives_throwException)); |
| 552 | // Call Java_MyClassNatives_throwException (JNI method that throws exception) |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 553 | env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 554 | EXPECT_EQ(1, gJava_MyClassNatives_foo_calls); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 555 | EXPECT_TRUE(env_->ExceptionCheck() == JNI_TRUE); |
| 556 | ScopedLocalRef<jthrowable> exception(env_, env_->ExceptionOccurred()); |
| 557 | env_->ExceptionClear(); |
| 558 | EXPECT_TRUE(env_->IsInstanceOf(exception.get(), jlre.get())); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 559 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 560 | // Check a single call of a JNI method is ok |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 561 | SetUpForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClassNatives_foo)); |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 562 | env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 563 | EXPECT_EQ(2, gJava_MyClassNatives_foo_calls); |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 566 | jint Java_MyClassNatives_nativeUpCall(JNIEnv* env, jobject thisObj, jint i) { |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 567 | if (i <= 0) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 568 | // We want to check raw Object*/Array* below |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 569 | ScopedObjectAccess soa(env); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 570 | |
| 571 | // Build stack trace |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 572 | jobject internal = Thread::Current()->CreateInternalStackTrace(soa); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 573 | jobjectArray ste_array = Thread::InternalStackTraceToStackTraceElementArray(env, internal); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 574 | mirror::ObjectArray<mirror::StackTraceElement>* trace_array = |
| 575 | soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(ste_array); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 576 | EXPECT_TRUE(trace_array != NULL); |
| 577 | EXPECT_EQ(11, trace_array->GetLength()); |
| 578 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 579 | // Check stack trace entries have expected values |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 580 | for (int32_t i = 0; i < trace_array->GetLength(); ++i) { |
| 581 | EXPECT_EQ(-2, trace_array->Get(i)->GetLineNumber()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 582 | mirror::StackTraceElement* ste = trace_array->Get(i); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 583 | EXPECT_STREQ("MyClassNatives.java", ste->GetFileName()->ToModifiedUtf8().c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 584 | EXPECT_STREQ("MyClassNatives", ste->GetDeclaringClass()->ToModifiedUtf8().c_str()); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 585 | EXPECT_STREQ("fooI", ste->GetMethodName()->ToModifiedUtf8().c_str()); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 586 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 587 | |
| 588 | // end recursion |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } else { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 591 | jclass jklass = env->FindClass("MyClassNatives"); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 592 | EXPECT_TRUE(jklass != NULL); |
| 593 | jmethodID jmethod = env->GetMethodID(jklass, "fooI", "(I)I"); |
| 594 | EXPECT_TRUE(jmethod != NULL); |
| 595 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 596 | // Recurse with i - 1 |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 597 | jint result = env->CallNonvirtualIntMethod(thisObj, jklass, jmethod, i - 1); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 598 | |
| 599 | // Return sum of all depths |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 600 | return i + result; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | TEST_F(JniCompilerTest, NativeStackTraceElement) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 605 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 606 | SetUpForTest(false, "fooI", "(I)I", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 607 | reinterpret_cast<void*>(&Java_MyClassNatives_nativeUpCall)); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 608 | jint result = env_->CallNonvirtualIntMethod(jobj_, jklass_, jmethod_, 10); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 609 | EXPECT_EQ(10+9+8+7+6+5+4+3+2+1, result); |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 612 | jobject Java_MyClassNatives_fooO(JNIEnv* env, jobject, jobject x) { |
Shih-wei Liao | 558788e | 2011-09-01 02:39:11 -0700 | [diff] [blame] | 613 | return env->NewGlobalRef(x); |
| 614 | } |
| 615 | |
Ian Rogers | b9231c8 | 2011-09-05 22:13:19 -0700 | [diff] [blame] | 616 | TEST_F(JniCompilerTest, ReturnGlobalRef) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 617 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 618 | SetUpForTest(false, "fooO", "(Ljava/lang/Object;)Ljava/lang/Object;", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 619 | reinterpret_cast<void*>(&Java_MyClassNatives_fooO)); |
Shih-wei Liao | 558788e | 2011-09-01 02:39:11 -0700 | [diff] [blame] | 620 | jobject result = env_->CallNonvirtualObjectMethod(jobj_, jklass_, jmethod_, jobj_); |
| 621 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(result)); |
| 622 | EXPECT_TRUE(env_->IsSameObject(result, jobj_)); |
| 623 | } |
| 624 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 625 | jint local_ref_test(JNIEnv* env, jobject thisObj, jint x) { |
| 626 | // Add 10 local references |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 627 | ScopedObjectAccess soa(env); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 628 | for (int i = 0; i < 10; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 629 | soa.AddLocalReference<jobject>(soa.Decode<mirror::Object*>(thisObj)); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 630 | } |
| 631 | return x+1; |
| 632 | } |
| 633 | |
| 634 | TEST_F(JniCompilerTest, LocalReferenceTableClearingTest) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 635 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 636 | SetUpForTest(false, "fooI", "(I)I", reinterpret_cast<void*>(&local_ref_test)); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 637 | // 1000 invocations of a method that adds 10 local references |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 638 | for (int i = 0; i < 1000; i++) { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 639 | jint result = env_->CallIntMethod(jobj_, jmethod_, i); |
| 640 | EXPECT_TRUE(result == i + 1); |
| 641 | } |
| 642 | } |
| 643 | |
Ian Rogers | b9231c8 | 2011-09-05 22:13:19 -0700 | [diff] [blame] | 644 | void my_arraycopy(JNIEnv* env, jclass klass, jobject src, jint src_pos, jobject dst, jint dst_pos, jint length) { |
| 645 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jklass_, klass)); |
| 646 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jklass_, dst)); |
Ian Rogers | 82f3e09 | 2011-09-05 22:54:45 -0700 | [diff] [blame] | 647 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, src)); |
Ian Rogers | b9231c8 | 2011-09-05 22:13:19 -0700 | [diff] [blame] | 648 | EXPECT_EQ(1234, src_pos); |
| 649 | EXPECT_EQ(5678, dst_pos); |
| 650 | EXPECT_EQ(9876, length); |
| 651 | } |
| 652 | |
| 653 | TEST_F(JniCompilerTest, JavaLangSystemArrayCopy) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 654 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 655 | SetUpForTest(true, "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V", |
Ian Rogers | b9231c8 | 2011-09-05 22:13:19 -0700 | [diff] [blame] | 656 | reinterpret_cast<void*>(&my_arraycopy)); |
Ian Rogers | 82f3e09 | 2011-09-05 22:54:45 -0700 | [diff] [blame] | 657 | env_->CallStaticVoidMethod(jklass_, jmethod_, jobj_, 1234, jklass_, 5678, 9876); |
Ian Rogers | b9231c8 | 2011-09-05 22:13:19 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 660 | jboolean my_casi(JNIEnv* env, jobject unsafe, jobject obj, jlong offset, jint expected, jint newval) { |
| 661 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, unsafe)); |
| 662 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj)); |
| 663 | EXPECT_EQ(0x12345678ABCDEF88ll, offset); |
| 664 | EXPECT_EQ(static_cast<jint>(0xCAFEF00D), expected); |
| 665 | EXPECT_EQ(static_cast<jint>(0xEBADF00D), newval); |
| 666 | return JNI_TRUE; |
| 667 | } |
| 668 | |
| 669 | TEST_F(JniCompilerTest, CompareAndSwapInt) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 670 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 671 | SetUpForTest(false, "compareAndSwapInt", "(Ljava/lang/Object;JII)Z", |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 672 | reinterpret_cast<void*>(&my_casi)); |
| 673 | jboolean result = env_->CallBooleanMethod(jobj_, jmethod_, jobj_, 0x12345678ABCDEF88ll, 0xCAFEF00D, 0xEBADF00D); |
| 674 | EXPECT_EQ(result, JNI_TRUE); |
| 675 | } |
| 676 | |
Ian Rogers | c779284 | 2012-03-03 15:36:20 -0800 | [diff] [blame] | 677 | jint my_gettext(JNIEnv* env, jclass klass, jlong val1, jobject obj1, jlong val2, jobject obj2) { |
| 678 | EXPECT_TRUE(env->IsInstanceOf(JniCompilerTest::jobj_, klass)); |
| 679 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj1)); |
| 680 | EXPECT_TRUE(env->IsSameObject(JniCompilerTest::jobj_, obj2)); |
| 681 | EXPECT_EQ(0x12345678ABCDEF88ll, val1); |
| 682 | EXPECT_EQ(0x7FEDCBA987654321ll, val2); |
| 683 | return 42; |
| 684 | } |
| 685 | |
| 686 | TEST_F(JniCompilerTest, GetText) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 687 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 688 | SetUpForTest(true, "getText", "(JLjava/lang/Object;JLjava/lang/Object;)I", |
Ian Rogers | c779284 | 2012-03-03 15:36:20 -0800 | [diff] [blame] | 689 | reinterpret_cast<void*>(&my_gettext)); |
| 690 | jint result = env_->CallStaticIntMethod(jklass_, jmethod_, 0x12345678ABCDEF88ll, jobj_, |
| 691 | 0x7FEDCBA987654321ll, jobj_); |
| 692 | EXPECT_EQ(result, 42); |
| 693 | } |
| 694 | |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 695 | TEST_F(JniCompilerTest, GetSinkPropertiesNative) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 696 | TEST_DISABLED_FOR_PORTABLE(); |
Brian Carlstrom | fc7120c | 2012-08-27 13:43:25 -0700 | [diff] [blame] | 697 | SetUpForTest(false, "getSinkPropertiesNative", "(Ljava/lang/String;)[Ljava/lang/Object;", NULL); |
| 698 | // This space intentionally left blank. Just testing compilation succeeds. |
| 699 | } |
| 700 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 701 | // This should return jclass, but we're imitating a bug pattern. |
| 702 | jobject Java_MyClassNatives_instanceMethodThatShouldReturnClass(JNIEnv* env, jobject) { |
| 703 | return env->NewStringUTF("not a class!"); |
| 704 | } |
| 705 | |
| 706 | // This should return jclass, but we're imitating a bug pattern. |
| 707 | jobject Java_MyClassNatives_staticMethodThatShouldReturnClass(JNIEnv* env, jclass) { |
| 708 | return env->NewStringUTF("not a class!"); |
| 709 | } |
| 710 | |
| 711 | TEST_F(JniCompilerTest, UpcallReturnTypeChecking_Instance) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 712 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 713 | SetUpForTest(false, "instanceMethodThatShouldReturnClass", "()Ljava/lang/Class;", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 714 | reinterpret_cast<void*>(&Java_MyClassNatives_instanceMethodThatShouldReturnClass)); |
| 715 | |
| 716 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 717 | // TODO: check type of returns with portable JNI compiler. |
| 718 | // This native method is bad, and tries to return a jstring as a jclass. |
| 719 | env_->CallObjectMethod(jobj_, jmethod_); |
| 720 | check_jni_abort_catcher.Check("attempt to return an instance of java.lang.String from java.lang.Class MyClassNatives.instanceMethodThatShouldReturnClass()"); |
| 721 | |
| 722 | // Here, we just call the method incorrectly; we should catch that too. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 723 | env_->CallVoidMethod(jobj_, jmethod_); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 724 | check_jni_abort_catcher.Check("attempt to return an instance of java.lang.String from java.lang.Class MyClassNatives.instanceMethodThatShouldReturnClass()"); |
| 725 | env_->CallStaticVoidMethod(jklass_, jmethod_); |
| 726 | check_jni_abort_catcher.Check("calling non-static method java.lang.Class MyClassNatives.instanceMethodThatShouldReturnClass() with CallStaticVoidMethodV"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | TEST_F(JniCompilerTest, UpcallReturnTypeChecking_Static) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 730 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 731 | SetUpForTest(true, "staticMethodThatShouldReturnClass", "()Ljava/lang/Class;", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 732 | reinterpret_cast<void*>(&Java_MyClassNatives_staticMethodThatShouldReturnClass)); |
| 733 | |
| 734 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 735 | // TODO: check type of returns with portable JNI compiler. |
| 736 | // This native method is bad, and tries to return a jstring as a jclass. |
| 737 | env_->CallStaticObjectMethod(jklass_, jmethod_); |
| 738 | check_jni_abort_catcher.Check("attempt to return an instance of java.lang.String from java.lang.Class MyClassNatives.staticMethodThatShouldReturnClass()"); |
| 739 | |
| 740 | // Here, we just call the method incorrectly; we should catch that too. |
| 741 | env_->CallStaticVoidMethod(jklass_, jmethod_); |
| 742 | check_jni_abort_catcher.Check("attempt to return an instance of java.lang.String from java.lang.Class MyClassNatives.staticMethodThatShouldReturnClass()"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 743 | env_->CallVoidMethod(jobj_, jmethod_); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 744 | check_jni_abort_catcher.Check("calling static method java.lang.Class MyClassNatives.staticMethodThatShouldReturnClass() with CallVoidMethodV"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // This should take jclass, but we're imitating a bug pattern. |
| 748 | void Java_MyClassNatives_instanceMethodThatShouldTakeClass(JNIEnv*, jobject, jclass) { |
| 749 | } |
| 750 | |
| 751 | // This should take jclass, but we're imitating a bug pattern. |
| 752 | void Java_MyClassNatives_staticMethodThatShouldTakeClass(JNIEnv*, jclass, jclass) { |
| 753 | } |
| 754 | |
| 755 | TEST_F(JniCompilerTest, UpcallArgumentTypeChecking_Instance) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 756 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 757 | SetUpForTest(false, "instanceMethodThatShouldTakeClass", "(ILjava/lang/Class;)V", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 758 | reinterpret_cast<void*>(&Java_MyClassNatives_instanceMethodThatShouldTakeClass)); |
| 759 | |
| 760 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 761 | // We deliberately pass a bad second argument here. |
| 762 | env_->CallVoidMethod(jobj_, jmethod_, 123, env_->NewStringUTF("not a class!")); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 763 | check_jni_abort_catcher.Check("bad arguments passed to void MyClassNatives.instanceMethodThatShouldTakeClass(int, java.lang.Class)"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | TEST_F(JniCompilerTest, UpcallArgumentTypeChecking_Static) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 767 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 768 | SetUpForTest(true, "staticMethodThatShouldTakeClass", "(ILjava/lang/Class;)V", |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 769 | reinterpret_cast<void*>(&Java_MyClassNatives_staticMethodThatShouldTakeClass)); |
| 770 | |
| 771 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 772 | // We deliberately pass a bad second argument here. |
| 773 | env_->CallStaticVoidMethod(jklass_, jmethod_, 123, env_->NewStringUTF("not a class!")); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 774 | check_jni_abort_catcher.Check("bad arguments passed to void MyClassNatives.staticMethodThatShouldTakeClass(int, java.lang.Class)"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 777 | } // namespace art |