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