blob: 5730cf24927c519efdf194139adbd0518bd969c9 [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
23#include <backtrace/BacktraceMap.h>
24#include <gtest/gtest.h>
25
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010027#include "class_linker-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080028#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070029#include "compiler_callbacks.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070030#include "dex2oat_environment_test.h"
Richard Uhlerf16d5722015-05-11 09:32:47 -070031#include "gc/space/image_space.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080032#include "mem_map.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070033#include "oat_file_assistant.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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "thread-inl.h"
38#include "utils.h"
39
40namespace art {
41
Andreas Gampee1459ae2016-06-29 09:36:30 -070042class OatFileAssistantTest : public Dex2oatEnvironmentTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080043 public:
Andreas Gampee1459ae2016-06-29 09:36:30 -070044 virtual void SetUp() OVERRIDE {
Richard Uhler66d874d2015-01-15 09:37:19 -080045 ReserveImageSpace();
Andreas Gampee1459ae2016-06-29 09:36:30 -070046 Dex2oatEnvironmentTest::SetUp();
Richard Uhler66d874d2015-01-15 09:37:19 -080047 }
48
Richard Uhlera48403e2016-04-26 10:24:38 -070049 // Pre-Relocate the image to a known non-zero offset so we don't have to
50 // deal with the runtime randomly relocating the image by 0 and messing up
51 // the expected results of the tests.
Richard Uhler03bc6592016-11-22 09:42:04 +000052 bool PreRelocateImage(const std::string& image_location, std::string* error_msg) {
Richard Uhlera48403e2016-04-26 10:24:38 -070053 std::string image;
Richard Uhler03bc6592016-11-22 09:42:04 +000054 if (!GetCachedImageFile(image_location, &image, error_msg)) {
Richard Uhlera48403e2016-04-26 10:24:38 -070055 return false;
56 }
57
58 std::string patchoat = GetAndroidRoot();
59 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
60
61 std::vector<std::string> argv;
62 argv.push_back(patchoat);
Richard Uhler03bc6592016-11-22 09:42:04 +000063 argv.push_back("--input-image-location=" + image_location);
Richard Uhlera48403e2016-04-26 10:24:38 -070064 argv.push_back("--output-image-file=" + image);
65 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
66 argv.push_back("--base-offset-delta=0x00008000");
67 return Exec(argv, error_msg);
68 }
69
Richard Uhler66d874d2015-01-15 09:37:19 -080070 virtual void PreRuntimeCreate() {
Richard Uhlera48403e2016-04-26 10:24:38 -070071 std::string error_msg;
Richard Uhler03bc6592016-11-22 09:42:04 +000072 ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg;
73 ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -080074 UnreserveImageSpace();
75 }
76
Andreas Gampee1459ae2016-06-29 09:36:30 -070077 virtual void PostRuntimeCreate() OVERRIDE {
Richard Uhler66d874d2015-01-15 09:37:19 -080078 ReserveImageSpace();
79 }
80
Richard Uhler03bc6592016-11-22 09:42:04 +000081 // Generate an oat file for the purposes of test.
82 void GenerateOatForTest(const std::string& dex_location,
83 const std::string& oat_location,
84 CompilerFilter::Filter filter,
85 bool relocate,
86 bool pic,
87 bool with_patch_info,
88 bool with_alternate_image) {
David Sehrd106d9f2016-08-16 19:22:57 -070089 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
90 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
Richard Uhler03bc6592016-11-22 09:42:04 +000091
92 if (!relocate) {
93 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
94 // relocated image file.
95 ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno);
96 }
David Sehrd106d9f2016-08-16 19:22:57 -070097
Richard Uhler93aa2102015-08-10 14:47:41 -070098 std::vector<std::string> args;
99 args.push_back("--dex-file=" + dex_location);
Richard Uhler03bc6592016-11-22 09:42:04 +0000100 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000101 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhlera48403e2016-04-26 10:24:38 -0700102 args.push_back("--runtime-arg");
Richard Uhler03bc6592016-11-22 09:42:04 +0000103
104 // Use -Xnorelocate regardless of the relocate argument.
105 // We control relocation by redirecting the dalvik cache when needed
106 // rather than use this flag.
Richard Uhlera48403e2016-04-26 10:24:38 -0700107 args.push_back("-Xnorelocate");
Richard Uhler93aa2102015-08-10 14:47:41 -0700108
Richard Uhlera48403e2016-04-26 10:24:38 -0700109 if (pic) {
110 args.push_back("--compile-pic");
111 }
112
113 if (with_patch_info) {
114 args.push_back("--include-patch-information");
115 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700116
Richard Uhler03bc6592016-11-22 09:42:04 +0000117 std::string image_location = GetImageLocation();
118 if (with_alternate_image) {
119 args.push_back("--boot-image=" + GetImageLocation2());
120 }
121
Richard Uhler93aa2102015-08-10 14:47:41 -0700122 std::string error_msg;
123 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700124
Richard Uhler03bc6592016-11-22 09:42:04 +0000125 if (!relocate) {
126 // Restore the dalvik cache if needed.
127 ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno);
128 }
129
130 // Verify the odex file was generated as expected.
131 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(),
132 oat_location.c_str(),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800133 nullptr,
134 nullptr,
135 false,
136 /*low_4gb*/false,
137 dex_location.c_str(),
138 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700139 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700140 EXPECT_EQ(pic, odex_file->IsPic());
141 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000142 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
143
Richard Uhler03bc6592016-11-22 09:42:04 +0000144 std::unique_ptr<ImageHeader> image_header(
145 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
146 kRuntimeISA,
147 &error_msg));
148 ASSERT_TRUE(image_header != nullptr) << error_msg;
149 const OatHeader& oat_header = odex_file->GetOatHeader();
150 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
151
152 if (CompilerFilter::DependsOnImageChecksum(filter)) {
153 if (with_alternate_image) {
154 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
155 } else {
156 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
157 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000158 }
Richard Uhler03bc6592016-11-22 09:42:04 +0000159
160 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
161 if (relocate) {
162 EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
163 oat_header.GetImageFileLocationOatDataBegin());
164 EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
165 } else {
166 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
167 oat_header.GetImageFileLocationOatDataBegin());
168 EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
169 }
170 }
171 }
172
173 // Generate a non-PIC odex file for the purposes of test.
174 // The generated odex file will be un-relocated.
175 void GenerateOdexForTest(const std::string& dex_location,
176 const std::string& odex_location,
177 CompilerFilter::Filter filter) {
178 GenerateOatForTest(dex_location,
179 odex_location,
180 filter,
181 /*relocate*/false,
182 /*pic*/false,
183 /*with_patch_info*/true,
184 /*with_alternate_image*/false);
Richard Uhler93aa2102015-08-10 14:47:41 -0700185 }
186
187 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000188 const std::string& odex_location,
189 CompilerFilter::Filter filter) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000190 GenerateOatForTest(dex_location,
191 odex_location,
192 filter,
193 /*relocate*/false,
194 /*pic*/true,
195 /*with_patch_info*/false,
196 /*with_alternate_image*/false);
Calin Juravleb077e152016-02-18 18:47:37 +0000197 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000198
Richard Uhlerd1537b52016-03-29 13:27:41 -0700199 // Generate a non-PIC odex file without patch information for the purposes
200 // of test. The generated odex file will be un-relocated.
Richard Uhlerd1537b52016-03-29 13:27:41 -0700201 void GenerateNoPatchOdexForTest(const std::string& dex_location,
202 const std::string& odex_location,
203 CompilerFilter::Filter filter) {
Richard Uhler03bc6592016-11-22 09:42:04 +0000204 GenerateOatForTest(dex_location,
205 odex_location,
206 filter,
207 /*relocate*/false,
208 /*pic*/false,
209 /*with_patch_info*/false,
210 /*with_alternate_image*/false);
211 }
212
213 // Generate an oat file in the oat location.
214 void GenerateOatForTest(const char* dex_location,
215 CompilerFilter::Filter filter,
216 bool relocate,
217 bool pic,
218 bool with_patch_info,
219 bool with_alternate_image) {
220 std::string oat_location;
221 std::string error_msg;
222 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
223 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
224 GenerateOatForTest(dex_location,
225 oat_location,
226 filter,
227 relocate,
228 pic,
229 with_patch_info,
230 with_alternate_image);
231 }
232
233 // Generate a standard oat file in the oat location.
234 void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
235 GenerateOatForTest(dex_location,
236 filter,
237 /*relocate*/true,
238 /*pic*/false,
239 /*with_patch_info*/false,
240 /*with_alternate_image*/false);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700241 }
242
Richard Uhler66d874d2015-01-15 09:37:19 -0800243 private:
244 // Reserve memory around where the image will be loaded so other memory
245 // won't conflict when it comes time to load the image.
246 // This can be called with an already loaded image to reserve the space
247 // around it.
248 void ReserveImageSpace() {
249 MemMap::Init();
250
251 // Ensure a chunk of memory is reserved for the image space.
Richard Uhlera48403e2016-04-26 10:24:38 -0700252 // The reservation_end includes room for the main space that has to come
253 // right after the image in case of the GSS collector.
254 uintptr_t reservation_start = ART_BASE_ADDRESS;
255 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800256
Richard Uhler66d874d2015-01-15 09:37:19 -0800257 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
258 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
259 for (BacktraceMap::const_iterator it = map->begin();
260 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700261 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
262 reservation_start = std::max(reservation_start, it->end);
263 }
264 ReserveImageSpaceChunk(reservation_start, reservation_end);
265 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800266
Richard Uhler3efe9792015-03-30 16:18:03 -0700267 // Reserve a chunk of memory for the image space in the given range.
268 // Only has effect for chunks with a positive number of bytes.
269 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
270 if (start < end) {
271 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800272 image_reservation_.push_back(std::unique_ptr<MemMap>(
273 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700274 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700275 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
277 LOG(INFO) << "Reserved space for image " <<
278 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
279 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800280 }
281 }
282
283
284 // Unreserve any memory reserved by ReserveImageSpace. This should be called
285 // before the image is loaded.
286 void UnreserveImageSpace() {
287 image_reservation_.clear();
288 }
289
Richard Uhler66d874d2015-01-15 09:37:19 -0800290 std::vector<std::unique_ptr<MemMap>> image_reservation_;
291};
292
293class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
294 public:
295 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
296 OatFileAssistantTest::SetUpRuntimeOptions(options);
297 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
298 }
299};
300
Richard Uhler66d874d2015-01-15 09:37:19 -0800301// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700302// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800303TEST_F(OatFileAssistantTest, DexNoOat) {
304 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
305 Copy(GetDexSrc1(), dex_location);
306
Richard Uhlerd1472a22016-04-15 15:18:56 -0700307 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800308
Richard Uhler7225a8d2016-11-22 10:12:03 +0000309 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000310 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000311 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000312 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000313 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000314 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000315 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000316 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800317
318 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000319 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
320 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700321 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800322}
323
324// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700325// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800326TEST_F(OatFileAssistantTest, NoDexNoOat) {
327 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
328
Richard Uhlerd1472a22016-04-15 15:18:56 -0700329 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800330
Andreas Gampe29d38e72016-03-23 15:31:51 +0000331 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
332 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700333 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
334
335 // Trying to make the oat file up to date should not fail or crash.
336 std::string error_msg;
Richard Uhlerd1472a22016-04-15 15:18:56 -0700337 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700338
339 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800340 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
341 EXPECT_EQ(nullptr, oat_file.get());
342}
343
344// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700345// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800346TEST_F(OatFileAssistantTest, OatUpToDate) {
347 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
348 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000349 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800350
Richard Uhlerd1472a22016-04-15 15:18:56 -0700351 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800352
Andreas Gampe29d38e72016-03-23 15:31:51 +0000353 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
354 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
355 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
356 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
357 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
358 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000359 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000360 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
361
Richard Uhler66d874d2015-01-15 09:37:19 -0800362 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000363 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler95abd042015-03-24 09:51:28 -0700364 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700365 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800366}
367
Richard Uhler9a37efc2016-08-05 16:32:55 -0700368// Case: We have a DEX file and ODEX file for a different dex location.
369// Expect: The status is kDex2OatNeeded.
370TEST_F(OatFileAssistantTest, OatForDifferentDex) {
371 // Generate an odex file for OatForDifferentDex_A.jar
372 std::string dex_location_a = GetScratchDir() + "/OatForDifferentDex_A.jar";
373 std::string odex_location = GetOdexDir() + "/OatForDifferentDex.odex";
374 Copy(GetDexSrc1(), dex_location_a);
375 GenerateOdexForTest(dex_location_a, odex_location, CompilerFilter::kSpeed);
376
377 // Try to use that odex file for OatForDifferentDex.jar
378 std::string dex_location = GetScratchDir() + "/OatForDifferentDex.jar";
379 Copy(GetDexSrc1(), dex_location);
380
381 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
382
Richard Uhler7225a8d2016-11-22 10:12:03 +0000383 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700384 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
385
386 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000387 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OdexFileStatus());
388 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700389}
390
Andreas Gampe29d38e72016-03-23 15:31:51 +0000391// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700392// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
393// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000394TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
395 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
396 Copy(GetDexSrc1(), dex_location);
397 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
398
Richard Uhlerd1472a22016-04-15 15:18:56 -0700399 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000400
401 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700402 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000403 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700404 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000405 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700406 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000407 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700408 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000409
410 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000411 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000412 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
413 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
414}
415
Richard Uhler66d874d2015-01-15 09:37:19 -0800416// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700417// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800418TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
419 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
420 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000421 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800422
Richard Uhlerd1472a22016-04-15 15:18:56 -0700423 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000424 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700425 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700426 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700427
428 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700429 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800430 ASSERT_TRUE(oat_file.get() != nullptr);
431 EXPECT_TRUE(oat_file->IsExecutable());
432 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700433 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
434 EXPECT_EQ(2u, dex_files.size());
435}
436
Richard Uhler67ff7d12015-05-14 13:21:13 -0700437// Case: We have a MultiDEX file where the secondary dex file is out of date.
438// Expect: The status is kDex2OatNeeded.
439TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
440 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
441
442 // Compile code for GetMultiDexSrc1.
443 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000444 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700445
446 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
447 // is out of date.
448 Copy(GetMultiDexSrc2(), dex_location);
449
Richard Uhlerd1472a22016-04-15 15:18:56 -0700450 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000451 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700452 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700453 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700454}
455
Richard Uhlere5fed032015-03-18 08:21:11 -0700456// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
457// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700458// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700459TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
460 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700461 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700462
463 // Create the dex file
464 Copy(GetMultiDexSrc1(), dex_location);
465
466 // Create the oat file with relative encoded dex location.
467 std::vector<std::string> args;
468 args.push_back("--dex-file=" + dex_location);
469 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
470 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000471 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700472
473 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700474 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700475
476 // Verify we can load both dex files.
477 OatFileAssistant oat_file_assistant(dex_location.c_str(),
478 oat_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700479 kRuntimeISA, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700480 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
481 ASSERT_TRUE(oat_file.get() != nullptr);
482 EXPECT_TRUE(oat_file->IsExecutable());
483 std::vector<std::unique_ptr<const DexFile>> dex_files;
484 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800485 EXPECT_EQ(2u, dex_files.size());
486}
487
Richard Uhler03bc6592016-11-22 09:42:04 +0000488// Case: We have a DEX file and an OAT file out of date with respect to the
489// dex checksum.
490TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
491 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800492
493 // We create a dex, generate an oat for it, then overwrite the dex with a
494 // different dex to make the oat out of date.
495 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000496 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800497 Copy(GetDexSrc2(), dex_location);
498
Richard Uhlerd1472a22016-04-15 15:18:56 -0700499 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000500 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000501 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000502 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000503 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800504
505 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000506 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
507 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
508 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
509}
510
511// Case: We have a DEX file and an OAT file out of date with respect to the
512// boot image.
513TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
514 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
515
516 Copy(GetDexSrc1(), dex_location);
517 GenerateOatForTest(dex_location.c_str(),
518 CompilerFilter::kSpeed,
519 /*relocate*/true,
520 /*pic*/false,
521 /*with_patch_info*/false,
522 /*with_alternate_image*/true);
523
524 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000525 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000526 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000527 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000528 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000529 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000530 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
531
532 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
533 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
534 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
535 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
536}
537
538// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
539// respect to the boot image.
540// It shouldn't matter that the OAT file is out of date, because it is
541// verify-at-runtime.
542TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
543 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
544
545 Copy(GetDexSrc1(), dex_location);
546 GenerateOatForTest(dex_location.c_str(),
547 CompilerFilter::kVerifyAtRuntime,
548 /*relocate*/true,
549 /*pic*/false,
550 /*with_patch_info*/false,
551 /*with_alternate_image*/true);
552
553 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
554 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
555 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000556 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhler03bc6592016-11-22 09:42:04 +0000557 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
558
559 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
560 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
561 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700562 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800563}
564
565// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700566// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800567TEST_F(OatFileAssistantTest, DexOdexNoOat) {
568 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700569 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800570
571 // Create the dex and odex files
572 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000573 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800574
575 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700576 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800577
Andreas Gampe29d38e72016-03-23 15:31:51 +0000578 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
579 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000580 EXPECT_EQ(-OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000581 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800582
583 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000584 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
585 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700586 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700587
588 // We should still be able to get the non-executable odex file to run from.
589 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
590 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800591}
592
593// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700594// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800595TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
596 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700597 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800598
599 // Create the dex and odex files
600 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000601 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800602
603 // Strip the dex file
604 Copy(GetStrippedDexSrc1(), dex_location);
605
606 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700607 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
Richard Uhler7225a8d2016-11-22 10:12:03 +0000609 EXPECT_EQ(-OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000610 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800611
612 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000613 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
614 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700615 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800616
617 // Make the oat file up to date.
618 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700619 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700620 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700621 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800622
Andreas Gampe29d38e72016-03-23 15:31:51 +0000623 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
624 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800625
626 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000627 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000628 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700629 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800630
631 // Verify we can load the dex files from it.
632 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
633 ASSERT_TRUE(oat_file.get() != nullptr);
634 EXPECT_TRUE(oat_file->IsExecutable());
635 std::vector<std::unique_ptr<const DexFile>> dex_files;
636 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
637 EXPECT_EQ(1u, dex_files.size());
638}
639
Richard Uhler95abd042015-03-24 09:51:28 -0700640// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
641// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800642TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
643 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700644 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800645
646 // Create the oat file from a different dex file so it looks out of date.
647 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000648 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800649
650 // Create the odex file
651 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000652 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800653
654 // Strip the dex file.
655 Copy(GetStrippedDexSrc1(), dex_location);
656
657 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700658 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800659
Andreas Gampe29d38e72016-03-23 15:31:51 +0000660 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
661 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000662 EXPECT_EQ(-OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000663 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
664 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
665 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800666
667 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000668 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
669 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700670 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800671
672 // Make the oat file up to date.
673 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700674 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700675 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700676 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800677
Andreas Gampe29d38e72016-03-23 15:31:51 +0000678 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
679 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
680 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
681 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800682
683 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000684 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000685 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700686 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800687
688 // Verify we can load the dex files from it.
689 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
690 ASSERT_TRUE(oat_file.get() != nullptr);
691 EXPECT_TRUE(oat_file->IsExecutable());
692 std::vector<std::unique_ptr<const DexFile>> dex_files;
693 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
694 EXPECT_EQ(1u, dex_files.size());
695}
696
Richard Uhler9b994ea2015-06-24 08:44:19 -0700697// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
698// OAT file. Expect: The status is kNoDexOptNeeded.
699TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
700 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
701
702 Copy(GetStrippedDexSrc1(), dex_location);
703
704 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700705 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700706
Andreas Gampe29d38e72016-03-23 15:31:51 +0000707 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
708 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
709 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
710 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
711 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
712 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700713
714 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000715 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
716 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700717 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
718
719 // Make the oat file up to date. This should have no effect.
720 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700721 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700722 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700723 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700724
Andreas Gampe29d38e72016-03-23 15:31:51 +0000725 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
726 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700727
728 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000729 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
730 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700731 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
732}
733
Richard Uhler95abd042015-03-24 09:51:28 -0700734// Case: We have a DEX file, no ODEX file and an OAT file that needs
735// relocation.
736// Expect: The status is kSelfPatchOatNeeded.
737TEST_F(OatFileAssistantTest, SelfRelocation) {
738 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
739 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
740
741 // Create the dex and odex files
742 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000743 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700744
745 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700746 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700747
Andreas Gampe29d38e72016-03-23 15:31:51 +0000748 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
749 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000750 EXPECT_EQ(OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000751 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000752 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000753 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700754
755 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000756 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
757 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700758 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700759
760 // Make the oat file up to date.
761 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700762 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700763 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700764 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700765
Andreas Gampe29d38e72016-03-23 15:31:51 +0000766 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
767 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700768
769 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000770 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000771 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700772 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700773
774 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
775 ASSERT_TRUE(oat_file.get() != nullptr);
776 EXPECT_TRUE(oat_file->IsExecutable());
777 std::vector<std::unique_ptr<const DexFile>> dex_files;
778 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
779 EXPECT_EQ(1u, dex_files.size());
780}
781
Richard Uhlerd1537b52016-03-29 13:27:41 -0700782// Case: We have a DEX file, no ODEX file and an OAT file that needs
783// relocation but doesn't have patch info.
784// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
785TEST_F(OatFileAssistantTest, NoSelfRelocation) {
786 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
787 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
788
789 // Create the dex and odex files
790 Copy(GetDexSrc1(), dex_location);
791 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
792
793 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700794 oat_location.c_str(), kRuntimeISA, true);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700795
Richard Uhler7225a8d2016-11-22 10:12:03 +0000796 EXPECT_EQ(OatFileAssistant::kDex2OatForRelocation,
Richard Uhlerd1537b52016-03-29 13:27:41 -0700797 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
798
799 // Make the oat file up to date.
800 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700801 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700802 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700803 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerd1537b52016-03-29 13:27:41 -0700804 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
805 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
806
807 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
808 ASSERT_TRUE(oat_file.get() != nullptr);
809 EXPECT_TRUE(oat_file->IsExecutable());
810 std::vector<std::unique_ptr<const DexFile>> dex_files;
811 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
812 EXPECT_EQ(1u, dex_files.size());
813}
814
Richard Uhler66d874d2015-01-15 09:37:19 -0800815// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
816// OAT files both have patch delta of 0.
Richard Uhler70a84262016-11-08 16:51:51 +0000817// Expect: It shouldn't crash, and status is kSelfPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800818TEST_F(OatFileAssistantTest, OdexOatOverlap) {
819 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700820 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
821 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800822
823 // Create the dex and odex files
824 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000825 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800826
827 // Create the oat file by copying the odex so they are located in the same
828 // place in memory.
829 Copy(odex_location, oat_location);
830
831 // Verify things don't go bad.
832 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700833 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800834
Richard Uhler7225a8d2016-11-22 10:12:03 +0000835 // kPatchoatForRelocation is expected rather than -kPatchoatForRelocation
836 // based on the assumption that the oat location is more up-to-date than the odex
Richard Uhler70a84262016-11-08 16:51:51 +0000837 // location, even if they both need relocation.
Richard Uhler7225a8d2016-11-22 10:12:03 +0000838 EXPECT_EQ(OatFileAssistant::kPatchoatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000839 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800840
841 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000842 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
843 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700844 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800845
846 // Things aren't relocated, so it should fall back to interpreted.
847 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
848 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700849
Richard Uhler66d874d2015-01-15 09:37:19 -0800850 EXPECT_FALSE(oat_file->IsExecutable());
851 std::vector<std::unique_ptr<const DexFile>> dex_files;
852 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
853 EXPECT_EQ(1u, dex_files.size());
854}
855
856// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700857// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800858TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
859 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700860 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800861
862 // Create the dex and odex files
863 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000864 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800865
866 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700867 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800868
Andreas Gampe29d38e72016-03-23 15:31:51 +0000869 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
870 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000871 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000872 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800873
874 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000875 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000876 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700877 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800878}
879
Andreas Gampe29d38e72016-03-23 15:31:51 +0000880// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
881// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
882TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
883 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
884 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000885
886 // Create the dex and odex files
887 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000888 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000889
890 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700891 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000892
Andreas Gampe29d38e72016-03-23 15:31:51 +0000893 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
894 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000895 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000896 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000897
898 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000899 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000900 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000901 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
902}
903
Richard Uhler66d874d2015-01-15 09:37:19 -0800904// Case: We have a DEX file and up-to-date OAT file for it.
905// Expect: We should load an executable dex file.
906TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
907 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
908
909 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000910 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800911
912 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700913 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000914
915 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
916 ASSERT_TRUE(oat_file.get() != nullptr);
917 EXPECT_TRUE(oat_file->IsExecutable());
918 std::vector<std::unique_ptr<const DexFile>> dex_files;
919 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
920 EXPECT_EQ(1u, dex_files.size());
921}
922
923// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
924// Expect: We should still load the oat file as executable.
925TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
926 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
927
928 Copy(GetDexSrc1(), dex_location);
929 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
930
931 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700932 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800933
934 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
935 ASSERT_TRUE(oat_file.get() != nullptr);
936 EXPECT_TRUE(oat_file->IsExecutable());
937 std::vector<std::unique_ptr<const DexFile>> dex_files;
938 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
939 EXPECT_EQ(1u, dex_files.size());
940}
941
942// Case: We have a DEX file and up-to-date OAT file for it.
943// Expect: Loading non-executable should load the oat non-executable.
944TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
945 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
946
947 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000948 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800949
950 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700951 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800952
953 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
954 ASSERT_TRUE(oat_file.get() != nullptr);
955 EXPECT_FALSE(oat_file->IsExecutable());
956 std::vector<std::unique_ptr<const DexFile>> dex_files;
957 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
958 EXPECT_EQ(1u, dex_files.size());
959}
960
961// Case: We have a DEX file.
962// Expect: We should load an executable dex file from an alternative oat
963// location.
964TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
965 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
966 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
967
968 Copy(GetDexSrc1(), dex_location);
969
970 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700971 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800972 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700973 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700974 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700975 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800976
977 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
978 ASSERT_TRUE(oat_file.get() != nullptr);
979 EXPECT_TRUE(oat_file->IsExecutable());
980 std::vector<std::unique_ptr<const DexFile>> dex_files;
981 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
982 EXPECT_EQ(1u, dex_files.size());
983
984 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
985
986 // Verify it didn't create an oat in the default location.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700987 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler03bc6592016-11-22 09:42:04 +0000988 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800989}
990
Richard Uhler8327cf72015-10-13 16:34:59 -0700991// Case: We have a DEX file but can't write the oat file.
992// Expect: We should fail to make the oat file up to date.
993TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
994 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
995
996 // Make the oat location unwritable by inserting some non-existent
997 // intermediate directories.
998 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
999
1000 Copy(GetDexSrc1(), dex_location);
1001
1002 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -07001003 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001004 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001005 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001006 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001007 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001008
1009 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1010 ASSERT_TRUE(oat_file.get() == nullptr);
1011}
1012
1013// Case: We don't have a DEX file and can't write the oat file.
1014// Expect: We should fail to generate the oat file without crashing.
1015TEST_F(OatFileAssistantTest, GenNoDex) {
1016 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
1017 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
1018
1019 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -07001020 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -07001021 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001022 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001023 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001024 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001025}
1026
Richard Uhler66d874d2015-01-15 09:37:19 -08001027// Turn an absolute path into a path relative to the current working
1028// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001029static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001030 char buf[MAXPATHLEN];
1031 std::string cwd = getcwd(buf, MAXPATHLEN);
1032
1033 // Split the target and cwd paths into components.
1034 std::vector<std::string> target_path;
1035 std::vector<std::string> cwd_path;
1036 Split(target, '/', &target_path);
1037 Split(cwd, '/', &cwd_path);
1038
1039 // Reverse the path components, so we can use pop_back().
1040 std::reverse(target_path.begin(), target_path.end());
1041 std::reverse(cwd_path.begin(), cwd_path.end());
1042
1043 // Drop the common prefix of the paths. Because we reversed the path
1044 // components, this becomes the common suffix of target_path and cwd_path.
1045 while (!target_path.empty() && !cwd_path.empty()
1046 && target_path.back() == cwd_path.back()) {
1047 target_path.pop_back();
1048 cwd_path.pop_back();
1049 }
1050
1051 // For each element of the remaining cwd_path, add '..' to the beginning
1052 // of the target path. Because we reversed the path components, we add to
1053 // the end of target_path.
1054 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1055 target_path.push_back("..");
1056 }
1057
1058 // Reverse again to get the right path order, and join to get the result.
1059 std::reverse(target_path.begin(), target_path.end());
1060 return Join(target_path, '/');
1061}
1062
1063// Case: Non-absolute path to Dex location.
1064// Expect: Not sure, but it shouldn't crash.
1065TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1066 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1067 Copy(GetDexSrc1(), abs_dex_location);
1068
1069 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001070 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001071
1072 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001073 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001074 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001075 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1076 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001077}
1078
1079// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001080// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001081TEST_F(OatFileAssistantTest, ShortDexLocation) {
1082 std::string dex_location = "/xx";
1083
Richard Uhlerd1472a22016-04-15 15:18:56 -07001084 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001085
1086 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001087 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1088 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001089 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1090 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001091 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001092
Richard Uhler9b994ea2015-06-24 08:44:19 -07001093 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001094 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001095 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001096 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001097 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001098 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001099}
1100
1101// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001102// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001103TEST_F(OatFileAssistantTest, LongDexExtension) {
1104 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1105 Copy(GetDexSrc1(), dex_location);
1106
Richard Uhlerd1472a22016-04-15 15:18:56 -07001107 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001108
Richard Uhler7225a8d2016-11-22 10:12:03 +00001109 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001110 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001111
1112 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001113 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1114 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001115}
1116
1117// A task to generate a dex location. Used by the RaceToGenerate test.
1118class RaceGenerateTask : public Task {
1119 public:
1120 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001121 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001122 {}
1123
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001124 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001125 // Load the dex files, and save a pointer to the loaded oat file, so that
1126 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001127 std::vector<std::unique_ptr<const DexFile>> dex_files;
1128 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001129 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001130 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1131 dex_location_.c_str(),
1132 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001133 /*class_loader*/nullptr,
1134 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001135 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001136 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001137 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001138 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1139 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001140 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001141 }
1142
1143 const OatFile* GetLoadedOatFile() const {
1144 return loaded_oat_file_;
1145 }
1146
1147 private:
1148 std::string dex_location_;
1149 std::string oat_location_;
1150 const OatFile* loaded_oat_file_;
1151};
1152
1153// Test the case where multiple processes race to generate an oat file.
1154// This simulates multiple processes using multiple threads.
1155//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001156// We want unique Oat files to be loaded even when there is a race to load.
1157// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1158// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001159TEST_F(OatFileAssistantTest, RaceToGenerate) {
1160 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001161 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001162
1163 // We use the lib core dex file, because it's large, and hopefully should
1164 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001165 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001166
1167 const int kNumThreads = 32;
1168 Thread* self = Thread::Current();
1169 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1170 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1171 for (int i = 0; i < kNumThreads; i++) {
1172 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1173 thread_pool.AddTask(self, task.get());
1174 tasks.push_back(std::move(task));
1175 }
1176 thread_pool.StartWorkers(self);
1177 thread_pool.Wait(self, true, false);
1178
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001179 // Verify every task got a unique oat file.
1180 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001181 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001182 const OatFile* oat_file = task->GetLoadedOatFile();
1183 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1184 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001185 }
1186}
1187
1188// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1189// disabled.
1190// Expect: We should load the odex file non-executable.
1191TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1192 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001193 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001194
1195 // Create the dex and odex files
1196 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001197 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001198
1199 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001200 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001201
1202 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1203 ASSERT_TRUE(oat_file.get() != nullptr);
1204 EXPECT_FALSE(oat_file->IsExecutable());
1205 std::vector<std::unique_ptr<const DexFile>> dex_files;
1206 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1207 EXPECT_EQ(1u, dex_files.size());
1208}
1209
1210// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1211// disabled.
1212// Expect: We should load the odex file non-executable.
1213TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1214 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001215 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001216
1217 // Create the dex and odex files
1218 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001219 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001220
1221 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001222 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001223
1224 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1225 ASSERT_TRUE(oat_file.get() != nullptr);
1226 EXPECT_FALSE(oat_file->IsExecutable());
1227 std::vector<std::unique_ptr<const DexFile>> dex_files;
1228 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1229 EXPECT_EQ(2u, dex_files.size());
1230}
1231
Richard Uhlerf4b34872016-04-13 11:03:46 -07001232TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1233 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1234 Copy(GetDexSrc1(), dex_location);
1235
Richard Uhlerd1472a22016-04-15 15:18:56 -07001236 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001237
1238 std::string error_msg;
1239 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1240 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001241 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001242 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1243 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler7225a8d2016-11-22 10:12:03 +00001244 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001245 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1246
1247 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1248 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001249 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001250 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1251 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1252 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1253 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1254
1255 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1256 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001257 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001258}
1259
Richard Uhlerb81881d2016-04-19 13:08:04 -07001260TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001261 std::string error_msg;
1262 std::string odex_file;
1263
Richard Uhlerb81881d2016-04-19 13:08:04 -07001264 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001265 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001266 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001267
Richard Uhlerb81881d2016-04-19 13:08:04 -07001268 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001269 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001270 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001271
Richard Uhlerb81881d2016-04-19 13:08:04 -07001272 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001273 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001274 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001275 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001276}
1277
Richard Uhler23cedd22015-04-08 13:17:29 -07001278// Verify the dexopt status values from dalvik.system.DexFile
1279// match the OatFileAssistant::DexOptStatus values.
1280TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001281 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1282 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1283 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1284 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1285 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1286 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
1287 {OatFileAssistant::kPatchoatForRelocation, "PATCHOAT_FOR_RELOCATION"}
1288 };
1289
Richard Uhler23cedd22015-04-08 13:17:29 -07001290 ScopedObjectAccess soa(Thread::Current());
1291 StackHandleScope<1> hs(soa.Self());
1292 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1293 Handle<mirror::Class> dexfile(
1294 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1295 ASSERT_FALSE(dexfile.Get() == nullptr);
1296 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1297
Richard Uhler7225a8d2016-11-22 10:12:03 +00001298 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1299 ArtField* art_field = mirror::Class::FindStaticField(
1300 soa.Self(), dexfile, field.second, "I");
1301 ASSERT_FALSE(art_field == nullptr);
1302 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1303 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1304 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001305}
Richard Uhler66d874d2015-01-15 09:37:19 -08001306
1307// TODO: More Tests:
1308// * Test class linker falls back to unquickened dex for DexNoOat
1309// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001310// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001311// * Test for status of oat while oat is being generated (how?)
1312// * Test case where 32 and 64 bit boot class paths differ,
1313// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1314// 64 bit boot class paths.
1315// * Test unexpected scenarios (?):
1316// - Dex is stripped, don't have odex.
1317// - Oat file corrupted after status check, before reload unexecutable
1318// because it's unrelocated and no dex2oat
Calin Juravleb077e152016-02-18 18:47:37 +00001319// * Test unrelocated specific target compilation type can be relocated to
1320// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001321} // namespace art