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