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