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 | */ |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 17 | #include "compiler/driver/compiler_driver.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include "UniquePtr.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 23 | #include "class_linker.h" |
| 24 | #include "common_test.h" |
| 25 | #include "dex_file.h" |
| 26 | #include "heap.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/class.h" |
| 28 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame^] | 29 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/abstract_method-inl.h" |
| 31 | #include "mirror/object_array-inl.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 36 | class CompilerDriverTest : public CommonTest { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 37 | protected: |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 38 | void CompileAll(jobject class_loader) LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 39 | compiler_driver_->CompileAll(class_loader, Runtime::Current()->GetCompileTimeClassPath(class_loader)); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 40 | MakeAllExecutable(class_loader); |
| 41 | } |
| 42 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | void EnsureCompiled(jobject class_loader, const char* class_name, const char* method, |
| 44 | const char* signature, bool is_virtual) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 45 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 46 | CompileAll(class_loader); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 48 | bool started = runtime_->Start(); |
| 49 | CHECK(started); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 50 | env_ = Thread::Current()->GetJniEnv(); |
| 51 | class_ = env_->FindClass(class_name); |
| 52 | CHECK(class_ != NULL) << "Class not found: " << class_name; |
Shih-wei Liao | 303b01e | 2011-09-14 00:46:13 -0700 | [diff] [blame] | 53 | if (is_virtual) { |
| 54 | mid_ = env_->GetMethodID(class_, method, signature); |
| 55 | } else { |
| 56 | mid_ = env_->GetStaticMethodID(class_, method, signature); |
| 57 | } |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 58 | CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature; |
| 59 | } |
| 60 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 61 | void MakeAllExecutable(jobject class_loader) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 62 | const std::vector<const DexFile*>& class_path |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 63 | = Runtime::Current()->GetCompileTimeClassPath(class_loader); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 64 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 65 | const DexFile* dex_file = class_path[i]; |
| 66 | CHECK(dex_file != NULL); |
| 67 | MakeDexFileExecutable(class_loader, *dex_file); |
| 68 | } |
| 69 | } |
| 70 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) { |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 72 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 73 | for (size_t i = 0; i < dex_file.NumClassDefs(); i++) { |
| 74 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(i); |
| 75 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 76 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | mirror::Class* c = class_linker->FindClass(descriptor, soa.Decode<mirror::ClassLoader*>(class_loader)); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 78 | CHECK(c != NULL); |
| 79 | for (size_t i = 0; i < c->NumDirectMethods(); i++) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 80 | MakeExecutable(c->GetDirectMethod(i)); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 81 | } |
| 82 | for (size_t i = 0; i < c->NumVirtualMethods(); i++) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 83 | MakeExecutable(c->GetVirtualMethod(i)); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 88 | JNIEnv* env_; |
| 89 | jclass class_; |
| 90 | jmethodID mid_; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 93 | // Disabled due to 10 second runtime on host |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 94 | TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) { |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 95 | CompileAll(NULL); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 96 | |
| 97 | // All libcore references should resolve |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 99 | const DexFile* dex = java_lang_dex_file_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 101 | EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings()); |
| 102 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | const mirror::String* string = dex_cache->GetResolvedString(i); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 104 | EXPECT_TRUE(string != NULL) << "string_idx=" << i; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 105 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 106 | EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes()); |
| 107 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 108 | mirror::Class* type = dex_cache->GetResolvedType(i); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(type != NULL) << "type_idx=" << i |
| 110 | << " " << dex->GetTypeDescriptor(dex->GetTypeId(i)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 111 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 112 | EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods()); |
| 113 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | mirror::AbstractMethod* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 115 | EXPECT_TRUE(method != NULL) << "method_idx=" << i |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 116 | << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i)) |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 117 | << " " << dex->GetMethodName(dex->GetMethodId(i)); |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 118 | EXPECT_TRUE(method->GetEntryPointFromCompiledCode() != NULL) << "method_idx=" << i |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 119 | << " " |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 120 | << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i)) |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 121 | << " " << dex->GetMethodName(dex->GetMethodId(i)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 122 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 123 | EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields()); |
| 124 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | mirror::Field* field = dex_cache->GetResolvedField(i); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 126 | EXPECT_TRUE(field != NULL) << "field_idx=" << i |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 127 | << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i)) |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 128 | << " " << dex->GetFieldName(dex->GetFieldId(i)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 131 | // TODO check Class::IsVerified for all classes |
| 132 | |
| 133 | // TODO: check that all Method::GetCode() values are non-null |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 136 | TEST_F(CompilerDriverTest, AbstractMethodErrorStub) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 137 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 138 | jobject class_loader; |
| 139 | { |
| 140 | ScopedObjectAccess soa(Thread::Current()); |
| 141 | CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z"); |
| 142 | CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V"); |
| 143 | class_loader = LoadDex("AbstractMethod"); |
| 144 | } |
| 145 | ASSERT_TRUE(class_loader != NULL); |
| 146 | EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true); |
Shih-wei Liao | 303b01e | 2011-09-14 00:46:13 -0700 | [diff] [blame] | 147 | |
Ian Rogers | a0841a8 | 2011-09-22 14:16:31 -0700 | [diff] [blame] | 148 | // Create a jobj_ of ConcreteClass, NOT AbstractClass. |
| 149 | jclass c_class = env_->FindClass("ConcreteClass"); |
| 150 | jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V"); |
| 151 | jobject jobj_ = env_->NewObject(c_class, constructor); |
Shih-wei Liao | 303b01e | 2011-09-14 00:46:13 -0700 | [diff] [blame] | 152 | ASSERT_TRUE(jobj_ != NULL); |
| 153 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 154 | // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception. |
Shih-wei Liao | 303b01e | 2011-09-14 00:46:13 -0700 | [diff] [blame] | 155 | env_->CallNonvirtualVoidMethod(jobj_, class_, mid_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 156 | EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE); |
| 157 | jthrowable exception = env_->ExceptionOccurred(); |
| 158 | env_->ExceptionClear(); |
| 159 | jclass jlame = env_->FindClass("java/lang/AbstractMethodError"); |
| 160 | EXPECT_TRUE(env_->IsInstanceOf(exception, jlame)); |
Ian Rogers | a0841a8 | 2011-09-22 14:16:31 -0700 | [diff] [blame] | 161 | Thread::Current()->ClearException(); |
Shih-wei Liao | 303b01e | 2011-09-14 00:46:13 -0700 | [diff] [blame] | 162 | } |
| 163 | |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 164 | // TODO: need check-cast test (when stub complete & we can throw/catch |
| 165 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 166 | } // namespace art |