blob: c2478120f62323d412c515c388d0b15e65f69aaa [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;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000455 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700456
457 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800458 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
459 EXPECT_EQ(nullptr, oat_file.get());
460}
461
462// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700463// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800464TEST_F(OatFileAssistantTest, OatUpToDate) {
465 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
466 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000467 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800468
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000469 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800470
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000471 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
472 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
473 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
474 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
475 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
476 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
477 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
478 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
479
Richard Uhler66d874d2015-01-15 09:37:19 -0800480 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
481 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
482 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
483 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
484 EXPECT_TRUE(oat_file_assistant.OatFileExists());
485 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
486 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
487 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700488 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700489 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800490}
491
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000492// Case: We have a DEX file and speed-profile OAT file for it.
493// Expect: The status is kNoDexOptNeeded if the profile hasn't changed.
494TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
495 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
496 Copy(GetDexSrc1(), dex_location);
497 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
498
499 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
500
501 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
502 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
503 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
504 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
505
506 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
507 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
508 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
509 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
510 EXPECT_TRUE(oat_file_assistant.OatFileExists());
511 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
512 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
513 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
514 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
515 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
516}
517
518// Case: We have a DEX file and speed-profile OAT file for it.
519// Expect: The status is kNoDex2OatNeeded if the profile has changed.
520TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) {
521 std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar";
522 Copy(GetDexSrc1(), dex_location);
523 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
524
525 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false);
526
527 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
528 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
529 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
530 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
531
532 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
533 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
534 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
535 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
536 EXPECT_TRUE(oat_file_assistant.OatFileExists());
537 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
538 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
539 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
540 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
541 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
542}
543
Richard Uhler66d874d2015-01-15 09:37:19 -0800544// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700545// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800546TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
547 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
548 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000549 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800550
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000551 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
552 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
553 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700554 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700555
556 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700557 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800558 ASSERT_TRUE(oat_file.get() != nullptr);
559 EXPECT_TRUE(oat_file->IsExecutable());
560 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700561 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
562 EXPECT_EQ(2u, dex_files.size());
563}
564
Richard Uhler67ff7d12015-05-14 13:21:13 -0700565// Case: We have a MultiDEX file where the secondary dex file is out of date.
566// Expect: The status is kDex2OatNeeded.
567TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
568 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
569
570 // Compile code for GetMultiDexSrc1.
571 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000572 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700573
574 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
575 // is out of date.
576 Copy(GetMultiDexSrc2(), dex_location);
577
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000578 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
579 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
580 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700581 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700582}
583
Richard Uhlere5fed032015-03-18 08:21:11 -0700584// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
585// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700586// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700587TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
588 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700589 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700590
591 // Create the dex file
592 Copy(GetMultiDexSrc1(), dex_location);
593
594 // Create the oat file with relative encoded dex location.
595 std::vector<std::string> args;
596 args.push_back("--dex-file=" + dex_location);
597 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
598 args.push_back("--oat-file=" + oat_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000599 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700600
601 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700602 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700603
604 // Verify we can load both dex files.
605 OatFileAssistant oat_file_assistant(dex_location.c_str(),
606 oat_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000607 kRuntimeISA, false, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700608 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
609 ASSERT_TRUE(oat_file.get() != nullptr);
610 EXPECT_TRUE(oat_file->IsExecutable());
611 std::vector<std::unique_ptr<const DexFile>> dex_files;
612 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800613 EXPECT_EQ(2u, dex_files.size());
614}
615
Richard Uhler95abd042015-03-24 09:51:28 -0700616// Case: We have a DEX file and out-of-date OAT file.
617// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800618TEST_F(OatFileAssistantTest, OatOutOfDate) {
619 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
620
621 // We create a dex, generate an oat for it, then overwrite the dex with a
622 // different dex to make the oat out of date.
623 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000624 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800625 Copy(GetDexSrc2(), dex_location);
626
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000627 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
628 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
629 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
630 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
631 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800632
633 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
634 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
635 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
636 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
637 EXPECT_TRUE(oat_file_assistant.OatFileExists());
638 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
639 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700640 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800641}
642
643// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700644// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800645TEST_F(OatFileAssistantTest, DexOdexNoOat) {
646 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700647 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800648
649 // Create the dex and odex files
650 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000651 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800652
653 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000654 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800655
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000656 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
657 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
658 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
659 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800660
661 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
662 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
663 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
664 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
665 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800666 EXPECT_FALSE(oat_file_assistant.OatFileExists());
667 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
668 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700669 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700670
671 // We should still be able to get the non-executable odex file to run from.
672 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
673 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800674}
675
676// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700677// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800678TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
679 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700680 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800681
682 // Create the dex and odex files
683 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000684 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800685
686 // Strip the dex file
687 Copy(GetStrippedDexSrc1(), dex_location);
688
689 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000690 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800691
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000692 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
693 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800694
695 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
696 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
697 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
698 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
699 EXPECT_FALSE(oat_file_assistant.OatFileExists());
700 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
701 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700702 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800703
704 // Make the oat file up to date.
705 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000706 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800707
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000708 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
709 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800710
711 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
712 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
713 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
714 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
715 EXPECT_TRUE(oat_file_assistant.OatFileExists());
716 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
717 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700718 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800719
720 // Verify we can load the dex files from it.
721 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
722 ASSERT_TRUE(oat_file.get() != nullptr);
723 EXPECT_TRUE(oat_file->IsExecutable());
724 std::vector<std::unique_ptr<const DexFile>> dex_files;
725 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
726 EXPECT_EQ(1u, dex_files.size());
727}
728
Richard Uhler95abd042015-03-24 09:51:28 -0700729// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
730// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800731TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
732 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700733 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800734
735 // Create the oat file from a different dex file so it looks out of date.
736 Copy(GetDexSrc2(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000737 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800738
739 // Create the odex file
740 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000741 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800742
743 // Strip the dex file.
744 Copy(GetStrippedDexSrc1(), dex_location);
745
746 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000747 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800748
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000749 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
750 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
751 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
752 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
753 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
754 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800755
756 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
757 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
758 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
759 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
760 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
761 EXPECT_TRUE(oat_file_assistant.OatFileExists());
762 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
763 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700764 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800765
766 // Make the oat file up to date.
767 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000768 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800769
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000770 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
771 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
772 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
773 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800774
775 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
776 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
777 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
778 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
779 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
780 EXPECT_TRUE(oat_file_assistant.OatFileExists());
781 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
782 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
783 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700784 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800785
786 // Verify we can load the dex files from it.
787 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
788 ASSERT_TRUE(oat_file.get() != nullptr);
789 EXPECT_TRUE(oat_file->IsExecutable());
790 std::vector<std::unique_ptr<const DexFile>> dex_files;
791 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
792 EXPECT_EQ(1u, dex_files.size());
793}
794
Richard Uhler9b994ea2015-06-24 08:44:19 -0700795// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
796// OAT file. Expect: The status is kNoDexOptNeeded.
797TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
798 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
799
800 Copy(GetStrippedDexSrc1(), dex_location);
801
802 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000803 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700804
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000805 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
806 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
807 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
808 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
809 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
810 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700811
812 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
813 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
814 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
815 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
816 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
817 EXPECT_FALSE(oat_file_assistant.OatFileExists());
818 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
819 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
820 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
821
822 // Make the oat file up to date. This should have no effect.
823 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000824 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700825
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000826 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
827 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700828
829 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
830 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
831 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
832 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
833 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
834 EXPECT_FALSE(oat_file_assistant.OatFileExists());
835 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
836 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
837 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
838}
839
Richard Uhler95abd042015-03-24 09:51:28 -0700840// Case: We have a DEX file, no ODEX file and an OAT file that needs
841// relocation.
842// Expect: The status is kSelfPatchOatNeeded.
843TEST_F(OatFileAssistantTest, SelfRelocation) {
844 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
845 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
846
847 // Create the dex and odex files
848 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000849 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700850
851 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000852 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700853
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000854 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
855 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
856 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
857 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
858 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
859 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700860
861 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
862 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
863 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
864 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
865 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
866 EXPECT_TRUE(oat_file_assistant.OatFileExists());
867 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
868 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
869 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700870 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700871
872 // Make the oat file up to date.
873 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000874 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700875
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000876 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
877 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700878
879 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
880 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
881 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
882 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
883 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
884 EXPECT_TRUE(oat_file_assistant.OatFileExists());
885 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
886 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
887 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700888 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700889
890 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
891 ASSERT_TRUE(oat_file.get() != nullptr);
892 EXPECT_TRUE(oat_file->IsExecutable());
893 std::vector<std::unique_ptr<const DexFile>> dex_files;
894 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
895 EXPECT_EQ(1u, dex_files.size());
896}
897
Richard Uhler1c4eb042016-03-29 13:27:41 -0700898// Case: We have a DEX file, no ODEX file and an OAT file that needs
899// relocation but doesn't have patch info.
900// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
901TEST_F(OatFileAssistantTest, NoSelfRelocation) {
902 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
903 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
904
905 // Create the dex and odex files
906 Copy(GetDexSrc1(), dex_location);
907 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
908
909 OatFileAssistant oat_file_assistant(dex_location.c_str(),
910 oat_location.c_str(), kRuntimeISA, false, true);
911
912 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
913 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
914
915 // Make the oat file up to date.
916 std::string error_msg;
917 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
918 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
919 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
920
921 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
922 ASSERT_TRUE(oat_file.get() != nullptr);
923 EXPECT_TRUE(oat_file->IsExecutable());
924 std::vector<std::unique_ptr<const DexFile>> dex_files;
925 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
926 EXPECT_EQ(1u, dex_files.size());
927}
928
Richard Uhler66d874d2015-01-15 09:37:19 -0800929// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
930// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700931// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800932TEST_F(OatFileAssistantTest, OdexOatOverlap) {
933 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700934 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
935 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800936
937 // Create the dex and odex files
938 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000939 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800940
941 // Create the oat file by copying the odex so they are located in the same
942 // place in memory.
943 Copy(odex_location, oat_location);
944
945 // Verify things don't go bad.
946 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000947 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800948
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000949 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
950 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800951
952 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
953 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
954 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
955 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
956 EXPECT_TRUE(oat_file_assistant.OatFileExists());
957 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
958 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700959 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800960
961 // Things aren't relocated, so it should fall back to interpreted.
962 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
963 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700964
Richard Uhler66d874d2015-01-15 09:37:19 -0800965 EXPECT_FALSE(oat_file->IsExecutable());
966 std::vector<std::unique_ptr<const DexFile>> dex_files;
967 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
968 EXPECT_EQ(1u, dex_files.size());
969}
970
971// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700972// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800973TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
974 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700975 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800976
977 // Create the dex and odex files
978 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000979 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800980
981 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000982 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800983
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000984 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
985 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
986 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
987 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800988
989 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
990 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
991 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
992 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
993 EXPECT_FALSE(oat_file_assistant.OatFileExists());
994 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
995 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700996 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800997}
998
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000999// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
1000// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
1001TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
1002 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
1003 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +00001004
1005 // Create the dex and odex files
1006 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001007 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +00001008
1009 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001010 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +00001011
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001012 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1013 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
1014 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1015 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +00001016
1017 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1018 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
1019 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
1020 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
1021 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1022 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1023 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1024 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
1025}
1026
Richard Uhler66d874d2015-01-15 09:37:19 -08001027// Case: We have a DEX file and up-to-date OAT file for it.
1028// Expect: We should load an executable dex file.
1029TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
1030 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1031
1032 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001033 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001034
1035 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001036 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
1037
1038 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1039 ASSERT_TRUE(oat_file.get() != nullptr);
1040 EXPECT_TRUE(oat_file->IsExecutable());
1041 std::vector<std::unique_ptr<const DexFile>> dex_files;
1042 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1043 EXPECT_EQ(1u, dex_files.size());
1044}
1045
1046// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
1047// Expect: We should still load the oat file as executable.
1048TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
1049 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1050
1051 Copy(GetDexSrc1(), dex_location);
1052 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
1053
1054 // Load the oat using an oat file assistant.
1055 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001056
1057 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1058 ASSERT_TRUE(oat_file.get() != nullptr);
1059 EXPECT_TRUE(oat_file->IsExecutable());
1060 std::vector<std::unique_ptr<const DexFile>> dex_files;
1061 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1062 EXPECT_EQ(1u, dex_files.size());
1063}
1064
1065// Case: We have a DEX file and up-to-date OAT file for it.
1066// Expect: Loading non-executable should load the oat non-executable.
1067TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
1068 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1069
1070 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001071 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001072
1073 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001074 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001075
1076 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1077 ASSERT_TRUE(oat_file.get() != nullptr);
1078 EXPECT_FALSE(oat_file->IsExecutable());
1079 std::vector<std::unique_ptr<const DexFile>> dex_files;
1080 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1081 EXPECT_EQ(1u, dex_files.size());
1082}
1083
1084// Case: We have a DEX file.
1085// Expect: We should load an executable dex file from an alternative oat
1086// location.
1087TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
1088 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
1089 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
1090
1091 Copy(GetDexSrc1(), dex_location);
1092
1093 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001094 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001095 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001096 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -08001097
1098 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1099 ASSERT_TRUE(oat_file.get() != nullptr);
1100 EXPECT_TRUE(oat_file->IsExecutable());
1101 std::vector<std::unique_ptr<const DexFile>> dex_files;
1102 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1103 EXPECT_EQ(1u, dex_files.size());
1104
1105 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
1106
1107 // Verify it didn't create an oat in the default location.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001108 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001109 EXPECT_FALSE(ofm.OatFileExists());
1110}
1111
Richard Uhler8327cf72015-10-13 16:34:59 -07001112// Case: We have a DEX file but can't write the oat file.
1113// Expect: We should fail to make the oat file up to date.
1114TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
1115 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
1116
1117 // Make the oat location unwritable by inserting some non-existent
1118 // intermediate directories.
1119 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
1120
1121 Copy(GetDexSrc1(), dex_location);
1122
1123 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001124 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001125 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001126 ASSERT_FALSE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001127
1128 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1129 ASSERT_TRUE(oat_file.get() == nullptr);
1130}
1131
1132// Case: We don't have a DEX file and can't write the oat file.
1133// Expect: We should fail to generate the oat file without crashing.
1134TEST_F(OatFileAssistantTest, GenNoDex) {
1135 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1136 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
1137
1138 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001139 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001140 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001141 ASSERT_FALSE(oat_file_assistant.GenerateOatFile(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001142}
1143
Richard Uhler66d874d2015-01-15 09:37:19 -08001144// Turn an absolute path into a path relative to the current working
1145// directory.
1146static std::string MakePathRelative(std::string target) {
1147 char buf[MAXPATHLEN];
1148 std::string cwd = getcwd(buf, MAXPATHLEN);
1149
1150 // Split the target and cwd paths into components.
1151 std::vector<std::string> target_path;
1152 std::vector<std::string> cwd_path;
1153 Split(target, '/', &target_path);
1154 Split(cwd, '/', &cwd_path);
1155
1156 // Reverse the path components, so we can use pop_back().
1157 std::reverse(target_path.begin(), target_path.end());
1158 std::reverse(cwd_path.begin(), cwd_path.end());
1159
1160 // Drop the common prefix of the paths. Because we reversed the path
1161 // components, this becomes the common suffix of target_path and cwd_path.
1162 while (!target_path.empty() && !cwd_path.empty()
1163 && target_path.back() == cwd_path.back()) {
1164 target_path.pop_back();
1165 cwd_path.pop_back();
1166 }
1167
1168 // For each element of the remaining cwd_path, add '..' to the beginning
1169 // of the target path. Because we reversed the path components, we add to
1170 // the end of target_path.
1171 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1172 target_path.push_back("..");
1173 }
1174
1175 // Reverse again to get the right path order, and join to get the result.
1176 std::reverse(target_path.begin(), target_path.end());
1177 return Join(target_path, '/');
1178}
1179
1180// Case: Non-absolute path to Dex location.
1181// Expect: Not sure, but it shouldn't crash.
1182TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1183 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1184 Copy(GetDexSrc1(), abs_dex_location);
1185
1186 std::string dex_location = MakePathRelative(abs_dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001187 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001188
1189 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001190 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1191 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001192 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1193 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1194 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1195 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1196 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1197 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1198}
1199
1200// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001201// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001202TEST_F(OatFileAssistantTest, ShortDexLocation) {
1203 std::string dex_location = "/xx";
1204
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001205 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001206
1207 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001208 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1209 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001210 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1211 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1212 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1213 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1214 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1215 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001216 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001217
Richard Uhler9b994ea2015-06-24 08:44:19 -07001218 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001219 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001220 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001221 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001222}
1223
1224// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001225// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001226TEST_F(OatFileAssistantTest, LongDexExtension) {
1227 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1228 Copy(GetDexSrc1(), dex_location);
1229
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001230 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001231
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001232 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1233 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001234
1235 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1236 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1237 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1238 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1239 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1240 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1241 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1242}
1243
1244// A task to generate a dex location. Used by the RaceToGenerate test.
1245class RaceGenerateTask : public Task {
1246 public:
1247 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
1248 : dex_location_(dex_location), oat_location_(oat_location),
1249 loaded_oat_file_(nullptr)
1250 {}
1251
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001252 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001253 // Load the dex files, and save a pointer to the loaded oat file, so that
1254 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001255 std::vector<std::unique_ptr<const DexFile>> dex_files;
1256 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001257 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001258 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1259 dex_location_.c_str(),
1260 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001261 /*class_loader*/nullptr,
1262 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001263 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001264 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001265 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001266 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1267 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001268 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001269 }
1270
1271 const OatFile* GetLoadedOatFile() const {
1272 return loaded_oat_file_;
1273 }
1274
1275 private:
1276 std::string dex_location_;
1277 std::string oat_location_;
1278 const OatFile* loaded_oat_file_;
1279};
1280
1281// Test the case where multiple processes race to generate an oat file.
1282// This simulates multiple processes using multiple threads.
1283//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001284// We want unique Oat files to be loaded even when there is a race to load.
1285// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1286// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001287TEST_F(OatFileAssistantTest, RaceToGenerate) {
1288 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001289 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001290
1291 // We use the lib core dex file, because it's large, and hopefully should
1292 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001293 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001294
1295 const int kNumThreads = 32;
1296 Thread* self = Thread::Current();
1297 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1298 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1299 for (int i = 0; i < kNumThreads; i++) {
1300 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1301 thread_pool.AddTask(self, task.get());
1302 tasks.push_back(std::move(task));
1303 }
1304 thread_pool.StartWorkers(self);
1305 thread_pool.Wait(self, true, false);
1306
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001307 // Verify every task got a unique oat file.
1308 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001309 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001310 const OatFile* oat_file = task->GetLoadedOatFile();
1311 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1312 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001313 }
1314}
1315
1316// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1317// disabled.
1318// Expect: We should load the odex file non-executable.
1319TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1320 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001321 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001322
1323 // Create the dex and odex files
1324 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001325 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001326
1327 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001328 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001329
1330 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1331 ASSERT_TRUE(oat_file.get() != nullptr);
1332 EXPECT_FALSE(oat_file->IsExecutable());
1333 std::vector<std::unique_ptr<const DexFile>> dex_files;
1334 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1335 EXPECT_EQ(1u, dex_files.size());
1336}
1337
1338// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1339// disabled.
1340// Expect: We should load the odex file non-executable.
1341TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1342 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001343 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001344
1345 // Create the dex and odex files
1346 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001347 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001348
1349 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001350 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001351
1352 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1353 ASSERT_TRUE(oat_file.get() != nullptr);
1354 EXPECT_FALSE(oat_file->IsExecutable());
1355 std::vector<std::unique_ptr<const DexFile>> dex_files;
1356 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1357 EXPECT_EQ(2u, dex_files.size());
1358}
1359
1360TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1361 std::string error_msg;
1362 std::string odex_file;
1363
1364 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001365 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001366 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001367
1368 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001369 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001370 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001371
1372 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001373 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001374 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001375 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001376}
1377
Richard Uhler23cedd22015-04-08 13:17:29 -07001378// Verify the dexopt status values from dalvik.system.DexFile
1379// match the OatFileAssistant::DexOptStatus values.
1380TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1381 ScopedObjectAccess soa(Thread::Current());
1382 StackHandleScope<1> hs(soa.Self());
1383 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1384 Handle<mirror::Class> dexfile(
1385 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1386 ASSERT_FALSE(dexfile.Get() == nullptr);
1387 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1388
Mathieu Chartierc7853442015-03-27 14:35:38 -07001389 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001390 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1391 ASSERT_FALSE(no_dexopt_needed == nullptr);
1392 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1393 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1394
Mathieu Chartierc7853442015-03-27 14:35:38 -07001395 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001396 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1397 ASSERT_FALSE(dex2oat_needed == nullptr);
1398 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1399 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1400
Mathieu Chartierc7853442015-03-27 14:35:38 -07001401 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001402 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1403 ASSERT_FALSE(patchoat_needed == nullptr);
1404 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1405 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1406
Mathieu Chartierc7853442015-03-27 14:35:38 -07001407 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001408 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1409 ASSERT_FALSE(self_patchoat_needed == nullptr);
1410 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1411 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1412}
Richard Uhler66d874d2015-01-15 09:37:19 -08001413
1414// TODO: More Tests:
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001415// * Image checksum change is out of date for kIntepretOnly, but not
1416// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1417// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001418// * Test class linker falls back to unquickened dex for DexNoOat
1419// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001420// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001421// * Test for status of oat while oat is being generated (how?)
1422// * Test case where 32 and 64 bit boot class paths differ,
1423// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1424// 64 bit boot class paths.
1425// * Test unexpected scenarios (?):
1426// - Dex is stripped, don't have odex.
1427// - Oat file corrupted after status check, before reload unexecutable
1428// because it's unrelocated and no dex2oat
Calin Juravled91b8a22016-02-18 18:47:37 +00001429// * Test unrelocated specific target compilation type can be relocated to
1430// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001431
1432} // namespace art