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