blob: 34806ce29379820bac64853911d5a3ef2a33c701 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 */
16
17#include "driver/compiler_driver.h"
18
19#include <stdint.h>
20#include <stdio.h>
21
22#include "UniquePtr.h"
23#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080024#include "common_compiler_test.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "dex_file.h"
26#include "gc/heap.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "mirror/class.h"
29#include "mirror/class-inl.h"
30#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "mirror/object_array-inl.h"
32#include "mirror/object-inl.h"
33
34namespace art {
35
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080036class CompilerDriverTest : public CommonCompilerTest {
Brian Carlstrom7940e442013-07-12 13:46:57 -070037 protected:
38 void CompileAll(jobject class_loader) LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers5fe9af72013-11-14 00:17:20 -080039 TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
Ian Rogersbd2c19f2013-07-25 17:43:46 -070040 timings.StartSplit("CompileAll");
Brian Carlstrom45602482013-07-21 22:07:55 -070041 compiler_driver_->CompileAll(class_loader,
42 Runtime::Current()->GetCompileTimeClassPath(class_loader),
43 timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 MakeAllExecutable(class_loader);
Ian Rogers5fe9af72013-11-14 00:17:20 -080045 timings.EndSplit();
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 }
47
48 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
49 const char* signature, bool is_virtual)
50 LOCKS_EXCLUDED(Locks::mutator_lock_) {
51 CompileAll(class_loader);
52 Thread::Current()->TransitionFromSuspendedToRunnable();
53 bool started = runtime_->Start();
54 CHECK(started);
55 env_ = Thread::Current()->GetJniEnv();
56 class_ = env_->FindClass(class_name);
57 CHECK(class_ != NULL) << "Class not found: " << class_name;
58 if (is_virtual) {
59 mid_ = env_->GetMethodID(class_, method, signature);
60 } else {
61 mid_ = env_->GetStaticMethodID(class_, method, signature);
62 }
63 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
64 }
65
66 void MakeAllExecutable(jobject class_loader) {
67 const std::vector<const DexFile*>& class_path
68 = Runtime::Current()->GetCompileTimeClassPath(class_loader);
69 for (size_t i = 0; i != class_path.size(); ++i) {
70 const DexFile* dex_file = class_path[i];
71 CHECK(dex_file != NULL);
72 MakeDexFileExecutable(class_loader, *dex_file);
73 }
74 }
75
76 void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) {
77 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
78 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
79 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
80 const char* descriptor = dex_file.GetClassDescriptor(class_def);
81 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -080082 SirtRef<mirror::ClassLoader> loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(class_loader));
83 mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader);
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 CHECK(c != NULL);
85 for (size_t i = 0; i < c->NumDirectMethods(); i++) {
86 MakeExecutable(c->GetDirectMethod(i));
87 }
88 for (size_t i = 0; i < c->NumVirtualMethods(); i++) {
89 MakeExecutable(c->GetVirtualMethod(i));
90 }
91 }
92 }
93
94 JNIEnv* env_;
95 jclass class_;
96 jmethodID mid_;
97};
98
99// Disabled due to 10 second runtime on host
100TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
101 CompileAll(NULL);
102
103 // All libcore references should resolve
104 ScopedObjectAccess soa(Thread::Current());
105 const DexFile* dex = java_lang_dex_file_;
106 mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex);
107 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
108 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
109 const mirror::String* string = dex_cache->GetResolvedString(i);
110 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
111 }
112 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
113 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
114 mirror::Class* type = dex_cache->GetResolvedType(i);
115 EXPECT_TRUE(type != NULL) << "type_idx=" << i
116 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i));
117 }
118 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
119 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700120 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 EXPECT_TRUE(method != NULL) << "method_idx=" << i
122 << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
123 << " " << dex->GetMethodName(dex->GetMethodId(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != NULL) << "method_idx=" << i
125 << " "
126 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
127 << " " << dex->GetMethodName(dex->GetMethodId(i));
128 EXPECT_TRUE(method->GetEntryPointFromPortableCompiledCode() != NULL) << "method_idx=" << i
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 << " "
130 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
131 << " " << dex->GetMethodName(dex->GetMethodId(i));
132 }
133 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
134 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700135 mirror::ArtField* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 EXPECT_TRUE(field != NULL) << "field_idx=" << i
137 << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i))
138 << " " << dex->GetFieldName(dex->GetFieldId(i));
139 }
140
141 // TODO check Class::IsVerified for all classes
142
143 // TODO: check that all Method::GetCode() values are non-null
144}
145
146TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
147 TEST_DISABLED_FOR_PORTABLE();
148 jobject class_loader;
149 {
150 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 SirtRef<mirror::ClassLoader> null_loader(soa.Self(), nullptr);
152 CompileVirtualMethod(null_loader, "java.lang.Class", "isFinalizable", "()Z");
153 CompileDirectMethod(null_loader, "java.lang.Object", "<init>", "()V");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 class_loader = LoadDex("AbstractMethod");
155 }
156 ASSERT_TRUE(class_loader != NULL);
157 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
158
159 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
160 jclass c_class = env_->FindClass("ConcreteClass");
161 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
162 jobject jobj_ = env_->NewObject(c_class, constructor);
163 ASSERT_TRUE(jobj_ != NULL);
164
165 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
166 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
167 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
168 jthrowable exception = env_->ExceptionOccurred();
169 env_->ExceptionClear();
170 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
171 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
172 Thread::Current()->ClearException();
173}
174
175// TODO: need check-cast test (when stub complete & we can throw/catch
176
177} // namespace art