blob: f5135115b09d77f9a611d8e52aef0fc991df32a9 [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 Rogersb726dcb2012-09-05 08:57:23 -070034 void CompileAll(jobject class_loader) LOCKS_EXCLUDED(Locks::mutator_lock_) {
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 Rogers00f7d0e2012-07-19 15:28:27 -070039 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
40 const char* signature, bool is_virtual)
Ian Rogersb726dcb2012-09-05 08:57:23 -070041 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Elliott Hughes1240dad2011-09-09 16:24:50 -070042 CompileAll(class_loader);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 Thread::Current()->TransitionFromSuspendedToRunnable();
Ian Rogersa0841a82011-09-22 14:16:31 -070044 runtime_->Start();
Elliott Hughes1240dad2011-09-09 16:24:50 -070045 env_ = Thread::Current()->GetJniEnv();
46 class_ = env_->FindClass(class_name);
47 CHECK(class_ != NULL) << "Class not found: " << class_name;
Shih-wei Liao303b01e2011-09-14 00:46:13 -070048 if (is_virtual) {
49 mid_ = env_->GetMethodID(class_, method, signature);
50 } else {
51 mid_ = env_->GetStaticMethodID(class_, method, signature);
52 }
Elliott Hughes1240dad2011-09-09 16:24:50 -070053 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
54 }
55
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056 void MakeAllExecutable(jobject class_loader) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070057 const std::vector<const DexFile*>& class_path
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080058 = Runtime::Current()->GetCompileTimeClassPath(class_loader);
Elliott Hughes1240dad2011-09-09 16:24:50 -070059 for (size_t i = 0; i != class_path.size(); ++i) {
60 const DexFile* dex_file = class_path[i];
61 CHECK(dex_file != NULL);
62 MakeDexFileExecutable(class_loader, *dex_file);
63 }
64 }
65
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) {
Elliott Hughes1240dad2011-09-09 16:24:50 -070067 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
68 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
69 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
70 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 ScopedObjectAccess soa(Thread::Current());
72 Class* c = class_linker->FindClass(descriptor, soa.Decode<ClassLoader*>(class_loader));
Elliott Hughes1240dad2011-09-09 16:24:50 -070073 CHECK(c != NULL);
74 for (size_t i = 0; i < c->NumDirectMethods(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075 MakeExecutable(c->GetDirectMethod(i));
Elliott Hughes1240dad2011-09-09 16:24:50 -070076 }
77 for (size_t i = 0; i < c->NumVirtualMethods(); i++) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070078 MakeExecutable(c->GetVirtualMethod(i));
Elliott Hughes1240dad2011-09-09 16:24:50 -070079 }
80 }
81 }
82
Elliott Hughes1240dad2011-09-09 16:24:50 -070083 JNIEnv* env_;
84 jclass class_;
85 jmethodID mid_;
buzbeec143c552011-08-20 17:38:58 -070086};
87
Brian Carlstrom7540ff42011-09-04 16:38:46 -070088// Disabled due to 10 second runtime on host
Elliott Hughes1240dad2011-09-09 16:24:50 -070089TEST_F(CompilerTest, DISABLED_LARGE_CompileDexLibCore) {
90 CompileAll(NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091
92 // All libcore references should resolve
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 ScopedObjectAccess soa(Thread::Current());
Brian Carlstroma004aa92012-02-08 18:05:09 -080094 const DexFile* dex = java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070095 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
96 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
97 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070098 const String* string = dex_cache->GetResolvedString(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -070099 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700100 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700101 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
102 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 Class* type = dex_cache->GetResolvedType(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700104 EXPECT_TRUE(type != NULL) << "type_idx=" << i
105 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700106 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700107 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
108 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700109 AbstractMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700110 EXPECT_TRUE(method != NULL) << "method_idx=" << i
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700111 << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700112 << " " << dex->GetMethodName(dex->GetMethodId(i));
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700113 EXPECT_TRUE(method->GetCode() != NULL) << "method_idx=" << i
114 << " "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700115 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
Shih-wei Liaoc486c112011-09-13 16:43:52 -0700116 << " " << dex->GetMethodName(dex->GetMethodId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700117 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700118 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
119 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700120 Field* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700121 EXPECT_TRUE(field != NULL) << "field_idx=" << i
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700122 << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i))
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700123 << " " << dex->GetFieldName(dex->GetFieldId(i));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700124 }
125
Brian Carlstrom83db7722011-08-26 17:32:56 -0700126 // TODO check Class::IsVerified for all classes
127
128 // TODO: check that all Method::GetCode() values are non-null
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700129}
130
Ian Rogersa0841a82011-09-22 14:16:31 -0700131TEST_F(CompilerTest, AbstractMethodErrorStub) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 jobject class_loader;
133 {
134 ScopedObjectAccess soa(Thread::Current());
135 CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z");
136 CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V");
137 class_loader = LoadDex("AbstractMethod");
138 }
139 ASSERT_TRUE(class_loader != NULL);
140 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700141
Ian Rogersa0841a82011-09-22 14:16:31 -0700142 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
143 jclass c_class = env_->FindClass("ConcreteClass");
144 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
145 jobject jobj_ = env_->NewObject(c_class, constructor);
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700146 ASSERT_TRUE(jobj_ != NULL);
147
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700149 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
151 jthrowable exception = env_->ExceptionOccurred();
152 env_->ExceptionClear();
153 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
154 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
Ian Rogersa0841a82011-09-22 14:16:31 -0700155 Thread::Current()->ClearException();
Shih-wei Liao303b01e2011-09-14 00:46:13 -0700156}
157
buzbee2a475e72011-09-07 17:19:17 -0700158// TODO: need check-cast test (when stub complete & we can throw/catch
159
buzbeec143c552011-08-20 17:38:58 -0700160} // namespace art