blob: 37c635377c7552e5ffc723dae75e5771d980a18b [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_assistant.h"
18
19#include <algorithm>
20#include <fstream>
21#include <string>
22#include <vector>
23#include <sys/param.h>
24
25#include <backtrace/BacktraceMap.h>
26#include <gtest/gtest.h>
27
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "class_linker.h"
30#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070031#include "compiler_callbacks.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032#include "mem_map.h"
33#include "os.h"
Richard Uhler23cedd22015-04-08 13:17:29 -070034#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "thread-inl.h"
36#include "utils.h"
37
38namespace art {
39
40class OatFileAssistantTest : public CommonRuntimeTest {
41 public:
42 virtual void SetUp() {
43 ReserveImageSpace();
44 CommonRuntimeTest::SetUp();
45
46 // Create a scratch directory to work from.
47 scratch_dir_ = android_data_ + "/OatFileAssistantTest";
48 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
49
Richard Uhler63434112015-03-16 14:32:16 -070050 // Create a subdirectory in scratch for odex files.
51 odex_oat_dir_ = scratch_dir_ + "/oat";
52 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
53
54 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
55 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
56
Richard Uhler66d874d2015-01-15 09:37:19 -080057
58 // Verify the environment is as we expect
59 uint32_t checksum;
60 std::string error_msg;
61 ASSERT_TRUE(OS::FileExists(GetImageFile().c_str()))
62 << "Expected pre-compiled boot image to be at: " << GetImageFile();
63 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
64 << "Expected dex file to be at: " << GetDexSrc1();
65 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
66 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
67 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
68 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
69 ASSERT_TRUE(OS::FileExists(GetMultiDexSrc1().c_str()))
70 << "Expected multidex file to be at: " << GetMultiDexSrc1();
71 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
72 << "Expected dex file to be at: " << GetDexSrc2();
73 }
74
75 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +000076 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -080077
78 // Set up the image location.
79 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
80 nullptr));
81 // Make sure compilercallbacks are not set so that relocation will be
82 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -070083 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -080084 }
85
86 virtual void PreRuntimeCreate() {
87 UnreserveImageSpace();
88 }
89
90 virtual void PostRuntimeCreate() {
91 ReserveImageSpace();
92 }
93
94 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -070095 ClearDirectory(odex_dir_.c_str());
96 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
97
98 ClearDirectory(odex_oat_dir_.c_str());
99 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800100
101 ClearDirectory(scratch_dir_.c_str());
102 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
103
104 CommonRuntimeTest::TearDown();
105 }
106
107 void Copy(std::string src, std::string dst) {
108 std::ifstream src_stream(src, std::ios::binary);
109 std::ofstream dst_stream(dst, std::ios::binary);
110
111 dst_stream << src_stream.rdbuf();
112 }
113
114 // Returns the directory where the pre-compiled core.art can be found.
115 // TODO: We should factor out this into common tests somewhere rather than
116 // re-hardcoding it here (This was copied originally from the elf writer
117 // test).
118 std::string GetImageDirectory() {
119 if (IsHost()) {
120 const char* host_dir = getenv("ANDROID_HOST_OUT");
121 CHECK(host_dir != NULL);
122 return std::string(host_dir) + "/framework";
123 } else {
124 return std::string("/data/art-test");
125 }
126 }
127
128 std::string GetImageLocation() {
129 return GetImageDirectory() + "/core.art";
130 }
131
132 std::string GetImageFile() {
133 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
134 + "/core.art";
135 }
136
137 std::string GetDexSrc1() {
138 return GetTestDexFileName("Main");
139 }
140
141 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
142 // file stripped.
143 std::string GetStrippedDexSrc1() {
144 return GetTestDexFileName("MainStripped");
145 }
146
147 std::string GetMultiDexSrc1() {
148 return GetTestDexFileName("MultiDex");
149 }
150
151 std::string GetDexSrc2() {
152 return GetTestDexFileName("Nested");
153 }
154
155 // Scratch directory, for dex and odex files (oat files will go in the
156 // dalvik cache).
157 std::string GetScratchDir() {
158 return scratch_dir_;
159 }
160
Richard Uhler63434112015-03-16 14:32:16 -0700161 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800162 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700163 std::string GetOdexDir() {
164 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800165 }
166
167 // Generate an odex file for the purposes of test.
168 // If pic is true, generates a PIC odex.
169 void GenerateOdexForTest(const std::string& dex_location,
170 const std::string& odex_location,
171 bool pic = false) {
172 // For this operation, we temporarily redirect the dalvik cache so dex2oat
173 // doesn't find the relocated image file.
174 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
175 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
176 std::vector<std::string> args;
177 args.push_back("--dex-file=" + dex_location);
178 args.push_back("--oat-file=" + odex_location);
179 if (pic) {
180 args.push_back("--compile-pic");
181 } else {
182 args.push_back("--include-patch-information");
Richard Uhler05dd8a62015-03-10 10:02:23 -0700183
184 // We need to use the quick compiler to generate non-PIC code, because
185 // the optimizing compiler always generates PIC.
186 args.push_back("--compiler-backend=Quick");
Richard Uhler66d874d2015-01-15 09:37:19 -0800187 }
188 args.push_back("--runtime-arg");
189 args.push_back("-Xnorelocate");
190 std::string error_msg;
191 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
192 setenv("ANDROID_DATA", android_data_.c_str(), 1);
193 }
194
195 void GeneratePicOdexForTest(const std::string& dex_location,
196 const std::string& odex_location) {
197 GenerateOdexForTest(dex_location, odex_location, true);
198 }
199
200 private:
201 // Reserve memory around where the image will be loaded so other memory
202 // won't conflict when it comes time to load the image.
203 // This can be called with an already loaded image to reserve the space
204 // around it.
205 void ReserveImageSpace() {
206 MemMap::Init();
207
208 // Ensure a chunk of memory is reserved for the image space.
209 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
210 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700211 // Include the main space that has to come right after the
212 // image in case of the GSS collector.
213 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800214
Richard Uhler66d874d2015-01-15 09:37:19 -0800215 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
216 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
217 for (BacktraceMap::const_iterator it = map->begin();
218 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700219 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
220 reservation_start = std::max(reservation_start, it->end);
221 }
222 ReserveImageSpaceChunk(reservation_start, reservation_end);
223 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800224
Richard Uhler3efe9792015-03-30 16:18:03 -0700225 // Reserve a chunk of memory for the image space in the given range.
226 // Only has effect for chunks with a positive number of bytes.
227 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
228 if (start < end) {
229 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800230 image_reservation_.push_back(std::unique_ptr<MemMap>(
231 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700232 reinterpret_cast<uint8_t*>(start), end - start,
Richard Uhler66d874d2015-01-15 09:37:19 -0800233 PROT_NONE, false, false, &error_msg)));
234 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
235 LOG(INFO) << "Reserved space for image " <<
236 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
237 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800238 }
239 }
240
241
242 // Unreserve any memory reserved by ReserveImageSpace. This should be called
243 // before the image is loaded.
244 void UnreserveImageSpace() {
245 image_reservation_.clear();
246 }
247
248 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700249 std::string odex_oat_dir_;
250 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800251 std::vector<std::unique_ptr<MemMap>> image_reservation_;
252};
253
254class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
255 public:
256 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
257 OatFileAssistantTest::SetUpRuntimeOptions(options);
258 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
259 }
260};
261
262// Generate an oat file for the purposes of test, as opposed to testing
263// generation of oat files.
264static void GenerateOatForTest(const char* dex_location) {
265 OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false);
266
267 std::string error_msg;
268 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
269}
270
271// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700272// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800273TEST_F(OatFileAssistantTest, DexNoOat) {
274 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
275 Copy(GetDexSrc1(), dex_location);
276
277 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
278
Richard Uhler95abd042015-03-24 09:51:28 -0700279 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800280
281 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
282 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
283 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
284 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
285 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700286 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800287 EXPECT_FALSE(oat_file_assistant.OatFileExists());
288 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
289 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
290 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700291 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800292}
293
294// Case: We have no DEX file and no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700295// Expect: Status is kDex2OatNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800296TEST_F(OatFileAssistantTest, NoDexNoOat) {
297 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
298
299 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
300
Richard Uhler95abd042015-03-24 09:51:28 -0700301 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
303 EXPECT_EQ(nullptr, oat_file.get());
304}
305
306// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700307// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800308TEST_F(OatFileAssistantTest, OatUpToDate) {
309 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
310 Copy(GetDexSrc1(), dex_location);
311 GenerateOatForTest(dex_location.c_str());
312
313 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
314
Richard Uhler95abd042015-03-24 09:51:28 -0700315 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800316 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
317 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
318 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
319 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
320 EXPECT_TRUE(oat_file_assistant.OatFileExists());
321 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
322 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
323 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700324 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800325}
326
327// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700328// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800329TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
330 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
331 Copy(GetMultiDexSrc1(), dex_location);
332 GenerateOatForTest(dex_location.c_str());
333
Richard Uhlere5fed032015-03-18 08:21:11 -0700334 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700335 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
336
337 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700338 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800339 ASSERT_TRUE(oat_file.get() != nullptr);
340 EXPECT_TRUE(oat_file->IsExecutable());
341 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700342 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
343 EXPECT_EQ(2u, dex_files.size());
344}
345
346// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
347// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700348// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700349TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
350 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700351 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700352
353 // Create the dex file
354 Copy(GetMultiDexSrc1(), dex_location);
355
356 // Create the oat file with relative encoded dex location.
357 std::vector<std::string> args;
358 args.push_back("--dex-file=" + dex_location);
359 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
360 args.push_back("--oat-file=" + oat_location);
361
362 std::string error_msg;
363 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
364
365 // Verify we can load both dex files.
366 OatFileAssistant oat_file_assistant(dex_location.c_str(),
367 oat_location.c_str(),
368 kRuntimeISA, true);
369 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
370 ASSERT_TRUE(oat_file.get() != nullptr);
371 EXPECT_TRUE(oat_file->IsExecutable());
372 std::vector<std::unique_ptr<const DexFile>> dex_files;
373 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 EXPECT_EQ(2u, dex_files.size());
375}
376
Richard Uhler95abd042015-03-24 09:51:28 -0700377// Case: We have a DEX file and out-of-date OAT file.
378// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800379TEST_F(OatFileAssistantTest, OatOutOfDate) {
380 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
381
382 // We create a dex, generate an oat for it, then overwrite the dex with a
383 // different dex to make the oat out of date.
384 Copy(GetDexSrc1(), dex_location);
385 GenerateOatForTest(dex_location.c_str());
386 Copy(GetDexSrc2(), dex_location);
387
388 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700389 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800390
391 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
392 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
393 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
394 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
395 EXPECT_TRUE(oat_file_assistant.OatFileExists());
396 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
397 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
398}
399
400// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700401// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800402TEST_F(OatFileAssistantTest, DexOdexNoOat) {
403 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700404 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800405
406 // Create the dex and odex files
407 Copy(GetDexSrc1(), dex_location);
408 GenerateOdexForTest(dex_location, odex_location);
409
410 // Verify the status.
411 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
412
Richard Uhler95abd042015-03-24 09:51:28 -0700413 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800414
415 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
416 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
417 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
418 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
419 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800420 EXPECT_FALSE(oat_file_assistant.OatFileExists());
421 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
422 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
423}
424
425// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700426// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800427TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
428 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700429 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800430
431 // Create the dex and odex files
432 Copy(GetDexSrc1(), dex_location);
433 GenerateOdexForTest(dex_location, odex_location);
434
435 // Strip the dex file
436 Copy(GetStrippedDexSrc1(), dex_location);
437
438 // Verify the status.
439 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
440
Richard Uhler95abd042015-03-24 09:51:28 -0700441 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800442
443 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
444 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
445 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
446 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
447 EXPECT_FALSE(oat_file_assistant.OatFileExists());
448 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
449 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
450
451 // Make the oat file up to date.
452 std::string error_msg;
453 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
454
Richard Uhler95abd042015-03-24 09:51:28 -0700455 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800456
457 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
458 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
459 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
460 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
461 EXPECT_TRUE(oat_file_assistant.OatFileExists());
462 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
463 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
464
465 // Verify we can load the dex files from it.
466 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
467 ASSERT_TRUE(oat_file.get() != nullptr);
468 EXPECT_TRUE(oat_file->IsExecutable());
469 std::vector<std::unique_ptr<const DexFile>> dex_files;
470 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
471 EXPECT_EQ(1u, dex_files.size());
472}
473
Richard Uhler95abd042015-03-24 09:51:28 -0700474// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
475// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800476TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
477 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700478 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800479
480 // Create the oat file from a different dex file so it looks out of date.
481 Copy(GetDexSrc2(), dex_location);
482 GenerateOatForTest(dex_location.c_str());
483
484 // Create the odex file
485 Copy(GetDexSrc1(), dex_location);
486 GenerateOdexForTest(dex_location, odex_location);
487
488 // Strip the dex file.
489 Copy(GetStrippedDexSrc1(), dex_location);
490
491 // Verify the status.
492 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
493
Richard Uhler95abd042015-03-24 09:51:28 -0700494 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800495
496 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
497 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
498 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
499 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
500 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
501 EXPECT_TRUE(oat_file_assistant.OatFileExists());
502 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
503 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
504
505 // Make the oat file up to date.
506 std::string error_msg;
507 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
508
Richard Uhler95abd042015-03-24 09:51:28 -0700509 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800510
511 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
512 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
513 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
514 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
515 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
516 EXPECT_TRUE(oat_file_assistant.OatFileExists());
517 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
518 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
519 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
520
521 // Verify we can load the dex files from it.
522 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
523 ASSERT_TRUE(oat_file.get() != nullptr);
524 EXPECT_TRUE(oat_file->IsExecutable());
525 std::vector<std::unique_ptr<const DexFile>> dex_files;
526 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
527 EXPECT_EQ(1u, dex_files.size());
528}
529
Richard Uhler95abd042015-03-24 09:51:28 -0700530// Case: We have a DEX file, no ODEX file and an OAT file that needs
531// relocation.
532// Expect: The status is kSelfPatchOatNeeded.
533TEST_F(OatFileAssistantTest, SelfRelocation) {
534 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
535 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
536
537 // Create the dex and odex files
538 Copy(GetDexSrc1(), dex_location);
539 GenerateOdexForTest(dex_location, oat_location);
540
541 OatFileAssistant oat_file_assistant(dex_location.c_str(),
542 oat_location.c_str(), kRuntimeISA, true);
543
544 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
545
546 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
547 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
548 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
549 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
550 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
551 EXPECT_TRUE(oat_file_assistant.OatFileExists());
552 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
553 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
554 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
555
556 // Make the oat file up to date.
557 std::string error_msg;
558 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
559
560 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
561
562 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
563 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
564 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
565 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
566 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
567 EXPECT_TRUE(oat_file_assistant.OatFileExists());
568 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
569 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
570 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
571
572 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
573 ASSERT_TRUE(oat_file.get() != nullptr);
574 EXPECT_TRUE(oat_file->IsExecutable());
575 std::vector<std::unique_ptr<const DexFile>> dex_files;
576 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
577 EXPECT_EQ(1u, dex_files.size());
578}
579
Richard Uhler66d874d2015-01-15 09:37:19 -0800580// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
581// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700582// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800583TEST_F(OatFileAssistantTest, OdexOatOverlap) {
584 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700585 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
586 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800587
588 // Create the dex and odex files
589 Copy(GetDexSrc1(), dex_location);
590 GenerateOdexForTest(dex_location, odex_location);
591
592 // Create the oat file by copying the odex so they are located in the same
593 // place in memory.
594 Copy(odex_location, oat_location);
595
596 // Verify things don't go bad.
597 OatFileAssistant oat_file_assistant(dex_location.c_str(),
598 oat_location.c_str(), kRuntimeISA, true);
599
Richard Uhler95abd042015-03-24 09:51:28 -0700600 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800601
602 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
603 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
604 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
605 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
606 EXPECT_TRUE(oat_file_assistant.OatFileExists());
607 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
608 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
609
610 // Things aren't relocated, so it should fall back to interpreted.
611 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
612 ASSERT_TRUE(oat_file.get() != nullptr);
613 EXPECT_FALSE(oat_file->IsExecutable());
614 std::vector<std::unique_ptr<const DexFile>> dex_files;
615 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
616 EXPECT_EQ(1u, dex_files.size());
617}
618
619// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700620// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800621TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
622 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700623 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800624
625 // Create the dex and odex files
626 Copy(GetDexSrc1(), dex_location);
627 GeneratePicOdexForTest(dex_location, odex_location);
628
629 // Verify the status.
630 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
631
Richard Uhler95abd042015-03-24 09:51:28 -0700632 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800633
634 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
635 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
636 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
637 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
638 EXPECT_FALSE(oat_file_assistant.OatFileExists());
639 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
640 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
641}
642
643// Case: We have a DEX file and up-to-date OAT file for it.
644// Expect: We should load an executable dex file.
645TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
646 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
647
648 Copy(GetDexSrc1(), dex_location);
649 GenerateOatForTest(dex_location.c_str());
650
651 // Load the oat using an oat file assistant.
652 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
653
654 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
655 ASSERT_TRUE(oat_file.get() != nullptr);
656 EXPECT_TRUE(oat_file->IsExecutable());
657 std::vector<std::unique_ptr<const DexFile>> dex_files;
658 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
659 EXPECT_EQ(1u, dex_files.size());
660}
661
662// Case: We have a DEX file and up-to-date OAT file for it.
663// Expect: Loading non-executable should load the oat non-executable.
664TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
665 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
666
667 Copy(GetDexSrc1(), dex_location);
668 GenerateOatForTest(dex_location.c_str());
669
670 // Load the oat using an oat file assistant.
671 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
672
673 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
674 ASSERT_TRUE(oat_file.get() != nullptr);
675 EXPECT_FALSE(oat_file->IsExecutable());
676 std::vector<std::unique_ptr<const DexFile>> dex_files;
677 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
678 EXPECT_EQ(1u, dex_files.size());
679}
680
681// Case: We have a DEX file.
682// Expect: We should load an executable dex file from an alternative oat
683// location.
684TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
685 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
686 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
687
688 Copy(GetDexSrc1(), dex_location);
689
690 OatFileAssistant oat_file_assistant(
691 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
692 std::string error_msg;
693 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
694
695 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
696 ASSERT_TRUE(oat_file.get() != nullptr);
697 EXPECT_TRUE(oat_file->IsExecutable());
698 std::vector<std::unique_ptr<const DexFile>> dex_files;
699 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
700 EXPECT_EQ(1u, dex_files.size());
701
702 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
703
704 // Verify it didn't create an oat in the default location.
705 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
706 EXPECT_FALSE(ofm.OatFileExists());
707}
708
709// Case: Non-existent Dex location.
710// Expect: The dex code is out of date, and trying to update it fails.
711TEST_F(OatFileAssistantTest, NonExsistentDexLocation) {
712 std::string dex_location = GetScratchDir() + "/BadDexLocation.jar";
713
714 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
715
716 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700717 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800718 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
719 EXPECT_FALSE(oat_file_assistant.OatFileExists());
720 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
721 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
722 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
723 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
724
725 std::string error_msg;
726 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
727 EXPECT_FALSE(error_msg.empty());
728}
729
730// Turn an absolute path into a path relative to the current working
731// directory.
732static std::string MakePathRelative(std::string target) {
733 char buf[MAXPATHLEN];
734 std::string cwd = getcwd(buf, MAXPATHLEN);
735
736 // Split the target and cwd paths into components.
737 std::vector<std::string> target_path;
738 std::vector<std::string> cwd_path;
739 Split(target, '/', &target_path);
740 Split(cwd, '/', &cwd_path);
741
742 // Reverse the path components, so we can use pop_back().
743 std::reverse(target_path.begin(), target_path.end());
744 std::reverse(cwd_path.begin(), cwd_path.end());
745
746 // Drop the common prefix of the paths. Because we reversed the path
747 // components, this becomes the common suffix of target_path and cwd_path.
748 while (!target_path.empty() && !cwd_path.empty()
749 && target_path.back() == cwd_path.back()) {
750 target_path.pop_back();
751 cwd_path.pop_back();
752 }
753
754 // For each element of the remaining cwd_path, add '..' to the beginning
755 // of the target path. Because we reversed the path components, we add to
756 // the end of target_path.
757 for (unsigned int i = 0; i < cwd_path.size(); i++) {
758 target_path.push_back("..");
759 }
760
761 // Reverse again to get the right path order, and join to get the result.
762 std::reverse(target_path.begin(), target_path.end());
763 return Join(target_path, '/');
764}
765
766// Case: Non-absolute path to Dex location.
767// Expect: Not sure, but it shouldn't crash.
768TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
769 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
770 Copy(GetDexSrc1(), abs_dex_location);
771
772 std::string dex_location = MakePathRelative(abs_dex_location);
773 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
774
775 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700776 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800777 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
778 EXPECT_FALSE(oat_file_assistant.OatFileExists());
779 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
780 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
781 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
782 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
783}
784
785// Case: Very short, non-existent Dex location.
786// Expect: Dex code is out of date, and trying to update it fails.
787TEST_F(OatFileAssistantTest, ShortDexLocation) {
788 std::string dex_location = "/xx";
789
790 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
791
792 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700793 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800794 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
795 EXPECT_FALSE(oat_file_assistant.OatFileExists());
796 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
797 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
798 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
799 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
800
801 std::string error_msg;
802 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
803 EXPECT_FALSE(error_msg.empty());
804}
805
806// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700807// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800808TEST_F(OatFileAssistantTest, LongDexExtension) {
809 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
810 Copy(GetDexSrc1(), dex_location);
811
812 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
813
Richard Uhler95abd042015-03-24 09:51:28 -0700814 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800815
816 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
817 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
818 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
819 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
820 EXPECT_FALSE(oat_file_assistant.OatFileExists());
821 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
822 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
823}
824
825// A task to generate a dex location. Used by the RaceToGenerate test.
826class RaceGenerateTask : public Task {
827 public:
828 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
829 : dex_location_(dex_location), oat_location_(oat_location),
830 loaded_oat_file_(nullptr)
831 {}
832
833 void Run(Thread* self) {
834 UNUSED(self);
835
836 // Load the dex files, and save a pointer to the loaded oat file, so that
837 // we can verify only one oat file was loaded for the dex location.
838 ClassLinker* linker = Runtime::Current()->GetClassLinker();
839 std::vector<std::unique_ptr<const DexFile>> dex_files;
840 std::vector<std::string> error_msgs;
841 dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs);
842 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700843 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
844 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800845 }
846
847 const OatFile* GetLoadedOatFile() const {
848 return loaded_oat_file_;
849 }
850
851 private:
852 std::string dex_location_;
853 std::string oat_location_;
854 const OatFile* loaded_oat_file_;
855};
856
857// Test the case where multiple processes race to generate an oat file.
858// This simulates multiple processes using multiple threads.
859//
860// We want only one Oat file to be loaded when there is a race to load, to
861// avoid using up the virtual memory address space.
862TEST_F(OatFileAssistantTest, RaceToGenerate) {
863 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700864 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800865
866 // We use the lib core dex file, because it's large, and hopefully should
867 // take a while to generate.
868 Copy(GetLibCoreDexFileName(), dex_location);
869
870 const int kNumThreads = 32;
871 Thread* self = Thread::Current();
872 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
873 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
874 for (int i = 0; i < kNumThreads; i++) {
875 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
876 thread_pool.AddTask(self, task.get());
877 tasks.push_back(std::move(task));
878 }
879 thread_pool.StartWorkers(self);
880 thread_pool.Wait(self, true, false);
881
882 // Verify every task got the same pointer.
883 const OatFile* expected = tasks[0]->GetLoadedOatFile();
884 for (auto& task : tasks) {
885 EXPECT_EQ(expected, task->GetLoadedOatFile());
886 }
887}
888
889// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
890// disabled.
891// Expect: We should load the odex file non-executable.
892TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
893 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700894 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800895
896 // Create the dex and odex files
897 Copy(GetDexSrc1(), dex_location);
898 GenerateOdexForTest(dex_location, odex_location);
899
900 // Load the oat using an executable oat file assistant.
901 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
902
903 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
904 ASSERT_TRUE(oat_file.get() != nullptr);
905 EXPECT_FALSE(oat_file->IsExecutable());
906 std::vector<std::unique_ptr<const DexFile>> dex_files;
907 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
908 EXPECT_EQ(1u, dex_files.size());
909}
910
911// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
912// disabled.
913// Expect: We should load the odex file non-executable.
914TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
915 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700916 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800917
918 // Create the dex and odex files
919 Copy(GetMultiDexSrc1(), dex_location);
920 GenerateOdexForTest(dex_location, odex_location);
921
922 // Load the oat using an executable oat file assistant.
923 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
924
925 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
926 ASSERT_TRUE(oat_file.get() != nullptr);
927 EXPECT_FALSE(oat_file->IsExecutable());
928 std::vector<std::unique_ptr<const DexFile>> dex_files;
929 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
930 EXPECT_EQ(2u, dex_files.size());
931}
932
933TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
934 std::string error_msg;
935 std::string odex_file;
936
937 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
938 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700939 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800940
941 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
942 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700943 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800944
945 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
946 "nopath.jar", kArm, &odex_file, &error_msg));
947 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
948 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
949}
950
Richard Uhler23cedd22015-04-08 13:17:29 -0700951// Verify the dexopt status values from dalvik.system.DexFile
952// match the OatFileAssistant::DexOptStatus values.
953TEST_F(OatFileAssistantTest, DexOptStatusValues) {
954 ScopedObjectAccess soa(Thread::Current());
955 StackHandleScope<1> hs(soa.Self());
956 ClassLinker* linker = Runtime::Current()->GetClassLinker();
957 Handle<mirror::Class> dexfile(
958 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
959 ASSERT_FALSE(dexfile.Get() == nullptr);
960 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
961
Mathieu Chartierc7853442015-03-27 14:35:38 -0700962 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700963 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
964 ASSERT_FALSE(no_dexopt_needed == nullptr);
965 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
966 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
967
Mathieu Chartierc7853442015-03-27 14:35:38 -0700968 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700969 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
970 ASSERT_FALSE(dex2oat_needed == nullptr);
971 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
972 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
973
Mathieu Chartierc7853442015-03-27 14:35:38 -0700974 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700975 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
976 ASSERT_FALSE(patchoat_needed == nullptr);
977 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
978 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
979
Mathieu Chartierc7853442015-03-27 14:35:38 -0700980 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700981 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
982 ASSERT_FALSE(self_patchoat_needed == nullptr);
983 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
984 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
985}
Richard Uhler66d874d2015-01-15 09:37:19 -0800986
987// TODO: More Tests:
988// * Test class linker falls back to unquickened dex for DexNoOat
989// * Test class linker falls back to unquickened dex for MultiDexNoOat
990// * Test multidex files:
991// - Multidex with only classes2.dex out of date should have status
992// kOutOfDate
993// * Test using secondary isa
994// * Test with profiling info?
995// * Test for status of oat while oat is being generated (how?)
996// * Test case where 32 and 64 bit boot class paths differ,
997// and we ask IsInBootClassPath for a class in exactly one of the 32 or
998// 64 bit boot class paths.
999// * Test unexpected scenarios (?):
1000// - Dex is stripped, don't have odex.
1001// - Oat file corrupted after status check, before reload unexecutable
1002// because it's unrelocated and no dex2oat
1003
1004} // namespace art