oat_test.cc revision d9cb8ae2ed78f957a773af61759432d7a7bf78af
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.h"
19#include "dex/verification_results.h"
20#include "dex/quick/dex_file_to_method_inliner_map.h"
21#include "dex/quick_compiler_callbacks.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23#include "implicit_check_options.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 == NULL) {
48      EXPECT_TRUE(oat_method.GetQuickCode() == NULL) << PrettyMethod(method) << " "
49                                                     << oat_method.GetQuickCode();
50      EXPECT_TRUE(oat_method.GetPortableCode() == NULL) << 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  InstructionSetFeatures insn_features;
99  compiler_options_.reset(new CompilerOptions);
100  verification_results_.reset(new VerificationResults(compiler_options_.get()));
101  method_inliner_map_.reset(new DexFileToMethodInlinerMap);
102  callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
103                                              method_inliner_map_.get()));
104  timer_.reset(new CumulativeLogger("Compilation times"));
105  compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
106                                            verification_results_.get(),
107                                            method_inliner_map_.get(),
108                                            compiler_kind, insn_set,
109                                            insn_features, false, NULL, 2, true, true,
110                                            timer_.get()));
111  jobject class_loader = NULL;
112  if (kCompile) {
113    TimingLogger timings("OatTest::WriteRead", false, false);
114    compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
115  }
116
117  ScopedObjectAccess soa(Thread::Current());
118  ScratchFile tmp;
119  SafeMap<std::string, std::string> key_value_store;
120  key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
121  key_value_store.Put(ImplicitCheckOptions::kImplicitChecksOatHeaderKey,
122                      ImplicitCheckOptions::Serialize(true, true, true));
123  OatWriter oat_writer(class_linker->GetBootClassPath(),
124                       42U,
125                       4096U,
126                       compiler_driver_.get(),
127                       &timings,
128                       &key_value_store);
129  bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
130                                            !kIsTargetBuild,
131                                            class_linker->GetBootClassPath(),
132                                            &oat_writer,
133                                            tmp.GetFile());
134  ASSERT_TRUE(success);
135
136  if (kCompile) {  // OatWriter strips the code, regenerate to compare
137    compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
138  }
139  std::string error_msg;
140  std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), NULL, false,
141                                            &error_msg));
142  ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
143  const OatHeader& oat_header = oat_file->GetOatHeader();
144  ASSERT_TRUE(oat_header.IsValid());
145  ASSERT_EQ(1U, oat_header.GetDexFileCount());  // core
146  ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
147  ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
148  ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
149
150  const DexFile* dex_file = java_lang_dex_file_;
151  uint32_t dex_file_checksum = dex_file->GetLocationChecksum();
152  const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation().c_str(),
153                                                                    &dex_file_checksum);
154  ASSERT_TRUE(oat_dex_file != nullptr);
155  CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
156  for (size_t i = 0; i < dex_file->NumClassDefs(); i++) {
157    const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
158    const byte* class_data = dex_file->GetClassData(class_def);
159    size_t num_virtual_methods = 0;
160    if (class_data != NULL) {
161      ClassDataItemIterator it(*dex_file, class_data);
162      num_virtual_methods = it.NumVirtualMethods();
163    }
164    const char* descriptor = dex_file->GetClassDescriptor(class_def);
165    StackHandleScope<1> hs(soa.Self());
166    mirror::Class* klass = class_linker->FindClass(soa.Self(), descriptor,
167                                                   NullHandle<mirror::ClassLoader>());
168
169    const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
170    CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
171    CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
172             oat_class.GetType()) << descriptor;
173
174    size_t method_index = 0;
175    for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
176      CheckMethod(klass->GetDirectMethod(i),
177                  oat_class.GetOatMethod(method_index), dex_file);
178    }
179    for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
180      CheckMethod(klass->GetVirtualMethod(i),
181                  oat_class.GetOatMethod(method_index), dex_file);
182    }
183  }
184}
185
186TEST_F(OatTest, OatHeaderSizeCheck) {
187  // If this test is failing and you have to update these constants,
188  // it is time to update OatHeader::kOatVersion
189  EXPECT_EQ(80U, sizeof(OatHeader));
190  EXPECT_EQ(8U, sizeof(OatMethodOffsets));
191  EXPECT_EQ(24U, sizeof(OatQuickMethodHeader));
192  EXPECT_EQ(79 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints));
193}
194
195TEST_F(OatTest, OatHeaderIsValid) {
196    InstructionSet instruction_set = kX86;
197    InstructionSetFeatures instruction_set_features;
198    std::vector<const DexFile*> dex_files;
199    uint32_t image_file_location_oat_checksum = 0;
200    uint32_t image_file_location_oat_begin = 0;
201    OatHeader* oat_header = OatHeader::Create(instruction_set,
202                                              instruction_set_features,
203                                              &dex_files,
204                                              image_file_location_oat_checksum,
205                                              image_file_location_oat_begin,
206                                              nullptr);
207    ASSERT_NE(oat_header, nullptr);
208    ASSERT_TRUE(oat_header->IsValid());
209
210    char* magic = const_cast<char*>(oat_header->GetMagic());
211    strcpy(magic, "");  // bad magic
212    ASSERT_FALSE(oat_header->IsValid());
213    strcpy(magic, "oat\n000");  // bad version
214    ASSERT_FALSE(oat_header->IsValid());
215}
216
217}  // namespace art
218