blob: a1988244c74aaf71f9af553865149da488f24952 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 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 "oat_file_assistant.h"
18
19#include <algorithm>
20#include <fstream>
21#include <string>
22#include <vector>
23#include <sys/param.h>
24
25#include <backtrace/BacktraceMap.h>
26#include <gtest/gtest.h>
27
28#include "class_linker.h"
29#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070030#include "compiler_callbacks.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "mem_map.h"
32#include "os.h"
33#include "thread-inl.h"
34#include "utils.h"
35
36namespace art {
37
38class OatFileAssistantTest : public CommonRuntimeTest {
39 public:
40 virtual void SetUp() {
41 ReserveImageSpace();
42 CommonRuntimeTest::SetUp();
43
44 // Create a scratch directory to work from.
45 scratch_dir_ = android_data_ + "/OatFileAssistantTest";
46 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
47
Richard Uhler63434112015-03-16 14:32:16 -070048 // Create a subdirectory in scratch for odex files.
49 odex_oat_dir_ = scratch_dir_ + "/oat";
50 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
51
52 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
53 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
54
Richard Uhler66d874d2015-01-15 09:37:19 -080055
56 // Verify the environment is as we expect
57 uint32_t checksum;
58 std::string error_msg;
59 ASSERT_TRUE(OS::FileExists(GetImageFile().c_str()))
60 << "Expected pre-compiled boot image to be at: " << GetImageFile();
61 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
62 << "Expected dex file to be at: " << GetDexSrc1();
63 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
64 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
65 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
66 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
67 ASSERT_TRUE(OS::FileExists(GetMultiDexSrc1().c_str()))
68 << "Expected multidex file to be at: " << GetMultiDexSrc1();
69 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
70 << "Expected dex file to be at: " << GetDexSrc2();
71 }
72
73 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +000074 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -080075
76 // Set up the image location.
77 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
78 nullptr));
79 // Make sure compilercallbacks are not set so that relocation will be
80 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -070081 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -080082 }
83
84 virtual void PreRuntimeCreate() {
85 UnreserveImageSpace();
86 }
87
88 virtual void PostRuntimeCreate() {
89 ReserveImageSpace();
90 }
91
92 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -070093 ClearDirectory(odex_dir_.c_str());
94 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
95
96 ClearDirectory(odex_oat_dir_.c_str());
97 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -080098
99 ClearDirectory(scratch_dir_.c_str());
100 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
101
102 CommonRuntimeTest::TearDown();
103 }
104
105 void Copy(std::string src, std::string dst) {
106 std::ifstream src_stream(src, std::ios::binary);
107 std::ofstream dst_stream(dst, std::ios::binary);
108
109 dst_stream << src_stream.rdbuf();
110 }
111
112 // Returns the directory where the pre-compiled core.art can be found.
113 // TODO: We should factor out this into common tests somewhere rather than
114 // re-hardcoding it here (This was copied originally from the elf writer
115 // test).
116 std::string GetImageDirectory() {
117 if (IsHost()) {
118 const char* host_dir = getenv("ANDROID_HOST_OUT");
119 CHECK(host_dir != NULL);
120 return std::string(host_dir) + "/framework";
121 } else {
122 return std::string("/data/art-test");
123 }
124 }
125
126 std::string GetImageLocation() {
127 return GetImageDirectory() + "/core.art";
128 }
129
130 std::string GetImageFile() {
131 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
132 + "/core.art";
133 }
134
135 std::string GetDexSrc1() {
136 return GetTestDexFileName("Main");
137 }
138
139 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
140 // file stripped.
141 std::string GetStrippedDexSrc1() {
142 return GetTestDexFileName("MainStripped");
143 }
144
145 std::string GetMultiDexSrc1() {
146 return GetTestDexFileName("MultiDex");
147 }
148
149 std::string GetDexSrc2() {
150 return GetTestDexFileName("Nested");
151 }
152
153 // Scratch directory, for dex and odex files (oat files will go in the
154 // dalvik cache).
155 std::string GetScratchDir() {
156 return scratch_dir_;
157 }
158
Richard Uhler63434112015-03-16 14:32:16 -0700159 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800160 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700161 std::string GetOdexDir() {
162 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800163 }
164
165 // Generate an odex file for the purposes of test.
166 // If pic is true, generates a PIC odex.
167 void GenerateOdexForTest(const std::string& dex_location,
168 const std::string& odex_location,
169 bool pic = false) {
170 // For this operation, we temporarily redirect the dalvik cache so dex2oat
171 // doesn't find the relocated image file.
172 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
173 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
174 std::vector<std::string> args;
175 args.push_back("--dex-file=" + dex_location);
176 args.push_back("--oat-file=" + odex_location);
177 if (pic) {
178 args.push_back("--compile-pic");
179 } else {
180 args.push_back("--include-patch-information");
Richard Uhler05dd8a62015-03-10 10:02:23 -0700181
182 // We need to use the quick compiler to generate non-PIC code, because
183 // the optimizing compiler always generates PIC.
184 args.push_back("--compiler-backend=Quick");
Richard Uhler66d874d2015-01-15 09:37:19 -0800185 }
186 args.push_back("--runtime-arg");
187 args.push_back("-Xnorelocate");
188 std::string error_msg;
189 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
190 setenv("ANDROID_DATA", android_data_.c_str(), 1);
191 }
192
193 void GeneratePicOdexForTest(const std::string& dex_location,
194 const std::string& odex_location) {
195 GenerateOdexForTest(dex_location, odex_location, true);
196 }
197
198 private:
199 // Reserve memory around where the image will be loaded so other memory
200 // won't conflict when it comes time to load the image.
201 // This can be called with an already loaded image to reserve the space
202 // around it.
203 void ReserveImageSpace() {
204 MemMap::Init();
205
206 // Ensure a chunk of memory is reserved for the image space.
207 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
208 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700209 // Include the main space that has to come right after the
210 // image in case of the GSS collector.
211 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800212
Richard Uhler66d874d2015-01-15 09:37:19 -0800213 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
214 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
215 for (BacktraceMap::const_iterator it = map->begin();
216 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700217 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
218 reservation_start = std::max(reservation_start, it->end);
219 }
220 ReserveImageSpaceChunk(reservation_start, reservation_end);
221 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800222
Richard Uhler3efe9792015-03-30 16:18:03 -0700223 // Reserve a chunk of memory for the image space in the given range.
224 // Only has effect for chunks with a positive number of bytes.
225 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
226 if (start < end) {
227 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800228 image_reservation_.push_back(std::unique_ptr<MemMap>(
229 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700230 reinterpret_cast<uint8_t*>(start), end - start,
Richard Uhler66d874d2015-01-15 09:37:19 -0800231 PROT_NONE, false, false, &error_msg)));
232 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
233 LOG(INFO) << "Reserved space for image " <<
234 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
235 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800236 }
237 }
238
239
240 // Unreserve any memory reserved by ReserveImageSpace. This should be called
241 // before the image is loaded.
242 void UnreserveImageSpace() {
243 image_reservation_.clear();
244 }
245
246 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700247 std::string odex_oat_dir_;
248 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800249 std::vector<std::unique_ptr<MemMap>> image_reservation_;
250};
251
252class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
253 public:
254 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
255 OatFileAssistantTest::SetUpRuntimeOptions(options);
256 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
257 }
258};
259
260// Generate an oat file for the purposes of test, as opposed to testing
261// generation of oat files.
262static void GenerateOatForTest(const char* dex_location) {
263 OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false);
264
265 std::string error_msg;
266 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
267}
268
269// Case: We have a DEX file, but no OAT file for it.
270// Expect: The oat file status is kOutOfDate.
271TEST_F(OatFileAssistantTest, DexNoOat) {
272 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
273 Copy(GetDexSrc1(), dex_location);
274
275 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
276
277 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
278
279 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
280 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
281 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
282 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
283 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
284 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.OdexFileStatus());
285 EXPECT_FALSE(oat_file_assistant.OatFileExists());
286 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
287 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
288 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
289 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.OatFileStatus());
290}
291
292// Case: We have no DEX file and no OAT file.
293// Expect: Status is out of date. Loading should fail, but not crash.
294TEST_F(OatFileAssistantTest, NoDexNoOat) {
295 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
296
297 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
298
299 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
300 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
301 EXPECT_EQ(nullptr, oat_file.get());
302}
303
304// Case: We have a DEX file and up-to-date OAT file for it.
305// Expect: The oat file status is kUpToDate.
306TEST_F(OatFileAssistantTest, OatUpToDate) {
307 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
308 Copy(GetDexSrc1(), dex_location);
309 GenerateOatForTest(dex_location.c_str());
310
311 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
312
313 EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus());
314 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
315 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
316 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
317 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
318 EXPECT_TRUE(oat_file_assistant.OatFileExists());
319 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
320 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
321 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
322 EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.OatFileStatus());
323}
324
325// Case: We have a MultiDEX file and up-to-date OAT file for it.
326// Expect: The oat file status is kUpToDate.
327TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
328 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
329 Copy(GetMultiDexSrc1(), dex_location);
330 GenerateOatForTest(dex_location.c_str());
331
332 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700333 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
334 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800335 ASSERT_TRUE(oat_file.get() != nullptr);
336 EXPECT_TRUE(oat_file->IsExecutable());
337 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700338 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
339 EXPECT_EQ(2u, dex_files.size());
340}
341
342// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
343// encoded dex locations.
344// Expect: The oat file status is kUpToDate.
345TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
346 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700347 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700348
349 // Create the dex file
350 Copy(GetMultiDexSrc1(), dex_location);
351
352 // Create the oat file with relative encoded dex location.
353 std::vector<std::string> args;
354 args.push_back("--dex-file=" + dex_location);
355 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
356 args.push_back("--oat-file=" + oat_location);
357
358 std::string error_msg;
359 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
360
361 // Verify we can load both dex files.
362 OatFileAssistant oat_file_assistant(dex_location.c_str(),
363 oat_location.c_str(),
364 kRuntimeISA, true);
365 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
366 ASSERT_TRUE(oat_file.get() != nullptr);
367 EXPECT_TRUE(oat_file->IsExecutable());
368 std::vector<std::unique_ptr<const DexFile>> dex_files;
369 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800370 EXPECT_EQ(2u, dex_files.size());
371}
372
373// Case: We have a DEX file and out of date OAT file.
374// Expect: The oat file status is kOutOfDate.
375TEST_F(OatFileAssistantTest, OatOutOfDate) {
376 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
377
378 // We create a dex, generate an oat for it, then overwrite the dex with a
379 // different dex to make the oat out of date.
380 Copy(GetDexSrc1(), dex_location);
381 GenerateOatForTest(dex_location.c_str());
382 Copy(GetDexSrc2(), dex_location);
383
384 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
385 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
386
387 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
388 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
389 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
390 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
391 EXPECT_TRUE(oat_file_assistant.OatFileExists());
392 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
393 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
394}
395
396// Case: We have a DEX file and an ODEX file, but no OAT file.
397// Expect: The oat file status is kNeedsRelocation.
398TEST_F(OatFileAssistantTest, DexOdexNoOat) {
399 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700400 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800401
402 // Create the dex and odex files
403 Copy(GetDexSrc1(), dex_location);
404 GenerateOdexForTest(dex_location, odex_location);
405
406 // Verify the status.
407 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
408
409 EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus());
410
411 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
412 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
413 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
414 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
415 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
416 EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.OdexFileNeedsRelocation());
417 EXPECT_FALSE(oat_file_assistant.OatFileExists());
418 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
419 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
420}
421
422// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
423// Expect: The oat file status is kNeedsRelocation.
424TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
425 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700426 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800427
428 // Create the dex and odex files
429 Copy(GetDexSrc1(), dex_location);
430 GenerateOdexForTest(dex_location, odex_location);
431
432 // Strip the dex file
433 Copy(GetStrippedDexSrc1(), dex_location);
434
435 // Verify the status.
436 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
437
438 EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus());
439
440 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
441 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
442 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
443 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
444 EXPECT_FALSE(oat_file_assistant.OatFileExists());
445 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
446 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
447
448 // Make the oat file up to date.
449 std::string error_msg;
450 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
451
452 EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus());
453
454 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
455 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
456 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
457 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
458 EXPECT_TRUE(oat_file_assistant.OatFileExists());
459 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
460 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
461
462 // Verify we can load the dex files from it.
463 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
464 ASSERT_TRUE(oat_file.get() != nullptr);
465 EXPECT_TRUE(oat_file->IsExecutable());
466 std::vector<std::unique_ptr<const DexFile>> dex_files;
467 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
468 EXPECT_EQ(1u, dex_files.size());
469}
470
471// Case: We have a stripped DEX file, an ODEX file, and an out of date OAT file.
472// Expect: The oat file status is kNeedsRelocation.
473TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
474 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700475 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800476
477 // Create the oat file from a different dex file so it looks out of date.
478 Copy(GetDexSrc2(), dex_location);
479 GenerateOatForTest(dex_location.c_str());
480
481 // Create the odex file
482 Copy(GetDexSrc1(), dex_location);
483 GenerateOdexForTest(dex_location, odex_location);
484
485 // Strip the dex file.
486 Copy(GetStrippedDexSrc1(), dex_location);
487
488 // Verify the status.
489 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
490
491 EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus());
492
493 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
494 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
495 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
496 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
497 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
498 EXPECT_TRUE(oat_file_assistant.OatFileExists());
499 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
500 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
501
502 // Make the oat file up to date.
503 std::string error_msg;
504 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
505
506 EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus());
507
508 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
509 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
510 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
511 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
512 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
513 EXPECT_TRUE(oat_file_assistant.OatFileExists());
514 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
515 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
516 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
517
518 // Verify we can load the dex files from it.
519 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
520 ASSERT_TRUE(oat_file.get() != nullptr);
521 EXPECT_TRUE(oat_file->IsExecutable());
522 std::vector<std::unique_ptr<const DexFile>> dex_files;
523 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
524 EXPECT_EQ(1u, dex_files.size());
525}
526
527// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
528// OAT files both have patch delta of 0.
529// Expect: It shouldn't crash.
530TEST_F(OatFileAssistantTest, OdexOatOverlap) {
531 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700532 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
533 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800534
535 // Create the dex and odex files
536 Copy(GetDexSrc1(), dex_location);
537 GenerateOdexForTest(dex_location, odex_location);
538
539 // Create the oat file by copying the odex so they are located in the same
540 // place in memory.
541 Copy(odex_location, oat_location);
542
543 // Verify things don't go bad.
544 OatFileAssistant oat_file_assistant(dex_location.c_str(),
545 oat_location.c_str(), kRuntimeISA, true);
546
547 EXPECT_EQ(OatFileAssistant::kNeedsRelocation, oat_file_assistant.GetStatus());
548
549 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
550 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
551 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
552 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
553 EXPECT_TRUE(oat_file_assistant.OatFileExists());
554 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
555 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
556
557 // Things aren't relocated, so it should fall back to interpreted.
558 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
559 ASSERT_TRUE(oat_file.get() != nullptr);
560 EXPECT_FALSE(oat_file->IsExecutable());
561 std::vector<std::unique_ptr<const DexFile>> dex_files;
562 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
563 EXPECT_EQ(1u, dex_files.size());
564}
565
566// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
567// Expect: The oat file status is kUpToDate, because PIC needs no relocation.
568TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
569 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700570 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800571
572 // Create the dex and odex files
573 Copy(GetDexSrc1(), dex_location);
574 GeneratePicOdexForTest(dex_location, odex_location);
575
576 // Verify the status.
577 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
578
579 EXPECT_EQ(OatFileAssistant::kUpToDate, oat_file_assistant.GetStatus());
580
581 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
582 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
583 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
584 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
585 EXPECT_FALSE(oat_file_assistant.OatFileExists());
586 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
587 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
588}
589
590// Case: We have a DEX file and up-to-date OAT file for it.
591// Expect: We should load an executable dex file.
592TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
593 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
594
595 Copy(GetDexSrc1(), dex_location);
596 GenerateOatForTest(dex_location.c_str());
597
598 // Load the oat using an oat file assistant.
599 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
600
601 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
602 ASSERT_TRUE(oat_file.get() != nullptr);
603 EXPECT_TRUE(oat_file->IsExecutable());
604 std::vector<std::unique_ptr<const DexFile>> dex_files;
605 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
606 EXPECT_EQ(1u, dex_files.size());
607}
608
609// Case: We have a DEX file and up-to-date OAT file for it.
610// Expect: Loading non-executable should load the oat non-executable.
611TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
612 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
613
614 Copy(GetDexSrc1(), dex_location);
615 GenerateOatForTest(dex_location.c_str());
616
617 // Load the oat using an oat file assistant.
618 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
619
620 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
621 ASSERT_TRUE(oat_file.get() != nullptr);
622 EXPECT_FALSE(oat_file->IsExecutable());
623 std::vector<std::unique_ptr<const DexFile>> dex_files;
624 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
625 EXPECT_EQ(1u, dex_files.size());
626}
627
628// Case: We have a DEX file.
629// Expect: We should load an executable dex file from an alternative oat
630// location.
631TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
632 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
633 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
634
635 Copy(GetDexSrc1(), dex_location);
636
637 OatFileAssistant oat_file_assistant(
638 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
639 std::string error_msg;
640 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
641
642 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
643 ASSERT_TRUE(oat_file.get() != nullptr);
644 EXPECT_TRUE(oat_file->IsExecutable());
645 std::vector<std::unique_ptr<const DexFile>> dex_files;
646 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
647 EXPECT_EQ(1u, dex_files.size());
648
649 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
650
651 // Verify it didn't create an oat in the default location.
652 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
653 EXPECT_FALSE(ofm.OatFileExists());
654}
655
656// Case: Non-existent Dex location.
657// Expect: The dex code is out of date, and trying to update it fails.
658TEST_F(OatFileAssistantTest, NonExsistentDexLocation) {
659 std::string dex_location = GetScratchDir() + "/BadDexLocation.jar";
660
661 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
662
663 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
664 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
665 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
666 EXPECT_FALSE(oat_file_assistant.OatFileExists());
667 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
668 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
669 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
670 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
671
672 std::string error_msg;
673 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
674 EXPECT_FALSE(error_msg.empty());
675}
676
677// Turn an absolute path into a path relative to the current working
678// directory.
679static std::string MakePathRelative(std::string target) {
680 char buf[MAXPATHLEN];
681 std::string cwd = getcwd(buf, MAXPATHLEN);
682
683 // Split the target and cwd paths into components.
684 std::vector<std::string> target_path;
685 std::vector<std::string> cwd_path;
686 Split(target, '/', &target_path);
687 Split(cwd, '/', &cwd_path);
688
689 // Reverse the path components, so we can use pop_back().
690 std::reverse(target_path.begin(), target_path.end());
691 std::reverse(cwd_path.begin(), cwd_path.end());
692
693 // Drop the common prefix of the paths. Because we reversed the path
694 // components, this becomes the common suffix of target_path and cwd_path.
695 while (!target_path.empty() && !cwd_path.empty()
696 && target_path.back() == cwd_path.back()) {
697 target_path.pop_back();
698 cwd_path.pop_back();
699 }
700
701 // For each element of the remaining cwd_path, add '..' to the beginning
702 // of the target path. Because we reversed the path components, we add to
703 // the end of target_path.
704 for (unsigned int i = 0; i < cwd_path.size(); i++) {
705 target_path.push_back("..");
706 }
707
708 // Reverse again to get the right path order, and join to get the result.
709 std::reverse(target_path.begin(), target_path.end());
710 return Join(target_path, '/');
711}
712
713// Case: Non-absolute path to Dex location.
714// Expect: Not sure, but it shouldn't crash.
715TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
716 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
717 Copy(GetDexSrc1(), abs_dex_location);
718
719 std::string dex_location = MakePathRelative(abs_dex_location);
720 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
721
722 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
723 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
724 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
725 EXPECT_FALSE(oat_file_assistant.OatFileExists());
726 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
727 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
728 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
729 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
730}
731
732// Case: Very short, non-existent Dex location.
733// Expect: Dex code is out of date, and trying to update it fails.
734TEST_F(OatFileAssistantTest, ShortDexLocation) {
735 std::string dex_location = "/xx";
736
737 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
738
739 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
740 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
741 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
742 EXPECT_FALSE(oat_file_assistant.OatFileExists());
743 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
744 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
745 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
746 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
747
748 std::string error_msg;
749 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
750 EXPECT_FALSE(error_msg.empty());
751}
752
753// Case: Non-standard extension for dex file.
754// Expect: The oat file status is kOutOfDate.
755TEST_F(OatFileAssistantTest, LongDexExtension) {
756 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
757 Copy(GetDexSrc1(), dex_location);
758
759 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
760
761 EXPECT_EQ(OatFileAssistant::kOutOfDate, oat_file_assistant.GetStatus());
762
763 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
764 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
765 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
766 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
767 EXPECT_FALSE(oat_file_assistant.OatFileExists());
768 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
769 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
770}
771
772// A task to generate a dex location. Used by the RaceToGenerate test.
773class RaceGenerateTask : public Task {
774 public:
775 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
776 : dex_location_(dex_location), oat_location_(oat_location),
777 loaded_oat_file_(nullptr)
778 {}
779
780 void Run(Thread* self) {
781 UNUSED(self);
782
783 // Load the dex files, and save a pointer to the loaded oat file, so that
784 // we can verify only one oat file was loaded for the dex location.
785 ClassLinker* linker = Runtime::Current()->GetClassLinker();
786 std::vector<std::unique_ptr<const DexFile>> dex_files;
787 std::vector<std::string> error_msgs;
788 dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs);
789 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700790 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
791 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800792 }
793
794 const OatFile* GetLoadedOatFile() const {
795 return loaded_oat_file_;
796 }
797
798 private:
799 std::string dex_location_;
800 std::string oat_location_;
801 const OatFile* loaded_oat_file_;
802};
803
804// Test the case where multiple processes race to generate an oat file.
805// This simulates multiple processes using multiple threads.
806//
807// We want only one Oat file to be loaded when there is a race to load, to
808// avoid using up the virtual memory address space.
809TEST_F(OatFileAssistantTest, RaceToGenerate) {
810 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700811 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800812
813 // We use the lib core dex file, because it's large, and hopefully should
814 // take a while to generate.
815 Copy(GetLibCoreDexFileName(), dex_location);
816
817 const int kNumThreads = 32;
818 Thread* self = Thread::Current();
819 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
820 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
821 for (int i = 0; i < kNumThreads; i++) {
822 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
823 thread_pool.AddTask(self, task.get());
824 tasks.push_back(std::move(task));
825 }
826 thread_pool.StartWorkers(self);
827 thread_pool.Wait(self, true, false);
828
829 // Verify every task got the same pointer.
830 const OatFile* expected = tasks[0]->GetLoadedOatFile();
831 for (auto& task : tasks) {
832 EXPECT_EQ(expected, task->GetLoadedOatFile());
833 }
834}
835
836// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
837// disabled.
838// Expect: We should load the odex file non-executable.
839TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
840 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700841 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800842
843 // Create the dex and odex files
844 Copy(GetDexSrc1(), dex_location);
845 GenerateOdexForTest(dex_location, odex_location);
846
847 // Load the oat using an executable oat file assistant.
848 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
849
850 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
851 ASSERT_TRUE(oat_file.get() != nullptr);
852 EXPECT_FALSE(oat_file->IsExecutable());
853 std::vector<std::unique_ptr<const DexFile>> dex_files;
854 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
855 EXPECT_EQ(1u, dex_files.size());
856}
857
858// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
859// disabled.
860// Expect: We should load the odex file non-executable.
861TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
862 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700863 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800864
865 // Create the dex and odex files
866 Copy(GetMultiDexSrc1(), dex_location);
867 GenerateOdexForTest(dex_location, odex_location);
868
869 // Load the oat using an executable oat file assistant.
870 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
871
872 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
873 ASSERT_TRUE(oat_file.get() != nullptr);
874 EXPECT_FALSE(oat_file->IsExecutable());
875 std::vector<std::unique_ptr<const DexFile>> dex_files;
876 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
877 EXPECT_EQ(2u, dex_files.size());
878}
879
880TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
881 std::string error_msg;
882 std::string odex_file;
883
884 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
885 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700886 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800887
888 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
889 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700890 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800891
892 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
893 "nopath.jar", kArm, &odex_file, &error_msg));
894 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
895 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
896}
897
898
899// TODO: More Tests:
900// * Test class linker falls back to unquickened dex for DexNoOat
901// * Test class linker falls back to unquickened dex for MultiDexNoOat
902// * Test multidex files:
903// - Multidex with only classes2.dex out of date should have status
904// kOutOfDate
905// * Test using secondary isa
906// * Test with profiling info?
907// * Test for status of oat while oat is being generated (how?)
908// * Test case where 32 and 64 bit boot class paths differ,
909// and we ask IsInBootClassPath for a class in exactly one of the 32 or
910// 64 bit boot class paths.
911// * Test unexpected scenarios (?):
912// - Dex is stripped, don't have odex.
913// - Oat file corrupted after status check, before reload unexecutable
914// because it's unrelocated and no dex2oat
915
916} // namespace art