blob: 26dbaab36709c1f61e2eedfff4157105ca47ccc4 [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
Richard Uhler66d874d2015-01-15 09:37:19 -080017#include <algorithm>
18#include <fstream>
19#include <string>
20#include <vector>
21#include <sys/param.h>
22
Andreas Gampe9186ced2016-12-12 14:28:21 -080023#include "android-base/strings.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080024#include <backtrace/BacktraceMap.h>
25#include <gtest/gtest.h>
26
Mathieu Chartierc7853442015-03-27 14:35:38 -070027#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010028#include "class_linker-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080029#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070030#include "compiler_callbacks.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070031#include "dex2oat_environment_test.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"
Andreas Gampee1459ae2016-06-29 09:36:30 -070034#include "oat_file_assistant.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070035#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070037#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080038#include "thread-inl.h"
39#include "utils.h"
40
41namespace art {
42
Andreas Gampee1459ae2016-06-29 09:36:30 -070043class OatFileAssistantTest : public Dex2oatEnvironmentTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080044 public:
Andreas Gampee1459ae2016-06-29 09:36:30 -070045 virtual void SetUp() OVERRIDE {
Richard Uhler66d874d2015-01-15 09:37:19 -080046 ReserveImageSpace();
Andreas Gampee1459ae2016-06-29 09:36:30 -070047 Dex2oatEnvironmentTest::SetUp();
Richard Uhler66d874d2015-01-15 09:37:19 -080048 }
49
Richard Uhlera48403e2016-04-26 10:24:38 -070050 // Pre-Relocate the image to a known non-zero offset so we don't have to
51 // deal with the runtime randomly relocating the image by 0 and messing up
52 // the expected results of the tests.
Richard Uhler03bc6592016-11-22 09:42:04 +000053 bool PreRelocateImage(const std::string& image_location, std::string* error_msg) {
Richard Uhlera48403e2016-04-26 10:24:38 -070054 std::string image;
Richard Uhler03bc6592016-11-22 09:42:04 +000055 if (!GetCachedImageFile(image_location, &image, error_msg)) {
Richard Uhlera48403e2016-04-26 10:24:38 -070056 return false;
57 }
58
59 std::string patchoat = GetAndroidRoot();
60 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
61
62 std::vector<std::string> argv;
63 argv.push_back(patchoat);
Richard Uhler03bc6592016-11-22 09:42:04 +000064 argv.push_back("--input-image-location=" + image_location);
Richard Uhlera48403e2016-04-26 10:24:38 -070065 argv.push_back("--output-image-file=" + image);
66 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
67 argv.push_back("--base-offset-delta=0x00008000");
68 return Exec(argv, error_msg);
69 }
70
Richard Uhler66d874d2015-01-15 09:37:19 -080071 virtual void PreRuntimeCreate() {
Richard Uhlera48403e2016-04-26 10:24:38 -070072 std::string error_msg;
Richard Uhler03bc6592016-11-22 09:42:04 +000073 ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg;
74 ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -080075 UnreserveImageSpace();
76 }
77
Andreas Gampee1459ae2016-06-29 09:36:30 -070078 virtual void PostRuntimeCreate() OVERRIDE {
Richard Uhler66d874d2015-01-15 09:37:19 -080079 ReserveImageSpace();
80 }
81
Richard Uhler03bc6592016-11-22 09:42:04 +000082 // Generate an oat file for the purposes of test.
83 void GenerateOatForTest(const std::string& dex_location,
84 const std::string& oat_location,
85 CompilerFilter::Filter filter,
86 bool relocate,
87 bool pic,
88 bool with_patch_info,
89 bool with_alternate_image) {
David Sehrd106d9f2016-08-16 19:22:57 -070090 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
91 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
Richard Uhler03bc6592016-11-22 09:42:04 +000092
93 if (!relocate) {
94 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
95 // relocated image file.
96 ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno);
97 }
David Sehrd106d9f2016-08-16 19:22:57 -070098
Richard Uhler93aa2102015-08-10 14:47:41 -070099 std::vector<std::string> args;
100 args.push_back("--dex-file=" + dex_location);
Richard Uhler03bc6592016-11-22 09:42:04 +0000101 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000102 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhlera48403e2016-04-26 10:24:38 -0700103 args.push_back("--runtime-arg");
Richard Uhler03bc6592016-11-22 09:42:04 +0000104
105 // Use -Xnorelocate regardless of the relocate argument.
106 // We control relocation by redirecting the dalvik cache when needed
107 // rather than use this flag.
Richard Uhlera48403e2016-04-26 10:24:38 -0700108 args.push_back("-Xnorelocate");
Richard Uhler93aa2102015-08-10 14:47:41 -0700109
Richard Uhlera48403e2016-04-26 10:24:38 -0700110 if (pic) {
111 args.push_back("--compile-pic");
112 }
113
114 if (with_patch_info) {
115 args.push_back("--include-patch-information");
116 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700117
Richard Uhler03bc6592016-11-22 09:42:04 +0000118 std::string image_location = GetImageLocation();
119 if (with_alternate_image) {
120 args.push_back("--boot-image=" + GetImageLocation2());
121 }
122
Richard Uhler93aa2102015-08-10 14:47:41 -0700123 std::string error_msg;
124 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700125
Richard Uhler03bc6592016-11-22 09:42:04 +0000126 if (!relocate) {
127 // Restore the dalvik cache if needed.
128 ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno);
129 }
130
131 // Verify the odex file was generated as expected.
132 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(),
133 oat_location.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800134 nullptr,
135 nullptr,
136 false,
137 /*low_4gb*/false,
138 dex_location.c_str(),
139 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700140 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700141 EXPECT_EQ(pic, odex_file->IsPic());
142 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000143 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
144
Richard Uhler03bc6592016-11-22 09:42:04 +0000145 std::unique_ptr<ImageHeader> image_header(
146 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
147 kRuntimeISA,
148 &error_msg));
149 ASSERT_TRUE(image_header != nullptr) << error_msg;
150 const OatHeader& oat_header = odex_file->GetOatHeader();
151 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
152
153 if (CompilerFilter::DependsOnImageChecksum(filter)) {
154 if (with_alternate_image) {
155 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
156 } else {
157 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
158 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000159 }
Richard Uhler03bc6592016-11-22 09:42:04 +0000160
161 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
162 if (relocate) {
163 EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
164 oat_header.GetImageFileLocationOatDataBegin());
165 EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
166 } else {
167 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
168 oat_header.GetImageFileLocationOatDataBegin());
169 EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
170 }
171 }
172 }
173
174 // Generate a non-PIC odex file for the purposes of test.
175 // The generated odex file will be un-relocated.
176 void GenerateOdexForTest(const std::string& dex_location,
177 const std::string& odex_location,
178 CompilerFilter::Filter filter) {
179 GenerateOatForTest(dex_location,
180 odex_location,
181 filter,
182 /*relocate*/false,
183 /*pic*/false,
184 /*with_patch_info*/true,
185 /*with_alternate_image*/false);
Richard Uhler93aa2102015-08-10 14:47:41 -0700186 }
187
188 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000189 const std::string& odex_location,
190 CompilerFilter::Filter filter) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000191 GenerateOatForTest(dex_location,
192 odex_location,
193 filter,
194 /*relocate*/false,
195 /*pic*/true,
196 /*with_patch_info*/false,
197 /*with_alternate_image*/false);
Calin Juravleb077e152016-02-18 18:47:37 +0000198 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000199
Richard Uhlerd1537b52016-03-29 13:27:41 -0700200 // Generate a non-PIC odex file without patch information for the purposes
201 // of test. The generated odex file will be un-relocated.
Richard Uhlerd1537b52016-03-29 13:27:41 -0700202 void GenerateNoPatchOdexForTest(const std::string& dex_location,
203 const std::string& odex_location,
204 CompilerFilter::Filter filter) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000205 GenerateOatForTest(dex_location,
206 odex_location,
207 filter,
208 /*relocate*/false,
209 /*pic*/false,
210 /*with_patch_info*/false,
211 /*with_alternate_image*/false);
212 }
213
214 // Generate an oat file in the oat location.
215 void GenerateOatForTest(const char* dex_location,
216 CompilerFilter::Filter filter,
217 bool relocate,
218 bool pic,
219 bool with_patch_info,
220 bool with_alternate_image) {
221 std::string oat_location;
222 std::string error_msg;
223 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
224 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
225 GenerateOatForTest(dex_location,
226 oat_location,
227 filter,
228 relocate,
229 pic,
230 with_patch_info,
231 with_alternate_image);
232 }
233
234 // Generate a standard oat file in the oat location.
235 void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
236 GenerateOatForTest(dex_location,
237 filter,
238 /*relocate*/true,
239 /*pic*/false,
240 /*with_patch_info*/false,
241 /*with_alternate_image*/false);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700242 }
243
Richard Uhler66d874d2015-01-15 09:37:19 -0800244 private:
245 // Reserve memory around where the image will be loaded so other memory
246 // won't conflict when it comes time to load the image.
247 // This can be called with an already loaded image to reserve the space
248 // around it.
249 void ReserveImageSpace() {
250 MemMap::Init();
251
252 // Ensure a chunk of memory is reserved for the image space.
Richard Uhlera48403e2016-04-26 10:24:38 -0700253 // The reservation_end includes room for the main space that has to come
254 // right after the image in case of the GSS collector.
255 uintptr_t reservation_start = ART_BASE_ADDRESS;
256 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800257
Richard Uhler66d874d2015-01-15 09:37:19 -0800258 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
259 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
260 for (BacktraceMap::const_iterator it = map->begin();
261 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700262 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
263 reservation_start = std::max(reservation_start, it->end);
264 }
265 ReserveImageSpaceChunk(reservation_start, reservation_end);
266 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800267
Richard Uhler3efe9792015-03-30 16:18:03 -0700268 // Reserve a chunk of memory for the image space in the given range.
269 // Only has effect for chunks with a positive number of bytes.
270 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
271 if (start < end) {
272 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800273 image_reservation_.push_back(std::unique_ptr<MemMap>(
274 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700275 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700276 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800277 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
278 LOG(INFO) << "Reserved space for image " <<
279 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
280 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800281 }
282 }
283
284
285 // Unreserve any memory reserved by ReserveImageSpace. This should be called
286 // before the image is loaded.
287 void UnreserveImageSpace() {
288 image_reservation_.clear();
289 }
290
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 std::vector<std::unique_ptr<MemMap>> image_reservation_;
292};
293
294class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
295 public:
296 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
297 OatFileAssistantTest::SetUpRuntimeOptions(options);
298 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
299 }
300};
301
Richard Uhler66d874d2015-01-15 09:37:19 -0800302// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700303// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800304TEST_F(OatFileAssistantTest, DexNoOat) {
305 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
306 Copy(GetDexSrc1(), dex_location);
307
Richard Uhlerd1472a22016-04-15 15:18:56 -0700308 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800309
Richard Uhler7225a8d2016-11-22 10:12:03 +0000310 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000311 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000312 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000313 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000314 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000315 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000316 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000317 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800318
319 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000320 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
321 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700322 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800323}
324
325// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700326// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800327TEST_F(OatFileAssistantTest, NoDexNoOat) {
328 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
329
Richard Uhlerd1472a22016-04-15 15:18:56 -0700330 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800331
Andreas Gampe29d38e72016-03-23 15:31:51 +0000332 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
333 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700334 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
335
336 // Trying to make the oat file up to date should not fail or crash.
337 std::string error_msg;
Richard Uhlerd1472a22016-04-15 15:18:56 -0700338 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700339
340 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800341 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
342 EXPECT_EQ(nullptr, oat_file.get());
343}
344
345// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700346// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800347TEST_F(OatFileAssistantTest, OatUpToDate) {
348 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
349 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000350 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800351
Richard Uhlerd1472a22016-04-15 15:18:56 -0700352 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800353
Andreas Gampe29d38e72016-03-23 15:31:51 +0000354 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
355 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
356 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
357 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
358 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
359 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000360 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000361 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
362
Richard Uhler66d874d2015-01-15 09:37:19 -0800363 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000364 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler95abd042015-03-24 09:51:28 -0700365 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700366 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800367}
368
Richard Uhler9a37efc2016-08-05 16:32:55 -0700369// Case: We have a DEX file and ODEX file for a different dex location.
370// Expect: The status is kDex2OatNeeded.
371TEST_F(OatFileAssistantTest, OatForDifferentDex) {
372 // Generate an odex file for OatForDifferentDex_A.jar
373 std::string dex_location_a = GetScratchDir() + "/OatForDifferentDex_A.jar";
374 std::string odex_location = GetOdexDir() + "/OatForDifferentDex.odex";
375 Copy(GetDexSrc1(), dex_location_a);
376 GenerateOdexForTest(dex_location_a, odex_location, CompilerFilter::kSpeed);
377
378 // Try to use that odex file for OatForDifferentDex.jar
379 std::string dex_location = GetScratchDir() + "/OatForDifferentDex.jar";
380 Copy(GetDexSrc1(), dex_location);
381
382 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
383
Richard Uhler7225a8d2016-11-22 10:12:03 +0000384 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700385 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
386
387 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000388 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OdexFileStatus());
389 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700390}
391
Andreas Gampe29d38e72016-03-23 15:31:51 +0000392// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700393// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
394// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000395TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
396 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
397 Copy(GetDexSrc1(), dex_location);
398 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
399
Richard Uhlerd1472a22016-04-15 15:18:56 -0700400 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000401
402 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700403 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000404 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700405 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000406 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700407 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000408 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700409 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000410
411 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000412 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000413 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
414 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
415}
416
Richard Uhler66d874d2015-01-15 09:37:19 -0800417// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700418// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800419TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
420 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
421 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000422 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800423
Richard Uhlerd1472a22016-04-15 15:18:56 -0700424 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000425 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700426 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700427 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700428
429 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700430 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800431 ASSERT_TRUE(oat_file.get() != nullptr);
432 EXPECT_TRUE(oat_file->IsExecutable());
433 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700434 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
435 EXPECT_EQ(2u, dex_files.size());
436}
437
Richard Uhler67ff7d12015-05-14 13:21:13 -0700438// Case: We have a MultiDEX file where the secondary dex file is out of date.
439// Expect: The status is kDex2OatNeeded.
440TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
441 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
442
443 // Compile code for GetMultiDexSrc1.
444 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000445 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700446
447 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
448 // is out of date.
449 Copy(GetMultiDexSrc2(), dex_location);
450
Richard Uhlerd1472a22016-04-15 15:18:56 -0700451 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000452 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700453 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700454 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700455}
456
Richard Uhlere5fed032015-03-18 08:21:11 -0700457// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
458// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700459// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700460TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
461 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700462 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700463
464 // Create the dex file
465 Copy(GetMultiDexSrc1(), dex_location);
466
467 // Create the oat file with relative encoded dex location.
468 std::vector<std::string> args;
469 args.push_back("--dex-file=" + dex_location);
470 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
471 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000472 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700473
474 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700475 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700476
477 // Verify we can load both dex files.
478 OatFileAssistant oat_file_assistant(dex_location.c_str(),
479 oat_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700480 kRuntimeISA, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700481 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
482 ASSERT_TRUE(oat_file.get() != nullptr);
483 EXPECT_TRUE(oat_file->IsExecutable());
484 std::vector<std::unique_ptr<const DexFile>> dex_files;
485 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800486 EXPECT_EQ(2u, dex_files.size());
487}
488
Richard Uhler03bc6592016-11-22 09:42:04 +0000489// Case: We have a DEX file and an OAT file out of date with respect to the
490// dex checksum.
491TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
492 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
494 // We create a dex, generate an oat for it, then overwrite the dex with a
495 // different dex to make the oat out of date.
496 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000497 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 Copy(GetDexSrc2(), dex_location);
499
Richard Uhlerd1472a22016-04-15 15:18:56 -0700500 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000501 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000502 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000503 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000504 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800505
506 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000507 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
508 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
509 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
510}
511
512// Case: We have a DEX file and an OAT file out of date with respect to the
513// boot image.
514TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
515 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
516
517 Copy(GetDexSrc1(), dex_location);
518 GenerateOatForTest(dex_location.c_str(),
519 CompilerFilter::kSpeed,
520 /*relocate*/true,
521 /*pic*/false,
522 /*with_patch_info*/false,
523 /*with_alternate_image*/true);
524
525 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000526 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000527 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000528 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000529 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000530 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000531 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
532
533 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
534 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
535 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
536 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
537}
538
539// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
540// respect to the boot image.
541// It shouldn't matter that the OAT file is out of date, because it is
542// verify-at-runtime.
543TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
544 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
545
546 Copy(GetDexSrc1(), dex_location);
547 GenerateOatForTest(dex_location.c_str(),
548 CompilerFilter::kVerifyAtRuntime,
549 /*relocate*/true,
550 /*pic*/false,
551 /*with_patch_info*/false,
552 /*with_alternate_image*/true);
553
554 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
555 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
556 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000557 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhler03bc6592016-11-22 09:42:04 +0000558 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
559
560 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
561 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
562 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700563 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800564}
565
566// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700567// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800568TEST_F(OatFileAssistantTest, DexOdexNoOat) {
569 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700570 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800571
572 // Create the dex and odex files
573 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000574 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800575
576 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700577 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800578
Andreas Gampe29d38e72016-03-23 15:31:51 +0000579 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
580 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000581 EXPECT_EQ(-OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000582 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800583
584 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000585 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
586 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700587 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700588
589 // We should still be able to get the non-executable odex file to run from.
590 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
591 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800592}
593
594// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700595// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800596TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
597 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700598 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800599
600 // Create the dex and odex files
601 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000602 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800603
604 // Strip the dex file
605 Copy(GetStrippedDexSrc1(), dex_location);
606
607 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700608 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800609
Richard Uhler7225a8d2016-11-22 10:12:03 +0000610 EXPECT_EQ(-OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000611 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800612
613 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000614 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
615 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700616 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800617
618 // Make the oat file up to date.
619 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700620 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700621 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700622 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800623
Andreas Gampe29d38e72016-03-23 15:31:51 +0000624 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
625 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800626
627 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000628 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000629 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700630 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800631
632 // Verify we can load the dex files from it.
633 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
634 ASSERT_TRUE(oat_file.get() != nullptr);
635 EXPECT_TRUE(oat_file->IsExecutable());
636 std::vector<std::unique_ptr<const DexFile>> dex_files;
637 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
638 EXPECT_EQ(1u, dex_files.size());
639}
640
Richard Uhler95abd042015-03-24 09:51:28 -0700641// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
642// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800643TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
644 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700645 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800646
647 // Create the oat file from a different dex file so it looks out of date.
648 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000649 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800650
651 // Create the odex file
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.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700659 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800660
Andreas Gampe29d38e72016-03-23 15:31:51 +0000661 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
662 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000663 EXPECT_EQ(-OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000664 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
665 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
666 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800667
668 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000669 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
670 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
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 Uhlerd1472a22016-04-15 15:18:56 -0700677 oat_file_assistant.MakeUpToDate(false, &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));
681 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
682 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800683
684 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000685 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000686 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700687 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800688
689 // Verify we can load the dex files from it.
690 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
691 ASSERT_TRUE(oat_file.get() != nullptr);
692 EXPECT_TRUE(oat_file->IsExecutable());
693 std::vector<std::unique_ptr<const DexFile>> dex_files;
694 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
695 EXPECT_EQ(1u, dex_files.size());
696}
697
Richard Uhler9b994ea2015-06-24 08:44:19 -0700698// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
699// OAT file. Expect: The status is kNoDexOptNeeded.
700TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
701 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
702
703 Copy(GetStrippedDexSrc1(), dex_location);
704
705 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700706 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700707
Andreas Gampe29d38e72016-03-23 15:31:51 +0000708 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
709 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
710 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
711 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
712 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
713 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700714
715 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000716 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
717 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700718 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
719
720 // Make the oat file up to date. This should have no effect.
721 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700722 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700723 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700724 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700725
Andreas Gampe29d38e72016-03-23 15:31:51 +0000726 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
727 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700728
729 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000730 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
731 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700732 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
733}
734
Richard Uhler95abd042015-03-24 09:51:28 -0700735// Case: We have a DEX file, no ODEX file and an OAT file that needs
736// relocation.
737// Expect: The status is kSelfPatchOatNeeded.
738TEST_F(OatFileAssistantTest, SelfRelocation) {
739 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
740 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
741
742 // Create the dex and odex files
743 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000744 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700745
746 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700747 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700748
Andreas Gampe29d38e72016-03-23 15:31:51 +0000749 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
750 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000751 EXPECT_EQ(OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000752 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000753 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000754 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700755
756 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000757 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
758 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700759 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700760
761 // Make the oat file up to date.
762 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700763 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700764 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700765 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700766
Andreas Gampe29d38e72016-03-23 15:31:51 +0000767 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
768 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700769
770 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000771 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000772 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700773 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700774
775 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
776 ASSERT_TRUE(oat_file.get() != nullptr);
777 EXPECT_TRUE(oat_file->IsExecutable());
778 std::vector<std::unique_ptr<const DexFile>> dex_files;
779 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
780 EXPECT_EQ(1u, dex_files.size());
781}
782
Richard Uhlerd1537b52016-03-29 13:27:41 -0700783// Case: We have a DEX file, no ODEX file and an OAT file that needs
784// relocation but doesn't have patch info.
785// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
786TEST_F(OatFileAssistantTest, NoSelfRelocation) {
787 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
788 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
789
790 // Create the dex and odex files
791 Copy(GetDexSrc1(), dex_location);
792 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
793
794 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700795 oat_location.c_str(), kRuntimeISA, true);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700796
Richard Uhler7225a8d2016-11-22 10:12:03 +0000797 EXPECT_EQ(OatFileAssistant::kDex2OatForRelocation,
Richard Uhlerd1537b52016-03-29 13:27:41 -0700798 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
799
800 // Make the oat file up to date.
801 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700802 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700803 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700804 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerd1537b52016-03-29 13:27:41 -0700805 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
806 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
807
808 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
809 ASSERT_TRUE(oat_file.get() != nullptr);
810 EXPECT_TRUE(oat_file->IsExecutable());
811 std::vector<std::unique_ptr<const DexFile>> dex_files;
812 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
813 EXPECT_EQ(1u, dex_files.size());
814}
815
Richard Uhler66d874d2015-01-15 09:37:19 -0800816// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
817// OAT files both have patch delta of 0.
Richard Uhler70a84262016-11-08 16:51:51 +0000818// Expect: It shouldn't crash, and status is kSelfPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800819TEST_F(OatFileAssistantTest, OdexOatOverlap) {
820 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700821 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
822 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800823
824 // Create the dex and odex files
825 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000826 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800827
828 // Create the oat file by copying the odex so they are located in the same
829 // place in memory.
830 Copy(odex_location, oat_location);
831
832 // Verify things don't go bad.
833 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700834 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800835
Richard Uhler7225a8d2016-11-22 10:12:03 +0000836 // kPatchoatForRelocation is expected rather than -kPatchoatForRelocation
837 // based on the assumption that the oat location is more up-to-date than the odex
Richard Uhler70a84262016-11-08 16:51:51 +0000838 // location, even if they both need relocation.
Richard Uhler7225a8d2016-11-22 10:12:03 +0000839 EXPECT_EQ(OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000840 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800841
842 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000843 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
844 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700845 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800846
847 // Things aren't relocated, so it should fall back to interpreted.
848 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
849 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700850
Richard Uhler66d874d2015-01-15 09:37:19 -0800851 EXPECT_FALSE(oat_file->IsExecutable());
852 std::vector<std::unique_ptr<const DexFile>> dex_files;
853 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
854 EXPECT_EQ(1u, dex_files.size());
855}
856
857// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700858// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800859TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
860 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700861 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800862
863 // Create the dex and odex files
864 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000865 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800866
867 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700868 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800869
Andreas Gampe29d38e72016-03-23 15:31:51 +0000870 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
871 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000872 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000873 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800874
875 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000876 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000877 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700878 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800879}
880
Andreas Gampe29d38e72016-03-23 15:31:51 +0000881// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
882// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
883TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
884 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
885 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000886
887 // Create the dex and odex files
888 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000889 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000890
891 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700892 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000893
Andreas Gampe29d38e72016-03-23 15:31:51 +0000894 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
895 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000896 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000897 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000898
899 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000900 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000901 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000902 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
903}
904
Richard Uhler66d874d2015-01-15 09:37:19 -0800905// Case: We have a DEX file and up-to-date OAT file for it.
906// Expect: We should load an executable dex file.
907TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
908 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
909
910 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000911 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800912
913 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700914 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000915
916 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
917 ASSERT_TRUE(oat_file.get() != nullptr);
918 EXPECT_TRUE(oat_file->IsExecutable());
919 std::vector<std::unique_ptr<const DexFile>> dex_files;
920 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
921 EXPECT_EQ(1u, dex_files.size());
922}
923
924// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
925// Expect: We should still load the oat file as executable.
926TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
927 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
928
929 Copy(GetDexSrc1(), dex_location);
930 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
931
932 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700933 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800934
935 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
936 ASSERT_TRUE(oat_file.get() != nullptr);
937 EXPECT_TRUE(oat_file->IsExecutable());
938 std::vector<std::unique_ptr<const DexFile>> dex_files;
939 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
940 EXPECT_EQ(1u, dex_files.size());
941}
942
943// Case: We have a DEX file and up-to-date OAT file for it.
944// Expect: Loading non-executable should load the oat non-executable.
945TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
946 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
947
948 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000949 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800950
951 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700952 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800953
954 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
955 ASSERT_TRUE(oat_file.get() != nullptr);
956 EXPECT_FALSE(oat_file->IsExecutable());
957 std::vector<std::unique_ptr<const DexFile>> dex_files;
958 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
959 EXPECT_EQ(1u, dex_files.size());
960}
961
962// Case: We have a DEX file.
963// Expect: We should load an executable dex file from an alternative oat
964// location.
965TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
966 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
967 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
968
969 Copy(GetDexSrc1(), dex_location);
970
971 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700972 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800973 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700974 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700975 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700976 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800977
978 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
979 ASSERT_TRUE(oat_file.get() != nullptr);
980 EXPECT_TRUE(oat_file->IsExecutable());
981 std::vector<std::unique_ptr<const DexFile>> dex_files;
982 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
983 EXPECT_EQ(1u, dex_files.size());
984
985 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
986
987 // Verify it didn't create an oat in the default location.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700988 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler03bc6592016-11-22 09:42:04 +0000989 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800990}
991
Richard Uhler8327cf72015-10-13 16:34:59 -0700992// Case: We have a DEX file but can't write the oat file.
993// Expect: We should fail to make the oat file up to date.
994TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
995 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
996
997 // Make the oat location unwritable by inserting some non-existent
998 // intermediate directories.
999 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
1000
1001 Copy(GetDexSrc1(), dex_location);
1002
1003 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -07001004 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001005 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001006 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001007 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001008 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001009
1010 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1011 ASSERT_TRUE(oat_file.get() == nullptr);
1012}
1013
1014// Case: We don't have a DEX file and can't write the oat file.
1015// Expect: We should fail to generate the oat file without crashing.
1016TEST_F(OatFileAssistantTest, GenNoDex) {
1017 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1018 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
1019
1020 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -07001021 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001022 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001023 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001024 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001025 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001026}
1027
Richard Uhler66d874d2015-01-15 09:37:19 -08001028// Turn an absolute path into a path relative to the current working
1029// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001030static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001031 char buf[MAXPATHLEN];
1032 std::string cwd = getcwd(buf, MAXPATHLEN);
1033
1034 // Split the target and cwd paths into components.
1035 std::vector<std::string> target_path;
1036 std::vector<std::string> cwd_path;
1037 Split(target, '/', &target_path);
1038 Split(cwd, '/', &cwd_path);
1039
1040 // Reverse the path components, so we can use pop_back().
1041 std::reverse(target_path.begin(), target_path.end());
1042 std::reverse(cwd_path.begin(), cwd_path.end());
1043
1044 // Drop the common prefix of the paths. Because we reversed the path
1045 // components, this becomes the common suffix of target_path and cwd_path.
1046 while (!target_path.empty() && !cwd_path.empty()
1047 && target_path.back() == cwd_path.back()) {
1048 target_path.pop_back();
1049 cwd_path.pop_back();
1050 }
1051
1052 // For each element of the remaining cwd_path, add '..' to the beginning
1053 // of the target path. Because we reversed the path components, we add to
1054 // the end of target_path.
1055 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1056 target_path.push_back("..");
1057 }
1058
1059 // Reverse again to get the right path order, and join to get the result.
1060 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -08001061 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -08001062}
1063
1064// Case: Non-absolute path to Dex location.
1065// Expect: Not sure, but it shouldn't crash.
1066TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1067 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1068 Copy(GetDexSrc1(), abs_dex_location);
1069
1070 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001071 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001072
1073 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001074 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001075 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001076 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1077 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001078}
1079
1080// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001081// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001082TEST_F(OatFileAssistantTest, ShortDexLocation) {
1083 std::string dex_location = "/xx";
1084
Richard Uhlerd1472a22016-04-15 15:18:56 -07001085 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001086
1087 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001088 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1089 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001090 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1091 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001092 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001093
Richard Uhler9b994ea2015-06-24 08:44:19 -07001094 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001095 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001096 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001097 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001098 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001099 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001100}
1101
1102// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001103// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001104TEST_F(OatFileAssistantTest, LongDexExtension) {
1105 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1106 Copy(GetDexSrc1(), dex_location);
1107
Richard Uhlerd1472a22016-04-15 15:18:56 -07001108 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001109
Richard Uhler7225a8d2016-11-22 10:12:03 +00001110 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001111 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001112
1113 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001114 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1115 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001116}
1117
1118// A task to generate a dex location. Used by the RaceToGenerate test.
1119class RaceGenerateTask : public Task {
1120 public:
1121 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001122 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001123 {}
1124
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001125 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001126 // Load the dex files, and save a pointer to the loaded oat file, so that
1127 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001128 std::vector<std::unique_ptr<const DexFile>> dex_files;
1129 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001130 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001131 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1132 dex_location_.c_str(),
1133 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001134 /*class_loader*/nullptr,
1135 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001136 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001137 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001138 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001139 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1140 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001141 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001142 }
1143
1144 const OatFile* GetLoadedOatFile() const {
1145 return loaded_oat_file_;
1146 }
1147
1148 private:
1149 std::string dex_location_;
1150 std::string oat_location_;
1151 const OatFile* loaded_oat_file_;
1152};
1153
1154// Test the case where multiple processes race to generate an oat file.
1155// This simulates multiple processes using multiple threads.
1156//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001157// We want unique Oat files to be loaded even when there is a race to load.
1158// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1159// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001160TEST_F(OatFileAssistantTest, RaceToGenerate) {
1161 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001162 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001163
1164 // We use the lib core dex file, because it's large, and hopefully should
1165 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001166 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001167
1168 const int kNumThreads = 32;
1169 Thread* self = Thread::Current();
1170 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1171 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1172 for (int i = 0; i < kNumThreads; i++) {
1173 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1174 thread_pool.AddTask(self, task.get());
1175 tasks.push_back(std::move(task));
1176 }
1177 thread_pool.StartWorkers(self);
1178 thread_pool.Wait(self, true, false);
1179
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001180 // Verify every task got a unique oat file.
1181 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001182 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001183 const OatFile* oat_file = task->GetLoadedOatFile();
1184 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1185 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001186 }
1187}
1188
1189// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1190// disabled.
1191// Expect: We should load the odex file non-executable.
1192TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1193 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001194 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001195
1196 // Create the dex and odex files
1197 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001198 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001199
1200 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001201 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001202
1203 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1204 ASSERT_TRUE(oat_file.get() != nullptr);
1205 EXPECT_FALSE(oat_file->IsExecutable());
1206 std::vector<std::unique_ptr<const DexFile>> dex_files;
1207 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1208 EXPECT_EQ(1u, dex_files.size());
1209}
1210
1211// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1212// disabled.
1213// Expect: We should load the odex file non-executable.
1214TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1215 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001216 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001217
1218 // Create the dex and odex files
1219 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001220 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001221
1222 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001223 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001224
1225 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1226 ASSERT_TRUE(oat_file.get() != nullptr);
1227 EXPECT_FALSE(oat_file->IsExecutable());
1228 std::vector<std::unique_ptr<const DexFile>> dex_files;
1229 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1230 EXPECT_EQ(2u, dex_files.size());
1231}
1232
Richard Uhlerf4b34872016-04-13 11:03:46 -07001233TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1234 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1235 Copy(GetDexSrc1(), dex_location);
1236
Richard Uhlerd1472a22016-04-15 15:18:56 -07001237 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001238
1239 std::string error_msg;
1240 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1241 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001242 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001243 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1244 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +00001245 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001246 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1247
1248 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1249 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001250 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001251 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1252 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1253 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1254 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1255
1256 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1257 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001258 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001259}
1260
Richard Uhlerb81881d2016-04-19 13:08:04 -07001261TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001262 std::string error_msg;
1263 std::string odex_file;
1264
Richard Uhlerb81881d2016-04-19 13:08:04 -07001265 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001266 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001267 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001268
Richard Uhlerb81881d2016-04-19 13:08:04 -07001269 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001270 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001271 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001272
Richard Uhlerb81881d2016-04-19 13:08:04 -07001273 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001274 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001275 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001276 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001277}
1278
Richard Uhler23cedd22015-04-08 13:17:29 -07001279// Verify the dexopt status values from dalvik.system.DexFile
1280// match the OatFileAssistant::DexOptStatus values.
1281TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001282 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1283 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1284 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1285 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1286 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1287 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
1288 {OatFileAssistant::kPatchoatForRelocation, "PATCHOAT_FOR_RELOCATION"}
1289 };
1290
Richard Uhler23cedd22015-04-08 13:17:29 -07001291 ScopedObjectAccess soa(Thread::Current());
1292 StackHandleScope<1> hs(soa.Self());
1293 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1294 Handle<mirror::Class> dexfile(
1295 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1296 ASSERT_FALSE(dexfile.Get() == nullptr);
1297 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1298
Richard Uhler7225a8d2016-11-22 10:12:03 +00001299 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1300 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001301 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001302 ASSERT_FALSE(art_field == nullptr);
1303 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1304 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1305 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001306}
Richard Uhler66d874d2015-01-15 09:37:19 -08001307
1308// TODO: More Tests:
1309// * Test class linker falls back to unquickened dex for DexNoOat
1310// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001311// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001312// * Test for status of oat while oat is being generated (how?)
1313// * Test case where 32 and 64 bit boot class paths differ,
1314// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1315// 64 bit boot class paths.
1316// * Test unexpected scenarios (?):
1317// - Dex is stripped, don't have odex.
1318// - Oat file corrupted after status check, before reload unexecutable
1319// because it's unrelocated and no dex2oat
Calin Juravleb077e152016-02-18 18:47:37 +00001320// * Test unrelocated specific target compilation type can be relocated to
1321// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001322} // namespace art