blob: d1b377a49a700041451797092e2766d2570bb639 [file] [log] [blame]
buzbeec143c552011-08-20 17:38:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Elliott Hughes90a33692011-08-30 13:27:07 -07003#include "compiler.h"
4
5#include <stdint.h>
6#include <stdio.h>
7
8#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -07009#include "class_linker.h"
10#include "common_test.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070011#include "dex_cache.h"
buzbeec143c552011-08-20 17:38:58 -070012#include "dex_file.h"
13#include "heap.h"
14#include "object.h"
buzbeec143c552011-08-20 17:38:58 -070015
16namespace art {
17
18class CompilerTest : public CommonTest {
Brian Carlstrombffb1552011-08-25 12:23:53 -070019 protected:
Brian Carlstrom8a487412011-08-29 20:08:52 -070020
Elliott Hughes1240dad2011-09-09 16:24:50 -070021 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 Liao303b01e2011-09-14 00:46:13 -070027 const char* class_name, const char* method, const char* signature, bool is_virtual) {
Elliott Hughes1240dad2011-09-09 16:24:50 -070028 CompileAll(class_loader);
Ian Rogersa0841a82011-09-22 14:16:31 -070029 runtime_->Start();
Elliott Hughes1240dad2011-09-09 16:24:50 -070030 env_ = Thread::Current()->GetJniEnv();
31 class_ = env_->FindClass(class_name);
32 CHECK(class_ != NULL) << "Class not found: " << class_name;
Shih-wei Liao303b01e2011-09-14 00:46:13 -070033 if (is_virtual) {
34 mid_ = env_->GetMethodID(class_, method, signature);
35 } else {
36 mid_ = env_->GetStaticMethodID(class_, method, signature);
37 }
Elliott Hughes1240dad2011-09-09 16:24:50 -070038 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
39 }
40
41 void MakeAllExecutable(const ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070042 const std::vector<const DexFile*>& class_path
43 = ClassLoader::GetCompileTimeClassPath(class_loader);
Elliott Hughes1240dad2011-09-09 16:24:50 -070044 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 Carlstrom3320cf42011-10-04 14:58:28 -070059 MakeExecutable(c->GetDirectMethod(i));
Elliott Hughes1240dad2011-09-09 16:24:50 -070060 }
61 for (size_t i = 0; i < c->NumVirtualMethods(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070062 MakeExecutable(c->GetVirtualMethod(i));
Elliott Hughes1240dad2011-09-09 16:24:50 -070063 }
64 }
65 }
66
Elliott Hughes1240dad2011-09-09 16:24:50 -070067 JNIEnv* env_;
68 jclass class_;
69 jmethodID mid_;
buzbeec143c552011-08-20 17:38:58 -070070};
71
Brian Carlstrom7540ff42011-09-04 16:38:46 -070072// Disabled due to 10 second runtime on host
Elliott Hughes1240dad2011-09-09 16:24:50 -070073TEST_F(CompilerTest, DISABLED_LARGE_CompileDexLibCore) {
74 CompileAll(NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070075
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 Hughescf4c6c42011-09-01 15:16:42 -070081 const String* string = dex_cache->GetResolvedString(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -070082 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070083 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070084 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
85 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070086 Class* type = dex_cache->GetResolvedType(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -070087 EXPECT_TRUE(type != NULL) << "type_idx=" << i
88 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070089 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070090 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
91 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070092 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -070093 EXPECT_TRUE(method != NULL) << "method_idx=" << i
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070094 << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
Brian Carlstrom7540ff42011-09-04 16:38:46 -070095 << " " << dex->GetMethodName(dex->GetMethodId(i));
Shih-wei Liaoc486c112011-09-13 16:43:52 -070096 EXPECT_TRUE(method->GetCode() != NULL) << "method_idx=" << i
97 << " "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070098 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
Shih-wei Liaoc486c112011-09-13 16:43:52 -070099 << " " << dex->GetMethodName(dex->GetMethodId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700100 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700101 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
102 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700103 Field* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700104 EXPECT_TRUE(field != NULL) << "field_idx=" << i
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700105 << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i))
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700106 << " " << dex->GetFieldName(dex->GetFieldId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700107 }
108
Brian Carlstrom83db7722011-08-26 17:32:56 -0700109 // TODO check Class::IsVerified for all classes
110
111 // TODO: check that all Method::GetCode() values are non-null
112
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700113 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 Carlstrom83db7722011-08-26 17:32:56 -0700116 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700117 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 Carlstrom83db7722011-08-26 17:32:56 -0700124 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700125}
126
Ian Rogersa0841a82011-09-22 14:16:31 -0700127TEST_F(CompilerTest, AbstractMethodErrorStub) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700128 CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V");
129
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700130 SirtRef<ClassLoader> class_loader(LoadDex("AbstractMethod"));
131 ASSERT_TRUE(class_loader.get() != NULL);
132 EnsureCompiled(class_loader.get(), "AbstractClass", "foo", "()V", true);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700133
Ian Rogersa0841a82011-09-22 14:16:31 -0700134 // 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 Liao303b01e2011-09-14 00:46:13 -0700138 ASSERT_TRUE(jobj_ != NULL);
139
140#if defined(__arm__)
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700141 Class* jlame = class_linker_->FindClass("Ljava/lang/AbstractMethodError;", class_loader.get());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700143 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Ian Rogersa0841a82011-09-22 14:16:31 -0700144 EXPECT_TRUE(Thread::Current()->IsExceptionPending());
145 EXPECT_TRUE(Thread::Current()->GetException()->InstanceOf(jlame));
146 Thread::Current()->ClearException();
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700147#endif // __arm__
148}
149
buzbee2a475e72011-09-07 17:19:17 -0700150// TODO: need check-cast test (when stub complete & we can throw/catch
151
buzbeec143c552011-08-20 17:38:58 -0700152} // namespace art