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