blob: 046d8ae779cc9b3e6b7191a4063da85891627986 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010029#include "class_linker-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070031#include "compiler_callbacks.h"
Richard Uhlerf16d5722015-05-11 09:32:47 -070032#include "gc/space/image_space.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "mem_map.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070034#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "os.h"
Richard Uhler23cedd22015-04-08 13:17:29 -070036#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "thread-inl.h"
38#include "utils.h"
39
40namespace art {
41
42class OatFileAssistantTest : public CommonRuntimeTest {
43 public:
44 virtual void SetUp() {
45 ReserveImageSpace();
46 CommonRuntimeTest::SetUp();
47
48 // Create a scratch directory to work from.
49 scratch_dir_ = android_data_ + "/OatFileAssistantTest";
50 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
51
Richard Uhler63434112015-03-16 14:32:16 -070052 // Create a subdirectory in scratch for odex files.
53 odex_oat_dir_ = scratch_dir_ + "/oat";
54 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
55
56 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
57 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
58
Richard Uhler66d874d2015-01-15 09:37:19 -080059
60 // Verify the environment is as we expect
61 uint32_t checksum;
62 std::string error_msg;
63 ASSERT_TRUE(OS::FileExists(GetImageFile().c_str()))
64 << "Expected pre-compiled boot image to be at: " << GetImageFile();
65 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
66 << "Expected dex file to be at: " << GetDexSrc1();
67 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
68 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
Igor Murashkinb1d8c312015-08-04 11:18:43 -070069 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
Richard Uhler66d874d2015-01-15 09:37:19 -080070 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
Richard Uhler66d874d2015-01-15 09:37:19 -080071 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
72 << "Expected dex file to be at: " << GetDexSrc2();
Richard Uhler67ff7d12015-05-14 13:21:13 -070073
74 // GetMultiDexSrc2 should have the same primary dex checksum as
75 // GetMultiDexSrc1, but a different secondary dex checksum.
76 std::vector<std::unique_ptr<const DexFile>> multi1;
77 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc1().c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -070078 GetMultiDexSrc1().c_str(), &error_msg, &multi1)) << error_msg;
Richard Uhler67ff7d12015-05-14 13:21:13 -070079 ASSERT_GT(multi1.size(), 1u);
80
81 std::vector<std::unique_ptr<const DexFile>> multi2;
82 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -070083 GetMultiDexSrc2().c_str(), &error_msg, &multi2)) << error_msg;
Richard Uhler67ff7d12015-05-14 13:21:13 -070084 ASSERT_GT(multi2.size(), 1u);
85
86 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
87 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -080088 }
89
90 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +000091 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -080092
93 // Set up the image location.
94 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
95 nullptr));
96 // Make sure compilercallbacks are not set so that relocation will be
97 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -070098 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -080099 }
100
101 virtual void PreRuntimeCreate() {
102 UnreserveImageSpace();
103 }
104
105 virtual void PostRuntimeCreate() {
106 ReserveImageSpace();
107 }
108
109 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -0700110 ClearDirectory(odex_dir_.c_str());
111 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
112
113 ClearDirectory(odex_oat_dir_.c_str());
114 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800115
116 ClearDirectory(scratch_dir_.c_str());
117 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
118
119 CommonRuntimeTest::TearDown();
120 }
121
122 void Copy(std::string src, std::string dst) {
123 std::ifstream src_stream(src, std::ios::binary);
124 std::ofstream dst_stream(dst, std::ios::binary);
125
126 dst_stream << src_stream.rdbuf();
127 }
128
129 // Returns the directory where the pre-compiled core.art can be found.
130 // TODO: We should factor out this into common tests somewhere rather than
131 // re-hardcoding it here (This was copied originally from the elf writer
132 // test).
133 std::string GetImageDirectory() {
134 if (IsHost()) {
135 const char* host_dir = getenv("ANDROID_HOST_OUT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 CHECK(host_dir != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800137 return std::string(host_dir) + "/framework";
138 } else {
139 return std::string("/data/art-test");
140 }
141 }
142
143 std::string GetImageLocation() {
144 return GetImageDirectory() + "/core.art";
145 }
146
147 std::string GetImageFile() {
148 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
149 + "/core.art";
150 }
151
152 std::string GetDexSrc1() {
153 return GetTestDexFileName("Main");
154 }
155
156 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
157 // file stripped.
158 std::string GetStrippedDexSrc1() {
159 return GetTestDexFileName("MainStripped");
160 }
161
162 std::string GetMultiDexSrc1() {
163 return GetTestDexFileName("MultiDex");
164 }
165
Richard Uhler67ff7d12015-05-14 13:21:13 -0700166 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
167 // with the contents of the secondary dex file changed.
168 std::string GetMultiDexSrc2() {
169 return GetTestDexFileName("MultiDexModifiedSecondary");
170 }
171
Richard Uhler66d874d2015-01-15 09:37:19 -0800172 std::string GetDexSrc2() {
173 return GetTestDexFileName("Nested");
174 }
175
176 // Scratch directory, for dex and odex files (oat files will go in the
177 // dalvik cache).
178 std::string GetScratchDir() {
179 return scratch_dir_;
180 }
181
Richard Uhler63434112015-03-16 14:32:16 -0700182 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800183 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700184 std::string GetOdexDir() {
185 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800186 }
187
Richard Uhler93aa2102015-08-10 14:47:41 -0700188 // Generate a non-PIC odex file for the purposes of test.
Richard Uhler94f5bda2015-07-22 08:25:11 -0700189 // The generated odex file will be un-relocated.
Richard Uhler66d874d2015-01-15 09:37:19 -0800190 void GenerateOdexForTest(const std::string& dex_location,
Richard Uhler93aa2102015-08-10 14:47:41 -0700191 const std::string& odex_location) {
192 // To generate an un-relocated odex file, we first compile a relocated
193 // version of the file, then manually call patchoat to make it look as if
194 // it is unrelocated.
195 std::string relocated_odex_location = odex_location + ".relocated";
196 std::vector<std::string> args;
197 args.push_back("--dex-file=" + dex_location);
198 args.push_back("--oat-file=" + relocated_odex_location);
199 args.push_back("--include-patch-information");
200
201 // We need to use the quick compiler to generate non-PIC code, because
202 // the optimizing compiler always generates PIC.
203 args.push_back("--compiler-backend=Quick");
204
205 std::string error_msg;
206 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
207
208 // Use patchoat to unrelocate the relocated odex file.
209 Runtime* runtime = Runtime::Current();
210 std::vector<std::string> argv;
211 argv.push_back(runtime->GetPatchoatExecutable());
212 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
213 argv.push_back("--input-oat-file=" + relocated_odex_location);
214 argv.push_back("--output-oat-file=" + odex_location);
215 argv.push_back("--base-offset-delta=0x00008000");
216 std::string command_line(Join(argv, ' '));
217 ASSERT_TRUE(Exec(argv, &error_msg)) << error_msg;
218
219 // Verify the odex file was generated as expected and really is
220 // unrelocated.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800221 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
222 odex_location.c_str(),
223 nullptr,
224 nullptr,
225 false,
226 /*low_4gb*/false,
227 dex_location.c_str(),
228 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700229 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
230
Jeff Haodcdc85b2015-12-04 14:06:18 -0800231 const std::vector<gc::space::ImageSpace*> image_spaces =
232 runtime->GetHeap()->GetBootImageSpaces();
233 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
234 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler93aa2102015-08-10 14:47:41 -0700235 const OatHeader& oat_header = odex_file->GetOatHeader();
236 EXPECT_FALSE(odex_file->IsPic());
237 EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum());
238 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
239 oat_header.GetImageFileLocationOatDataBegin());
240 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
241 }
242
243 void GeneratePicOdexForTest(const std::string& dex_location,
244 const std::string& odex_location) {
245 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
246 // relocated image file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800247 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
248 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
249 std::vector<std::string> args;
250 args.push_back("--dex-file=" + dex_location);
251 args.push_back("--oat-file=" + odex_location);
Richard Uhler93aa2102015-08-10 14:47:41 -0700252 args.push_back("--compile-pic");
Richard Uhler66d874d2015-01-15 09:37:19 -0800253 args.push_back("--runtime-arg");
254 args.push_back("-Xnorelocate");
255 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700256 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800257 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler94f5bda2015-07-22 08:25:11 -0700258
259 // Verify the odex file was generated as expected.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800260 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
261 odex_location.c_str(),
262 nullptr,
263 nullptr,
264 false,
265 /*low_4gb*/false,
266 dex_location.c_str(),
267 &error_msg));
Richard Uhler94f5bda2015-07-22 08:25:11 -0700268 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700269 EXPECT_TRUE(odex_file->IsPic());
Richard Uhler66d874d2015-01-15 09:37:19 -0800270 }
271
David Brazdilce4b0ba2016-01-28 15:05:49 +0000272 void GenerateExtractOnlyOdexForTest(const std::string& dex_location,
Calin Juravled91b8a22016-02-18 18:47:37 +0000273 const std::string& odex_location) {
David Brazdilce4b0ba2016-01-28 15:05:49 +0000274 std::vector<std::string> args;
275 args.push_back("--dex-file=" + dex_location);
276 args.push_back("--oat-file=" + odex_location);
277 args.push_back("--compiler-filter=verify-at-runtime");
278 std::string error_msg;
279 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
280
281 // Verify the odex file was generated as expected.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800282 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
283 odex_location.c_str(),
284 nullptr,
285 nullptr,
286 false,
287 /*low_4gb*/false,
288 dex_location.c_str(),
289 &error_msg));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000290 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
291 EXPECT_TRUE(odex_file->IsExtractOnly());
292 EXPECT_EQ(odex_file->GetOatHeader().GetImageFileLocationOatChecksum(), 0u);
293 EXPECT_EQ(odex_file->GetOatHeader().GetImageFileLocationOatDataBegin(), 0u);
294 EXPECT_EQ(odex_file->GetOatHeader().GetImagePatchDelta(), 0);
Calin Juravled91b8a22016-02-18 18:47:37 +0000295 }
296
297 void GenerateProfileGuideOdexForTest(const std::string& dex_location,
298 const std::string& odex_location) {
299 std::vector<std::string> args;
300 args.push_back("--dex-file=" + dex_location);
301 args.push_back("--oat-file=" + odex_location);
302 ScratchFile profile_file;
303 args.push_back("--profile-file=" + profile_file.GetFilename());
304 std::string error_msg;
305 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
306
307 // Verify the odex file was generated as expected.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800308 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
309 odex_location.c_str(),
310 nullptr,
311 nullptr,
312 false,
313 /*low_4gb*/false,
314 dex_location.c_str(),
315 &error_msg));
Calin Juravled91b8a22016-02-18 18:47:37 +0000316 printf("error %s", error_msg.c_str());
317 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
318 EXPECT_TRUE(odex_file->IsProfileGuideCompiled());
319 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000320
Richard Uhler66d874d2015-01-15 09:37:19 -0800321 private:
322 // Reserve memory around where the image will be loaded so other memory
323 // won't conflict when it comes time to load the image.
324 // This can be called with an already loaded image to reserve the space
325 // around it.
326 void ReserveImageSpace() {
327 MemMap::Init();
328
329 // Ensure a chunk of memory is reserved for the image space.
330 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
331 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700332 // Include the main space that has to come right after the
333 // image in case of the GSS collector.
334 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800335
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
337 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
338 for (BacktraceMap::const_iterator it = map->begin();
339 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700340 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
341 reservation_start = std::max(reservation_start, it->end);
342 }
343 ReserveImageSpaceChunk(reservation_start, reservation_end);
344 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800345
Richard Uhler3efe9792015-03-30 16:18:03 -0700346 // Reserve a chunk of memory for the image space in the given range.
347 // Only has effect for chunks with a positive number of bytes.
348 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
349 if (start < end) {
350 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800351 image_reservation_.push_back(std::unique_ptr<MemMap>(
352 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700353 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700354 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800355 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
356 LOG(INFO) << "Reserved space for image " <<
357 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
358 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800359 }
360 }
361
362
363 // Unreserve any memory reserved by ReserveImageSpace. This should be called
364 // before the image is loaded.
365 void UnreserveImageSpace() {
366 image_reservation_.clear();
367 }
368
369 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700370 std::string odex_oat_dir_;
371 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 std::vector<std::unique_ptr<MemMap>> image_reservation_;
373};
374
375class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
376 public:
377 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
378 OatFileAssistantTest::SetUpRuntimeOptions(options);
379 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
380 }
381};
382
383// Generate an oat file for the purposes of test, as opposed to testing
384// generation of oat files.
385static void GenerateOatForTest(const char* dex_location) {
Calin Juravled91b8a22016-02-18 18:47:37 +0000386 OatFileAssistant oat_file_assistant(dex_location,
387 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800388
389 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700390 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800391}
392
393// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700394// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800395TEST_F(OatFileAssistantTest, DexNoOat) {
396 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
397 Copy(GetDexSrc1(), dex_location);
398
Calin Juravled91b8a22016-02-18 18:47:37 +0000399 OatFileAssistant oat_file_assistant(dex_location.c_str(),
400 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800401
Richard Uhler95abd042015-03-24 09:51:28 -0700402 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800403
404 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
405 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
406 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
407 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
408 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700409 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 EXPECT_FALSE(oat_file_assistant.OatFileExists());
411 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
412 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
413 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700414 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700415 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800416}
417
418// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700419// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800420TEST_F(OatFileAssistantTest, NoDexNoOat) {
421 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
422
Calin Juravled91b8a22016-02-18 18:47:37 +0000423 OatFileAssistant oat_file_assistant(dex_location.c_str(),
424 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800425
Richard Uhler9b994ea2015-06-24 08:44:19 -0700426 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
427 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
428
429 // Trying to make the oat file up to date should not fail or crash.
430 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700431 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700432
433 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800434 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
435 EXPECT_EQ(nullptr, oat_file.get());
436}
437
438// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700439// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800440TEST_F(OatFileAssistantTest, OatUpToDate) {
441 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
442 Copy(GetDexSrc1(), dex_location);
443 GenerateOatForTest(dex_location.c_str());
444
Calin Juravled91b8a22016-02-18 18:47:37 +0000445 OatFileAssistant oat_file_assistant(dex_location.c_str(),
446 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800447
Richard Uhler95abd042015-03-24 09:51:28 -0700448 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
450 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
451 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
452 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
453 EXPECT_TRUE(oat_file_assistant.OatFileExists());
454 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
455 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
456 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700457 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700458 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800459}
460
461// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700462// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800463TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
464 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
465 Copy(GetMultiDexSrc1(), dex_location);
466 GenerateOatForTest(dex_location.c_str());
467
Calin Juravled91b8a22016-02-18 18:47:37 +0000468 OatFileAssistant oat_file_assistant(dex_location.c_str(),
469 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700470 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700471 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700472
473 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700474 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800475 ASSERT_TRUE(oat_file.get() != nullptr);
476 EXPECT_TRUE(oat_file->IsExecutable());
477 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700478 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
479 EXPECT_EQ(2u, dex_files.size());
480}
481
Richard Uhler67ff7d12015-05-14 13:21:13 -0700482// Case: We have a MultiDEX file where the secondary dex file is out of date.
483// Expect: The status is kDex2OatNeeded.
484TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
485 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
486
487 // Compile code for GetMultiDexSrc1.
488 Copy(GetMultiDexSrc1(), dex_location);
489 GenerateOatForTest(dex_location.c_str());
490
491 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
492 // is out of date.
493 Copy(GetMultiDexSrc2(), dex_location);
494
Calin Juravled91b8a22016-02-18 18:47:37 +0000495 OatFileAssistant oat_file_assistant(dex_location.c_str(),
496 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700497 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700498 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700499}
500
Richard Uhlere5fed032015-03-18 08:21:11 -0700501// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
502// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700503// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700504TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
505 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700506 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700507
508 // Create the dex file
509 Copy(GetMultiDexSrc1(), dex_location);
510
511 // Create the oat file with relative encoded dex location.
512 std::vector<std::string> args;
513 args.push_back("--dex-file=" + dex_location);
514 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
515 args.push_back("--oat-file=" + oat_location);
516
517 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700518 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700519
520 // Verify we can load both dex files.
521 OatFileAssistant oat_file_assistant(dex_location.c_str(),
522 oat_location.c_str(),
Calin Juravled91b8a22016-02-18 18:47:37 +0000523 OatFileAssistant::kFullCompilation,
Richard Uhlere5fed032015-03-18 08:21:11 -0700524 kRuntimeISA, true);
525 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
526 ASSERT_TRUE(oat_file.get() != nullptr);
527 EXPECT_TRUE(oat_file->IsExecutable());
528 std::vector<std::unique_ptr<const DexFile>> dex_files;
529 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800530 EXPECT_EQ(2u, dex_files.size());
531}
532
Richard Uhler95abd042015-03-24 09:51:28 -0700533// Case: We have a DEX file and out-of-date OAT file.
534// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800535TEST_F(OatFileAssistantTest, OatOutOfDate) {
536 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
537
538 // We create a dex, generate an oat for it, then overwrite the dex with a
539 // different dex to make the oat out of date.
540 Copy(GetDexSrc1(), dex_location);
541 GenerateOatForTest(dex_location.c_str());
542 Copy(GetDexSrc2(), dex_location);
543
Calin Juravled91b8a22016-02-18 18:47:37 +0000544 OatFileAssistant oat_file_assistant(dex_location.c_str(),
545 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700546 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800547
548 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
549 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
550 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
551 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
552 EXPECT_TRUE(oat_file_assistant.OatFileExists());
553 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
554 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700555 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800556}
557
558// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700559// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800560TEST_F(OatFileAssistantTest, DexOdexNoOat) {
561 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700562 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800563
564 // Create the dex and odex files
565 Copy(GetDexSrc1(), dex_location);
566 GenerateOdexForTest(dex_location, odex_location);
567
568 // Verify the status.
Calin Juravled91b8a22016-02-18 18:47:37 +0000569 OatFileAssistant oat_file_assistant(dex_location.c_str(),
570 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800571
Richard Uhler95abd042015-03-24 09:51:28 -0700572 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800573
574 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
575 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
576 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
577 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
578 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800579 EXPECT_FALSE(oat_file_assistant.OatFileExists());
580 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
581 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700582 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700583
584 // We should still be able to get the non-executable odex file to run from.
585 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
586 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800587}
588
589// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700590// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800591TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
592 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700593 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800594
595 // Create the dex and odex files
596 Copy(GetDexSrc1(), dex_location);
597 GenerateOdexForTest(dex_location, odex_location);
598
599 // Strip the dex file
600 Copy(GetStrippedDexSrc1(), dex_location);
601
602 // Verify the status.
Calin Juravled91b8a22016-02-18 18:47:37 +0000603 OatFileAssistant oat_file_assistant(dex_location.c_str(),
604 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800605
Richard Uhler95abd042015-03-24 09:51:28 -0700606 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800607
608 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
609 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
610 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
611 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
612 EXPECT_FALSE(oat_file_assistant.OatFileExists());
613 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
614 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700615 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800616
617 // Make the oat file up to date.
618 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700619 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800620
Richard Uhler95abd042015-03-24 09:51:28 -0700621 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800622
623 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
624 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
625 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
626 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
627 EXPECT_TRUE(oat_file_assistant.OatFileExists());
628 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
629 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700630 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800631
632 // Verify we can load the dex files from it.
633 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
634 ASSERT_TRUE(oat_file.get() != nullptr);
635 EXPECT_TRUE(oat_file->IsExecutable());
636 std::vector<std::unique_ptr<const DexFile>> dex_files;
637 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
638 EXPECT_EQ(1u, dex_files.size());
639}
640
Richard Uhler95abd042015-03-24 09:51:28 -0700641// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
642// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800643TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
644 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700645 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800646
647 // Create the oat file from a different dex file so it looks out of date.
648 Copy(GetDexSrc2(), dex_location);
649 GenerateOatForTest(dex_location.c_str());
650
651 // Create the odex file
652 Copy(GetDexSrc1(), dex_location);
653 GenerateOdexForTest(dex_location, odex_location);
654
655 // Strip the dex file.
656 Copy(GetStrippedDexSrc1(), dex_location);
657
658 // Verify the status.
Calin Juravled91b8a22016-02-18 18:47:37 +0000659 OatFileAssistant oat_file_assistant(dex_location.c_str(),
660 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800661
Richard Uhler95abd042015-03-24 09:51:28 -0700662 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800663
664 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
665 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
666 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
667 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
668 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
669 EXPECT_TRUE(oat_file_assistant.OatFileExists());
670 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
671 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700672 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800673
674 // Make the oat file up to date.
675 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700676 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800677
Richard Uhler95abd042015-03-24 09:51:28 -0700678 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800679
680 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
681 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
682 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
683 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
684 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
685 EXPECT_TRUE(oat_file_assistant.OatFileExists());
686 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
687 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
688 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700689 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800690
691 // Verify we can load the dex files from it.
692 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
693 ASSERT_TRUE(oat_file.get() != nullptr);
694 EXPECT_TRUE(oat_file->IsExecutable());
695 std::vector<std::unique_ptr<const DexFile>> dex_files;
696 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
697 EXPECT_EQ(1u, dex_files.size());
698}
699
Richard Uhler9b994ea2015-06-24 08:44:19 -0700700// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
701// OAT file. Expect: The status is kNoDexOptNeeded.
702TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
703 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
704
705 Copy(GetStrippedDexSrc1(), dex_location);
706
707 // Verify the status.
Calin Juravled91b8a22016-02-18 18:47:37 +0000708 OatFileAssistant oat_file_assistant(dex_location.c_str(),
709 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700710
711 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
712
713 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
714 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
715 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
716 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
717 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
718 EXPECT_FALSE(oat_file_assistant.OatFileExists());
719 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
720 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
721 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
722
723 // Make the oat file up to date. This should have no effect.
724 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700725 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700726
727 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
728
729 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
730 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
731 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
732 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
733 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
734 EXPECT_FALSE(oat_file_assistant.OatFileExists());
735 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
736 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
737 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
738}
739
Richard Uhler95abd042015-03-24 09:51:28 -0700740// Case: We have a DEX file, no ODEX file and an OAT file that needs
741// relocation.
742// Expect: The status is kSelfPatchOatNeeded.
743TEST_F(OatFileAssistantTest, SelfRelocation) {
744 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
745 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
746
747 // Create the dex and odex files
748 Copy(GetDexSrc1(), dex_location);
749 GenerateOdexForTest(dex_location, oat_location);
750
751 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Calin Juravled91b8a22016-02-18 18:47:37 +0000752 oat_location.c_str(), OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700753
754 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
755
756 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
757 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
758 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
759 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
760 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
761 EXPECT_TRUE(oat_file_assistant.OatFileExists());
762 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
763 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
764 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700765 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700766
767 // Make the oat file up to date.
768 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700769 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700770
771 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
772
773 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
774 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
775 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
776 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
777 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
778 EXPECT_TRUE(oat_file_assistant.OatFileExists());
779 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
780 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
781 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700782 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700783
784 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
785 ASSERT_TRUE(oat_file.get() != nullptr);
786 EXPECT_TRUE(oat_file->IsExecutable());
787 std::vector<std::unique_ptr<const DexFile>> dex_files;
788 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
789 EXPECT_EQ(1u, dex_files.size());
790}
791
Richard Uhler66d874d2015-01-15 09:37:19 -0800792// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
793// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700794// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800795TEST_F(OatFileAssistantTest, OdexOatOverlap) {
796 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700797 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
798 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800799
800 // Create the dex and odex files
801 Copy(GetDexSrc1(), dex_location);
802 GenerateOdexForTest(dex_location, odex_location);
803
804 // Create the oat file by copying the odex so they are located in the same
805 // place in memory.
806 Copy(odex_location, oat_location);
807
808 // Verify things don't go bad.
809 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Calin Juravled91b8a22016-02-18 18:47:37 +0000810 oat_location.c_str(), OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800811
Richard Uhler95abd042015-03-24 09:51:28 -0700812 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800813
814 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
815 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
816 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
817 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
818 EXPECT_TRUE(oat_file_assistant.OatFileExists());
819 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
820 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700821 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800822
823 // Things aren't relocated, so it should fall back to interpreted.
824 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
825 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700826
Richard Uhler66d874d2015-01-15 09:37:19 -0800827 EXPECT_FALSE(oat_file->IsExecutable());
828 std::vector<std::unique_ptr<const DexFile>> dex_files;
829 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
830 EXPECT_EQ(1u, dex_files.size());
831}
832
833// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700834// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800835TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
836 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700837 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800838
839 // Create the dex and odex files
840 Copy(GetDexSrc1(), dex_location);
841 GeneratePicOdexForTest(dex_location, odex_location);
842
843 // Verify the status.
Calin Juravled91b8a22016-02-18 18:47:37 +0000844 OatFileAssistant oat_file_assistant(dex_location.c_str(),
845 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800846
Richard Uhler95abd042015-03-24 09:51:28 -0700847 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800848
849 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
850 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
851 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
852 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
853 EXPECT_FALSE(oat_file_assistant.OatFileExists());
854 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
855 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700856 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800857}
858
David Brazdilce4b0ba2016-01-28 15:05:49 +0000859// Case: We have a DEX file and a ExtractOnly ODEX file, but no OAT file.
860// Expect: The status is kNoDexOptNeeded, because ExtractOnly contains no code.
861TEST_F(OatFileAssistantTest, DexExtractOnlyOdexNoOat) {
862 std::string dex_location = GetScratchDir() + "/DexExtractOnlyOdexNoOat.jar";
863 std::string odex_location = GetOdexDir() + "/DexExtractOnlyOdexNoOat.odex";
864
865 // Create the dex and odex files
866 Copy(GetDexSrc1(), dex_location);
867 GenerateExtractOnlyOdexForTest(dex_location, odex_location);
868
869 // Verify the status.
Calin Juravled91b8a22016-02-18 18:47:37 +0000870 OatFileAssistant oat_file_assistant(dex_location.c_str(),
871 OatFileAssistant::kFullCompilation | OatFileAssistant::kExtractOnly,
872 kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000873
874 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
875
876 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
877 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
878 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
879 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
880 EXPECT_FALSE(oat_file_assistant.OatFileExists());
881 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
882 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
883 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
884}
885
Richard Uhler66d874d2015-01-15 09:37:19 -0800886// Case: We have a DEX file and up-to-date OAT file for it.
887// Expect: We should load an executable dex file.
888TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
889 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
890
891 Copy(GetDexSrc1(), dex_location);
892 GenerateOatForTest(dex_location.c_str());
893
894 // Load the oat using an oat file assistant.
Calin Juravled91b8a22016-02-18 18:47:37 +0000895 OatFileAssistant oat_file_assistant(dex_location.c_str(),
896 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800897
898 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
899 ASSERT_TRUE(oat_file.get() != nullptr);
900 EXPECT_TRUE(oat_file->IsExecutable());
901 std::vector<std::unique_ptr<const DexFile>> dex_files;
902 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
903 EXPECT_EQ(1u, dex_files.size());
904}
905
906// Case: We have a DEX file and up-to-date OAT file for it.
907// Expect: Loading non-executable should load the oat non-executable.
908TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
909 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
910
911 Copy(GetDexSrc1(), dex_location);
912 GenerateOatForTest(dex_location.c_str());
913
914 // Load the oat using an oat file assistant.
Calin Juravled91b8a22016-02-18 18:47:37 +0000915 OatFileAssistant oat_file_assistant(dex_location.c_str(),
916 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800917
918 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
919 ASSERT_TRUE(oat_file.get() != nullptr);
920 EXPECT_FALSE(oat_file->IsExecutable());
921 std::vector<std::unique_ptr<const DexFile>> dex_files;
922 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
923 EXPECT_EQ(1u, dex_files.size());
924}
925
926// Case: We have a DEX file.
927// Expect: We should load an executable dex file from an alternative oat
928// location.
929TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
930 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
931 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
932
933 Copy(GetDexSrc1(), dex_location);
934
935 OatFileAssistant oat_file_assistant(
Calin Juravled91b8a22016-02-18 18:47:37 +0000936 dex_location.c_str(), oat_location.c_str(),
937 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800938 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700939 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800940
941 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
942 ASSERT_TRUE(oat_file.get() != nullptr);
943 EXPECT_TRUE(oat_file->IsExecutable());
944 std::vector<std::unique_ptr<const DexFile>> dex_files;
945 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
946 EXPECT_EQ(1u, dex_files.size());
947
948 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
949
950 // Verify it didn't create an oat in the default location.
Calin Juravled91b8a22016-02-18 18:47:37 +0000951 OatFileAssistant ofm(dex_location.c_str(),
952 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800953 EXPECT_FALSE(ofm.OatFileExists());
954}
955
Richard Uhler8327cf72015-10-13 16:34:59 -0700956// Case: We have a DEX file but can't write the oat file.
957// Expect: We should fail to make the oat file up to date.
958TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
959 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
960
961 // Make the oat location unwritable by inserting some non-existent
962 // intermediate directories.
963 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
964
965 Copy(GetDexSrc1(), dex_location);
966
967 OatFileAssistant oat_file_assistant(
Calin Juravled91b8a22016-02-18 18:47:37 +0000968 dex_location.c_str(), oat_location.c_str(),
969 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700970 std::string error_msg;
971 ASSERT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
972
973 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
974 ASSERT_TRUE(oat_file.get() == nullptr);
975}
976
977// Case: We don't have a DEX file and can't write the oat file.
978// Expect: We should fail to generate the oat file without crashing.
979TEST_F(OatFileAssistantTest, GenNoDex) {
980 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
981 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
982
983 OatFileAssistant oat_file_assistant(
Calin Juravled91b8a22016-02-18 18:47:37 +0000984 dex_location.c_str(), oat_location.c_str(),
985 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700986 std::string error_msg;
987 ASSERT_FALSE(oat_file_assistant.GenerateOatFile(&error_msg));
988}
989
Richard Uhler66d874d2015-01-15 09:37:19 -0800990// Turn an absolute path into a path relative to the current working
991// directory.
992static std::string MakePathRelative(std::string target) {
993 char buf[MAXPATHLEN];
994 std::string cwd = getcwd(buf, MAXPATHLEN);
995
996 // Split the target and cwd paths into components.
997 std::vector<std::string> target_path;
998 std::vector<std::string> cwd_path;
999 Split(target, '/', &target_path);
1000 Split(cwd, '/', &cwd_path);
1001
1002 // Reverse the path components, so we can use pop_back().
1003 std::reverse(target_path.begin(), target_path.end());
1004 std::reverse(cwd_path.begin(), cwd_path.end());
1005
1006 // Drop the common prefix of the paths. Because we reversed the path
1007 // components, this becomes the common suffix of target_path and cwd_path.
1008 while (!target_path.empty() && !cwd_path.empty()
1009 && target_path.back() == cwd_path.back()) {
1010 target_path.pop_back();
1011 cwd_path.pop_back();
1012 }
1013
1014 // For each element of the remaining cwd_path, add '..' to the beginning
1015 // of the target path. Because we reversed the path components, we add to
1016 // the end of target_path.
1017 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1018 target_path.push_back("..");
1019 }
1020
1021 // Reverse again to get the right path order, and join to get the result.
1022 std::reverse(target_path.begin(), target_path.end());
1023 return Join(target_path, '/');
1024}
1025
1026// Case: Non-absolute path to Dex location.
1027// Expect: Not sure, but it shouldn't crash.
1028TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1029 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1030 Copy(GetDexSrc1(), abs_dex_location);
1031
1032 std::string dex_location = MakePathRelative(abs_dex_location);
Calin Juravled91b8a22016-02-18 18:47:37 +00001033 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1034 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001035
1036 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -07001037 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -08001038 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1039 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1040 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1041 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1042 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1043 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1044}
1045
1046// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001047// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001048TEST_F(OatFileAssistantTest, ShortDexLocation) {
1049 std::string dex_location = "/xx";
1050
Calin Juravled91b8a22016-02-18 18:47:37 +00001051 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1052 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001053
1054 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001055 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -08001056 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1057 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1058 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1059 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1060 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1061 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001062 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001063
Richard Uhler9b994ea2015-06-24 08:44:19 -07001064 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001065 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001066 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001067 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001068}
1069
1070// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001071// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001072TEST_F(OatFileAssistantTest, LongDexExtension) {
1073 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1074 Copy(GetDexSrc1(), dex_location);
1075
Calin Juravled91b8a22016-02-18 18:47:37 +00001076 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1077 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001078
Richard Uhler95abd042015-03-24 09:51:28 -07001079 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -08001080
1081 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1082 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1083 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1084 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1085 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1086 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1087 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1088}
1089
1090// A task to generate a dex location. Used by the RaceToGenerate test.
1091class RaceGenerateTask : public Task {
1092 public:
1093 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
1094 : dex_location_(dex_location), oat_location_(oat_location),
1095 loaded_oat_file_(nullptr)
1096 {}
1097
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001098 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001099 // Load the dex files, and save a pointer to the loaded oat file, so that
1100 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001101 std::vector<std::unique_ptr<const DexFile>> dex_files;
1102 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001103 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001104 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1105 dex_location_.c_str(),
1106 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001107 /*class_loader*/nullptr,
1108 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001109 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001110 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001111 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001112 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1113 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001114 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001115 }
1116
1117 const OatFile* GetLoadedOatFile() const {
1118 return loaded_oat_file_;
1119 }
1120
1121 private:
1122 std::string dex_location_;
1123 std::string oat_location_;
1124 const OatFile* loaded_oat_file_;
1125};
1126
1127// Test the case where multiple processes race to generate an oat file.
1128// This simulates multiple processes using multiple threads.
1129//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001130// We want unique Oat files to be loaded even when there is a race to load.
1131// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1132// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001133TEST_F(OatFileAssistantTest, RaceToGenerate) {
1134 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001135 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001136
1137 // We use the lib core dex file, because it's large, and hopefully should
1138 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001139 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001140
1141 const int kNumThreads = 32;
1142 Thread* self = Thread::Current();
1143 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1144 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1145 for (int i = 0; i < kNumThreads; i++) {
1146 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1147 thread_pool.AddTask(self, task.get());
1148 tasks.push_back(std::move(task));
1149 }
1150 thread_pool.StartWorkers(self);
1151 thread_pool.Wait(self, true, false);
1152
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001153 // Verify every task got a unique oat file.
1154 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001155 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001156 const OatFile* oat_file = task->GetLoadedOatFile();
1157 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1158 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001159 }
1160}
1161
1162// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1163// disabled.
1164// Expect: We should load the odex file non-executable.
1165TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1166 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001167 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001168
1169 // Create the dex and odex files
1170 Copy(GetDexSrc1(), dex_location);
1171 GenerateOdexForTest(dex_location, odex_location);
1172
1173 // Load the oat using an executable oat file assistant.
Calin Juravled91b8a22016-02-18 18:47:37 +00001174 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1175 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001176
1177 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1178 ASSERT_TRUE(oat_file.get() != nullptr);
1179 EXPECT_FALSE(oat_file->IsExecutable());
1180 std::vector<std::unique_ptr<const DexFile>> dex_files;
1181 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1182 EXPECT_EQ(1u, dex_files.size());
1183}
1184
1185// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1186// disabled.
1187// Expect: We should load the odex file non-executable.
1188TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1189 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001190 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001191
1192 // Create the dex and odex files
1193 Copy(GetMultiDexSrc1(), dex_location);
1194 GenerateOdexForTest(dex_location, odex_location);
1195
1196 // Load the oat using an executable oat file assistant.
Calin Juravled91b8a22016-02-18 18:47:37 +00001197 OatFileAssistant oat_file_assistant(dex_location.c_str(),
1198 OatFileAssistant::kFullCompilation, kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001199
1200 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1201 ASSERT_TRUE(oat_file.get() != nullptr);
1202 EXPECT_FALSE(oat_file->IsExecutable());
1203 std::vector<std::unique_ptr<const DexFile>> dex_files;
1204 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1205 EXPECT_EQ(2u, dex_files.size());
1206}
1207
1208TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1209 std::string error_msg;
1210 std::string odex_file;
1211
1212 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001213 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001214 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001215
1216 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001217 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001218 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001219
1220 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001221 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001222 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001223 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001224}
1225
Calin Juravled91b8a22016-02-18 18:47:37 +00001226// Case: We have a DEX file, extract-only ODEX, and fully compiled OAT.
1227// Expect: The status depends on the target compilation type mask.
1228TEST_F(OatFileAssistantTest, TargetCompilationType) {
1229 std::string dex_location = GetScratchDir() + "/TargetCompilationType.jar";
1230 std::string odex_location = GetOdexDir() + "/TargetCompilationType.odex";
1231 Copy(GetDexSrc1(), dex_location);
1232 GenerateExtractOnlyOdexForTest(dex_location, odex_location);
1233 GenerateOatForTest(dex_location.c_str());
1234
1235 OatFileAssistant ofa_full(dex_location.c_str(),
1236 OatFileAssistant::kFullCompilation, kRuntimeISA, false);
1237 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, ofa_full.GetDexOptNeeded());
1238 EXPECT_FALSE(ofa_full.IsInBootClassPath());
1239 EXPECT_TRUE(ofa_full.OdexFileIsOutOfDate());
1240 EXPECT_TRUE(ofa_full.OatFileIsUpToDate());
1241
1242 OatFileAssistant ofa_extract(dex_location.c_str(),
1243 OatFileAssistant::kExtractOnly, kRuntimeISA, false);
1244 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, ofa_extract.GetDexOptNeeded());
1245 EXPECT_FALSE(ofa_extract.IsInBootClassPath());
1246 EXPECT_TRUE(ofa_extract.OdexFileIsUpToDate());
1247 EXPECT_TRUE(ofa_extract.OatFileIsOutOfDate());
1248
1249 OatFileAssistant ofa_profile(dex_location.c_str(),
1250 OatFileAssistant::kProfileGuideCompilation, kRuntimeISA, false);
1251 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, ofa_profile.GetDexOptNeeded());
1252 EXPECT_FALSE(ofa_profile.IsInBootClassPath());
1253 EXPECT_TRUE(ofa_profile.OdexFileIsOutOfDate());
1254 EXPECT_TRUE(ofa_profile.OatFileIsOutOfDate());
1255
1256 OatFileAssistant ofa_extract_full(dex_location.c_str(),
1257 OatFileAssistant::kFullCompilation | OatFileAssistant::kExtractOnly,
1258 kRuntimeISA, false);
1259 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, ofa_extract_full.GetDexOptNeeded());
1260 EXPECT_FALSE(ofa_extract_full.IsInBootClassPath());
1261 EXPECT_TRUE(ofa_extract_full.OdexFileIsUpToDate());
1262 EXPECT_TRUE(ofa_extract_full.OatFileIsUpToDate());
1263}
1264
Richard Uhler23cedd22015-04-08 13:17:29 -07001265// Verify the dexopt status values from dalvik.system.DexFile
1266// match the OatFileAssistant::DexOptStatus values.
1267TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1268 ScopedObjectAccess soa(Thread::Current());
1269 StackHandleScope<1> hs(soa.Self());
1270 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1271 Handle<mirror::Class> dexfile(
1272 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1273 ASSERT_FALSE(dexfile.Get() == nullptr);
1274 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1275
Mathieu Chartierc7853442015-03-27 14:35:38 -07001276 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001277 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1278 ASSERT_FALSE(no_dexopt_needed == nullptr);
1279 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1280 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1281
Mathieu Chartierc7853442015-03-27 14:35:38 -07001282 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001283 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1284 ASSERT_FALSE(dex2oat_needed == nullptr);
1285 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1286 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1287
Mathieu Chartierc7853442015-03-27 14:35:38 -07001288 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001289 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1290 ASSERT_FALSE(patchoat_needed == nullptr);
1291 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1292 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1293
Mathieu Chartierc7853442015-03-27 14:35:38 -07001294 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001295 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1296 ASSERT_FALSE(self_patchoat_needed == nullptr);
1297 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1298 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
Calin Juravled91b8a22016-02-18 18:47:37 +00001299
1300 ArtField* compilation_type_full = mirror::Class::FindStaticField(
1301 soa.Self(), dexfile, "COMPILATION_TYPE_FULL", "I");
1302 ASSERT_FALSE(compilation_type_full == nullptr);
1303 EXPECT_EQ(compilation_type_full->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1304 EXPECT_EQ(OatFileAssistant::kFullCompilation, compilation_type_full->GetInt(dexfile.Get()));
1305
1306 ArtField* compilation_type_profile_guide = mirror::Class::FindStaticField(
1307 soa.Self(), dexfile, "COMPILATION_TYPE_PROFILE_GUIDE", "I");
1308 ASSERT_FALSE(compilation_type_profile_guide == nullptr);
1309 EXPECT_EQ(compilation_type_profile_guide->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1310 EXPECT_EQ(OatFileAssistant::kProfileGuideCompilation,
1311 compilation_type_profile_guide->GetInt(dexfile.Get()));
1312
1313 ArtField* compilation_type_extract_only = mirror::Class::FindStaticField(
1314 soa.Self(), dexfile, "COMPILATION_TYPE_EXTRACT_ONLY", "I");
1315 ASSERT_FALSE(compilation_type_extract_only == nullptr);
1316 EXPECT_EQ(compilation_type_extract_only->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1317 EXPECT_EQ(OatFileAssistant::kExtractOnly, compilation_type_extract_only->GetInt(dexfile.Get()));
Richard Uhler23cedd22015-04-08 13:17:29 -07001318}
Richard Uhler66d874d2015-01-15 09:37:19 -08001319
1320// TODO: More Tests:
1321// * Test class linker falls back to unquickened dex for DexNoOat
1322// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001323// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001324// * Test for status of oat while oat is being generated (how?)
1325// * Test case where 32 and 64 bit boot class paths differ,
1326// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1327// 64 bit boot class paths.
1328// * Test unexpected scenarios (?):
1329// - Dex is stripped, don't have odex.
1330// - Oat file corrupted after status check, before reload unexecutable
1331// because it's unrelocated and no dex2oat
Calin Juravled91b8a22016-02-18 18:47:37 +00001332// * Test unrelocated specific target compilation type can be relocated to
1333// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001334
1335} // namespace art