blob: 7dcb36cad966c6bed31434ee57ca0c4195f19b85 [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 Uhler7dfe5de2016-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 Uhler7dfe5de2016-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 Uhler7dfe5de2016-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 Uhler7dfe5de2016-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 Uhler7dfe5de2016-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 Gampe7bcfcb82016-03-23 15:31:51 +0000219 const std::string& odex_location,
Richard Uhler7dfe5de2016-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 Uhler7dfe5de2016-04-26 10:24:38 -0700229 args.push_back("--oat-file=" + odex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000230 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhler7dfe5de2016-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 Uhler7dfe5de2016-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 Uhler7dfe5de2016-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 Chartierbcb6a722016-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 Uhler7dfe5de2016-04-26 10:24:38 -0700257 EXPECT_EQ(pic, odex_file->IsPic());
258 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000259 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
260
Vladimir Marko8c185bf2016-05-23 15:32:42 +0100261 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000262 const std::vector<gc::space::ImageSpace*> image_spaces =
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700263 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe7bcfcb82016-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 Hao4f351aa2016-04-07 15:40:54 -0700267 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
268 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
Andreas Gampe7bcfcb82016-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 Gampe7bcfcb82016-03-23 15:31:51 +0000276 const std::string& odex_location,
277 CompilerFilter::Filter filter) {
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700278 GenerateOdexForTest(dex_location, odex_location, filter, true, false);
Calin Juravled91b8a22016-02-18 18:47:37 +0000279 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000280
Richard Uhler1c4eb042016-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 Uhler1c4eb042016-03-29 13:27:41 -0700283 void GenerateNoPatchOdexForTest(const std::string& dex_location,
284 const std::string& odex_location,
285 CompilerFilter::Filter filter) {
Richard Uhler7dfe5de2016-04-26 10:24:38 -0700286 GenerateOdexForTest(dex_location, odex_location, filter, false, false);
Richard Uhler1c4eb042016-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 Uhler7dfe5de2016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Uhlerff0274b2016-03-30 12:17:55 -0700424 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
425 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700426
427 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800428 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
429 EXPECT_EQ(nullptr, oat_file.get());
430}
431
432// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700433// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800434TEST_F(OatFileAssistantTest, OatUpToDate) {
435 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
436 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000437 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800438
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000439 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800440
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000441 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
442 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
443 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
444 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
445 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
446 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
447 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
448 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
449
Richard Uhler66d874d2015-01-15 09:37:19 -0800450 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
451 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
452 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
453 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
454 EXPECT_TRUE(oat_file_assistant.OatFileExists());
455 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
456 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
457 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700458 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700459 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800460}
461
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000462// Case: We have a DEX file and speed-profile OAT file for it.
463// Expect: The status is kNoDexOptNeeded if the profile hasn't changed.
464TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
465 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
466 Copy(GetDexSrc1(), dex_location);
467 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
468
469 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
470
471 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
472 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
473 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
474 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
475
476 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
477 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
478 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
479 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
480 EXPECT_TRUE(oat_file_assistant.OatFileExists());
481 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
482 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
483 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
484 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
485 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
486}
487
488// Case: We have a DEX file and speed-profile OAT file for it.
489// Expect: The status is kNoDex2OatNeeded if the profile has changed.
490TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) {
491 std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar";
492 Copy(GetDexSrc1(), dex_location);
493 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
494
495 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false);
496
497 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
498 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
499 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
500 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
501
502 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
503 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
504 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
505 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
506 EXPECT_TRUE(oat_file_assistant.OatFileExists());
507 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
508 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
509 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
510 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
511 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
512}
513
Richard Uhler66d874d2015-01-15 09:37:19 -0800514// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700515// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800516TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
517 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
518 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000519 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800520
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000521 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
522 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
523 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700524 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700525
526 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700527 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800528 ASSERT_TRUE(oat_file.get() != nullptr);
529 EXPECT_TRUE(oat_file->IsExecutable());
530 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700531 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
532 EXPECT_EQ(2u, dex_files.size());
533}
534
Richard Uhler67ff7d12015-05-14 13:21:13 -0700535// Case: We have a MultiDEX file where the secondary dex file is out of date.
536// Expect: The status is kDex2OatNeeded.
537TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
538 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
539
540 // Compile code for GetMultiDexSrc1.
541 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000542 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700543
544 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
545 // is out of date.
546 Copy(GetMultiDexSrc2(), dex_location);
547
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000548 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
549 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
550 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700551 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700552}
553
Richard Uhlere5fed032015-03-18 08:21:11 -0700554// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
555// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700556// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700557TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
558 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700559 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700560
561 // Create the dex file
562 Copy(GetMultiDexSrc1(), dex_location);
563
564 // Create the oat file with relative encoded dex location.
565 std::vector<std::string> args;
566 args.push_back("--dex-file=" + dex_location);
567 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
568 args.push_back("--oat-file=" + oat_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000569 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700570
571 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700572 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700573
574 // Verify we can load both dex files.
575 OatFileAssistant oat_file_assistant(dex_location.c_str(),
576 oat_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000577 kRuntimeISA, false, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700578 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
579 ASSERT_TRUE(oat_file.get() != nullptr);
580 EXPECT_TRUE(oat_file->IsExecutable());
581 std::vector<std::unique_ptr<const DexFile>> dex_files;
582 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800583 EXPECT_EQ(2u, dex_files.size());
584}
585
Richard Uhler95abd042015-03-24 09:51:28 -0700586// Case: We have a DEX file and out-of-date OAT file.
587// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800588TEST_F(OatFileAssistantTest, OatOutOfDate) {
589 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
590
591 // We create a dex, generate an oat for it, then overwrite the dex with a
592 // different dex to make the oat out of date.
593 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000594 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800595 Copy(GetDexSrc2(), dex_location);
596
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000597 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
598 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
599 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
600 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
601 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800602
603 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
604 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
605 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
606 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
607 EXPECT_TRUE(oat_file_assistant.OatFileExists());
608 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
609 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700610 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800611}
612
613// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700614// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800615TEST_F(OatFileAssistantTest, DexOdexNoOat) {
616 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700617 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800618
619 // Create the dex and odex files
620 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000621 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800622
623 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000624 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800625
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000626 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
627 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
628 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
629 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800630
631 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
632 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
633 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
634 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
635 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800636 EXPECT_FALSE(oat_file_assistant.OatFileExists());
637 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
638 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700639 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700640
641 // We should still be able to get the non-executable odex file to run from.
642 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
643 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800644}
645
646// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700647// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800648TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
649 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700650 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800651
652 // Create the dex and odex files
653 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000654 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800655
656 // Strip the dex file
657 Copy(GetStrippedDexSrc1(), dex_location);
658
659 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000660 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800661
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000662 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
663 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800664
665 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
666 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
667 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
668 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
669 EXPECT_FALSE(oat_file_assistant.OatFileExists());
670 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
671 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700672 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800673
674 // Make the oat file up to date.
675 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700676 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
677 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800678
Andreas Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Gampe7bcfcb82016-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 Uhlerff0274b2016-03-30 12:17:55 -0700739 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
740 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800741
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000742 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
743 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
744 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
745 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800746
747 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
748 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
749 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
750 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
751 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
752 EXPECT_TRUE(oat_file_assistant.OatFileExists());
753 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
754 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
755 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700756 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800757
758 // Verify we can load the dex files from it.
759 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
760 ASSERT_TRUE(oat_file.get() != nullptr);
761 EXPECT_TRUE(oat_file->IsExecutable());
762 std::vector<std::unique_ptr<const DexFile>> dex_files;
763 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
764 EXPECT_EQ(1u, dex_files.size());
765}
766
Richard Uhler9b994ea2015-06-24 08:44:19 -0700767// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
768// OAT file. Expect: The status is kNoDexOptNeeded.
769TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
770 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
771
772 Copy(GetStrippedDexSrc1(), dex_location);
773
774 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000775 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700776
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000777 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
778 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
779 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
780 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
781 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
782 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700783
784 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
785 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
786 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
787 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
788 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
789 EXPECT_FALSE(oat_file_assistant.OatFileExists());
790 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
791 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
792 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
793
794 // Make the oat file up to date. This should have no effect.
795 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700796 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
797 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700798
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000799 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
800 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700801
802 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
803 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
804 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
805 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
806 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
807 EXPECT_FALSE(oat_file_assistant.OatFileExists());
808 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
809 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
810 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
811}
812
Richard Uhler95abd042015-03-24 09:51:28 -0700813// Case: We have a DEX file, no ODEX file and an OAT file that needs
814// relocation.
815// Expect: The status is kSelfPatchOatNeeded.
816TEST_F(OatFileAssistantTest, SelfRelocation) {
817 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
818 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
819
820 // Create the dex and odex files
821 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000822 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700823
824 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000825 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700826
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000827 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
828 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
829 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
830 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
831 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
832 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700833
834 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
835 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
836 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
837 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
838 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
839 EXPECT_TRUE(oat_file_assistant.OatFileExists());
840 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
841 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
842 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700843 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700844
845 // Make the oat file up to date.
846 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700847 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
848 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700849
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000850 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
851 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700852
853 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
854 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
855 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
856 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
857 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
858 EXPECT_TRUE(oat_file_assistant.OatFileExists());
859 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
860 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
861 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700862 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700863
864 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
865 ASSERT_TRUE(oat_file.get() != nullptr);
866 EXPECT_TRUE(oat_file->IsExecutable());
867 std::vector<std::unique_ptr<const DexFile>> dex_files;
868 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
869 EXPECT_EQ(1u, dex_files.size());
870}
871
Richard Uhler1c4eb042016-03-29 13:27:41 -0700872// Case: We have a DEX file, no ODEX file and an OAT file that needs
873// relocation but doesn't have patch info.
874// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
875TEST_F(OatFileAssistantTest, NoSelfRelocation) {
876 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
877 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
878
879 // Create the dex and odex files
880 Copy(GetDexSrc1(), dex_location);
881 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
882
883 OatFileAssistant oat_file_assistant(dex_location.c_str(),
884 oat_location.c_str(), kRuntimeISA, false, true);
885
886 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
887 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
888
889 // Make the oat file up to date.
890 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -0700891 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
892 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler1c4eb042016-03-29 13:27:41 -0700893 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
894 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
895
896 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
897 ASSERT_TRUE(oat_file.get() != nullptr);
898 EXPECT_TRUE(oat_file->IsExecutable());
899 std::vector<std::unique_ptr<const DexFile>> dex_files;
900 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
901 EXPECT_EQ(1u, dex_files.size());
902}
903
Richard Uhler66d874d2015-01-15 09:37:19 -0800904// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
905// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700906// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800907TEST_F(OatFileAssistantTest, OdexOatOverlap) {
908 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700909 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
910 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800911
912 // Create the dex and odex files
913 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000914 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800915
916 // Create the oat file by copying the odex so they are located in the same
917 // place in memory.
918 Copy(odex_location, oat_location);
919
920 // Verify things don't go bad.
921 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000922 oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800923
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000924 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
925 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800926
927 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
928 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
929 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
930 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
931 EXPECT_TRUE(oat_file_assistant.OatFileExists());
932 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
933 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700934 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800935
936 // Things aren't relocated, so it should fall back to interpreted.
937 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
938 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700939
Richard Uhler66d874d2015-01-15 09:37:19 -0800940 EXPECT_FALSE(oat_file->IsExecutable());
941 std::vector<std::unique_ptr<const DexFile>> dex_files;
942 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
943 EXPECT_EQ(1u, dex_files.size());
944}
945
946// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700947// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800948TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
949 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700950 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800951
952 // Create the dex and odex files
953 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000954 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800955
956 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000957 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800958
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000959 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
960 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
961 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
962 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800963
964 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
965 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
966 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
967 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
968 EXPECT_FALSE(oat_file_assistant.OatFileExists());
969 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
970 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700971 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800972}
973
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000974// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
975// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
976TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
977 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
978 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000979
980 // Create the dex and odex files
981 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000982 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000983
984 // Verify the status.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000985 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000986
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000987 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
988 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
989 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
990 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000991
992 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
993 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
994 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
995 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
996 EXPECT_FALSE(oat_file_assistant.OatFileExists());
997 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
998 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
999 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
1000}
1001
Richard Uhler66d874d2015-01-15 09:37:19 -08001002// Case: We have a DEX file and up-to-date OAT file for it.
1003// Expect: We should load an executable dex file.
1004TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
1005 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
1006
1007 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001008 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001009
1010 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001011 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
1012
1013 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1014 ASSERT_TRUE(oat_file.get() != nullptr);
1015 EXPECT_TRUE(oat_file->IsExecutable());
1016 std::vector<std::unique_ptr<const DexFile>> dex_files;
1017 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1018 EXPECT_EQ(1u, dex_files.size());
1019}
1020
1021// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
1022// Expect: We should still load the oat file as executable.
1023TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
1024 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
1025
1026 Copy(GetDexSrc1(), dex_location);
1027 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
1028
1029 // Load the oat using an oat file assistant.
1030 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001031
1032 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1033 ASSERT_TRUE(oat_file.get() != nullptr);
1034 EXPECT_TRUE(oat_file->IsExecutable());
1035 std::vector<std::unique_ptr<const DexFile>> dex_files;
1036 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1037 EXPECT_EQ(1u, dex_files.size());
1038}
1039
1040// Case: We have a DEX file and up-to-date OAT file for it.
1041// Expect: Loading non-executable should load the oat non-executable.
1042TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
1043 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
1044
1045 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001046 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001047
1048 // Load the oat using an oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001049 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001050
1051 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1052 ASSERT_TRUE(oat_file.get() != nullptr);
1053 EXPECT_FALSE(oat_file->IsExecutable());
1054 std::vector<std::unique_ptr<const DexFile>> dex_files;
1055 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1056 EXPECT_EQ(1u, dex_files.size());
1057}
1058
1059// Case: We have a DEX file.
1060// Expect: We should load an executable dex file from an alternative oat
1061// location.
1062TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
1063 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
1064 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
1065
1066 Copy(GetDexSrc1(), dex_location);
1067
1068 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001069 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001070 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001071 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
1072 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -08001073
1074 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1075 ASSERT_TRUE(oat_file.get() != nullptr);
1076 EXPECT_TRUE(oat_file->IsExecutable());
1077 std::vector<std::unique_ptr<const DexFile>> dex_files;
1078 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1079 EXPECT_EQ(1u, dex_files.size());
1080
1081 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
1082
1083 // Verify it didn't create an oat in the default location.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001084 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001085 EXPECT_FALSE(ofm.OatFileExists());
1086}
1087
Richard Uhler8327cf72015-10-13 16:34:59 -07001088// Case: We have a DEX file but can't write the oat file.
1089// Expect: We should fail to make the oat file up to date.
1090TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
1091 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
1092
1093 // Make the oat location unwritable by inserting some non-existent
1094 // intermediate directories.
1095 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
1096
1097 Copy(GetDexSrc1(), dex_location);
1098
1099 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001100 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001101 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001102 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
1103 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001104
1105 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1106 ASSERT_TRUE(oat_file.get() == nullptr);
1107}
1108
1109// Case: We don't have a DEX file and can't write the oat file.
1110// Expect: We should fail to generate the oat file without crashing.
1111TEST_F(OatFileAssistantTest, GenNoDex) {
1112 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1113 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
1114
1115 OatFileAssistant oat_file_assistant(
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001116 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001117 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001118 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
1119 oat_file_assistant.GenerateOatFile(CompilerFilter::kSpeed, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001120}
1121
Richard Uhler66d874d2015-01-15 09:37:19 -08001122// Turn an absolute path into a path relative to the current working
1123// directory.
1124static std::string MakePathRelative(std::string target) {
1125 char buf[MAXPATHLEN];
1126 std::string cwd = getcwd(buf, MAXPATHLEN);
1127
1128 // Split the target and cwd paths into components.
1129 std::vector<std::string> target_path;
1130 std::vector<std::string> cwd_path;
1131 Split(target, '/', &target_path);
1132 Split(cwd, '/', &cwd_path);
1133
1134 // Reverse the path components, so we can use pop_back().
1135 std::reverse(target_path.begin(), target_path.end());
1136 std::reverse(cwd_path.begin(), cwd_path.end());
1137
1138 // Drop the common prefix of the paths. Because we reversed the path
1139 // components, this becomes the common suffix of target_path and cwd_path.
1140 while (!target_path.empty() && !cwd_path.empty()
1141 && target_path.back() == cwd_path.back()) {
1142 target_path.pop_back();
1143 cwd_path.pop_back();
1144 }
1145
1146 // For each element of the remaining cwd_path, add '..' to the beginning
1147 // of the target path. Because we reversed the path components, we add to
1148 // the end of target_path.
1149 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1150 target_path.push_back("..");
1151 }
1152
1153 // Reverse again to get the right path order, and join to get the result.
1154 std::reverse(target_path.begin(), target_path.end());
1155 return Join(target_path, '/');
1156}
1157
1158// Case: Non-absolute path to Dex location.
1159// Expect: Not sure, but it shouldn't crash.
1160TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1161 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1162 Copy(GetDexSrc1(), abs_dex_location);
1163
1164 std::string dex_location = MakePathRelative(abs_dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001165 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001166
1167 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001168 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1169 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001170 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1171 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1172 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1173 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1174 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1175 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1176}
1177
1178// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001179// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001180TEST_F(OatFileAssistantTest, ShortDexLocation) {
1181 std::string dex_location = "/xx";
1182
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001183 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001184
1185 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001186 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1187 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001188 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1189 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1190 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1191 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1192 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1193 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001194 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001195
Richard Uhler9b994ea2015-06-24 08:44:19 -07001196 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001197 std::string error_msg;
Richard Uhlerff0274b2016-03-30 12:17:55 -07001198 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
1199 oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001200 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001201}
1202
1203// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001204// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001205TEST_F(OatFileAssistantTest, LongDexExtension) {
1206 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1207 Copy(GetDexSrc1(), dex_location);
1208
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001209 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001210
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001211 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1212 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001213
1214 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1215 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1216 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1217 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1218 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1219 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1220 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1221}
1222
1223// A task to generate a dex location. Used by the RaceToGenerate test.
1224class RaceGenerateTask : public Task {
1225 public:
1226 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Hao5872d7c2016-04-27 11:07:41 -07001227 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001228 {}
1229
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001230 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001231 // Load the dex files, and save a pointer to the loaded oat file, so that
1232 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001233 std::vector<std::unique_ptr<const DexFile>> dex_files;
1234 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001235 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001236 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1237 dex_location_.c_str(),
1238 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001239 /*class_loader*/nullptr,
1240 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001241 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001242 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001243 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001244 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1245 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001246 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001247 }
1248
1249 const OatFile* GetLoadedOatFile() const {
1250 return loaded_oat_file_;
1251 }
1252
1253 private:
1254 std::string dex_location_;
1255 std::string oat_location_;
1256 const OatFile* loaded_oat_file_;
1257};
1258
1259// Test the case where multiple processes race to generate an oat file.
1260// This simulates multiple processes using multiple threads.
1261//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001262// We want unique Oat files to be loaded even when there is a race to load.
1263// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1264// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001265TEST_F(OatFileAssistantTest, RaceToGenerate) {
1266 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001267 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001268
1269 // We use the lib core dex file, because it's large, and hopefully should
1270 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001271 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001272
1273 const int kNumThreads = 32;
1274 Thread* self = Thread::Current();
1275 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1276 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1277 for (int i = 0; i < kNumThreads; i++) {
1278 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1279 thread_pool.AddTask(self, task.get());
1280 tasks.push_back(std::move(task));
1281 }
1282 thread_pool.StartWorkers(self);
1283 thread_pool.Wait(self, true, false);
1284
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001285 // Verify every task got a unique oat file.
1286 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001287 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001288 const OatFile* oat_file = task->GetLoadedOatFile();
1289 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1290 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001291 }
1292}
1293
1294// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1295// disabled.
1296// Expect: We should load the odex file non-executable.
1297TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1298 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001299 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001300
1301 // Create the dex and odex files
1302 Copy(GetDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001303 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001304
1305 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001306 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001307
1308 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1309 ASSERT_TRUE(oat_file.get() != nullptr);
1310 EXPECT_FALSE(oat_file->IsExecutable());
1311 std::vector<std::unique_ptr<const DexFile>> dex_files;
1312 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1313 EXPECT_EQ(1u, dex_files.size());
1314}
1315
1316// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1317// disabled.
1318// Expect: We should load the odex file non-executable.
1319TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1320 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001321 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001322
1323 // Create the dex and odex files
1324 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001325 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001326
1327 // Load the oat using an executable oat file assistant.
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001328 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001329
1330 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1331 ASSERT_TRUE(oat_file.get() != nullptr);
1332 EXPECT_FALSE(oat_file->IsExecutable());
1333 std::vector<std::unique_ptr<const DexFile>> dex_files;
1334 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1335 EXPECT_EQ(2u, dex_files.size());
1336}
1337
1338TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1339 std::string error_msg;
1340 std::string odex_file;
1341
1342 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001343 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001344 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001345
1346 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001347 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001348 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001349
1350 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001351 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001352 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001353 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001354}
1355
Richard Uhler23cedd22015-04-08 13:17:29 -07001356// Verify the dexopt status values from dalvik.system.DexFile
1357// match the OatFileAssistant::DexOptStatus values.
1358TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1359 ScopedObjectAccess soa(Thread::Current());
1360 StackHandleScope<1> hs(soa.Self());
1361 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1362 Handle<mirror::Class> dexfile(
1363 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1364 ASSERT_FALSE(dexfile.Get() == nullptr);
1365 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1366
Mathieu Chartierc7853442015-03-27 14:35:38 -07001367 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001368 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1369 ASSERT_FALSE(no_dexopt_needed == nullptr);
1370 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1371 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1372
Mathieu Chartierc7853442015-03-27 14:35:38 -07001373 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001374 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1375 ASSERT_FALSE(dex2oat_needed == nullptr);
1376 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1377 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1378
Mathieu Chartierc7853442015-03-27 14:35:38 -07001379 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001380 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1381 ASSERT_FALSE(patchoat_needed == nullptr);
1382 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1383 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1384
Mathieu Chartierc7853442015-03-27 14:35:38 -07001385 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001386 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1387 ASSERT_FALSE(self_patchoat_needed == nullptr);
1388 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1389 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1390}
Richard Uhler66d874d2015-01-15 09:37:19 -08001391
1392// TODO: More Tests:
Andreas Gampe7bcfcb82016-03-23 15:31:51 +00001393// * Image checksum change is out of date for kIntepretOnly, but not
1394// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1395// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001396// * Test class linker falls back to unquickened dex for DexNoOat
1397// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001398// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001399// * Test for status of oat while oat is being generated (how?)
1400// * Test case where 32 and 64 bit boot class paths differ,
1401// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1402// 64 bit boot class paths.
1403// * Test unexpected scenarios (?):
1404// - Dex is stripped, don't have odex.
1405// - Oat file corrupted after status check, before reload unexecutable
1406// because it's unrelocated and no dex2oat
Calin Juravled91b8a22016-02-18 18:47:37 +00001407// * Test unrelocated specific target compilation type can be relocated to
1408// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001409
1410} // namespace art