Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 24 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | #include "common_compiler_test.h" |
Nicolas Geoffray | c7da1d6 | 2017-04-19 09:36:24 +0100 | [diff] [blame] | 26 | #include "compiled_class.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 28 | #include "dex_file_types.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 29 | #include "gc/heap.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 31 | #include "mirror/class_loader.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "mirror/dex_cache-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "mirror/object_array-inl.h" |
| 34 | #include "mirror/object-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | #include "handle_scope-inl.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 36 | #include "jit/profile_compilation_info.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 41 | class CompilerDriverTest : public CommonCompilerTest { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 42 | protected: |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 43 | void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 44 | TimingLogger timings("CompilerDriverTest::CompileAll", false, false); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 45 | TimingLogger::ScopedTiming t(__FUNCTION__, &timings); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 46 | compiler_driver_->CompileAll(class_loader, |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 47 | GetDexFiles(class_loader), |
Nicolas Geoffray | 6bb7f1b | 2016-11-03 10:52:49 +0000 | [diff] [blame] | 48 | /* verifier_deps */ nullptr, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 49 | &timings); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 50 | t.NewTiming("MakeAllExecutable"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | MakeAllExecutable(class_loader); |
| 52 | } |
| 53 | |
| 54 | void EnsureCompiled(jobject class_loader, const char* class_name, const char* method, |
| 55 | const char* signature, bool is_virtual) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 56 | REQUIRES(!Locks::mutator_lock_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 57 | CompileAll(class_loader); |
| 58 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 59 | bool started = runtime_->Start(); |
| 60 | CHECK(started); |
| 61 | env_ = Thread::Current()->GetJniEnv(); |
| 62 | class_ = env_->FindClass(class_name); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 63 | CHECK(class_ != nullptr) << "Class not found: " << class_name; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | if (is_virtual) { |
| 65 | mid_ = env_->GetMethodID(class_, method, signature); |
| 66 | } else { |
| 67 | mid_ = env_->GetStaticMethodID(class_, method, signature); |
| 68 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 69 | CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void MakeAllExecutable(jobject class_loader) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 73 | const std::vector<const DexFile*> class_path = GetDexFiles(class_loader); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 74 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 75 | const DexFile* dex_file = class_path[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 76 | CHECK(dex_file != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 77 | MakeDexFileExecutable(class_loader, *dex_file); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) { |
| 82 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 83 | for (size_t i = 0; i < dex_file.NumClassDefs(); i++) { |
| 84 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(i); |
| 85 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 86 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 87 | StackHandleScope<1> hs(soa.Self()); |
| 88 | Handle<mirror::ClassLoader> loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 89 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 90 | mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 91 | CHECK(c != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 92 | const auto pointer_size = class_linker->GetImagePointerSize(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 93 | for (auto& m : c->GetMethods(pointer_size)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 94 | MakeExecutable(&m); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | JNIEnv* env_; |
| 100 | jclass class_; |
| 101 | jmethodID mid_; |
| 102 | }; |
| 103 | |
| 104 | // Disabled due to 10 second runtime on host |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 105 | // TODO: Update the test for hash-based dex cache arrays. Bug: 30627598 |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 106 | TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 107 | CompileAll(nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | |
| 109 | // All libcore references should resolve |
| 110 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 111 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 112 | const DexFile& dex = *java_lang_dex_file_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 113 | ObjPtr<mirror::DexCache> dex_cache = class_linker_->FindDexCache(soa.Self(), dex); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 114 | EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 116 | const mirror::String* string = dex_cache->GetResolvedString(dex::StringIndex(i)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(string != nullptr) << "string_idx=" << i; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 118 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 119 | EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 120 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 121 | mirror::Class* type = dex_cache->GetResolvedType(dex::TypeIndex(i)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(type != nullptr) << "type_idx=" << i |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 123 | << " " << dex.GetTypeDescriptor(dex.GetTypeId(dex::TypeIndex(i))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 124 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 125 | EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 126 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 127 | auto pointer_size = cl->GetImagePointerSize(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 128 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 129 | ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(method != nullptr) << "method_idx=" << i |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 131 | << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) |
| 132 | << " " << dex.GetMethodName(dex.GetMethodId(i)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i |
| 134 | << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " " |
| 135 | << dex.GetMethodName(dex.GetMethodId(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 136 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 137 | EXPECT_TRUE(dex_cache->StaticArtFieldSize() == dex_cache->NumResolvedFields() |
| 138 | || dex.NumFieldIds() == dex_cache->NumResolvedFields()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 139 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 140 | ArtField* field = dex_cache->GetResolvedField(i, cl->GetImagePointerSize()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(field != nullptr) << "field_idx=" << i |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 142 | << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i)) |
| 143 | << " " << dex.GetFieldName(dex.GetFieldId(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // TODO check Class::IsVerified for all classes |
| 147 | |
| 148 | // TODO: check that all Method::GetCode() values are non-null |
| 149 | } |
| 150 | |
Yi Kong | 5dcf19d | 2016-04-04 17:44:59 +0100 | [diff] [blame] | 151 | TEST_F(CompilerDriverTest, AbstractMethodErrorStub) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 152 | jobject class_loader; |
| 153 | { |
| 154 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 155 | class_loader = LoadDex("AbstractMethod"); |
| 156 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 157 | ASSERT_TRUE(class_loader != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 158 | EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true); |
| 159 | |
| 160 | // Create a jobj_ of ConcreteClass, NOT AbstractClass. |
| 161 | jclass c_class = env_->FindClass("ConcreteClass"); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 162 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 163 | jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V"); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 164 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 165 | jobject jobj_ = env_->NewObject(c_class, constructor); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 166 | ASSERT_TRUE(jobj_ != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 167 | |
| 168 | // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception. |
| 169 | env_->CallNonvirtualVoidMethod(jobj_, class_, mid_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 170 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 171 | EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE); |
| 172 | jthrowable exception = env_->ExceptionOccurred(); |
| 173 | env_->ExceptionClear(); |
| 174 | jclass jlame = env_->FindClass("java/lang/AbstractMethodError"); |
| 175 | EXPECT_TRUE(env_->IsInstanceOf(exception, jlame)); |
Serguei Katkov | a309d76 | 2014-05-26 11:23:39 +0700 | [diff] [blame] | 176 | { |
| 177 | ScopedObjectAccess soa(Thread::Current()); |
| 178 | Thread::Current()->ClearException(); |
| 179 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 182 | class CompilerDriverMethodsTest : public CompilerDriverTest { |
| 183 | protected: |
| 184 | std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE { |
| 185 | return new std::unordered_set<std::string>({ |
| 186 | "byte StaticLeafMethods.identity(byte)", |
| 187 | "int StaticLeafMethods.sum(int, int, int)", |
| 188 | "double StaticLeafMethods.sum(double, double, double, double)" |
| 189 | }); |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | TEST_F(CompilerDriverMethodsTest, Selection) { |
| 194 | Thread* self = Thread::Current(); |
| 195 | jobject class_loader; |
| 196 | { |
| 197 | ScopedObjectAccess soa(self); |
| 198 | class_loader = LoadDex("StaticLeafMethods"); |
| 199 | } |
| 200 | ASSERT_NE(class_loader, nullptr); |
| 201 | |
| 202 | // Need to enable dex-file writability. Methods rejected to be compiled will run through the |
| 203 | // dex-to-dex compiler. |
| 204 | for (const DexFile* dex_file : GetDexFiles(class_loader)) { |
| 205 | ASSERT_TRUE(dex_file->EnableWrite()); |
| 206 | } |
| 207 | |
| 208 | CompileAll(class_loader); |
| 209 | |
| 210 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 211 | ScopedObjectAccess soa(self); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 212 | StackHandleScope<1> hs(self); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 213 | Handle<mirror::ClassLoader> h_loader( |
| 214 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 215 | mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader); |
| 216 | ASSERT_NE(klass, nullptr); |
| 217 | |
| 218 | std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods()); |
| 219 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 220 | const auto pointer_size = class_linker->GetImagePointerSize(); |
| 221 | for (auto& m : klass->GetDirectMethods(pointer_size)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 222 | std::string name = m.PrettyMethod(true); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 223 | const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 224 | ASSERT_NE(code, nullptr); |
| 225 | if (expected->find(name) != expected->end()) { |
| 226 | expected->erase(name); |
| 227 | EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code)); |
| 228 | } else { |
| 229 | EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code)); |
| 230 | } |
| 231 | } |
| 232 | EXPECT_TRUE(expected->empty()); |
| 233 | } |
| 234 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 235 | class CompilerDriverProfileTest : public CompilerDriverTest { |
| 236 | protected: |
| 237 | ProfileCompilationInfo* GetProfileCompilationInfo() OVERRIDE { |
| 238 | ScopedObjectAccess soa(Thread::Current()); |
| 239 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex"); |
| 240 | |
| 241 | ProfileCompilationInfo info; |
| 242 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 243 | profile_info_.AddMethodIndex(dex_file->GetLocation(), dex_file->GetLocationChecksum(), 1); |
| 244 | profile_info_.AddMethodIndex(dex_file->GetLocation(), dex_file->GetLocationChecksum(), 2); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 245 | } |
| 246 | return &profile_info_; |
| 247 | } |
| 248 | |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 249 | CompilerFilter::Filter GetCompilerFilter() const OVERRIDE { |
| 250 | // Use a profile based filter. |
| 251 | return CompilerFilter::kSpeedProfile; |
| 252 | } |
| 253 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 254 | std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) { |
| 255 | if (clazz == "Main") { |
| 256 | return std::unordered_set<std::string>({ |
| 257 | "java.lang.String Main.getA()", |
| 258 | "java.lang.String Main.getB()"}); |
| 259 | } else if (clazz == "Second") { |
| 260 | return std::unordered_set<std::string>({ |
| 261 | "java.lang.String Second.getX()", |
| 262 | "java.lang.String Second.getY()"}); |
| 263 | } else { |
| 264 | return std::unordered_set<std::string>(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void CheckCompiledMethods(jobject class_loader, |
| 269 | const std::string& clazz, |
| 270 | const std::unordered_set<std::string>& expected_methods) { |
| 271 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 272 | Thread* self = Thread::Current(); |
| 273 | ScopedObjectAccess soa(self); |
| 274 | StackHandleScope<1> hs(self); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 275 | Handle<mirror::ClassLoader> h_loader( |
| 276 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 277 | mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader); |
| 278 | ASSERT_NE(klass, nullptr); |
| 279 | |
| 280 | const auto pointer_size = class_linker->GetImagePointerSize(); |
| 281 | size_t number_of_compiled_methods = 0; |
| 282 | for (auto& m : klass->GetVirtualMethods(pointer_size)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 283 | std::string name = m.PrettyMethod(true); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 284 | const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
| 285 | ASSERT_NE(code, nullptr); |
| 286 | if (expected_methods.find(name) != expected_methods.end()) { |
| 287 | number_of_compiled_methods++; |
| 288 | EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code)); |
| 289 | } else { |
| 290 | EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code)); |
| 291 | } |
| 292 | } |
| 293 | EXPECT_EQ(expected_methods.size(), number_of_compiled_methods); |
| 294 | } |
| 295 | |
| 296 | private: |
| 297 | ProfileCompilationInfo profile_info_; |
| 298 | }; |
| 299 | |
| 300 | TEST_F(CompilerDriverProfileTest, ProfileGuidedCompilation) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 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. |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 311 | for (const DexFile* dex_file : GetDexFiles(class_loader)) { |
| 312 | ASSERT_TRUE(dex_file->EnableWrite()); |
| 313 | } |
| 314 | |
| 315 | CompileAll(class_loader); |
| 316 | |
| 317 | std::unordered_set<std::string> m = GetExpectedMethodsForClass("Main"); |
| 318 | std::unordered_set<std::string> s = GetExpectedMethodsForClass("Second"); |
| 319 | CheckCompiledMethods(class_loader, "LMain;", m); |
| 320 | CheckCompiledMethods(class_loader, "LSecond;", s); |
| 321 | } |
| 322 | |
Nicolas Geoffray | c7da1d6 | 2017-04-19 09:36:24 +0100 | [diff] [blame] | 323 | // Test that a verify only compiler filter updates the CompiledClass map, |
| 324 | // which will be used for OatClass. |
| 325 | class CompilerDriverVerifyTest : public CompilerDriverTest { |
| 326 | protected: |
| 327 | CompilerFilter::Filter GetCompilerFilter() const OVERRIDE { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame^] | 328 | return CompilerFilter::kVerify; |
Nicolas Geoffray | c7da1d6 | 2017-04-19 09:36:24 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void CheckVerifiedClass(jobject class_loader, const std::string& clazz) const { |
| 332 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 333 | Thread* self = Thread::Current(); |
| 334 | ScopedObjectAccess soa(self); |
| 335 | StackHandleScope<1> hs(self); |
| 336 | Handle<mirror::ClassLoader> h_loader( |
| 337 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
| 338 | mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader); |
| 339 | ASSERT_NE(klass, nullptr); |
| 340 | EXPECT_TRUE(klass->IsVerified()); |
| 341 | |
| 342 | CompiledClass* compiled_class = compiler_driver_->GetCompiledClass( |
| 343 | ClassReference(&klass->GetDexFile(), klass->GetDexTypeIndex().index_)); |
| 344 | ASSERT_NE(compiled_class, nullptr); |
| 345 | EXPECT_EQ(compiled_class->GetStatus(), mirror::Class::kStatusVerified); |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | TEST_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 Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 364 | // TODO: need check-cast test (when stub complete & we can throw/catch |
| 365 | |
| 366 | } // namespace art |