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
32 namespace art {
33
34 class OatTest : public CommonCompilerTest {
35 protected:
36 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
37
CheckMethod(mirror::ArtMethod * method,const OatFile::OatMethod & oat_method,const DexFile * dex_file)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 SwapVector<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 SwapVector<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
TEST_F(OatTest,WriteRead)87 TEST_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, nullptr, 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 0,
124 compiler_driver_.get(),
125 &timings,
126 &key_value_store);
127 bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
128 !kIsTargetBuild,
129 class_linker->GetBootClassPath(),
130 &oat_writer,
131 tmp.GetFile());
132 ASSERT_TRUE(success);
133
134 if (kCompile) { // OatWriter strips the code, regenerate to compare
135 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
136 }
137 std::string error_msg;
138 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), nullptr,
139 nullptr, false, &error_msg));
140 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
141 const OatHeader& oat_header = oat_file->GetOatHeader();
142 ASSERT_TRUE(oat_header.IsValid());
143 ASSERT_EQ(1U, oat_header.GetDexFileCount()); // core
144 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
145 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
146 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
147
148 const DexFile* dex_file = java_lang_dex_file_;
149 uint32_t dex_file_checksum = dex_file->GetLocationChecksum();
150 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation().c_str(),
151 &dex_file_checksum);
152 ASSERT_TRUE(oat_dex_file != nullptr);
153 CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
154 for (size_t i = 0; i < dex_file->NumClassDefs(); i++) {
155 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
156 const byte* class_data = dex_file->GetClassData(class_def);
157 size_t num_virtual_methods = 0;
158 if (class_data != NULL) {
159 ClassDataItemIterator it(*dex_file, class_data);
160 num_virtual_methods = it.NumVirtualMethods();
161 }
162 const char* descriptor = dex_file->GetClassDescriptor(class_def);
163 StackHandleScope<1> hs(soa.Self());
164 mirror::Class* klass = class_linker->FindClass(soa.Self(), descriptor,
165 NullHandle<mirror::ClassLoader>());
166
167 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
168 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
169 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
170 oat_class.GetType()) << descriptor;
171
172 size_t method_index = 0;
173 for (size_t i = 0; i < klass->NumDirectMethods(); i++, method_index++) {
174 CheckMethod(klass->GetDirectMethod(i),
175 oat_class.GetOatMethod(method_index), dex_file);
176 }
177 for (size_t i = 0; i < num_virtual_methods; i++, method_index++) {
178 CheckMethod(klass->GetVirtualMethod(i),
179 oat_class.GetOatMethod(method_index), dex_file);
180 }
181 }
182 }
183
TEST_F(OatTest,OatHeaderSizeCheck)184 TEST_F(OatTest, OatHeaderSizeCheck) {
185 // If this test is failing and you have to update these constants,
186 // it is time to update OatHeader::kOatVersion
187 EXPECT_EQ(84U, sizeof(OatHeader));
188 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
189 EXPECT_EQ(28U, sizeof(OatQuickMethodHeader));
190 EXPECT_EQ(79 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints));
191 }
192
TEST_F(OatTest,OatHeaderIsValid)193 TEST_F(OatTest, OatHeaderIsValid) {
194 InstructionSet instruction_set = kX86;
195 InstructionSetFeatures instruction_set_features;
196 std::vector<const DexFile*> dex_files;
197 uint32_t image_file_location_oat_checksum = 0;
198 uint32_t image_file_location_oat_begin = 0;
199 OatHeader* oat_header = OatHeader::Create(instruction_set,
200 instruction_set_features,
201 &dex_files,
202 image_file_location_oat_checksum,
203 image_file_location_oat_begin,
204 nullptr);
205 ASSERT_NE(oat_header, nullptr);
206 ASSERT_TRUE(oat_header->IsValid());
207
208 char* magic = const_cast<char*>(oat_header->GetMagic());
209 strcpy(magic, ""); // bad magic
210 ASSERT_FALSE(oat_header->IsValid());
211 strcpy(magic, "oat\n000"); // bad version
212 ASSERT_FALSE(oat_header->IsValid());
213 }
214
215 } // namespace art
216