blob: d4337b971bf8923b82c51fbe822481824914b9d7 [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 Uhler95abd042015-03-24 09:51:28 -0700781// Expect: It shouldn't crash, and status is kPatchOatNeeded.
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
Andreas Gampe29d38e72016-03-23 15:31:51 +0000799 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
800 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800801
802 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
803 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
804 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
805 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
806 EXPECT_TRUE(oat_file_assistant.OatFileExists());
807 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
808 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700809 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800810
811 // Things aren't relocated, so it should fall back to interpreted.
812 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
813 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700814
Richard Uhler66d874d2015-01-15 09:37:19 -0800815 EXPECT_FALSE(oat_file->IsExecutable());
816 std::vector<std::unique_ptr<const DexFile>> dex_files;
817 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
818 EXPECT_EQ(1u, dex_files.size());
819}
820
821// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700822// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800823TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
824 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700825 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800826
827 // Create the dex and odex files
828 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000829 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800830
831 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700832 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800833
Andreas Gampe29d38e72016-03-23 15:31:51 +0000834 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
835 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
836 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
837 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800838
839 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
840 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
841 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
842 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
843 EXPECT_FALSE(oat_file_assistant.OatFileExists());
844 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
845 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700846 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800847}
848
Andreas Gampe29d38e72016-03-23 15:31:51 +0000849// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
850// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
851TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
852 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
853 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000854
855 // Create the dex and odex files
856 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000857 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000858
859 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700860 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000861
Andreas Gampe29d38e72016-03-23 15:31:51 +0000862 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
863 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
864 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
865 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000866
867 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
868 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
869 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
Nicolas Geoffray845e5062016-03-23 06:42:05 +0000870 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000871 EXPECT_FALSE(oat_file_assistant.OatFileExists());
872 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
873 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
874 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
875}
876
Richard Uhler66d874d2015-01-15 09:37:19 -0800877// Case: We have a DEX file and up-to-date OAT file for it.
878// Expect: We should load an executable dex file.
879TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
880 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
881
882 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000883 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800884
885 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700886 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000887
888 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
889 ASSERT_TRUE(oat_file.get() != nullptr);
890 EXPECT_TRUE(oat_file->IsExecutable());
891 std::vector<std::unique_ptr<const DexFile>> dex_files;
892 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
893 EXPECT_EQ(1u, dex_files.size());
894}
895
896// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
897// Expect: We should still load the oat file as executable.
898TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
899 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
900
901 Copy(GetDexSrc1(), dex_location);
902 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
903
904 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700905 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800906
907 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
908 ASSERT_TRUE(oat_file.get() != nullptr);
909 EXPECT_TRUE(oat_file->IsExecutable());
910 std::vector<std::unique_ptr<const DexFile>> dex_files;
911 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
912 EXPECT_EQ(1u, dex_files.size());
913}
914
915// Case: We have a DEX file and up-to-date OAT file for it.
916// Expect: Loading non-executable should load the oat non-executable.
917TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
918 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
919
920 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000921 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800922
923 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700924 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800925
926 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
927 ASSERT_TRUE(oat_file.get() != nullptr);
928 EXPECT_FALSE(oat_file->IsExecutable());
929 std::vector<std::unique_ptr<const DexFile>> dex_files;
930 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
931 EXPECT_EQ(1u, dex_files.size());
932}
933
934// Case: We have a DEX file.
935// Expect: We should load an executable dex file from an alternative oat
936// location.
937TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
938 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
939 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
940
941 Copy(GetDexSrc1(), dex_location);
942
943 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700944 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800945 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700946 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700947 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700948 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800949
950 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
951 ASSERT_TRUE(oat_file.get() != nullptr);
952 EXPECT_TRUE(oat_file->IsExecutable());
953 std::vector<std::unique_ptr<const DexFile>> dex_files;
954 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
955 EXPECT_EQ(1u, dex_files.size());
956
957 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
958
959 // Verify it didn't create an oat in the default location.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700960 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800961 EXPECT_FALSE(ofm.OatFileExists());
962}
963
Richard Uhler8327cf72015-10-13 16:34:59 -0700964// Case: We have a DEX file but can't write the oat file.
965// Expect: We should fail to make the oat file up to date.
966TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
967 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
968
969 // Make the oat location unwritable by inserting some non-existent
970 // intermediate directories.
971 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
972
973 Copy(GetDexSrc1(), dex_location);
974
975 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700976 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700977 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700978 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700979 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700980 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700981
982 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
983 ASSERT_TRUE(oat_file.get() == nullptr);
984}
985
986// Case: We don't have a DEX file and can't write the oat file.
987// Expect: We should fail to generate the oat file without crashing.
988TEST_F(OatFileAssistantTest, GenNoDex) {
989 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
990 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
991
992 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700993 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700994 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700995 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700996 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700997 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700998}
999
Richard Uhler66d874d2015-01-15 09:37:19 -08001000// Turn an absolute path into a path relative to the current working
1001// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -08001002static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001003 char buf[MAXPATHLEN];
1004 std::string cwd = getcwd(buf, MAXPATHLEN);
1005
1006 // Split the target and cwd paths into components.
1007 std::vector<std::string> target_path;
1008 std::vector<std::string> cwd_path;
1009 Split(target, '/', &target_path);
1010 Split(cwd, '/', &cwd_path);
1011
1012 // Reverse the path components, so we can use pop_back().
1013 std::reverse(target_path.begin(), target_path.end());
1014 std::reverse(cwd_path.begin(), cwd_path.end());
1015
1016 // Drop the common prefix of the paths. Because we reversed the path
1017 // components, this becomes the common suffix of target_path and cwd_path.
1018 while (!target_path.empty() && !cwd_path.empty()
1019 && target_path.back() == cwd_path.back()) {
1020 target_path.pop_back();
1021 cwd_path.pop_back();
1022 }
1023
1024 // For each element of the remaining cwd_path, add '..' to the beginning
1025 // of the target path. Because we reversed the path components, we add to
1026 // the end of target_path.
1027 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1028 target_path.push_back("..");
1029 }
1030
1031 // Reverse again to get the right path order, and join to get the result.
1032 std::reverse(target_path.begin(), target_path.end());
1033 return Join(target_path, '/');
1034}
1035
1036// Case: Non-absolute path to Dex location.
1037// Expect: Not sure, but it shouldn't crash.
1038TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1039 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1040 Copy(GetDexSrc1(), abs_dex_location);
1041
1042 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001043 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001044
1045 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001046 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1047 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001048 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1049 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1050 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1051 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1052 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1053 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1054}
1055
1056// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001057// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001058TEST_F(OatFileAssistantTest, ShortDexLocation) {
1059 std::string dex_location = "/xx";
1060
Richard Uhlerd1472a22016-04-15 15:18:56 -07001061 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001062
1063 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001064 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1065 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001066 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1067 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1068 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1069 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1070 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1071 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001072 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001073
Richard Uhler9b994ea2015-06-24 08:44:19 -07001074 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001075 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001076 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001077 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001078 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001079 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001080}
1081
1082// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001083// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001084TEST_F(OatFileAssistantTest, LongDexExtension) {
1085 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1086 Copy(GetDexSrc1(), dex_location);
1087
Richard Uhlerd1472a22016-04-15 15:18:56 -07001088 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001089
Andreas Gampe29d38e72016-03-23 15:31:51 +00001090 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1091 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001092
1093 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1094 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1095 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1096 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1097 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1098 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1099 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1100}
1101
1102// A task to generate a dex location. Used by the RaceToGenerate test.
1103class RaceGenerateTask : public Task {
1104 public:
1105 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001106 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001107 {}
1108
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001109 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001110 // Load the dex files, and save a pointer to the loaded oat file, so that
1111 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001112 std::vector<std::unique_ptr<const DexFile>> dex_files;
1113 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001114 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001115 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1116 dex_location_.c_str(),
1117 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001118 /*class_loader*/nullptr,
1119 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001120 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001121 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001122 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001123 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1124 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001125 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001126 }
1127
1128 const OatFile* GetLoadedOatFile() const {
1129 return loaded_oat_file_;
1130 }
1131
1132 private:
1133 std::string dex_location_;
1134 std::string oat_location_;
1135 const OatFile* loaded_oat_file_;
1136};
1137
1138// Test the case where multiple processes race to generate an oat file.
1139// This simulates multiple processes using multiple threads.
1140//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001141// We want unique Oat files to be loaded even when there is a race to load.
1142// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1143// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001144TEST_F(OatFileAssistantTest, RaceToGenerate) {
1145 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001146 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001147
1148 // We use the lib core dex file, because it's large, and hopefully should
1149 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001150 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001151
1152 const int kNumThreads = 32;
1153 Thread* self = Thread::Current();
1154 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1155 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1156 for (int i = 0; i < kNumThreads; i++) {
1157 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1158 thread_pool.AddTask(self, task.get());
1159 tasks.push_back(std::move(task));
1160 }
1161 thread_pool.StartWorkers(self);
1162 thread_pool.Wait(self, true, false);
1163
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001164 // Verify every task got a unique oat file.
1165 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001166 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001167 const OatFile* oat_file = task->GetLoadedOatFile();
1168 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1169 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001170 }
1171}
1172
1173// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1174// disabled.
1175// Expect: We should load the odex file non-executable.
1176TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1177 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001178 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001179
1180 // Create the dex and odex files
1181 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001182 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001183
1184 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001185 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001186
1187 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1188 ASSERT_TRUE(oat_file.get() != nullptr);
1189 EXPECT_FALSE(oat_file->IsExecutable());
1190 std::vector<std::unique_ptr<const DexFile>> dex_files;
1191 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1192 EXPECT_EQ(1u, dex_files.size());
1193}
1194
1195// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1196// disabled.
1197// Expect: We should load the odex file non-executable.
1198TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1199 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001200 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001201
1202 // Create the dex and odex files
1203 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001204 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001205
1206 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001207 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001208
1209 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1210 ASSERT_TRUE(oat_file.get() != nullptr);
1211 EXPECT_FALSE(oat_file->IsExecutable());
1212 std::vector<std::unique_ptr<const DexFile>> dex_files;
1213 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1214 EXPECT_EQ(2u, dex_files.size());
1215}
1216
Richard Uhlerf4b34872016-04-13 11:03:46 -07001217TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1218 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1219 Copy(GetDexSrc1(), dex_location);
1220
Richard Uhlerd1472a22016-04-15 15:18:56 -07001221 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001222
1223 std::string error_msg;
1224 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1225 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001226 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001227 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1228 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1229 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1230 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1231
1232 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1233 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001234 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001235 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1236 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1237 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1238 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1239
1240 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1241 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001242 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001243}
1244
Richard Uhlerb81881d2016-04-19 13:08:04 -07001245TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001246 std::string error_msg;
1247 std::string odex_file;
1248
Richard Uhlerb81881d2016-04-19 13:08:04 -07001249 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001250 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001251 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001252
Richard Uhlerb81881d2016-04-19 13:08:04 -07001253 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001254 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001255 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001256
Richard Uhlerb81881d2016-04-19 13:08:04 -07001257 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001258 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001259 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001260 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001261}
1262
Richard Uhler23cedd22015-04-08 13:17:29 -07001263// Verify the dexopt status values from dalvik.system.DexFile
1264// match the OatFileAssistant::DexOptStatus values.
1265TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1266 ScopedObjectAccess soa(Thread::Current());
1267 StackHandleScope<1> hs(soa.Self());
1268 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1269 Handle<mirror::Class> dexfile(
1270 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1271 ASSERT_FALSE(dexfile.Get() == nullptr);
1272 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1273
Mathieu Chartierc7853442015-03-27 14:35:38 -07001274 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001275 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1276 ASSERT_FALSE(no_dexopt_needed == nullptr);
1277 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1278 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1279
Mathieu Chartierc7853442015-03-27 14:35:38 -07001280 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001281 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1282 ASSERT_FALSE(dex2oat_needed == nullptr);
1283 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1284 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1285
Mathieu Chartierc7853442015-03-27 14:35:38 -07001286 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001287 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1288 ASSERT_FALSE(patchoat_needed == nullptr);
1289 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1290 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1291
Mathieu Chartierc7853442015-03-27 14:35:38 -07001292 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001293 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1294 ASSERT_FALSE(self_patchoat_needed == nullptr);
1295 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1296 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1297}
Richard Uhler66d874d2015-01-15 09:37:19 -08001298
1299// TODO: More Tests:
Andreas Gampe29d38e72016-03-23 15:31:51 +00001300// * Image checksum change is out of date for kIntepretOnly, but not
1301// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1302// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001303// * Test class linker falls back to unquickened dex for DexNoOat
1304// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001305// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001306// * Test for status of oat while oat is being generated (how?)
1307// * Test case where 32 and 64 bit boot class paths differ,
1308// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1309// 64 bit boot class paths.
1310// * Test unexpected scenarios (?):
1311// - Dex is stripped, don't have odex.
1312// - Oat file corrupted after status check, before reload unexecutable
1313// because it's unrelocated and no dex2oat
Calin Juravleb077e152016-02-18 18:47:37 +00001314// * Test unrelocated specific target compilation type can be relocated to
1315// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001316
1317} // namespace art