oat_test.cc revision 928f72bd75c385ba2708c58521171a77264d4486
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 == 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  OatWriter oat_writer(class_linker->GetBootClassPath(),
122                       42U,
123                       4096U,
124                       0,
125                       compiler_driver_.get(),
126                       &timings,
127                       &key_value_store);
128  bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
129                                            !kIsTargetBuild,
130                                            class_linker->GetBootClassPath(),
131                                            &oat_writer,
132                                            tmp.GetFile());
133  ASSERT_TRUE(success);
134
135  if (kCompile) {  // OatWriter strips the code, regenerate to compare
136    compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
137  }
138  std::string error_msg;
139  std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), NULL, false,
140                                            &error_msg));
141  ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
142  const OatHeader& oat_header = oat_file->GetOatHeader();
143  ASSERT_TRUE(oat_header.IsValid());
144  ASSERT_EQ(1U, oat_header.GetDexFileCount());  // core
145  ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
146  ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
147  ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
148
149  const DexFile* dex_file = java_lang_dex_file_;
150  uint32_t dex_file_checksum = dex_file->GetLocationChecksum();
151  const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation().c_str(),
152                                                                    &dex_file_checksum);
153  ASSERT_TRUE(oat_dex_file != nullptr);
154  CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
155  for (size_t i = 0; i < dex_file->NumClassDefs(); i++) {
156    const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
157    const byte* class_data = dex_file->GetClassData(class_def);
158    size_t num_virtual_methods = 0;
159    if (class_data != NULL) {
160      ClassDataItemIterator it(*dex_file, class_data);
161      num_virtual_methods = it.NumVirtualMethods();
162    }
163    const char* descriptor = dex_file->GetClassDescriptor(class_def);
164    StackHandleScope<1> hs(soa.Self());
165    mirror::Class* klass = class_linker->FindClass(soa.Self(), descriptor,
166                                                   NullHandle<mirror::ClassLoader>());
167
168    const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
169    CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
170    CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
171             oat_class.GetType()) << descriptor;
172
173    size_t method_index = 0;
174    for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
175      CheckMethod(klass->GetDirectMethod(i),
176                  oat_class.GetOatMethod(method_index), dex_file);
177    }
178    for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
179      CheckMethod(klass->GetVirtualMethod(i),
180                  oat_class.GetOatMethod(method_index), dex_file);
181    }
182  }
183}
184
185TEST_F(OatTest, OatHeaderSizeCheck) {
186  // If this test is failing and you have to update these constants,
187  // it is time to update OatHeader::kOatVersion
188  EXPECT_EQ(84U, sizeof(OatHeader));
189  EXPECT_EQ(8U, sizeof(OatMethodOffsets));
190  EXPECT_EQ(24U, sizeof(OatQuickMethodHeader));
191  EXPECT_EQ(91 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints));
192}
193
194TEST_F(OatTest, OatHeaderIsValid) {
195    InstructionSet instruction_set = kX86;
196    InstructionSetFeatures instruction_set_features;
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(instruction_set,
201                                                            instruction_set_features,
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