blob: d8e3797ddcc5310ff8642c8b25175e170ba9a48f [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();
Richard Uhler66d874d2015-01-15 09:37:19 -080070 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
71 << "Expected dex file to be at: " << GetDexSrc2();
Richard Uhlera5a1c132015-05-14 13:21:13 -070072
73 // GetMultiDexSrc2 should have the same primary dex checksum as
74 // GetMultiDexSrc1, but a different secondary dex checksum.
75 std::vector<std::unique_ptr<const DexFile>> multi1;
76 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc1().c_str(),
77 GetMultiDexSrc1().c_str(), &error_msg, &multi1)) << error_msg;
78 ASSERT_GT(multi1.size(), 1u);
79
80 std::vector<std::unique_ptr<const DexFile>> multi2;
81 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(),
82 GetMultiDexSrc2().c_str(), &error_msg, &multi2)) << error_msg;
83 ASSERT_GT(multi2.size(), 1u);
84
85 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
86 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -080087 }
88
89 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +000090 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -080091
92 // Set up the image location.
93 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
94 nullptr));
95 // Make sure compilercallbacks are not set so that relocation will be
96 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -070097 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -080098 }
99
100 virtual void PreRuntimeCreate() {
101 UnreserveImageSpace();
102 }
103
104 virtual void PostRuntimeCreate() {
105 ReserveImageSpace();
106 }
107
108 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -0700109 ClearDirectory(odex_dir_.c_str());
110 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
111
112 ClearDirectory(odex_oat_dir_.c_str());
113 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800114
115 ClearDirectory(scratch_dir_.c_str());
116 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
117
118 CommonRuntimeTest::TearDown();
119 }
120
121 void Copy(std::string src, std::string dst) {
122 std::ifstream src_stream(src, std::ios::binary);
123 std::ofstream dst_stream(dst, std::ios::binary);
124
125 dst_stream << src_stream.rdbuf();
126 }
127
128 // Returns the directory where the pre-compiled core.art can be found.
129 // TODO: We should factor out this into common tests somewhere rather than
130 // re-hardcoding it here (This was copied originally from the elf writer
131 // test).
132 std::string GetImageDirectory() {
133 if (IsHost()) {
134 const char* host_dir = getenv("ANDROID_HOST_OUT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 CHECK(host_dir != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800136 return std::string(host_dir) + "/framework";
137 } else {
138 return std::string("/data/art-test");
139 }
140 }
141
142 std::string GetImageLocation() {
143 return GetImageDirectory() + "/core.art";
144 }
145
146 std::string GetImageFile() {
147 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
148 + "/core.art";
149 }
150
151 std::string GetDexSrc1() {
152 return GetTestDexFileName("Main");
153 }
154
155 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
156 // file stripped.
157 std::string GetStrippedDexSrc1() {
158 return GetTestDexFileName("MainStripped");
159 }
160
161 std::string GetMultiDexSrc1() {
162 return GetTestDexFileName("MultiDex");
163 }
164
Richard Uhlera5a1c132015-05-14 13:21:13 -0700165 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
166 // with the contents of the secondary dex file changed.
167 std::string GetMultiDexSrc2() {
168 return GetTestDexFileName("MultiDexModifiedSecondary");
169 }
170
Richard Uhler66d874d2015-01-15 09:37:19 -0800171 std::string GetDexSrc2() {
172 return GetTestDexFileName("Nested");
173 }
174
175 // Scratch directory, for dex and odex files (oat files will go in the
176 // dalvik cache).
177 std::string GetScratchDir() {
178 return scratch_dir_;
179 }
180
Richard Uhler63434112015-03-16 14:32:16 -0700181 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800182 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700183 std::string GetOdexDir() {
184 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800185 }
186
187 // Generate an odex file for the purposes of test.
188 // If pic is true, generates a PIC odex.
189 void GenerateOdexForTest(const std::string& dex_location,
190 const std::string& odex_location,
191 bool pic = false) {
192 // For this operation, we temporarily redirect the dalvik cache so dex2oat
193 // doesn't find the relocated image file.
194 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
195 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
196 std::vector<std::string> args;
197 args.push_back("--dex-file=" + dex_location);
198 args.push_back("--oat-file=" + odex_location);
199 if (pic) {
200 args.push_back("--compile-pic");
201 } else {
202 args.push_back("--include-patch-information");
Richard Uhler05dd8a62015-03-10 10:02:23 -0700203
204 // We need to use the quick compiler to generate non-PIC code, because
205 // the optimizing compiler always generates PIC.
206 args.push_back("--compiler-backend=Quick");
Richard Uhler66d874d2015-01-15 09:37:19 -0800207 }
208 args.push_back("--runtime-arg");
209 args.push_back("-Xnorelocate");
210 std::string error_msg;
211 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
212 setenv("ANDROID_DATA", android_data_.c_str(), 1);
213 }
214
215 void GeneratePicOdexForTest(const std::string& dex_location,
216 const std::string& odex_location) {
217 GenerateOdexForTest(dex_location, odex_location, true);
218 }
219
220 private:
221 // Reserve memory around where the image will be loaded so other memory
222 // won't conflict when it comes time to load the image.
223 // This can be called with an already loaded image to reserve the space
224 // around it.
225 void ReserveImageSpace() {
226 MemMap::Init();
227
228 // Ensure a chunk of memory is reserved for the image space.
229 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
230 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700231 // Include the main space that has to come right after the
232 // image in case of the GSS collector.
233 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800234
Richard Uhler66d874d2015-01-15 09:37:19 -0800235 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
236 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
237 for (BacktraceMap::const_iterator it = map->begin();
238 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700239 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
240 reservation_start = std::max(reservation_start, it->end);
241 }
242 ReserveImageSpaceChunk(reservation_start, reservation_end);
243 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800244
Richard Uhler3efe9792015-03-30 16:18:03 -0700245 // Reserve a chunk of memory for the image space in the given range.
246 // Only has effect for chunks with a positive number of bytes.
247 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
248 if (start < end) {
249 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800250 image_reservation_.push_back(std::unique_ptr<MemMap>(
251 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700252 reinterpret_cast<uint8_t*>(start), end - start,
Richard Uhler66d874d2015-01-15 09:37:19 -0800253 PROT_NONE, false, false, &error_msg)));
254 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
255 LOG(INFO) << "Reserved space for image " <<
256 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
257 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800258 }
259 }
260
261
262 // Unreserve any memory reserved by ReserveImageSpace. This should be called
263 // before the image is loaded.
264 void UnreserveImageSpace() {
265 image_reservation_.clear();
266 }
267
268 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700269 std::string odex_oat_dir_;
270 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800271 std::vector<std::unique_ptr<MemMap>> image_reservation_;
272};
273
274class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
275 public:
276 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
277 OatFileAssistantTest::SetUpRuntimeOptions(options);
278 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
279 }
280};
281
282// Generate an oat file for the purposes of test, as opposed to testing
283// generation of oat files.
284static void GenerateOatForTest(const char* dex_location) {
285 OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false);
286
287 std::string error_msg;
288 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
289}
290
291// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700292// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800293TEST_F(OatFileAssistantTest, DexNoOat) {
294 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
295 Copy(GetDexSrc1(), dex_location);
296
297 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
298
Richard Uhler95abd042015-03-24 09:51:28 -0700299 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800300
301 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
302 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
303 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
304 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
305 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700306 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800307 EXPECT_FALSE(oat_file_assistant.OatFileExists());
308 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
309 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
310 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700311 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800312}
313
314// Case: We have no DEX file and no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700315// Expect: Status is kDex2OatNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800316TEST_F(OatFileAssistantTest, NoDexNoOat) {
317 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
318
319 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
320
Richard Uhler95abd042015-03-24 09:51:28 -0700321 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
323 EXPECT_EQ(nullptr, oat_file.get());
324}
325
326// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700327// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800328TEST_F(OatFileAssistantTest, OatUpToDate) {
329 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
330 Copy(GetDexSrc1(), dex_location);
331 GenerateOatForTest(dex_location.c_str());
332
333 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
334
Richard Uhler95abd042015-03-24 09:51:28 -0700335 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800336 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
337 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
338 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
339 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
340 EXPECT_TRUE(oat_file_assistant.OatFileExists());
341 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
342 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
343 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700344 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800345}
346
347// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700348// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800349TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
350 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
351 Copy(GetMultiDexSrc1(), dex_location);
352 GenerateOatForTest(dex_location.c_str());
353
Richard Uhlere5fed032015-03-18 08:21:11 -0700354 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700355 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
356
357 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700358 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800359 ASSERT_TRUE(oat_file.get() != nullptr);
360 EXPECT_TRUE(oat_file->IsExecutable());
361 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700362 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
363 EXPECT_EQ(2u, dex_files.size());
364}
365
Richard Uhlera5a1c132015-05-14 13:21:13 -0700366// Case: We have a MultiDEX file where the secondary dex file is out of date.
367// Expect: The status is kDex2OatNeeded.
368TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
369 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
370
371 // Compile code for GetMultiDexSrc1.
372 Copy(GetMultiDexSrc1(), dex_location);
373 GenerateOatForTest(dex_location.c_str());
374
375 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
376 // is out of date.
377 Copy(GetMultiDexSrc2(), dex_location);
378
379 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
380 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
381}
382
Richard Uhlere5fed032015-03-18 08:21:11 -0700383// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
384// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700385// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700386TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
387 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700388 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700389
390 // Create the dex file
391 Copy(GetMultiDexSrc1(), dex_location);
392
393 // Create the oat file with relative encoded dex location.
394 std::vector<std::string> args;
395 args.push_back("--dex-file=" + dex_location);
396 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
397 args.push_back("--oat-file=" + oat_location);
398
399 std::string error_msg;
400 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
401
402 // Verify we can load both dex files.
403 OatFileAssistant oat_file_assistant(dex_location.c_str(),
404 oat_location.c_str(),
405 kRuntimeISA, true);
406 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
407 ASSERT_TRUE(oat_file.get() != nullptr);
408 EXPECT_TRUE(oat_file->IsExecutable());
409 std::vector<std::unique_ptr<const DexFile>> dex_files;
410 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800411 EXPECT_EQ(2u, dex_files.size());
412}
413
Richard Uhler95abd042015-03-24 09:51:28 -0700414// Case: We have a DEX file and out-of-date OAT file.
415// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800416TEST_F(OatFileAssistantTest, OatOutOfDate) {
417 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
418
419 // We create a dex, generate an oat for it, then overwrite the dex with a
420 // different dex to make the oat out of date.
421 Copy(GetDexSrc1(), dex_location);
422 GenerateOatForTest(dex_location.c_str());
423 Copy(GetDexSrc2(), dex_location);
424
425 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700426 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800427
428 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
429 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
430 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
431 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
432 EXPECT_TRUE(oat_file_assistant.OatFileExists());
433 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
434 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
435}
436
437// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700438// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800439TEST_F(OatFileAssistantTest, DexOdexNoOat) {
440 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700441 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800442
443 // Create the dex and odex files
444 Copy(GetDexSrc1(), dex_location);
445 GenerateOdexForTest(dex_location, odex_location);
446
447 // Verify the status.
448 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
449
Richard Uhler95abd042015-03-24 09:51:28 -0700450 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800451
452 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
453 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
454 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
455 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
456 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800457 EXPECT_FALSE(oat_file_assistant.OatFileExists());
458 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
459 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
460}
461
462// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700463// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800464TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
465 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700466 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800467
468 // Create the dex and odex files
469 Copy(GetDexSrc1(), dex_location);
470 GenerateOdexForTest(dex_location, odex_location);
471
472 // Strip the dex file
473 Copy(GetStrippedDexSrc1(), dex_location);
474
475 // Verify the status.
476 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
477
Richard Uhler95abd042015-03-24 09:51:28 -0700478 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800479
480 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
481 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
482 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
483 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
484 EXPECT_FALSE(oat_file_assistant.OatFileExists());
485 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
486 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
487
488 // Make the oat file up to date.
489 std::string error_msg;
490 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
491
Richard Uhler95abd042015-03-24 09:51:28 -0700492 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
494 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
495 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
496 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
497 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
498 EXPECT_TRUE(oat_file_assistant.OatFileExists());
499 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
500 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
501
502 // Verify we can load the dex files from it.
503 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
504 ASSERT_TRUE(oat_file.get() != nullptr);
505 EXPECT_TRUE(oat_file->IsExecutable());
506 std::vector<std::unique_ptr<const DexFile>> dex_files;
507 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
508 EXPECT_EQ(1u, dex_files.size());
509}
510
Richard Uhler95abd042015-03-24 09:51:28 -0700511// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
512// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800513TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
514 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700515 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800516
517 // Create the oat file from a different dex file so it looks out of date.
518 Copy(GetDexSrc2(), dex_location);
519 GenerateOatForTest(dex_location.c_str());
520
521 // Create the odex file
522 Copy(GetDexSrc1(), dex_location);
523 GenerateOdexForTest(dex_location, odex_location);
524
525 // Strip the dex file.
526 Copy(GetStrippedDexSrc1(), dex_location);
527
528 // Verify the status.
529 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
530
Richard Uhler95abd042015-03-24 09:51:28 -0700531 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800532
533 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
534 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
535 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
536 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
537 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
538 EXPECT_TRUE(oat_file_assistant.OatFileExists());
539 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
540 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
541
542 // Make the oat file up to date.
543 std::string error_msg;
544 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
545
Richard Uhler95abd042015-03-24 09:51:28 -0700546 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800547
548 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
549 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
550 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
551 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
552 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
553 EXPECT_TRUE(oat_file_assistant.OatFileExists());
554 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
555 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
556 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
557
558 // Verify we can load the dex files from it.
559 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
560 ASSERT_TRUE(oat_file.get() != nullptr);
561 EXPECT_TRUE(oat_file->IsExecutable());
562 std::vector<std::unique_ptr<const DexFile>> dex_files;
563 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
564 EXPECT_EQ(1u, dex_files.size());
565}
566
Richard Uhler95abd042015-03-24 09:51:28 -0700567// Case: We have a DEX file, no ODEX file and an OAT file that needs
568// relocation.
569// Expect: The status is kSelfPatchOatNeeded.
570TEST_F(OatFileAssistantTest, SelfRelocation) {
571 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
572 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
573
574 // Create the dex and odex files
575 Copy(GetDexSrc1(), dex_location);
576 GenerateOdexForTest(dex_location, oat_location);
577
578 OatFileAssistant oat_file_assistant(dex_location.c_str(),
579 oat_location.c_str(), kRuntimeISA, true);
580
581 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
582
583 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
584 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
585 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
586 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
587 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
588 EXPECT_TRUE(oat_file_assistant.OatFileExists());
589 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
590 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
591 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
592
593 // Make the oat file up to date.
594 std::string error_msg;
595 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
596
597 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
598
599 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
600 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
601 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
602 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
603 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
604 EXPECT_TRUE(oat_file_assistant.OatFileExists());
605 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
606 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
607 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
608
609 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
610 ASSERT_TRUE(oat_file.get() != nullptr);
611 EXPECT_TRUE(oat_file->IsExecutable());
612 std::vector<std::unique_ptr<const DexFile>> dex_files;
613 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
614 EXPECT_EQ(1u, dex_files.size());
615}
616
Richard Uhler66d874d2015-01-15 09:37:19 -0800617// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
618// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700619// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800620TEST_F(OatFileAssistantTest, OdexOatOverlap) {
621 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700622 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
623 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800624
625 // Create the dex and odex files
626 Copy(GetDexSrc1(), dex_location);
627 GenerateOdexForTest(dex_location, odex_location);
628
629 // Create the oat file by copying the odex so they are located in the same
630 // place in memory.
631 Copy(odex_location, oat_location);
632
633 // Verify things don't go bad.
634 OatFileAssistant oat_file_assistant(dex_location.c_str(),
635 oat_location.c_str(), kRuntimeISA, true);
636
Richard Uhler95abd042015-03-24 09:51:28 -0700637 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800638
639 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
640 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
641 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
642 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
643 EXPECT_TRUE(oat_file_assistant.OatFileExists());
644 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
645 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
646
647 // Things aren't relocated, so it should fall back to interpreted.
648 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
649 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700650
Richard Uhler66d874d2015-01-15 09:37:19 -0800651 EXPECT_FALSE(oat_file->IsExecutable());
652 std::vector<std::unique_ptr<const DexFile>> dex_files;
653 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
654 EXPECT_EQ(1u, dex_files.size());
Richard Uhlerf16d5722015-05-11 09:32:47 -0700655
656 // Add some extra checks to help diagnose apparently flaky test failures.
657 Runtime* runtime = Runtime::Current();
658 const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace();
659 ASSERT_TRUE(image_space != nullptr);
660 const ImageHeader& image_header = image_space->GetImageHeader();
661 const OatHeader& oat_header = oat_file->GetOatHeader();
662 EXPECT_FALSE(oat_file->IsPic());
663 EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum());
664 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
665 oat_header.GetImageFileLocationOatDataBegin());
666 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
Richard Uhler66d874d2015-01-15 09:37:19 -0800667}
668
669// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700670// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800671TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
672 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700673 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800674
675 // Create the dex and odex files
676 Copy(GetDexSrc1(), dex_location);
677 GeneratePicOdexForTest(dex_location, odex_location);
678
679 // Verify the status.
680 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
681
Richard Uhler95abd042015-03-24 09:51:28 -0700682 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800683
684 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
685 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
686 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
687 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
688 EXPECT_FALSE(oat_file_assistant.OatFileExists());
689 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
690 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
691}
692
693// Case: We have a DEX file and up-to-date OAT file for it.
694// Expect: We should load an executable dex file.
695TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
696 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
697
698 Copy(GetDexSrc1(), dex_location);
699 GenerateOatForTest(dex_location.c_str());
700
701 // Load the oat using an oat file assistant.
702 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
703
704 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
705 ASSERT_TRUE(oat_file.get() != nullptr);
706 EXPECT_TRUE(oat_file->IsExecutable());
707 std::vector<std::unique_ptr<const DexFile>> dex_files;
708 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
709 EXPECT_EQ(1u, dex_files.size());
710}
711
712// Case: We have a DEX file and up-to-date OAT file for it.
713// Expect: Loading non-executable should load the oat non-executable.
714TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
715 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
716
717 Copy(GetDexSrc1(), dex_location);
718 GenerateOatForTest(dex_location.c_str());
719
720 // Load the oat using an oat file assistant.
721 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
722
723 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
724 ASSERT_TRUE(oat_file.get() != nullptr);
725 EXPECT_FALSE(oat_file->IsExecutable());
726 std::vector<std::unique_ptr<const DexFile>> dex_files;
727 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
728 EXPECT_EQ(1u, dex_files.size());
729}
730
731// Case: We have a DEX file.
732// Expect: We should load an executable dex file from an alternative oat
733// location.
734TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
735 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
736 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
737
738 Copy(GetDexSrc1(), dex_location);
739
740 OatFileAssistant oat_file_assistant(
741 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
742 std::string error_msg;
743 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
744
745 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
746 ASSERT_TRUE(oat_file.get() != nullptr);
747 EXPECT_TRUE(oat_file->IsExecutable());
748 std::vector<std::unique_ptr<const DexFile>> dex_files;
749 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
750 EXPECT_EQ(1u, dex_files.size());
751
752 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
753
754 // Verify it didn't create an oat in the default location.
755 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
756 EXPECT_FALSE(ofm.OatFileExists());
757}
758
759// Case: Non-existent Dex location.
760// Expect: The dex code is out of date, and trying to update it fails.
761TEST_F(OatFileAssistantTest, NonExsistentDexLocation) {
762 std::string dex_location = GetScratchDir() + "/BadDexLocation.jar";
763
764 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
765
766 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700767 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800768 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
769 EXPECT_FALSE(oat_file_assistant.OatFileExists());
770 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
771 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
772 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
773 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
774
775 std::string error_msg;
776 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
777 EXPECT_FALSE(error_msg.empty());
778}
779
780// Turn an absolute path into a path relative to the current working
781// directory.
782static std::string MakePathRelative(std::string target) {
783 char buf[MAXPATHLEN];
784 std::string cwd = getcwd(buf, MAXPATHLEN);
785
786 // Split the target and cwd paths into components.
787 std::vector<std::string> target_path;
788 std::vector<std::string> cwd_path;
789 Split(target, '/', &target_path);
790 Split(cwd, '/', &cwd_path);
791
792 // Reverse the path components, so we can use pop_back().
793 std::reverse(target_path.begin(), target_path.end());
794 std::reverse(cwd_path.begin(), cwd_path.end());
795
796 // Drop the common prefix of the paths. Because we reversed the path
797 // components, this becomes the common suffix of target_path and cwd_path.
798 while (!target_path.empty() && !cwd_path.empty()
799 && target_path.back() == cwd_path.back()) {
800 target_path.pop_back();
801 cwd_path.pop_back();
802 }
803
804 // For each element of the remaining cwd_path, add '..' to the beginning
805 // of the target path. Because we reversed the path components, we add to
806 // the end of target_path.
807 for (unsigned int i = 0; i < cwd_path.size(); i++) {
808 target_path.push_back("..");
809 }
810
811 // Reverse again to get the right path order, and join to get the result.
812 std::reverse(target_path.begin(), target_path.end());
813 return Join(target_path, '/');
814}
815
816// Case: Non-absolute path to Dex location.
817// Expect: Not sure, but it shouldn't crash.
818TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
819 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
820 Copy(GetDexSrc1(), abs_dex_location);
821
822 std::string dex_location = MakePathRelative(abs_dex_location);
823 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
824
825 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700826 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800827 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
828 EXPECT_FALSE(oat_file_assistant.OatFileExists());
829 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
830 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
831 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
832 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
833}
834
835// Case: Very short, non-existent Dex location.
836// Expect: Dex code is out of date, and trying to update it fails.
837TEST_F(OatFileAssistantTest, ShortDexLocation) {
838 std::string dex_location = "/xx";
839
840 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
841
842 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700843 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800844 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
845 EXPECT_FALSE(oat_file_assistant.OatFileExists());
846 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
847 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
848 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
849 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
850
851 std::string error_msg;
852 EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
853 EXPECT_FALSE(error_msg.empty());
854}
855
856// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700857// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800858TEST_F(OatFileAssistantTest, LongDexExtension) {
859 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
860 Copy(GetDexSrc1(), dex_location);
861
862 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
863
Richard Uhler95abd042015-03-24 09:51:28 -0700864 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800865
866 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
867 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
868 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
869 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
870 EXPECT_FALSE(oat_file_assistant.OatFileExists());
871 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
872 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
873}
874
875// A task to generate a dex location. Used by the RaceToGenerate test.
876class RaceGenerateTask : public Task {
877 public:
878 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
879 : dex_location_(dex_location), oat_location_(oat_location),
880 loaded_oat_file_(nullptr)
881 {}
882
883 void Run(Thread* self) {
884 UNUSED(self);
885
886 // Load the dex files, and save a pointer to the loaded oat file, so that
887 // we can verify only one oat file was loaded for the dex location.
888 ClassLinker* linker = Runtime::Current()->GetClassLinker();
889 std::vector<std::unique_ptr<const DexFile>> dex_files;
890 std::vector<std::string> error_msgs;
891 dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs);
892 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700893 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
894 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800895 }
896
897 const OatFile* GetLoadedOatFile() const {
898 return loaded_oat_file_;
899 }
900
901 private:
902 std::string dex_location_;
903 std::string oat_location_;
904 const OatFile* loaded_oat_file_;
905};
906
907// Test the case where multiple processes race to generate an oat file.
908// This simulates multiple processes using multiple threads.
909//
910// We want only one Oat file to be loaded when there is a race to load, to
911// avoid using up the virtual memory address space.
912TEST_F(OatFileAssistantTest, RaceToGenerate) {
913 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700914 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800915
916 // We use the lib core dex file, because it's large, and hopefully should
917 // take a while to generate.
918 Copy(GetLibCoreDexFileName(), dex_location);
919
920 const int kNumThreads = 32;
921 Thread* self = Thread::Current();
922 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
923 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
924 for (int i = 0; i < kNumThreads; i++) {
925 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
926 thread_pool.AddTask(self, task.get());
927 tasks.push_back(std::move(task));
928 }
929 thread_pool.StartWorkers(self);
930 thread_pool.Wait(self, true, false);
931
932 // Verify every task got the same pointer.
933 const OatFile* expected = tasks[0]->GetLoadedOatFile();
934 for (auto& task : tasks) {
935 EXPECT_EQ(expected, task->GetLoadedOatFile());
936 }
937}
938
939// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
940// disabled.
941// Expect: We should load the odex file non-executable.
942TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
943 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700944 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800945
946 // Create the dex and odex files
947 Copy(GetDexSrc1(), dex_location);
948 GenerateOdexForTest(dex_location, odex_location);
949
950 // Load the oat using an executable oat file assistant.
951 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
952
953 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
954 ASSERT_TRUE(oat_file.get() != nullptr);
955 EXPECT_FALSE(oat_file->IsExecutable());
956 std::vector<std::unique_ptr<const DexFile>> dex_files;
957 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
958 EXPECT_EQ(1u, dex_files.size());
959}
960
961// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
962// disabled.
963// Expect: We should load the odex file non-executable.
964TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
965 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700966 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800967
968 // Create the dex and odex files
969 Copy(GetMultiDexSrc1(), dex_location);
970 GenerateOdexForTest(dex_location, odex_location);
971
972 // Load the oat using an executable oat file assistant.
973 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
974
975 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
976 ASSERT_TRUE(oat_file.get() != nullptr);
977 EXPECT_FALSE(oat_file->IsExecutable());
978 std::vector<std::unique_ptr<const DexFile>> dex_files;
979 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
980 EXPECT_EQ(2u, dex_files.size());
981}
982
983TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
984 std::string error_msg;
985 std::string odex_file;
986
987 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
988 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700989 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800990
991 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
992 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -0700993 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800994
995 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
996 "nopath.jar", kArm, &odex_file, &error_msg));
997 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
998 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
999}
1000
Richard Uhler23cedd22015-04-08 13:17:29 -07001001// Verify the dexopt status values from dalvik.system.DexFile
1002// match the OatFileAssistant::DexOptStatus values.
1003TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1004 ScopedObjectAccess soa(Thread::Current());
1005 StackHandleScope<1> hs(soa.Self());
1006 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1007 Handle<mirror::Class> dexfile(
1008 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1009 ASSERT_FALSE(dexfile.Get() == nullptr);
1010 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1011
Mathieu Chartierc7853442015-03-27 14:35:38 -07001012 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001013 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1014 ASSERT_FALSE(no_dexopt_needed == nullptr);
1015 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1016 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1017
Mathieu Chartierc7853442015-03-27 14:35:38 -07001018 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001019 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1020 ASSERT_FALSE(dex2oat_needed == nullptr);
1021 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1022 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1023
Mathieu Chartierc7853442015-03-27 14:35:38 -07001024 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001025 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1026 ASSERT_FALSE(patchoat_needed == nullptr);
1027 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1028 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1029
Mathieu Chartierc7853442015-03-27 14:35:38 -07001030 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001031 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1032 ASSERT_FALSE(self_patchoat_needed == nullptr);
1033 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1034 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1035}
Richard Uhler66d874d2015-01-15 09:37:19 -08001036
1037// TODO: More Tests:
1038// * Test class linker falls back to unquickened dex for DexNoOat
1039// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001040// * Test using secondary isa
1041// * Test with profiling info?
1042// * Test for status of oat while oat is being generated (how?)
1043// * Test case where 32 and 64 bit boot class paths differ,
1044// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1045// 64 bit boot class paths.
1046// * Test unexpected scenarios (?):
1047// - Dex is stripped, don't have odex.
1048// - Oat file corrupted after status check, before reload unexecutable
1049// because it's unrelocated and no dex2oat
1050
1051} // namespace art