blob: 156d394171d222e9b729a472b52907db500e5d8e [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
Richard Uhler66d874d2015-01-15 09:37:19 -080017#include <algorithm>
18#include <fstream>
19#include <string>
20#include <vector>
21#include <sys/param.h>
22
23#include <backtrace/BacktraceMap.h>
24#include <gtest/gtest.h>
25
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010027#include "class_linker-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070029#include "compiler_callbacks.h"
Andreas Gampe25a9abe2016-06-24 14:55:38 -070030#include "dex2oat_environment_test.h"
Richard Uhlerf16d5722015-05-11 09:32:47 -070031#include "gc/space/image_space.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032#include "mem_map.h"
Andreas Gampe25a9abe2016-06-24 14:55:38 -070033#include "oat_file_assistant.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
Andreas Gampe25a9abe2016-06-24 14:55:38 -070042class OatFileAssistantTest : public Dex2oatEnvironmentTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080043 public:
Andreas Gampe25a9abe2016-06-24 14:55:38 -070044 virtual void SetUp() OVERRIDE {
Richard Uhler66d874d2015-01-15 09:37:19 -080045 ReserveImageSpace();
Andreas Gampe25a9abe2016-06-24 14:55:38 -070046 Dex2oatEnvironmentTest::SetUp();
Richard Uhler66d874d2015-01-15 09:37:19 -080047 }
48
Richard Uhler7dfe5de2016-04-26 10:24:38 -070049 // Pre-Relocate the image to a known non-zero offset so we don't have to
50 // deal with the runtime randomly relocating the image by 0 and messing up
51 // the expected results of the tests.
52 bool PreRelocateImage(std::string* error_msg) {
53 std::string image;
54 if (!GetCachedImageFile(&image, error_msg)) {
55 return false;
56 }
57
58 std::string patchoat = GetAndroidRoot();
59 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
60
61 std::vector<std::string> argv;
62 argv.push_back(patchoat);
63 argv.push_back("--input-image-location=" + GetImageLocation());
64 argv.push_back("--output-image-file=" + image);
65 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
66 argv.push_back("--base-offset-delta=0x00008000");
67 return Exec(argv, error_msg);
68 }
69
Richard Uhler66d874d2015-01-15 09:37:19 -080070 virtual void PreRuntimeCreate() {
Richard Uhler7dfe5de2016-04-26 10:24:38 -070071 std::string error_msg;
72 ASSERT_TRUE(PreRelocateImage(&error_msg)) << error_msg;
73
Richard Uhler66d874d2015-01-15 09:37:19 -080074 UnreserveImageSpace();
75 }
76
Andreas Gampe25a9abe2016-06-24 14:55:38 -070077 virtual void PostRuntimeCreate() OVERRIDE {
Richard Uhler66d874d2015-01-15 09:37:19 -080078 ReserveImageSpace();
79 }
80
Richard Uhler93aa2102015-08-10 14:47:41 -070081 // Generate a non-PIC odex file for the purposes of test.
Richard Uhler94f5bda2015-07-22 08:25:11 -070082 // The generated odex file will be un-relocated.
Richard Uhler66d874d2015-01-15 09:37:19 -080083 void GenerateOdexForTest(const std::string& dex_location,
Andreas Gampe7bcfcb82016-03-23 15:31:51 +000084 const std::string& odex_location,
Richard Uhler7dfe5de2016-04-26 10:24:38 -070085 CompilerFilter::Filter filter,
86 bool pic = false,
87 bool with_patch_info = true) {
88 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
89 // relocated image file.
90 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
91 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
Richard Uhler93aa2102015-08-10 14:47:41 -070092 std::vector<std::string> args;
93 args.push_back("--dex-file=" + dex_location);
Richard Uhler7dfe5de2016-04-26 10:24:38 -070094 args.push_back("--oat-file=" + odex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +000095 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler7dfe5de2016-04-26 10:24:38 -070096 args.push_back("--runtime-arg");
97 args.push_back("-Xnorelocate");
Richard Uhler93aa2102015-08-10 14:47:41 -070098
Richard Uhler7dfe5de2016-04-26 10:24:38 -070099 if (pic) {
100 args.push_back("--compile-pic");
101 }
102
103 if (with_patch_info) {
104 args.push_back("--include-patch-information");
105 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700106
107 std::string error_msg;
108 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700109 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler93aa2102015-08-10 14:47:41 -0700110
111 // Verify the odex file was generated as expected and really is
112 // unrelocated.
Mathieu Chartierbcb6a722016-03-08 16:49:58 -0800113 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
114 odex_location.c_str(),
115 nullptr,
116 nullptr,
117 false,
118 /*low_4gb*/false,
119 dex_location.c_str(),
120 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700121 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700122 EXPECT_EQ(pic, odex_file->IsPic());
123 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000124 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
125
Vladimir Marko8c185bf2016-05-23 15:32:42 +0100126 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000127 const std::vector<gc::space::ImageSpace*> image_spaces =
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700128 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000129 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
130 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
131 const OatHeader& oat_header = odex_file->GetOatHeader();
Jeff Hao4f351aa2016-04-07 15:40:54 -0700132 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
133 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000134 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
135 oat_header.GetImageFileLocationOatDataBegin());
136 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
137 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700138 }
139
140 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000141 const std::string& odex_location,
142 CompilerFilter::Filter filter) {
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700143 GenerateOdexForTest(dex_location, odex_location, filter, true, false);
Calin Juravled91b8a22016-02-18 18:47:37 +0000144 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000145
Richard Uhler1c4eb042016-03-29 13:27:41 -0700146 // Generate a non-PIC odex file without patch information for the purposes
147 // of test. The generated odex file will be un-relocated.
Richard Uhler1c4eb042016-03-29 13:27:41 -0700148 void GenerateNoPatchOdexForTest(const std::string& dex_location,
149 const std::string& odex_location,
150 CompilerFilter::Filter filter) {
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700151 GenerateOdexForTest(dex_location, odex_location, filter, false, false);
Richard Uhler1c4eb042016-03-29 13:27:41 -0700152 }
153
Richard Uhler66d874d2015-01-15 09:37:19 -0800154 private:
155 // Reserve memory around where the image will be loaded so other memory
156 // won't conflict when it comes time to load the image.
157 // This can be called with an already loaded image to reserve the space
158 // around it.
159 void ReserveImageSpace() {
160 MemMap::Init();
161
162 // Ensure a chunk of memory is reserved for the image space.
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700163 // The reservation_end includes room for the main space that has to come
164 // right after the image in case of the GSS collector.
165 uintptr_t reservation_start = ART_BASE_ADDRESS;
166 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800167
Richard Uhler66d874d2015-01-15 09:37:19 -0800168 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
169 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
170 for (BacktraceMap::const_iterator it = map->begin();
171 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700172 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
173 reservation_start = std::max(reservation_start, it->end);
174 }
175 ReserveImageSpaceChunk(reservation_start, reservation_end);
176 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800177
Richard Uhler3efe9792015-03-30 16:18:03 -0700178 // Reserve a chunk of memory for the image space in the given range.
179 // Only has effect for chunks with a positive number of bytes.
180 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
181 if (start < end) {
182 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800183 image_reservation_.push_back(std::unique_ptr<MemMap>(
184 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700185 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700186 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800187 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
188 LOG(INFO) << "Reserved space for image " <<
189 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
190 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800191 }
192 }
193
194
195 // Unreserve any memory reserved by ReserveImageSpace. This should be called
196 // before the image is loaded.
197 void UnreserveImageSpace() {
198 image_reservation_.clear();
199 }
200
Richard Uhler66d874d2015-01-15 09:37:19 -0800201 std::vector<std::unique_ptr<MemMap>> image_reservation_;
202};
203
204class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
205 public:
206 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
207 OatFileAssistantTest::SetUpRuntimeOptions(options);
208 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
209 }
210};
211
212// Generate an oat file for the purposes of test, as opposed to testing
213// generation of oat files.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000214static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
215 // Use an oat file assistant to find the proper oat location.
216 OatFileAssistant ofa(dex_location, kRuntimeISA, false, false);
217 const std::string* oat_location = ofa.OatFileName();
218 ASSERT_TRUE(oat_location != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800219
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000220 std::vector<std::string> args;
221 args.push_back("--dex-file=" + std::string(dex_location));
222 args.push_back("--oat-file=" + *oat_location);
223 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
224 args.push_back("--runtime-arg");
225 args.push_back("-Xnorelocate");
Richard Uhler66d874d2015-01-15 09:37:19 -0800226 std::string error_msg;
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000227 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
228
229 // Verify the oat file was generated as expected.
230 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location->c_str(),
231 oat_location->c_str(),
232 nullptr,
233 nullptr,
234 false,
235 /*low_4gb*/false,
236 dex_location,
237 &error_msg));
238 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
239 EXPECT_EQ(filter, oat_file->GetCompilerFilter());
Richard Uhler66d874d2015-01-15 09:37:19 -0800240}
241
242// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700243// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800244TEST_F(OatFileAssistantTest, DexNoOat) {
245 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
246 Copy(GetDexSrc1(), dex_location);
247
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000248 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800249
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000250 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
251 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
252 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
253 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
254 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
255 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
256 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
257 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800258
259 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
260 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
261 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
262 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
263 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700264 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800265 EXPECT_FALSE(oat_file_assistant.OatFileExists());
266 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
267 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
268 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700269 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700270 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800271}
272
273// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700274// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800275TEST_F(OatFileAssistantTest, NoDexNoOat) {
276 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
277
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000278 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800279
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000280 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
281 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700282 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
283
284 // Trying to make the oat file up to date should not fail or crash.
285 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700286 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
287 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700288
289 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800290 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
291 EXPECT_EQ(nullptr, oat_file.get());
292}
293
294// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700295// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800296TEST_F(OatFileAssistantTest, OatUpToDate) {
297 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
298 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000299 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800300
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000301 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800302
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000303 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
304 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
305 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
306 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
307 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
308 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
309 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
310 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
311
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
313 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
314 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
315 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
316 EXPECT_TRUE(oat_file_assistant.OatFileExists());
317 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
318 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
319 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700320 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700321 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800322}
323
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000324// Case: We have a DEX file and speed-profile OAT file for it.
325// Expect: The status is kNoDexOptNeeded if the profile hasn't changed.
326TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
327 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
328 Copy(GetDexSrc1(), dex_location);
329 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
330
331 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
332
333 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
334 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
335 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
336 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
337
338 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
339 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
340 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
341 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
342 EXPECT_TRUE(oat_file_assistant.OatFileExists());
343 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
344 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
345 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
346 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
347 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
348}
349
350// Case: We have a DEX file and speed-profile OAT file for it.
351// Expect: The status is kNoDex2OatNeeded if the profile has changed.
352TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) {
353 std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar";
354 Copy(GetDexSrc1(), dex_location);
355 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
356
357 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false);
358
359 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
360 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
361 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
362 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
363
364 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
365 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
366 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
367 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
368 EXPECT_TRUE(oat_file_assistant.OatFileExists());
369 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
370 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
371 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
372 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
373 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
374}
375
Richard Uhler66d874d2015-01-15 09:37:19 -0800376// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700377// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800378TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
379 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
380 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000381 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800382
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000383 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
384 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
385 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700386 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700387
388 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700389 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800390 ASSERT_TRUE(oat_file.get() != nullptr);
391 EXPECT_TRUE(oat_file->IsExecutable());
392 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700393 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
394 EXPECT_EQ(2u, dex_files.size());
395}
396
Richard Uhler67ff7d12015-05-14 13:21:13 -0700397// Case: We have a MultiDEX file where the secondary dex file is out of date.
398// Expect: The status is kDex2OatNeeded.
399TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
400 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
401
402 // Compile code for GetMultiDexSrc1.
403 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000404 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700405
406 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
407 // is out of date.
408 Copy(GetMultiDexSrc2(), dex_location);
409
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000410 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
411 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
412 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700413 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700414}
415
Richard Uhlere5fed032015-03-18 08:21:11 -0700416// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
417// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700418// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700419TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
420 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700421 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700422
423 // Create the dex file
424 Copy(GetMultiDexSrc1(), dex_location);
425
426 // Create the oat file with relative encoded dex location.
427 std::vector<std::string> args;
428 args.push_back("--dex-file=" + dex_location);
429 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
430 args.push_back("--oat-file=" + oat_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000431 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700432
433 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700434 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700435
436 // Verify we can load both dex files.
437 OatFileAssistant oat_file_assistant(dex_location.c_str(),
438 oat_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000439 kRuntimeISA, false, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700440 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
441 ASSERT_TRUE(oat_file.get() != nullptr);
442 EXPECT_TRUE(oat_file->IsExecutable());
443 std::vector<std::unique_ptr<const DexFile>> dex_files;
444 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800445 EXPECT_EQ(2u, dex_files.size());
446}
447
Richard Uhler95abd042015-03-24 09:51:28 -0700448// Case: We have a DEX file and out-of-date OAT file.
449// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800450TEST_F(OatFileAssistantTest, OatOutOfDate) {
451 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
452
453 // We create a dex, generate an oat for it, then overwrite the dex with a
454 // different dex to make the oat out of date.
455 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000456 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800457 Copy(GetDexSrc2(), dex_location);
458
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000459 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
460 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
461 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
462 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
463 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800464
465 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
466 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
467 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
468 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
469 EXPECT_TRUE(oat_file_assistant.OatFileExists());
470 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
471 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700472 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800473}
474
475// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700476// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800477TEST_F(OatFileAssistantTest, DexOdexNoOat) {
478 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700479 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800480
481 // Create the dex and odex files
482 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000483 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800484
485 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000486 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800487
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000488 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
489 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
490 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
491 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800492
493 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
494 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
495 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
496 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
497 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 EXPECT_FALSE(oat_file_assistant.OatFileExists());
499 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
500 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700501 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700502
503 // We should still be able to get the non-executable odex file to run from.
504 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
505 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800506}
507
508// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700509// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800510TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
511 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700512 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800513
514 // Create the dex and odex files
515 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000516 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800517
518 // Strip the dex file
519 Copy(GetStrippedDexSrc1(), dex_location);
520
521 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000522 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800523
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000524 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
525 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800526
527 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
528 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
529 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
530 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
531 EXPECT_FALSE(oat_file_assistant.OatFileExists());
532 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
533 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700534 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800535
536 // Make the oat file up to date.
537 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700538 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
539 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800540
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000541 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
542 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800543
544 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
545 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
546 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
547 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
548 EXPECT_TRUE(oat_file_assistant.OatFileExists());
549 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
550 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700551 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800552
553 // Verify we can load the dex files from it.
554 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
555 ASSERT_TRUE(oat_file.get() != nullptr);
556 EXPECT_TRUE(oat_file->IsExecutable());
557 std::vector<std::unique_ptr<const DexFile>> dex_files;
558 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
559 EXPECT_EQ(1u, dex_files.size());
560}
561
Richard Uhler95abd042015-03-24 09:51:28 -0700562// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
563// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800564TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
565 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700566 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800567
568 // Create the oat file from a different dex file so it looks out of date.
569 Copy(GetDexSrc2(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000570 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800571
572 // Create the odex file
573 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000574 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800575
576 // Strip the dex file.
577 Copy(GetStrippedDexSrc1(), dex_location);
578
579 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000580 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800581
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000582 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
583 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
584 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
585 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
586 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
587 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800588
589 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
590 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
591 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
592 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
593 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
594 EXPECT_TRUE(oat_file_assistant.OatFileExists());
595 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
596 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700597 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800598
599 // Make the oat file up to date.
600 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700601 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
602 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800603
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000604 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
605 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
606 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
607 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
609 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
610 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
611 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
612 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
613 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
614 EXPECT_TRUE(oat_file_assistant.OatFileExists());
615 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
616 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
617 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700618 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800619
620 // Verify we can load the dex files from it.
621 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
622 ASSERT_TRUE(oat_file.get() != nullptr);
623 EXPECT_TRUE(oat_file->IsExecutable());
624 std::vector<std::unique_ptr<const DexFile>> dex_files;
625 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
626 EXPECT_EQ(1u, dex_files.size());
627}
628
Richard Uhler9b994ea2015-06-24 08:44:19 -0700629// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
630// OAT file. Expect: The status is kNoDexOptNeeded.
631TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
632 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
633
634 Copy(GetStrippedDexSrc1(), dex_location);
635
636 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000637 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700638
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000639 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
640 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
641 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
642 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
643 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
644 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700645
646 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
647 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
648 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
649 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
650 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
651 EXPECT_FALSE(oat_file_assistant.OatFileExists());
652 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
653 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
654 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
655
656 // Make the oat file up to date. This should have no effect.
657 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700658 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
659 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700660
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000661 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
662 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700663
664 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
665 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
666 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
667 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
668 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
669 EXPECT_FALSE(oat_file_assistant.OatFileExists());
670 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
671 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
672 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
673}
674
Richard Uhler95abd042015-03-24 09:51:28 -0700675// Case: We have a DEX file, no ODEX file and an OAT file that needs
676// relocation.
677// Expect: The status is kSelfPatchOatNeeded.
678TEST_F(OatFileAssistantTest, SelfRelocation) {
679 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
680 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
681
682 // Create the dex and odex files
683 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000684 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700685
686 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000687 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700688
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000689 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
690 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
691 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
692 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
693 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
694 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700695
696 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
697 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
698 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
699 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
700 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
701 EXPECT_TRUE(oat_file_assistant.OatFileExists());
702 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
703 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
704 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700705 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700706
707 // Make the oat file up to date.
708 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700709 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
710 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700711
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000712 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
713 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700714
715 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
716 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
717 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
718 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
719 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
720 EXPECT_TRUE(oat_file_assistant.OatFileExists());
721 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
722 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
723 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700724 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700725
726 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
727 ASSERT_TRUE(oat_file.get() != nullptr);
728 EXPECT_TRUE(oat_file->IsExecutable());
729 std::vector<std::unique_ptr<const DexFile>> dex_files;
730 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
731 EXPECT_EQ(1u, dex_files.size());
732}
733
Richard Uhler1c4eb042016-03-29 13:27:41 -0700734// Case: We have a DEX file, no ODEX file and an OAT file that needs
735// relocation but doesn't have patch info.
736// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
737TEST_F(OatFileAssistantTest, NoSelfRelocation) {
738 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
739 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
740
741 // Create the dex and odex files
742 Copy(GetDexSrc1(), dex_location);
743 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
744
745 OatFileAssistant oat_file_assistant(dex_location.c_str(),
746 oat_location.c_str(), kRuntimeISA, false, true);
747
748 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
749 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
750
751 // Make the oat file up to date.
752 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700753 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
754 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler1c4eb042016-03-29 13:27:41 -0700755 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
756 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
757
758 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
759 ASSERT_TRUE(oat_file.get() != nullptr);
760 EXPECT_TRUE(oat_file->IsExecutable());
761 std::vector<std::unique_ptr<const DexFile>> dex_files;
762 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
763 EXPECT_EQ(1u, dex_files.size());
764}
765
Richard Uhler66d874d2015-01-15 09:37:19 -0800766// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
767// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700768// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800769TEST_F(OatFileAssistantTest, OdexOatOverlap) {
770 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700771 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
772 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800773
774 // Create the dex and odex files
775 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000776 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800777
778 // Create the oat file by copying the odex so they are located in the same
779 // place in memory.
780 Copy(odex_location, oat_location);
781
782 // Verify things don't go bad.
783 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000784 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800785
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000786 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
787 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800788
789 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
790 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
791 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
792 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
793 EXPECT_TRUE(oat_file_assistant.OatFileExists());
794 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
795 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700796 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800797
798 // Things aren't relocated, so it should fall back to interpreted.
799 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
800 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700801
Richard Uhler66d874d2015-01-15 09:37:19 -0800802 EXPECT_FALSE(oat_file->IsExecutable());
803 std::vector<std::unique_ptr<const DexFile>> dex_files;
804 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
805 EXPECT_EQ(1u, dex_files.size());
806}
807
808// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700809// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800810TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
811 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700812 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800813
814 // Create the dex and odex files
815 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000816 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800817
818 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000819 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800820
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000821 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
822 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
823 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
824 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800825
826 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
827 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
828 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
829 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
830 EXPECT_FALSE(oat_file_assistant.OatFileExists());
831 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
832 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700833 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800834}
835
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000836// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
837// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
838TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
839 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
840 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000841
842 // Create the dex and odex files
843 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000844 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000845
846 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000847 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000848
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000849 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
850 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
851 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
852 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000853
854 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
855 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
856 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
857 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
858 EXPECT_FALSE(oat_file_assistant.OatFileExists());
859 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
860 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
861 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
862}
863
Richard Uhler66d874d2015-01-15 09:37:19 -0800864// Case: We have a DEX file and up-to-date OAT file for it.
865// Expect: We should load an executable dex file.
866TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
867 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
868
869 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000870 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800871
872 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000873 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
874
875 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
876 ASSERT_TRUE(oat_file.get() != nullptr);
877 EXPECT_TRUE(oat_file->IsExecutable());
878 std::vector<std::unique_ptr<const DexFile>> dex_files;
879 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
880 EXPECT_EQ(1u, dex_files.size());
881}
882
883// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
884// Expect: We should still load the oat file as executable.
885TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
886 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
887
888 Copy(GetDexSrc1(), dex_location);
889 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
890
891 // Load the oat using an oat file assistant.
892 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800893
894 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
895 ASSERT_TRUE(oat_file.get() != nullptr);
896 EXPECT_TRUE(oat_file->IsExecutable());
897 std::vector<std::unique_ptr<const DexFile>> dex_files;
898 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
899 EXPECT_EQ(1u, dex_files.size());
900}
901
902// Case: We have a DEX file and up-to-date OAT file for it.
903// Expect: Loading non-executable should load the oat non-executable.
904TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
905 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
906
907 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000908 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800909
910 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000911 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800912
913 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
914 ASSERT_TRUE(oat_file.get() != nullptr);
915 EXPECT_FALSE(oat_file->IsExecutable());
916 std::vector<std::unique_ptr<const DexFile>> dex_files;
917 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
918 EXPECT_EQ(1u, dex_files.size());
919}
920
921// Case: We have a DEX file.
922// Expect: We should load an executable dex file from an alternative oat
923// location.
924TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
925 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
926 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
927
928 Copy(GetDexSrc1(), dex_location);
929
930 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000931 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800932 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700933 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
934 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800935
936 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
937 ASSERT_TRUE(oat_file.get() != nullptr);
938 EXPECT_TRUE(oat_file->IsExecutable());
939 std::vector<std::unique_ptr<const DexFile>> dex_files;
940 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
941 EXPECT_EQ(1u, dex_files.size());
942
943 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
944
945 // Verify it didn't create an oat in the default location.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000946 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800947 EXPECT_FALSE(ofm.OatFileExists());
948}
949
Richard Uhler8327cf72015-10-13 16:34:59 -0700950// Case: We have a DEX file but can't write the oat file.
951// Expect: We should fail to make the oat file up to date.
952TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
953 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
954
955 // Make the oat location unwritable by inserting some non-existent
956 // intermediate directories.
957 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
958
959 Copy(GetDexSrc1(), dex_location);
960
961 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000962 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700963 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700964 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
965 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700966
967 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
968 ASSERT_TRUE(oat_file.get() == nullptr);
969}
970
971// Case: We don't have a DEX file and can't write the oat file.
972// Expect: We should fail to generate the oat file without crashing.
973TEST_F(OatFileAssistantTest, GenNoDex) {
974 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
975 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
976
977 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000978 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700979 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700980 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
981 oat_file_assistant.GenerateOatFile(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700982}
983
Richard Uhler66d874d2015-01-15 09:37:19 -0800984// Turn an absolute path into a path relative to the current working
985// directory.
986static std::string MakePathRelative(std::string target) {
987 char buf[MAXPATHLEN];
988 std::string cwd = getcwd(buf, MAXPATHLEN);
989
990 // Split the target and cwd paths into components.
991 std::vector<std::string> target_path;
992 std::vector<std::string> cwd_path;
993 Split(target, '/', &target_path);
994 Split(cwd, '/', &cwd_path);
995
996 // Reverse the path components, so we can use pop_back().
997 std::reverse(target_path.begin(), target_path.end());
998 std::reverse(cwd_path.begin(), cwd_path.end());
999
1000 // Drop the common prefix of the paths. Because we reversed the path
1001 // components, this becomes the common suffix of target_path and cwd_path.
1002 while (!target_path.empty() && !cwd_path.empty()
1003 && target_path.back() == cwd_path.back()) {
1004 target_path.pop_back();
1005 cwd_path.pop_back();
1006 }
1007
1008 // For each element of the remaining cwd_path, add '..' to the beginning
1009 // of the target path. Because we reversed the path components, we add to
1010 // the end of target_path.
1011 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1012 target_path.push_back("..");
1013 }
1014
1015 // Reverse again to get the right path order, and join to get the result.
1016 std::reverse(target_path.begin(), target_path.end());
1017 return Join(target_path, '/');
1018}
1019
1020// Case: Non-absolute path to Dex location.
1021// Expect: Not sure, but it shouldn't crash.
1022TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1023 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1024 Copy(GetDexSrc1(), abs_dex_location);
1025
1026 std::string dex_location = MakePathRelative(abs_dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001027 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001028
1029 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001030 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1031 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001032 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1033 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1034 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1035 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1036 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1037 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1038}
1039
1040// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001041// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001042TEST_F(OatFileAssistantTest, ShortDexLocation) {
1043 std::string dex_location = "/xx";
1044
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001045 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001046
1047 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001048 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1049 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001050 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1051 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1052 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1053 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1054 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1055 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001056 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001057
Richard Uhler9b994ea2015-06-24 08:44:19 -07001058 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001059 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001060 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
1061 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001062 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001063}
1064
1065// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001066// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001067TEST_F(OatFileAssistantTest, LongDexExtension) {
1068 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1069 Copy(GetDexSrc1(), dex_location);
1070
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001071 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001072
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001073 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1074 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001075
1076 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1077 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1078 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1079 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1080 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1081 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1082 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1083}
1084
1085// A task to generate a dex location. Used by the RaceToGenerate test.
1086class RaceGenerateTask : public Task {
1087 public:
1088 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Hao5872d7c2016-04-27 11:07:41 -07001089 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001090 {}
1091
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001092 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001093 // Load the dex files, and save a pointer to the loaded oat file, so that
1094 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001095 std::vector<std::unique_ptr<const DexFile>> dex_files;
1096 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001097 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001098 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1099 dex_location_.c_str(),
1100 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001101 /*class_loader*/nullptr,
1102 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001103 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001104 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001105 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001106 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1107 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001108 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001109 }
1110
1111 const OatFile* GetLoadedOatFile() const {
1112 return loaded_oat_file_;
1113 }
1114
1115 private:
1116 std::string dex_location_;
1117 std::string oat_location_;
1118 const OatFile* loaded_oat_file_;
1119};
1120
1121// Test the case where multiple processes race to generate an oat file.
1122// This simulates multiple processes using multiple threads.
1123//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001124// We want unique Oat files to be loaded even when there is a race to load.
1125// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1126// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001127TEST_F(OatFileAssistantTest, RaceToGenerate) {
1128 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001129 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001130
1131 // We use the lib core dex file, because it's large, and hopefully should
1132 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001133 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001134
1135 const int kNumThreads = 32;
1136 Thread* self = Thread::Current();
1137 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1138 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1139 for (int i = 0; i < kNumThreads; i++) {
1140 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1141 thread_pool.AddTask(self, task.get());
1142 tasks.push_back(std::move(task));
1143 }
1144 thread_pool.StartWorkers(self);
1145 thread_pool.Wait(self, true, false);
1146
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001147 // Verify every task got a unique oat file.
1148 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001149 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001150 const OatFile* oat_file = task->GetLoadedOatFile();
1151 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1152 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001153 }
1154}
1155
1156// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1157// disabled.
1158// Expect: We should load the odex file non-executable.
1159TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1160 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001161 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001162
1163 // Create the dex and odex files
1164 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001165 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001166
1167 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001168 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001169
1170 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1171 ASSERT_TRUE(oat_file.get() != nullptr);
1172 EXPECT_FALSE(oat_file->IsExecutable());
1173 std::vector<std::unique_ptr<const DexFile>> dex_files;
1174 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1175 EXPECT_EQ(1u, dex_files.size());
1176}
1177
1178// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1179// disabled.
1180// Expect: We should load the odex file non-executable.
1181TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1182 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001183 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001184
1185 // Create the dex and odex files
1186 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001187 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001188
1189 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001190 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001191
1192 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1193 ASSERT_TRUE(oat_file.get() != nullptr);
1194 EXPECT_FALSE(oat_file->IsExecutable());
1195 std::vector<std::unique_ptr<const DexFile>> dex_files;
1196 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1197 EXPECT_EQ(2u, dex_files.size());
1198}
1199
1200TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1201 std::string error_msg;
1202 std::string odex_file;
1203
1204 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001205 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001206 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001207
1208 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001209 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001210 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001211
1212 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001213 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001214 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001215 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001216}
1217
Richard Uhler23cedd22015-04-08 13:17:29 -07001218// Verify the dexopt status values from dalvik.system.DexFile
1219// match the OatFileAssistant::DexOptStatus values.
1220TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1221 ScopedObjectAccess soa(Thread::Current());
1222 StackHandleScope<1> hs(soa.Self());
1223 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1224 Handle<mirror::Class> dexfile(
1225 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1226 ASSERT_FALSE(dexfile.Get() == nullptr);
1227 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1228
Mathieu Chartierc7853442015-03-27 14:35:38 -07001229 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001230 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1231 ASSERT_FALSE(no_dexopt_needed == nullptr);
1232 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1233 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1234
Mathieu Chartierc7853442015-03-27 14:35:38 -07001235 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001236 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1237 ASSERT_FALSE(dex2oat_needed == nullptr);
1238 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1239 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1240
Mathieu Chartierc7853442015-03-27 14:35:38 -07001241 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001242 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1243 ASSERT_FALSE(patchoat_needed == nullptr);
1244 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1245 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1246
Mathieu Chartierc7853442015-03-27 14:35:38 -07001247 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001248 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1249 ASSERT_FALSE(self_patchoat_needed == nullptr);
1250 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1251 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1252}
Richard Uhler66d874d2015-01-15 09:37:19 -08001253
1254// TODO: More Tests:
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001255// * Image checksum change is out of date for kIntepretOnly, but not
1256// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1257// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001258// * Test class linker falls back to unquickened dex for DexNoOat
1259// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001260// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001261// * Test for status of oat while oat is being generated (how?)
1262// * Test case where 32 and 64 bit boot class paths differ,
1263// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1264// 64 bit boot class paths.
1265// * Test unexpected scenarios (?):
1266// - Dex is stripped, don't have odex.
1267// - Oat file corrupted after status check, before reload unexecutable
1268// because it's unrelocated and no dex2oat
Calin Juravled91b8a22016-02-18 18:47:37 +00001269// * Test unrelocated specific target compilation type can be relocated to
1270// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001271
1272} // namespace art