blob: 865fcb063adc1e5a820dbea3d1f980ea6b49087a [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"
Vladimir Marko3481ba22015-04-13 12:22:36 +010029#include "class_linker-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070031#include "compiler_callbacks.h"
Richard Uhlerf16d5722015-05-11 09:32:47 -070032#include "gc/space/image_space.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "mem_map.h"
34#include "os.h"
Richard Uhler23cedd22015-04-08 13:17:29 -070035#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "thread-inl.h"
37#include "utils.h"
38
39namespace art {
40
41class OatFileAssistantTest : public CommonRuntimeTest {
42 public:
43 virtual void SetUp() {
44 ReserveImageSpace();
45 CommonRuntimeTest::SetUp();
46
47 // Create a scratch directory to work from.
48 scratch_dir_ = android_data_ + "/OatFileAssistantTest";
49 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
50
Richard Uhler63434112015-03-16 14:32:16 -070051 // Create a subdirectory in scratch for odex files.
52 odex_oat_dir_ = scratch_dir_ + "/oat";
53 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
54
55 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
56 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
57
Richard Uhler66d874d2015-01-15 09:37:19 -080058
59 // Verify the environment is as we expect
60 uint32_t checksum;
61 std::string error_msg;
62 ASSERT_TRUE(OS::FileExists(GetImageFile().c_str()))
63 << "Expected pre-compiled boot image to be at: " << GetImageFile();
64 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
65 << "Expected dex file to be at: " << GetDexSrc1();
66 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
67 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
68 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
69 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
70 ASSERT_TRUE(OS::FileExists(GetMultiDexSrc1().c_str()))
71 << "Expected multidex file to be at: " << GetMultiDexSrc1();
72 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
73 << "Expected dex file to be at: " << GetDexSrc2();
74 }
75
76 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +000077 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -080078
79 // Set up the image location.
80 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
81 nullptr));
82 // Make sure compilercallbacks are not set so that relocation will be
83 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -070084 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -080085 }
86
87 virtual void PreRuntimeCreate() {
88 UnreserveImageSpace();
89 }
90
91 virtual void PostRuntimeCreate() {
92 ReserveImageSpace();
93 }
94
95 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -070096 ClearDirectory(odex_dir_.c_str());
97 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
98
99 ClearDirectory(odex_oat_dir_.c_str());
100 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800101
102 ClearDirectory(scratch_dir_.c_str());
103 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
104
105 CommonRuntimeTest::TearDown();
106 }
107
108 void Copy(std::string src, std::string dst) {
109 std::ifstream src_stream(src, std::ios::binary);
110 std::ofstream dst_stream(dst, std::ios::binary);
111
112 dst_stream << src_stream.rdbuf();
113 }
114
115 // Returns the directory where the pre-compiled core.art can be found.
116 // TODO: We should factor out this into common tests somewhere rather than
117 // re-hardcoding it here (This was copied originally from the elf writer
118 // test).
119 std::string GetImageDirectory() {
120 if (IsHost()) {
121 const char* host_dir = getenv("ANDROID_HOST_OUT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 CHECK(host_dir != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800123 return std::string(host_dir) + "/framework";
124 } else {
125 return std::string("/data/art-test");
126 }
127 }
128
129 std::string GetImageLocation() {
130 return GetImageDirectory() + "/core.art";
131 }
132
133 std::string GetImageFile() {
134 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
135 + "/core.art";
136 }
137
138 std::string GetDexSrc1() {
139 return GetTestDexFileName("Main");
140 }
141
142 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
143 // file stripped.
144 std::string GetStrippedDexSrc1() {
145 return GetTestDexFileName("MainStripped");
146 }
147
148 std::string GetMultiDexSrc1() {
149 return GetTestDexFileName("MultiDex");
150 }
151
152 std::string GetDexSrc2() {
153 return GetTestDexFileName("Nested");
154 }
155
156 // Scratch directory, for dex and odex files (oat files will go in the
157 // dalvik cache).
158 std::string GetScratchDir() {
159 return scratch_dir_;
160 }
161
Richard Uhler63434112015-03-16 14:32:16 -0700162 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800163 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700164 std::string GetOdexDir() {
165 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800166 }
167
168 // Generate an odex file for the purposes of test.
169 // If pic is true, generates a PIC odex.
170 void GenerateOdexForTest(const std::string& dex_location,
171 const std::string& odex_location,
172 bool pic = false) {
173 // For this operation, we temporarily redirect the dalvik cache so dex2oat
174 // doesn't find the relocated image file.
175 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
176 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
177 std::vector<std::string> args;
178 args.push_back("--dex-file=" + dex_location);
179 args.push_back("--oat-file=" + odex_location);
180 if (pic) {
181 args.push_back("--compile-pic");
182 } else {
183 args.push_back("--include-patch-information");
Richard Uhler05dd8a62015-03-10 10:02:23 -0700184
185 // We need to use the quick compiler to generate non-PIC code, because
186 // the optimizing compiler always generates PIC.
187 args.push_back("--compiler-backend=Quick");
Richard Uhler66d874d2015-01-15 09:37:19 -0800188 }
189 args.push_back("--runtime-arg");
190 args.push_back("-Xnorelocate");
191 std::string error_msg;
192 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
193 setenv("ANDROID_DATA", android_data_.c_str(), 1);
194 }
195
196 void GeneratePicOdexForTest(const std::string& dex_location,
197 const std::string& odex_location) {
198 GenerateOdexForTest(dex_location, odex_location, true);
199 }
200
201 private:
202 // Reserve memory around where the image will be loaded so other memory
203 // won't conflict when it comes time to load the image.
204 // This can be called with an already loaded image to reserve the space
205 // around it.
206 void ReserveImageSpace() {
207 MemMap::Init();
208
209 // Ensure a chunk of memory is reserved for the image space.
210 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
211 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700212 // Include the main space that has to come right after the
213 // image in case of the GSS collector.
214 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800215
Richard Uhler66d874d2015-01-15 09:37:19 -0800216 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
217 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
218 for (BacktraceMap::const_iterator it = map->begin();
219 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700220 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
221 reservation_start = std::max(reservation_start, it->end);
222 }
223 ReserveImageSpaceChunk(reservation_start, reservation_end);
224 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800225
Richard Uhler3efe9792015-03-30 16:18:03 -0700226 // Reserve a chunk of memory for the image space in the given range.
227 // Only has effect for chunks with a positive number of bytes.
228 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
229 if (start < end) {
230 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800231 image_reservation_.push_back(std::unique_ptr<MemMap>(
232 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700233 reinterpret_cast<uint8_t*>(start), end - start,
Richard Uhler66d874d2015-01-15 09:37:19 -0800234 PROT_NONE, false, false, &error_msg)));
235 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
236 LOG(INFO) << "Reserved space for image " <<
237 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
238 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800239 }
240 }
241
242
243 // Unreserve any memory reserved by ReserveImageSpace. This should be called
244 // before the image is loaded.
245 void UnreserveImageSpace() {
246 image_reservation_.clear();
247 }
248
249 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700250 std::string odex_oat_dir_;
251 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800252 std::vector<std::unique_ptr<MemMap>> image_reservation_;
253};
254
255class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
256 public:
257 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
258 OatFileAssistantTest::SetUpRuntimeOptions(options);
259 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
260 }
261};
262
263// Generate an oat file for the purposes of test, as opposed to testing
264// generation of oat files.
265static void GenerateOatForTest(const char* dex_location) {
266 OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false);
267
268 std::string error_msg;
269 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
270}
271
272// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700273// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800274TEST_F(OatFileAssistantTest, DexNoOat) {
275 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
276 Copy(GetDexSrc1(), dex_location);
277
278 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
279
Richard Uhler95abd042015-03-24 09:51:28 -0700280 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800281
282 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
283 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
284 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
285 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
286 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700287 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800288 EXPECT_FALSE(oat_file_assistant.OatFileExists());
289 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
290 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
291 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700292 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800293}
294
295// Case: We have no DEX file and no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700296// Expect: Status is kDex2OatNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800297TEST_F(OatFileAssistantTest, NoDexNoOat) {
298 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
299
300 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
301
Richard Uhler95abd042015-03-24 09:51:28 -0700302 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
304 EXPECT_EQ(nullptr, oat_file.get());
305}
306
307// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700308// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800309TEST_F(OatFileAssistantTest, OatUpToDate) {
310 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
311 Copy(GetDexSrc1(), dex_location);
312 GenerateOatForTest(dex_location.c_str());
313
314 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
315
Richard Uhler95abd042015-03-24 09:51:28 -0700316 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800317 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
318 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
319 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
320 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
321 EXPECT_TRUE(oat_file_assistant.OatFileExists());
322 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
323 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
324 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700325 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800326}
327
328// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700329// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800330TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
331 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
332 Copy(GetMultiDexSrc1(), dex_location);
333 GenerateOatForTest(dex_location.c_str());
334
Richard Uhlere5fed032015-03-18 08:21:11 -0700335 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700336 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
337
338 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700339 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800340 ASSERT_TRUE(oat_file.get() != nullptr);
341 EXPECT_TRUE(oat_file->IsExecutable());
342 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700343 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
344 EXPECT_EQ(2u, dex_files.size());
345}
346
347// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
348// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700349// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700350TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
351 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700352 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700353
354 // Create the dex file
355 Copy(GetMultiDexSrc1(), dex_location);
356
357 // Create the oat file with relative encoded dex location.
358 std::vector<std::string> args;
359 args.push_back("--dex-file=" + dex_location);
360 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
361 args.push_back("--oat-file=" + oat_location);
362
363 std::string error_msg;
364 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
365
366 // Verify we can load both dex files.
367 OatFileAssistant oat_file_assistant(dex_location.c_str(),
368 oat_location.c_str(),
369 kRuntimeISA, true);
370 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
371 ASSERT_TRUE(oat_file.get() != nullptr);
372 EXPECT_TRUE(oat_file->IsExecutable());
373 std::vector<std::unique_ptr<const DexFile>> dex_files;
374 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800375 EXPECT_EQ(2u, dex_files.size());
376}
377
Richard Uhler95abd042015-03-24 09:51:28 -0700378// Case: We have a DEX file and out-of-date OAT file.
379// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800380TEST_F(OatFileAssistantTest, OatOutOfDate) {
381 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
382
383 // We create a dex, generate an oat for it, then overwrite the dex with a
384 // different dex to make the oat out of date.
385 Copy(GetDexSrc1(), dex_location);
386 GenerateOatForTest(dex_location.c_str());
387 Copy(GetDexSrc2(), dex_location);
388
389 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700390 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800391
392 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
393 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
394 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
395 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
396 EXPECT_TRUE(oat_file_assistant.OatFileExists());
397 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
398 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
399}
400
401// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700402// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800403TEST_F(OatFileAssistantTest, DexOdexNoOat) {
404 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700405 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800406
407 // Create the dex and odex files
408 Copy(GetDexSrc1(), dex_location);
409 GenerateOdexForTest(dex_location, odex_location);
410
411 // Verify the status.
412 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
413
Richard Uhler95abd042015-03-24 09:51:28 -0700414 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800415
416 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
417 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
418 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
419 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
420 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800421 EXPECT_FALSE(oat_file_assistant.OatFileExists());
422 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
423 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
424}
425
426// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700427// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800428TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
429 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700430 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800431
432 // Create the dex and odex files
433 Copy(GetDexSrc1(), dex_location);
434 GenerateOdexForTest(dex_location, odex_location);
435
436 // Strip the dex file
437 Copy(GetStrippedDexSrc1(), dex_location);
438
439 // Verify the status.
440 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
441
Richard Uhler95abd042015-03-24 09:51:28 -0700442 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800443
444 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
445 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
446 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
447 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
448 EXPECT_FALSE(oat_file_assistant.OatFileExists());
449 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
450 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
451
452 // Make the oat file up to date.
453 std::string error_msg;
454 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
455
Richard Uhler95abd042015-03-24 09:51:28 -0700456 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800457
458 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
459 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
460 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
461 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
462 EXPECT_TRUE(oat_file_assistant.OatFileExists());
463 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
464 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
465
466 // Verify we can load the dex files from it.
467 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
468 ASSERT_TRUE(oat_file.get() != nullptr);
469 EXPECT_TRUE(oat_file->IsExecutable());
470 std::vector<std::unique_ptr<const DexFile>> dex_files;
471 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
472 EXPECT_EQ(1u, dex_files.size());
473}
474
Richard Uhler95abd042015-03-24 09:51:28 -0700475// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
476// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800477TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
478 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700479 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800480
481 // Create the oat file from a different dex file so it looks out of date.
482 Copy(GetDexSrc2(), dex_location);
483 GenerateOatForTest(dex_location.c_str());
484
485 // Create the odex file
486 Copy(GetDexSrc1(), dex_location);
487 GenerateOdexForTest(dex_location, odex_location);
488
489 // Strip the dex file.
490 Copy(GetStrippedDexSrc1(), dex_location);
491
492 // Verify the status.
493 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
494
Richard Uhler95abd042015-03-24 09:51:28 -0700495 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800496
497 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
498 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
499 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
500 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
501 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
502 EXPECT_TRUE(oat_file_assistant.OatFileExists());
503 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
504 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
505
506 // Make the oat file up to date.
507 std::string error_msg;
508 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
509
Richard Uhler95abd042015-03-24 09:51:28 -0700510 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800511
512 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
513 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
514 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
515 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
516 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
517 EXPECT_TRUE(oat_file_assistant.OatFileExists());
518 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
519 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
520 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
521
522 // Verify we can load the dex files from it.
523 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
524 ASSERT_TRUE(oat_file.get() != nullptr);
525 EXPECT_TRUE(oat_file->IsExecutable());
526 std::vector<std::unique_ptr<const DexFile>> dex_files;
527 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
528 EXPECT_EQ(1u, dex_files.size());
529}
530
Richard Uhler95abd042015-03-24 09:51:28 -0700531// Case: We have a DEX file, no ODEX file and an OAT file that needs
532// relocation.
533// Expect: The status is kSelfPatchOatNeeded.
534TEST_F(OatFileAssistantTest, SelfRelocation) {
535 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
536 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
537
538 // Create the dex and odex files
539 Copy(GetDexSrc1(), dex_location);
540 GenerateOdexForTest(dex_location, oat_location);
541
542 OatFileAssistant oat_file_assistant(dex_location.c_str(),
543 oat_location.c_str(), kRuntimeISA, true);
544
545 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
546
547 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
548 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
549 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
550 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
551 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
552 EXPECT_TRUE(oat_file_assistant.OatFileExists());
553 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
554 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
555 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
556
557 // Make the oat file up to date.
558 std::string error_msg;
559 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
560
561 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
562
563 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
564 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
565 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
566 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
567 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
568 EXPECT_TRUE(oat_file_assistant.OatFileExists());
569 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
570 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
571 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
572
573 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
574 ASSERT_TRUE(oat_file.get() != nullptr);
575 EXPECT_TRUE(oat_file->IsExecutable());
576 std::vector<std::unique_ptr<const DexFile>> dex_files;
577 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
578 EXPECT_EQ(1u, dex_files.size());
579}
580
Richard Uhler66d874d2015-01-15 09:37:19 -0800581// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
582// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700583// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800584TEST_F(OatFileAssistantTest, OdexOatOverlap) {
585 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700586 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
587 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800588
589 // Create the dex and odex files
590 Copy(GetDexSrc1(), dex_location);
591 GenerateOdexForTest(dex_location, odex_location);
592
593 // Create the oat file by copying the odex so they are located in the same
594 // place in memory.
595 Copy(odex_location, oat_location);
596
597 // Verify things don't go bad.
598 OatFileAssistant oat_file_assistant(dex_location.c_str(),
599 oat_location.c_str(), kRuntimeISA, true);
600
Richard Uhler95abd042015-03-24 09:51:28 -0700601 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800602
603 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
604 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
605 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
606 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
607 EXPECT_TRUE(oat_file_assistant.OatFileExists());
608 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
609 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
610
611 // Things aren't relocated, so it should fall back to interpreted.
612 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
613 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700614
Richard Uhler66d874d2015-01-15 09:37:19 -0800615 EXPECT_FALSE(oat_file->IsExecutable());
616 std::vector<std::unique_ptr<const DexFile>> dex_files;
617 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
618 EXPECT_EQ(1u, dex_files.size());
Richard Uhlerf16d5722015-05-11 09:32:47 -0700619
620 // Add some extra checks to help diagnose apparently flaky test failures.
621 Runtime* runtime = Runtime::Current();
622 const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace();
623 ASSERT_TRUE(image_space != nullptr);
624 const ImageHeader& image_header = image_space->GetImageHeader();
625 const OatHeader& oat_header = oat_file->GetOatHeader();
626 EXPECT_FALSE(oat_file->IsPic());
627 EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum());
628 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
629 oat_header.GetImageFileLocationOatDataBegin());
630 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
Richard Uhler66d874d2015-01-15 09:37:19 -0800631}
632
633// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700634// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800635TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
636 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700637 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800638
639 // Create the dex and odex files
640 Copy(GetDexSrc1(), dex_location);
641 GeneratePicOdexForTest(dex_location, odex_location);
642
643 // Verify the status.
644 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
645
Richard Uhler95abd042015-03-24 09:51:28 -0700646 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800647
648 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
649 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
650 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
651 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
652 EXPECT_FALSE(oat_file_assistant.OatFileExists());
653 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
654 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
655}
656
657// Case: We have a DEX file and up-to-date OAT file for it.
658// Expect: We should load an executable dex file.
659TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
660 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
661
662 Copy(GetDexSrc1(), dex_location);
663 GenerateOatForTest(dex_location.c_str());
664
665 // Load the oat using an oat file assistant.
666 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
667
668 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
669 ASSERT_TRUE(oat_file.get() != nullptr);
670 EXPECT_TRUE(oat_file->IsExecutable());
671 std::vector<std::unique_ptr<const DexFile>> dex_files;
672 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
673 EXPECT_EQ(1u, dex_files.size());
674}
675
676// Case: We have a DEX file and up-to-date OAT file for it.
677// Expect: Loading non-executable should load the oat non-executable.
678TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
679 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
680
681 Copy(GetDexSrc1(), dex_location);
682 GenerateOatForTest(dex_location.c_str());
683
684 // Load the oat using an oat file assistant.
685 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
686
687 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
688 ASSERT_TRUE(oat_file.get() != nullptr);
689 EXPECT_FALSE(oat_file->IsExecutable());
690 std::vector<std::unique_ptr<const DexFile>> dex_files;
691 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
692 EXPECT_EQ(1u, dex_files.size());
693}
694
695// Case: We have a DEX file.
696// Expect: We should load an executable dex file from an alternative oat
697// location.
698TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
699 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
700 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
701
702 Copy(GetDexSrc1(), dex_location);
703
704 OatFileAssistant oat_file_assistant(
705 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
706 std::string error_msg;
707 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
708
709 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
710 ASSERT_TRUE(oat_file.get() != nullptr);
711 EXPECT_TRUE(oat_file->IsExecutable());
712 std::vector<std::unique_ptr<const DexFile>> dex_files;
713 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
714 EXPECT_EQ(1u, dex_files.size());
715
716 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
717
718 // Verify it didn't create an oat in the default location.
719 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
720 EXPECT_FALSE(ofm.OatFileExists());
721}
722
723// Case: Non-existent Dex location.
724// Expect: The dex code is out of date, and trying to update it fails.
725TEST_F(OatFileAssistantTest, NonExsistentDexLocation) {
726 std::string dex_location = GetScratchDir() + "/BadDexLocation.jar";
727
728 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
729
730 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700731 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800732 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
733 EXPECT_FALSE(oat_file_assistant.OatFileExists());
734 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
735 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
736 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
737 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
738
739 std::string error_msg;
740 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
741 EXPECT_FALSE(error_msg.empty());
742}
743
744// Turn an absolute path into a path relative to the current working
745// directory.
746static std::string MakePathRelative(std::string target) {
747 char buf[MAXPATHLEN];
748 std::string cwd = getcwd(buf, MAXPATHLEN);
749
750 // Split the target and cwd paths into components.
751 std::vector<std::string> target_path;
752 std::vector<std::string> cwd_path;
753 Split(target, '/', &target_path);
754 Split(cwd, '/', &cwd_path);
755
756 // Reverse the path components, so we can use pop_back().
757 std::reverse(target_path.begin(), target_path.end());
758 std::reverse(cwd_path.begin(), cwd_path.end());
759
760 // Drop the common prefix of the paths. Because we reversed the path
761 // components, this becomes the common suffix of target_path and cwd_path.
762 while (!target_path.empty() && !cwd_path.empty()
763 && target_path.back() == cwd_path.back()) {
764 target_path.pop_back();
765 cwd_path.pop_back();
766 }
767
768 // For each element of the remaining cwd_path, add '..' to the beginning
769 // of the target path. Because we reversed the path components, we add to
770 // the end of target_path.
771 for (unsigned int i = 0; i < cwd_path.size(); i++) {
772 target_path.push_back("..");
773 }
774
775 // Reverse again to get the right path order, and join to get the result.
776 std::reverse(target_path.begin(), target_path.end());
777 return Join(target_path, '/');
778}
779
780// Case: Non-absolute path to Dex location.
781// Expect: Not sure, but it shouldn't crash.
782TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
783 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
784 Copy(GetDexSrc1(), abs_dex_location);
785
786 std::string dex_location = MakePathRelative(abs_dex_location);
787 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
788
789 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700790 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800791 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
792 EXPECT_FALSE(oat_file_assistant.OatFileExists());
793 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
794 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
795 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
796 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
797}
798
799// Case: Very short, non-existent Dex location.
800// Expect: Dex code is out of date, and trying to update it fails.
801TEST_F(OatFileAssistantTest, ShortDexLocation) {
802 std::string dex_location = "/xx";
803
804 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
805
806 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700807 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800808 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
809 EXPECT_FALSE(oat_file_assistant.OatFileExists());
810 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
811 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
812 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
813 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
814
815 std::string error_msg;
816 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
817 EXPECT_FALSE(error_msg.empty());
818}
819
820// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700821// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800822TEST_F(OatFileAssistantTest, LongDexExtension) {
823 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
824 Copy(GetDexSrc1(), dex_location);
825
826 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
827
Richard Uhler95abd042015-03-24 09:51:28 -0700828 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800829
830 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
831 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
832 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
833 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
834 EXPECT_FALSE(oat_file_assistant.OatFileExists());
835 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
836 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
837}
838
839// A task to generate a dex location. Used by the RaceToGenerate test.
840class RaceGenerateTask : public Task {
841 public:
842 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
843 : dex_location_(dex_location), oat_location_(oat_location),
844 loaded_oat_file_(nullptr)
845 {}
846
847 void Run(Thread* self) {
848 UNUSED(self);
849
850 // Load the dex files, and save a pointer to the loaded oat file, so that
851 // we can verify only one oat file was loaded for the dex location.
852 ClassLinker* linker = Runtime::Current()->GetClassLinker();
853 std::vector<std::unique_ptr<const DexFile>> dex_files;
854 std::vector<std::string> error_msgs;
855 dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs);
856 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700857 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
858 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800859 }
860
861 const OatFile* GetLoadedOatFile() const {
862 return loaded_oat_file_;
863 }
864
865 private:
866 std::string dex_location_;
867 std::string oat_location_;
868 const OatFile* loaded_oat_file_;
869};
870
871// Test the case where multiple processes race to generate an oat file.
872// This simulates multiple processes using multiple threads.
873//
874// We want only one Oat file to be loaded when there is a race to load, to
875// avoid using up the virtual memory address space.
876TEST_F(OatFileAssistantTest, RaceToGenerate) {
877 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700878 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800879
880 // We use the lib core dex file, because it's large, and hopefully should
881 // take a while to generate.
882 Copy(GetLibCoreDexFileName(), dex_location);
883
884 const int kNumThreads = 32;
885 Thread* self = Thread::Current();
886 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
887 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
888 for (int i = 0; i < kNumThreads; i++) {
889 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
890 thread_pool.AddTask(self, task.get());
891 tasks.push_back(std::move(task));
892 }
893 thread_pool.StartWorkers(self);
894 thread_pool.Wait(self, true, false);
895
896 // Verify every task got the same pointer.
897 const OatFile* expected = tasks[0]->GetLoadedOatFile();
898 for (auto& task : tasks) {
899 EXPECT_EQ(expected, task->GetLoadedOatFile());
900 }
901}
902
903// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
904// disabled.
905// Expect: We should load the odex file non-executable.
906TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
907 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700908 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800909
910 // Create the dex and odex files
911 Copy(GetDexSrc1(), dex_location);
912 GenerateOdexForTest(dex_location, odex_location);
913
914 // Load the oat using an executable oat file assistant.
915 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
916
917 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
918 ASSERT_TRUE(oat_file.get() != nullptr);
919 EXPECT_FALSE(oat_file->IsExecutable());
920 std::vector<std::unique_ptr<const DexFile>> dex_files;
921 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
922 EXPECT_EQ(1u, dex_files.size());
923}
924
925// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
926// disabled.
927// Expect: We should load the odex file non-executable.
928TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
929 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700930 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800931
932 // Create the dex and odex files
933 Copy(GetMultiDexSrc1(), dex_location);
934 GenerateOdexForTest(dex_location, odex_location);
935
936 // Load the oat using an executable oat file assistant.
937 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
938
939 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
940 ASSERT_TRUE(oat_file.get() != nullptr);
941 EXPECT_FALSE(oat_file->IsExecutable());
942 std::vector<std::unique_ptr<const DexFile>> dex_files;
943 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
944 EXPECT_EQ(2u, dex_files.size());
945}
946
947TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
948 std::string error_msg;
949 std::string odex_file;
950
951 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
952 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700953 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800954
955 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
956 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700957 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800958
959 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
960 "nopath.jar", kArm, &odex_file, &error_msg));
961 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
962 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
963}
964
Richard Uhler23cedd22015-04-08 13:17:29 -0700965// Verify the dexopt status values from dalvik.system.DexFile
966// match the OatFileAssistant::DexOptStatus values.
967TEST_F(OatFileAssistantTest, DexOptStatusValues) {
968 ScopedObjectAccess soa(Thread::Current());
969 StackHandleScope<1> hs(soa.Self());
970 ClassLinker* linker = Runtime::Current()->GetClassLinker();
971 Handle<mirror::Class> dexfile(
972 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
973 ASSERT_FALSE(dexfile.Get() == nullptr);
974 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
975
Mathieu Chartierc7853442015-03-27 14:35:38 -0700976 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700977 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
978 ASSERT_FALSE(no_dexopt_needed == nullptr);
979 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
980 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
981
Mathieu Chartierc7853442015-03-27 14:35:38 -0700982 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700983 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
984 ASSERT_FALSE(dex2oat_needed == nullptr);
985 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
986 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
987
Mathieu Chartierc7853442015-03-27 14:35:38 -0700988 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700989 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
990 ASSERT_FALSE(patchoat_needed == nullptr);
991 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
992 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
993
Mathieu Chartierc7853442015-03-27 14:35:38 -0700994 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -0700995 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
996 ASSERT_FALSE(self_patchoat_needed == nullptr);
997 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
998 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
999}
Richard Uhler66d874d2015-01-15 09:37:19 -08001000
1001// TODO: More Tests:
1002// * Test class linker falls back to unquickened dex for DexNoOat
1003// * Test class linker falls back to unquickened dex for MultiDexNoOat
1004// * Test multidex files:
1005// - Multidex with only classes2.dex out of date should have status
1006// kOutOfDate
1007// * Test using secondary isa
1008// * Test with profiling info?
1009// * Test for status of oat while oat is being generated (how?)
1010// * Test case where 32 and 64 bit boot class paths differ,
1011// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1012// 64 bit boot class paths.
1013// * Test unexpected scenarios (?):
1014// - Dex is stripped, don't have odex.
1015// - Oat file corrupted after status check, before reload unexecutable
1016// because it's unrelocated and no dex2oat
1017
1018} // namespace art