blob: 26ea39f20555435d9ff39d9a45dbc66e014f7dfb [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);
Brian Carlstrom45602482013-07-21 22:07:55 -070045 compiler_driver_->CompileAll(class_loader,
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070046 GetDexFiles(class_loader),
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000047 /* verifier_deps */ nullptr,
Ian Rogers3d504072014-03-01 09:16:49 -080048 &timings);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070049 t.NewTiming("MakeAllExecutable");
Brian Carlstrom7940e442013-07-12 13:46:57 -070050 MakeAllExecutable(class_loader);
51 }
52
53 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
54 const char* signature, bool is_virtual)
Mathieu Chartier90443472015-07-16 20:32:27 -070055 REQUIRES(!Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 CompileAll(class_loader);
57 Thread::Current()->TransitionFromSuspendedToRunnable();
58 bool started = runtime_->Start();
59 CHECK(started);
60 env_ = Thread::Current()->GetJniEnv();
61 class_ = env_->FindClass(class_name);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 CHECK(class_ != nullptr) << "Class not found: " << class_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 if (is_virtual) {
64 mid_ = env_->GetMethodID(class_, method, signature);
65 } else {
66 mid_ = env_->GetStaticMethodID(class_, method, signature);
67 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature;
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 }
70
71 void MakeAllExecutable(jobject class_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070072 const std::vector<const DexFile*> class_path = GetDexFiles(class_loader);
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 for (size_t i = 0; i != class_path.size(); ++i) {
74 const DexFile* dex_file = class_path[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 CHECK(dex_file != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 MakeDexFileExecutable(class_loader, *dex_file);
77 }
78 }
79
80 void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) {
81 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
82 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
83 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
84 const char* descriptor = dex_file.GetClassDescriptor(class_def);
85 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070086 StackHandleScope<1> hs(soa.Self());
87 Handle<mirror::ClassLoader> loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -070088 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Ian Rogers98379392014-02-24 16:53:16 -080089 mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 CHECK(c != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 const auto pointer_size = class_linker->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -080092 for (auto& m : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 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
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000104// TODO: Update the test for hash-based dex cache arrays. Bug: 30627598
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 CompileAll(nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107
108 // All libcore references should resolve
109 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800111 const DexFile& dex = *java_lang_dex_file_;
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700112 ObjPtr<mirror::DexCache> dex_cache = class_linker_->FindDexCache(soa.Self(), dex);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800113 EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800115 const mirror::String* string = dex_cache->GetResolvedString(dex::StringIndex(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 EXPECT_TRUE(string != nullptr) << "string_idx=" << i;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800118 EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800120 mirror::Class* type = dex_cache->GetResolvedType(dex::TypeIndex(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 EXPECT_TRUE(type != nullptr) << "type_idx=" << i
Andreas Gampea5b09a62016-11-17 15:21:22 -0800122 << " " << dex.GetTypeDescriptor(dex.GetTypeId(dex::TypeIndex(i)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800124 EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 auto* cl = Runtime::Current()->GetClassLinker();
126 auto pointer_size = cl->GetImagePointerSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 EXPECT_TRUE(method != nullptr) << "method_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800130 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
131 << " " << dex.GetMethodName(dex.GetMethodId(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700132 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i
133 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " "
134 << dex.GetMethodName(dex.GetMethodId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000136 EXPECT_TRUE(dex_cache->StaticArtFieldSize() == dex_cache->NumResolvedFields()
137 || dex.NumFieldIds() == dex_cache->NumResolvedFields());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000139 ArtField* field = dex_cache->GetResolvedField(i, cl->GetImagePointerSize());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 EXPECT_TRUE(field != nullptr) << "field_idx=" << i
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800141 << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i))
142 << " " << dex.GetFieldName(dex.GetFieldId(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 }
144
145 // TODO check Class::IsVerified for all classes
146
147 // TODO: check that all Method::GetCode() values are non-null
148}
149
Yi Kong5dcf19d2016-04-04 17:44:59 +0100150TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 jobject class_loader;
152 {
153 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 class_loader = LoadDex("AbstractMethod");
155 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 ASSERT_TRUE(class_loader != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
158
159 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
160 jclass c_class = env_->FindClass("ConcreteClass");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 jobject jobj_ = env_->NewObject(c_class, constructor);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700165 ASSERT_TRUE(jobj_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166
167 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
168 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700169
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
171 jthrowable exception = env_->ExceptionOccurred();
172 env_->ExceptionClear();
173 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
174 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
Serguei Katkova309d762014-05-26 11:23:39 +0700175 {
176 ScopedObjectAccess soa(Thread::Current());
177 Thread::Current()->ClearException();
178 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179}
180
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700181class CompilerDriverMethodsTest : public CompilerDriverTest {
182 protected:
183 std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE {
184 return new std::unordered_set<std::string>({
185 "byte StaticLeafMethods.identity(byte)",
186 "int StaticLeafMethods.sum(int, int, int)",
187 "double StaticLeafMethods.sum(double, double, double, double)"
188 });
189 }
190};
191
192TEST_F(CompilerDriverMethodsTest, Selection) {
193 Thread* self = Thread::Current();
194 jobject class_loader;
195 {
196 ScopedObjectAccess soa(self);
197 class_loader = LoadDex("StaticLeafMethods");
198 }
199 ASSERT_NE(class_loader, nullptr);
200
201 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
202 // dex-to-dex compiler.
203 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
204 ASSERT_TRUE(dex_file->EnableWrite());
205 }
206
207 CompileAll(class_loader);
208
209 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700210 ScopedObjectAccess soa(self);
Mathieu Chartiered150002015-08-28 11:16:54 -0700211 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700212 Handle<mirror::ClassLoader> h_loader(
213 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700214 mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader);
215 ASSERT_NE(klass, nullptr);
216
217 std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods());
218
Mathieu Chartiere401d142015-04-22 13:56:20 -0700219 const auto pointer_size = class_linker->GetImagePointerSize();
220 for (auto& m : klass->GetDirectMethods(pointer_size)) {
David Sehr709b0702016-10-13 09:12:37 -0700221 std::string name = m.PrettyMethod(true);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700223 ASSERT_NE(code, nullptr);
224 if (expected->find(name) != expected->end()) {
225 expected->erase(name);
226 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
227 } else {
228 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
229 }
230 }
231 EXPECT_TRUE(expected->empty());
232}
233
Calin Juravle877fd962016-01-05 14:29:29 +0000234class CompilerDriverProfileTest : public CompilerDriverTest {
235 protected:
236 ProfileCompilationInfo* GetProfileCompilationInfo() OVERRIDE {
237 ScopedObjectAccess soa(Thread::Current());
238 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
239
240 ProfileCompilationInfo info;
241 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800242 profile_info_.AddMethodIndex(dex_file->GetLocation(), dex_file->GetLocationChecksum(), 1);
243 profile_info_.AddMethodIndex(dex_file->GetLocation(), dex_file->GetLocationChecksum(), 2);
Calin Juravle877fd962016-01-05 14:29:29 +0000244 }
245 return &profile_info_;
246 }
247
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800248 CompilerFilter::Filter GetCompilerFilter() const OVERRIDE {
249 // Use a profile based filter.
250 return CompilerFilter::kSpeedProfile;
251 }
252
Calin Juravle877fd962016-01-05 14:29:29 +0000253 std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) {
254 if (clazz == "Main") {
255 return std::unordered_set<std::string>({
256 "java.lang.String Main.getA()",
257 "java.lang.String Main.getB()"});
258 } else if (clazz == "Second") {
259 return std::unordered_set<std::string>({
260 "java.lang.String Second.getX()",
261 "java.lang.String Second.getY()"});
262 } else {
263 return std::unordered_set<std::string>();
264 }
265 }
266
267 void CheckCompiledMethods(jobject class_loader,
268 const std::string& clazz,
269 const std::unordered_set<std::string>& expected_methods) {
270 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
271 Thread* self = Thread::Current();
272 ScopedObjectAccess soa(self);
273 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700274 Handle<mirror::ClassLoader> h_loader(
275 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Calin Juravle877fd962016-01-05 14:29:29 +0000276 mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader);
277 ASSERT_NE(klass, nullptr);
278
279 const auto pointer_size = class_linker->GetImagePointerSize();
280 size_t number_of_compiled_methods = 0;
281 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
David Sehr709b0702016-10-13 09:12:37 -0700282 std::string name = m.PrettyMethod(true);
Calin Juravle877fd962016-01-05 14:29:29 +0000283 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
284 ASSERT_NE(code, nullptr);
285 if (expected_methods.find(name) != expected_methods.end()) {
286 number_of_compiled_methods++;
287 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
288 } else {
289 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
290 }
291 }
292 EXPECT_EQ(expected_methods.size(), number_of_compiled_methods);
293 }
294
295 private:
296 ProfileCompilationInfo profile_info_;
297};
298
299TEST_F(CompilerDriverProfileTest, ProfileGuidedCompilation) {
Calin Juravle877fd962016-01-05 14:29:29 +0000300 Thread* self = Thread::Current();
301 jobject class_loader;
302 {
303 ScopedObjectAccess soa(self);
304 class_loader = LoadDex("ProfileTestMultiDex");
305 }
306 ASSERT_NE(class_loader, nullptr);
307
308 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
309 // dex-to-dex compiler.
Calin Juravle877fd962016-01-05 14:29:29 +0000310 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
311 ASSERT_TRUE(dex_file->EnableWrite());
312 }
313
314 CompileAll(class_loader);
315
316 std::unordered_set<std::string> m = GetExpectedMethodsForClass("Main");
317 std::unordered_set<std::string> s = GetExpectedMethodsForClass("Second");
318 CheckCompiledMethods(class_loader, "LMain;", m);
319 CheckCompiledMethods(class_loader, "LSecond;", s);
320}
321
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +0100322// Test that a verify only compiler filter updates the CompiledClass map,
323// which will be used for OatClass.
324class CompilerDriverVerifyTest : public CompilerDriverTest {
325 protected:
326 CompilerFilter::Filter GetCompilerFilter() const OVERRIDE {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100327 return CompilerFilter::kVerify;
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +0100328 }
329
330 void CheckVerifiedClass(jobject class_loader, const std::string& clazz) const {
331 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
332 Thread* self = Thread::Current();
333 ScopedObjectAccess soa(self);
334 StackHandleScope<1> hs(self);
335 Handle<mirror::ClassLoader> h_loader(
336 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
337 mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader);
338 ASSERT_NE(klass, nullptr);
339 EXPECT_TRUE(klass->IsVerified());
340
Andreas Gampebb846102017-05-11 21:03:35 -0700341 mirror::Class::Status status;
342 bool found = compiler_driver_->GetCompiledClass(
343 ClassReference(&klass->GetDexFile(), klass->GetDexTypeIndex().index_), &status);
344 ASSERT_TRUE(found);
345 EXPECT_EQ(status, mirror::Class::kStatusVerified);
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +0100346 }
347};
348
349TEST_F(CompilerDriverVerifyTest, VerifyCompilation) {
350 Thread* self = Thread::Current();
351 jobject class_loader;
352 {
353 ScopedObjectAccess soa(self);
354 class_loader = LoadDex("ProfileTestMultiDex");
355 }
356 ASSERT_NE(class_loader, nullptr);
357
358 CompileAll(class_loader);
359
360 CheckVerifiedClass(class_loader, "LMain;");
361 CheckVerifiedClass(class_loader, "LSecond;");
362}
363
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364// TODO: need check-cast test (when stub complete & we can throw/catch
365
366} // namespace art