oat_test.cc revision 22f8e5c82d12951be38cd893426e13bee33fd69d
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 "common_compiler_test.h" 18#include "compiler/compiler.h" 19#include "compiler/oat_writer.h" 20#include "entrypoints/quick/quick_entrypoints.h" 21#include "implicit_check_options.h" 22#include "mirror/art_method-inl.h" 23#include "mirror/class-inl.h" 24#include "mirror/object-inl.h" 25#include "mirror/object_array-inl.h" 26#include "oat_file-inl.h" 27#include "vector_output_stream.h" 28 29namespace art { 30 31class OatTest : public CommonCompilerTest { 32 protected: 33 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore 34 35 void CheckMethod(mirror::ArtMethod* method, 36 const OatFile::OatMethod& oat_method, 37 const DexFile* dex_file) 38 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 39 const CompiledMethod* compiled_method = 40 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, 41 method->GetDexMethodIndex())); 42 43 if (compiled_method == NULL) { 44 EXPECT_TRUE(oat_method.GetQuickCode() == NULL) << PrettyMethod(method) << " " 45 << oat_method.GetQuickCode(); 46 EXPECT_TRUE(oat_method.GetPortableCode() == NULL) << PrettyMethod(method) << " " 47 << oat_method.GetPortableCode(); 48 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U); 49 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U); 50 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U); 51 } else { 52 const void* quick_oat_code = oat_method.GetQuickCode(); 53 if (quick_oat_code != nullptr) { 54 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes()); 55 EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask()); 56 EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask()); 57 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2); 58 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned); 59 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); 60 EXPECT_TRUE(quick_code != nullptr); 61 size_t code_size = quick_code->size() * sizeof(quick_code[0]); 62 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size)) 63 << PrettyMethod(method) << " " << code_size; 64 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size)); 65 } else { 66 const void* portable_oat_code = oat_method.GetPortableCode(); 67 EXPECT_TRUE(portable_oat_code != nullptr) << PrettyMethod(method); 68 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U); 69 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U); 70 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U); 71 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(portable_oat_code), 2); 72 portable_oat_code = reinterpret_cast<const void*>(oat_code_aligned); 73 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode(); 74 EXPECT_TRUE(portable_code != nullptr); 75 size_t code_size = portable_code->size() * sizeof(portable_code[0]); 76 EXPECT_EQ(0, memcmp(quick_oat_code, &portable_code[0], code_size)) 77 << PrettyMethod(method) << " " << code_size; 78 CHECK_EQ(0, memcmp(quick_oat_code, &portable_code[0], code_size)); 79 } 80 } 81 } 82}; 83 84TEST_F(OatTest, WriteRead) { 85 TimingLogger timings("OatTest::WriteRead", false, false); 86 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 87 88 // TODO: make selectable. 89 Compiler::Kind compiler_kind = kUsePortableCompiler 90 ? Compiler::kPortable 91 : Compiler::kQuick; 92 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86; 93 94 InstructionSetFeatures insn_features; 95 compiler_options_.reset(new CompilerOptions); 96 verification_results_.reset(new VerificationResults(compiler_options_.get())); 97 method_inliner_map_.reset(new DexFileToMethodInlinerMap); 98 callbacks_.reset(new CompilerCallbacksImpl(verification_results_.get(), 99 method_inliner_map_.get())); 100 timer_.reset(new CumulativeLogger("Compilation times")); 101 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(), 102 verification_results_.get(), 103 method_inliner_map_.get(), 104 compiler_kind, insn_set, 105 insn_features, false, NULL, 2, true, true, 106 timer_.get())); 107 jobject class_loader = NULL; 108 if (kCompile) { 109 TimingLogger timings("OatTest::WriteRead", false, false); 110 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings); 111 } 112 113 ScopedObjectAccess soa(Thread::Current()); 114 ScratchFile tmp; 115 SafeMap<std::string, std::string> key_value_store; 116 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art"); 117 key_value_store.Put(ImplicitCheckOptions::kImplicitChecksOatHeaderKey, 118 ImplicitCheckOptions::Serialize(true, true, true)); 119 OatWriter oat_writer(class_linker->GetBootClassPath(), 120 42U, 121 4096U, 122 compiler_driver_.get(), 123 &timings, 124 &key_value_store); 125 bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(), 126 !kIsTargetBuild, 127 class_linker->GetBootClassPath(), 128 &oat_writer, 129 tmp.GetFile()); 130 ASSERT_TRUE(success); 131 132 if (kCompile) { // OatWriter strips the code, regenerate to compare 133 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings); 134 } 135 std::string error_msg; 136 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), NULL, false, 137 &error_msg)); 138 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg; 139 const OatHeader& oat_header = oat_file->GetOatHeader(); 140 ASSERT_TRUE(oat_header.IsValid()); 141 ASSERT_EQ(1U, oat_header.GetDexFileCount()); // core 142 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum()); 143 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin()); 144 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey))); 145 146 const DexFile* dex_file = java_lang_dex_file_; 147 uint32_t dex_file_checksum = dex_file->GetLocationChecksum(); 148 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation().c_str(), 149 &dex_file_checksum); 150 ASSERT_TRUE(oat_dex_file != nullptr); 151 CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); 152 for (size_t i = 0; i < dex_file->NumClassDefs(); i++) { 153 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); 154 const byte* class_data = dex_file->GetClassData(class_def); 155 size_t num_virtual_methods = 0; 156 if (class_data != NULL) { 157 ClassDataItemIterator it(*dex_file, class_data); 158 num_virtual_methods = it.NumVirtualMethods(); 159 } 160 const char* descriptor = dex_file->GetClassDescriptor(class_def); 161 StackHandleScope<1> hs(soa.Self()); 162 mirror::Class* klass = class_linker->FindClass(soa.Self(), descriptor, 163 NullHandle<mirror::ClassLoader>()); 164 165 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i); 166 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor; 167 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled, 168 oat_class.GetType()) << descriptor; 169 170 size_t method_index = 0; 171 for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) { 172 CheckMethod(klass->GetDirectMethod(i), 173 oat_class.GetOatMethod(method_index), dex_file); 174 } 175 for (size_t i = 0; i < num_virtual_methods; i++, method_index++) { 176 CheckMethod(klass->GetVirtualMethod(i), 177 oat_class.GetOatMethod(method_index), dex_file); 178 } 179 } 180} 181 182TEST_F(OatTest, OatHeaderSizeCheck) { 183 // If this test is failing and you have to update these constants, 184 // it is time to update OatHeader::kOatVersion 185 EXPECT_EQ(80U, sizeof(OatHeader)); 186 EXPECT_EQ(8U, sizeof(OatMethodOffsets)); 187 EXPECT_EQ(24U, sizeof(OatQuickMethodHeader)); 188 EXPECT_EQ(77 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints)); 189} 190 191TEST_F(OatTest, OatHeaderIsValid) { 192 InstructionSet instruction_set = kX86; 193 InstructionSetFeatures instruction_set_features; 194 std::vector<const DexFile*> dex_files; 195 uint32_t image_file_location_oat_checksum = 0; 196 uint32_t image_file_location_oat_begin = 0; 197 OatHeader* oat_header = OatHeader::Create(instruction_set, 198 instruction_set_features, 199 &dex_files, 200 image_file_location_oat_checksum, 201 image_file_location_oat_begin, 202 nullptr); 203 ASSERT_NE(oat_header, nullptr); 204 ASSERT_TRUE(oat_header->IsValid()); 205 206 char* magic = const_cast<char*>(oat_header->GetMagic()); 207 strcpy(magic, ""); // bad magic 208 ASSERT_FALSE(oat_header->IsValid()); 209 strcpy(magic, "oat\n000"); // bad version 210 ASSERT_FALSE(oat_header->IsValid()); 211} 212 213} // namespace art 214