blob: de4999111e5755a58ca028e2e2f2da94c4e067a1 [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.
Richard Uhler95abd042015-03-24 09:51:28 -0700270// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800271TEST_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
Richard Uhler95abd042015-03-24 09:51:28 -0700277 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800278
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());
Richard Uhler95abd042015-03-24 09:51:28 -0700284 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800285 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());
Richard Uhler95abd042015-03-24 09:51:28 -0700289 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800290}
291
292// Case: We have no DEX file and no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700293// Expect: Status is kDex2OatNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800294TEST_F(OatFileAssistantTest, NoDexNoOat) {
295 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
296
297 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
298
Richard Uhler95abd042015-03-24 09:51:28 -0700299 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800300 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.
Richard Uhler95abd042015-03-24 09:51:28 -0700305// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800306TEST_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
Richard Uhler95abd042015-03-24 09:51:28 -0700313 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800314 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());
Richard Uhler95abd042015-03-24 09:51:28 -0700322 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800323}
324
325// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700326// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800327TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
328 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
329 Copy(GetMultiDexSrc1(), dex_location);
330 GenerateOatForTest(dex_location.c_str());
331
Richard Uhlere5fed032015-03-18 08:21:11 -0700332 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700333 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
334
335 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700336 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800337 ASSERT_TRUE(oat_file.get() != nullptr);
338 EXPECT_TRUE(oat_file->IsExecutable());
339 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700340 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
341 EXPECT_EQ(2u, dex_files.size());
342}
343
344// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
345// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700346// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700347TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
348 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700349 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700350
351 // Create the dex file
352 Copy(GetMultiDexSrc1(), dex_location);
353
354 // Create the oat file with relative encoded dex location.
355 std::vector<std::string> args;
356 args.push_back("--dex-file=" + dex_location);
357 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
358 args.push_back("--oat-file=" + oat_location);
359
360 std::string error_msg;
361 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
362
363 // Verify we can load both dex files.
364 OatFileAssistant oat_file_assistant(dex_location.c_str(),
365 oat_location.c_str(),
366 kRuntimeISA, true);
367 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
368 ASSERT_TRUE(oat_file.get() != nullptr);
369 EXPECT_TRUE(oat_file->IsExecutable());
370 std::vector<std::unique_ptr<const DexFile>> dex_files;
371 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 EXPECT_EQ(2u, dex_files.size());
373}
374
Richard Uhler95abd042015-03-24 09:51:28 -0700375// Case: We have a DEX file and out-of-date OAT file.
376// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800377TEST_F(OatFileAssistantTest, OatOutOfDate) {
378 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
379
380 // We create a dex, generate an oat for it, then overwrite the dex with a
381 // different dex to make the oat out of date.
382 Copy(GetDexSrc1(), dex_location);
383 GenerateOatForTest(dex_location.c_str());
384 Copy(GetDexSrc2(), dex_location);
385
386 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700387 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800388
389 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
390 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
391 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
392 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
393 EXPECT_TRUE(oat_file_assistant.OatFileExists());
394 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
395 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
396}
397
398// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700399// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800400TEST_F(OatFileAssistantTest, DexOdexNoOat) {
401 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700402 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800403
404 // Create the dex and odex files
405 Copy(GetDexSrc1(), dex_location);
406 GenerateOdexForTest(dex_location, odex_location);
407
408 // Verify the status.
409 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
410
Richard Uhler95abd042015-03-24 09:51:28 -0700411 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800412
413 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
414 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
415 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
416 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
417 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800418 EXPECT_FALSE(oat_file_assistant.OatFileExists());
419 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
420 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
421}
422
423// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700424// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800425TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
426 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700427 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800428
429 // Create the dex and odex files
430 Copy(GetDexSrc1(), dex_location);
431 GenerateOdexForTest(dex_location, odex_location);
432
433 // Strip the dex file
434 Copy(GetStrippedDexSrc1(), dex_location);
435
436 // Verify the status.
437 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
438
Richard Uhler95abd042015-03-24 09:51:28 -0700439 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800440
441 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
442 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
443 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
444 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
445 EXPECT_FALSE(oat_file_assistant.OatFileExists());
446 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
447 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
448
449 // Make the oat file up to date.
450 std::string error_msg;
451 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
452
Richard Uhler95abd042015-03-24 09:51:28 -0700453 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800454
455 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
456 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
457 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
458 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
459 EXPECT_TRUE(oat_file_assistant.OatFileExists());
460 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
461 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
462
463 // Verify we can load the dex files from it.
464 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
465 ASSERT_TRUE(oat_file.get() != nullptr);
466 EXPECT_TRUE(oat_file->IsExecutable());
467 std::vector<std::unique_ptr<const DexFile>> dex_files;
468 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
469 EXPECT_EQ(1u, dex_files.size());
470}
471
Richard Uhler95abd042015-03-24 09:51:28 -0700472// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
473// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800474TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
475 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700476 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800477
478 // Create the oat file from a different dex file so it looks out of date.
479 Copy(GetDexSrc2(), dex_location);
480 GenerateOatForTest(dex_location.c_str());
481
482 // Create the odex file
483 Copy(GetDexSrc1(), dex_location);
484 GenerateOdexForTest(dex_location, odex_location);
485
486 // Strip the dex file.
487 Copy(GetStrippedDexSrc1(), dex_location);
488
489 // Verify the status.
490 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
491
Richard Uhler95abd042015-03-24 09:51:28 -0700492 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
494 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
495 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
496 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
497 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
498 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
499 EXPECT_TRUE(oat_file_assistant.OatFileExists());
500 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
501 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
502
503 // Make the oat file up to date.
504 std::string error_msg;
505 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
506
Richard Uhler95abd042015-03-24 09:51:28 -0700507 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800508
509 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
510 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
511 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
512 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
513 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
514 EXPECT_TRUE(oat_file_assistant.OatFileExists());
515 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
516 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
517 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
518
519 // Verify we can load the dex files from it.
520 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
521 ASSERT_TRUE(oat_file.get() != nullptr);
522 EXPECT_TRUE(oat_file->IsExecutable());
523 std::vector<std::unique_ptr<const DexFile>> dex_files;
524 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
525 EXPECT_EQ(1u, dex_files.size());
526}
527
Richard Uhler95abd042015-03-24 09:51:28 -0700528// Case: We have a DEX file, no ODEX file and an OAT file that needs
529// relocation.
530// Expect: The status is kSelfPatchOatNeeded.
531TEST_F(OatFileAssistantTest, SelfRelocation) {
532 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
533 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
534
535 // Create the dex and odex files
536 Copy(GetDexSrc1(), dex_location);
537 GenerateOdexForTest(dex_location, oat_location);
538
539 OatFileAssistant oat_file_assistant(dex_location.c_str(),
540 oat_location.c_str(), kRuntimeISA, true);
541
542 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
543
544 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
545 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
546 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
547 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
548 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
549 EXPECT_TRUE(oat_file_assistant.OatFileExists());
550 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
551 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
552 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
553
554 // Make the oat file up to date.
555 std::string error_msg;
556 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
557
558 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
559
560 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
561 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
562 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
563 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
564 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
565 EXPECT_TRUE(oat_file_assistant.OatFileExists());
566 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
567 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
568 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
569
570 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
571 ASSERT_TRUE(oat_file.get() != nullptr);
572 EXPECT_TRUE(oat_file->IsExecutable());
573 std::vector<std::unique_ptr<const DexFile>> dex_files;
574 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
575 EXPECT_EQ(1u, dex_files.size());
576}
577
Richard Uhler66d874d2015-01-15 09:37:19 -0800578// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
579// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700580// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800581TEST_F(OatFileAssistantTest, OdexOatOverlap) {
582 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700583 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
584 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800585
586 // Create the dex and odex files
587 Copy(GetDexSrc1(), dex_location);
588 GenerateOdexForTest(dex_location, odex_location);
589
590 // Create the oat file by copying the odex so they are located in the same
591 // place in memory.
592 Copy(odex_location, oat_location);
593
594 // Verify things don't go bad.
595 OatFileAssistant oat_file_assistant(dex_location.c_str(),
596 oat_location.c_str(), kRuntimeISA, true);
597
Richard Uhler95abd042015-03-24 09:51:28 -0700598 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800599
600 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
601 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
602 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
603 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
604 EXPECT_TRUE(oat_file_assistant.OatFileExists());
605 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
606 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
607
608 // Things aren't relocated, so it should fall back to interpreted.
609 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
610 ASSERT_TRUE(oat_file.get() != nullptr);
611 EXPECT_FALSE(oat_file->IsExecutable());
612 std::vector<std::unique_ptr<const DexFile>> dex_files;
613 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
614 EXPECT_EQ(1u, dex_files.size());
615}
616
617// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700618// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800619TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
620 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700621 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800622
623 // Create the dex and odex files
624 Copy(GetDexSrc1(), dex_location);
625 GeneratePicOdexForTest(dex_location, odex_location);
626
627 // Verify the status.
628 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
629
Richard Uhler95abd042015-03-24 09:51:28 -0700630 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800631
632 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
633 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
634 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
635 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
636 EXPECT_FALSE(oat_file_assistant.OatFileExists());
637 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
638 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
639}
640
641// Case: We have a DEX file and up-to-date OAT file for it.
642// Expect: We should load an executable dex file.
643TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
644 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
645
646 Copy(GetDexSrc1(), dex_location);
647 GenerateOatForTest(dex_location.c_str());
648
649 // Load the oat using an oat file assistant.
650 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
651
652 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
653 ASSERT_TRUE(oat_file.get() != nullptr);
654 EXPECT_TRUE(oat_file->IsExecutable());
655 std::vector<std::unique_ptr<const DexFile>> dex_files;
656 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
657 EXPECT_EQ(1u, dex_files.size());
658}
659
660// Case: We have a DEX file and up-to-date OAT file for it.
661// Expect: Loading non-executable should load the oat non-executable.
662TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
663 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
664
665 Copy(GetDexSrc1(), dex_location);
666 GenerateOatForTest(dex_location.c_str());
667
668 // Load the oat using an oat file assistant.
669 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
670
671 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
672 ASSERT_TRUE(oat_file.get() != nullptr);
673 EXPECT_FALSE(oat_file->IsExecutable());
674 std::vector<std::unique_ptr<const DexFile>> dex_files;
675 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
676 EXPECT_EQ(1u, dex_files.size());
677}
678
679// Case: We have a DEX file.
680// Expect: We should load an executable dex file from an alternative oat
681// location.
682TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
683 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
684 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
685
686 Copy(GetDexSrc1(), dex_location);
687
688 OatFileAssistant oat_file_assistant(
689 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
690 std::string error_msg;
691 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
692
693 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
694 ASSERT_TRUE(oat_file.get() != nullptr);
695 EXPECT_TRUE(oat_file->IsExecutable());
696 std::vector<std::unique_ptr<const DexFile>> dex_files;
697 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
698 EXPECT_EQ(1u, dex_files.size());
699
700 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
701
702 // Verify it didn't create an oat in the default location.
703 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
704 EXPECT_FALSE(ofm.OatFileExists());
705}
706
707// Case: Non-existent Dex location.
708// Expect: The dex code is out of date, and trying to update it fails.
709TEST_F(OatFileAssistantTest, NonExsistentDexLocation) {
710 std::string dex_location = GetScratchDir() + "/BadDexLocation.jar";
711
712 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
713
714 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700715 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800716 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
717 EXPECT_FALSE(oat_file_assistant.OatFileExists());
718 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
719 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
720 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
721 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
722
723 std::string error_msg;
724 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
725 EXPECT_FALSE(error_msg.empty());
726}
727
728// Turn an absolute path into a path relative to the current working
729// directory.
730static std::string MakePathRelative(std::string target) {
731 char buf[MAXPATHLEN];
732 std::string cwd = getcwd(buf, MAXPATHLEN);
733
734 // Split the target and cwd paths into components.
735 std::vector<std::string> target_path;
736 std::vector<std::string> cwd_path;
737 Split(target, '/', &target_path);
738 Split(cwd, '/', &cwd_path);
739
740 // Reverse the path components, so we can use pop_back().
741 std::reverse(target_path.begin(), target_path.end());
742 std::reverse(cwd_path.begin(), cwd_path.end());
743
744 // Drop the common prefix of the paths. Because we reversed the path
745 // components, this becomes the common suffix of target_path and cwd_path.
746 while (!target_path.empty() && !cwd_path.empty()
747 && target_path.back() == cwd_path.back()) {
748 target_path.pop_back();
749 cwd_path.pop_back();
750 }
751
752 // For each element of the remaining cwd_path, add '..' to the beginning
753 // of the target path. Because we reversed the path components, we add to
754 // the end of target_path.
755 for (unsigned int i = 0; i < cwd_path.size(); i++) {
756 target_path.push_back("..");
757 }
758
759 // Reverse again to get the right path order, and join to get the result.
760 std::reverse(target_path.begin(), target_path.end());
761 return Join(target_path, '/');
762}
763
764// Case: Non-absolute path to Dex location.
765// Expect: Not sure, but it shouldn't crash.
766TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
767 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
768 Copy(GetDexSrc1(), abs_dex_location);
769
770 std::string dex_location = MakePathRelative(abs_dex_location);
771 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
772
773 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700774 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800775 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
776 EXPECT_FALSE(oat_file_assistant.OatFileExists());
777 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
778 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
779 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
780 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
781}
782
783// Case: Very short, non-existent Dex location.
784// Expect: Dex code is out of date, and trying to update it fails.
785TEST_F(OatFileAssistantTest, ShortDexLocation) {
786 std::string dex_location = "/xx";
787
788 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
789
790 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700791 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800792 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
793 EXPECT_FALSE(oat_file_assistant.OatFileExists());
794 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
795 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
796 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
797 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
798
799 std::string error_msg;
800 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
801 EXPECT_FALSE(error_msg.empty());
802}
803
804// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700805// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800806TEST_F(OatFileAssistantTest, LongDexExtension) {
807 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
808 Copy(GetDexSrc1(), dex_location);
809
810 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
811
Richard Uhler95abd042015-03-24 09:51:28 -0700812 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800813
814 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
815 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
816 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
817 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
818 EXPECT_FALSE(oat_file_assistant.OatFileExists());
819 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
820 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
821}
822
823// A task to generate a dex location. Used by the RaceToGenerate test.
824class RaceGenerateTask : public Task {
825 public:
826 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
827 : dex_location_(dex_location), oat_location_(oat_location),
828 loaded_oat_file_(nullptr)
829 {}
830
831 void Run(Thread* self) {
832 UNUSED(self);
833
834 // Load the dex files, and save a pointer to the loaded oat file, so that
835 // we can verify only one oat file was loaded for the dex location.
836 ClassLinker* linker = Runtime::Current()->GetClassLinker();
837 std::vector<std::unique_ptr<const DexFile>> dex_files;
838 std::vector<std::string> error_msgs;
839 dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs);
840 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700841 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
842 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800843 }
844
845 const OatFile* GetLoadedOatFile() const {
846 return loaded_oat_file_;
847 }
848
849 private:
850 std::string dex_location_;
851 std::string oat_location_;
852 const OatFile* loaded_oat_file_;
853};
854
855// Test the case where multiple processes race to generate an oat file.
856// This simulates multiple processes using multiple threads.
857//
858// We want only one Oat file to be loaded when there is a race to load, to
859// avoid using up the virtual memory address space.
860TEST_F(OatFileAssistantTest, RaceToGenerate) {
861 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700862 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800863
864 // We use the lib core dex file, because it's large, and hopefully should
865 // take a while to generate.
866 Copy(GetLibCoreDexFileName(), dex_location);
867
868 const int kNumThreads = 32;
869 Thread* self = Thread::Current();
870 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
871 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
872 for (int i = 0; i < kNumThreads; i++) {
873 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
874 thread_pool.AddTask(self, task.get());
875 tasks.push_back(std::move(task));
876 }
877 thread_pool.StartWorkers(self);
878 thread_pool.Wait(self, true, false);
879
880 // Verify every task got the same pointer.
881 const OatFile* expected = tasks[0]->GetLoadedOatFile();
882 for (auto& task : tasks) {
883 EXPECT_EQ(expected, task->GetLoadedOatFile());
884 }
885}
886
887// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
888// disabled.
889// Expect: We should load the odex file non-executable.
890TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
891 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700892 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800893
894 // Create the dex and odex files
895 Copy(GetDexSrc1(), dex_location);
896 GenerateOdexForTest(dex_location, odex_location);
897
898 // Load the oat using an executable oat file assistant.
899 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
900
901 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
902 ASSERT_TRUE(oat_file.get() != nullptr);
903 EXPECT_FALSE(oat_file->IsExecutable());
904 std::vector<std::unique_ptr<const DexFile>> dex_files;
905 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
906 EXPECT_EQ(1u, dex_files.size());
907}
908
909// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
910// disabled.
911// Expect: We should load the odex file non-executable.
912TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
913 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700914 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800915
916 // Create the dex and odex files
917 Copy(GetMultiDexSrc1(), dex_location);
918 GenerateOdexForTest(dex_location, odex_location);
919
920 // Load the oat using an executable oat file assistant.
921 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
922
923 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
924 ASSERT_TRUE(oat_file.get() != nullptr);
925 EXPECT_FALSE(oat_file->IsExecutable());
926 std::vector<std::unique_ptr<const DexFile>> dex_files;
927 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
928 EXPECT_EQ(2u, dex_files.size());
929}
930
931TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
932 std::string error_msg;
933 std::string odex_file;
934
935 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
936 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700937 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800938
939 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
940 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700941 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800942
943 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
944 "nopath.jar", kArm, &odex_file, &error_msg));
945 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
946 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
947}
948
949
950// TODO: More Tests:
951// * Test class linker falls back to unquickened dex for DexNoOat
952// * Test class linker falls back to unquickened dex for MultiDexNoOat
953// * Test multidex files:
954// - Multidex with only classes2.dex out of date should have status
955// kOutOfDate
956// * Test using secondary isa
957// * Test with profiling info?
958// * Test for status of oat while oat is being generated (how?)
959// * Test case where 32 and 64 bit boot class paths differ,
960// and we ask IsInBootClassPath for a class in exactly one of the 32 or
961// 64 bit boot class paths.
962// * Test unexpected scenarios (?):
963// - Dex is stripped, don't have odex.
964// - Oat file corrupted after status check, before reload unexecutable
965// because it's unrelocated and no dex2oat
966
967} // namespace art