blob: 1329fddc7944f4bc4b68e7f036e7d00906be101c [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"
Andreas Gampea5b09a62016-11-17 15:21:22 -080027#include "dex_file_types.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "gc/heap.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "mirror/class_loader.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "mirror/object_array-inl.h"
33#include "mirror/object-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034#include "handle_scope-inl.h"
Calin Juravle33083d62017-01-18 15:29:12 -080035#include "jit/profile_compilation_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070037
38namespace art {
39
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080040class CompilerDriverTest : public CommonCompilerTest {
Brian Carlstrom7940e442013-07-12 13:46:57 -070041 protected:
Mathieu Chartier90443472015-07-16 20:32:27 -070042 void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) {
Ian Rogers5fe9af72013-11-14 00:17:20 -080043 TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070044 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +010045 compiler_driver_->CompileAll(class_loader, GetDexFiles(class_loader), &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(
Mathieu Chartier0795f232016-09-27 18:43:30 -070085 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();
Alex Lighte64300b2015-12-15 15:02:47 -080089 for (auto& m : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 MakeExecutable(&m);
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 }
92 }
93 }
94
95 JNIEnv* env_;
96 jclass class_;
97 jmethodID mid_;
98};
99
100// Disabled due to 10 second runtime on host
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000101// TODO: Update the test for hash-based dex cache arrays. Bug: 30627598
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102TEST_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 Chartier28357fa2016-10-18 16:27:40 -0700109 ObjPtr<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++) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800112 const mirror::String* string = dex_cache->GetResolvedString(dex::StringIndex(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++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800117 mirror::Class* type = dex_cache->GetResolvedType(dex::TypeIndex(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 EXPECT_TRUE(type != nullptr) << "type_idx=" << i
Andreas Gampea5b09a62016-11-17 15:21:22 -0800119 << " " << dex.GetTypeDescriptor(dex.GetTypeId(dex::TypeIndex(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 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000133 EXPECT_TRUE(dex_cache->StaticArtFieldSize() == dex_cache->NumResolvedFields()
134 || dex.NumFieldIds() == dex_cache->NumResolvedFields());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000136 ArtField* field = dex_cache->GetResolvedField(i, cl->GetImagePointerSize());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 EXPECT_TRUE(field != nullptr) << "field_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800138 << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i))
139 << " " << dex.GetFieldName(dex.GetFieldId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 }
141
142 // TODO check Class::IsVerified for all classes
143
144 // TODO: check that all Method::GetCode() values are non-null
145}
146
Yi Kong5dcf19d2016-04-04 17:44:59 +0100147TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 jobject class_loader;
149 {
150 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 class_loader = LoadDex("AbstractMethod");
152 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700153 ASSERT_TRUE(class_loader != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
155
156 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
157 jclass c_class = env_->FindClass("ConcreteClass");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700160
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 jobject jobj_ = env_->NewObject(c_class, constructor);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 ASSERT_TRUE(jobj_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163
164 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
165 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 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
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700178class CompilerDriverMethodsTest : public CompilerDriverTest {
179 protected:
180 std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE {
181 return new std::unordered_set<std::string>({
182 "byte StaticLeafMethods.identity(byte)",
183 "int StaticLeafMethods.sum(int, int, int)",
184 "double StaticLeafMethods.sum(double, double, double, double)"
185 });
186 }
187};
188
189TEST_F(CompilerDriverMethodsTest, Selection) {
190 Thread* self = Thread::Current();
191 jobject class_loader;
192 {
193 ScopedObjectAccess soa(self);
194 class_loader = LoadDex("StaticLeafMethods");
195 }
196 ASSERT_NE(class_loader, nullptr);
197
198 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
199 // dex-to-dex compiler.
200 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
201 ASSERT_TRUE(dex_file->EnableWrite());
202 }
203
204 CompileAll(class_loader);
205
206 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700207 ScopedObjectAccess soa(self);
Mathieu Chartiered150002015-08-28 11:16:54 -0700208 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700209 Handle<mirror::ClassLoader> h_loader(
210 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700211 mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader);
212 ASSERT_NE(klass, nullptr);
213
214 std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods());
215
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 const auto pointer_size = class_linker->GetImagePointerSize();
217 for (auto& m : klass->GetDirectMethods(pointer_size)) {
David Sehr709b0702016-10-13 09:12:37 -0700218 std::string name = m.PrettyMethod(true);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700219 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700220 ASSERT_NE(code, nullptr);
221 if (expected->find(name) != expected->end()) {
222 expected->erase(name);
223 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
224 } else {
225 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
226 }
227 }
228 EXPECT_TRUE(expected->empty());
229}
230
Calin Juravle877fd962016-01-05 14:29:29 +0000231class CompilerDriverProfileTest : public CompilerDriverTest {
232 protected:
233 ProfileCompilationInfo* GetProfileCompilationInfo() OVERRIDE {
234 ScopedObjectAccess soa(Thread::Current());
235 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
236
237 ProfileCompilationInfo info;
238 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700239 profile_info_.AddMethodIndex(dex_file->GetLocation(),
240 dex_file->GetLocationChecksum(),
241 1,
242 dex_file->NumMethodIds());
243 profile_info_.AddMethodIndex(dex_file->GetLocation(),
244 dex_file->GetLocationChecksum(),
245 2,
246 dex_file->NumMethodIds());
Calin Juravle877fd962016-01-05 14:29:29 +0000247 }
248 return &profile_info_;
249 }
250
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800251 CompilerFilter::Filter GetCompilerFilter() const OVERRIDE {
252 // Use a profile based filter.
253 return CompilerFilter::kSpeedProfile;
254 }
255
Calin Juravle877fd962016-01-05 14:29:29 +0000256 std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) {
257 if (clazz == "Main") {
258 return std::unordered_set<std::string>({
259 "java.lang.String Main.getA()",
260 "java.lang.String Main.getB()"});
261 } else if (clazz == "Second") {
262 return std::unordered_set<std::string>({
263 "java.lang.String Second.getX()",
264 "java.lang.String Second.getY()"});
265 } else {
266 return std::unordered_set<std::string>();
267 }
268 }
269
270 void CheckCompiledMethods(jobject class_loader,
271 const std::string& clazz,
272 const std::unordered_set<std::string>& expected_methods) {
273 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
274 Thread* self = Thread::Current();
275 ScopedObjectAccess soa(self);
276 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700277 Handle<mirror::ClassLoader> h_loader(
278 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Calin Juravle877fd962016-01-05 14:29:29 +0000279 mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader);
280 ASSERT_NE(klass, nullptr);
281
282 const auto pointer_size = class_linker->GetImagePointerSize();
283 size_t number_of_compiled_methods = 0;
284 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
David Sehr709b0702016-10-13 09:12:37 -0700285 std::string name = m.PrettyMethod(true);
Calin Juravle877fd962016-01-05 14:29:29 +0000286 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
287 ASSERT_NE(code, nullptr);
288 if (expected_methods.find(name) != expected_methods.end()) {
289 number_of_compiled_methods++;
290 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
291 } else {
292 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
293 }
294 }
295 EXPECT_EQ(expected_methods.size(), number_of_compiled_methods);
296 }
297
298 private:
299 ProfileCompilationInfo profile_info_;
300};
301
302TEST_F(CompilerDriverProfileTest, ProfileGuidedCompilation) {
Calin Juravle877fd962016-01-05 14:29:29 +0000303 Thread* self = Thread::Current();
304 jobject class_loader;
305 {
306 ScopedObjectAccess soa(self);
307 class_loader = LoadDex("ProfileTestMultiDex");
308 }
309 ASSERT_NE(class_loader, nullptr);
310
311 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
312 // dex-to-dex compiler.
Calin Juravle877fd962016-01-05 14:29:29 +0000313 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
314 ASSERT_TRUE(dex_file->EnableWrite());
315 }
316
317 CompileAll(class_loader);
318
319 std::unordered_set<std::string> m = GetExpectedMethodsForClass("Main");
320 std::unordered_set<std::string> s = GetExpectedMethodsForClass("Second");
321 CheckCompiledMethods(class_loader, "LMain;", m);
322 CheckCompiledMethods(class_loader, "LSecond;", s);
323}
324
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +0100325// Test that a verify only compiler filter updates the CompiledClass map,
326// which will be used for OatClass.
327class CompilerDriverVerifyTest : public CompilerDriverTest {
328 protected:
329 CompilerFilter::Filter GetCompilerFilter() const OVERRIDE {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100330 return CompilerFilter::kVerify;
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +0100331 }
332
333 void CheckVerifiedClass(jobject class_loader, const std::string& clazz) const {
334 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
335 Thread* self = Thread::Current();
336 ScopedObjectAccess soa(self);
337 StackHandleScope<1> hs(self);
338 Handle<mirror::ClassLoader> h_loader(
339 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
340 mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader);
341 ASSERT_NE(klass, nullptr);
342 EXPECT_TRUE(klass->IsVerified());
343
Andreas Gampebb846102017-05-11 21:03:35 -0700344 mirror::Class::Status status;
345 bool found = compiler_driver_->GetCompiledClass(
346 ClassReference(&klass->GetDexFile(), klass->GetDexTypeIndex().index_), &status);
347 ASSERT_TRUE(found);
348 EXPECT_EQ(status, mirror::Class::kStatusVerified);
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +0100349 }
350};
351
352TEST_F(CompilerDriverVerifyTest, VerifyCompilation) {
353 Thread* self = Thread::Current();
354 jobject class_loader;
355 {
356 ScopedObjectAccess soa(self);
357 class_loader = LoadDex("ProfileTestMultiDex");
358 }
359 ASSERT_NE(class_loader, nullptr);
360
361 CompileAll(class_loader);
362
363 CheckVerifiedClass(class_loader, "LMain;");
364 CheckVerifiedClass(class_loader, "LSecond;");
365}
366
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367// TODO: need check-cast test (when stub complete & we can throw/catch
368
369} // namespace art