blob: 05c5a224528afdea26bb953351aa9d0b894bd003 [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"
Richard Uhler23cedd22015-04-08 13:17:29 -070036#include "scoped_thread_state_change.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080037#include "thread-inl.h"
38#include "utils.h"
39
40namespace art {
41
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.
90 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
91 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
Richard Uhler93aa2102015-08-10 14:47:41 -070092 std::vector<std::string> args;
93 args.push_back("--dex-file=" + dex_location);
Richard Uhlera48403e2016-04-26 10:24:38 -070094 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +000095 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
Richard Uhlera48403e2016-04-26 10:24:38 -070096 args.push_back("--runtime-arg");
97 args.push_back("-Xnorelocate");
Richard Uhler93aa2102015-08-10 14:47:41 -070098
Richard Uhlera48403e2016-04-26 10:24:38 -070099 if (pic) {
100 args.push_back("--compile-pic");
101 }
102
103 if (with_patch_info) {
104 args.push_back("--include-patch-information");
105 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700106
107 std::string error_msg;
108 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700109 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler93aa2102015-08-10 14:47:41 -0700110
111 // Verify the odex file was generated as expected and really is
112 // unrelocated.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800113 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
114 odex_location.c_str(),
115 nullptr,
116 nullptr,
117 false,
118 /*low_4gb*/false,
119 dex_location.c_str(),
120 &error_msg));
Richard Uhler93aa2102015-08-10 14:47:41 -0700121 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhlera48403e2016-04-26 10:24:38 -0700122 EXPECT_EQ(pic, odex_file->IsPic());
123 EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000124 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
125
Vladimir Markof6d1e0f2016-05-23 15:32:42 +0100126 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
Andreas Gampe29d38e72016-03-23 15:31:51 +0000127 const std::vector<gc::space::ImageSpace*> image_spaces =
Richard Uhlera48403e2016-04-26 10:24:38 -0700128 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000129 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
130 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
131 const OatHeader& oat_header = odex_file->GetOatHeader();
Jeff Haob11ffb72016-04-07 15:40:54 -0700132 uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum();
133 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000134 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
135 oat_header.GetImageFileLocationOatDataBegin());
136 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
137 }
Richard Uhler93aa2102015-08-10 14:47:41 -0700138 }
139
140 void GeneratePicOdexForTest(const std::string& dex_location,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000141 const std::string& odex_location,
142 CompilerFilter::Filter filter) {
Richard Uhlera48403e2016-04-26 10:24:38 -0700143 GenerateOdexForTest(dex_location, odex_location, filter, true, false);
Calin Juravleb077e152016-02-18 18:47:37 +0000144 }
David Brazdilce4b0ba2016-01-28 15:05:49 +0000145
Richard Uhlerd1537b52016-03-29 13:27:41 -0700146 // Generate a non-PIC odex file without patch information for the purposes
147 // of test. The generated odex file will be un-relocated.
Richard Uhlerd1537b52016-03-29 13:27:41 -0700148 void GenerateNoPatchOdexForTest(const std::string& dex_location,
149 const std::string& odex_location,
150 CompilerFilter::Filter filter) {
Richard Uhlera48403e2016-04-26 10:24:38 -0700151 GenerateOdexForTest(dex_location, odex_location, filter, false, false);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700152 }
153
Richard Uhler66d874d2015-01-15 09:37:19 -0800154 private:
155 // Reserve memory around where the image will be loaded so other memory
156 // won't conflict when it comes time to load the image.
157 // This can be called with an already loaded image to reserve the space
158 // around it.
159 void ReserveImageSpace() {
160 MemMap::Init();
161
162 // Ensure a chunk of memory is reserved for the image space.
Richard Uhlera48403e2016-04-26 10:24:38 -0700163 // The reservation_end includes room for the main space that has to come
164 // right after the image in case of the GSS collector.
165 uintptr_t reservation_start = ART_BASE_ADDRESS;
166 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800167
Richard Uhler66d874d2015-01-15 09:37:19 -0800168 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
169 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
170 for (BacktraceMap::const_iterator it = map->begin();
171 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700172 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
173 reservation_start = std::max(reservation_start, it->end);
174 }
175 ReserveImageSpaceChunk(reservation_start, reservation_end);
176 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800177
Richard Uhler3efe9792015-03-30 16:18:03 -0700178 // Reserve a chunk of memory for the image space in the given range.
179 // Only has effect for chunks with a positive number of bytes.
180 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
181 if (start < end) {
182 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800183 image_reservation_.push_back(std::unique_ptr<MemMap>(
184 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700185 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700186 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800187 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
188 LOG(INFO) << "Reserved space for image " <<
189 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
190 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800191 }
192 }
193
194
195 // Unreserve any memory reserved by ReserveImageSpace. This should be called
196 // before the image is loaded.
197 void UnreserveImageSpace() {
198 image_reservation_.clear();
199 }
200
Richard Uhler66d874d2015-01-15 09:37:19 -0800201 std::vector<std::unique_ptr<MemMap>> image_reservation_;
202};
203
204class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
205 public:
206 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
207 OatFileAssistantTest::SetUpRuntimeOptions(options);
208 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
209 }
210};
211
212// Generate an oat file for the purposes of test, as opposed to testing
213// generation of oat files.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000214static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
215 // Use an oat file assistant to find the proper oat location.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700216 std::string oat_location;
217 std::string error_msg;
218 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
219 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800220
Andreas Gampe29d38e72016-03-23 15:31:51 +0000221 std::vector<std::string> args;
222 args.push_back("--dex-file=" + std::string(dex_location));
Richard Uhlerb81881d2016-04-19 13:08:04 -0700223 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000224 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
225 args.push_back("--runtime-arg");
226 args.push_back("-Xnorelocate");
Andreas Gampe29d38e72016-03-23 15:31:51 +0000227 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
228
229 // Verify the oat file was generated as expected.
Richard Uhlerb81881d2016-04-19 13:08:04 -0700230 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location.c_str(),
231 oat_location.c_str(),
Andreas Gampe29d38e72016-03-23 15:31:51 +0000232 nullptr,
233 nullptr,
234 false,
235 /*low_4gb*/false,
236 dex_location,
237 &error_msg));
238 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
239 EXPECT_EQ(filter, oat_file->GetCompilerFilter());
Richard Uhler66d874d2015-01-15 09:37:19 -0800240}
241
242// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700243// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800244TEST_F(OatFileAssistantTest, DexNoOat) {
245 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
246 Copy(GetDexSrc1(), dex_location);
247
Richard Uhlerd1472a22016-04-15 15:18:56 -0700248 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800249
Andreas Gampe29d38e72016-03-23 15:31:51 +0000250 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
251 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
252 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
253 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
254 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
255 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
256 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
257 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800258
259 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
260 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
261 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
262 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
263 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700264 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800265 EXPECT_FALSE(oat_file_assistant.OatFileExists());
266 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
267 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
268 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700269 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700270 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800271}
272
273// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700274// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800275TEST_F(OatFileAssistantTest, NoDexNoOat) {
276 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
277
Richard Uhlerd1472a22016-04-15 15:18:56 -0700278 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800279
Andreas Gampe29d38e72016-03-23 15:31:51 +0000280 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
281 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700282 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
283
284 // Trying to make the oat file up to date should not fail or crash.
285 std::string error_msg;
Richard Uhlerd1472a22016-04-15 15:18:56 -0700286 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700287
288 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800289 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
290 EXPECT_EQ(nullptr, oat_file.get());
291}
292
293// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700294// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800295TEST_F(OatFileAssistantTest, OatUpToDate) {
296 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
297 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000298 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800299
Richard Uhlerd1472a22016-04-15 15:18:56 -0700300 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800301
Andreas Gampe29d38e72016-03-23 15:31:51 +0000302 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
303 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
304 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
305 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
306 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
307 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
308 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
309 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
310
Richard Uhler66d874d2015-01-15 09:37:19 -0800311 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
312 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
313 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
314 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
315 EXPECT_TRUE(oat_file_assistant.OatFileExists());
316 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
317 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
318 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700319 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700320 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800321}
322
Richard Uhler9a37efc2016-08-05 16:32:55 -0700323// Case: We have a DEX file and ODEX file for a different dex location.
324// Expect: The status is kDex2OatNeeded.
325TEST_F(OatFileAssistantTest, OatForDifferentDex) {
326 // Generate an odex file for OatForDifferentDex_A.jar
327 std::string dex_location_a = GetScratchDir() + "/OatForDifferentDex_A.jar";
328 std::string odex_location = GetOdexDir() + "/OatForDifferentDex.odex";
329 Copy(GetDexSrc1(), dex_location_a);
330 GenerateOdexForTest(dex_location_a, odex_location, CompilerFilter::kSpeed);
331
332 // Try to use that odex file for OatForDifferentDex.jar
333 std::string dex_location = GetScratchDir() + "/OatForDifferentDex.jar";
334 Copy(GetDexSrc1(), dex_location);
335
336 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
337
338 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
339 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
340
341 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
342 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
343 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
344 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
345 EXPECT_FALSE(oat_file_assistant.OatFileExists());
346 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
347 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
348 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
349}
350
Andreas Gampe29d38e72016-03-23 15:31:51 +0000351// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700352// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
353// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000354TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
355 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
356 Copy(GetDexSrc1(), dex_location);
357 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
358
Richard Uhlerd1472a22016-04-15 15:18:56 -0700359 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000360
361 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700362 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000363 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700364 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, false));
365 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
366 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
367 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
368 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000369
370 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
371 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
372 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
373 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
374 EXPECT_TRUE(oat_file_assistant.OatFileExists());
375 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
376 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
377 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
378 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
379 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
380}
381
Richard Uhler66d874d2015-01-15 09:37:19 -0800382// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700383// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800384TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
385 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
386 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000387 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800388
Richard Uhlerd1472a22016-04-15 15:18:56 -0700389 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000390 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700391 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700392 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700393
394 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700395 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800396 ASSERT_TRUE(oat_file.get() != nullptr);
397 EXPECT_TRUE(oat_file->IsExecutable());
398 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700399 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
400 EXPECT_EQ(2u, dex_files.size());
401}
402
Richard Uhler67ff7d12015-05-14 13:21:13 -0700403// Case: We have a MultiDEX file where the secondary dex file is out of date.
404// Expect: The status is kDex2OatNeeded.
405TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
406 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
407
408 // Compile code for GetMultiDexSrc1.
409 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000410 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700411
412 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
413 // is out of date.
414 Copy(GetMultiDexSrc2(), dex_location);
415
Richard Uhlerd1472a22016-04-15 15:18:56 -0700416 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000417 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700418 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700419 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700420}
421
Richard Uhlere5fed032015-03-18 08:21:11 -0700422// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
423// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700424// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700425TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
426 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700427 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700428
429 // Create the dex file
430 Copy(GetMultiDexSrc1(), dex_location);
431
432 // Create the oat file with relative encoded dex location.
433 std::vector<std::string> args;
434 args.push_back("--dex-file=" + dex_location);
435 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
436 args.push_back("--oat-file=" + oat_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000437 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700438
439 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700440 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700441
442 // Verify we can load both dex files.
443 OatFileAssistant oat_file_assistant(dex_location.c_str(),
444 oat_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700445 kRuntimeISA, true);
Richard Uhlere5fed032015-03-18 08:21:11 -0700446 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
447 ASSERT_TRUE(oat_file.get() != nullptr);
448 EXPECT_TRUE(oat_file->IsExecutable());
449 std::vector<std::unique_ptr<const DexFile>> dex_files;
450 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800451 EXPECT_EQ(2u, dex_files.size());
452}
453
Richard Uhler95abd042015-03-24 09:51:28 -0700454// Case: We have a DEX file and out-of-date OAT file.
455// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800456TEST_F(OatFileAssistantTest, OatOutOfDate) {
457 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
458
459 // We create a dex, generate an oat for it, then overwrite the dex with a
460 // different dex to make the oat out of date.
461 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000462 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800463 Copy(GetDexSrc2(), dex_location);
464
Richard Uhlerd1472a22016-04-15 15:18:56 -0700465 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000466 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
467 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
468 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
469 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800470
471 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
472 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
473 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
474 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
475 EXPECT_TRUE(oat_file_assistant.OatFileExists());
476 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
477 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700478 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800479}
480
481// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700482// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800483TEST_F(OatFileAssistantTest, DexOdexNoOat) {
484 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700485 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800486
487 // Create the dex and odex files
488 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000489 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800490
491 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700492 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800493
Andreas Gampe29d38e72016-03-23 15:31:51 +0000494 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
495 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
496 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
497 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800498
499 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
500 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
501 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
502 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
503 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800504 EXPECT_FALSE(oat_file_assistant.OatFileExists());
505 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
506 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700507 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700508
509 // We should still be able to get the non-executable odex file to run from.
510 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
511 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800512}
513
514// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700515// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800516TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
517 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700518 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800519
520 // Create the dex and odex files
521 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000522 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800523
524 // Strip the dex file
525 Copy(GetStrippedDexSrc1(), dex_location);
526
527 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700528 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800529
Andreas Gampe29d38e72016-03-23 15:31:51 +0000530 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
531 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800532
533 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
534 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
535 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
536 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
537 EXPECT_FALSE(oat_file_assistant.OatFileExists());
538 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
539 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700540 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800541
542 // Make the oat file up to date.
543 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700544 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700545 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700546 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800547
Andreas Gampe29d38e72016-03-23 15:31:51 +0000548 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
549 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800550
551 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
552 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
553 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
554 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
555 EXPECT_TRUE(oat_file_assistant.OatFileExists());
556 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
557 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700558 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800559
560 // Verify we can load the dex files from it.
561 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
562 ASSERT_TRUE(oat_file.get() != nullptr);
563 EXPECT_TRUE(oat_file->IsExecutable());
564 std::vector<std::unique_ptr<const DexFile>> dex_files;
565 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
566 EXPECT_EQ(1u, dex_files.size());
567}
568
Richard Uhler95abd042015-03-24 09:51:28 -0700569// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
570// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800571TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
572 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700573 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800574
575 // Create the oat file from a different dex file so it looks out of date.
576 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000577 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800578
579 // Create the odex file
580 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000581 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800582
583 // Strip the dex file.
584 Copy(GetStrippedDexSrc1(), dex_location);
585
586 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700587 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800588
Andreas Gampe29d38e72016-03-23 15:31:51 +0000589 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
590 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
591 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
592 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
593 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
594 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800595
596 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
597 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
598 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
599 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
600 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
601 EXPECT_TRUE(oat_file_assistant.OatFileExists());
602 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
603 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700604 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800605
606 // Make the oat file up to date.
607 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700608 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700609 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700610 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800611
Andreas Gampe29d38e72016-03-23 15:31:51 +0000612 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
613 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
614 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped.
615 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800616
617 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
618 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
619 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
620 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
621 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
622 EXPECT_TRUE(oat_file_assistant.OatFileExists());
623 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
624 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
625 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700626 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800627
628 // Verify we can load the dex files from it.
629 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
630 ASSERT_TRUE(oat_file.get() != nullptr);
631 EXPECT_TRUE(oat_file->IsExecutable());
632 std::vector<std::unique_ptr<const DexFile>> dex_files;
633 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
634 EXPECT_EQ(1u, dex_files.size());
635}
636
Richard Uhler9b994ea2015-06-24 08:44:19 -0700637// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
638// OAT file. Expect: The status is kNoDexOptNeeded.
639TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
640 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
641
642 Copy(GetStrippedDexSrc1(), dex_location);
643
644 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700645 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700646
Andreas Gampe29d38e72016-03-23 15:31:51 +0000647 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
648 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
649 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
650 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
651 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
652 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700653
654 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
655 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
656 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
657 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
658 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
659 EXPECT_FALSE(oat_file_assistant.OatFileExists());
660 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
661 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
662 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
663
664 // Make the oat file up to date. This should have no effect.
665 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700666 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700667 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700668 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700669
Andreas Gampe29d38e72016-03-23 15:31:51 +0000670 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
671 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700672
673 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
674 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
675 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
676 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
677 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
678 EXPECT_FALSE(oat_file_assistant.OatFileExists());
679 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
680 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
681 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
682}
683
Richard Uhler95abd042015-03-24 09:51:28 -0700684// Case: We have a DEX file, no ODEX file and an OAT file that needs
685// relocation.
686// Expect: The status is kSelfPatchOatNeeded.
687TEST_F(OatFileAssistantTest, SelfRelocation) {
688 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
689 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
690
691 // Create the dex and odex files
692 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000693 GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
Richard Uhler95abd042015-03-24 09:51:28 -0700694
695 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700696 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700697
Andreas Gampe29d38e72016-03-23 15:31:51 +0000698 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
699 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
700 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded,
701 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
702 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
703 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler95abd042015-03-24 09:51:28 -0700704
705 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
706 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
707 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
708 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
709 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
710 EXPECT_TRUE(oat_file_assistant.OatFileExists());
711 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
712 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
713 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700714 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700715
716 // Make the oat file up to date.
717 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700718 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700719 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700720 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700721
Andreas Gampe29d38e72016-03-23 15:31:51 +0000722 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
723 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler95abd042015-03-24 09:51:28 -0700724
725 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
726 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
727 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
728 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
729 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
730 EXPECT_TRUE(oat_file_assistant.OatFileExists());
731 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
732 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
733 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700734 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700735
736 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
737 ASSERT_TRUE(oat_file.get() != nullptr);
738 EXPECT_TRUE(oat_file->IsExecutable());
739 std::vector<std::unique_ptr<const DexFile>> dex_files;
740 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
741 EXPECT_EQ(1u, dex_files.size());
742}
743
Richard Uhlerd1537b52016-03-29 13:27:41 -0700744// Case: We have a DEX file, no ODEX file and an OAT file that needs
745// relocation but doesn't have patch info.
746// Expect: The status is kDex2OatNeeded, because we can't run patchoat.
747TEST_F(OatFileAssistantTest, NoSelfRelocation) {
748 std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar";
749 std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat";
750
751 // Create the dex and odex files
752 Copy(GetDexSrc1(), dex_location);
753 GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed);
754
755 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700756 oat_location.c_str(), kRuntimeISA, true);
Richard Uhlerd1537b52016-03-29 13:27:41 -0700757
758 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
759 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
760
761 // Make the oat file up to date.
762 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700763 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700764 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700765 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerd1537b52016-03-29 13:27:41 -0700766 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
767 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
768
769 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
770 ASSERT_TRUE(oat_file.get() != nullptr);
771 EXPECT_TRUE(oat_file->IsExecutable());
772 std::vector<std::unique_ptr<const DexFile>> dex_files;
773 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
774 EXPECT_EQ(1u, dex_files.size());
775}
776
Richard Uhler66d874d2015-01-15 09:37:19 -0800777// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
778// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700779// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800780TEST_F(OatFileAssistantTest, OdexOatOverlap) {
781 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700782 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
783 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800784
785 // Create the dex and odex files
786 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000787 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800788
789 // Create the oat file by copying the odex so they are located in the same
790 // place in memory.
791 Copy(odex_location, oat_location);
792
793 // Verify things don't go bad.
794 OatFileAssistant oat_file_assistant(dex_location.c_str(),
Richard Uhlerd1472a22016-04-15 15:18:56 -0700795 oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800796
Andreas Gampe29d38e72016-03-23 15:31:51 +0000797 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded,
798 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800799
800 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
801 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
802 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
803 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
804 EXPECT_TRUE(oat_file_assistant.OatFileExists());
805 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
806 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700807 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800808
809 // Things aren't relocated, so it should fall back to interpreted.
810 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
811 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700812
Richard Uhler66d874d2015-01-15 09:37:19 -0800813 EXPECT_FALSE(oat_file->IsExecutable());
814 std::vector<std::unique_ptr<const DexFile>> dex_files;
815 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
816 EXPECT_EQ(1u, dex_files.size());
817}
818
819// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700820// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800821TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
822 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700823 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800824
825 // Create the dex and odex files
826 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000827 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800828
829 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700830 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800831
Andreas Gampe29d38e72016-03-23 15:31:51 +0000832 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
833 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
834 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
835 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800836
837 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
838 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
839 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
840 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
841 EXPECT_FALSE(oat_file_assistant.OatFileExists());
842 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
843 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700844 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800845}
846
Andreas Gampe29d38e72016-03-23 15:31:51 +0000847// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
848// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
849TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
850 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
851 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000852
853 // Create the dex and odex files
854 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000855 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000856
857 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700858 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000859
Andreas Gampe29d38e72016-03-23 15:31:51 +0000860 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
861 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime));
862 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
863 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000864
865 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
866 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
867 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
Nicolas Geoffray845e5062016-03-23 06:42:05 +0000868 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000869 EXPECT_FALSE(oat_file_assistant.OatFileExists());
870 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
871 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
872 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
873}
874
Richard Uhler66d874d2015-01-15 09:37:19 -0800875// Case: We have a DEX file and up-to-date OAT file for it.
876// Expect: We should load an executable dex file.
877TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
878 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
879
880 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000881 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800882
883 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700884 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000885
886 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
887 ASSERT_TRUE(oat_file.get() != nullptr);
888 EXPECT_TRUE(oat_file->IsExecutable());
889 std::vector<std::unique_ptr<const DexFile>> dex_files;
890 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
891 EXPECT_EQ(1u, dex_files.size());
892}
893
894// Case: We have a DEX file and up-to-date interpret-only OAT file for it.
895// Expect: We should still load the oat file as executable.
896TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
897 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
898
899 Copy(GetDexSrc1(), dex_location);
900 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly);
901
902 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700903 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800904
905 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
906 ASSERT_TRUE(oat_file.get() != nullptr);
907 EXPECT_TRUE(oat_file->IsExecutable());
908 std::vector<std::unique_ptr<const DexFile>> dex_files;
909 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
910 EXPECT_EQ(1u, dex_files.size());
911}
912
913// Case: We have a DEX file and up-to-date OAT file for it.
914// Expect: Loading non-executable should load the oat non-executable.
915TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
916 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
917
918 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000919 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800920
921 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700922 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800923
924 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
925 ASSERT_TRUE(oat_file.get() != nullptr);
926 EXPECT_FALSE(oat_file->IsExecutable());
927 std::vector<std::unique_ptr<const DexFile>> dex_files;
928 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
929 EXPECT_EQ(1u, dex_files.size());
930}
931
932// Case: We have a DEX file.
933// Expect: We should load an executable dex file from an alternative oat
934// location.
935TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
936 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
937 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
938
939 Copy(GetDexSrc1(), dex_location);
940
941 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700942 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800943 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700944 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700945 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700946 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800947
948 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
949 ASSERT_TRUE(oat_file.get() != nullptr);
950 EXPECT_TRUE(oat_file->IsExecutable());
951 std::vector<std::unique_ptr<const DexFile>> dex_files;
952 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
953 EXPECT_EQ(1u, dex_files.size());
954
955 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
956
957 // Verify it didn't create an oat in the default location.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700958 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800959 EXPECT_FALSE(ofm.OatFileExists());
960}
961
Richard Uhler8327cf72015-10-13 16:34:59 -0700962// Case: We have a DEX file but can't write the oat file.
963// Expect: We should fail to make the oat file up to date.
964TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
965 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
966
967 // Make the oat location unwritable by inserting some non-existent
968 // intermediate directories.
969 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
970
971 Copy(GetDexSrc1(), dex_location);
972
973 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700974 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700975 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700976 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700977 ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700978 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700979
980 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
981 ASSERT_TRUE(oat_file.get() == nullptr);
982}
983
984// Case: We don't have a DEX file and can't write the oat file.
985// Expect: We should fail to generate the oat file without crashing.
986TEST_F(OatFileAssistantTest, GenNoDex) {
987 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
988 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
989
990 OatFileAssistant oat_file_assistant(
Richard Uhlerd1472a22016-04-15 15:18:56 -0700991 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700992 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700993 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700994 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerf4b34872016-04-13 11:03:46 -0700995 oat_file_assistant.GenerateOatFile(&error_msg));
Richard Uhler8327cf72015-10-13 16:34:59 -0700996}
997
Richard Uhler66d874d2015-01-15 09:37:19 -0800998// Turn an absolute path into a path relative to the current working
999// directory.
1000static std::string MakePathRelative(std::string target) {
1001 char buf[MAXPATHLEN];
1002 std::string cwd = getcwd(buf, MAXPATHLEN);
1003
1004 // Split the target and cwd paths into components.
1005 std::vector<std::string> target_path;
1006 std::vector<std::string> cwd_path;
1007 Split(target, '/', &target_path);
1008 Split(cwd, '/', &cwd_path);
1009
1010 // Reverse the path components, so we can use pop_back().
1011 std::reverse(target_path.begin(), target_path.end());
1012 std::reverse(cwd_path.begin(), cwd_path.end());
1013
1014 // Drop the common prefix of the paths. Because we reversed the path
1015 // components, this becomes the common suffix of target_path and cwd_path.
1016 while (!target_path.empty() && !cwd_path.empty()
1017 && target_path.back() == cwd_path.back()) {
1018 target_path.pop_back();
1019 cwd_path.pop_back();
1020 }
1021
1022 // For each element of the remaining cwd_path, add '..' to the beginning
1023 // of the target path. Because we reversed the path components, we add to
1024 // the end of target_path.
1025 for (unsigned int i = 0; i < cwd_path.size(); i++) {
1026 target_path.push_back("..");
1027 }
1028
1029 // Reverse again to get the right path order, and join to get the result.
1030 std::reverse(target_path.begin(), target_path.end());
1031 return Join(target_path, '/');
1032}
1033
1034// Case: Non-absolute path to Dex location.
1035// Expect: Not sure, but it shouldn't crash.
1036TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1037 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1038 Copy(GetDexSrc1(), abs_dex_location);
1039
1040 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001041 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001042
1043 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001044 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1045 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001046 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1047 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1048 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1049 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1050 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1051 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1052}
1053
1054// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001055// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001056TEST_F(OatFileAssistantTest, ShortDexLocation) {
1057 std::string dex_location = "/xx";
1058
Richard Uhlerd1472a22016-04-15 15:18:56 -07001059 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001060
1061 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001062 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1063 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001064 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1065 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1066 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1067 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1068 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1069 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001070 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001071
Richard Uhler9b994ea2015-06-24 08:44:19 -07001072 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001073 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001074 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001075 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001076 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001077 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001078}
1079
1080// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001081// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001082TEST_F(OatFileAssistantTest, LongDexExtension) {
1083 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1084 Copy(GetDexSrc1(), dex_location);
1085
Richard Uhlerd1472a22016-04-15 15:18:56 -07001086 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001087
Andreas Gampe29d38e72016-03-23 15:31:51 +00001088 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1089 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001090
1091 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
1092 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
1093 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
1094 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
1095 EXPECT_FALSE(oat_file_assistant.OatFileExists());
1096 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
1097 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
1098}
1099
1100// A task to generate a dex location. Used by the RaceToGenerate test.
1101class RaceGenerateTask : public Task {
1102 public:
1103 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001104 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001105 {}
1106
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001107 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001108 // Load the dex files, and save a pointer to the loaded oat file, so that
1109 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001110 std::vector<std::unique_ptr<const DexFile>> dex_files;
1111 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001112 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001113 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1114 dex_location_.c_str(),
1115 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001116 /*class_loader*/nullptr,
1117 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001118 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001119 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001120 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001121 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1122 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001123 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001124 }
1125
1126 const OatFile* GetLoadedOatFile() const {
1127 return loaded_oat_file_;
1128 }
1129
1130 private:
1131 std::string dex_location_;
1132 std::string oat_location_;
1133 const OatFile* loaded_oat_file_;
1134};
1135
1136// Test the case where multiple processes race to generate an oat file.
1137// This simulates multiple processes using multiple threads.
1138//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001139// We want unique Oat files to be loaded even when there is a race to load.
1140// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1141// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001142TEST_F(OatFileAssistantTest, RaceToGenerate) {
1143 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001144 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001145
1146 // We use the lib core dex file, because it's large, and hopefully should
1147 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001148 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001149
1150 const int kNumThreads = 32;
1151 Thread* self = Thread::Current();
1152 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1153 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1154 for (int i = 0; i < kNumThreads; i++) {
1155 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1156 thread_pool.AddTask(self, task.get());
1157 tasks.push_back(std::move(task));
1158 }
1159 thread_pool.StartWorkers(self);
1160 thread_pool.Wait(self, true, false);
1161
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001162 // Verify every task got a unique oat file.
1163 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001164 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001165 const OatFile* oat_file = task->GetLoadedOatFile();
1166 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1167 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001168 }
1169}
1170
1171// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1172// disabled.
1173// Expect: We should load the odex file non-executable.
1174TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1175 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001176 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001177
1178 // Create the dex and odex files
1179 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001180 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001181
1182 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001183 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001184
1185 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1186 ASSERT_TRUE(oat_file.get() != nullptr);
1187 EXPECT_FALSE(oat_file->IsExecutable());
1188 std::vector<std::unique_ptr<const DexFile>> dex_files;
1189 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1190 EXPECT_EQ(1u, dex_files.size());
1191}
1192
1193// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1194// disabled.
1195// Expect: We should load the odex file non-executable.
1196TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1197 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001198 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001199
1200 // Create the dex and odex files
1201 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001202 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001203
1204 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001205 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001206
1207 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1208 ASSERT_TRUE(oat_file.get() != nullptr);
1209 EXPECT_FALSE(oat_file->IsExecutable());
1210 std::vector<std::unique_ptr<const DexFile>> dex_files;
1211 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1212 EXPECT_EQ(2u, dex_files.size());
1213}
1214
Richard Uhlerf4b34872016-04-13 11:03:46 -07001215TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1216 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1217 Copy(GetDexSrc1(), dex_location);
1218
Richard Uhlerd1472a22016-04-15 15:18:56 -07001219 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001220
1221 std::string error_msg;
1222 Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only");
1223 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001224 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001225 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1226 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1227 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded,
1228 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1229
1230 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1231 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001232 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001233 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1234 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly));
1235 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1236 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1237
1238 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1239 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001240 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001241}
1242
Richard Uhlerb81881d2016-04-19 13:08:04 -07001243TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001244 std::string error_msg;
1245 std::string odex_file;
1246
Richard Uhlerb81881d2016-04-19 13:08:04 -07001247 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001248 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001249 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001250
Richard Uhlerb81881d2016-04-19 13:08:04 -07001251 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001252 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001253 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001254
Richard Uhlerb81881d2016-04-19 13:08:04 -07001255 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001256 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001257 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001258 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001259}
1260
Richard Uhler23cedd22015-04-08 13:17:29 -07001261// Verify the dexopt status values from dalvik.system.DexFile
1262// match the OatFileAssistant::DexOptStatus values.
1263TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1264 ScopedObjectAccess soa(Thread::Current());
1265 StackHandleScope<1> hs(soa.Self());
1266 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1267 Handle<mirror::Class> dexfile(
1268 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1269 ASSERT_FALSE(dexfile.Get() == nullptr);
1270 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1271
Mathieu Chartierc7853442015-03-27 14:35:38 -07001272 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001273 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1274 ASSERT_FALSE(no_dexopt_needed == nullptr);
1275 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1276 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1277
Mathieu Chartierc7853442015-03-27 14:35:38 -07001278 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001279 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1280 ASSERT_FALSE(dex2oat_needed == nullptr);
1281 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1282 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1283
Mathieu Chartierc7853442015-03-27 14:35:38 -07001284 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001285 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1286 ASSERT_FALSE(patchoat_needed == nullptr);
1287 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1288 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1289
Mathieu Chartierc7853442015-03-27 14:35:38 -07001290 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001291 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1292 ASSERT_FALSE(self_patchoat_needed == nullptr);
1293 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1294 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1295}
Richard Uhler66d874d2015-01-15 09:37:19 -08001296
1297// TODO: More Tests:
Andreas Gampe29d38e72016-03-23 15:31:51 +00001298// * Image checksum change is out of date for kIntepretOnly, but not
1299// kVerifyAtRuntime. But target of kVerifyAtRuntime still says current
1300// kInterpretOnly is out of date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001301// * Test class linker falls back to unquickened dex for DexNoOat
1302// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001303// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001304// * Test for status of oat while oat is being generated (how?)
1305// * Test case where 32 and 64 bit boot class paths differ,
1306// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1307// 64 bit boot class paths.
1308// * Test unexpected scenarios (?):
1309// - Dex is stripped, don't have odex.
1310// - Oat file corrupted after status check, before reload unexecutable
1311// because it's unrelocated and no dex2oat
Calin Juravleb077e152016-02-18 18:47:37 +00001312// * Test unrelocated specific target compilation type can be relocated to
1313// make it up to date.
Richard Uhler66d874d2015-01-15 09:37:19 -08001314
1315} // namespace art