blob: bddfa4f21a2ba20c4111a21f608f37f39fa72b9c [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,
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000191 const std::string& odex_location,
192 CompilerFilter::Filter filter) {
Richard Uhler93aa2102015-08-10 14:47:41 -0700193 // To generate an un-relocated odex file, we first compile a relocated
194 // version of the file, then manually call patchoat to make it look as if
195 // it is unrelocated.
196 std::string relocated_odex_location = odex_location + ".relocated";
197 std::vector<std::string> args;
198 args.push_back("--dex-file=" + dex_location);
199 args.push_back("--oat-file=" + relocated_odex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000200 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler93aa2102015-08-10 14:47:41 -0700201
202 // We need to use the quick compiler to generate non-PIC code, because
203 // the optimizing compiler always generates PIC.
204 args.push_back("--compiler-backend=Quick");
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000205 args.push_back("--include-patch-information");
Richard Uhler93aa2102015-08-10 14:47:41 -0700206
207 std::string error_msg;
208 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
209
210 // Use patchoat to unrelocate the relocated odex file.
211 Runtime* runtime = Runtime::Current();
212 std::vector<std::string> argv;
213 argv.push_back(runtime->GetPatchoatExecutable());
214 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
215 argv.push_back("--input-oat-file=" + relocated_odex_location);
216 argv.push_back("--output-oat-file=" + odex_location);
217 argv.push_back("--base-offset-delta=0x00008000");
218 std::string command_line(Join(argv, ' '));
219 ASSERT_TRUE(Exec(argv, &error_msg)) << error_msg;
220
221 // Verify the odex file was generated as expected and really is
222 // unrelocated.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800223 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
224 odex_location.c_str(),
225 nullptr,
226 nullptr,
227 false,
228 /*low_4gb*/false,
229 dex_location.c_str(),
230 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700231 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700232 EXPECT_FALSE(odex_file->IsPic());
Richard Uhler1c4eb042016-03-29 13:27:41 -0700233 EXPECT_TRUE(odex_file->HasPatchInfo());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000234 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
235
236 if (CompilerFilter::IsCompilationEnabled(filter)) {
237 const std::vector<gc::space::ImageSpace*> image_spaces =
238 runtime->GetHeap()->GetBootImageSpaces();
239 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
240 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
241 const OatHeader& oat_header = odex_file->GetOatHeader();
242 EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum());
243 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
244 oat_header.GetImageFileLocationOatDataBegin());
245 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
246 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700247 }
248
249 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000250 const std::string& odex_location,
251 CompilerFilter::Filter filter) {
Richard Uhler93aa2102015-08-10 14:47:41 -0700252 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
253 // relocated image file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800254 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
255 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
256 std::vector<std::string> args;
257 args.push_back("--dex-file=" + dex_location);
258 args.push_back("--oat-file=" + odex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000259 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler93aa2102015-08-10 14:47:41 -0700260 args.push_back("--compile-pic");
Richard Uhler66d874d2015-01-15 09:37:19 -0800261 args.push_back("--runtime-arg");
262 args.push_back("-Xnorelocate");
263 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700264 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800265 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler94f5bda2015-07-22 08:25:11 -0700266
267 // Verify the odex file was generated as expected.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800268 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
269 odex_location.c_str(),
270 nullptr,
271 nullptr,
272 false,
273 /*low_4gb*/false,
274 dex_location.c_str(),
275 &error_msg));
Richard Uhler94f5bda2015-07-22 08:25:11 -0700276 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700277 EXPECT_TRUE(odex_file->IsPic());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000278 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
Calin Juravled91b8a22016-02-18 18:47:37 +0000279 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000280
Richard Uhler1c4eb042016-03-29 13:27:41 -0700281 // Generate a non-PIC odex file without patch information for the purposes
282 // of test. The generated odex file will be un-relocated.
283 // TODO: This won't work correctly if we depend on the boot image being
284 // randomly relocated by a non-zero amount. We should have a better solution
285 // for avoiding that flakiness and duplicating code to generate odex and oat
286 // files for test.
287 void GenerateNoPatchOdexForTest(const std::string& dex_location,
288 const std::string& odex_location,
289 CompilerFilter::Filter filter) {
290 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
291 // relocated image file.
292 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
293 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
294 std::vector<std::string> args;
295 args.push_back("--dex-file=" + dex_location);
296 args.push_back("--oat-file=" + odex_location);
297 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
298 args.push_back("--runtime-arg");
299 args.push_back("-Xnorelocate");
300 std::string error_msg;
301 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
302 setenv("ANDROID_DATA", android_data_.c_str(), 1);
303
304 // Verify the odex file was generated as expected.
305 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
306 odex_location.c_str(),
307 nullptr,
308 nullptr,
309 false,
310 /*low_4gb*/false,
311 dex_location.c_str(),
312 &error_msg));
313 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
314 EXPECT_FALSE(odex_file->IsPic());
315 EXPECT_FALSE(odex_file->HasPatchInfo());
316 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
317 }
318
Richard Uhler66d874d2015-01-15 09:37:19 -0800319 private:
320 // Reserve memory around where the image will be loaded so other memory
321 // won't conflict when it comes time to load the image.
322 // This can be called with an already loaded image to reserve the space
323 // around it.
324 void ReserveImageSpace() {
325 MemMap::Init();
326
327 // Ensure a chunk of memory is reserved for the image space.
328 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
329 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700330 // Include the main space that has to come right after the
331 // image in case of the GSS collector.
332 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800333
Richard Uhler66d874d2015-01-15 09:37:19 -0800334 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
335 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
336 for (BacktraceMap::const_iterator it = map->begin();
337 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700338 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
339 reservation_start = std::max(reservation_start, it->end);
340 }
341 ReserveImageSpaceChunk(reservation_start, reservation_end);
342 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800343
Richard Uhler3efe9792015-03-30 16:18:03 -0700344 // Reserve a chunk of memory for the image space in the given range.
345 // Only has effect for chunks with a positive number of bytes.
346 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
347 if (start < end) {
348 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800349 image_reservation_.push_back(std::unique_ptr<MemMap>(
350 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700351 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700352 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800353 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
354 LOG(INFO) << "Reserved space for image " <<
355 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
356 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800357 }
358 }
359
360
361 // Unreserve any memory reserved by ReserveImageSpace. This should be called
362 // before the image is loaded.
363 void UnreserveImageSpace() {
364 image_reservation_.clear();
365 }
366
367 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700368 std::string odex_oat_dir_;
369 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800370 std::vector<std::unique_ptr<MemMap>> image_reservation_;
371};
372
373class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
374 public:
375 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
376 OatFileAssistantTest::SetUpRuntimeOptions(options);
377 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
378 }
379};
380
381// Generate an oat file for the purposes of test, as opposed to testing
382// generation of oat files.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000383static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
384 // Use an oat file assistant to find the proper oat location.
385 OatFileAssistant ofa(dex_location, kRuntimeISA, false, false);
386 const std::string* oat_location = ofa.OatFileName();
387 ASSERT_TRUE(oat_location != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800388
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000389 std::vector<std::string> args;
390 args.push_back("--dex-file=" + std::string(dex_location));
391 args.push_back("--oat-file=" + *oat_location);
392 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
393 args.push_back("--runtime-arg");
394 args.push_back("-Xnorelocate");
Richard Uhler66d874d2015-01-15 09:37:19 -0800395 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000396 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
397
398 // Verify the oat file was generated as expected.
399 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location->c_str(),
400 oat_location->c_str(),
401 nullptr,
402 nullptr,
403 false,
404 /*low_4gb*/false,
405 dex_location,
406 &error_msg));
407 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
408 EXPECT_EQ(filter, oat_file->GetCompilerFilter());
Richard Uhler66d874d2015-01-15 09:37:19 -0800409}
410
411// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700412// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800413TEST_F(OatFileAssistantTest, DexNoOat) {
414 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
415 Copy(GetDexSrc1(), dex_location);
416
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000417 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800418
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000419 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
420 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
421 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
422 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
423 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
424 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
425 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
426 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800427
428 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
429 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
430 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
431 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
432 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700433 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800434 EXPECT_FALSE(oat_file_assistant.OatFileExists());
435 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
436 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
437 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700438 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700439 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800440}
441
442// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700443// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800444TEST_F(OatFileAssistantTest, NoDexNoOat) {
445 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
446
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000447 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800448
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000449 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
450 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700451 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
452
453 // Trying to make the oat file up to date should not fail or crash.
454 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700455 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
456 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700457
458 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800459 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
460 EXPECT_EQ(nullptr, oat_file.get());
461}
462
463// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700464// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800465TEST_F(OatFileAssistantTest, OatUpToDate) {
466 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
467 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000468 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800469
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000470 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800471
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000472 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
473 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
474 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
475 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
476 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
477 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
478 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
479 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
480
Richard Uhler66d874d2015-01-15 09:37:19 -0800481 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
482 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
483 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
484 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
485 EXPECT_TRUE(oat_file_assistant.OatFileExists());
486 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
487 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
488 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700489 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700490 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800491}
492
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000493// Case: We have a DEX file and speed-profile OAT file for it.
494// Expect: The status is kNoDexOptNeeded if the profile hasn't changed.
495TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
496 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
497 Copy(GetDexSrc1(), dex_location);
498 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
499
500 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
501
502 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
503 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
504 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
505 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
506
507 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
508 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
509 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
510 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
511 EXPECT_TRUE(oat_file_assistant.OatFileExists());
512 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
513 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
514 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
515 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
516 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
517}
518
519// Case: We have a DEX file and speed-profile OAT file for it.
520// Expect: The status is kNoDex2OatNeeded if the profile has changed.
521TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) {
522 std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar";
523 Copy(GetDexSrc1(), dex_location);
524 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
525
526 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false);
527
528 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
529 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
530 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
531 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
532
533 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
534 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
535 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
536 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
537 EXPECT_TRUE(oat_file_assistant.OatFileExists());
538 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
539 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
540 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
541 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
542 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
543}
544
Richard Uhler66d874d2015-01-15 09:37:19 -0800545// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700546// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800547TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
548 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
549 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000550 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800551
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000552 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
553 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
554 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700555 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700556
557 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700558 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800559 ASSERT_TRUE(oat_file.get() != nullptr);
560 EXPECT_TRUE(oat_file->IsExecutable());
561 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700562 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
563 EXPECT_EQ(2u, dex_files.size());
564}
565
Richard Uhler67ff7d12015-05-14 13:21:13 -0700566// Case: We have a MultiDEX file where the secondary dex file is out of date.
567// Expect: The status is kDex2OatNeeded.
568TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
569 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
570
571 // Compile code for GetMultiDexSrc1.
572 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000573 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700574
575 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
576 // is out of date.
577 Copy(GetMultiDexSrc2(), dex_location);
578
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000579 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
580 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
581 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700582 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700583}
584
Richard Uhlere5fed032015-03-18 08:21:11 -0700585// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
586// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700587// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700588TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
589 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700590 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700591
592 // Create the dex file
593 Copy(GetMultiDexSrc1(), dex_location);
594
595 // Create the oat file with relative encoded dex location.
596 std::vector<std::string> args;
597 args.push_back("--dex-file=" + dex_location);
598 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
599 args.push_back("--oat-file=" + oat_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000600 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700601
602 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700603 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700604
605 // Verify we can load both dex files.
606 OatFileAssistant oat_file_assistant(dex_location.c_str(),
607 oat_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000608 kRuntimeISA, false, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700609 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
610 ASSERT_TRUE(oat_file.get() != nullptr);
611 EXPECT_TRUE(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());
Richard Uhler66d874d2015-01-15 09:37:19 -0800614 EXPECT_EQ(2u, dex_files.size());
615}
616
Richard Uhler95abd042015-03-24 09:51:28 -0700617// Case: We have a DEX file and out-of-date OAT file.
618// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800619TEST_F(OatFileAssistantTest, OatOutOfDate) {
620 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
621
622 // We create a dex, generate an oat for it, then overwrite the dex with a
623 // different dex to make the oat out of date.
624 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000625 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800626 Copy(GetDexSrc2(), dex_location);
627
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000628 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
629 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
630 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
631 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
632 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800633
634 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
635 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
636 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
637 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
638 EXPECT_TRUE(oat_file_assistant.OatFileExists());
639 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
640 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700641 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800642}
643
644// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700645// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800646TEST_F(OatFileAssistantTest, DexOdexNoOat) {
647 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700648 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800649
650 // Create the dex and odex files
651 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000652 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800653
654 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000655 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800656
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000657 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
658 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
659 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
660 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800661
662 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
663 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
664 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
665 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
666 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800667 EXPECT_FALSE(oat_file_assistant.OatFileExists());
668 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
669 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700670 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700671
672 // We should still be able to get the non-executable odex file to run from.
673 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
674 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800675}
676
677// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700678// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800679TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
680 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700681 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800682
683 // Create the dex and odex files
684 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000685 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800686
687 // Strip the dex file
688 Copy(GetStrippedDexSrc1(), dex_location);
689
690 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000691 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800692
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000693 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
694 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800695
696 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
697 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
698 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
699 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
700 EXPECT_FALSE(oat_file_assistant.OatFileExists());
701 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
702 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700703 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800704
705 // Make the oat file up to date.
706 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700707 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
708 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800709
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000710 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
711 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800712
713 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
714 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
715 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
716 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
717 EXPECT_TRUE(oat_file_assistant.OatFileExists());
718 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
719 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700720 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800721
722 // Verify we can load the dex files from it.
723 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
724 ASSERT_TRUE(oat_file.get() != nullptr);
725 EXPECT_TRUE(oat_file->IsExecutable());
726 std::vector<std::unique_ptr<const DexFile>> dex_files;
727 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
728 EXPECT_EQ(1u, dex_files.size());
729}
730
Richard Uhler95abd042015-03-24 09:51:28 -0700731// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
732// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800733TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
734 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700735 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800736
737 // Create the oat file from a different dex file so it looks out of date.
738 Copy(GetDexSrc2(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000739 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800740
741 // Create the odex file
742 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000743 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800744
745 // Strip the dex file.
746 Copy(GetStrippedDexSrc1(), dex_location);
747
748 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000749 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800750
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000751 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
752 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
753 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
754 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
755 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
756 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800757
758 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
759 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
760 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
761 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
762 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
763 EXPECT_TRUE(oat_file_assistant.OatFileExists());
764 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
765 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700766 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800767
768 // Make the oat file up to date.
769 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700770 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
771 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800772
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000773 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
774 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
775 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
776 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800777
778 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
779 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
780 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
781 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
782 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
783 EXPECT_TRUE(oat_file_assistant.OatFileExists());
784 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
785 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
786 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700787 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800788
789 // Verify we can load the dex files from it.
790 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
791 ASSERT_TRUE(oat_file.get() != nullptr);
792 EXPECT_TRUE(oat_file->IsExecutable());
793 std::vector<std::unique_ptr<const DexFile>> dex_files;
794 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
795 EXPECT_EQ(1u, dex_files.size());
796}
797
Richard Uhler9b994ea2015-06-24 08:44:19 -0700798// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
799// OAT file. Expect: The status is kNoDexOptNeeded.
800TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
801 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
802
803 Copy(GetStrippedDexSrc1(), dex_location);
804
805 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000806 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700807
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000808 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
809 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
810 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
811 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
812 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
813 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700814
815 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
816 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
817 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
818 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
819 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
820 EXPECT_FALSE(oat_file_assistant.OatFileExists());
821 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
822 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
823 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
824
825 // Make the oat file up to date. This should have no effect.
826 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700827 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
828 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700829
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000830 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
831 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700832
833 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
834 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
835 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
836 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
837 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
838 EXPECT_FALSE(oat_file_assistant.OatFileExists());
839 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
840 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
841 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
842}
843
Richard Uhler95abd042015-03-24 09:51:28 -0700844// Case: We have a DEX file, no ODEX file and an OAT file that needs
845// relocation.
846// Expect: The status is kSelfPatchOatNeeded.
847TEST_F(OatFileAssistantTest, SelfRelocation) {
848 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
849 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
850
851 // Create the dex and odex files
852 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000853 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700854
855 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000856 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700857
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000858 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
859 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
860 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
861 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
862 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
863 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700864
865 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
866 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
867 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
868 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
869 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
870 EXPECT_TRUE(oat_file_assistant.OatFileExists());
871 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
872 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
873 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700874 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700875
876 // Make the oat file up to date.
877 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700878 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
879 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700880
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000881 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
882 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700883
884 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
885 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
886 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
887 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
888 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
889 EXPECT_TRUE(oat_file_assistant.OatFileExists());
890 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
891 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
892 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700893 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700894
895 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
896 ASSERT_TRUE(oat_file.get() != nullptr);
897 EXPECT_TRUE(oat_file->IsExecutable());
898 std::vector<std::unique_ptr<const DexFile>> dex_files;
899 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
900 EXPECT_EQ(1u, dex_files.size());
901}
902
Richard Uhler1c4eb042016-03-29 13:27:41 -0700903// Case: We have a DEX file, no ODEX file and an OAT file that needs
904// relocation but doesn't have patch info.
905// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
906TEST_F(OatFileAssistantTest, NoSelfRelocation) {
907 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
908 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
909
910 // Create the dex and odex files
911 Copy(GetDexSrc1(), dex_location);
912 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
913
914 OatFileAssistant oat_file_assistant(dex_location.c_str(),
915 oat_location.c_str(), kRuntimeISA, false, true);
916
917 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
918 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
919
920 // Make the oat file up to date.
921 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700922 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
923 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler1c4eb042016-03-29 13:27:41 -0700924 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
925 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
926
927 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
928 ASSERT_TRUE(oat_file.get() != nullptr);
929 EXPECT_TRUE(oat_file->IsExecutable());
930 std::vector<std::unique_ptr<const DexFile>> dex_files;
931 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
932 EXPECT_EQ(1u, dex_files.size());
933}
934
Richard Uhler66d874d2015-01-15 09:37:19 -0800935// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
936// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700937// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800938TEST_F(OatFileAssistantTest, OdexOatOverlap) {
939 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700940 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
941 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800942
943 // Create the dex and odex files
944 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000945 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800946
947 // Create the oat file by copying the odex so they are located in the same
948 // place in memory.
949 Copy(odex_location, oat_location);
950
951 // Verify things don't go bad.
952 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000953 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800954
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000955 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
956 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800957
958 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
959 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
960 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
961 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
962 EXPECT_TRUE(oat_file_assistant.OatFileExists());
963 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
964 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700965 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800966
967 // Things aren't relocated, so it should fall back to interpreted.
968 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
969 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700970
Richard Uhler66d874d2015-01-15 09:37:19 -0800971 EXPECT_FALSE(oat_file->IsExecutable());
972 std::vector<std::unique_ptr<const DexFile>> dex_files;
973 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
974 EXPECT_EQ(1u, dex_files.size());
975}
976
977// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700978// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800979TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
980 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700981 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800982
983 // Create the dex and odex files
984 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000985 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800986
987 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000988 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800989
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000990 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
991 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
992 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
993 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800994
995 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
996 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
997 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
998 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
999 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1000 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1001 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001002 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001003}
1004
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001005// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
1006// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
1007TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
1008 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
1009 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +00001010
1011 // Create the dex and odex files
1012 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001013 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +00001014
1015 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001016 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +00001017
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001018 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1019 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
1020 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1021 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +00001022
1023 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1024 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
1025 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
1026 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
1027 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1028 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1029 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1030 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
1031}
1032
Richard Uhler66d874d2015-01-15 09:37:19 -08001033// Case: We have a DEX file and up-to-date OAT file for it.
1034// Expect: We should load an executable dex file.
1035TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
1036 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1037
1038 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001039 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001040
1041 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001042 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
1043
1044 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1045 ASSERT_TRUE(oat_file.get() != nullptr);
1046 EXPECT_TRUE(oat_file->IsExecutable());
1047 std::vector<std::unique_ptr<const DexFile>> dex_files;
1048 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1049 EXPECT_EQ(1u, dex_files.size());
1050}
1051
1052// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
1053// Expect: We should still load the oat file as executable.
1054TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
1055 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1056
1057 Copy(GetDexSrc1(), dex_location);
1058 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
1059
1060 // Load the oat using an oat file assistant.
1061 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001062
1063 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1064 ASSERT_TRUE(oat_file.get() != nullptr);
1065 EXPECT_TRUE(oat_file->IsExecutable());
1066 std::vector<std::unique_ptr<const DexFile>> dex_files;
1067 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1068 EXPECT_EQ(1u, dex_files.size());
1069}
1070
1071// Case: We have a DEX file and up-to-date OAT file for it.
1072// Expect: Loading non-executable should load the oat non-executable.
1073TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
1074 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1075
1076 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001077 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001078
1079 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001080 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001081
1082 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1083 ASSERT_TRUE(oat_file.get() != nullptr);
1084 EXPECT_FALSE(oat_file->IsExecutable());
1085 std::vector<std::unique_ptr<const DexFile>> dex_files;
1086 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1087 EXPECT_EQ(1u, dex_files.size());
1088}
1089
1090// Case: We have a DEX file.
1091// Expect: We should load an executable dex file from an alternative oat
1092// location.
1093TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
1094 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
1095 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
1096
1097 Copy(GetDexSrc1(), dex_location);
1098
1099 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001100 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001101 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001102 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
1103 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -08001104
1105 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1106 ASSERT_TRUE(oat_file.get() != nullptr);
1107 EXPECT_TRUE(oat_file->IsExecutable());
1108 std::vector<std::unique_ptr<const DexFile>> dex_files;
1109 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1110 EXPECT_EQ(1u, dex_files.size());
1111
1112 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
1113
1114 // Verify it didn't create an oat in the default location.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001115 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001116 EXPECT_FALSE(ofm.OatFileExists());
1117}
1118
Richard Uhler8327cf72015-10-13 16:34:59 -07001119// Case: We have a DEX file but can't write the oat file.
1120// Expect: We should fail to make the oat file up to date.
1121TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
1122 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
1123
1124 // Make the oat location unwritable by inserting some non-existent
1125 // intermediate directories.
1126 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
1127
1128 Copy(GetDexSrc1(), dex_location);
1129
1130 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001131 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001132 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001133 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
1134 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001135
1136 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1137 ASSERT_TRUE(oat_file.get() == nullptr);
1138}
1139
1140// Case: We don't have a DEX file and can't write the oat file.
1141// Expect: We should fail to generate the oat file without crashing.
1142TEST_F(OatFileAssistantTest, GenNoDex) {
1143 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1144 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
1145
1146 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001147 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001148 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001149 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
1150 oat_file_assistant.GenerateOatFile(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001151}
1152
Richard Uhler66d874d2015-01-15 09:37:19 -08001153// Turn an absolute path into a path relative to the current working
1154// directory.
1155static std::string MakePathRelative(std::string target) {
1156 char buf[MAXPATHLEN];
1157 std::string cwd = getcwd(buf, MAXPATHLEN);
1158
1159 // Split the target and cwd paths into components.
1160 std::vector<std::string> target_path;
1161 std::vector<std::string> cwd_path;
1162 Split(target, '/', &target_path);
1163 Split(cwd, '/', &cwd_path);
1164
1165 // Reverse the path components, so we can use pop_back().
1166 std::reverse(target_path.begin(), target_path.end());
1167 std::reverse(cwd_path.begin(), cwd_path.end());
1168
1169 // Drop the common prefix of the paths. Because we reversed the path
1170 // components, this becomes the common suffix of target_path and cwd_path.
1171 while (!target_path.empty() && !cwd_path.empty()
1172 && target_path.back() == cwd_path.back()) {
1173 target_path.pop_back();
1174 cwd_path.pop_back();
1175 }
1176
1177 // For each element of the remaining cwd_path, add '..' to the beginning
1178 // of the target path. Because we reversed the path components, we add to
1179 // the end of target_path.
1180 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1181 target_path.push_back("..");
1182 }
1183
1184 // Reverse again to get the right path order, and join to get the result.
1185 std::reverse(target_path.begin(), target_path.end());
1186 return Join(target_path, '/');
1187}
1188
1189// Case: Non-absolute path to Dex location.
1190// Expect: Not sure, but it shouldn't crash.
1191TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1192 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1193 Copy(GetDexSrc1(), abs_dex_location);
1194
1195 std::string dex_location = MakePathRelative(abs_dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001196 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001197
1198 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001199 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1200 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001201 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1202 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1203 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1204 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1205 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1206 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1207}
1208
1209// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001210// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001211TEST_F(OatFileAssistantTest, ShortDexLocation) {
1212 std::string dex_location = "/xx";
1213
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001214 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001215
1216 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001217 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1218 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001219 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1220 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1221 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1222 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1223 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1224 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001225 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001226
Richard Uhler9b994ea2015-06-24 08:44:19 -07001227 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001228 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001229 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
1230 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001231 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001232}
1233
1234// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001235// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001236TEST_F(OatFileAssistantTest, LongDexExtension) {
1237 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1238 Copy(GetDexSrc1(), dex_location);
1239
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001240 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001241
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001242 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1243 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001244
1245 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1246 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1247 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1248 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1249 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1250 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1251 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1252}
1253
1254// A task to generate a dex location. Used by the RaceToGenerate test.
1255class RaceGenerateTask : public Task {
1256 public:
1257 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
1258 : dex_location_(dex_location), oat_location_(oat_location),
1259 loaded_oat_file_(nullptr)
1260 {}
1261
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001262 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001263 // Load the dex files, and save a pointer to the loaded oat file, so that
1264 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001265 std::vector<std::unique_ptr<const DexFile>> dex_files;
1266 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001267 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001268 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1269 dex_location_.c_str(),
1270 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001271 /*class_loader*/nullptr,
1272 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001273 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001274 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001275 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001276 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1277 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001278 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001279 }
1280
1281 const OatFile* GetLoadedOatFile() const {
1282 return loaded_oat_file_;
1283 }
1284
1285 private:
1286 std::string dex_location_;
1287 std::string oat_location_;
1288 const OatFile* loaded_oat_file_;
1289};
1290
1291// Test the case where multiple processes race to generate an oat file.
1292// This simulates multiple processes using multiple threads.
1293//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001294// We want unique Oat files to be loaded even when there is a race to load.
1295// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1296// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001297TEST_F(OatFileAssistantTest, RaceToGenerate) {
1298 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001299 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001300
1301 // We use the lib core dex file, because it's large, and hopefully should
1302 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001303 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001304
1305 const int kNumThreads = 32;
1306 Thread* self = Thread::Current();
1307 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1308 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1309 for (int i = 0; i < kNumThreads; i++) {
1310 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1311 thread_pool.AddTask(self, task.get());
1312 tasks.push_back(std::move(task));
1313 }
1314 thread_pool.StartWorkers(self);
1315 thread_pool.Wait(self, true, false);
1316
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001317 // Verify every task got a unique oat file.
1318 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001319 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001320 const OatFile* oat_file = task->GetLoadedOatFile();
1321 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1322 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001323 }
1324}
1325
1326// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1327// disabled.
1328// Expect: We should load the odex file non-executable.
1329TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1330 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001331 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001332
1333 // Create the dex and odex files
1334 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001335 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001336
1337 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001338 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001339
1340 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1341 ASSERT_TRUE(oat_file.get() != nullptr);
1342 EXPECT_FALSE(oat_file->IsExecutable());
1343 std::vector<std::unique_ptr<const DexFile>> dex_files;
1344 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1345 EXPECT_EQ(1u, dex_files.size());
1346}
1347
1348// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1349// disabled.
1350// Expect: We should load the odex file non-executable.
1351TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1352 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001353 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001354
1355 // Create the dex and odex files
1356 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001357 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001358
1359 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001360 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001361
1362 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1363 ASSERT_TRUE(oat_file.get() != nullptr);
1364 EXPECT_FALSE(oat_file->IsExecutable());
1365 std::vector<std::unique_ptr<const DexFile>> dex_files;
1366 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1367 EXPECT_EQ(2u, dex_files.size());
1368}
1369
1370TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1371 std::string error_msg;
1372 std::string odex_file;
1373
1374 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001375 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001376 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001377
1378 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001379 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001380 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001381
1382 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001383 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001384 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001385 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001386}
1387
Richard Uhler23cedd22015-04-08 13:17:29 -07001388// Verify the dexopt status values from dalvik.system.DexFile
1389// match the OatFileAssistant::DexOptStatus values.
1390TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1391 ScopedObjectAccess soa(Thread::Current());
1392 StackHandleScope<1> hs(soa.Self());
1393 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1394 Handle<mirror::Class> dexfile(
1395 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1396 ASSERT_FALSE(dexfile.Get() == nullptr);
1397 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1398
Mathieu Chartierc7853442015-03-27 14:35:38 -07001399 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001400 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1401 ASSERT_FALSE(no_dexopt_needed == nullptr);
1402 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1403 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1404
Mathieu Chartierc7853442015-03-27 14:35:38 -07001405 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001406 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1407 ASSERT_FALSE(dex2oat_needed == nullptr);
1408 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1409 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1410
Mathieu Chartierc7853442015-03-27 14:35:38 -07001411 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001412 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1413 ASSERT_FALSE(patchoat_needed == nullptr);
1414 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1415 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1416
Mathieu Chartierc7853442015-03-27 14:35:38 -07001417 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001418 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1419 ASSERT_FALSE(self_patchoat_needed == nullptr);
1420 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1421 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1422}
Richard Uhler66d874d2015-01-15 09:37:19 -08001423
1424// TODO: More Tests:
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001425// * Image checksum change is out of date for kIntepretOnly, but not
1426// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1427// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001428// * Test class linker falls back to unquickened dex for DexNoOat
1429// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001430// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001431// * Test for status of oat while oat is being generated (how?)
1432// * Test case where 32 and 64 bit boot class paths differ,
1433// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1434// 64 bit boot class paths.
1435// * Test unexpected scenarios (?):
1436// - Dex is stripped, don't have odex.
1437// - Oat file corrupted after status check, before reload unexecutable
1438// because it's unrelocated and no dex2oat
Calin Juravled91b8a22016-02-18 18:47:37 +00001439// * Test unrelocated specific target compilation type can be relocated to
1440// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001441
1442} // namespace art