compiler_driver_test.cc revision 31708b736a2d75a9eb21f51038a7703f84f95f31
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> 21#include <memory> 22 23#include "art_method-inl.h" 24#include "class_linker-inl.h" 25#include "common_compiler_test.h" 26#include "dex_file.h" 27#include "gc/heap.h" 28#include "mirror/class-inl.h" 29#include "mirror/class_loader.h" 30#include "mirror/dex_cache-inl.h" 31#include "mirror/object_array-inl.h" 32#include "mirror/object-inl.h" 33#include "handle_scope-inl.h" 34#include "jit/offline_profiling_info.h" 35#include "scoped_thread_state_change.h" 36 37namespace art { 38 39class CompilerDriverTest : public CommonCompilerTest { 40 protected: 41 void CompileAll(jobject class_loader) REQUIRES(!Locks::mutator_lock_) { 42 TimingLogger timings("CompilerDriverTest::CompileAll", false, false); 43 TimingLogger::ScopedTiming t(__FUNCTION__, &timings); 44 compiler_driver_->CompileAll(class_loader, 45 GetDexFiles(class_loader), 46 &timings); 47 t.NewTiming("MakeAllExecutable"); 48 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) 53 REQUIRES(!Locks::mutator_lock_) { 54 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); 60 CHECK(class_ != nullptr) << "Class not found: " << class_name; 61 if (is_virtual) { 62 mid_ = env_->GetMethodID(class_, method, signature); 63 } else { 64 mid_ = env_->GetStaticMethodID(class_, method, signature); 65 } 66 CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature; 67 } 68 69 void MakeAllExecutable(jobject class_loader) { 70 const std::vector<const DexFile*> class_path = GetDexFiles(class_loader); 71 for (size_t i = 0; i != class_path.size(); ++i) { 72 const DexFile* dex_file = class_path[i]; 73 CHECK(dex_file != nullptr); 74 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()); 84 StackHandleScope<1> hs(soa.Self()); 85 Handle<mirror::ClassLoader> loader( 86 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader))); 87 mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader); 88 CHECK(c != nullptr); 89 const auto pointer_size = class_linker->GetImagePointerSize(); 90 for (auto& m : c->GetMethods(pointer_size)) { 91 MakeExecutable(&m); 92 } 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) { 103 CompileAll(nullptr); 104 105 // All libcore references should resolve 106 ScopedObjectAccess soa(Thread::Current()); 107 ASSERT_TRUE(java_lang_dex_file_ != nullptr); 108 const DexFile& dex = *java_lang_dex_file_; 109 mirror::DexCache* dex_cache = class_linker_->FindDexCache(soa.Self(), dex); 110 EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings()); 111 for (size_t i = 0; i < dex_cache->NumStrings(); i++) { 112 const mirror::String* string = dex_cache->GetResolvedString(i); 113 EXPECT_TRUE(string != nullptr) << "string_idx=" << i; 114 } 115 EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes()); 116 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { 117 mirror::Class* type = dex_cache->GetResolvedType(i); 118 EXPECT_TRUE(type != nullptr) << "type_idx=" << i 119 << " " << dex.GetTypeDescriptor(dex.GetTypeId(i)); 120 } 121 EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods()); 122 auto* cl = Runtime::Current()->GetClassLinker(); 123 auto pointer_size = cl->GetImagePointerSize(); 124 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { 125 ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size); 126 EXPECT_TRUE(method != nullptr) << "method_idx=" << i 127 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) 128 << " " << dex.GetMethodName(dex.GetMethodId(i)); 129 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i 130 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " " 131 << dex.GetMethodName(dex.GetMethodId(i)); 132 } 133 EXPECT_EQ(dex.NumFieldIds(), dex_cache->NumResolvedFields()); 134 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { 135 ArtField* field = cl->GetResolvedField(i, dex_cache); 136 EXPECT_TRUE(field != nullptr) << "field_idx=" << i 137 << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i)) 138 << " " << dex.GetFieldName(dex.GetFieldId(i)); 139 } 140 141 // TODO check Class::IsVerified for all classes 142 143 // TODO: check that all Method::GetCode() values are non-null 144} 145 146TEST_F(CompilerDriverTest, DISABLED_AbstractMethodErrorStub) { 147 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK(); 148 TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK(); 149 TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS(); 150 jobject class_loader; 151 { 152 ScopedObjectAccess soa(Thread::Current()); 153 CompileVirtualMethod(ScopedNullHandle<mirror::ClassLoader>(), 154 "java.lang.Class", 155 "isFinalizable", 156 "()Z"); 157 CompileDirectMethod(ScopedNullHandle<mirror::ClassLoader>(), 158 "java.lang.Object", 159 "<init>", 160 "()V"); 161 class_loader = LoadDex("AbstractMethod"); 162 } 163 ASSERT_TRUE(class_loader != nullptr); 164 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true); 165 166 // Create a jobj_ of ConcreteClass, NOT AbstractClass. 167 jclass c_class = env_->FindClass("ConcreteClass"); 168 169 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V"); 170 171 jobject jobj_ = env_->NewObject(c_class, constructor); 172 ASSERT_TRUE(jobj_ != nullptr); 173 174 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception. 175 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_); 176 177 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE); 178 jthrowable exception = env_->ExceptionOccurred(); 179 env_->ExceptionClear(); 180 jclass jlame = env_->FindClass("java/lang/AbstractMethodError"); 181 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame)); 182 { 183 ScopedObjectAccess soa(Thread::Current()); 184 Thread::Current()->ClearException(); 185 } 186} 187 188class CompilerDriverMethodsTest : public CompilerDriverTest { 189 protected: 190 std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE { 191 return new std::unordered_set<std::string>({ 192 "byte StaticLeafMethods.identity(byte)", 193 "int StaticLeafMethods.sum(int, int, int)", 194 "double StaticLeafMethods.sum(double, double, double, double)" 195 }); 196 } 197}; 198 199TEST_F(CompilerDriverMethodsTest, Selection) { 200 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK(); 201 TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK(); 202 TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS(); 203 Thread* self = Thread::Current(); 204 jobject class_loader; 205 { 206 ScopedObjectAccess soa(self); 207 class_loader = LoadDex("StaticLeafMethods"); 208 } 209 ASSERT_NE(class_loader, nullptr); 210 211 // Need to enable dex-file writability. Methods rejected to be compiled will run through the 212 // dex-to-dex compiler. 213 for (const DexFile* dex_file : GetDexFiles(class_loader)) { 214 ASSERT_TRUE(dex_file->EnableWrite()); 215 } 216 217 CompileAll(class_loader); 218 219 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 220 ScopedObjectAccess soa(self); 221 StackHandleScope<1> hs(self); 222 Handle<mirror::ClassLoader> h_loader(hs.NewHandle( 223 reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader)))); 224 mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader); 225 ASSERT_NE(klass, nullptr); 226 227 std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods()); 228 229 const auto pointer_size = class_linker->GetImagePointerSize(); 230 for (auto& m : klass->GetDirectMethods(pointer_size)) { 231 std::string name = PrettyMethod(&m, true); 232 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); 233 ASSERT_NE(code, nullptr); 234 if (expected->find(name) != expected->end()) { 235 expected->erase(name); 236 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code)); 237 } else { 238 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code)); 239 } 240 } 241 EXPECT_TRUE(expected->empty()); 242} 243 244class CompilerDriverProfileTest : public CompilerDriverTest { 245 protected: 246 ProfileCompilationInfo* GetProfileCompilationInfo() OVERRIDE { 247 ScopedObjectAccess soa(Thread::Current()); 248 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex"); 249 250 ProfileCompilationInfo info; 251 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { 252 std::string key = ProfileCompilationInfo::GetProfileDexFileKey(dex_file->GetLocation()); 253 profile_info_.AddData(key, dex_file->GetLocationChecksum(), 1); 254 profile_info_.AddData(key, dex_file->GetLocationChecksum(), 2); 255 } 256 return &profile_info_; 257 } 258 259 std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) { 260 if (clazz == "Main") { 261 return std::unordered_set<std::string>({ 262 "java.lang.String Main.getA()", 263 "java.lang.String Main.getB()"}); 264 } else if (clazz == "Second") { 265 return std::unordered_set<std::string>({ 266 "java.lang.String Second.getX()", 267 "java.lang.String Second.getY()"}); 268 } else { 269 return std::unordered_set<std::string>(); 270 } 271 } 272 273 void CheckCompiledMethods(jobject class_loader, 274 const std::string& clazz, 275 const std::unordered_set<std::string>& expected_methods) { 276 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 277 Thread* self = Thread::Current(); 278 ScopedObjectAccess soa(self); 279 StackHandleScope<1> hs(self); 280 Handle<mirror::ClassLoader> h_loader(hs.NewHandle( 281 reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader)))); 282 mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader); 283 ASSERT_NE(klass, nullptr); 284 285 const auto pointer_size = class_linker->GetImagePointerSize(); 286 size_t number_of_compiled_methods = 0; 287 for (auto& m : klass->GetVirtualMethods(pointer_size)) { 288 std::string name = PrettyMethod(&m, true); 289 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); 290 ASSERT_NE(code, nullptr); 291 if (expected_methods.find(name) != expected_methods.end()) { 292 number_of_compiled_methods++; 293 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code)); 294 } else { 295 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code)); 296 } 297 } 298 EXPECT_EQ(expected_methods.size(), number_of_compiled_methods); 299 } 300 301 private: 302 ProfileCompilationInfo profile_info_; 303}; 304 305TEST_F(CompilerDriverProfileTest, ProfileGuidedCompilation) { 306 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING_WITH_QUICK(); 307 TEST_DISABLED_FOR_READ_BARRIER_WITH_QUICK(); 308 TEST_DISABLED_FOR_READ_BARRIER_WITH_OPTIMIZING_FOR_UNSUPPORTED_INSTRUCTION_SETS(); 309 Thread* self = Thread::Current(); 310 jobject class_loader; 311 { 312 ScopedObjectAccess soa(self); 313 class_loader = LoadDex("ProfileTestMultiDex"); 314 } 315 ASSERT_NE(class_loader, nullptr); 316 317 // Need to enable dex-file writability. Methods rejected to be compiled will run through the 318 // dex-to-dex compiler. 319 ProfileCompilationInfo info; 320 for (const DexFile* dex_file : GetDexFiles(class_loader)) { 321 ASSERT_TRUE(dex_file->EnableWrite()); 322 } 323 324 CompileAll(class_loader); 325 326 std::unordered_set<std::string> m = GetExpectedMethodsForClass("Main"); 327 std::unordered_set<std::string> s = GetExpectedMethodsForClass("Second"); 328 CheckCompiledMethods(class_loader, "LMain;", m); 329 CheckCompiledMethods(class_loader, "LSecond;", s); 330} 331 332// TODO: need check-cast test (when stub complete & we can throw/catch 333 334} // namespace art 335