blob: 088726fff501e1c8a75d5538794b5d7d044f864d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
buzbeec143c552011-08-20 17:38:58 -070016
Elliott Hughes90a33692011-08-30 13:27:07 -070017#include "compiler.h"
18
19#include <stdint.h>
20#include <stdio.h>
21
22#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -070023#include "class_linker.h"
24#include "common_test.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070025#include "dex_cache.h"
buzbeec143c552011-08-20 17:38:58 -070026#include "dex_file.h"
27#include "heap.h"
28#include "object.h"
buzbeec143c552011-08-20 17:38:58 -070029
30namespace art {
31
32class CompilerTest : public CommonTest {
Brian Carlstrombffb1552011-08-25 12:23:53 -070033 protected:
Ian Rogers365c1022012-06-22 15:05:28 -070034 void CompileAll(ClassLoader* class_loader) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080035 compiler_->CompileAll(class_loader, Runtime::Current()->GetCompileTimeClassPath(class_loader));
Elliott Hughes1240dad2011-09-09 16:24:50 -070036 MakeAllExecutable(class_loader);
37 }
38
Ian Rogers365c1022012-06-22 15:05:28 -070039 void EnsureCompiled(ClassLoader* class_loader, const char* class_name, const char* method,
40 const char* signature, bool is_virtual) {
Elliott Hughes1240dad2011-09-09 16:24:50 -070041 CompileAll(class_loader);
Ian Rogersa0841a82011-09-22 14:16:31 -070042 runtime_->Start();
Elliott Hughes1240dad2011-09-09 16:24:50 -070043 env_ = Thread::Current()->GetJniEnv();
44 class_ = env_->FindClass(class_name);
45 CHECK(class_ != NULL) << "Class not found: " << class_name;
Shih-wei Liao303b01e2011-09-14 00:46:13 -070046 if (is_virtual) {
47 mid_ = env_->GetMethodID(class_, method, signature);
48 } else {
49 mid_ = env_->GetStaticMethodID(class_, method, signature);
50 }
Elliott Hughes1240dad2011-09-09 16:24:50 -070051 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
52 }
53
Ian Rogers365c1022012-06-22 15:05:28 -070054 void MakeAllExecutable(ClassLoader* class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070055 const std::vector<const DexFile*>& class_path
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080056 = Runtime::Current()->GetCompileTimeClassPath(class_loader);
Elliott Hughes1240dad2011-09-09 16:24:50 -070057 for (size_t i = 0; i != class_path.size(); ++i) {
58 const DexFile* dex_file = class_path[i];
59 CHECK(dex_file != NULL);
60 MakeDexFileExecutable(class_loader, *dex_file);
61 }
62 }
63
Ian Rogers365c1022012-06-22 15:05:28 -070064 void MakeDexFileExecutable(ClassLoader* class_loader, const DexFile& dex_file) {
Elliott Hughes1240dad2011-09-09 16:24:50 -070065 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
66 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
67 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
68 const char* descriptor = dex_file.GetClassDescriptor(class_def);
69 Class* c = class_linker->FindClass(descriptor, class_loader);
70 CHECK(c != NULL);
71 for (size_t i = 0; i < c->NumDirectMethods(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070072 MakeExecutable(c->GetDirectMethod(i));
Elliott Hughes1240dad2011-09-09 16:24:50 -070073 }
74 for (size_t i = 0; i < c->NumVirtualMethods(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075 MakeExecutable(c->GetVirtualMethod(i));
Elliott Hughes1240dad2011-09-09 16:24:50 -070076 }
77 }
78 }
79
Elliott Hughes1240dad2011-09-09 16:24:50 -070080 JNIEnv* env_;
81 jclass class_;
82 jmethodID mid_;
buzbeec143c552011-08-20 17:38:58 -070083};
84
Brian Carlstrom7540ff42011-09-04 16:38:46 -070085// Disabled due to 10 second runtime on host
Elliott Hughes1240dad2011-09-09 16:24:50 -070086TEST_F(CompilerTest, DISABLED_LARGE_CompileDexLibCore) {
87 CompileAll(NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070088
89 // All libcore references should resolve
Brian Carlstroma004aa92012-02-08 18:05:09 -080090 const DexFile* dex = java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
92 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
93 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094 const String* string = dex_cache->GetResolvedString(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -070095 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070096 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070097 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
98 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070099 Class* type = dex_cache->GetResolvedType(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700100 EXPECT_TRUE(type != NULL) << "type_idx=" << i
101 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700102 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700103 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
104 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700105 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700106 EXPECT_TRUE(method != NULL) << "method_idx=" << i
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700107 << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700108 << " " << dex->GetMethodName(dex->GetMethodId(i));
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700109 EXPECT_TRUE(method->GetCode() != NULL) << "method_idx=" << i
110 << " "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700111 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700112 << " " << dex->GetMethodName(dex->GetMethodId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700113 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700114 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
115 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700116 Field* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700117 EXPECT_TRUE(field != NULL) << "field_idx=" << i
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700118 << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i))
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700119 << " " << dex->GetFieldName(dex->GetFieldId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700120 }
121
Brian Carlstrom83db7722011-08-26 17:32:56 -0700122 // TODO check Class::IsVerified for all classes
123
124 // TODO: check that all Method::GetCode() values are non-null
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700125}
126
Ian Rogersa0841a82011-09-22 14:16:31 -0700127TEST_F(CompilerTest, AbstractMethodErrorStub) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800128 CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z");
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700129 CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V");
130
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700131 SirtRef<ClassLoader> class_loader(LoadDex("AbstractMethod"));
132 ASSERT_TRUE(class_loader.get() != NULL);
133 EnsureCompiled(class_loader.get(), "AbstractClass", "foo", "()V", true);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700134
Ian Rogersa0841a82011-09-22 14:16:31 -0700135 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
136 jclass c_class = env_->FindClass("ConcreteClass");
137 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
138 jobject jobj_ = env_->NewObject(c_class, constructor);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700139 ASSERT_TRUE(jobj_ != NULL);
140
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}
148
buzbee2a475e72011-09-07 17:19:17 -0700149// TODO: need check-cast test (when stub complete & we can throw/catch
150
buzbeec143c552011-08-20 17:38:58 -0700151} // namespace art