blob: 5325a68b3770c74743beedb672db9ef9f99e6e84 [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>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#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"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070033#include "handle_scope-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034
35namespace art {
36
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080037class CompilerDriverTest : public CommonCompilerTest {
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 protected:
39 void CompileAll(jobject class_loader) LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers5fe9af72013-11-14 00:17:20 -080040 TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070041 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Brian Carlstrom45602482013-07-21 22:07:55 -070042 compiler_driver_->CompileAll(class_loader,
43 Runtime::Current()->GetCompileTimeClassPath(class_loader),
Ian Rogers3d504072014-03-01 09:16:49 -080044 &timings);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070045 t.NewTiming("MakeAllExecutable");
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 MakeAllExecutable(class_loader);
47 }
48
49 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
50 const char* signature, bool is_virtual)
51 LOCKS_EXCLUDED(Locks::mutator_lock_) {
52 CompileAll(class_loader);
53 Thread::Current()->TransitionFromSuspendedToRunnable();
54 bool started = runtime_->Start();
55 CHECK(started);
56 env_ = Thread::Current()->GetJniEnv();
57 class_ = env_->FindClass(class_name);
58 CHECK(class_ != NULL) << "Class not found: " << class_name;
59 if (is_virtual) {
60 mid_ = env_->GetMethodID(class_, method, signature);
61 } else {
62 mid_ = env_->GetStaticMethodID(class_, method, signature);
63 }
64 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
65 }
66
67 void MakeAllExecutable(jobject class_loader) {
68 const std::vector<const DexFile*>& class_path
69 = Runtime::Current()->GetCompileTimeClassPath(class_loader);
70 for (size_t i = 0; i != class_path.size(); ++i) {
71 const DexFile* dex_file = class_path[i];
72 CHECK(dex_file != NULL);
73 MakeDexFileExecutable(class_loader, *dex_file);
74 }
75 }
76
77 void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) {
78 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
79 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
80 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
81 const char* descriptor = dex_file.GetClassDescriptor(class_def);
82 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 StackHandleScope<1> hs(soa.Self());
84 Handle<mirror::ClassLoader> loader(
85 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
Ian Rogers98379392014-02-24 16:53:16 -080086 mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader);
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 CHECK(c != NULL);
88 for (size_t i = 0; i < c->NumDirectMethods(); i++) {
89 MakeExecutable(c->GetDirectMethod(i));
90 }
91 for (size_t i = 0; i < c->NumVirtualMethods(); i++) {
92 MakeExecutable(c->GetVirtualMethod(i));
93 }
94 }
95 }
96
97 JNIEnv* env_;
98 jclass class_;
99 jmethodID mid_;
100};
101
102// Disabled due to 10 second runtime on host
103TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
104 CompileAll(NULL);
105
106 // All libcore references should resolve
107 ScopedObjectAccess soa(Thread::Current());
108 const DexFile* dex = java_lang_dex_file_;
109 mirror::DexCache* dex_cache = class_linker_->FindDexCache(*dex);
110 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
111 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
112 const mirror::String* string = dex_cache->GetResolvedString(i);
113 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
114 }
115 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
116 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
117 mirror::Class* type = dex_cache->GetResolvedType(i);
118 EXPECT_TRUE(type != NULL) << "type_idx=" << i
119 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i));
120 }
121 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
122 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700123 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 EXPECT_TRUE(method != NULL) << "method_idx=" << i
125 << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
126 << " " << dex->GetMethodName(dex->GetMethodId(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != NULL) << "method_idx=" << i
128 << " "
129 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
130 << " " << dex->GetMethodName(dex->GetMethodId(i));
131 EXPECT_TRUE(method->GetEntryPointFromPortableCompiledCode() != NULL) << "method_idx=" << i
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 << " "
133 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i))
134 << " " << dex->GetMethodName(dex->GetMethodId(i));
135 }
136 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
137 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700138 mirror::ArtField* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 EXPECT_TRUE(field != NULL) << "field_idx=" << i
140 << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i))
141 << " " << dex->GetFieldName(dex->GetFieldId(i));
142 }
143
144 // TODO check Class::IsVerified for all classes
145
146 // TODO: check that all Method::GetCode() values are non-null
147}
148
149TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
150 TEST_DISABLED_FOR_PORTABLE();
Hiroshi Yamauchi05b15d62014-03-19 12:57:56 -0700151 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 jobject class_loader;
153 {
154 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700155 CompileVirtualMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Class", "isFinalizable",
156 "()Z");
157 CompileDirectMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Object", "<init>", "()V");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 class_loader = LoadDex("AbstractMethod");
159 }
160 ASSERT_TRUE(class_loader != NULL);
161 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
162
163 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
164 jclass c_class = env_->FindClass("ConcreteClass");
165 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
166 jobject jobj_ = env_->NewObject(c_class, constructor);
167 ASSERT_TRUE(jobj_ != NULL);
168
169 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
170 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
171 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
172 jthrowable exception = env_->ExceptionOccurred();
173 env_->ExceptionClear();
174 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
175 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
Serguei Katkova309d762014-05-26 11:23:39 +0700176 {
177 ScopedObjectAccess soa(Thread::Current());
178 Thread::Current()->ClearException();
179 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180}
181
182// TODO: need check-cast test (when stub complete & we can throw/catch
183
184} // namespace art