blob: b2b86ee289c8e94a17bf8a82508ab46709f5fe57 [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 <string>
18#include <vector>
19#include <sys/param.h>
20
Andreas Gampe9186ced2016-12-12 14:28:21 -080021#include "android-base/strings.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080022#include <gtest/gtest.h>
23
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010025#include "class_linker-inl.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080026#include "dexopt_test.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070027#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070028#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080031#include "thread-inl.h"
32#include "utils.h"
33
34namespace art {
35
Calin Juravle36eb3132017-01-13 16:32:38 -080036class OatFileAssistantTest : public DexoptTest {};
Richard Uhler66d874d2015-01-15 09:37:19 -080037
Calin Juravle36eb3132017-01-13 16:32:38 -080038class OatFileAssistantNoDex2OatTest : public DexoptTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080039 public:
40 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Calin Juravle36eb3132017-01-13 16:32:38 -080041 DexoptTest::SetUpRuntimeOptions(options);
Richard Uhler66d874d2015-01-15 09:37:19 -080042 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
43 }
44};
45
Calin Juravle357c66d2017-05-04 01:57:17 +000046class ScopedNonWritable {
47 public:
48 explicit ScopedNonWritable(const std::string& dex_location) {
49 is_valid_ = false;
50 size_t pos = dex_location.rfind('/');
51 if (pos != std::string::npos) {
52 is_valid_ = true;
53 dex_parent_ = dex_location.substr(0, pos);
54 if (chmod(dex_parent_.c_str(), 0555) != 0) {
55 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
56 }
57 }
58 }
59
60 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
61
62 ~ScopedNonWritable() {
63 if (is_valid_) {
64 if (chmod(dex_parent_.c_str(), 0777) != 0) {
65 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
66 }
67 }
68 }
69
70 private:
71 std::string dex_parent_;
72 bool is_valid_;
73};
74
75static bool IsExecutedAsRoot() {
76 return geteuid() == 0;
77}
Calin Juravle36eb3132017-01-13 16:32:38 -080078
Richard Uhler66d874d2015-01-15 09:37:19 -080079// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -070080// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -080081TEST_F(OatFileAssistantTest, DexNoOat) {
82 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
83 Copy(GetDexSrc1(), dex_location);
84
Richard Uhlerd1472a22016-04-15 15:18:56 -070085 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -080086
Richard Uhler7225a8d2016-11-22 10:12:03 +000087 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +010088 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +000089 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +010090 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +000091 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000092 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +000093 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000094 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -080095
96 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +000097 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
98 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -070099 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800100}
101
102// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700103// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800104TEST_F(OatFileAssistantTest, NoDexNoOat) {
105 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
106
Richard Uhlerd1472a22016-04-15 15:18:56 -0700107 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800108
Andreas Gampe29d38e72016-03-23 15:31:51 +0000109 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
110 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700111 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
112
113 // Trying to make the oat file up to date should not fail or crash.
114 std::string error_msg;
Richard Uhlerd1472a22016-04-15 15:18:56 -0700115 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700116
117 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800118 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
119 EXPECT_EQ(nullptr, oat_file.get());
120}
121
Calin Juravle357c66d2017-05-04 01:57:17 +0000122// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
123// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
124TEST_F(OatFileAssistantTest, OdexUpToDate) {
125 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
126 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
127 Copy(GetDexSrc1(), dex_location);
128 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
129
130 // For the use of oat location by making the dex parent not writable.
131 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
132
133 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
134 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
135 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
136 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
137 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
138 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
139 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
140 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
141
142 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
143 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
144 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
145 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
146}
147
148// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
149// file via a symlink.
150// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
151TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
152 std::string scratch_dir = GetScratchDir();
153 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
154 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
155
156 Copy(GetDexSrc1(), dex_location);
157 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
158
159 // Now replace the dex location with a symlink.
160 std::string link = scratch_dir + "/link";
161 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
162 dex_location = link + "/OdexUpToDate.jar";
163
164 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
165
166 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
167 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
168 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
169 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
170 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
171 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
172 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
173 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
174
175 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
176 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
177 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
178 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
179}
180
Richard Uhler66d874d2015-01-15 09:37:19 -0800181// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700182// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800183TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000184 if (IsExecutedAsRoot()) {
185 // We cannot simulate non writable locations when executed as root: b/38000545.
186 LOG(ERROR) << "Test skipped because it's running as root";
187 return;
188 }
189
Richard Uhler66d874d2015-01-15 09:37:19 -0800190 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
191 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000192 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800193
Calin Juravle357c66d2017-05-04 01:57:17 +0000194 // For the use of oat location by making the dex parent not writable.
195 ScopedNonWritable scoped_non_writable(dex_location);
196 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
197
198 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
199
200 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
201 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
202 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
203 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
204 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
205 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
206 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
207 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
208
209 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
210 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
211 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
212 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
213}
214
215// Case: We have a DEX file and up-to-date OAT file for it. We load the dex file
216// via a symlink.
217// Expect: The status is kNoDexOptNeeded.
218TEST_F(OatFileAssistantTest, OatUpToDateSymLink) {
219 if (IsExecutedAsRoot()) {
220 // We cannot simulate non writable locations when executed as root: b/38000545.
221 LOG(ERROR) << "Test skipped because it's running as root";
222 return;
223 }
224
225 std::string real = GetScratchDir() + "/real";
226 ASSERT_EQ(0, mkdir(real.c_str(), 0700));
227 std::string link = GetScratchDir() + "/link";
228 ASSERT_EQ(0, symlink(real.c_str(), link.c_str()));
229
230 std::string dex_location = real + "/OatUpToDate.jar";
231
232 Copy(GetDexSrc1(), dex_location);
233 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
234
235 // Update the dex location to point to the symlink.
236 dex_location = link + "/OatUpToDate.jar";
237
238 // For the use of oat location by making the dex parent not writable.
239 ScopedNonWritable scoped_non_writable(dex_location);
240 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
241
Richard Uhlerd1472a22016-04-15 15:18:56 -0700242 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800243
Andreas Gampe29d38e72016-03-23 15:31:51 +0000244 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
245 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
246 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100247 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000248 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100249 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000250 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000251 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
252
Richard Uhler66d874d2015-01-15 09:37:19 -0800253 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000254 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler95abd042015-03-24 09:51:28 -0700255 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700256 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800257}
258
Richard Uhler2f27abd2017-01-31 14:02:34 +0000259// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
260// ODEX file.
261TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
262 // This test case is only meaningful if vdex is enabled.
263 if (!kIsVdexEnabled) {
264 return;
265 }
Richard Uhler9a37efc2016-08-05 16:32:55 -0700266
Richard Uhler2f27abd2017-01-31 14:02:34 +0000267 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000268 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000269
Richard Uhler9a37efc2016-08-05 16:32:55 -0700270 Copy(GetDexSrc1(), dex_location);
271
Richard Uhler2f27abd2017-01-31 14:02:34 +0000272 // Generating and deleting the oat file should have the side effect of
273 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000274 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
275 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000276
Calin Juravle357c66d2017-05-04 01:57:17 +0000277 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000278
279 // Even though the vdex file is up to date, because we don't have the oat
280 // file, we can't know that the vdex depends on the boot image and is up to
281 // date with respect to the boot image. Instead we must assume the vdex file
282 // depends on the boot image and is out of date with respect to the boot
283 // image.
284 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
285 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
286
287 // Make sure we don't crash in this case when we dump the status. We don't
288 // care what the actual dumped value is.
289 oat_file_assistant.GetStatusDump();
290}
291
292// Case: We have a DEX file and empty VDEX and ODEX files.
293TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
294 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
295 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
296 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
297
298 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000299 ScratchFile vdex_file(vdex_location.c_str());
300 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000301
302 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
303 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
304 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
305}
306
307// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
308// file.
309TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
310 // This test case is only meaningful if vdex is enabled.
311 if (!kIsVdexEnabled) {
312 return;
313 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000314 if (IsExecutedAsRoot()) {
315 // We cannot simulate non writable locations when executed as root: b/38000545.
316 LOG(ERROR) << "Test skipped because it's running as root";
317 return;
318 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000319
320 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
321 std::string oat_location;
322 std::string error_msg;
323 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
324 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
325
326 Copy(GetDexSrc1(), dex_location);
327 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
328 ASSERT_EQ(0, unlink(oat_location.c_str()));
329
Calin Juravle357c66d2017-05-04 01:57:17 +0000330 ScopedNonWritable scoped_non_writable(dex_location);
331 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700332 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
333
Richard Uhler2f27abd2017-01-31 14:02:34 +0000334 // Even though the vdex file is up to date, because we don't have the oat
335 // file, we can't know that the vdex depends on the boot image and is up to
336 // date with respect to the boot image. Instead we must assume the vdex file
337 // depends on the boot image and is out of date with respect to the boot
338 // image.
339 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700340 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700341}
342
Andreas Gampe29d38e72016-03-23 15:31:51 +0000343// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700344// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
345// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000346TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000347 if (IsExecutedAsRoot()) {
348 // We cannot simulate non writable locations when executed as root: b/38000545.
349 LOG(ERROR) << "Test skipped because it's running as root";
350 return;
351 }
352
Andreas Gampe29d38e72016-03-23 15:31:51 +0000353 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
354 Copy(GetDexSrc1(), dex_location);
355 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
356
Calin Juravle357c66d2017-05-04 01:57:17 +0000357 ScopedNonWritable scoped_non_writable(dex_location);
358 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
359
Richard Uhlerd1472a22016-04-15 15:18:56 -0700360 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000361
362 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700363 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000364 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100365 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000366 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700367 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000368 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100369 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000370
371 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000372 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000373 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
374 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
375}
376
Richard Uhler66d874d2015-01-15 09:37:19 -0800377// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700378// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800379TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000380 if (IsExecutedAsRoot()) {
381 // We cannot simulate non writable locations when executed as root: b/38000545.
382 LOG(ERROR) << "Test skipped because it's running as root";
383 return;
384 }
385
Richard Uhler66d874d2015-01-15 09:37:19 -0800386 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
387 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000388 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800389
Calin Juravle357c66d2017-05-04 01:57:17 +0000390 ScopedNonWritable scoped_non_writable(dex_location);
391 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
392
Richard Uhlerd1472a22016-04-15 15:18:56 -0700393 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000394 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700395 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700396 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700397
398 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700399 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800400 ASSERT_TRUE(oat_file.get() != nullptr);
401 EXPECT_TRUE(oat_file->IsExecutable());
402 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700403 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
404 EXPECT_EQ(2u, dex_files.size());
405}
406
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000407// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700408// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000409TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000410 if (IsExecutedAsRoot()) {
411 // We cannot simulate non writable locations when executed as root: b/38000545.
412 LOG(ERROR) << "Test skipped because it's running as root";
413 return;
414 }
415
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000416 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700417
418 // Compile code for GetMultiDexSrc1.
419 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000420 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700421
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000422 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700423 // is out of date.
424 Copy(GetMultiDexSrc2(), dex_location);
425
Calin Juravle357c66d2017-05-04 01:57:17 +0000426 ScopedNonWritable scoped_non_writable(dex_location);
427 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
428
Richard Uhlerd1472a22016-04-15 15:18:56 -0700429 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000430 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700431 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700432 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700433}
434
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000435// Case: We have a stripped MultiDEX file where the non-main multidex entry is
436// out of date with respect to the odex file.
437TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
438 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
439 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
440
441 // Compile the oat from GetMultiDexSrc1.
442 Copy(GetMultiDexSrc1(), dex_location);
443 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
444
445 // Compile the odex from GetMultiDexSrc2, which has a different non-main
446 // dex checksum.
447 Copy(GetMultiDexSrc2(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100448 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000449
450 // Strip the dex file.
451 Copy(GetStrippedDexSrc1(), dex_location);
452
453 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable*/false);
454
455 // Because the dex file is stripped, the odex file is considered the source
456 // of truth for the dex checksums. The oat file should be considered
457 // unusable.
458 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
459 ASSERT_TRUE(best_file.get() != nullptr);
460 EXPECT_EQ(best_file->GetLocation(), odex_location);
461 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
462 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
463 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
464}
465
Calin Juravle357c66d2017-05-04 01:57:17 +0000466// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
Richard Uhlere5fed032015-03-18 08:21:11 -0700467// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700468// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700469TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
470 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000471 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
Richard Uhlere5fed032015-03-18 08:21:11 -0700472
473 // Create the dex file
474 Copy(GetMultiDexSrc1(), dex_location);
475
476 // Create the oat file with relative encoded dex location.
477 std::vector<std::string> args;
478 args.push_back("--dex-file=" + dex_location);
479 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
Calin Juravle357c66d2017-05-04 01:57:17 +0000480 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000481 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700482
483 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700484 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700485
486 // Verify we can load both dex files.
Calin Juravle357c66d2017-05-04 01:57:17 +0000487 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
488
Richard Uhlere5fed032015-03-18 08:21:11 -0700489 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
490 ASSERT_TRUE(oat_file.get() != nullptr);
491 EXPECT_TRUE(oat_file->IsExecutable());
492 std::vector<std::unique_ptr<const DexFile>> dex_files;
493 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800494 EXPECT_EQ(2u, dex_files.size());
495}
496
Richard Uhler03bc6592016-11-22 09:42:04 +0000497// Case: We have a DEX file and an OAT file out of date with respect to the
498// dex checksum.
499TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000500 if (IsExecutedAsRoot()) {
501 // We cannot simulate non writable locations when executed as root: b/38000545.
502 LOG(ERROR) << "Test skipped because it's running as root";
503 return;
504 }
505
Richard Uhler03bc6592016-11-22 09:42:04 +0000506 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800507
508 // We create a dex, generate an oat for it, then overwrite the dex with a
509 // different dex to make the oat out of date.
510 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000511 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800512 Copy(GetDexSrc2(), dex_location);
513
Calin Juravle357c66d2017-05-04 01:57:17 +0000514 ScopedNonWritable scoped_non_writable(dex_location);
515 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
516
Richard Uhlerd1472a22016-04-15 15:18:56 -0700517 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000518 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100519 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000520 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000521 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800522
523 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000524 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
525 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
526 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
527}
528
Richard Uhler2f27abd2017-01-31 14:02:34 +0000529// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
530// to the dex checksum, but no ODEX file.
531TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
532 // This test case is only meaningful if vdex is enabled.
533 if (!kIsVdexEnabled) {
534 return;
535 }
536
537 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000538 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000539
540 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000541 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
542 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000543 Copy(GetDexSrc2(), dex_location);
544
Calin Juravle357c66d2017-05-04 01:57:17 +0000545 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000546
547 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
548 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
549}
550
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000551// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
552// is out of date and there is no corresponding ODEX file.
553TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000554 // This test case is only meaningful if vdex is enabled.
555 if (!kIsVdexEnabled) {
556 return;
557 }
558
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000559 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000560 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000561
562 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000563 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
564 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000565 Copy(GetMultiDexSrc2(), dex_location);
566
Calin Juravle357c66d2017-05-04 01:57:17 +0000567 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000568
569 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
570 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
571}
572
Richard Uhler03bc6592016-11-22 09:42:04 +0000573// Case: We have a DEX file and an OAT file out of date with respect to the
574// boot image.
575TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000576 if (IsExecutedAsRoot()) {
577 // We cannot simulate non writable locations when executed as root: b/38000545.
578 LOG(ERROR) << "Test skipped because it's running as root";
579 return;
580 }
581
Richard Uhler03bc6592016-11-22 09:42:04 +0000582 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
583
584 Copy(GetDexSrc1(), dex_location);
585 GenerateOatForTest(dex_location.c_str(),
586 CompilerFilter::kSpeed,
587 /*relocate*/true,
588 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000589 /*with_alternate_image*/true);
590
Calin Juravle357c66d2017-05-04 01:57:17 +0000591 ScopedNonWritable scoped_non_writable(dex_location);
592 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
593
Richard Uhler03bc6592016-11-22 09:42:04 +0000594 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000595 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100596 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000597 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100598 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000599 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000600 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
601
602 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
603 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
604 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
605 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
606}
607
608// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
609// respect to the boot image.
610// It shouldn't matter that the OAT file is out of date, because it is
611// verify-at-runtime.
612TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000613 if (IsExecutedAsRoot()) {
614 // We cannot simulate non writable locations when executed as root: b/38000545.
615 LOG(ERROR) << "Test skipped because it's running as root";
616 return;
617 }
618
Richard Uhler03bc6592016-11-22 09:42:04 +0000619 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
620
621 Copy(GetDexSrc1(), dex_location);
622 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100623 CompilerFilter::kExtract,
Richard Uhler03bc6592016-11-22 09:42:04 +0000624 /*relocate*/true,
625 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000626 /*with_alternate_image*/true);
627
Calin Juravle357c66d2017-05-04 01:57:17 +0000628 ScopedNonWritable scoped_non_writable(dex_location);
629 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
630
Richard Uhler03bc6592016-11-22 09:42:04 +0000631 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
632 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100633 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000634 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100635 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000636
637 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
638 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
639 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
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 Uhler66d874d2015-01-15 09:37:19 -0800644TEST_F(OatFileAssistantTest, DexOdexNoOat) {
645 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700646 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800647
648 // Create the dex and odex files
649 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000650 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800651
652 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700653 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800654
Andreas Gampe29d38e72016-03-23 15:31:51 +0000655 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100656 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler5923b522016-12-08 09:48:01 +0000657 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000658 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800659
660 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000661 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
662 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700663 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700664
665 // We should still be able to get the non-executable odex file to run from.
666 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
667 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800668}
669
Richard Uhler5923b522016-12-08 09:48:01 +0000670// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800671TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
672 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700673 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800674
675 // Create the dex and odex files
676 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000677 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800678
679 // Strip the dex file
680 Copy(GetStrippedDexSrc1(), dex_location);
681
682 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700683 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800684
Richard Uhler5923b522016-12-08 09:48:01 +0000685 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000686 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800687
688 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000689 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000690 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700691 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800692
Richard Uhler66d874d2015-01-15 09:37:19 -0800693 // Verify we can load the dex files from it.
694 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
695 ASSERT_TRUE(oat_file.get() != nullptr);
696 EXPECT_TRUE(oat_file->IsExecutable());
697 std::vector<std::unique_ptr<const DexFile>> dex_files;
698 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
699 EXPECT_EQ(1u, dex_files.size());
700}
701
Richard Uhler5923b522016-12-08 09:48:01 +0000702// Case: We have a stripped DEX file, a PIC ODEX file, and an out-of-date OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800703TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
704 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700705 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800706
707 // Create the oat file from a different dex file so it looks out of date.
708 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000709 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800710
711 // Create the odex file
712 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000713 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800714
715 // Strip the dex file.
716 Copy(GetStrippedDexSrc1(), dex_location);
717
718 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700719 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800720
Andreas Gampe29d38e72016-03-23 15:31:51 +0000721 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100722 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000723 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
724 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100725 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file
Andreas Gampe29d38e72016-03-23 15:31:51 +0000726 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800727
728 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000729 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
730 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700731 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800732
733 // Verify we can load the dex files from it.
734 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
735 ASSERT_TRUE(oat_file.get() != nullptr);
736 EXPECT_TRUE(oat_file->IsExecutable());
737 std::vector<std::unique_ptr<const DexFile>> dex_files;
738 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
739 EXPECT_EQ(1u, dex_files.size());
740}
741
Richard Uhler9b994ea2015-06-24 08:44:19 -0700742// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
743// OAT file. Expect: The status is kNoDexOptNeeded.
744TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
745 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
746
747 Copy(GetStrippedDexSrc1(), dex_location);
748
749 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700750 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700751
Andreas Gampe29d38e72016-03-23 15:31:51 +0000752 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
753 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
754 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100755 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000756 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100757 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700758
759 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000760 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
761 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700762 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
763
764 // Make the oat file up to date. This should have no effect.
765 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700766 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700767 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700768 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700769
Andreas Gampe29d38e72016-03-23 15:31:51 +0000770 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
771 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700772
773 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000774 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
775 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700776 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
777}
778
Richard Uhler66d874d2015-01-15 09:37:19 -0800779// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
780// OAT files both have patch delta of 0.
Richard Uhler5923b522016-12-08 09:48:01 +0000781// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800782TEST_F(OatFileAssistantTest, OdexOatOverlap) {
783 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700784 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800785
Calin Juravle357c66d2017-05-04 01:57:17 +0000786 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800787 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000788 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000789 GenerateOatForTest(dex_location.c_str(),
790 CompilerFilter::kSpeed,
791 /*relocate*/false,
792 /*pic*/false,
793 /*with_alternate_image*/false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800794
795 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000796 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800797
Calin Juravle357c66d2017-05-04 01:57:17 +0000798 // -kDex2OatForRelocation is expected rather than kDex2OatForRelocation
799 // based on the assumption that the odex location is more up-to-date than the oat
Richard Uhler70a84262016-11-08 16:51:51 +0000800 // location, even if they both need relocation.
Calin Juravle357c66d2017-05-04 01:57:17 +0000801 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000802 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800803
804 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000805 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
806 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700807 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800808
809 // Things aren't relocated, so it should fall back to interpreted.
810 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
811 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700812
Richard Uhler66d874d2015-01-15 09:37:19 -0800813 EXPECT_FALSE(oat_file->IsExecutable());
814 std::vector<std::unique_ptr<const DexFile>> dex_files;
815 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
816 EXPECT_EQ(1u, dex_files.size());
817}
818
Andreas Gampe29d38e72016-03-23 15:31:51 +0000819// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
820// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
821TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
822 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
823 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000824
825 // Create the dex and odex files
826 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100827 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000828
829 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700830 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000831
Andreas Gampe29d38e72016-03-23 15:31:51 +0000832 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100833 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000834 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000835 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000836
837 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000838 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000839 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000840 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
841}
842
Richard Uhler66d874d2015-01-15 09:37:19 -0800843// Case: We have a DEX file and up-to-date OAT file for it.
844// Expect: We should load an executable dex file.
845TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000846 if (IsExecutedAsRoot()) {
847 // We cannot simulate non writable locations when executed as root: b/38000545.
848 LOG(ERROR) << "Test skipped because it's running as root";
849 return;
850 }
851
Richard Uhler66d874d2015-01-15 09:37:19 -0800852 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
853
854 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000855 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800856
Calin Juravle357c66d2017-05-04 01:57:17 +0000857 ScopedNonWritable scoped_non_writable(dex_location);
858 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
859
Richard Uhler66d874d2015-01-15 09:37:19 -0800860 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700861 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000862
863 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
864 ASSERT_TRUE(oat_file.get() != nullptr);
865 EXPECT_TRUE(oat_file->IsExecutable());
866 std::vector<std::unique_ptr<const DexFile>> dex_files;
867 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
868 EXPECT_EQ(1u, dex_files.size());
869}
870
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100871// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000872// Expect: We should still load the oat file as executable.
873TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000874 if (IsExecutedAsRoot()) {
875 // We cannot simulate non writable locations when executed as root: b/38000545.
876 LOG(ERROR) << "Test skipped because it's running as root";
877 return;
878 }
879
Andreas Gampe29d38e72016-03-23 15:31:51 +0000880 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
881
882 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100883 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000884
Calin Juravle357c66d2017-05-04 01:57:17 +0000885 ScopedNonWritable scoped_non_writable(dex_location);
886 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
887
Andreas Gampe29d38e72016-03-23 15:31:51 +0000888 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700889 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800890
891 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
892 ASSERT_TRUE(oat_file.get() != nullptr);
893 EXPECT_TRUE(oat_file->IsExecutable());
894 std::vector<std::unique_ptr<const DexFile>> dex_files;
895 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
896 EXPECT_EQ(1u, dex_files.size());
897}
898
899// Case: We have a DEX file and up-to-date OAT file for it.
900// Expect: Loading non-executable should load the oat non-executable.
901TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000902 if (IsExecutedAsRoot()) {
903 // We cannot simulate non writable locations when executed as root: b/38000545.
904 LOG(ERROR) << "Test skipped because it's running as root";
905 return;
906 }
907
Richard Uhler66d874d2015-01-15 09:37:19 -0800908 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
909
910 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000911
912 ScopedNonWritable scoped_non_writable(dex_location);
913 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
914
Andreas Gampe29d38e72016-03-23 15:31:51 +0000915 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800916
917 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700918 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800919
920 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
921 ASSERT_TRUE(oat_file.get() != nullptr);
922 EXPECT_FALSE(oat_file->IsExecutable());
923 std::vector<std::unique_ptr<const DexFile>> dex_files;
924 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
925 EXPECT_EQ(1u, dex_files.size());
926}
927
Richard Uhler8327cf72015-10-13 16:34:59 -0700928// Case: We don't have a DEX file and can't write the oat file.
929// Expect: We should fail to generate the oat file without crashing.
930TEST_F(OatFileAssistantTest, GenNoDex) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000931 if (IsExecutedAsRoot()) {
932 // We cannot simulate non writable locations when executed as root: b/38000545.
933 LOG(ERROR) << "Test skipped because it's running as root";
934 return;
935 }
Richard Uhler8327cf72015-10-13 16:34:59 -0700936
Calin Juravle357c66d2017-05-04 01:57:17 +0000937 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
938
939 ScopedNonWritable scoped_non_writable(dex_location);
940 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
941
942 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700943 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700944 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Calin Juravle357c66d2017-05-04 01:57:17 +0000945 // We should get kUpdateSucceeded from MakeUpToDate since there's nothing
946 // that can be done in this situation.
947 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
948 oat_file_assistant.MakeUpToDate(false, &error_msg));
949
950 // Verify it didn't create an oat in the default location (dalvik-cache).
951 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
952 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
953 // Verify it didn't create the odex file in the default location (../oat/isa/...odex)
954 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OdexFileStatus());
Richard Uhler8327cf72015-10-13 16:34:59 -0700955}
956
Richard Uhler66d874d2015-01-15 09:37:19 -0800957// Turn an absolute path into a path relative to the current working
958// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -0800959static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800960 char buf[MAXPATHLEN];
961 std::string cwd = getcwd(buf, MAXPATHLEN);
962
963 // Split the target and cwd paths into components.
964 std::vector<std::string> target_path;
965 std::vector<std::string> cwd_path;
966 Split(target, '/', &target_path);
967 Split(cwd, '/', &cwd_path);
968
969 // Reverse the path components, so we can use pop_back().
970 std::reverse(target_path.begin(), target_path.end());
971 std::reverse(cwd_path.begin(), cwd_path.end());
972
973 // Drop the common prefix of the paths. Because we reversed the path
974 // components, this becomes the common suffix of target_path and cwd_path.
975 while (!target_path.empty() && !cwd_path.empty()
976 && target_path.back() == cwd_path.back()) {
977 target_path.pop_back();
978 cwd_path.pop_back();
979 }
980
981 // For each element of the remaining cwd_path, add '..' to the beginning
982 // of the target path. Because we reversed the path components, we add to
983 // the end of target_path.
984 for (unsigned int i = 0; i < cwd_path.size(); i++) {
985 target_path.push_back("..");
986 }
987
988 // Reverse again to get the right path order, and join to get the result.
989 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -0800990 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -0800991}
992
993// Case: Non-absolute path to Dex location.
994// Expect: Not sure, but it shouldn't crash.
995TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
996 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
997 Copy(GetDexSrc1(), abs_dex_location);
998
999 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001000 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001001
1002 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001003 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001004 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001005 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1006 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001007}
1008
1009// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001010// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001011TEST_F(OatFileAssistantTest, ShortDexLocation) {
1012 std::string dex_location = "/xx";
1013
Richard Uhlerd1472a22016-04-15 15:18:56 -07001014 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001015
1016 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001017 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1018 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001019 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1020 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001021 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001022
Richard Uhler9b994ea2015-06-24 08:44:19 -07001023 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001024 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001025 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001026 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001027 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001028 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001029}
1030
1031// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001032// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001033TEST_F(OatFileAssistantTest, LongDexExtension) {
1034 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1035 Copy(GetDexSrc1(), dex_location);
1036
Richard Uhlerd1472a22016-04-15 15:18:56 -07001037 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001038
Richard Uhler7225a8d2016-11-22 10:12:03 +00001039 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001040 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001041
1042 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001043 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1044 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001045}
1046
1047// A task to generate a dex location. Used by the RaceToGenerate test.
1048class RaceGenerateTask : public Task {
1049 public:
1050 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001051 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001052 {}
1053
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001054 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001055 // Load the dex files, and save a pointer to the loaded oat file, so that
1056 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001057 std::vector<std::unique_ptr<const DexFile>> dex_files;
1058 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001059 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001060 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1061 dex_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001062 /*class_loader*/nullptr,
1063 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001064 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001065 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001066 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001067 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1068 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001069 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001070 }
1071
1072 const OatFile* GetLoadedOatFile() const {
1073 return loaded_oat_file_;
1074 }
1075
1076 private:
1077 std::string dex_location_;
1078 std::string oat_location_;
1079 const OatFile* loaded_oat_file_;
1080};
1081
1082// Test the case where multiple processes race to generate an oat file.
1083// This simulates multiple processes using multiple threads.
1084//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001085// We want unique Oat files to be loaded even when there is a race to load.
1086// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1087// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001088TEST_F(OatFileAssistantTest, RaceToGenerate) {
1089 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001090 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001091
1092 // We use the lib core dex file, because it's large, and hopefully should
1093 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001094 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001095
1096 const int kNumThreads = 32;
1097 Thread* self = Thread::Current();
1098 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1099 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1100 for (int i = 0; i < kNumThreads; i++) {
1101 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1102 thread_pool.AddTask(self, task.get());
1103 tasks.push_back(std::move(task));
1104 }
1105 thread_pool.StartWorkers(self);
1106 thread_pool.Wait(self, true, false);
1107
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001108 // Verify every task got a unique oat file.
1109 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001110 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001111 const OatFile* oat_file = task->GetLoadedOatFile();
1112 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1113 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001114 }
1115}
1116
1117// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1118// disabled.
1119// Expect: We should load the odex file non-executable.
1120TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1121 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001122 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001123
1124 // Create the dex and odex files
1125 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001126 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001127
1128 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001129 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001130
1131 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1132 ASSERT_TRUE(oat_file.get() != nullptr);
1133 EXPECT_FALSE(oat_file->IsExecutable());
1134 std::vector<std::unique_ptr<const DexFile>> dex_files;
1135 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1136 EXPECT_EQ(1u, dex_files.size());
1137}
1138
1139// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1140// disabled.
1141// Expect: We should load the odex file non-executable.
1142TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1143 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001144 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001145
1146 // Create the dex and odex files
1147 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001148 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001149
1150 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001151 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001152
1153 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1154 ASSERT_TRUE(oat_file.get() != nullptr);
1155 EXPECT_FALSE(oat_file->IsExecutable());
1156 std::vector<std::unique_ptr<const DexFile>> dex_files;
1157 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1158 EXPECT_EQ(2u, dex_files.size());
1159}
1160
Richard Uhlerf4b34872016-04-13 11:03:46 -07001161TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1162 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1163 Copy(GetDexSrc1(), dex_location);
1164
Richard Uhlerd1472a22016-04-15 15:18:56 -07001165 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001166
1167 std::string error_msg;
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001168 Runtime::Current()->AddCompilerOption("--compiler-filter=quicken");
Richard Uhlerf4b34872016-04-13 11:03:46 -07001169 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001170 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +00001171 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001172 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +00001173 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001174 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1175
1176 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1177 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001178 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001179 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001180 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001181 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1182 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1183
1184 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1185 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001186 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001187}
1188
Richard Uhlerb81881d2016-04-19 13:08:04 -07001189TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001190 std::string error_msg;
1191 std::string odex_file;
1192
Richard Uhlerb81881d2016-04-19 13:08:04 -07001193 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001194 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001195 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001196
Richard Uhlerb81881d2016-04-19 13:08:04 -07001197 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001198 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001199 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001200
Richard Uhlerb81881d2016-04-19 13:08:04 -07001201 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001202 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001203 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001204 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001205}
1206
Richard Uhler23cedd22015-04-08 13:17:29 -07001207// Verify the dexopt status values from dalvik.system.DexFile
1208// match the OatFileAssistant::DexOptStatus values.
1209TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001210 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1211 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1212 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1213 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1214 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1215 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001216 };
1217
Richard Uhler23cedd22015-04-08 13:17:29 -07001218 ScopedObjectAccess soa(Thread::Current());
1219 StackHandleScope<1> hs(soa.Self());
1220 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1221 Handle<mirror::Class> dexfile(
1222 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001223 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001224 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1225
Richard Uhler7225a8d2016-11-22 10:12:03 +00001226 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1227 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001228 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001229 ASSERT_FALSE(art_field == nullptr);
1230 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1231 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1232 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001233}
Richard Uhler66d874d2015-01-15 09:37:19 -08001234
1235// TODO: More Tests:
1236// * Test class linker falls back to unquickened dex for DexNoOat
1237// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001238// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001239// * Test for status of oat while oat is being generated (how?)
1240// * Test case where 32 and 64 bit boot class paths differ,
1241// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1242// 64 bit boot class paths.
1243// * Test unexpected scenarios (?):
1244// - Dex is stripped, don't have odex.
1245// - Oat file corrupted after status check, before reload unexecutable
1246// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001247} // namespace art