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" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include "dex_file.h" |
| 27 | #include "gc/heap.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 29 | #include "mirror/class_loader.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 30 | #include "mirror/dex_cache-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | #include "mirror/object_array-inl.h" |
| 32 | #include "mirror/object-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 33 | #include "handle_scope-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 34 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 38 | class CompilerDriverTest : public CommonCompilerTest { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 39 | protected: |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 40 | void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 41 | TimingLogger timings("CompilerDriverTest::CompileAll", false, false); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 42 | TimingLogger::ScopedTiming t(__FUNCTION__, &timings); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 43 | compiler_driver_->CompileAll(class_loader, |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 44 | GetDexFiles(class_loader), |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 45 | &timings); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 46 | t.NewTiming("MakeAllExecutable"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 47 | 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 Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 52 | REQUIRES(!Locks::mutator_lock_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 53 | 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 59 | CHECK(class_ != nullptr) << "Class not found: " << class_name; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 60 | if (is_virtual) { |
| 61 | mid_ = env_->GetMethodID(class_, method, signature); |
| 62 | } else { |
| 63 | mid_ = env_->GetStaticMethodID(class_, method, signature); |
| 64 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 65 | CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void MakeAllExecutable(jobject class_loader) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 69 | const std::vector<const DexFile*> class_path = GetDexFiles(class_loader); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 71 | const DexFile* dex_file = class_path[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 72 | CHECK(dex_file != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 73 | 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 Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 83 | StackHandleScope<1> hs(soa.Self()); |
| 84 | Handle<mirror::ClassLoader> loader( |
| 85 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 86 | mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 87 | CHECK(c != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 88 | const auto pointer_size = class_linker->GetImagePointerSize(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame^] | 89 | for (auto& m : c->GetMethods(pointer_size)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 90 | MakeExecutable(&m); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | JNIEnv* env_; |
| 96 | jclass class_; |
| 97 | jmethodID mid_; |
| 98 | }; |
| 99 | |
| 100 | // Disabled due to 10 second runtime on host |
| 101 | TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 102 | CompileAll(nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | |
| 104 | // All libcore references should resolve |
| 105 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 106 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 107 | const DexFile& dex = *java_lang_dex_file_; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 108 | mirror::DexCache* dex_cache = class_linker_->FindDexCache(soa.Self(), dex); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 109 | EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 110 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
| 111 | const mirror::String* string = dex_cache->GetResolvedString(i); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 112 | EXPECT_TRUE(string != nullptr) << "string_idx=" << i; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 113 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 114 | EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 116 | mirror::Class* type = dex_cache->GetResolvedType(i); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(type != nullptr) << "type_idx=" << i |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 118 | << " " << dex.GetTypeDescriptor(dex.GetTypeId(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 119 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 120 | EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 121 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 122 | auto pointer_size = cl->GetImagePointerSize(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 123 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 125 | EXPECT_TRUE(method != nullptr) << "method_idx=" << i |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 126 | << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) |
| 127 | << " " << dex.GetMethodName(dex.GetMethodId(i)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 128 | EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i |
| 129 | << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " " |
| 130 | << dex.GetMethodName(dex.GetMethodId(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 131 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 132 | EXPECT_EQ(dex.NumFieldIds(), dex_cache->NumResolvedFields()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 133 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 134 | ArtField* field = cl->GetResolvedField(i, dex_cache); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(field != nullptr) << "field_idx=" << i |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 136 | << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i)) |
| 137 | << " " << dex.GetFieldName(dex.GetFieldId(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // TODO check Class::IsVerified for all classes |
| 141 | |
| 142 | // TODO: check that all Method::GetCode() values are non-null |
| 143 | } |
| 144 | |
| 145 | TEST_F(CompilerDriverTest, AbstractMethodErrorStub) { |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 146 | TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 147 | TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK(); |
| 148 | TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 149 | jobject class_loader; |
| 150 | { |
| 151 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 152 | CompileVirtualMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Class", "isFinalizable", |
| 153 | "()Z"); |
| 154 | CompileDirectMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Object", "<init>", "()V"); |
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) { |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 194 | TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK(); |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 195 | TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK(); |
| 196 | TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS(); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 197 | Thread* self = Thread::Current(); |
| 198 | jobject class_loader; |
| 199 | { |
| 200 | ScopedObjectAccess soa(self); |
| 201 | class_loader = LoadDex("StaticLeafMethods"); |
| 202 | } |
| 203 | ASSERT_NE(class_loader, nullptr); |
| 204 | |
| 205 | // Need to enable dex-file writability. Methods rejected to be compiled will run through the |
| 206 | // dex-to-dex compiler. |
| 207 | for (const DexFile* dex_file : GetDexFiles(class_loader)) { |
| 208 | ASSERT_TRUE(dex_file->EnableWrite()); |
| 209 | } |
| 210 | |
| 211 | CompileAll(class_loader); |
| 212 | |
| 213 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 214 | ScopedObjectAccess soa(self); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 215 | StackHandleScope<1> hs(self); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 216 | Handle<mirror::ClassLoader> h_loader(hs.NewHandle( |
| 217 | reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader)))); |
| 218 | mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader); |
| 219 | ASSERT_NE(klass, nullptr); |
| 220 | |
| 221 | std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods()); |
| 222 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 223 | const auto pointer_size = class_linker->GetImagePointerSize(); |
| 224 | for (auto& m : klass->GetDirectMethods(pointer_size)) { |
| 225 | std::string name = PrettyMethod(&m, true); |
| 226 | const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 227 | ASSERT_NE(code, nullptr); |
| 228 | if (expected->find(name) != expected->end()) { |
| 229 | expected->erase(name); |
| 230 | EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code)); |
| 231 | } else { |
| 232 | EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code)); |
| 233 | } |
| 234 | } |
| 235 | EXPECT_TRUE(expected->empty()); |
| 236 | } |
| 237 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 238 | // TODO: need check-cast test (when stub complete & we can throw/catch |
| 239 | |
| 240 | } // namespace art |