blob: 1107599779924d0f4b52fe42404994ce0a12076f [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_compiler_test.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "dex_file.h"
27#include "gc/heap.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:
Mathieu Chartier90443472015-07-16 20:32:27 -070040 void CompileAll(jobject class_loader) REQUIRES(!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)
Mathieu Chartier90443472015-07-16 20:32:27 -070052 REQUIRES(!Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 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);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 CHECK(class_ != nullptr) << "Class not found: " << class_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -070060 if (is_virtual) {
61 mid_ = env_->GetMethodID(class_, method, signature);
62 } else {
63 mid_ = env_->GetStaticMethodID(class_, method, signature);
64 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070065 CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature;
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 }
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];
Mathieu Chartier2cebb242015-04-21 16:50:40 -070072 CHECK(dex_file != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 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);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070087 CHECK(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 const auto pointer_size = class_linker->GetImagePointerSize();
89 for (auto& m : c->GetDirectMethods(pointer_size)) {
90 MakeExecutable(&m);
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070092 for (auto& m : c->GetVirtualMethods(pointer_size)) {
93 MakeExecutable(&m);
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 }
95 }
96 }
97
98 JNIEnv* env_;
99 jclass class_;
100 jmethodID mid_;
101};
102
103// Disabled due to 10 second runtime on host
104TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700105 CompileAll(nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106
107 // All libcore references should resolve
108 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800110 const DexFile& dex = *java_lang_dex_file_;
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700111 mirror::DexCache* dex_cache = class_linker_->FindDexCache(soa.Self(), dex);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800112 EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
114 const mirror::String* string = dex_cache->GetResolvedString(i);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 EXPECT_TRUE(string != nullptr) << "string_idx=" << i;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800117 EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
119 mirror::Class* type = dex_cache->GetResolvedType(i);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120 EXPECT_TRUE(type != nullptr) << "type_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800121 << " " << dex.GetTypeDescriptor(dex.GetTypeId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800123 EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 auto* cl = Runtime::Current()->GetClassLinker();
125 auto pointer_size = cl->GetImagePointerSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 EXPECT_TRUE(method != nullptr) << "method_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800129 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
130 << " " << dex.GetMethodName(dex.GetMethodId(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700131 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i
132 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " "
133 << dex.GetMethodName(dex.GetMethodId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800135 EXPECT_EQ(dex.NumFieldIds(), dex_cache->NumResolvedFields());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 ArtField* field = cl->GetResolvedField(i, dex_cache);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 EXPECT_TRUE(field != nullptr) << "field_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800139 << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i))
140 << " " << dex.GetFieldName(dex.GetFieldId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 }
142
143 // TODO check Class::IsVerified for all classes
144
145 // TODO: check that all Method::GetCode() values are non-null
146}
147
148TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
Roland Levillain4d027112015-07-01 15:41:14 +0100149 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 jobject class_loader;
151 {
152 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700153 CompileVirtualMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Class", "isFinalizable",
154 "()Z");
155 CompileDirectMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Object", "<init>", "()V");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 class_loader = LoadDex("AbstractMethod");
157 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700158 ASSERT_TRUE(class_loader != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
160
161 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
162 jclass c_class = env_->FindClass("ConcreteClass");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 jobject jobj_ = env_->NewObject(c_class, constructor);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700167 ASSERT_TRUE(jobj_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168
169 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
170 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
173 jthrowable exception = env_->ExceptionOccurred();
174 env_->ExceptionClear();
175 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
176 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
Serguei Katkova309d762014-05-26 11:23:39 +0700177 {
178 ScopedObjectAccess soa(Thread::Current());
179 Thread::Current()->ClearException();
180 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181}
182
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700183class CompilerDriverMethodsTest : public CompilerDriverTest {
184 protected:
185 std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE {
186 return new std::unordered_set<std::string>({
187 "byte StaticLeafMethods.identity(byte)",
188 "int StaticLeafMethods.sum(int, int, int)",
189 "double StaticLeafMethods.sum(double, double, double, double)"
190 });
191 }
192};
193
194TEST_F(CompilerDriverMethodsTest, Selection) {
Roland Levillain4d027112015-07-01 15:41:14 +0100195 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700196 Thread* self = Thread::Current();
197 jobject class_loader;
198 {
199 ScopedObjectAccess soa(self);
200 class_loader = LoadDex("StaticLeafMethods");
201 }
202 ASSERT_NE(class_loader, nullptr);
203
204 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
205 // dex-to-dex compiler.
206 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
207 ASSERT_TRUE(dex_file->EnableWrite());
208 }
209
210 CompileAll(class_loader);
211
212 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700213 ScopedObjectAccess soa(self);
Mathieu Chartiered150002015-08-28 11:16:54 -0700214 StackHandleScope<1> hs(self);
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700215 Handle<mirror::ClassLoader> h_loader(hs.NewHandle(
216 reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader))));
217 mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader);
218 ASSERT_NE(klass, nullptr);
219
220 std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods());
221
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 const auto pointer_size = class_linker->GetImagePointerSize();
223 for (auto& m : klass->GetDirectMethods(pointer_size)) {
224 std::string name = PrettyMethod(&m, true);
225 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700226 ASSERT_NE(code, nullptr);
227 if (expected->find(name) != expected->end()) {
228 expected->erase(name);
229 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
230 } else {
231 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
232 }
233 }
234 EXPECT_TRUE(expected->empty());
235}
236
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237// TODO: need check-cast test (when stub complete & we can throw/catch
238
239} // namespace art