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