blob: 23ba4d8cc14e8d37b5a3708e9df6105a93048016 [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"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070034#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080035#include "os.h"
Richard Uhler23cedd22015-04-08 13:17:29 -070036#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "thread-inl.h"
38#include "utils.h"
39
40namespace art {
41
42class OatFileAssistantTest : public CommonRuntimeTest {
43 public:
44 virtual void SetUp() {
45 ReserveImageSpace();
46 CommonRuntimeTest::SetUp();
47
48 // Create a scratch directory to work from.
49 scratch_dir_ = android_data_ + "/OatFileAssistantTest";
50 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
51
Richard Uhler63434112015-03-16 14:32:16 -070052 // Create a subdirectory in scratch for odex files.
53 odex_oat_dir_ = scratch_dir_ + "/oat";
54 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
55
56 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
57 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
58
Richard Uhler66d874d2015-01-15 09:37:19 -080059 // Verify the environment is as we expect
60 uint32_t checksum;
61 std::string error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -070062 ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str()))
63 << "Expected pre-compiled boot image to be at: " << GetSystemImageFile();
Richard Uhler66d874d2015-01-15 09:37:19 -080064 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();
Igor Murashkinb1d8c312015-08-04 11:18:43 -070068 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
Richard Uhler66d874d2015-01-15 09:37:19 -080069 << "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 Uhler67ff7d12015-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(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -070077 GetMultiDexSrc1().c_str(), &error_msg, &multi1)) << error_msg;
Richard Uhler67ff7d12015-05-14 13:21:13 -070078 ASSERT_GT(multi1.size(), 1u);
79
80 std::vector<std::unique_ptr<const DexFile>> multi2;
81 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -070082 GetMultiDexSrc2().c_str(), &error_msg, &multi2)) << error_msg;
Richard Uhler67ff7d12015-05-14 13:21:13 -070083 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
Richard Uhlera48403e2016-04-26 10:24:38 -070089 // Pre-Relocate the image to a known non-zero offset so we don't have to
90 // deal with the runtime randomly relocating the image by 0 and messing up
91 // the expected results of the tests.
92 bool PreRelocateImage(std::string* error_msg) {
93 std::string image;
94 if (!GetCachedImageFile(&image, error_msg)) {
95 return false;
96 }
97
98 std::string patchoat = GetAndroidRoot();
99 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
100
101 std::vector<std::string> argv;
102 argv.push_back(patchoat);
103 argv.push_back("--input-image-location=" + GetImageLocation());
104 argv.push_back("--output-image-file=" + image);
105 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
106 argv.push_back("--base-offset-delta=0x00008000");
107 return Exec(argv, error_msg);
108 }
109
Richard Uhler66d874d2015-01-15 09:37:19 -0800110 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +0000111 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -0800112
113 // Set up the image location.
114 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
115 nullptr));
116 // Make sure compilercallbacks are not set so that relocation will be
117 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700118 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -0800119 }
120
121 virtual void PreRuntimeCreate() {
Richard Uhlera48403e2016-04-26 10:24:38 -0700122 std::string error_msg;
123 ASSERT_TRUE(PreRelocateImage(&error_msg)) << error_msg;
124
Richard Uhler66d874d2015-01-15 09:37:19 -0800125 UnreserveImageSpace();
126 }
127
128 virtual void PostRuntimeCreate() {
129 ReserveImageSpace();
130 }
131
132 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -0700133 ClearDirectory(odex_dir_.c_str());
134 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
135
136 ClearDirectory(odex_oat_dir_.c_str());
137 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800138
139 ClearDirectory(scratch_dir_.c_str());
140 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
141
142 CommonRuntimeTest::TearDown();
143 }
144
145 void Copy(std::string src, std::string dst) {
146 std::ifstream src_stream(src, std::ios::binary);
147 std::ofstream dst_stream(dst, std::ios::binary);
148
149 dst_stream << src_stream.rdbuf();
150 }
151
152 // Returns the directory where the pre-compiled core.art can be found.
153 // TODO: We should factor out this into common tests somewhere rather than
154 // re-hardcoding it here (This was copied originally from the elf writer
155 // test).
156 std::string GetImageDirectory() {
157 if (IsHost()) {
158 const char* host_dir = getenv("ANDROID_HOST_OUT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700159 CHECK(host_dir != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800160 return std::string(host_dir) + "/framework";
161 } else {
162 return std::string("/data/art-test");
163 }
164 }
165
166 std::string GetImageLocation() {
167 return GetImageDirectory() + "/core.art";
168 }
169
Richard Uhlera48403e2016-04-26 10:24:38 -0700170 std::string GetSystemImageFile() {
Richard Uhler66d874d2015-01-15 09:37:19 -0800171 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
172 + "/core.art";
173 }
174
Richard Uhlera48403e2016-04-26 10:24:38 -0700175 bool GetCachedImageFile(/*out*/std::string* image, std::string* error_msg) {
176 std::string cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA), true);
177 return GetDalvikCacheFilename(GetImageLocation().c_str(), cache.c_str(), image, error_msg);
178 }
179
Richard Uhler66d874d2015-01-15 09:37:19 -0800180 std::string GetDexSrc1() {
181 return GetTestDexFileName("Main");
182 }
183
184 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
185 // file stripped.
186 std::string GetStrippedDexSrc1() {
187 return GetTestDexFileName("MainStripped");
188 }
189
190 std::string GetMultiDexSrc1() {
191 return GetTestDexFileName("MultiDex");
192 }
193
Richard Uhler67ff7d12015-05-14 13:21:13 -0700194 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
195 // with the contents of the secondary dex file changed.
196 std::string GetMultiDexSrc2() {
197 return GetTestDexFileName("MultiDexModifiedSecondary");
198 }
199
Richard Uhler66d874d2015-01-15 09:37:19 -0800200 std::string GetDexSrc2() {
201 return GetTestDexFileName("Nested");
202 }
203
204 // Scratch directory, for dex and odex files (oat files will go in the
205 // dalvik cache).
206 std::string GetScratchDir() {
207 return scratch_dir_;
208 }
209
Richard Uhler63434112015-03-16 14:32:16 -0700210 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800211 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700212 std::string GetOdexDir() {
213 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800214 }
215
Richard Uhler93aa2102015-08-10 14:47:41 -0700216 // Generate a non-PIC odex file for the purposes of test.
Richard Uhler94f5bda2015-07-22 08:25:11 -0700217 // The generated odex file will be un-relocated.
Richard Uhler66d874d2015-01-15 09:37:19 -0800218 void GenerateOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000219 const std::string& odex_location,
Richard Uhlera48403e2016-04-26 10:24:38 -0700220 CompilerFilter::Filter filter,
221 bool pic = false,
222 bool with_patch_info = true) {
223 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
224 // relocated image file.
225 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
226 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
Richard Uhler93aa2102015-08-10 14:47:41 -0700227 std::vector<std::string> args;
228 args.push_back("--dex-file=" + dex_location);
Richard Uhlera48403e2016-04-26 10:24:38 -0700229 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000230 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhlera48403e2016-04-26 10:24:38 -0700231 args.push_back("--runtime-arg");
232 args.push_back("-Xnorelocate");
Richard Uhler93aa2102015-08-10 14:47:41 -0700233
Richard Uhlera48403e2016-04-26 10:24:38 -0700234 if (pic) {
235 args.push_back("--compile-pic");
236 }
237
238 if (with_patch_info) {
239 args.push_back("--include-patch-information");
240 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700241
242 std::string error_msg;
243 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700244 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler93aa2102015-08-10 14:47:41 -0700245
246 // Verify the odex file was generated as expected and really is
247 // unrelocated.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800248 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
249 odex_location.c_str(),
250 nullptr,
251 nullptr,
252 false,
253 /*low_4gb*/false,
254 dex_location.c_str(),
255 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700256 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700257 EXPECT_EQ(pic, odex_file->IsPic());
258 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000259 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
260
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100261 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000262 const std::vector<gc::space::ImageSpace*> image_spaces =
Richard Uhlera48403e2016-04-26 10:24:38 -0700263 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000264 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
265 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
266 const OatHeader& oat_header = odex_file->GetOatHeader();
Jeff Haob11ffb72016-04-07 15:40:54 -0700267 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
268 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000269 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
270 oat_header.GetImageFileLocationOatDataBegin());
271 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
272 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700273 }
274
275 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000276 const std::string& odex_location,
277 CompilerFilter::Filter filter) {
Richard Uhlera48403e2016-04-26 10:24:38 -0700278 GenerateOdexForTest(dex_location, odex_location, filter, true, false);
Calin Juravleb077e152016-02-18 18:47:37 +0000279 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000280
Richard Uhlerd1537b52016-03-29 13:27:41 -0700281 // Generate a non-PIC odex file without patch information for the purposes
282 // of test. The generated odex file will be un-relocated.
Richard Uhlerd1537b52016-03-29 13:27:41 -0700283 void GenerateNoPatchOdexForTest(const std::string& dex_location,
284 const std::string& odex_location,
285 CompilerFilter::Filter filter) {
Richard Uhlera48403e2016-04-26 10:24:38 -0700286 GenerateOdexForTest(dex_location, odex_location, filter, false, false);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700287 }
288
Richard Uhler66d874d2015-01-15 09:37:19 -0800289 private:
290 // Reserve memory around where the image will be loaded so other memory
291 // won't conflict when it comes time to load the image.
292 // This can be called with an already loaded image to reserve the space
293 // around it.
294 void ReserveImageSpace() {
295 MemMap::Init();
296
297 // Ensure a chunk of memory is reserved for the image space.
Richard Uhlera48403e2016-04-26 10:24:38 -0700298 // The reservation_end includes room for the main space that has to come
299 // right after the image in case of the GSS collector.
300 uintptr_t reservation_start = ART_BASE_ADDRESS;
301 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800302
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
304 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
305 for (BacktraceMap::const_iterator it = map->begin();
306 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700307 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
308 reservation_start = std::max(reservation_start, it->end);
309 }
310 ReserveImageSpaceChunk(reservation_start, reservation_end);
311 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800312
Richard Uhler3efe9792015-03-30 16:18:03 -0700313 // Reserve a chunk of memory for the image space in the given range.
314 // Only has effect for chunks with a positive number of bytes.
315 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
316 if (start < end) {
317 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800318 image_reservation_.push_back(std::unique_ptr<MemMap>(
319 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700320 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700321 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
323 LOG(INFO) << "Reserved space for image " <<
324 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
325 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800326 }
327 }
328
329
330 // Unreserve any memory reserved by ReserveImageSpace. This should be called
331 // before the image is loaded.
332 void UnreserveImageSpace() {
333 image_reservation_.clear();
334 }
335
336 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700337 std::string odex_oat_dir_;
338 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800339 std::vector<std::unique_ptr<MemMap>> image_reservation_;
340};
341
342class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
343 public:
344 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
345 OatFileAssistantTest::SetUpRuntimeOptions(options);
346 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
347 }
348};
349
350// Generate an oat file for the purposes of test, as opposed to testing
351// generation of oat files.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000352static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
353 // Use an oat file assistant to find the proper oat location.
354 OatFileAssistant ofa(dex_location, kRuntimeISA, false, false);
355 const std::string* oat_location = ofa.OatFileName();
356 ASSERT_TRUE(oat_location != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800357
Andreas Gampe29d38e72016-03-23 15:31:51 +0000358 std::vector<std::string> args;
359 args.push_back("--dex-file=" + std::string(dex_location));
360 args.push_back("--oat-file=" + *oat_location);
361 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
362 args.push_back("--runtime-arg");
363 args.push_back("-Xnorelocate");
Richard Uhler66d874d2015-01-15 09:37:19 -0800364 std::string error_msg;
Andreas Gampe29d38e72016-03-23 15:31:51 +0000365 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
366
367 // Verify the oat file was generated as expected.
368 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location->c_str(),
369 oat_location->c_str(),
370 nullptr,
371 nullptr,
372 false,
373 /*low_4gb*/false,
374 dex_location,
375 &error_msg));
376 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
377 EXPECT_EQ(filter, oat_file->GetCompilerFilter());
Richard Uhler66d874d2015-01-15 09:37:19 -0800378}
379
380// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700381// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800382TEST_F(OatFileAssistantTest, DexNoOat) {
383 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
384 Copy(GetDexSrc1(), dex_location);
385
Andreas Gampe29d38e72016-03-23 15:31:51 +0000386 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800387
Andreas Gampe29d38e72016-03-23 15:31:51 +0000388 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
389 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
390 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
391 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
392 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
393 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
394 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
395 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800396
397 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
398 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
399 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
400 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
401 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700402 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800403 EXPECT_FALSE(oat_file_assistant.OatFileExists());
404 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
405 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
406 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700407 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700408 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800409}
410
411// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700412// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800413TEST_F(OatFileAssistantTest, NoDexNoOat) {
414 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
415
Andreas Gampe29d38e72016-03-23 15:31:51 +0000416 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800417
Andreas Gampe29d38e72016-03-23 15:31:51 +0000418 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
419 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700420 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
421
422 // Trying to make the oat file up to date should not fail or crash.
423 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700424 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700425
426 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800427 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
428 EXPECT_EQ(nullptr, oat_file.get());
429}
430
431// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700432// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800433TEST_F(OatFileAssistantTest, OatUpToDate) {
434 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
435 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000436 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800437
Andreas Gampe29d38e72016-03-23 15:31:51 +0000438 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800439
Andreas Gampe29d38e72016-03-23 15:31:51 +0000440 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
441 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
442 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
443 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
444 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
445 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
446 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
447 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
448
Richard Uhler66d874d2015-01-15 09:37:19 -0800449 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
450 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
451 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
452 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
453 EXPECT_TRUE(oat_file_assistant.OatFileExists());
454 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
455 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
456 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700457 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700458 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800459}
460
Andreas Gampe29d38e72016-03-23 15:31:51 +0000461// Case: We have a DEX file and speed-profile OAT file for it.
462// Expect: The status is kNoDexOptNeeded if the profile hasn't changed.
463TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
464 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
465 Copy(GetDexSrc1(), dex_location);
466 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
467
468 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
469
470 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
471 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
472 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
473 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
474
475 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
476 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
477 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
478 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
479 EXPECT_TRUE(oat_file_assistant.OatFileExists());
480 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
481 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
482 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
483 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
484 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
485}
486
487// Case: We have a DEX file and speed-profile OAT file for it.
488// Expect: The status is kNoDex2OatNeeded if the profile has changed.
489TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) {
490 std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar";
491 Copy(GetDexSrc1(), dex_location);
492 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
493
494 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false);
495
496 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
497 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
498 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
499 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
500
501 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
502 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
503 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
504 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
505 EXPECT_TRUE(oat_file_assistant.OatFileExists());
506 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
507 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
508 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
509 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
510 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
511}
512
Richard Uhler66d874d2015-01-15 09:37:19 -0800513// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700514// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800515TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
516 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
517 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000518 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800519
Andreas Gampe29d38e72016-03-23 15:31:51 +0000520 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
521 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
522 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700523 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700524
525 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700526 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800527 ASSERT_TRUE(oat_file.get() != nullptr);
528 EXPECT_TRUE(oat_file->IsExecutable());
529 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700530 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
531 EXPECT_EQ(2u, dex_files.size());
532}
533
Richard Uhler67ff7d12015-05-14 13:21:13 -0700534// Case: We have a MultiDEX file where the secondary dex file is out of date.
535// Expect: The status is kDex2OatNeeded.
536TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
537 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
538
539 // Compile code for GetMultiDexSrc1.
540 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000541 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700542
543 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
544 // is out of date.
545 Copy(GetMultiDexSrc2(), dex_location);
546
Andreas Gampe29d38e72016-03-23 15:31:51 +0000547 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
548 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
549 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700550 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700551}
552
Richard Uhlere5fed032015-03-18 08:21:11 -0700553// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
554// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700555// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700556TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
557 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700558 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700559
560 // Create the dex file
561 Copy(GetMultiDexSrc1(), dex_location);
562
563 // Create the oat file with relative encoded dex location.
564 std::vector<std::string> args;
565 args.push_back("--dex-file=" + dex_location);
566 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
567 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000568 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700569
570 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700571 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700572
573 // Verify we can load both dex files.
574 OatFileAssistant oat_file_assistant(dex_location.c_str(),
575 oat_location.c_str(),
Andreas Gampe29d38e72016-03-23 15:31:51 +0000576 kRuntimeISA, false, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700577 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
578 ASSERT_TRUE(oat_file.get() != nullptr);
579 EXPECT_TRUE(oat_file->IsExecutable());
580 std::vector<std::unique_ptr<const DexFile>> dex_files;
581 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800582 EXPECT_EQ(2u, dex_files.size());
583}
584
Richard Uhler95abd042015-03-24 09:51:28 -0700585// Case: We have a DEX file and out-of-date OAT file.
586// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800587TEST_F(OatFileAssistantTest, OatOutOfDate) {
588 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
589
590 // We create a dex, generate an oat for it, then overwrite the dex with a
591 // different dex to make the oat out of date.
592 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000593 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800594 Copy(GetDexSrc2(), dex_location);
595
Andreas Gampe29d38e72016-03-23 15:31:51 +0000596 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
597 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
598 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
599 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
600 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800601
602 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
603 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
604 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
605 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
606 EXPECT_TRUE(oat_file_assistant.OatFileExists());
607 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
608 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700609 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800610}
611
612// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700613// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800614TEST_F(OatFileAssistantTest, DexOdexNoOat) {
615 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700616 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800617
618 // Create the dex and odex files
619 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000620 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800621
622 // Verify the status.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000623 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800624
Andreas Gampe29d38e72016-03-23 15:31:51 +0000625 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
626 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
627 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
628 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800629
630 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
631 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
632 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
633 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
634 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800635 EXPECT_FALSE(oat_file_assistant.OatFileExists());
636 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
637 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700638 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700639
640 // We should still be able to get the non-executable odex file to run from.
641 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
642 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800643}
644
645// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700646// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800647TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
648 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700649 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800650
651 // Create the dex and odex files
652 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000653 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800654
655 // Strip the dex file
656 Copy(GetStrippedDexSrc1(), dex_location);
657
658 // Verify the status.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000659 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800660
Andreas Gampe29d38e72016-03-23 15:31:51 +0000661 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
662 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800663
664 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
665 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
666 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
667 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
668 EXPECT_FALSE(oat_file_assistant.OatFileExists());
669 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
670 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700671 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800672
673 // Make the oat file up to date.
674 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700675 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700676 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700677 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800678
Andreas Gampe29d38e72016-03-23 15:31:51 +0000679 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
680 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800681
682 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
683 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
684 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
685 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
686 EXPECT_TRUE(oat_file_assistant.OatFileExists());
687 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
688 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700689 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800690
691 // Verify we can load the dex files from it.
692 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
693 ASSERT_TRUE(oat_file.get() != nullptr);
694 EXPECT_TRUE(oat_file->IsExecutable());
695 std::vector<std::unique_ptr<const DexFile>> dex_files;
696 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
697 EXPECT_EQ(1u, dex_files.size());
698}
699
Richard Uhler95abd042015-03-24 09:51:28 -0700700// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
701// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800702TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
703 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700704 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800705
706 // Create the oat file from a different dex file so it looks out of date.
707 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000708 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800709
710 // Create the odex file
711 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000712 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800713
714 // Strip the dex file.
715 Copy(GetStrippedDexSrc1(), dex_location);
716
717 // Verify the status.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000718 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800719
Andreas Gampe29d38e72016-03-23 15:31:51 +0000720 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
721 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
722 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
723 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
724 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
725 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800726
727 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
728 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
729 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
730 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
731 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
732 EXPECT_TRUE(oat_file_assistant.OatFileExists());
733 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
734 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700735 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800736
737 // Make the oat file up to date.
738 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700739 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700740 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700741 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800742
Andreas Gampe29d38e72016-03-23 15:31:51 +0000743 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
744 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
745 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
746 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800747
748 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
749 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
750 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
751 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
752 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
753 EXPECT_TRUE(oat_file_assistant.OatFileExists());
754 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
755 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
756 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700757 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800758
759 // Verify we can load the dex files from it.
760 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
761 ASSERT_TRUE(oat_file.get() != nullptr);
762 EXPECT_TRUE(oat_file->IsExecutable());
763 std::vector<std::unique_ptr<const DexFile>> dex_files;
764 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
765 EXPECT_EQ(1u, dex_files.size());
766}
767
Richard Uhler9b994ea2015-06-24 08:44:19 -0700768// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
769// OAT file. Expect: The status is kNoDexOptNeeded.
770TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
771 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
772
773 Copy(GetStrippedDexSrc1(), dex_location);
774
775 // Verify the status.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000776 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700777
Andreas Gampe29d38e72016-03-23 15:31:51 +0000778 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
779 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
780 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
781 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
782 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
783 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700784
785 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
786 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
787 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
788 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
789 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
790 EXPECT_FALSE(oat_file_assistant.OatFileExists());
791 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
792 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
793 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
794
795 // Make the oat file up to date. This should have no effect.
796 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700797 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700798 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700799 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700800
Andreas Gampe29d38e72016-03-23 15:31:51 +0000801 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
802 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700803
804 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
805 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
806 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
807 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
808 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
809 EXPECT_FALSE(oat_file_assistant.OatFileExists());
810 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
811 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
812 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
813}
814
Richard Uhler95abd042015-03-24 09:51:28 -0700815// Case: We have a DEX file, no ODEX file and an OAT file that needs
816// relocation.
817// Expect: The status is kSelfPatchOatNeeded.
818TEST_F(OatFileAssistantTest, SelfRelocation) {
819 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
820 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
821
822 // Create the dex and odex files
823 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000824 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700825
826 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe29d38e72016-03-23 15:31:51 +0000827 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700828
Andreas Gampe29d38e72016-03-23 15:31:51 +0000829 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
830 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
831 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
832 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
833 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
834 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700835
836 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
837 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
838 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
839 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
840 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
841 EXPECT_TRUE(oat_file_assistant.OatFileExists());
842 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
843 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
844 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700845 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700846
847 // Make the oat file up to date.
848 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700849 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700850 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700851 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700852
Andreas Gampe29d38e72016-03-23 15:31:51 +0000853 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
854 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700855
856 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
857 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
858 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
859 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
860 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
861 EXPECT_TRUE(oat_file_assistant.OatFileExists());
862 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
863 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
864 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700865 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700866
867 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
868 ASSERT_TRUE(oat_file.get() != nullptr);
869 EXPECT_TRUE(oat_file->IsExecutable());
870 std::vector<std::unique_ptr<const DexFile>> dex_files;
871 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
872 EXPECT_EQ(1u, dex_files.size());
873}
874
Richard Uhlerd1537b52016-03-29 13:27:41 -0700875// Case: We have a DEX file, no ODEX file and an OAT file that needs
876// relocation but doesn't have patch info.
877// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
878TEST_F(OatFileAssistantTest, NoSelfRelocation) {
879 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
880 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
881
882 // Create the dex and odex files
883 Copy(GetDexSrc1(), dex_location);
884 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
885
886 OatFileAssistant oat_file_assistant(dex_location.c_str(),
887 oat_location.c_str(), kRuntimeISA, false, true);
888
889 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
890 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
891
892 // Make the oat file up to date.
893 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700894 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700895 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700896 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhlerd1537b52016-03-29 13:27:41 -0700897 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
898 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
899
900 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
901 ASSERT_TRUE(oat_file.get() != nullptr);
902 EXPECT_TRUE(oat_file->IsExecutable());
903 std::vector<std::unique_ptr<const DexFile>> dex_files;
904 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
905 EXPECT_EQ(1u, dex_files.size());
906}
907
Richard Uhler66d874d2015-01-15 09:37:19 -0800908// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
909// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700910// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800911TEST_F(OatFileAssistantTest, OdexOatOverlap) {
912 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700913 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
914 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800915
916 // Create the dex and odex files
917 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000918 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800919
920 // Create the oat file by copying the odex so they are located in the same
921 // place in memory.
922 Copy(odex_location, oat_location);
923
924 // Verify things don't go bad.
925 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe29d38e72016-03-23 15:31:51 +0000926 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800927
Andreas Gampe29d38e72016-03-23 15:31:51 +0000928 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
929 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800930
931 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
932 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
933 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
934 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
935 EXPECT_TRUE(oat_file_assistant.OatFileExists());
936 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
937 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700938 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800939
940 // Things aren't relocated, so it should fall back to interpreted.
941 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
942 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700943
Richard Uhler66d874d2015-01-15 09:37:19 -0800944 EXPECT_FALSE(oat_file->IsExecutable());
945 std::vector<std::unique_ptr<const DexFile>> dex_files;
946 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
947 EXPECT_EQ(1u, dex_files.size());
948}
949
950// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700951// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800952TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
953 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700954 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800955
956 // Create the dex and odex files
957 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000958 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800959
960 // Verify the status.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000961 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800962
Andreas Gampe29d38e72016-03-23 15:31:51 +0000963 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
964 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
965 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
966 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800967
968 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
969 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
970 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
971 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
972 EXPECT_FALSE(oat_file_assistant.OatFileExists());
973 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
974 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700975 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800976}
977
Andreas Gampe29d38e72016-03-23 15:31:51 +0000978// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
979// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
980TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
981 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
982 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000983
984 // Create the dex and odex files
985 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000986 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000987
988 // Verify the status.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000989 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000990
Andreas Gampe29d38e72016-03-23 15:31:51 +0000991 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
992 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
993 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
994 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000995
996 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
997 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
998 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
Nicolas Geoffray845e5062016-03-23 06:42:05 +0000999 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
David Brazdilce4b0ba2016-01-28 15:05:49 +00001000 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1001 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1002 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1003 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
1004}
1005
Richard Uhler66d874d2015-01-15 09:37:19 -08001006// Case: We have a DEX file and up-to-date OAT file for it.
1007// Expect: We should load an executable dex file.
1008TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
1009 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1010
1011 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001012 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001013
1014 // Load the oat using an oat file assistant.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001015 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
1016
1017 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1018 ASSERT_TRUE(oat_file.get() != nullptr);
1019 EXPECT_TRUE(oat_file->IsExecutable());
1020 std::vector<std::unique_ptr<const DexFile>> dex_files;
1021 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1022 EXPECT_EQ(1u, dex_files.size());
1023}
1024
1025// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
1026// Expect: We should still load the oat file as executable.
1027TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
1028 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1029
1030 Copy(GetDexSrc1(), dex_location);
1031 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
1032
1033 // Load the oat using an oat file assistant.
1034 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001035
1036 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1037 ASSERT_TRUE(oat_file.get() != nullptr);
1038 EXPECT_TRUE(oat_file->IsExecutable());
1039 std::vector<std::unique_ptr<const DexFile>> dex_files;
1040 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1041 EXPECT_EQ(1u, dex_files.size());
1042}
1043
1044// Case: We have a DEX file and up-to-date OAT file for it.
1045// Expect: Loading non-executable should load the oat non-executable.
1046TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
1047 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1048
1049 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001050 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001051
1052 // Load the oat using an oat file assistant.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001053 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001054
1055 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1056 ASSERT_TRUE(oat_file.get() != nullptr);
1057 EXPECT_FALSE(oat_file->IsExecutable());
1058 std::vector<std::unique_ptr<const DexFile>> dex_files;
1059 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1060 EXPECT_EQ(1u, dex_files.size());
1061}
1062
1063// Case: We have a DEX file.
1064// Expect: We should load an executable dex file from an alternative oat
1065// location.
1066TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
1067 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
1068 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
1069
1070 Copy(GetDexSrc1(), dex_location);
1071
1072 OatFileAssistant oat_file_assistant(
Andreas Gampe29d38e72016-03-23 15:31:51 +00001073 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001074 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001075 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001076 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001077 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -08001078
1079 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1080 ASSERT_TRUE(oat_file.get() != nullptr);
1081 EXPECT_TRUE(oat_file->IsExecutable());
1082 std::vector<std::unique_ptr<const DexFile>> dex_files;
1083 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1084 EXPECT_EQ(1u, dex_files.size());
1085
1086 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
1087
1088 // Verify it didn't create an oat in the default location.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001089 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001090 EXPECT_FALSE(ofm.OatFileExists());
1091}
1092
Richard Uhler8327cf72015-10-13 16:34:59 -07001093// Case: We have a DEX file but can't write the oat file.
1094// Expect: We should fail to make the oat file up to date.
1095TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
1096 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
1097
1098 // Make the oat location unwritable by inserting some non-existent
1099 // intermediate directories.
1100 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
1101
1102 Copy(GetDexSrc1(), dex_location);
1103
1104 OatFileAssistant oat_file_assistant(
Andreas Gampe29d38e72016-03-23 15:31:51 +00001105 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001106 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001107 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001108 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001109 oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001110
1111 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1112 ASSERT_TRUE(oat_file.get() == nullptr);
1113}
1114
1115// Case: We don't have a DEX file and can't write the oat file.
1116// Expect: We should fail to generate the oat file without crashing.
1117TEST_F(OatFileAssistantTest, GenNoDex) {
1118 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1119 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
1120
1121 OatFileAssistant oat_file_assistant(
Andreas Gampe29d38e72016-03-23 15:31:51 +00001122 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001123 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001124 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001125 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001126 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001127}
1128
Richard Uhler66d874d2015-01-15 09:37:19 -08001129// Turn an absolute path into a path relative to the current working
1130// directory.
1131static std::string MakePathRelative(std::string target) {
1132 char buf[MAXPATHLEN];
1133 std::string cwd = getcwd(buf, MAXPATHLEN);
1134
1135 // Split the target and cwd paths into components.
1136 std::vector<std::string> target_path;
1137 std::vector<std::string> cwd_path;
1138 Split(target, '/', &target_path);
1139 Split(cwd, '/', &cwd_path);
1140
1141 // Reverse the path components, so we can use pop_back().
1142 std::reverse(target_path.begin(), target_path.end());
1143 std::reverse(cwd_path.begin(), cwd_path.end());
1144
1145 // Drop the common prefix of the paths. Because we reversed the path
1146 // components, this becomes the common suffix of target_path and cwd_path.
1147 while (!target_path.empty() && !cwd_path.empty()
1148 && target_path.back() == cwd_path.back()) {
1149 target_path.pop_back();
1150 cwd_path.pop_back();
1151 }
1152
1153 // For each element of the remaining cwd_path, add '..' to the beginning
1154 // of the target path. Because we reversed the path components, we add to
1155 // the end of target_path.
1156 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1157 target_path.push_back("..");
1158 }
1159
1160 // Reverse again to get the right path order, and join to get the result.
1161 std::reverse(target_path.begin(), target_path.end());
1162 return Join(target_path, '/');
1163}
1164
1165// Case: Non-absolute path to Dex location.
1166// Expect: Not sure, but it shouldn't crash.
1167TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1168 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1169 Copy(GetDexSrc1(), abs_dex_location);
1170
1171 std::string dex_location = MakePathRelative(abs_dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001172 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001173
1174 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001175 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1176 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001177 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1178 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1179 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1180 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1181 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1182 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1183}
1184
1185// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001186// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001187TEST_F(OatFileAssistantTest, ShortDexLocation) {
1188 std::string dex_location = "/xx";
1189
Andreas Gampe29d38e72016-03-23 15:31:51 +00001190 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001191
1192 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001193 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1194 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001195 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1196 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1197 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1198 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1199 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1200 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001201 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001202
Richard Uhler9b994ea2015-06-24 08:44:19 -07001203 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001204 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001205 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001206 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001207 oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001208 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001209}
1210
1211// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001212// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001213TEST_F(OatFileAssistantTest, LongDexExtension) {
1214 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1215 Copy(GetDexSrc1(), dex_location);
1216
Andreas Gampe29d38e72016-03-23 15:31:51 +00001217 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001218
Andreas Gampe29d38e72016-03-23 15:31:51 +00001219 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1220 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001221
1222 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1223 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1224 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1225 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1226 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1227 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1228 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1229}
1230
1231// A task to generate a dex location. Used by the RaceToGenerate test.
1232class RaceGenerateTask : public Task {
1233 public:
1234 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001235 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001236 {}
1237
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001238 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001239 // Load the dex files, and save a pointer to the loaded oat file, so that
1240 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001241 std::vector<std::unique_ptr<const DexFile>> dex_files;
1242 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001243 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001244 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1245 dex_location_.c_str(),
1246 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001247 /*class_loader*/nullptr,
1248 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001249 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001250 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001251 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001252 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1253 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001254 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001255 }
1256
1257 const OatFile* GetLoadedOatFile() const {
1258 return loaded_oat_file_;
1259 }
1260
1261 private:
1262 std::string dex_location_;
1263 std::string oat_location_;
1264 const OatFile* loaded_oat_file_;
1265};
1266
1267// Test the case where multiple processes race to generate an oat file.
1268// This simulates multiple processes using multiple threads.
1269//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001270// We want unique Oat files to be loaded even when there is a race to load.
1271// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1272// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001273TEST_F(OatFileAssistantTest, RaceToGenerate) {
1274 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001275 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001276
1277 // We use the lib core dex file, because it's large, and hopefully should
1278 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001279 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001280
1281 const int kNumThreads = 32;
1282 Thread* self = Thread::Current();
1283 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1284 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1285 for (int i = 0; i < kNumThreads; i++) {
1286 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1287 thread_pool.AddTask(self, task.get());
1288 tasks.push_back(std::move(task));
1289 }
1290 thread_pool.StartWorkers(self);
1291 thread_pool.Wait(self, true, false);
1292
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001293 // Verify every task got a unique oat file.
1294 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001295 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001296 const OatFile* oat_file = task->GetLoadedOatFile();
1297 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1298 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001299 }
1300}
1301
1302// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1303// disabled.
1304// Expect: We should load the odex file non-executable.
1305TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1306 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001307 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001308
1309 // Create the dex and odex files
1310 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001311 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001312
1313 // Load the oat using an executable oat file assistant.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001314 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001315
1316 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1317 ASSERT_TRUE(oat_file.get() != nullptr);
1318 EXPECT_FALSE(oat_file->IsExecutable());
1319 std::vector<std::unique_ptr<const DexFile>> dex_files;
1320 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1321 EXPECT_EQ(1u, dex_files.size());
1322}
1323
1324// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1325// disabled.
1326// Expect: We should load the odex file non-executable.
1327TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1328 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001329 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001330
1331 // Create the dex and odex files
1332 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001333 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001334
1335 // Load the oat using an executable oat file assistant.
Andreas Gampe29d38e72016-03-23 15:31:51 +00001336 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001337
1338 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1339 ASSERT_TRUE(oat_file.get() != nullptr);
1340 EXPECT_FALSE(oat_file->IsExecutable());
1341 std::vector<std::unique_ptr<const DexFile>> dex_files;
1342 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1343 EXPECT_EQ(2u, dex_files.size());
1344}
1345
Richard Uhlerf4b34872016-04-13 11:03:46 -07001346TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1347 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1348 Copy(GetDexSrc1(), dex_location);
1349
1350 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
1351
1352 std::string error_msg;
1353 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1354 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
1355 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
1356 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1357 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1358 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1359 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1360
1361 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1362 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
1363 oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
1364 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1365 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1366 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1367 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1368
1369 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1370 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
1371 oat_file_assistant.MakeUpToDate(&error_msg));
1372}
1373
Richard Uhler66d874d2015-01-15 09:37:19 -08001374TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1375 std::string error_msg;
1376 std::string odex_file;
1377
1378 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001379 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001380 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001381
1382 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001383 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001384 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001385
1386 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001387 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001388 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001389 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001390}
1391
Richard Uhler23cedd22015-04-08 13:17:29 -07001392// Verify the dexopt status values from dalvik.system.DexFile
1393// match the OatFileAssistant::DexOptStatus values.
1394TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1395 ScopedObjectAccess soa(Thread::Current());
1396 StackHandleScope<1> hs(soa.Self());
1397 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1398 Handle<mirror::Class> dexfile(
1399 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1400 ASSERT_FALSE(dexfile.Get() == nullptr);
1401 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1402
Mathieu Chartierc7853442015-03-27 14:35:38 -07001403 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001404 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1405 ASSERT_FALSE(no_dexopt_needed == nullptr);
1406 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1407 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1408
Mathieu Chartierc7853442015-03-27 14:35:38 -07001409 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001410 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1411 ASSERT_FALSE(dex2oat_needed == nullptr);
1412 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1413 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1414
Mathieu Chartierc7853442015-03-27 14:35:38 -07001415 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001416 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1417 ASSERT_FALSE(patchoat_needed == nullptr);
1418 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1419 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1420
Mathieu Chartierc7853442015-03-27 14:35:38 -07001421 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001422 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1423 ASSERT_FALSE(self_patchoat_needed == nullptr);
1424 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1425 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1426}
Richard Uhler66d874d2015-01-15 09:37:19 -08001427
1428// TODO: More Tests:
Andreas Gampe29d38e72016-03-23 15:31:51 +00001429// * Image checksum change is out of date for kIntepretOnly, but not
1430// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1431// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001432// * Test class linker falls back to unquickened dex for DexNoOat
1433// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001434// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001435// * Test for status of oat while oat is being generated (how?)
1436// * Test case where 32 and 64 bit boot class paths differ,
1437// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1438// 64 bit boot class paths.
1439// * Test unexpected scenarios (?):
1440// - Dex is stripped, don't have odex.
1441// - Oat file corrupted after status check, before reload unexecutable
1442// because it's unrelocated and no dex2oat
Calin Juravleb077e152016-02-18 18:47:37 +00001443// * Test unrelocated specific target compilation type can be relocated to
1444// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001445
1446} // namespace art