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