blob: a98da0f0294057d7b0c412798b0c2b94317c0e4c [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "oat_file_assistant.h"
18
Richard Uhler66d874d2015-01-15 09:37:19 -080019#include <sys/param.h>
20
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <string>
22#include <vector>
Shubham Ajmerab22dea02017-10-04 18:36:41 -070023#include <fcntl.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024
Richard Uhler66d874d2015-01-15 09:37:19 -080025#include <gtest/gtest.h>
26
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "android-base/strings.h"
28
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010030#include "class_linker-inl.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070031#include "class_loader_context.h"
Jeff Hao0cb17282017-07-12 14:51:49 -070032#include "common_runtime_test.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080033#include "dexopt_test.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070034#include "oat_file.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070035#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070037#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070038#include "thread-current-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080039#include "utils.h"
40
41namespace art {
42
Igor Murashkin2ffb7032017-11-08 13:35:21 -080043static const std::string kSpecialSharedLibrary = "&"; // NOLINT [runtime/string] [4]
Calin Juravle44e5efa2017-09-12 00:54:26 -070044static ClassLoaderContext* kSpecialSharedLibraryContext = nullptr;
Calin Juravle27e0d1f2017-07-26 00:16:07 -070045
Calin Juravle36eb3132017-01-13 16:32:38 -080046class OatFileAssistantTest : public DexoptTest {};
Richard Uhler66d874d2015-01-15 09:37:19 -080047
Calin Juravle36eb3132017-01-13 16:32:38 -080048class OatFileAssistantNoDex2OatTest : public DexoptTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080049 public:
50 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Calin Juravle36eb3132017-01-13 16:32:38 -080051 DexoptTest::SetUpRuntimeOptions(options);
Richard Uhler66d874d2015-01-15 09:37:19 -080052 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
53 }
54};
55
Calin Juravle357c66d2017-05-04 01:57:17 +000056class ScopedNonWritable {
57 public:
58 explicit ScopedNonWritable(const std::string& dex_location) {
59 is_valid_ = false;
60 size_t pos = dex_location.rfind('/');
61 if (pos != std::string::npos) {
62 is_valid_ = true;
63 dex_parent_ = dex_location.substr(0, pos);
64 if (chmod(dex_parent_.c_str(), 0555) != 0) {
65 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
66 }
67 }
68 }
69
70 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
71
72 ~ScopedNonWritable() {
73 if (is_valid_) {
74 if (chmod(dex_parent_.c_str(), 0777) != 0) {
75 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
76 }
77 }
78 }
79
80 private:
81 std::string dex_parent_;
82 bool is_valid_;
83};
84
85static bool IsExecutedAsRoot() {
86 return geteuid() == 0;
87}
Calin Juravle36eb3132017-01-13 16:32:38 -080088
Richard Uhler66d874d2015-01-15 09:37:19 -080089// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -070090// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -080091TEST_F(OatFileAssistantTest, DexNoOat) {
92 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
93 Copy(GetDexSrc1(), dex_location);
94
Richard Uhlerd1472a22016-04-15 15:18:56 -070095 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -080096
Richard Uhler7225a8d2016-11-22 10:12:03 +000097 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +010098 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +000099 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100100 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000101 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000102 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000103 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000104 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800105
106 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000107 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
108 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700109 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800110}
111
112// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700113// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800114TEST_F(OatFileAssistantTest, NoDexNoOat) {
115 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
116
Richard Uhlerd1472a22016-04-15 15:18:56 -0700117 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800118
Andreas Gampe29d38e72016-03-23 15:31:51 +0000119 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
120 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700121 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
122
123 // Trying to make the oat file up to date should not fail or crash.
124 std::string error_msg;
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700125 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700126 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700127
128 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800129 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
130 EXPECT_EQ(nullptr, oat_file.get());
131}
132
Calin Juravle357c66d2017-05-04 01:57:17 +0000133// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
134// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
135TEST_F(OatFileAssistantTest, OdexUpToDate) {
136 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
137 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
138 Copy(GetDexSrc1(), dex_location);
139 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
140
141 // For the use of oat location by making the dex parent not writable.
142 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
143
144 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
145 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
146 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
147 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
148 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
149 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
150 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
151 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
152
153 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
154 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
155 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
156 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
157}
158
159// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
160// file via a symlink.
161// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
162TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
163 std::string scratch_dir = GetScratchDir();
164 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
165 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
166
167 Copy(GetDexSrc1(), dex_location);
168 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
169
170 // Now replace the dex location with a symlink.
171 std::string link = scratch_dir + "/link";
172 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
173 dex_location = link + "/OdexUpToDate.jar";
174
175 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
176
177 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
178 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
179 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
180 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
181 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
182 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
183 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
184 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
185
186 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
187 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
188 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
189 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
190}
191
Richard Uhler66d874d2015-01-15 09:37:19 -0800192// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700193// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800194TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000195 if (IsExecutedAsRoot()) {
196 // We cannot simulate non writable locations when executed as root: b/38000545.
197 LOG(ERROR) << "Test skipped because it's running as root";
198 return;
199 }
200
Richard Uhler66d874d2015-01-15 09:37:19 -0800201 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
202 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000203 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800204
Calin Juravle357c66d2017-05-04 01:57:17 +0000205 // For the use of oat location by making the dex parent not writable.
206 ScopedNonWritable scoped_non_writable(dex_location);
207 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
208
209 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
210
211 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
212 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
213 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
214 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
215 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
216 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
217 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
218 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
219
220 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
221 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
222 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
223 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
224}
225
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700226// Case: Passing valid file descriptors of updated odex/vdex filesalong with
227// the dex file.
228// Expect: The status is kNoDexOptNeeded.
229TEST_F(OatFileAssistantTest, GetDexOptNeededWithFd) {
230 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
231 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
232 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
233
234 Copy(GetDexSrc1(), dex_location);
235 GenerateOatForTest(dex_location.c_str(),
236 odex_location.c_str(),
237 CompilerFilter::kSpeed,
238 true,
239 false,
240 false);
241
242 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
243 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700244 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700245
246 OatFileAssistant oat_file_assistant(dex_location.c_str(),
247 kRuntimeISA,
248 false,
249 vdex_fd.get(),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700250 odex_fd.get(),
251 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700252 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
253 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
254 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
255 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
256 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
257 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
258 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
259 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
260
261 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
262 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
263 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
264 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
265}
266
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700267// Case: Passing invalid odex fd and valid vdex and zip fds.
268// Expect: The status should be kDex2OatForBootImage.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700269TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexFd) {
270 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
271 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
272 std::string vdex_location = GetScratchDir() + "/OatUpToDate.vdex";
273
274 Copy(GetDexSrc1(), dex_location);
275 GenerateOatForTest(dex_location.c_str(),
276 odex_location.c_str(),
277 CompilerFilter::kSpeed,
278 true,
279 false,
280 false);
281
282 android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700283 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700284
285 OatFileAssistant oat_file_assistant(dex_location.c_str(),
286 kRuntimeISA,
287 false,
288 vdex_fd.get(),
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700289 -1 /* oat_fd */,
290 zip_fd.get());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700291 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
292 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700293 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
294 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
295
296 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700297 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OdexFileStatus());
298 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700299 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700300}
301
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700302// Case: Passing invalid vdex fd and valid odex and zip fds.
303// Expect: The status should be kDex2OatFromScratch.
304TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidVdexFd) {
305 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
306 std::string odex_location = GetScratchDir() + "/OatUpToDate.odex";
307
308 Copy(GetDexSrc1(), dex_location);
309 GenerateOatForTest(dex_location.c_str(),
310 odex_location.c_str(),
311 CompilerFilter::kSpeed,
312 true,
313 false,
314 false);
315
316 android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
317 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
318
319 OatFileAssistant oat_file_assistant(dex_location.c_str(),
320 kRuntimeISA,
321 false,
322 -1 /* vdex_fd */,
323 odex_fd.get(),
324 zip_fd.get());
325
326 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
327 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
328 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
329 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
330 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
331 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
332}
333
334// Case: Passing invalid vdex and odex fd with valid zip fd.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700335// Expect: The status is kDex2oatFromScratch.
336TEST_F(OatFileAssistantTest, GetDexOptNeededWithInvalidOdexVdexFd) {
337 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
338
339 Copy(GetDexSrc1(), dex_location);
340
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700341 android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700342 OatFileAssistant oat_file_assistant(dex_location.c_str(),
343 kRuntimeISA,
344 false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700345 -1 /* vdex_fd */,
346 -1 /* oat_fd */,
347 zip_fd);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700348 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
349 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
350 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
351 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
352}
353
Richard Uhler2f27abd2017-01-31 14:02:34 +0000354// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
355// ODEX file.
356TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000357 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000358 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000359
Richard Uhler9a37efc2016-08-05 16:32:55 -0700360 Copy(GetDexSrc1(), dex_location);
361
Richard Uhler2f27abd2017-01-31 14:02:34 +0000362 // Generating and deleting the oat file should have the side effect of
363 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000364 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
365 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000366
Calin Juravle357c66d2017-05-04 01:57:17 +0000367 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000368
369 // Even though the vdex file is up to date, because we don't have the oat
370 // file, we can't know that the vdex depends on the boot image and is up to
371 // date with respect to the boot image. Instead we must assume the vdex file
372 // depends on the boot image and is out of date with respect to the boot
373 // image.
374 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
375 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
376
377 // Make sure we don't crash in this case when we dump the status. We don't
378 // care what the actual dumped value is.
379 oat_file_assistant.GetStatusDump();
380}
381
382// Case: We have a DEX file and empty VDEX and ODEX files.
383TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
384 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
385 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
386 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
387
388 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000389 ScratchFile vdex_file(vdex_location.c_str());
390 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000391
392 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
393 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
394 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
395}
396
397// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
398// file.
399TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000400 if (IsExecutedAsRoot()) {
401 // We cannot simulate non writable locations when executed as root: b/38000545.
402 LOG(ERROR) << "Test skipped because it's running as root";
403 return;
404 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000405
406 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
407 std::string oat_location;
408 std::string error_msg;
409 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
410 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
411
412 Copy(GetDexSrc1(), dex_location);
413 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
414 ASSERT_EQ(0, unlink(oat_location.c_str()));
415
Calin Juravle357c66d2017-05-04 01:57:17 +0000416 ScopedNonWritable scoped_non_writable(dex_location);
417 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700418 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
419
Richard Uhler2f27abd2017-01-31 14:02:34 +0000420 // Even though the vdex file is up to date, because we don't have the oat
421 // file, we can't know that the vdex depends on the boot image and is up to
422 // date with respect to the boot image. Instead we must assume the vdex file
423 // depends on the boot image and is out of date with respect to the boot
424 // image.
425 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700426 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700427}
428
Andreas Gampe29d38e72016-03-23 15:31:51 +0000429// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700430// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
431// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000432TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000433 if (IsExecutedAsRoot()) {
434 // We cannot simulate non writable locations when executed as root: b/38000545.
435 LOG(ERROR) << "Test skipped because it's running as root";
436 return;
437 }
438
Andreas Gampe29d38e72016-03-23 15:31:51 +0000439 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
440 Copy(GetDexSrc1(), dex_location);
441 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
442
Calin Juravle357c66d2017-05-04 01:57:17 +0000443 ScopedNonWritable scoped_non_writable(dex_location);
444 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
445
Richard Uhlerd1472a22016-04-15 15:18:56 -0700446 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000447
448 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700449 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000450 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100451 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000452 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700453 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000454 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100455 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000456
457 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000458 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000459 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
460 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
461}
462
Richard Uhler66d874d2015-01-15 09:37:19 -0800463// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700464// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800465TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000466 if (IsExecutedAsRoot()) {
467 // We cannot simulate non writable locations when executed as root: b/38000545.
468 LOG(ERROR) << "Test skipped because it's running as root";
469 return;
470 }
471
Richard Uhler66d874d2015-01-15 09:37:19 -0800472 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
473 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000474 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800475
Calin Juravle357c66d2017-05-04 01:57:17 +0000476 ScopedNonWritable scoped_non_writable(dex_location);
477 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
478
Richard Uhlerd1472a22016-04-15 15:18:56 -0700479 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000480 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700481 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700482 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700483
484 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700485 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 ASSERT_TRUE(oat_file.get() != nullptr);
487 EXPECT_TRUE(oat_file->IsExecutable());
488 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700489 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
490 EXPECT_EQ(2u, dex_files.size());
491}
492
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000493// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700494// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000495TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000496 if (IsExecutedAsRoot()) {
497 // We cannot simulate non writable locations when executed as root: b/38000545.
498 LOG(ERROR) << "Test skipped because it's running as root";
499 return;
500 }
501
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000502 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700503
504 // Compile code for GetMultiDexSrc1.
505 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000506 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700507
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000508 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700509 // is out of date.
510 Copy(GetMultiDexSrc2(), dex_location);
511
Calin Juravle357c66d2017-05-04 01:57:17 +0000512 ScopedNonWritable scoped_non_writable(dex_location);
513 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
514
Richard Uhlerd1472a22016-04-15 15:18:56 -0700515 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000516 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700517 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700518 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700519}
520
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000521// Case: We have a stripped MultiDEX file where the non-main multidex entry is
522// out of date with respect to the odex file.
523TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
524 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
525 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
526
527 // Compile the oat from GetMultiDexSrc1.
528 Copy(GetMultiDexSrc1(), dex_location);
529 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
530
531 // Compile the odex from GetMultiDexSrc2, which has a different non-main
532 // dex checksum.
533 Copy(GetMultiDexSrc2(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100534 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000535
536 // Strip the dex file.
537 Copy(GetStrippedDexSrc1(), dex_location);
538
539 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable*/false);
540
541 // Because the dex file is stripped, the odex file is considered the source
542 // of truth for the dex checksums. The oat file should be considered
543 // unusable.
544 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
545 ASSERT_TRUE(best_file.get() != nullptr);
546 EXPECT_EQ(best_file->GetLocation(), odex_location);
547 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
548 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
549 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
550}
551
Calin Juravle357c66d2017-05-04 01:57:17 +0000552// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
Richard Uhlere5fed032015-03-18 08:21:11 -0700553// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700554// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700555TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
556 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000557 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
Richard Uhlere5fed032015-03-18 08:21:11 -0700558
559 // Create the dex file
560 Copy(GetMultiDexSrc1(), dex_location);
561
562 // Create the oat file with relative encoded dex location.
563 std::vector<std::string> args;
564 args.push_back("--dex-file=" + dex_location);
565 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
Calin Juravle357c66d2017-05-04 01:57:17 +0000566 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000567 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700568
569 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700570 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700571
572 // Verify we can load both dex files.
Calin Juravle357c66d2017-05-04 01:57:17 +0000573 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
574
Richard Uhlere5fed032015-03-18 08:21:11 -0700575 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
576 ASSERT_TRUE(oat_file.get() != nullptr);
577 EXPECT_TRUE(oat_file->IsExecutable());
578 std::vector<std::unique_ptr<const DexFile>> dex_files;
579 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800580 EXPECT_EQ(2u, dex_files.size());
581}
582
Richard Uhler03bc6592016-11-22 09:42:04 +0000583// Case: We have a DEX file and an OAT file out of date with respect to the
584// dex checksum.
585TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000586 if (IsExecutedAsRoot()) {
587 // We cannot simulate non writable locations when executed as root: b/38000545.
588 LOG(ERROR) << "Test skipped because it's running as root";
589 return;
590 }
591
Richard Uhler03bc6592016-11-22 09:42:04 +0000592 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800593
594 // We create a dex, generate an oat for it, then overwrite the dex with a
595 // different dex to make the oat out of date.
596 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000597 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800598 Copy(GetDexSrc2(), dex_location);
599
Calin Juravle357c66d2017-05-04 01:57:17 +0000600 ScopedNonWritable scoped_non_writable(dex_location);
601 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
602
Richard Uhlerd1472a22016-04-15 15:18:56 -0700603 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000604 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100605 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000606 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000607 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
609 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000610 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
611 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
612 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
613}
614
Richard Uhler2f27abd2017-01-31 14:02:34 +0000615// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
616// to the dex checksum, but no ODEX file.
617TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000618 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000619 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000620
621 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000622 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
623 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000624 Copy(GetDexSrc2(), dex_location);
625
Calin Juravle357c66d2017-05-04 01:57:17 +0000626 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000627
628 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
629 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
630}
631
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000632// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
633// is out of date and there is no corresponding ODEX file.
634TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000635 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000636 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000637
638 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000639 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
640 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000641 Copy(GetMultiDexSrc2(), dex_location);
642
Calin Juravle357c66d2017-05-04 01:57:17 +0000643 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000644
645 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
646 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
647}
648
Richard Uhler03bc6592016-11-22 09:42:04 +0000649// Case: We have a DEX file and an OAT file out of date with respect to the
650// boot image.
651TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000652 if (IsExecutedAsRoot()) {
653 // We cannot simulate non writable locations when executed as root: b/38000545.
654 LOG(ERROR) << "Test skipped because it's running as root";
655 return;
656 }
657
Richard Uhler03bc6592016-11-22 09:42:04 +0000658 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
659
660 Copy(GetDexSrc1(), dex_location);
661 GenerateOatForTest(dex_location.c_str(),
662 CompilerFilter::kSpeed,
663 /*relocate*/true,
664 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000665 /*with_alternate_image*/true);
666
Calin Juravle357c66d2017-05-04 01:57:17 +0000667 ScopedNonWritable scoped_non_writable(dex_location);
668 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
669
Richard Uhler03bc6592016-11-22 09:42:04 +0000670 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000671 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100672 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000673 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100674 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000675 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000676 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
677
678 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
679 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
680 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
681 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
682}
683
684// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
685// respect to the boot image.
686// It shouldn't matter that the OAT file is out of date, because it is
687// verify-at-runtime.
688TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000689 if (IsExecutedAsRoot()) {
690 // We cannot simulate non writable locations when executed as root: b/38000545.
691 LOG(ERROR) << "Test skipped because it's running as root";
692 return;
693 }
694
Richard Uhler03bc6592016-11-22 09:42:04 +0000695 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
696
697 Copy(GetDexSrc1(), dex_location);
698 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100699 CompilerFilter::kExtract,
Richard Uhler03bc6592016-11-22 09:42:04 +0000700 /*relocate*/true,
701 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000702 /*with_alternate_image*/true);
703
Calin Juravle357c66d2017-05-04 01:57:17 +0000704 ScopedNonWritable scoped_non_writable(dex_location);
705 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
706
Richard Uhler03bc6592016-11-22 09:42:04 +0000707 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
708 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100709 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000710 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100711 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000712
713 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
714 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
715 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700716 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800717}
718
719// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800720TEST_F(OatFileAssistantTest, DexOdexNoOat) {
721 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700722 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800723
724 // Create the dex and odex files
725 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000726 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800727
728 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700729 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800730
Andreas Gampe29d38e72016-03-23 15:31:51 +0000731 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100732 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler5923b522016-12-08 09:48:01 +0000733 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000734 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800735
736 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000737 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
738 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700739 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700740
741 // We should still be able to get the non-executable odex file to run from.
742 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
743 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800744}
745
Richard Uhler5923b522016-12-08 09:48:01 +0000746// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800747TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
748 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700749 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800750
751 // Create the dex and odex files
752 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000753 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800754
755 // Strip the dex file
756 Copy(GetStrippedDexSrc1(), dex_location);
757
758 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700759 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800760
Richard Uhler5923b522016-12-08 09:48:01 +0000761 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000762 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800763
764 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000765 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000766 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700767 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800768
Richard Uhler66d874d2015-01-15 09:37:19 -0800769 // Verify we can load the dex files from it.
770 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
771 ASSERT_TRUE(oat_file.get() != nullptr);
772 EXPECT_TRUE(oat_file->IsExecutable());
773 std::vector<std::unique_ptr<const DexFile>> dex_files;
774 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
775 EXPECT_EQ(1u, dex_files.size());
776}
777
Richard Uhler5923b522016-12-08 09:48:01 +0000778// 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 -0800779TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
780 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700781 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800782
783 // Create the oat file from a different dex file so it looks out of date.
784 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000785 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800786
787 // Create the odex file
788 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000789 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800790
791 // Strip the dex file.
792 Copy(GetStrippedDexSrc1(), dex_location);
793
794 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700795 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800796
Andreas Gampe29d38e72016-03-23 15:31:51 +0000797 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100798 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000799 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
800 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100801 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file
Andreas Gampe29d38e72016-03-23 15:31:51 +0000802 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800803
804 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000805 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
806 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700807 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800808
809 // Verify we can load the dex files from it.
810 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
811 ASSERT_TRUE(oat_file.get() != nullptr);
812 EXPECT_TRUE(oat_file->IsExecutable());
813 std::vector<std::unique_ptr<const DexFile>> dex_files;
814 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
815 EXPECT_EQ(1u, dex_files.size());
816}
817
Richard Uhler9b994ea2015-06-24 08:44:19 -0700818// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
819// OAT file. Expect: The status is kNoDexOptNeeded.
820TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
821 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
822
823 Copy(GetStrippedDexSrc1(), dex_location);
824
825 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700826 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700827
Andreas Gampe29d38e72016-03-23 15:31:51 +0000828 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
829 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
830 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100831 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
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::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700834
835 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000836 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
837 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700838 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
839
840 // Make the oat file up to date. This should have no effect.
841 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700842 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700843 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -0700844 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg)) <<
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700845 error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700846
Andreas Gampe29d38e72016-03-23 15:31:51 +0000847 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
848 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700849
850 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000851 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
852 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700853 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
854}
855
Richard Uhler66d874d2015-01-15 09:37:19 -0800856// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
857// OAT files both have patch delta of 0.
Richard Uhler5923b522016-12-08 09:48:01 +0000858// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800859TEST_F(OatFileAssistantTest, OdexOatOverlap) {
860 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700861 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800862
Calin Juravle357c66d2017-05-04 01:57:17 +0000863 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800864 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000865 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000866 GenerateOatForTest(dex_location.c_str(),
867 CompilerFilter::kSpeed,
868 /*relocate*/false,
869 /*pic*/false,
870 /*with_alternate_image*/false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800871
872 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000873 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800874
Calin Juravle357c66d2017-05-04 01:57:17 +0000875 // -kDex2OatForRelocation is expected rather than kDex2OatForRelocation
876 // based on the assumption that the odex location is more up-to-date than the oat
Richard Uhler70a84262016-11-08 16:51:51 +0000877 // location, even if they both need relocation.
Calin Juravle357c66d2017-05-04 01:57:17 +0000878 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000879 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800880
881 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000882 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
883 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700884 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800885
886 // Things aren't relocated, so it should fall back to interpreted.
887 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
888 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700889
Richard Uhler66d874d2015-01-15 09:37:19 -0800890 EXPECT_FALSE(oat_file->IsExecutable());
891 std::vector<std::unique_ptr<const DexFile>> dex_files;
892 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
893 EXPECT_EQ(1u, dex_files.size());
894}
895
Andreas Gampe29d38e72016-03-23 15:31:51 +0000896// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
897// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
898TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
899 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
900 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000901
902 // Create the dex and odex files
903 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100904 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000905
906 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700907 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000908
Andreas Gampe29d38e72016-03-23 15:31:51 +0000909 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100910 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000911 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000912 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000913
914 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000915 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000916 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000917 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
918}
919
Richard Uhler66d874d2015-01-15 09:37:19 -0800920// Case: We have a DEX file and up-to-date OAT file for it.
921// Expect: We should load an executable dex file.
922TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000923 if (IsExecutedAsRoot()) {
924 // We cannot simulate non writable locations when executed as root: b/38000545.
925 LOG(ERROR) << "Test skipped because it's running as root";
926 return;
927 }
928
Richard Uhler66d874d2015-01-15 09:37:19 -0800929 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
930
931 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000932 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800933
Calin Juravle357c66d2017-05-04 01:57:17 +0000934 ScopedNonWritable scoped_non_writable(dex_location);
935 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
936
Richard Uhler66d874d2015-01-15 09:37:19 -0800937 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700938 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000939
940 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
941 ASSERT_TRUE(oat_file.get() != nullptr);
942 EXPECT_TRUE(oat_file->IsExecutable());
943 std::vector<std::unique_ptr<const DexFile>> dex_files;
944 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
945 EXPECT_EQ(1u, dex_files.size());
946}
947
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100948// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000949// Expect: We should still load the oat file as executable.
950TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000951 if (IsExecutedAsRoot()) {
952 // We cannot simulate non writable locations when executed as root: b/38000545.
953 LOG(ERROR) << "Test skipped because it's running as root";
954 return;
955 }
956
Andreas Gampe29d38e72016-03-23 15:31:51 +0000957 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
958
959 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100960 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000961
Calin Juravle357c66d2017-05-04 01:57:17 +0000962 ScopedNonWritable scoped_non_writable(dex_location);
963 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
964
Andreas Gampe29d38e72016-03-23 15:31:51 +0000965 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700966 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800967
968 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
969 ASSERT_TRUE(oat_file.get() != nullptr);
970 EXPECT_TRUE(oat_file->IsExecutable());
971 std::vector<std::unique_ptr<const DexFile>> dex_files;
972 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
973 EXPECT_EQ(1u, dex_files.size());
974}
975
976// Case: We have a DEX file and up-to-date OAT file for it.
977// Expect: Loading non-executable should load the oat non-executable.
978TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000979 if (IsExecutedAsRoot()) {
980 // We cannot simulate non writable locations when executed as root: b/38000545.
981 LOG(ERROR) << "Test skipped because it's running as root";
982 return;
983 }
984
Richard Uhler66d874d2015-01-15 09:37:19 -0800985 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
986
987 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000988
989 ScopedNonWritable scoped_non_writable(dex_location);
990 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
991
Andreas Gampe29d38e72016-03-23 15:31:51 +0000992 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800993
994 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700995 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800996
997 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
998 ASSERT_TRUE(oat_file.get() != nullptr);
999 EXPECT_FALSE(oat_file->IsExecutable());
1000 std::vector<std::unique_ptr<const DexFile>> dex_files;
1001 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1002 EXPECT_EQ(1u, dex_files.size());
1003}
1004
Richard Uhler8327cf72015-10-13 16:34:59 -07001005// Case: We don't have a DEX file and can't write the oat file.
1006// Expect: We should fail to generate the oat file without crashing.
1007TEST_F(OatFileAssistantTest, GenNoDex) {
Calin Juravle357c66d2017-05-04 01:57:17 +00001008 if (IsExecutedAsRoot()) {
1009 // We cannot simulate non writable locations when executed as root: b/38000545.
1010 LOG(ERROR) << "Test skipped because it's running as root";
1011 return;
1012 }
Richard Uhler8327cf72015-10-13 16:34:59 -07001013
Calin Juravle357c66d2017-05-04 01:57:17 +00001014 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1015
1016 ScopedNonWritable scoped_non_writable(dex_location);
1017 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
1018
1019 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001020 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001021 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Calin Juravle357c66d2017-05-04 01:57:17 +00001022 // We should get kUpdateSucceeded from MakeUpToDate since there's nothing
1023 // that can be done in this situation.
1024 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001025 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg));
Calin Juravle357c66d2017-05-04 01:57:17 +00001026
1027 // Verify it didn't create an oat in the default location (dalvik-cache).
1028 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
1029 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
1030 // Verify it didn't create the odex file in the default location (../oat/isa/...odex)
1031 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OdexFileStatus());
Richard Uhler8327cf72015-10-13 16:34:59 -07001032}
1033
Richard Uhler66d874d2015-01-15 09:37:19 -08001034// Turn an absolute path into a path relative to the current working
1035// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001036static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001037 char buf[MAXPATHLEN];
1038 std::string cwd = getcwd(buf, MAXPATHLEN);
1039
1040 // Split the target and cwd paths into components.
1041 std::vector<std::string> target_path;
1042 std::vector<std::string> cwd_path;
1043 Split(target, '/', &target_path);
1044 Split(cwd, '/', &cwd_path);
1045
1046 // Reverse the path components, so we can use pop_back().
1047 std::reverse(target_path.begin(), target_path.end());
1048 std::reverse(cwd_path.begin(), cwd_path.end());
1049
1050 // Drop the common prefix of the paths. Because we reversed the path
1051 // components, this becomes the common suffix of target_path and cwd_path.
1052 while (!target_path.empty() && !cwd_path.empty()
1053 && target_path.back() == cwd_path.back()) {
1054 target_path.pop_back();
1055 cwd_path.pop_back();
1056 }
1057
1058 // For each element of the remaining cwd_path, add '..' to the beginning
1059 // of the target path. Because we reversed the path components, we add to
1060 // the end of target_path.
1061 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1062 target_path.push_back("..");
1063 }
1064
1065 // Reverse again to get the right path order, and join to get the result.
1066 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -08001067 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -08001068}
1069
1070// Case: Non-absolute path to Dex location.
1071// Expect: Not sure, but it shouldn't crash.
1072TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1073 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1074 Copy(GetDexSrc1(), abs_dex_location);
1075
1076 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001077 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001078
1079 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001080 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001081 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001082 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1083 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001084}
1085
1086// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001087// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001088TEST_F(OatFileAssistantTest, ShortDexLocation) {
1089 std::string dex_location = "/xx";
1090
Richard Uhlerd1472a22016-04-15 15:18:56 -07001091 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001092
1093 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001094 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1095 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001096 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1097 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001098 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001099
Richard Uhler9b994ea2015-06-24 08:44:19 -07001100 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001101 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001102 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001103 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001104 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001105 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001106}
1107
1108// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001109// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001110TEST_F(OatFileAssistantTest, LongDexExtension) {
1111 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1112 Copy(GetDexSrc1(), dex_location);
1113
Richard Uhlerd1472a22016-04-15 15:18:56 -07001114 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001115
Richard Uhler7225a8d2016-11-22 10:12:03 +00001116 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001117 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001118
1119 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001120 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1121 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001122}
1123
1124// A task to generate a dex location. Used by the RaceToGenerate test.
1125class RaceGenerateTask : public Task {
1126 public:
1127 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001128 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001129 {}
1130
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001131 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001132 // Load the dex files, and save a pointer to the loaded oat file, so that
1133 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001134 std::vector<std::unique_ptr<const DexFile>> dex_files;
1135 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001136 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001137 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1138 dex_location_.c_str(),
Jeff Hao0cb17282017-07-12 14:51:49 -07001139 Runtime::Current()->GetSystemClassLoader(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001140 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001141 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001142 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001143 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001144 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1145 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001146 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001147 }
1148
1149 const OatFile* GetLoadedOatFile() const {
1150 return loaded_oat_file_;
1151 }
1152
1153 private:
1154 std::string dex_location_;
1155 std::string oat_location_;
1156 const OatFile* loaded_oat_file_;
1157};
1158
1159// Test the case where multiple processes race to generate an oat file.
1160// This simulates multiple processes using multiple threads.
1161//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001162// We want unique Oat files to be loaded even when there is a race to load.
1163// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1164// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001165TEST_F(OatFileAssistantTest, RaceToGenerate) {
1166 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001167 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001168
Jeff Hao0cb17282017-07-12 14:51:49 -07001169 // Start the runtime to initialize the system's class loader.
1170 Thread::Current()->TransitionFromSuspendedToRunnable();
1171 runtime_->Start();
1172
Richard Uhler66d874d2015-01-15 09:37:19 -08001173 // We use the lib core dex file, because it's large, and hopefully should
1174 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001175 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001176
1177 const int kNumThreads = 32;
1178 Thread* self = Thread::Current();
1179 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1180 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1181 for (int i = 0; i < kNumThreads; i++) {
1182 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1183 thread_pool.AddTask(self, task.get());
1184 tasks.push_back(std::move(task));
1185 }
1186 thread_pool.StartWorkers(self);
1187 thread_pool.Wait(self, true, false);
1188
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001189 // Verify every task got a unique oat file.
1190 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001191 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001192 const OatFile* oat_file = task->GetLoadedOatFile();
1193 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1194 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001195 }
1196}
1197
1198// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1199// disabled.
1200// Expect: We should load the odex file non-executable.
1201TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1202 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001203 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001204
1205 // Create the dex and odex files
1206 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001207 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001208
1209 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001210 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001211
1212 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1213 ASSERT_TRUE(oat_file.get() != nullptr);
1214 EXPECT_FALSE(oat_file->IsExecutable());
1215 std::vector<std::unique_ptr<const DexFile>> dex_files;
1216 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1217 EXPECT_EQ(1u, dex_files.size());
1218}
1219
1220// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1221// disabled.
1222// Expect: We should load the odex file non-executable.
1223TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1224 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001225 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001226
1227 // Create the dex and odex files
1228 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001229 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001230
1231 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001232 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001233
1234 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1235 ASSERT_TRUE(oat_file.get() != nullptr);
1236 EXPECT_FALSE(oat_file->IsExecutable());
1237 std::vector<std::unique_ptr<const DexFile>> dex_files;
1238 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1239 EXPECT_EQ(2u, dex_files.size());
1240}
1241
Richard Uhlerf4b34872016-04-13 11:03:46 -07001242TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1243 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1244 Copy(GetDexSrc1(), dex_location);
1245
Richard Uhlerd1472a22016-04-15 15:18:56 -07001246 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001247
1248 std::string error_msg;
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001249 Runtime::Current()->AddCompilerOption("--compiler-filter=quicken");
Richard Uhlerf4b34872016-04-13 11:03:46 -07001250 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001251 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg)) <<
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001252 error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +00001253 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001254 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +00001255 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001256 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1257
1258 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1259 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001260 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg))
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001261 << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001262 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001263 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001264 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1265 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1266
1267 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1268 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001269 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001270}
1271
Richard Uhlerb81881d2016-04-19 13:08:04 -07001272TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001273 std::string error_msg;
1274 std::string odex_file;
1275
Richard Uhlerb81881d2016-04-19 13:08:04 -07001276 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001277 "/foo/bar/baz.jar", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001278 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001279
Richard Uhlerb81881d2016-04-19 13:08:04 -07001280 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001281 "/foo/bar/baz.funnyext", InstructionSet::kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001282 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001283
Richard Uhlerb81881d2016-04-19 13:08:04 -07001284 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001285 "nopath.jar", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001286 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Vladimir Marko33bff252017-11-01 14:35:42 +00001287 "/foo/bar/baz_noext", InstructionSet::kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001288}
1289
Richard Uhler23cedd22015-04-08 13:17:29 -07001290// Verify the dexopt status values from dalvik.system.DexFile
1291// match the OatFileAssistant::DexOptStatus values.
1292TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001293 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1294 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1295 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1296 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1297 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1298 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001299 };
1300
Richard Uhler23cedd22015-04-08 13:17:29 -07001301 ScopedObjectAccess soa(Thread::Current());
1302 StackHandleScope<1> hs(soa.Self());
1303 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1304 Handle<mirror::Class> dexfile(
1305 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001306 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001307 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1308
Richard Uhler7225a8d2016-11-22 10:12:03 +00001309 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1310 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001311 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001312 ASSERT_FALSE(art_field == nullptr);
1313 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1314 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1315 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001316}
Richard Uhler66d874d2015-01-15 09:37:19 -08001317
Calin Juravle07c6d722017-06-07 17:06:12 +00001318// Verify that when no compiler filter is passed the default one from OatFileAssistant is used.
1319TEST_F(OatFileAssistantTest, DefaultMakeUpToDateFilter) {
1320 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1321 Copy(GetDexSrc1(), dex_location);
1322
1323 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1324
1325 const CompilerFilter::Filter default_filter =
1326 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1327 std::string error_msg;
1328 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001329 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg)) <<
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001330 error_msg;
Calin Juravle07c6d722017-06-07 17:06:12 +00001331 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1332 oat_file_assistant.GetDexOptNeeded(default_filter));
1333 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1334 EXPECT_NE(nullptr, oat_file.get());
1335 EXPECT_EQ(default_filter, oat_file->GetCompilerFilter());
1336}
1337
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001338TEST_F(OatFileAssistantTest, MakeUpToDateWithSpecialSharedLibrary) {
1339 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1340 Copy(GetDexSrc1(), dex_location);
1341
1342 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1343
1344 const CompilerFilter::Filter default_filter =
1345 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1346 std::string error_msg;
Calin Juravle44e5efa2017-09-12 00:54:26 -07001347 int status = oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibraryContext, &error_msg);
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001348 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, status) << error_msg;
1349 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1350 oat_file_assistant.GetDexOptNeeded(default_filter));
1351 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1352 EXPECT_NE(nullptr, oat_file.get());
1353 EXPECT_EQ(kSpecialSharedLibrary,
1354 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
1355}
1356
1357TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) {
1358 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1359 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1360 Copy(GetDexSrc1(), dex_location);
1361 Copy(GetDexSrc2(), context_location);
1362
1363 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1364
1365 const CompilerFilter::Filter default_filter =
1366 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1367 std::string error_msg;
1368 std::string context_str = "PCL[" + context_location + "]";
Calin Juravle44e5efa2017-09-12 00:54:26 -07001369 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1370 ASSERT_TRUE(context != nullptr);
1371 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
1372
1373 int status = oat_file_assistant.MakeUpToDate(false, context.get(), &error_msg);
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001374 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, status) << error_msg;
1375 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Calin Juravle44e5efa2017-09-12 00:54:26 -07001376 oat_file_assistant.GetDexOptNeeded(default_filter, false, false, context.get()));
1377
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001378 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1379 EXPECT_NE(nullptr, oat_file.get());
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001380 EXPECT_EQ(context->EncodeContextForOatFile(""),
1381 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
1382}
1383
Calin Juravle44e5efa2017-09-12 00:54:26 -07001384TEST_F(OatFileAssistantTest, GetDexOptNeededWithOutOfDateContext) {
1385 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1386 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1387 Copy(GetDexSrc1(), dex_location);
1388 Copy(GetDexSrc2(), context_location);
1389
1390 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1391
1392 const CompilerFilter::Filter default_filter =
1393 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1394 std::string error_msg;
1395 std::string context_str = "PCL[" + context_location + "]";
1396 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1397 ASSERT_TRUE(context != nullptr);
1398 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
1399
1400 int status = oat_file_assistant.MakeUpToDate(false, context.get(), &error_msg);
1401 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, status) << error_msg;
1402 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1403 oat_file_assistant.GetDexOptNeeded(default_filter, false, false, context.get()));
1404
1405 // Update the context by overriding the jar file.
1406 Copy(GetMultiDexSrc2(), context_location);
1407 std::unique_ptr<ClassLoaderContext> updated_context = ClassLoaderContext::Create(context_str);
1408 ASSERT_TRUE(updated_context != nullptr);
1409 // DexOptNeeded should advise compilation from scratch.
1410 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
1411 oat_file_assistant.GetDexOptNeeded(
1412 default_filter, false, false, updated_context.get()));
1413}
1414
Calin Juravle20c46442017-09-12 00:54:26 -07001415TEST_F(OatFileAssistantTest, GetDexOptNeededWithUpToDateContextRelative) {
1416 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1417 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1418 Copy(GetDexSrc1(), dex_location);
1419 Copy(GetDexSrc2(), context_location);
1420
1421 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1422
1423 const CompilerFilter::Filter default_filter =
1424 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1425 std::string error_msg;
1426 std::string context_str = "PCL[" + context_location + "]";
1427 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str);
1428 ASSERT_TRUE(context != nullptr);
1429 ASSERT_TRUE(context->OpenDexFiles(kRuntimeISA, ""));
1430
1431 int status = oat_file_assistant.MakeUpToDate(false, context.get(), &error_msg);
1432 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, status) << error_msg;
1433
1434 // A relative context simulates a dependent split context.
1435 std::unique_ptr<ClassLoaderContext> relative_context =
1436 ClassLoaderContext::Create("PCL[ContextDex.jar]");
1437 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1438 oat_file_assistant.GetDexOptNeeded(
1439 default_filter, false, false, relative_context.get()));
1440}
1441
Richard Uhler66d874d2015-01-15 09:37:19 -08001442// TODO: More Tests:
1443// * Test class linker falls back to unquickened dex for DexNoOat
1444// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001445// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001446// * Test for status of oat while oat is being generated (how?)
1447// * Test case where 32 and 64 bit boot class paths differ,
1448// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1449// 64 bit boot class paths.
1450// * Test unexpected scenarios (?):
1451// - Dex is stripped, don't have odex.
1452// - Oat file corrupted after status check, before reload unexecutable
1453// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001454} // namespace art