blob: 053f1f4bf5f0f98dbb7784cfbb255def2bfb5530 [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"
Calin Juravle877fd962016-01-05 14:29:29 +000034#include "jit/offline_profiling_info.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "scoped_thread_state_change.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036
37namespace art {
38
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080039class CompilerDriverTest : public CommonCompilerTest {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 protected:
Mathieu Chartier90443472015-07-16 20:32:27 -070041 void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) {
Ian Rogers5fe9af72013-11-14 00:17:20 -080042 TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070043 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Brian Carlstrom45602482013-07-21 22:07:55 -070044 compiler_driver_->CompileAll(class_loader,
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070045 GetDexFiles(class_loader),
Ian Rogers3d504072014-03-01 09:16:49 -080046 &timings);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070047 t.NewTiming("MakeAllExecutable");
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 MakeAllExecutable(class_loader);
49 }
50
51 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
52 const char* signature, bool is_virtual)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 REQUIRES(!Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 CompileAll(class_loader);
55 Thread::Current()->TransitionFromSuspendedToRunnable();
56 bool started = runtime_->Start();
57 CHECK(started);
58 env_ = Thread::Current()->GetJniEnv();
59 class_ = env_->FindClass(class_name);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070060 CHECK(class_ != nullptr) << "Class not found: " << class_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 if (is_virtual) {
62 mid_ = env_->GetMethodID(class_, method, signature);
63 } else {
64 mid_ = env_->GetStaticMethodID(class_, method, signature);
65 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature;
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 }
68
69 void MakeAllExecutable(jobject class_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070070 const std::vector<const DexFile*> class_path = GetDexFiles(class_loader);
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 for (size_t i = 0; i != class_path.size(); ++i) {
72 const DexFile* dex_file = class_path[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 CHECK(dex_file != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 MakeDexFileExecutable(class_loader, *dex_file);
75 }
76 }
77
78 void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) {
79 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
80 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
81 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
82 const char* descriptor = dex_file.GetClassDescriptor(class_def);
83 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 StackHandleScope<1> hs(soa.Self());
85 Handle<mirror::ClassLoader> loader(
86 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
Ian Rogers98379392014-02-24 16:53:16 -080087 mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070088 CHECK(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 const auto pointer_size = class_linker->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -080090 for (auto& m : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 MakeExecutable(&m);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 }
93 }
94 }
95
96 JNIEnv* env_;
97 jclass class_;
98 jmethodID mid_;
99};
100
101// Disabled due to 10 second runtime on host
102TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700103 CompileAll(nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104
105 // All libcore references should resolve
106 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700107 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800108 const DexFile& dex = *java_lang_dex_file_;
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700109 mirror::DexCache* dex_cache = class_linker_->FindDexCache(soa.Self(), dex);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800110 EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
112 const mirror::String* string = dex_cache->GetResolvedString(i);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 EXPECT_TRUE(string != nullptr) << "string_idx=" << i;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800115 EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
117 mirror::Class* type = dex_cache->GetResolvedType(i);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 EXPECT_TRUE(type != nullptr) << "type_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800119 << " " << dex.GetTypeDescriptor(dex.GetTypeId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800121 EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 auto* cl = Runtime::Current()->GetClassLinker();
123 auto pointer_size = cl->GetImagePointerSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 EXPECT_TRUE(method != nullptr) << "method_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800127 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
128 << " " << dex.GetMethodName(dex.GetMethodId(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i
130 << " " << 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 Chartiere401d142015-04-22 13:56:20 -0700135 ArtField* field = cl->GetResolvedField(i, dex_cache);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 EXPECT_TRUE(field != nullptr) << "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
Yi Kong5dcf19d2016-04-04 17:44:59 +0100146TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
Roland Levillain4d027112015-07-01 15:41:14 +0100147 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000148 TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK();
149 TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 jobject class_loader;
151 {
152 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 class_loader = LoadDex("AbstractMethod");
154 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 ASSERT_TRUE(class_loader != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
157
158 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
159 jclass c_class = env_->FindClass("ConcreteClass");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700160
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700162
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 jobject jobj_ = env_->NewObject(c_class, constructor);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700164 ASSERT_TRUE(jobj_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165
166 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
167 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
170 jthrowable exception = env_->ExceptionOccurred();
171 env_->ExceptionClear();
172 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
173 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
Serguei Katkova309d762014-05-26 11:23:39 +0700174 {
175 ScopedObjectAccess soa(Thread::Current());
176 Thread::Current()->ClearException();
177 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178}
179
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700180class CompilerDriverMethodsTest : public CompilerDriverTest {
181 protected:
182 std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE {
183 return new std::unordered_set<std::string>({
184 "byte StaticLeafMethods.identity(byte)",
185 "int StaticLeafMethods.sum(int, int, int)",
186 "double StaticLeafMethods.sum(double, double, double, double)"
187 });
188 }
189};
190
191TEST_F(CompilerDriverMethodsTest, Selection) {
Roland Levillain4d027112015-07-01 15:41:14 +0100192 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000193 TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK();
194 TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700195 Thread* self = Thread::Current();
196 jobject class_loader;
197 {
198 ScopedObjectAccess soa(self);
199 class_loader = LoadDex("StaticLeafMethods");
200 }
201 ASSERT_NE(class_loader, nullptr);
202
203 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
204 // dex-to-dex compiler.
205 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
206 ASSERT_TRUE(dex_file->EnableWrite());
207 }
208
209 CompileAll(class_loader);
210
211 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700212 ScopedObjectAccess soa(self);
Mathieu Chartiered150002015-08-28 11:16:54 -0700213 StackHandleScope<1> hs(self);
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700214 Handle<mirror::ClassLoader> h_loader(hs.NewHandle(
215 reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader))));
216 mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader);
217 ASSERT_NE(klass, nullptr);
218
219 std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods());
220
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221 const auto pointer_size = class_linker->GetImagePointerSize();
222 for (auto& m : klass->GetDirectMethods(pointer_size)) {
223 std::string name = PrettyMethod(&m, true);
224 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700225 ASSERT_NE(code, nullptr);
226 if (expected->find(name) != expected->end()) {
227 expected->erase(name);
228 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
229 } else {
230 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
231 }
232 }
233 EXPECT_TRUE(expected->empty());
234}
235
Calin Juravle877fd962016-01-05 14:29:29 +0000236class CompilerDriverProfileTest : public CompilerDriverTest {
237 protected:
238 ProfileCompilationInfo* GetProfileCompilationInfo() OVERRIDE {
239 ScopedObjectAccess soa(Thread::Current());
240 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
241
242 ProfileCompilationInfo info;
243 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
Calin Juravle31708b72016-02-05 19:44:05 +0000244 std::string key = ProfileCompilationInfo::GetProfileDexFileKey(dex_file->GetLocation());
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800245 profile_info_.AddMethodIndex(key, dex_file->GetLocationChecksum(), 1);
246 profile_info_.AddMethodIndex(key, dex_file->GetLocationChecksum(), 2);
Calin Juravle877fd962016-01-05 14:29:29 +0000247 }
248 return &profile_info_;
249 }
250
251 std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) {
252 if (clazz == "Main") {
253 return std::unordered_set<std::string>({
254 "java.lang.String Main.getA()",
255 "java.lang.String Main.getB()"});
256 } else if (clazz == "Second") {
257 return std::unordered_set<std::string>({
258 "java.lang.String Second.getX()",
259 "java.lang.String Second.getY()"});
260 } else {
261 return std::unordered_set<std::string>();
262 }
263 }
264
265 void CheckCompiledMethods(jobject class_loader,
266 const std::string& clazz,
267 const std::unordered_set<std::string>& expected_methods) {
268 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
269 Thread* self = Thread::Current();
270 ScopedObjectAccess soa(self);
271 StackHandleScope<1> hs(self);
272 Handle<mirror::ClassLoader> h_loader(hs.NewHandle(
273 reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader))));
274 mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader);
275 ASSERT_NE(klass, nullptr);
276
277 const auto pointer_size = class_linker->GetImagePointerSize();
278 size_t number_of_compiled_methods = 0;
279 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
280 std::string name = PrettyMethod(&m, true);
281 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
282 ASSERT_NE(code, nullptr);
283 if (expected_methods.find(name) != expected_methods.end()) {
284 number_of_compiled_methods++;
285 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
286 } else {
287 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
288 }
289 }
290 EXPECT_EQ(expected_methods.size(), number_of_compiled_methods);
291 }
292
293 private:
294 ProfileCompilationInfo profile_info_;
295};
296
297TEST_F(CompilerDriverProfileTest, ProfileGuidedCompilation) {
298 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK();
299 TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK();
300 TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS();
301 Thread* self = Thread::Current();
302 jobject class_loader;
303 {
304 ScopedObjectAccess soa(self);
305 class_loader = LoadDex("ProfileTestMultiDex");
306 }
307 ASSERT_NE(class_loader, nullptr);
308
309 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
310 // dex-to-dex compiler.
311 ProfileCompilationInfo info;
312 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
313 ASSERT_TRUE(dex_file->EnableWrite());
314 }
315
316 CompileAll(class_loader);
317
318 std::unordered_set<std::string> m = GetExpectedMethodsForClass("Main");
319 std::unordered_set<std::string> s = GetExpectedMethodsForClass("Second");
320 CheckCompiledMethods(class_loader, "LMain;", m);
321 CheckCompiledMethods(class_loader, "LSecond;", s);
322}
323
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324// TODO: need check-cast test (when stub complete & we can throw/catch
325
326} // namespace art