blob: e951ade08bfc6f6b0f44088003fd73cf68875711 [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 Hughes7957d542011-09-09 17:16:01 -070021 void AssertStaticIntMethod(jint expected, const ClassLoader* class_loader,
Elliott Hughes1240dad2011-09-09 16:24:50 -070022 const char* class_name, const char* method, const char* signature,
Elliott Hughes7957d542011-09-09 17:16:01 -070023 ...) {
Shih-wei Liao303b01e2011-09-14 00:46:13 -070024 EnsureCompiled(class_loader, class_name, method, signature, false);
Elliott Hughesd3a72972011-09-07 15:31:59 -070025#if defined(__arm__)
Brian Carlstrombffb1552011-08-25 12:23:53 -070026 va_list args;
Elliott Hughes7957d542011-09-09 17:16:01 -070027 va_start(args, signature);
Elliott Hughes1240dad2011-09-09 16:24:50 -070028 jint result = env_->CallStaticIntMethodV(class_, mid_, args);
Brian Carlstrombffb1552011-08-25 12:23:53 -070029 va_end(args);
Elliott Hughes1240dad2011-09-09 16:24:50 -070030 LOG(INFO) << class_name << "." << method << "(...) result is " << result;
Brian Carlstrombffb1552011-08-25 12:23:53 -070031 EXPECT_EQ(expected, result);
32#endif // __arm__
33 }
Elliott Hughes1240dad2011-09-09 16:24:50 -070034
Elliott Hughes7957d542011-09-09 17:16:01 -070035 void AssertStaticLongMethod(jlong expected, const ClassLoader* class_loader,
Elliott Hughes1240dad2011-09-09 16:24:50 -070036 const char* class_name, const char* method, const char* signature,
Elliott Hughes7957d542011-09-09 17:16:01 -070037 ...) {
Shih-wei Liao303b01e2011-09-14 00:46:13 -070038 EnsureCompiled(class_loader, class_name, method, signature, false);
Elliott Hughesd3a72972011-09-07 15:31:59 -070039#if defined(__arm__)
buzbeebafc3422011-08-25 15:22:55 -070040 va_list args;
Elliott Hughes7957d542011-09-09 17:16:01 -070041 va_start(args, signature);
Elliott Hughes1240dad2011-09-09 16:24:50 -070042 jlong result = env_->CallStaticLongMethodV(class_, mid_, args);
buzbeebafc3422011-08-25 15:22:55 -070043 va_end(args);
Elliott Hughes1240dad2011-09-09 16:24:50 -070044 LOG(INFO) << class_name << "." << method << "(...) result is " << result;
buzbeebafc3422011-08-25 15:22:55 -070045 EXPECT_EQ(expected, result);
46#endif // __arm__
47 }
Elliott Hughes1240dad2011-09-09 16:24:50 -070048
49 void CompileAll(const ClassLoader* class_loader) {
50 compiler_->CompileAll(class_loader);
51 MakeAllExecutable(class_loader);
52 }
53
54 void EnsureCompiled(const ClassLoader* class_loader,
Shih-wei Liao303b01e2011-09-14 00:46:13 -070055 const char* class_name, const char* method, const char* signature, bool is_virtual) {
Elliott Hughes1240dad2011-09-09 16:24:50 -070056 CompileAll(class_loader);
Ian Rogersa0841a82011-09-22 14:16:31 -070057 runtime_->Start();
Elliott Hughes1240dad2011-09-09 16:24:50 -070058 env_ = Thread::Current()->GetJniEnv();
59 class_ = env_->FindClass(class_name);
60 CHECK(class_ != NULL) << "Class not found: " << class_name;
Shih-wei Liao303b01e2011-09-14 00:46:13 -070061 if (is_virtual) {
62 mid_ = env_->GetMethodID(class_, method, signature);
63 } else {
64 mid_ = env_->GetStaticMethodID(class_, method, signature);
65 }
Elliott Hughes1240dad2011-09-09 16:24:50 -070066 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
67 }
68
69 void MakeAllExecutable(const ClassLoader* class_loader) {
70 const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
71 for (size_t i = 0; i != class_path.size(); ++i) {
72 const DexFile* dex_file = class_path[i];
73 CHECK(dex_file != NULL);
74 MakeDexFileExecutable(class_loader, *dex_file);
75 }
76 }
77
78 void MakeDexFileExecutable(const ClassLoader* class_loader, const DexFile& dex_file) {
79 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
80 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
81 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
82 const char* descriptor = dex_file.GetClassDescriptor(class_def);
83 Class* c = class_linker->FindClass(descriptor, class_loader);
84 CHECK(c != NULL);
85 for (size_t i = 0; i < c->NumDirectMethods(); i++) {
86 MakeMethodExecutable(c->GetDirectMethod(i));
87 }
88 for (size_t i = 0; i < c->NumVirtualMethods(); i++) {
89 MakeMethodExecutable(c->GetVirtualMethod(i));
90 }
91 }
92 }
93
94 void MakeMethodExecutable(Method* m) {
95 if (m->GetCodeArray() != NULL) {
96 MakeExecutable(m->GetCodeArray());
97 } else {
98 LOG(WARNING) << "no code for " << PrettyMethod(m);
99 }
100 if (m->GetInvokeStubArray() != NULL) {
101 MakeExecutable(m->GetInvokeStubArray());
102 } else {
103 LOG(WARNING) << "no invoke stub for " << PrettyMethod(m);
104 }
105 }
106
107 JNIEnv* env_;
108 jclass class_;
109 jmethodID mid_;
buzbeec143c552011-08-20 17:38:58 -0700110};
111
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700112// Disabled due to 10 second runtime on host
Elliott Hughes1240dad2011-09-09 16:24:50 -0700113TEST_F(CompilerTest, DISABLED_LARGE_CompileDexLibCore) {
114 CompileAll(NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700115
116 // All libcore references should resolve
117 const DexFile* dex = java_lang_dex_file_.get();
118 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
119 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
120 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700121 const String* string = dex_cache->GetResolvedString(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700122 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700123 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700124 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
125 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700126 Class* type = dex_cache->GetResolvedType(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700127 EXPECT_TRUE(type != NULL) << "type_idx=" << i
128 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700129 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700130 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
131 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700132 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700133 EXPECT_TRUE(method != NULL) << "method_idx=" << i
134 << " " << dex->GetMethodClassDescriptor(dex->GetMethodId(i))
135 << " " << dex->GetMethodName(dex->GetMethodId(i));
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700136 EXPECT_TRUE(method->GetCode() != NULL) << "method_idx=" << i
137 << " "
138 << dex->GetMethodClassDescriptor(dex->GetMethodId(i))
139 << " " << dex->GetMethodName(dex->GetMethodId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700141 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
142 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700143 Field* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700144 EXPECT_TRUE(field != NULL) << "field_idx=" << i
145 << " " << dex->GetFieldClassDescriptor(dex->GetFieldId(i))
146 << " " << dex->GetFieldName(dex->GetFieldId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700147 }
148
Brian Carlstrom83db7722011-08-26 17:32:56 -0700149 // TODO check Class::IsVerified for all classes
150
151 // TODO: check that all Method::GetCode() values are non-null
152
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700153 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumCodeAndDirectMethods());
154 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
155 for (size_t i = 0; i < dex_cache->NumCodeAndDirectMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700156 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700157 if (method->IsDirect()) {
158 EXPECT_EQ(method->GetCode(), code_and_direct_methods->GetResolvedCode(i));
159 EXPECT_EQ(method, code_and_direct_methods->GetResolvedMethod(i));
160 } else {
161 EXPECT_EQ(0U, code_and_direct_methods->GetResolvedCode(i));
162 EXPECT_TRUE(code_and_direct_methods->GetResolvedMethod(i) == NULL);
163 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700164 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700165}
166
Ian Rogersa0841a82011-09-22 14:16:31 -0700167TEST_F(CompilerTest, AbstractMethodErrorStub) {
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700168 const ClassLoader* class_loader = LoadDex("AbstractMethod");
Ian Rogersa0841a82011-09-22 14:16:31 -0700169 ASSERT_TRUE(class_loader != NULL);
170 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700171
Ian Rogersa0841a82011-09-22 14:16:31 -0700172 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
173 jclass c_class = env_->FindClass("ConcreteClass");
174 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
175 jobject jobj_ = env_->NewObject(c_class, constructor);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700176 ASSERT_TRUE(jobj_ != NULL);
177
178#if defined(__arm__)
Ian Rogersa0841a82011-09-22 14:16:31 -0700179 Class* jlame = class_linker_->FindClass("Ljava/lang/AbstractMethodError;", class_loader);
180 // Force non-virtal call to AbstractClass foo, will throw AbstractMethodError exception.
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700181 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Ian Rogersa0841a82011-09-22 14:16:31 -0700182 EXPECT_TRUE(Thread::Current()->IsExceptionPending());
183 EXPECT_TRUE(Thread::Current()->GetException()->InstanceOf(jlame));
184 Thread::Current()->ClearException();
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700185#endif // __arm__
186}
187
buzbee2a475e72011-09-07 17:19:17 -0700188// TODO: need check-cast test (when stub complete & we can throw/catch
189
buzbeec143c552011-08-20 17:38:58 -0700190} // namespace art