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