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