blob: e78ff9078b1c6cd962904c73e82eed9bf4f409a3 [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
Vladimir Marko3481ba22015-04-13 12:22:36 +010023#include "class_linker-inl.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-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "mirror/class_loader.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#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"
Ian Rogerse63db272014-07-15 15:36:11 -070034#include "scoped_thread_state_change.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
36namespace art {
37
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080038class CompilerDriverTest : public CommonCompilerTest {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 protected:
40 void CompileAll(jobject class_loader) LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers5fe9af72013-11-14 00:17:20 -080041 TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070042 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Brian Carlstrom45602482013-07-21 22:07:55 -070043 compiler_driver_->CompileAll(class_loader,
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070044 GetDexFiles(class_loader),
Ian Rogers3d504072014-03-01 09:16:49 -080045 &timings);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070046 t.NewTiming("MakeAllExecutable");
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 MakeAllExecutable(class_loader);
48 }
49
50 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
51 const char* signature, bool is_virtual)
52 LOCKS_EXCLUDED(Locks::mutator_lock_) {
53 CompileAll(class_loader);
54 Thread::Current()->TransitionFromSuspendedToRunnable();
55 bool started = runtime_->Start();
56 CHECK(started);
57 env_ = Thread::Current()->GetJniEnv();
58 class_ = env_->FindClass(class_name);
59 CHECK(class_ != NULL) << "Class not found: " << class_name;
60 if (is_virtual) {
61 mid_ = env_->GetMethodID(class_, method, signature);
62 } else {
63 mid_ = env_->GetStaticMethodID(class_, method, signature);
64 }
65 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature;
66 }
67
68 void MakeAllExecutable(jobject class_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070069 const std::vector<const DexFile*> class_path = GetDexFiles(class_loader);
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 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);
Andreas Gampe277ccbd2014-11-03 21:36:10 -080088 for (size_t j = 0; j < c->NumDirectMethods(); j++) {
89 MakeExecutable(c->GetDirectMethod(j));
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -080091 for (size_t j = 0; j < c->NumVirtualMethods(); j++) {
92 MakeExecutable(c->GetVirtualMethod(j));
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 }
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());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800108 ASSERT_TRUE(java_lang_dex_file_ != NULL);
109 const DexFile& dex = *java_lang_dex_file_;
110 mirror::DexCache* dex_cache = class_linker_->FindDexCache(dex);
111 EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
113 const mirror::String* string = dex_cache->GetResolvedString(i);
114 EXPECT_TRUE(string != NULL) << "string_idx=" << i;
115 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800116 EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
118 mirror::Class* type = dex_cache->GetResolvedType(i);
119 EXPECT_TRUE(type != NULL) << "type_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800120 << " " << dex.GetTypeDescriptor(dex.GetTypeId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800122 EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700124 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 EXPECT_TRUE(method != NULL) << "method_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800126 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
127 << " " << dex.GetMethodName(dex.GetMethodId(i));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != NULL) << "method_idx=" << i
129 << " "
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800130 << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
131 << " " << dex.GetMethodName(dex.GetMethodId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800133 EXPECT_EQ(dex.NumFieldIds(), dex_cache->NumResolvedFields());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135 ArtField* field = Runtime::Current()->GetClassLinker()->GetResolvedField(i, dex_cache);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 EXPECT_TRUE(field != NULL) << "field_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800137 << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i))
138 << " " << dex.GetFieldName(dex.GetFieldId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 }
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) {
Hiroshi Yamauchi05b15d62014-03-19 12:57:56 -0700147 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 jobject class_loader;
149 {
150 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700151 CompileVirtualMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Class", "isFinalizable",
152 "()Z");
153 CompileDirectMethod(NullHandle<mirror::ClassLoader>(), "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));
Serguei Katkova309d762014-05-26 11:23:39 +0700172 {
173 ScopedObjectAccess soa(Thread::Current());
174 Thread::Current()->ClearException();
175 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176}
177
178// TODO: need check-cast test (when stub complete & we can throw/catch
179
180} // namespace art