blob: e048177fa4468b4fdc83a5f6dec8c0834a4d635a [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>
23
Richard Uhler66d874d2015-01-15 09:37:19 -080024#include <gtest/gtest.h>
25
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "android-base/strings.h"
27
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010029#include "class_linker-inl.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070030#include "class_loader_context.h"
Jeff Hao0cb17282017-07-12 14:51:49 -070031#include "common_runtime_test.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080032#include "dexopt_test.h"
Calin Juravle27e0d1f2017-07-26 00:16:07 -070033#include "oat_file.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070034#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070037#include "thread-current-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038#include "utils.h"
39
40namespace art {
41
Calin Juravle27e0d1f2017-07-26 00:16:07 -070042static const std::string kSpecialSharedLibrary = "&";
43
Calin Juravle36eb3132017-01-13 16:32:38 -080044class OatFileAssistantTest : public DexoptTest {};
Richard Uhler66d874d2015-01-15 09:37:19 -080045
Calin Juravle36eb3132017-01-13 16:32:38 -080046class OatFileAssistantNoDex2OatTest : public DexoptTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080047 public:
48 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Calin Juravle36eb3132017-01-13 16:32:38 -080049 DexoptTest::SetUpRuntimeOptions(options);
Richard Uhler66d874d2015-01-15 09:37:19 -080050 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
51 }
52};
53
Calin Juravle357c66d2017-05-04 01:57:17 +000054class ScopedNonWritable {
55 public:
56 explicit ScopedNonWritable(const std::string& dex_location) {
57 is_valid_ = false;
58 size_t pos = dex_location.rfind('/');
59 if (pos != std::string::npos) {
60 is_valid_ = true;
61 dex_parent_ = dex_location.substr(0, pos);
62 if (chmod(dex_parent_.c_str(), 0555) != 0) {
63 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
64 }
65 }
66 }
67
68 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
69
70 ~ScopedNonWritable() {
71 if (is_valid_) {
72 if (chmod(dex_parent_.c_str(), 0777) != 0) {
73 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
74 }
75 }
76 }
77
78 private:
79 std::string dex_parent_;
80 bool is_valid_;
81};
82
83static bool IsExecutedAsRoot() {
84 return geteuid() == 0;
85}
Calin Juravle36eb3132017-01-13 16:32:38 -080086
Richard Uhler66d874d2015-01-15 09:37:19 -080087// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -070088// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -080089TEST_F(OatFileAssistantTest, DexNoOat) {
90 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
91 Copy(GetDexSrc1(), dex_location);
92
Richard Uhlerd1472a22016-04-15 15:18:56 -070093 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -080094
Richard Uhler7225a8d2016-11-22 10:12:03 +000095 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +010096 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
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::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +000099 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000100 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
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::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800103
104 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000105 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
106 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700107 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800108}
109
110// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700111// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800112TEST_F(OatFileAssistantTest, NoDexNoOat) {
113 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
114
Richard Uhlerd1472a22016-04-15 15:18:56 -0700115 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800116
Andreas Gampe29d38e72016-03-23 15:31:51 +0000117 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
118 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700119 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
120
121 // Trying to make the oat file up to date should not fail or crash.
122 std::string error_msg;
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700123 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
124 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700125
126 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800127 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
128 EXPECT_EQ(nullptr, oat_file.get());
129}
130
Calin Juravle357c66d2017-05-04 01:57:17 +0000131// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
132// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
133TEST_F(OatFileAssistantTest, OdexUpToDate) {
134 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
135 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
136 Copy(GetDexSrc1(), dex_location);
137 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
138
139 // For the use of oat location by making the dex parent not writable.
140 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
141
142 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
143 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
144 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
145 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
146 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
147 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
148 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
149 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
150
151 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
152 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
153 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
154 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
155}
156
157// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
158// file via a symlink.
159// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
160TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
161 std::string scratch_dir = GetScratchDir();
162 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
163 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
164
165 Copy(GetDexSrc1(), dex_location);
166 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
167
168 // Now replace the dex location with a symlink.
169 std::string link = scratch_dir + "/link";
170 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
171 dex_location = link + "/OdexUpToDate.jar";
172
173 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
174
175 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
176 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
177 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
178 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
179 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
180 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
181 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
182 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
183
184 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
185 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
186 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
187 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
188}
189
Richard Uhler66d874d2015-01-15 09:37:19 -0800190// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700191// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800192TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000193 if (IsExecutedAsRoot()) {
194 // We cannot simulate non writable locations when executed as root: b/38000545.
195 LOG(ERROR) << "Test skipped because it's running as root";
196 return;
197 }
198
Richard Uhler66d874d2015-01-15 09:37:19 -0800199 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
200 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000201 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800202
Calin Juravle357c66d2017-05-04 01:57:17 +0000203 // For the use of oat location by making the dex parent not writable.
204 ScopedNonWritable scoped_non_writable(dex_location);
205 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
206
207 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
208
209 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
210 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
211 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
212 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
213 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
214 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
215 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
216 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
217
218 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
219 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
220 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
221 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
222}
223
224// Case: We have a DEX file and up-to-date OAT file for it. We load the dex file
225// via a symlink.
226// Expect: The status is kNoDexOptNeeded.
227TEST_F(OatFileAssistantTest, OatUpToDateSymLink) {
228 if (IsExecutedAsRoot()) {
229 // We cannot simulate non writable locations when executed as root: b/38000545.
230 LOG(ERROR) << "Test skipped because it's running as root";
231 return;
232 }
233
234 std::string real = GetScratchDir() + "/real";
235 ASSERT_EQ(0, mkdir(real.c_str(), 0700));
236 std::string link = GetScratchDir() + "/link";
237 ASSERT_EQ(0, symlink(real.c_str(), link.c_str()));
238
239 std::string dex_location = real + "/OatUpToDate.jar";
240
241 Copy(GetDexSrc1(), dex_location);
242 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
243
244 // Update the dex location to point to the symlink.
245 dex_location = link + "/OatUpToDate.jar";
246
247 // For the use of oat location by making the dex parent not writable.
248 ScopedNonWritable scoped_non_writable(dex_location);
249 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
250
Richard Uhlerd1472a22016-04-15 15:18:56 -0700251 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800252
Andreas Gampe29d38e72016-03-23 15:31:51 +0000253 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
254 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
255 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100256 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000257 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100258 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000259 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000260 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
261
Richard Uhler66d874d2015-01-15 09:37:19 -0800262 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000263 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler95abd042015-03-24 09:51:28 -0700264 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700265 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800266}
267
Richard Uhler2f27abd2017-01-31 14:02:34 +0000268// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
269// ODEX file.
270TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
271 // This test case is only meaningful if vdex is enabled.
272 if (!kIsVdexEnabled) {
273 return;
274 }
Richard Uhler9a37efc2016-08-05 16:32:55 -0700275
Richard Uhler2f27abd2017-01-31 14:02:34 +0000276 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000277 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000278
Richard Uhler9a37efc2016-08-05 16:32:55 -0700279 Copy(GetDexSrc1(), dex_location);
280
Richard Uhler2f27abd2017-01-31 14:02:34 +0000281 // Generating and deleting the oat file should have the side effect of
282 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000283 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
284 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000285
Calin Juravle357c66d2017-05-04 01:57:17 +0000286 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000287
288 // Even though the vdex file is up to date, because we don't have the oat
289 // file, we can't know that the vdex depends on the boot image and is up to
290 // date with respect to the boot image. Instead we must assume the vdex file
291 // depends on the boot image and is out of date with respect to the boot
292 // image.
293 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
294 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
295
296 // Make sure we don't crash in this case when we dump the status. We don't
297 // care what the actual dumped value is.
298 oat_file_assistant.GetStatusDump();
299}
300
301// Case: We have a DEX file and empty VDEX and ODEX files.
302TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
303 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
304 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
305 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
306
307 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000308 ScratchFile vdex_file(vdex_location.c_str());
309 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000310
311 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
312 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
313 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
314}
315
316// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
317// file.
318TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
319 // This test case is only meaningful if vdex is enabled.
320 if (!kIsVdexEnabled) {
321 return;
322 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000323 if (IsExecutedAsRoot()) {
324 // We cannot simulate non writable locations when executed as root: b/38000545.
325 LOG(ERROR) << "Test skipped because it's running as root";
326 return;
327 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000328
329 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
330 std::string oat_location;
331 std::string error_msg;
332 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
333 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
334
335 Copy(GetDexSrc1(), dex_location);
336 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
337 ASSERT_EQ(0, unlink(oat_location.c_str()));
338
Calin Juravle357c66d2017-05-04 01:57:17 +0000339 ScopedNonWritable scoped_non_writable(dex_location);
340 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700341 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
342
Richard Uhler2f27abd2017-01-31 14:02:34 +0000343 // Even though the vdex file is up to date, because we don't have the oat
344 // file, we can't know that the vdex depends on the boot image and is up to
345 // date with respect to the boot image. Instead we must assume the vdex file
346 // depends on the boot image and is out of date with respect to the boot
347 // image.
348 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700349 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700350}
351
Andreas Gampe29d38e72016-03-23 15:31:51 +0000352// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700353// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
354// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000355TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000356 if (IsExecutedAsRoot()) {
357 // We cannot simulate non writable locations when executed as root: b/38000545.
358 LOG(ERROR) << "Test skipped because it's running as root";
359 return;
360 }
361
Andreas Gampe29d38e72016-03-23 15:31:51 +0000362 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
363 Copy(GetDexSrc1(), dex_location);
364 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
365
Calin Juravle357c66d2017-05-04 01:57:17 +0000366 ScopedNonWritable scoped_non_writable(dex_location);
367 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
368
Richard Uhlerd1472a22016-04-15 15:18:56 -0700369 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000370
371 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700372 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000373 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100374 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000375 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700376 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000377 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100378 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000379
380 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000381 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000382 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
383 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
384}
385
Richard Uhler66d874d2015-01-15 09:37:19 -0800386// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700387// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800388TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000389 if (IsExecutedAsRoot()) {
390 // We cannot simulate non writable locations when executed as root: b/38000545.
391 LOG(ERROR) << "Test skipped because it's running as root";
392 return;
393 }
394
Richard Uhler66d874d2015-01-15 09:37:19 -0800395 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
396 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000397 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800398
Calin Juravle357c66d2017-05-04 01:57:17 +0000399 ScopedNonWritable scoped_non_writable(dex_location);
400 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
401
Richard Uhlerd1472a22016-04-15 15:18:56 -0700402 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000403 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700404 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700405 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700406
407 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700408 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800409 ASSERT_TRUE(oat_file.get() != nullptr);
410 EXPECT_TRUE(oat_file->IsExecutable());
411 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700412 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
413 EXPECT_EQ(2u, dex_files.size());
414}
415
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000416// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700417// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000418TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000419 if (IsExecutedAsRoot()) {
420 // We cannot simulate non writable locations when executed as root: b/38000545.
421 LOG(ERROR) << "Test skipped because it's running as root";
422 return;
423 }
424
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000425 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700426
427 // Compile code for GetMultiDexSrc1.
428 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000429 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700430
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000431 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700432 // is out of date.
433 Copy(GetMultiDexSrc2(), dex_location);
434
Calin Juravle357c66d2017-05-04 01:57:17 +0000435 ScopedNonWritable scoped_non_writable(dex_location);
436 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
437
Richard Uhlerd1472a22016-04-15 15:18:56 -0700438 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000439 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700440 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700441 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700442}
443
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000444// Case: We have a stripped MultiDEX file where the non-main multidex entry is
445// out of date with respect to the odex file.
446TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
447 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
448 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
449
450 // Compile the oat from GetMultiDexSrc1.
451 Copy(GetMultiDexSrc1(), dex_location);
452 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
453
454 // Compile the odex from GetMultiDexSrc2, which has a different non-main
455 // dex checksum.
456 Copy(GetMultiDexSrc2(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100457 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000458
459 // Strip the dex file.
460 Copy(GetStrippedDexSrc1(), dex_location);
461
462 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable*/false);
463
464 // Because the dex file is stripped, the odex file is considered the source
465 // of truth for the dex checksums. The oat file should be considered
466 // unusable.
467 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
468 ASSERT_TRUE(best_file.get() != nullptr);
469 EXPECT_EQ(best_file->GetLocation(), odex_location);
470 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
471 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
472 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
473}
474
Calin Juravle357c66d2017-05-04 01:57:17 +0000475// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
Richard Uhlere5fed032015-03-18 08:21:11 -0700476// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700477// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700478TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
479 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000480 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
Richard Uhlere5fed032015-03-18 08:21:11 -0700481
482 // Create the dex file
483 Copy(GetMultiDexSrc1(), dex_location);
484
485 // Create the oat file with relative encoded dex location.
486 std::vector<std::string> args;
487 args.push_back("--dex-file=" + dex_location);
488 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
Calin Juravle357c66d2017-05-04 01:57:17 +0000489 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000490 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700491
492 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700493 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700494
495 // Verify we can load both dex files.
Calin Juravle357c66d2017-05-04 01:57:17 +0000496 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
497
Richard Uhlere5fed032015-03-18 08:21:11 -0700498 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
499 ASSERT_TRUE(oat_file.get() != nullptr);
500 EXPECT_TRUE(oat_file->IsExecutable());
501 std::vector<std::unique_ptr<const DexFile>> dex_files;
502 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800503 EXPECT_EQ(2u, dex_files.size());
504}
505
Richard Uhler03bc6592016-11-22 09:42:04 +0000506// Case: We have a DEX file and an OAT file out of date with respect to the
507// dex checksum.
508TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000509 if (IsExecutedAsRoot()) {
510 // We cannot simulate non writable locations when executed as root: b/38000545.
511 LOG(ERROR) << "Test skipped because it's running as root";
512 return;
513 }
514
Richard Uhler03bc6592016-11-22 09:42:04 +0000515 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800516
517 // We create a dex, generate an oat for it, then overwrite the dex with a
518 // different dex to make the oat out of date.
519 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000520 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800521 Copy(GetDexSrc2(), dex_location);
522
Calin Juravle357c66d2017-05-04 01:57:17 +0000523 ScopedNonWritable scoped_non_writable(dex_location);
524 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
525
Richard Uhlerd1472a22016-04-15 15:18:56 -0700526 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000527 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100528 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000529 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000530 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800531
532 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000533 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
534 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
535 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
536}
537
Richard Uhler2f27abd2017-01-31 14:02:34 +0000538// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
539// to the dex checksum, but no ODEX file.
540TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
541 // This test case is only meaningful if vdex is enabled.
542 if (!kIsVdexEnabled) {
543 return;
544 }
545
546 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000547 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000548
549 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000550 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
551 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000552 Copy(GetDexSrc2(), dex_location);
553
Calin Juravle357c66d2017-05-04 01:57:17 +0000554 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000555
556 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
557 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
558}
559
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000560// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
561// is out of date and there is no corresponding ODEX file.
562TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000563 // This test case is only meaningful if vdex is enabled.
564 if (!kIsVdexEnabled) {
565 return;
566 }
567
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000568 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000569 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000570
571 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000572 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
573 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000574 Copy(GetMultiDexSrc2(), dex_location);
575
Calin Juravle357c66d2017-05-04 01:57:17 +0000576 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000577
578 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
579 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
580}
581
Richard Uhler03bc6592016-11-22 09:42:04 +0000582// Case: We have a DEX file and an OAT file out of date with respect to the
583// boot image.
584TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000585 if (IsExecutedAsRoot()) {
586 // We cannot simulate non writable locations when executed as root: b/38000545.
587 LOG(ERROR) << "Test skipped because it's running as root";
588 return;
589 }
590
Richard Uhler03bc6592016-11-22 09:42:04 +0000591 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
592
593 Copy(GetDexSrc1(), dex_location);
594 GenerateOatForTest(dex_location.c_str(),
595 CompilerFilter::kSpeed,
596 /*relocate*/true,
597 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000598 /*with_alternate_image*/true);
599
Calin Juravle357c66d2017-05-04 01:57:17 +0000600 ScopedNonWritable scoped_non_writable(dex_location);
601 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
602
Richard Uhler03bc6592016-11-22 09:42:04 +0000603 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000604 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
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::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100607 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000608 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000609 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
610
611 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
612 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
613 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
614 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
615}
616
617// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
618// respect to the boot image.
619// It shouldn't matter that the OAT file is out of date, because it is
620// verify-at-runtime.
621TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000622 if (IsExecutedAsRoot()) {
623 // We cannot simulate non writable locations when executed as root: b/38000545.
624 LOG(ERROR) << "Test skipped because it's running as root";
625 return;
626 }
627
Richard Uhler03bc6592016-11-22 09:42:04 +0000628 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
629
630 Copy(GetDexSrc1(), dex_location);
631 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100632 CompilerFilter::kExtract,
Richard Uhler03bc6592016-11-22 09:42:04 +0000633 /*relocate*/true,
634 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000635 /*with_alternate_image*/true);
636
Calin Juravle357c66d2017-05-04 01:57:17 +0000637 ScopedNonWritable scoped_non_writable(dex_location);
638 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
639
Richard Uhler03bc6592016-11-22 09:42:04 +0000640 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
641 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100642 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000643 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100644 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000645
646 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
647 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
648 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700649 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800650}
651
652// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800653TEST_F(OatFileAssistantTest, DexOdexNoOat) {
654 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700655 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800656
657 // Create the dex and odex files
658 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000659 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800660
661 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700662 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800663
Andreas Gampe29d38e72016-03-23 15:31:51 +0000664 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100665 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler5923b522016-12-08 09:48:01 +0000666 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000667 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800668
669 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000670 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
671 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700672 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700673
674 // We should still be able to get the non-executable odex file to run from.
675 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
676 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800677}
678
Richard Uhler5923b522016-12-08 09:48:01 +0000679// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800680TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
681 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700682 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800683
684 // Create the dex and odex files
685 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000686 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800687
688 // Strip the dex file
689 Copy(GetStrippedDexSrc1(), dex_location);
690
691 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700692 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800693
Richard Uhler5923b522016-12-08 09:48:01 +0000694 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000695 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800696
697 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000698 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000699 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700700 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800701
Richard Uhler66d874d2015-01-15 09:37:19 -0800702 // Verify we can load the dex files from it.
703 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
704 ASSERT_TRUE(oat_file.get() != nullptr);
705 EXPECT_TRUE(oat_file->IsExecutable());
706 std::vector<std::unique_ptr<const DexFile>> dex_files;
707 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
708 EXPECT_EQ(1u, dex_files.size());
709}
710
Richard Uhler5923b522016-12-08 09:48:01 +0000711// 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 -0800712TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
713 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700714 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800715
716 // Create the oat file from a different dex file so it looks out of date.
717 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000718 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800719
720 // Create the odex file
721 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000722 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800723
724 // Strip the dex file.
725 Copy(GetStrippedDexSrc1(), dex_location);
726
727 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700728 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800729
Andreas Gampe29d38e72016-03-23 15:31:51 +0000730 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100731 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000732 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
733 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100734 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file
Andreas Gampe29d38e72016-03-23 15:31:51 +0000735 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800736
737 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000738 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
739 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700740 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800741
742 // Verify we can load the dex files from it.
743 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
744 ASSERT_TRUE(oat_file.get() != nullptr);
745 EXPECT_TRUE(oat_file->IsExecutable());
746 std::vector<std::unique_ptr<const DexFile>> dex_files;
747 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
748 EXPECT_EQ(1u, dex_files.size());
749}
750
Richard Uhler9b994ea2015-06-24 08:44:19 -0700751// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
752// OAT file. Expect: The status is kNoDexOptNeeded.
753TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
754 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
755
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 Uhler9b994ea2015-06-24 08:44:19 -0700760
Andreas Gampe29d38e72016-03-23 15:31:51 +0000761 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
762 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
763 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100764 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000765 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100766 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700767
768 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000769 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
770 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700771 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
772
773 // Make the oat file up to date. This should have no effect.
774 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700775 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700776 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700777 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg)) <<
778 error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700779
Andreas Gampe29d38e72016-03-23 15:31:51 +0000780 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
781 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700782
783 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000784 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
785 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700786 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
787}
788
Richard Uhler66d874d2015-01-15 09:37:19 -0800789// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
790// OAT files both have patch delta of 0.
Richard Uhler5923b522016-12-08 09:48:01 +0000791// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800792TEST_F(OatFileAssistantTest, OdexOatOverlap) {
793 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700794 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800795
Calin Juravle357c66d2017-05-04 01:57:17 +0000796 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800797 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000798 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000799 GenerateOatForTest(dex_location.c_str(),
800 CompilerFilter::kSpeed,
801 /*relocate*/false,
802 /*pic*/false,
803 /*with_alternate_image*/false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800804
805 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000806 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800807
Calin Juravle357c66d2017-05-04 01:57:17 +0000808 // -kDex2OatForRelocation is expected rather than kDex2OatForRelocation
809 // based on the assumption that the odex location is more up-to-date than the oat
Richard Uhler70a84262016-11-08 16:51:51 +0000810 // location, even if they both need relocation.
Calin Juravle357c66d2017-05-04 01:57:17 +0000811 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000812 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800813
814 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000815 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
816 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700817 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800818
819 // Things aren't relocated, so it should fall back to interpreted.
820 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
821 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700822
Richard Uhler66d874d2015-01-15 09:37:19 -0800823 EXPECT_FALSE(oat_file->IsExecutable());
824 std::vector<std::unique_ptr<const DexFile>> dex_files;
825 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
826 EXPECT_EQ(1u, dex_files.size());
827}
828
Andreas Gampe29d38e72016-03-23 15:31:51 +0000829// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
830// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
831TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
832 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
833 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000834
835 // Create the dex and odex files
836 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100837 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000838
839 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700840 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000841
Andreas Gampe29d38e72016-03-23 15:31:51 +0000842 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100843 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000844 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000845 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000846
847 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000848 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000849 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000850 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
851}
852
Richard Uhler66d874d2015-01-15 09:37:19 -0800853// Case: We have a DEX file and up-to-date OAT file for it.
854// Expect: We should load an executable dex file.
855TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000856 if (IsExecutedAsRoot()) {
857 // We cannot simulate non writable locations when executed as root: b/38000545.
858 LOG(ERROR) << "Test skipped because it's running as root";
859 return;
860 }
861
Richard Uhler66d874d2015-01-15 09:37:19 -0800862 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
863
864 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000865 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800866
Calin Juravle357c66d2017-05-04 01:57:17 +0000867 ScopedNonWritable scoped_non_writable(dex_location);
868 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
869
Richard Uhler66d874d2015-01-15 09:37:19 -0800870 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700871 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000872
873 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
874 ASSERT_TRUE(oat_file.get() != nullptr);
875 EXPECT_TRUE(oat_file->IsExecutable());
876 std::vector<std::unique_ptr<const DexFile>> dex_files;
877 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
878 EXPECT_EQ(1u, dex_files.size());
879}
880
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100881// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000882// Expect: We should still load the oat file as executable.
883TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000884 if (IsExecutedAsRoot()) {
885 // We cannot simulate non writable locations when executed as root: b/38000545.
886 LOG(ERROR) << "Test skipped because it's running as root";
887 return;
888 }
889
Andreas Gampe29d38e72016-03-23 15:31:51 +0000890 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
891
892 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100893 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000894
Calin Juravle357c66d2017-05-04 01:57:17 +0000895 ScopedNonWritable scoped_non_writable(dex_location);
896 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
897
Andreas Gampe29d38e72016-03-23 15:31:51 +0000898 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700899 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800900
901 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
902 ASSERT_TRUE(oat_file.get() != nullptr);
903 EXPECT_TRUE(oat_file->IsExecutable());
904 std::vector<std::unique_ptr<const DexFile>> dex_files;
905 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
906 EXPECT_EQ(1u, dex_files.size());
907}
908
909// Case: We have a DEX file and up-to-date OAT file for it.
910// Expect: Loading non-executable should load the oat non-executable.
911TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000912 if (IsExecutedAsRoot()) {
913 // We cannot simulate non writable locations when executed as root: b/38000545.
914 LOG(ERROR) << "Test skipped because it's running as root";
915 return;
916 }
917
Richard Uhler66d874d2015-01-15 09:37:19 -0800918 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
919
920 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000921
922 ScopedNonWritable scoped_non_writable(dex_location);
923 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
924
Andreas Gampe29d38e72016-03-23 15:31:51 +0000925 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800926
927 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700928 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800929
930 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
931 ASSERT_TRUE(oat_file.get() != nullptr);
932 EXPECT_FALSE(oat_file->IsExecutable());
933 std::vector<std::unique_ptr<const DexFile>> dex_files;
934 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
935 EXPECT_EQ(1u, dex_files.size());
936}
937
Richard Uhler8327cf72015-10-13 16:34:59 -0700938// Case: We don't have a DEX file and can't write the oat file.
939// Expect: We should fail to generate the oat file without crashing.
940TEST_F(OatFileAssistantTest, GenNoDex) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000941 if (IsExecutedAsRoot()) {
942 // We cannot simulate non writable locations when executed as root: b/38000545.
943 LOG(ERROR) << "Test skipped because it's running as root";
944 return;
945 }
Richard Uhler8327cf72015-10-13 16:34:59 -0700946
Calin Juravle357c66d2017-05-04 01:57:17 +0000947 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
948
949 ScopedNonWritable scoped_non_writable(dex_location);
950 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
951
952 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700953 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700954 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Calin Juravle357c66d2017-05-04 01:57:17 +0000955 // We should get kUpdateSucceeded from MakeUpToDate since there's nothing
956 // that can be done in this situation.
957 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700958 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg));
Calin Juravle357c66d2017-05-04 01:57:17 +0000959
960 // Verify it didn't create an oat in the default location (dalvik-cache).
961 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
962 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
963 // Verify it didn't create the odex file in the default location (../oat/isa/...odex)
964 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OdexFileStatus());
Richard Uhler8327cf72015-10-13 16:34:59 -0700965}
966
Richard Uhler66d874d2015-01-15 09:37:19 -0800967// Turn an absolute path into a path relative to the current working
968// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -0800969static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800970 char buf[MAXPATHLEN];
971 std::string cwd = getcwd(buf, MAXPATHLEN);
972
973 // Split the target and cwd paths into components.
974 std::vector<std::string> target_path;
975 std::vector<std::string> cwd_path;
976 Split(target, '/', &target_path);
977 Split(cwd, '/', &cwd_path);
978
979 // Reverse the path components, so we can use pop_back().
980 std::reverse(target_path.begin(), target_path.end());
981 std::reverse(cwd_path.begin(), cwd_path.end());
982
983 // Drop the common prefix of the paths. Because we reversed the path
984 // components, this becomes the common suffix of target_path and cwd_path.
985 while (!target_path.empty() && !cwd_path.empty()
986 && target_path.back() == cwd_path.back()) {
987 target_path.pop_back();
988 cwd_path.pop_back();
989 }
990
991 // For each element of the remaining cwd_path, add '..' to the beginning
992 // of the target path. Because we reversed the path components, we add to
993 // the end of target_path.
994 for (unsigned int i = 0; i < cwd_path.size(); i++) {
995 target_path.push_back("..");
996 }
997
998 // Reverse again to get the right path order, and join to get the result.
999 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -08001000 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -08001001}
1002
1003// Case: Non-absolute path to Dex location.
1004// Expect: Not sure, but it shouldn't crash.
1005TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1006 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1007 Copy(GetDexSrc1(), abs_dex_location);
1008
1009 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001010 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001011
1012 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001013 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001014 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001015 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1016 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001017}
1018
1019// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001020// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001021TEST_F(OatFileAssistantTest, ShortDexLocation) {
1022 std::string dex_location = "/xx";
1023
Richard Uhlerd1472a22016-04-15 15:18:56 -07001024 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001025
1026 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001027 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1028 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001029 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1030 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001031 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001032
Richard Uhler9b994ea2015-06-24 08:44:19 -07001033 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001034 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001035 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001036 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001037 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001038 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001039}
1040
1041// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001042// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001043TEST_F(OatFileAssistantTest, LongDexExtension) {
1044 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1045 Copy(GetDexSrc1(), dex_location);
1046
Richard Uhlerd1472a22016-04-15 15:18:56 -07001047 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001048
Richard Uhler7225a8d2016-11-22 10:12:03 +00001049 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001050 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001051
1052 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001053 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1054 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001055}
1056
1057// A task to generate a dex location. Used by the RaceToGenerate test.
1058class RaceGenerateTask : public Task {
1059 public:
1060 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001061 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001062 {}
1063
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001064 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001065 // Load the dex files, and save a pointer to the loaded oat file, so that
1066 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001067 std::vector<std::unique_ptr<const DexFile>> dex_files;
1068 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001069 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001070 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1071 dex_location_.c_str(),
Jeff Hao0cb17282017-07-12 14:51:49 -07001072 Runtime::Current()->GetSystemClassLoader(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001073 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001074 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001075 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001076 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001077 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1078 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001079 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001080 }
1081
1082 const OatFile* GetLoadedOatFile() const {
1083 return loaded_oat_file_;
1084 }
1085
1086 private:
1087 std::string dex_location_;
1088 std::string oat_location_;
1089 const OatFile* loaded_oat_file_;
1090};
1091
1092// Test the case where multiple processes race to generate an oat file.
1093// This simulates multiple processes using multiple threads.
1094//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001095// We want unique Oat files to be loaded even when there is a race to load.
1096// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1097// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001098TEST_F(OatFileAssistantTest, RaceToGenerate) {
1099 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001100 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001101
Jeff Hao0cb17282017-07-12 14:51:49 -07001102 // Start the runtime to initialize the system's class loader.
1103 Thread::Current()->TransitionFromSuspendedToRunnable();
1104 runtime_->Start();
1105
Richard Uhler66d874d2015-01-15 09:37:19 -08001106 // We use the lib core dex file, because it's large, and hopefully should
1107 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001108 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001109
1110 const int kNumThreads = 32;
1111 Thread* self = Thread::Current();
1112 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1113 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1114 for (int i = 0; i < kNumThreads; i++) {
1115 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1116 thread_pool.AddTask(self, task.get());
1117 tasks.push_back(std::move(task));
1118 }
1119 thread_pool.StartWorkers(self);
1120 thread_pool.Wait(self, true, false);
1121
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001122 // Verify every task got a unique oat file.
1123 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001124 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001125 const OatFile* oat_file = task->GetLoadedOatFile();
1126 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1127 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001128 }
1129}
1130
1131// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1132// disabled.
1133// Expect: We should load the odex file non-executable.
1134TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1135 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001136 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001137
1138 // Create the dex and odex files
1139 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001140 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001141
1142 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001143 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001144
1145 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1146 ASSERT_TRUE(oat_file.get() != nullptr);
1147 EXPECT_FALSE(oat_file->IsExecutable());
1148 std::vector<std::unique_ptr<const DexFile>> dex_files;
1149 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1150 EXPECT_EQ(1u, dex_files.size());
1151}
1152
1153// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1154// disabled.
1155// Expect: We should load the odex file non-executable.
1156TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1157 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001158 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001159
1160 // Create the dex and odex files
1161 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001162 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001163
1164 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001165 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001166
1167 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1168 ASSERT_TRUE(oat_file.get() != nullptr);
1169 EXPECT_FALSE(oat_file->IsExecutable());
1170 std::vector<std::unique_ptr<const DexFile>> dex_files;
1171 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1172 EXPECT_EQ(2u, dex_files.size());
1173}
1174
Richard Uhlerf4b34872016-04-13 11:03:46 -07001175TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1176 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1177 Copy(GetDexSrc1(), dex_location);
1178
Richard Uhlerd1472a22016-04-15 15:18:56 -07001179 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001180
1181 std::string error_msg;
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001182 Runtime::Current()->AddCompilerOption("--compiler-filter=quicken");
Richard Uhlerf4b34872016-04-13 11:03:46 -07001183 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001184 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg)) <<
1185 error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +00001186 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001187 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +00001188 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001189 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1190
1191 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1192 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001193 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg))
1194 << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001195 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001196 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001197 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1198 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1199
1200 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1201 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001202 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001203}
1204
Richard Uhlerb81881d2016-04-19 13:08:04 -07001205TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001206 std::string error_msg;
1207 std::string odex_file;
1208
Richard Uhlerb81881d2016-04-19 13:08:04 -07001209 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001210 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001211 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001212
Richard Uhlerb81881d2016-04-19 13:08:04 -07001213 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001214 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001215 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001216
Richard Uhlerb81881d2016-04-19 13:08:04 -07001217 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001218 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001219 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001220 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001221}
1222
Richard Uhler23cedd22015-04-08 13:17:29 -07001223// Verify the dexopt status values from dalvik.system.DexFile
1224// match the OatFileAssistant::DexOptStatus values.
1225TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001226 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1227 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1228 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1229 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1230 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1231 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001232 };
1233
Richard Uhler23cedd22015-04-08 13:17:29 -07001234 ScopedObjectAccess soa(Thread::Current());
1235 StackHandleScope<1> hs(soa.Self());
1236 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1237 Handle<mirror::Class> dexfile(
1238 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001239 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001240 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1241
Richard Uhler7225a8d2016-11-22 10:12:03 +00001242 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1243 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001244 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001245 ASSERT_FALSE(art_field == nullptr);
1246 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1247 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1248 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001249}
Richard Uhler66d874d2015-01-15 09:37:19 -08001250
Calin Juravle07c6d722017-06-07 17:06:12 +00001251// Verify that when no compiler filter is passed the default one from OatFileAssistant is used.
1252TEST_F(OatFileAssistantTest, DefaultMakeUpToDateFilter) {
1253 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1254 Copy(GetDexSrc1(), dex_location);
1255
1256 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1257
1258 const CompilerFilter::Filter default_filter =
1259 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1260 std::string error_msg;
1261 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001262 oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg)) <<
1263 error_msg;
Calin Juravle07c6d722017-06-07 17:06:12 +00001264 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1265 oat_file_assistant.GetDexOptNeeded(default_filter));
1266 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1267 EXPECT_NE(nullptr, oat_file.get());
1268 EXPECT_EQ(default_filter, oat_file->GetCompilerFilter());
1269}
1270
Calin Juravle27e0d1f2017-07-26 00:16:07 -07001271TEST_F(OatFileAssistantTest, MakeUpToDateWithSpecialSharedLibrary) {
1272 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1273 Copy(GetDexSrc1(), dex_location);
1274
1275 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1276
1277 const CompilerFilter::Filter default_filter =
1278 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1279 std::string error_msg;
1280 int status = oat_file_assistant.MakeUpToDate(false, kSpecialSharedLibrary, &error_msg);
1281 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, status) << error_msg;
1282 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1283 oat_file_assistant.GetDexOptNeeded(default_filter));
1284 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1285 EXPECT_NE(nullptr, oat_file.get());
1286 EXPECT_EQ(kSpecialSharedLibrary,
1287 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
1288}
1289
1290TEST_F(OatFileAssistantTest, MakeUpToDateWithContext) {
1291 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1292 std::string context_location = GetScratchDir() + "/ContextDex.jar";
1293 Copy(GetDexSrc1(), dex_location);
1294 Copy(GetDexSrc2(), context_location);
1295
1296 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1297
1298 const CompilerFilter::Filter default_filter =
1299 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1300 std::string error_msg;
1301 std::string context_str = "PCL[" + context_location + "]";
1302 int status = oat_file_assistant.MakeUpToDate(false, context_str, &error_msg);
1303 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, status) << error_msg;
1304 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1305 oat_file_assistant.GetDexOptNeeded(default_filter));
1306 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1307 EXPECT_NE(nullptr, oat_file.get());
1308 std::unique_ptr<ClassLoaderContext> context =
1309 ClassLoaderContext::Create(context_str);
1310 context->OpenDexFiles(kRuntimeISA, "");
1311 EXPECT_EQ(context->EncodeContextForOatFile(""),
1312 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
1313}
1314
Richard Uhler66d874d2015-01-15 09:37:19 -08001315// TODO: More Tests:
1316// * Test class linker falls back to unquickened dex for DexNoOat
1317// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001318// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001319// * Test for status of oat while oat is being generated (how?)
1320// * Test case where 32 and 64 bit boot class paths differ,
1321// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1322// 64 bit boot class paths.
1323// * Test unexpected scenarios (?):
1324// - Dex is stripped, don't have odex.
1325// - Oat file corrupted after status check, before reload unexecutable
1326// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001327} // namespace art