blob: 303676a74b9a03442268a1460fba8a872fe36bfa [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.
52 bool PreRelocateImage(std::string* error_msg) {
53 std::string image;
54 if (!GetCachedImageFile(&image, error_msg)) {
55 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);
63 argv.push_back("--input-image-location=" + GetImageLocation());
64 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;
72 ASSERT_TRUE(PreRelocateImage(&error_msg)) << error_msg;
73
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 Uhler93aa2102015-08-10 14:47:41 -070081 // Generate a non-PIC odex file for the purposes of test.
Richard Uhler94f5bda2015-07-22 08:25:11 -070082 // The generated odex file will be un-relocated.
Richard Uhler66d874d2015-01-15 09:37:19 -080083 void GenerateOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +000084 const std::string& odex_location,
Richard Uhlera48403e2016-04-26 10:24:38 -070085 CompilerFilter::Filter filter,
86 bool pic = false,
87 bool with_patch_info = true) {
88 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
89 // relocated image file.
David Sehrd106d9f2016-08-16 19:22:57 -070090 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
91 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
92 ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno);
93
Richard Uhler93aa2102015-08-10 14:47:41 -070094 std::vector<std::string> args;
95 args.push_back("--dex-file=" + dex_location);
Richard Uhlera48403e2016-04-26 10:24:38 -070096 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +000097 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhlera48403e2016-04-26 10:24:38 -070098 args.push_back("--runtime-arg");
99 args.push_back("-Xnorelocate");
Richard Uhler93aa2102015-08-10 14:47:41 -0700100
Richard Uhlera48403e2016-04-26 10:24:38 -0700101 if (pic) {
102 args.push_back("--compile-pic");
103 }
104
105 if (with_patch_info) {
106 args.push_back("--include-patch-information");
107 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700108
109 std::string error_msg;
110 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
David Sehrd106d9f2016-08-16 19:22:57 -0700111 ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno);
Richard Uhler93aa2102015-08-10 14:47:41 -0700112
113 // Verify the odex file was generated as expected and really is
114 // unrelocated.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800115 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
116 odex_location.c_str(),
117 nullptr,
118 nullptr,
119 false,
120 /*low_4gb*/false,
121 dex_location.c_str(),
122 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700123 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700124 EXPECT_EQ(pic, odex_file->IsPic());
125 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000126 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
127
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100128 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000129 const std::vector<gc::space::ImageSpace*> image_spaces =
Richard Uhlera48403e2016-04-26 10:24:38 -0700130 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000131 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
132 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
133 const OatHeader& oat_header = odex_file->GetOatHeader();
Jeff Haob11ffb72016-04-07 15:40:54 -0700134 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
135 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000136 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
137 oat_header.GetImageFileLocationOatDataBegin());
138 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
139 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700140 }
141
142 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000143 const std::string& odex_location,
144 CompilerFilter::Filter filter) {
Richard Uhlera48403e2016-04-26 10:24:38 -0700145 GenerateOdexForTest(dex_location, odex_location, filter, true, false);
Calin Juravleb077e152016-02-18 18:47:37 +0000146 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000147
Richard Uhlerd1537b52016-03-29 13:27:41 -0700148 // Generate a non-PIC odex file without patch information for the purposes
149 // of test. The generated odex file will be un-relocated.
Richard Uhlerd1537b52016-03-29 13:27:41 -0700150 void GenerateNoPatchOdexForTest(const std::string& dex_location,
151 const std::string& odex_location,
152 CompilerFilter::Filter filter) {
Richard Uhlera48403e2016-04-26 10:24:38 -0700153 GenerateOdexForTest(dex_location, odex_location, filter, false, false);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700154 }
155
Richard Uhler66d874d2015-01-15 09:37:19 -0800156 private:
157 // Reserve memory around where the image will be loaded so other memory
158 // won't conflict when it comes time to load the image.
159 // This can be called with an already loaded image to reserve the space
160 // around it.
161 void ReserveImageSpace() {
162 MemMap::Init();
163
164 // Ensure a chunk of memory is reserved for the image space.
Richard Uhlera48403e2016-04-26 10:24:38 -0700165 // The reservation_end includes room for the main space that has to come
166 // right after the image in case of the GSS collector.
167 uintptr_t reservation_start = ART_BASE_ADDRESS;
168 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800169
Richard Uhler66d874d2015-01-15 09:37:19 -0800170 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
171 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
172 for (BacktraceMap::const_iterator it = map->begin();
173 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700174 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
175 reservation_start = std::max(reservation_start, it->end);
176 }
177 ReserveImageSpaceChunk(reservation_start, reservation_end);
178 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800179
Richard Uhler3efe9792015-03-30 16:18:03 -0700180 // Reserve a chunk of memory for the image space in the given range.
181 // Only has effect for chunks with a positive number of bytes.
182 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
183 if (start < end) {
184 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800185 image_reservation_.push_back(std::unique_ptr<MemMap>(
186 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700187 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700188 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800189 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
190 LOG(INFO) << "Reserved space for image " <<
191 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
192 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800193 }
194 }
195
196
197 // Unreserve any memory reserved by ReserveImageSpace. This should be called
198 // before the image is loaded.
199 void UnreserveImageSpace() {
200 image_reservation_.clear();
201 }
202
Richard Uhler66d874d2015-01-15 09:37:19 -0800203 std::vector<std::unique_ptr<MemMap>> image_reservation_;
204};
205
206class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
207 public:
208 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
209 OatFileAssistantTest::SetUpRuntimeOptions(options);
210 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
211 }
212};
213
214// Generate an oat file for the purposes of test, as opposed to testing
215// generation of oat files.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000216static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
217 // Use an oat file assistant to find the proper oat location.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700218 std::string oat_location;
219 std::string error_msg;
220 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
221 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800222
Andreas Gampe29d38e72016-03-23 15:31:51 +0000223 std::vector<std::string> args;
224 args.push_back("--dex-file=" + std::string(dex_location));
Richard Uhlerb81881d2016-04-19 13:08:04 -0700225 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000226 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
227 args.push_back("--runtime-arg");
228 args.push_back("-Xnorelocate");
Andreas Gampe29d38e72016-03-23 15:31:51 +0000229 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
230
231 // Verify the oat file was generated as expected.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700232 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location.c_str(),
233 oat_location.c_str(),
Andreas Gampe29d38e72016-03-23 15:31:51 +0000234 nullptr,
235 nullptr,
236 false,
237 /*low_4gb*/false,
238 dex_location,
239 &error_msg));
240 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
241 EXPECT_EQ(filter, oat_file->GetCompilerFilter());
Richard Uhler66d874d2015-01-15 09:37:19 -0800242}
243
244// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700245// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800246TEST_F(OatFileAssistantTest, DexNoOat) {
247 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
248 Copy(GetDexSrc1(), dex_location);
249
Richard Uhlerd1472a22016-04-15 15:18:56 -0700250 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800251
Andreas Gampe29d38e72016-03-23 15:31:51 +0000252 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
253 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
254 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
255 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
256 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
257 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
258 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
259 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800260
261 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
262 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
263 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
264 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
265 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700266 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800267 EXPECT_FALSE(oat_file_assistant.OatFileExists());
268 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
269 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
270 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700271 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700272 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800273}
274
275// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700276// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800277TEST_F(OatFileAssistantTest, NoDexNoOat) {
278 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
279
Richard Uhlerd1472a22016-04-15 15:18:56 -0700280 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800281
Andreas Gampe29d38e72016-03-23 15:31:51 +0000282 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
283 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700284 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
285
286 // Trying to make the oat file up to date should not fail or crash.
287 std::string error_msg;
Richard Uhlerd1472a22016-04-15 15:18:56 -0700288 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700289
290 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
292 EXPECT_EQ(nullptr, oat_file.get());
293}
294
295// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700296// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800297TEST_F(OatFileAssistantTest, OatUpToDate) {
298 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
299 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000300 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800301
Richard Uhlerd1472a22016-04-15 15:18:56 -0700302 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800303
Andreas Gampe29d38e72016-03-23 15:31:51 +0000304 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
305 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
306 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
307 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
308 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
309 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
310 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
311 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
312
Richard Uhler66d874d2015-01-15 09:37:19 -0800313 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
314 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
315 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
316 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
317 EXPECT_TRUE(oat_file_assistant.OatFileExists());
318 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
319 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
320 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700321 EXPECT_EQ(OatFileAssistant::kOatUpToDate, 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
Richard Uhler9a37efc2016-08-05 16:32:55 -0700325// Case: We have a DEX file and ODEX file for a different dex location.
326// Expect: The status is kDex2OatNeeded.
327TEST_F(OatFileAssistantTest, OatForDifferentDex) {
328 // Generate an odex file for OatForDifferentDex_A.jar
329 std::string dex_location_a = GetScratchDir() + "/OatForDifferentDex_A.jar";
330 std::string odex_location = GetOdexDir() + "/OatForDifferentDex.odex";
331 Copy(GetDexSrc1(), dex_location_a);
332 GenerateOdexForTest(dex_location_a, odex_location, CompilerFilter::kSpeed);
333
334 // Try to use that odex file for OatForDifferentDex.jar
335 std::string dex_location = GetScratchDir() + "/OatForDifferentDex.jar";
336 Copy(GetDexSrc1(), dex_location);
337
338 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
339
340 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
341 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
342
343 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
344 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
345 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
346 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
347 EXPECT_FALSE(oat_file_assistant.OatFileExists());
348 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
349 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
350 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
351}
352
Andreas Gampe29d38e72016-03-23 15:31:51 +0000353// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700354// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
355// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000356TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
357 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
358 Copy(GetDexSrc1(), dex_location);
359 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
360
Richard Uhlerd1472a22016-04-15 15:18:56 -0700361 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000362
363 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700364 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000365 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700366 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, false));
367 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
368 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
369 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
370 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000371
372 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
373 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
374 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
375 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
376 EXPECT_TRUE(oat_file_assistant.OatFileExists());
377 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
378 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
379 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
380 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
381 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
382}
383
Richard Uhler66d874d2015-01-15 09:37:19 -0800384// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700385// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800386TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
387 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
388 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000389 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800390
Richard Uhlerd1472a22016-04-15 15:18:56 -0700391 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000392 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700393 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700394 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700395
396 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700397 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800398 ASSERT_TRUE(oat_file.get() != nullptr);
399 EXPECT_TRUE(oat_file->IsExecutable());
400 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700401 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
402 EXPECT_EQ(2u, dex_files.size());
403}
404
Richard Uhler67ff7d12015-05-14 13:21:13 -0700405// Case: We have a MultiDEX file where the secondary dex file is out of date.
406// Expect: The status is kDex2OatNeeded.
407TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
408 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
409
410 // Compile code for GetMultiDexSrc1.
411 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000412 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700413
414 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
415 // is out of date.
416 Copy(GetMultiDexSrc2(), dex_location);
417
Richard Uhlerd1472a22016-04-15 15:18:56 -0700418 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000419 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700420 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700421 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700422}
423
Richard Uhlere5fed032015-03-18 08:21:11 -0700424// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
425// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700426// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700427TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
428 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700429 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700430
431 // Create the dex file
432 Copy(GetMultiDexSrc1(), dex_location);
433
434 // Create the oat file with relative encoded dex location.
435 std::vector<std::string> args;
436 args.push_back("--dex-file=" + dex_location);
437 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
438 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000439 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700440
441 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700442 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700443
444 // Verify we can load both dex files.
445 OatFileAssistant oat_file_assistant(dex_location.c_str(),
446 oat_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700447 kRuntimeISA, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700448 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
449 ASSERT_TRUE(oat_file.get() != nullptr);
450 EXPECT_TRUE(oat_file->IsExecutable());
451 std::vector<std::unique_ptr<const DexFile>> dex_files;
452 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800453 EXPECT_EQ(2u, dex_files.size());
454}
455
Richard Uhler95abd042015-03-24 09:51:28 -0700456// Case: We have a DEX file and out-of-date OAT file.
457// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800458TEST_F(OatFileAssistantTest, OatOutOfDate) {
459 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
460
461 // We create a dex, generate an oat for it, then overwrite the dex with a
462 // different dex to make the oat out of date.
463 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000464 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800465 Copy(GetDexSrc2(), dex_location);
466
Richard Uhlerd1472a22016-04-15 15:18:56 -0700467 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000468 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
469 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
470 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
471 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800472
473 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
474 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
475 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
476 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
477 EXPECT_TRUE(oat_file_assistant.OatFileExists());
478 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
479 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700480 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800481}
482
483// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700484// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800485TEST_F(OatFileAssistantTest, DexOdexNoOat) {
486 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700487 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800488
489 // Create the dex and odex files
490 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000491 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800492
493 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700494 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800495
Andreas Gampe29d38e72016-03-23 15:31:51 +0000496 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
497 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
498 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
499 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800500
501 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
502 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
503 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
504 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
505 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800506 EXPECT_FALSE(oat_file_assistant.OatFileExists());
507 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
508 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700509 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700510
511 // We should still be able to get the non-executable odex file to run from.
512 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
513 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800514}
515
516// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700517// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800518TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
519 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700520 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800521
522 // Create the dex and odex files
523 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000524 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800525
526 // Strip the dex file
527 Copy(GetStrippedDexSrc1(), dex_location);
528
529 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700530 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800531
Andreas Gampe29d38e72016-03-23 15:31:51 +0000532 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
533 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800534
535 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
536 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
537 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
538 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
539 EXPECT_FALSE(oat_file_assistant.OatFileExists());
540 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
541 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700542 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800543
544 // Make the oat file up to date.
545 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700546 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700547 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700548 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800549
Andreas Gampe29d38e72016-03-23 15:31:51 +0000550 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
551 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800552
553 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
554 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
555 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
556 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
557 EXPECT_TRUE(oat_file_assistant.OatFileExists());
558 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
559 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700560 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800561
562 // Verify we can load the dex files from it.
563 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
564 ASSERT_TRUE(oat_file.get() != nullptr);
565 EXPECT_TRUE(oat_file->IsExecutable());
566 std::vector<std::unique_ptr<const DexFile>> dex_files;
567 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
568 EXPECT_EQ(1u, dex_files.size());
569}
570
Richard Uhler95abd042015-03-24 09:51:28 -0700571// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
572// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800573TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
574 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700575 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800576
577 // Create the oat file from a different dex file so it looks out of date.
578 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000579 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800580
581 // Create the odex file
582 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000583 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800584
585 // Strip the dex file.
586 Copy(GetStrippedDexSrc1(), dex_location);
587
588 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700589 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800590
Andreas Gampe29d38e72016-03-23 15:31:51 +0000591 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
592 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
593 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
594 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
595 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
596 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800597
598 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
599 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
600 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
601 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
602 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
603 EXPECT_TRUE(oat_file_assistant.OatFileExists());
604 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
605 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700606 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800607
608 // Make the oat file up to date.
609 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700610 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700611 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700612 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800613
Andreas Gampe29d38e72016-03-23 15:31:51 +0000614 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
615 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
616 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
617 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800618
619 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
620 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
621 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
622 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
623 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
624 EXPECT_TRUE(oat_file_assistant.OatFileExists());
625 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
626 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
627 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700628 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800629
630 // Verify we can load the dex files from it.
631 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
632 ASSERT_TRUE(oat_file.get() != nullptr);
633 EXPECT_TRUE(oat_file->IsExecutable());
634 std::vector<std::unique_ptr<const DexFile>> dex_files;
635 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
636 EXPECT_EQ(1u, dex_files.size());
637}
638
Richard Uhler9b994ea2015-06-24 08:44:19 -0700639// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
640// OAT file. Expect: The status is kNoDexOptNeeded.
641TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
642 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
643
644 Copy(GetStrippedDexSrc1(), dex_location);
645
646 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700647 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700648
Andreas Gampe29d38e72016-03-23 15:31:51 +0000649 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
650 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
651 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
652 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
653 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
654 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700655
656 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
657 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
658 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
659 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
660 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
661 EXPECT_FALSE(oat_file_assistant.OatFileExists());
662 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
663 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
664 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
665
666 // Make the oat file up to date. This should have no effect.
667 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700668 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700669 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700670 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700671
Andreas Gampe29d38e72016-03-23 15:31:51 +0000672 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
673 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700674
675 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
676 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
677 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
678 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
679 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
680 EXPECT_FALSE(oat_file_assistant.OatFileExists());
681 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
682 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
683 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
684}
685
Richard Uhler95abd042015-03-24 09:51:28 -0700686// Case: We have a DEX file, no ODEX file and an OAT file that needs
687// relocation.
688// Expect: The status is kSelfPatchOatNeeded.
689TEST_F(OatFileAssistantTest, SelfRelocation) {
690 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
691 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
692
693 // Create the dex and odex files
694 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000695 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700696
697 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700698 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700699
Andreas Gampe29d38e72016-03-23 15:31:51 +0000700 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
701 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
702 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
703 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
704 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
705 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700706
707 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
708 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
709 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
710 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
711 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
712 EXPECT_TRUE(oat_file_assistant.OatFileExists());
713 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
714 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
715 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700716 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700717
718 // Make the oat file up to date.
719 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700720 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700721 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700722 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700723
Andreas Gampe29d38e72016-03-23 15:31:51 +0000724 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
725 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700726
727 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
728 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
729 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
730 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
731 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
732 EXPECT_TRUE(oat_file_assistant.OatFileExists());
733 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
734 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
735 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700736 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700737
738 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
739 ASSERT_TRUE(oat_file.get() != nullptr);
740 EXPECT_TRUE(oat_file->IsExecutable());
741 std::vector<std::unique_ptr<const DexFile>> dex_files;
742 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
743 EXPECT_EQ(1u, dex_files.size());
744}
745
Richard Uhlerd1537b52016-03-29 13:27:41 -0700746// Case: We have a DEX file, no ODEX file and an OAT file that needs
747// relocation but doesn't have patch info.
748// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
749TEST_F(OatFileAssistantTest, NoSelfRelocation) {
750 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
751 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
752
753 // Create the dex and odex files
754 Copy(GetDexSrc1(), dex_location);
755 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
756
757 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700758 oat_location.c_str(), kRuntimeISA, true);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700759
760 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
761 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
762
763 // Make the oat file up to date.
764 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700765 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700766 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700767 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerd1537b52016-03-29 13:27:41 -0700768 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
769 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
770
771 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
772 ASSERT_TRUE(oat_file.get() != nullptr);
773 EXPECT_TRUE(oat_file->IsExecutable());
774 std::vector<std::unique_ptr<const DexFile>> dex_files;
775 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
776 EXPECT_EQ(1u, dex_files.size());
777}
778
Richard Uhler66d874d2015-01-15 09:37:19 -0800779// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
780// OAT files both have patch delta of 0.
Richard Uhler70a84262016-11-08 16:51:51 +0000781// Expect: It shouldn't crash, and status is kSelfPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800782TEST_F(OatFileAssistantTest, OdexOatOverlap) {
783 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700784 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
785 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800786
787 // Create the dex and odex files
788 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000789 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800790
791 // Create the oat file by copying the odex so they are located in the same
792 // place in memory.
793 Copy(odex_location, oat_location);
794
795 // Verify things don't go bad.
796 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700797 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800798
Richard Uhler70a84262016-11-08 16:51:51 +0000799 // kSelfPatchOatNeeded is expected rather than kPatchOatNeeded based on the
800 // assumption that the oat location is more up-to-date than the odex
801 // location, even if they both need relocation.
802 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000803 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800804
805 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
806 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
807 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
808 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
809 EXPECT_TRUE(oat_file_assistant.OatFileExists());
810 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
811 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700812 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800813
814 // Things aren't relocated, so it should fall back to interpreted.
815 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
816 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700817
Richard Uhler66d874d2015-01-15 09:37:19 -0800818 EXPECT_FALSE(oat_file->IsExecutable());
819 std::vector<std::unique_ptr<const DexFile>> dex_files;
820 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
821 EXPECT_EQ(1u, dex_files.size());
822}
823
824// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700825// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800826TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
827 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700828 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800829
830 // Create the dex and odex files
831 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000832 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800833
834 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700835 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800836
Andreas Gampe29d38e72016-03-23 15:31:51 +0000837 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
838 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
839 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
840 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800841
842 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
843 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
844 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
845 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
846 EXPECT_FALSE(oat_file_assistant.OatFileExists());
847 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
848 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700849 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800850}
851
Andreas Gampe29d38e72016-03-23 15:31:51 +0000852// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
853// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
854TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
855 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
856 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000857
858 // Create the dex and odex files
859 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000860 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000861
862 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700863 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000864
Andreas Gampe29d38e72016-03-23 15:31:51 +0000865 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
866 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
867 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
868 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000869
870 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
871 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
872 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
Nicolas Geoffray845e5062016-03-23 06:42:05 +0000873 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000874 EXPECT_FALSE(oat_file_assistant.OatFileExists());
875 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
876 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
877 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
878}
879
Richard Uhler66d874d2015-01-15 09:37:19 -0800880// Case: We have a DEX file and up-to-date OAT file for it.
881// Expect: We should load an executable dex file.
882TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
883 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
884
885 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000886 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800887
888 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700889 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000890
891 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
892 ASSERT_TRUE(oat_file.get() != nullptr);
893 EXPECT_TRUE(oat_file->IsExecutable());
894 std::vector<std::unique_ptr<const DexFile>> dex_files;
895 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
896 EXPECT_EQ(1u, dex_files.size());
897}
898
899// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
900// Expect: We should still load the oat file as executable.
901TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
902 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
903
904 Copy(GetDexSrc1(), dex_location);
905 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
906
907 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700908 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800909
910 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
911 ASSERT_TRUE(oat_file.get() != nullptr);
912 EXPECT_TRUE(oat_file->IsExecutable());
913 std::vector<std::unique_ptr<const DexFile>> dex_files;
914 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
915 EXPECT_EQ(1u, dex_files.size());
916}
917
918// Case: We have a DEX file and up-to-date OAT file for it.
919// Expect: Loading non-executable should load the oat non-executable.
920TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
921 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
922
923 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000924 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800925
926 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700927 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800928
929 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
930 ASSERT_TRUE(oat_file.get() != nullptr);
931 EXPECT_FALSE(oat_file->IsExecutable());
932 std::vector<std::unique_ptr<const DexFile>> dex_files;
933 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
934 EXPECT_EQ(1u, dex_files.size());
935}
936
937// Case: We have a DEX file.
938// Expect: We should load an executable dex file from an alternative oat
939// location.
940TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
941 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
942 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
943
944 Copy(GetDexSrc1(), dex_location);
945
946 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700947 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800948 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700949 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700950 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700951 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
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_TRUE(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 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
961
962 // Verify it didn't create an oat in the default location.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700963 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800964 EXPECT_FALSE(ofm.OatFileExists());
965}
966
Richard Uhler8327cf72015-10-13 16:34:59 -0700967// Case: We have a DEX file but can't write the oat file.
968// Expect: We should fail to make the oat file up to date.
969TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
970 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
971
972 // Make the oat location unwritable by inserting some non-existent
973 // intermediate directories.
974 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
975
976 Copy(GetDexSrc1(), dex_location);
977
978 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700979 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700980 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700981 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700982 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700983 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700984
985 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
986 ASSERT_TRUE(oat_file.get() == nullptr);
987}
988
989// Case: We don't have a DEX file and can't write the oat file.
990// Expect: We should fail to generate the oat file without crashing.
991TEST_F(OatFileAssistantTest, GenNoDex) {
992 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
993 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
994
995 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700996 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700997 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700998 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700999 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001000 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -07001001}
1002
Richard Uhler66d874d2015-01-15 09:37:19 -08001003// Turn an absolute path into a path relative to the current working
1004// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001005static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001006 char buf[MAXPATHLEN];
1007 std::string cwd = getcwd(buf, MAXPATHLEN);
1008
1009 // Split the target and cwd paths into components.
1010 std::vector<std::string> target_path;
1011 std::vector<std::string> cwd_path;
1012 Split(target, '/', &target_path);
1013 Split(cwd, '/', &cwd_path);
1014
1015 // Reverse the path components, so we can use pop_back().
1016 std::reverse(target_path.begin(), target_path.end());
1017 std::reverse(cwd_path.begin(), cwd_path.end());
1018
1019 // Drop the common prefix of the paths. Because we reversed the path
1020 // components, this becomes the common suffix of target_path and cwd_path.
1021 while (!target_path.empty() && !cwd_path.empty()
1022 && target_path.back() == cwd_path.back()) {
1023 target_path.pop_back();
1024 cwd_path.pop_back();
1025 }
1026
1027 // For each element of the remaining cwd_path, add '..' to the beginning
1028 // of the target path. Because we reversed the path components, we add to
1029 // the end of target_path.
1030 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1031 target_path.push_back("..");
1032 }
1033
1034 // Reverse again to get the right path order, and join to get the result.
1035 std::reverse(target_path.begin(), target_path.end());
1036 return Join(target_path, '/');
1037}
1038
1039// Case: Non-absolute path to Dex location.
1040// Expect: Not sure, but it shouldn't crash.
1041TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1042 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1043 Copy(GetDexSrc1(), abs_dex_location);
1044
1045 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001046 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001047
1048 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001049 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1050 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001051 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1052 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1053 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1054 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1055 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1056 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1057}
1058
1059// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001060// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001061TEST_F(OatFileAssistantTest, ShortDexLocation) {
1062 std::string dex_location = "/xx";
1063
Richard Uhlerd1472a22016-04-15 15:18:56 -07001064 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001065
1066 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001067 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1068 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001069 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1070 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1071 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1072 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1073 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1074 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001075 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001076
Richard Uhler9b994ea2015-06-24 08:44:19 -07001077 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001078 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001079 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001080 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001081 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001082 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001083}
1084
1085// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001086// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001087TEST_F(OatFileAssistantTest, LongDexExtension) {
1088 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1089 Copy(GetDexSrc1(), dex_location);
1090
Richard Uhlerd1472a22016-04-15 15:18:56 -07001091 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001092
Andreas Gampe29d38e72016-03-23 15:31:51 +00001093 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1094 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001095
1096 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1097 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1098 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1099 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1100 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1101 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1102 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1103}
1104
1105// A task to generate a dex location. Used by the RaceToGenerate test.
1106class RaceGenerateTask : public Task {
1107 public:
1108 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001109 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001110 {}
1111
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001112 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001113 // Load the dex files, and save a pointer to the loaded oat file, so that
1114 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001115 std::vector<std::unique_ptr<const DexFile>> dex_files;
1116 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001117 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001118 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1119 dex_location_.c_str(),
1120 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001121 /*class_loader*/nullptr,
1122 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001123 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001124 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001125 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001126 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1127 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001128 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001129 }
1130
1131 const OatFile* GetLoadedOatFile() const {
1132 return loaded_oat_file_;
1133 }
1134
1135 private:
1136 std::string dex_location_;
1137 std::string oat_location_;
1138 const OatFile* loaded_oat_file_;
1139};
1140
1141// Test the case where multiple processes race to generate an oat file.
1142// This simulates multiple processes using multiple threads.
1143//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001144// We want unique Oat files to be loaded even when there is a race to load.
1145// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1146// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001147TEST_F(OatFileAssistantTest, RaceToGenerate) {
1148 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001149 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001150
1151 // We use the lib core dex file, because it's large, and hopefully should
1152 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001153 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001154
1155 const int kNumThreads = 32;
1156 Thread* self = Thread::Current();
1157 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1158 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1159 for (int i = 0; i < kNumThreads; i++) {
1160 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1161 thread_pool.AddTask(self, task.get());
1162 tasks.push_back(std::move(task));
1163 }
1164 thread_pool.StartWorkers(self);
1165 thread_pool.Wait(self, true, false);
1166
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001167 // Verify every task got a unique oat file.
1168 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001169 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001170 const OatFile* oat_file = task->GetLoadedOatFile();
1171 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1172 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001173 }
1174}
1175
1176// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1177// disabled.
1178// Expect: We should load the odex file non-executable.
1179TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1180 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001181 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001182
1183 // Create the dex and odex files
1184 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001185 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001186
1187 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001188 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001189
1190 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1191 ASSERT_TRUE(oat_file.get() != nullptr);
1192 EXPECT_FALSE(oat_file->IsExecutable());
1193 std::vector<std::unique_ptr<const DexFile>> dex_files;
1194 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1195 EXPECT_EQ(1u, dex_files.size());
1196}
1197
1198// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1199// disabled.
1200// Expect: We should load the odex file non-executable.
1201TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1202 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001203 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001204
1205 // Create the dex and odex files
1206 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001207 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001208
1209 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001210 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001211
1212 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1213 ASSERT_TRUE(oat_file.get() != nullptr);
1214 EXPECT_FALSE(oat_file->IsExecutable());
1215 std::vector<std::unique_ptr<const DexFile>> dex_files;
1216 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1217 EXPECT_EQ(2u, dex_files.size());
1218}
1219
Richard Uhlerf4b34872016-04-13 11:03:46 -07001220TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1221 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1222 Copy(GetDexSrc1(), dex_location);
1223
Richard Uhlerd1472a22016-04-15 15:18:56 -07001224 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001225
1226 std::string error_msg;
1227 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1228 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001229 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001230 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1231 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1232 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1233 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1234
1235 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1236 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001237 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001238 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1239 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1240 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1241 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1242
1243 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1244 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001245 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001246}
1247
Richard Uhlerb81881d2016-04-19 13:08:04 -07001248TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001249 std::string error_msg;
1250 std::string odex_file;
1251
Richard Uhlerb81881d2016-04-19 13:08:04 -07001252 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001253 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001254 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001255
Richard Uhlerb81881d2016-04-19 13:08:04 -07001256 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001257 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001258 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001259
Richard Uhlerb81881d2016-04-19 13:08:04 -07001260 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001261 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001262 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001263 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001264}
1265
Richard Uhler23cedd22015-04-08 13:17:29 -07001266// Verify the dexopt status values from dalvik.system.DexFile
1267// match the OatFileAssistant::DexOptStatus values.
1268TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1269 ScopedObjectAccess soa(Thread::Current());
1270 StackHandleScope<1> hs(soa.Self());
1271 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1272 Handle<mirror::Class> dexfile(
1273 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1274 ASSERT_FALSE(dexfile.Get() == nullptr);
1275 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1276
Mathieu Chartierc7853442015-03-27 14:35:38 -07001277 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001278 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1279 ASSERT_FALSE(no_dexopt_needed == nullptr);
1280 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1281 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1282
Mathieu Chartierc7853442015-03-27 14:35:38 -07001283 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001284 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1285 ASSERT_FALSE(dex2oat_needed == nullptr);
1286 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1287 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1288
Mathieu Chartierc7853442015-03-27 14:35:38 -07001289 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001290 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1291 ASSERT_FALSE(patchoat_needed == nullptr);
1292 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1293 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1294
Mathieu Chartierc7853442015-03-27 14:35:38 -07001295 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001296 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1297 ASSERT_FALSE(self_patchoat_needed == nullptr);
1298 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1299 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1300}
Richard Uhler66d874d2015-01-15 09:37:19 -08001301
1302// TODO: More Tests:
Andreas Gampe29d38e72016-03-23 15:31:51 +00001303// * Image checksum change is out of date for kIntepretOnly, but not
1304// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1305// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001306// * Test class linker falls back to unquickened dex for DexNoOat
1307// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001308// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001309// * Test for status of oat while oat is being generated (how?)
1310// * Test case where 32 and 64 bit boot class paths differ,
1311// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1312// 64 bit boot class paths.
1313// * Test unexpected scenarios (?):
1314// - Dex is stripped, don't have odex.
1315// - Oat file corrupted after status check, before reload unexecutable
1316// because it's unrelocated and no dex2oat
Calin Juravleb077e152016-02-18 18:47:37 +00001317// * Test unrelocated specific target compilation type can be relocated to
1318// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001319
1320} // namespace art