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