blob: 2c81eddf3963edc41d6a3827393d2a62d3a22c11 [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
17#include "oat_file_assistant.h"
18
19#include <algorithm>
20#include <fstream>
21#include <string>
22#include <vector>
23#include <sys/param.h>
24
25#include <backtrace/BacktraceMap.h>
26#include <gtest/gtest.h>
27
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010029#include "class_linker-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "common_runtime_test.h"
Andreas Gampebb9c6b12015-03-29 13:56:36 -070031#include "compiler_callbacks.h"
Richard Uhlerf16d5722015-05-11 09:32:47 -070032#include "gc/space/image_space.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "mem_map.h"
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
42class OatFileAssistantTest : public CommonRuntimeTest {
43 public:
44 virtual void SetUp() {
45 ReserveImageSpace();
46 CommonRuntimeTest::SetUp();
47
48 // Create a scratch directory to work from.
49 scratch_dir_ = android_data_ + "/OatFileAssistantTest";
50 ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700));
51
Richard Uhler63434112015-03-16 14:32:16 -070052 // Create a subdirectory in scratch for odex files.
53 odex_oat_dir_ = scratch_dir_ + "/oat";
54 ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700));
55
56 odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA));
57 ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700));
58
Richard Uhler66d874d2015-01-15 09:37:19 -080059
60 // Verify the environment is as we expect
61 uint32_t checksum;
62 std::string error_msg;
63 ASSERT_TRUE(OS::FileExists(GetImageFile().c_str()))
64 << "Expected pre-compiled boot image to be at: " << GetImageFile();
65 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str()))
66 << "Expected dex file to be at: " << GetDexSrc1();
67 ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str()))
68 << "Expected stripped dex file to be at: " << GetStrippedDexSrc1();
Igor Murashkinb1d8c312015-08-04 11:18:43 -070069 ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg))
Richard Uhler66d874d2015-01-15 09:37:19 -080070 << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1();
Richard Uhler66d874d2015-01-15 09:37:19 -080071 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str()))
72 << "Expected dex file to be at: " << GetDexSrc2();
Richard Uhler67ff7d12015-05-14 13:21:13 -070073
74 // GetMultiDexSrc2 should have the same primary dex checksum as
75 // GetMultiDexSrc1, but a different secondary dex checksum.
76 std::vector<std::unique_ptr<const DexFile>> multi1;
77 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc1().c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -070078 GetMultiDexSrc1().c_str(), &error_msg, &multi1)) << error_msg;
Richard Uhler67ff7d12015-05-14 13:21:13 -070079 ASSERT_GT(multi1.size(), 1u);
80
81 std::vector<std::unique_ptr<const DexFile>> multi2;
82 ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(),
Igor Murashkinb1d8c312015-08-04 11:18:43 -070083 GetMultiDexSrc2().c_str(), &error_msg, &multi2)) << error_msg;
Richard Uhler67ff7d12015-05-14 13:21:13 -070084 ASSERT_GT(multi2.size(), 1u);
85
86 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum());
87 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum());
Richard Uhler66d874d2015-01-15 09:37:19 -080088 }
89
90 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Richard Uhler892fc962015-03-10 16:57:05 +000091 // options->push_back(std::make_pair("-verbose:oat", nullptr));
Richard Uhler66d874d2015-01-15 09:37:19 -080092
93 // Set up the image location.
94 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(),
95 nullptr));
96 // Make sure compilercallbacks are not set so that relocation will be
97 // enabled.
Andreas Gampebb9c6b12015-03-29 13:56:36 -070098 callbacks_.reset();
Richard Uhler66d874d2015-01-15 09:37:19 -080099 }
100
101 virtual void PreRuntimeCreate() {
102 UnreserveImageSpace();
103 }
104
105 virtual void PostRuntimeCreate() {
106 ReserveImageSpace();
107 }
108
109 virtual void TearDown() {
Richard Uhler63434112015-03-16 14:32:16 -0700110 ClearDirectory(odex_dir_.c_str());
111 ASSERT_EQ(0, rmdir(odex_dir_.c_str()));
112
113 ClearDirectory(odex_oat_dir_.c_str());
114 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str()));
Richard Uhler66d874d2015-01-15 09:37:19 -0800115
116 ClearDirectory(scratch_dir_.c_str());
117 ASSERT_EQ(0, rmdir(scratch_dir_.c_str()));
118
119 CommonRuntimeTest::TearDown();
120 }
121
122 void Copy(std::string src, std::string dst) {
123 std::ifstream src_stream(src, std::ios::binary);
124 std::ofstream dst_stream(dst, std::ios::binary);
125
126 dst_stream << src_stream.rdbuf();
127 }
128
129 // Returns the directory where the pre-compiled core.art can be found.
130 // TODO: We should factor out this into common tests somewhere rather than
131 // re-hardcoding it here (This was copied originally from the elf writer
132 // test).
133 std::string GetImageDirectory() {
134 if (IsHost()) {
135 const char* host_dir = getenv("ANDROID_HOST_OUT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700136 CHECK(host_dir != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800137 return std::string(host_dir) + "/framework";
138 } else {
139 return std::string("/data/art-test");
140 }
141 }
142
143 std::string GetImageLocation() {
144 return GetImageDirectory() + "/core.art";
145 }
146
147 std::string GetImageFile() {
148 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA)
149 + "/core.art";
150 }
151
152 std::string GetDexSrc1() {
153 return GetTestDexFileName("Main");
154 }
155
156 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex
157 // file stripped.
158 std::string GetStrippedDexSrc1() {
159 return GetTestDexFileName("MainStripped");
160 }
161
162 std::string GetMultiDexSrc1() {
163 return GetTestDexFileName("MultiDex");
164 }
165
Richard Uhler67ff7d12015-05-14 13:21:13 -0700166 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but
167 // with the contents of the secondary dex file changed.
168 std::string GetMultiDexSrc2() {
169 return GetTestDexFileName("MultiDexModifiedSecondary");
170 }
171
Richard Uhler66d874d2015-01-15 09:37:19 -0800172 std::string GetDexSrc2() {
173 return GetTestDexFileName("Nested");
174 }
175
176 // Scratch directory, for dex and odex files (oat files will go in the
177 // dalvik cache).
178 std::string GetScratchDir() {
179 return scratch_dir_;
180 }
181
Richard Uhler63434112015-03-16 14:32:16 -0700182 // Odex directory is the subdirectory in the scratch directory where odex
Richard Uhler66d874d2015-01-15 09:37:19 -0800183 // files should be located.
Richard Uhler63434112015-03-16 14:32:16 -0700184 std::string GetOdexDir() {
185 return odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800186 }
187
Richard Uhler93aa2102015-08-10 14:47:41 -0700188 // Generate a non-PIC odex file for the purposes of test.
Richard Uhler94f5bda2015-07-22 08:25:11 -0700189 // The generated odex file will be un-relocated.
Richard Uhler66d874d2015-01-15 09:37:19 -0800190 void GenerateOdexForTest(const std::string& dex_location,
Richard Uhler93aa2102015-08-10 14:47:41 -0700191 const std::string& odex_location) {
192 // To generate an un-relocated odex file, we first compile a relocated
193 // version of the file, then manually call patchoat to make it look as if
194 // it is unrelocated.
195 std::string relocated_odex_location = odex_location + ".relocated";
196 std::vector<std::string> args;
197 args.push_back("--dex-file=" + dex_location);
198 args.push_back("--oat-file=" + relocated_odex_location);
199 args.push_back("--include-patch-information");
200
201 // We need to use the quick compiler to generate non-PIC code, because
202 // the optimizing compiler always generates PIC.
203 args.push_back("--compiler-backend=Quick");
204
205 std::string error_msg;
206 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
207
208 // Use patchoat to unrelocate the relocated odex file.
209 Runtime* runtime = Runtime::Current();
210 std::vector<std::string> argv;
211 argv.push_back(runtime->GetPatchoatExecutable());
212 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
213 argv.push_back("--input-oat-file=" + relocated_odex_location);
214 argv.push_back("--output-oat-file=" + odex_location);
215 argv.push_back("--base-offset-delta=0x00008000");
216 std::string command_line(Join(argv, ' '));
217 ASSERT_TRUE(Exec(argv, &error_msg)) << error_msg;
218
219 // Verify the odex file was generated as expected and really is
220 // unrelocated.
221 std::unique_ptr<OatFile> odex_file(OatFile::Open(
222 odex_location.c_str(), odex_location.c_str(), nullptr, nullptr,
223 false, dex_location.c_str(), &error_msg));
224 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
225
226 const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace();
227 ASSERT_TRUE(image_space != nullptr);
228 const ImageHeader& image_header = image_space->GetImageHeader();
229 const OatHeader& oat_header = odex_file->GetOatHeader();
230 EXPECT_FALSE(odex_file->IsPic());
231 EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum());
232 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
233 oat_header.GetImageFileLocationOatDataBegin());
234 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
235 }
236
237 void GeneratePicOdexForTest(const std::string& dex_location,
238 const std::string& odex_location) {
239 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
240 // relocated image file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800241 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
242 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
243 std::vector<std::string> args;
244 args.push_back("--dex-file=" + dex_location);
245 args.push_back("--oat-file=" + odex_location);
Richard Uhler93aa2102015-08-10 14:47:41 -0700246 args.push_back("--compile-pic");
Richard Uhler66d874d2015-01-15 09:37:19 -0800247 args.push_back("--runtime-arg");
248 args.push_back("-Xnorelocate");
249 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700250 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800251 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler94f5bda2015-07-22 08:25:11 -0700252
253 // Verify the odex file was generated as expected.
254 std::unique_ptr<OatFile> odex_file(OatFile::Open(
255 odex_location.c_str(), odex_location.c_str(), nullptr, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700256 false, dex_location.c_str(), &error_msg));
Richard Uhler94f5bda2015-07-22 08:25:11 -0700257 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700258 EXPECT_TRUE(odex_file->IsPic());
Richard Uhler66d874d2015-01-15 09:37:19 -0800259 }
260
261 private:
262 // Reserve memory around where the image will be loaded so other memory
263 // won't conflict when it comes time to load the image.
264 // This can be called with an already loaded image to reserve the space
265 // around it.
266 void ReserveImageSpace() {
267 MemMap::Init();
268
269 // Ensure a chunk of memory is reserved for the image space.
270 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
271 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700272 // Include the main space that has to come right after the
273 // image in case of the GSS collector.
274 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800275
Richard Uhler66d874d2015-01-15 09:37:19 -0800276 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
277 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
278 for (BacktraceMap::const_iterator it = map->begin();
279 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700280 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
281 reservation_start = std::max(reservation_start, it->end);
282 }
283 ReserveImageSpaceChunk(reservation_start, reservation_end);
284 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800285
Richard Uhler3efe9792015-03-30 16:18:03 -0700286 // Reserve a chunk of memory for the image space in the given range.
287 // Only has effect for chunks with a positive number of bytes.
288 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
289 if (start < end) {
290 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800291 image_reservation_.push_back(std::unique_ptr<MemMap>(
292 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700293 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700294 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800295 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
296 LOG(INFO) << "Reserved space for image " <<
297 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
298 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800299 }
300 }
301
302
303 // Unreserve any memory reserved by ReserveImageSpace. This should be called
304 // before the image is loaded.
305 void UnreserveImageSpace() {
306 image_reservation_.clear();
307 }
308
309 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700310 std::string odex_oat_dir_;
311 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800312 std::vector<std::unique_ptr<MemMap>> image_reservation_;
313};
314
315class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
316 public:
317 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
318 OatFileAssistantTest::SetUpRuntimeOptions(options);
319 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
320 }
321};
322
323// Generate an oat file for the purposes of test, as opposed to testing
324// generation of oat files.
325static void GenerateOatForTest(const char* dex_location) {
326 OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false);
327
328 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700329 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800330}
331
332// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700333// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800334TEST_F(OatFileAssistantTest, DexNoOat) {
335 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
336 Copy(GetDexSrc1(), dex_location);
337
338 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
339
Richard Uhler95abd042015-03-24 09:51:28 -0700340 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800341
342 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
343 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
344 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
345 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
346 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700347 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800348 EXPECT_FALSE(oat_file_assistant.OatFileExists());
349 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
350 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
351 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700352 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700353 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800354}
355
356// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700357// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800358TEST_F(OatFileAssistantTest, NoDexNoOat) {
359 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
360
361 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
362
Richard Uhler9b994ea2015-06-24 08:44:19 -0700363 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
364 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
365
366 // Trying to make the oat file up to date should not fail or crash.
367 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700368 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700369
370 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800371 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
372 EXPECT_EQ(nullptr, oat_file.get());
373}
374
375// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700376// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800377TEST_F(OatFileAssistantTest, OatUpToDate) {
378 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
379 Copy(GetDexSrc1(), dex_location);
380 GenerateOatForTest(dex_location.c_str());
381
382 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
383
Richard Uhler95abd042015-03-24 09:51:28 -0700384 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800385 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
386 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
387 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
388 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
389 EXPECT_TRUE(oat_file_assistant.OatFileExists());
390 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
391 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
392 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700393 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700394 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800395}
396
397// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700398// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800399TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
400 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
401 Copy(GetMultiDexSrc1(), dex_location);
402 GenerateOatForTest(dex_location.c_str());
403
Richard Uhlere5fed032015-03-18 08:21:11 -0700404 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700405 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700406 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700407
408 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700409 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800410 ASSERT_TRUE(oat_file.get() != nullptr);
411 EXPECT_TRUE(oat_file->IsExecutable());
412 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700413 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
414 EXPECT_EQ(2u, dex_files.size());
415}
416
Richard Uhler67ff7d12015-05-14 13:21:13 -0700417// Case: We have a MultiDEX file where the secondary dex file is out of date.
418// Expect: The status is kDex2OatNeeded.
419TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
420 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
421
422 // Compile code for GetMultiDexSrc1.
423 Copy(GetMultiDexSrc1(), dex_location);
424 GenerateOatForTest(dex_location.c_str());
425
426 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
427 // is out of date.
428 Copy(GetMultiDexSrc2(), dex_location);
429
430 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
431 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700432 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700433}
434
Richard Uhlere5fed032015-03-18 08:21:11 -0700435// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
436// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700437// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700438TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
439 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700440 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700441
442 // Create the dex file
443 Copy(GetMultiDexSrc1(), dex_location);
444
445 // Create the oat file with relative encoded dex location.
446 std::vector<std::string> args;
447 args.push_back("--dex-file=" + dex_location);
448 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
449 args.push_back("--oat-file=" + oat_location);
450
451 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700452 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700453
454 // Verify we can load both dex files.
455 OatFileAssistant oat_file_assistant(dex_location.c_str(),
456 oat_location.c_str(),
457 kRuntimeISA, true);
458 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
459 ASSERT_TRUE(oat_file.get() != nullptr);
460 EXPECT_TRUE(oat_file->IsExecutable());
461 std::vector<std::unique_ptr<const DexFile>> dex_files;
462 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800463 EXPECT_EQ(2u, dex_files.size());
464}
465
Richard Uhler95abd042015-03-24 09:51:28 -0700466// Case: We have a DEX file and out-of-date OAT file.
467// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800468TEST_F(OatFileAssistantTest, OatOutOfDate) {
469 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
470
471 // We create a dex, generate an oat for it, then overwrite the dex with a
472 // different dex to make the oat out of date.
473 Copy(GetDexSrc1(), dex_location);
474 GenerateOatForTest(dex_location.c_str());
475 Copy(GetDexSrc2(), dex_location);
476
477 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700478 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800479
480 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
481 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
482 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
483 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
484 EXPECT_TRUE(oat_file_assistant.OatFileExists());
485 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
486 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700487 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800488}
489
490// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700491// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800492TEST_F(OatFileAssistantTest, DexOdexNoOat) {
493 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700494 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800495
496 // Create the dex and odex files
497 Copy(GetDexSrc1(), dex_location);
498 GenerateOdexForTest(dex_location, odex_location);
499
500 // Verify the status.
501 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
502
Richard Uhler95abd042015-03-24 09:51:28 -0700503 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800504
505 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
506 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
507 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
508 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
509 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800510 EXPECT_FALSE(oat_file_assistant.OatFileExists());
511 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
512 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700513 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700514
515 // We should still be able to get the non-executable odex file to run from.
516 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
517 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800518}
519
520// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700521// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800522TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
523 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700524 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800525
526 // Create the dex and odex files
527 Copy(GetDexSrc1(), dex_location);
528 GenerateOdexForTest(dex_location, odex_location);
529
530 // Strip the dex file
531 Copy(GetStrippedDexSrc1(), dex_location);
532
533 // Verify the status.
534 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
535
Richard Uhler95abd042015-03-24 09:51:28 -0700536 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800537
538 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
539 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
540 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
541 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
542 EXPECT_FALSE(oat_file_assistant.OatFileExists());
543 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
544 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700545 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800546
547 // Make the oat file up to date.
548 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700549 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800550
Richard Uhler95abd042015-03-24 09:51:28 -0700551 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
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);
579 GenerateOatForTest(dex_location.c_str());
580
581 // Create the odex file
582 Copy(GetDexSrc1(), dex_location);
583 GenerateOdexForTest(dex_location, odex_location);
584
585 // Strip the dex file.
586 Copy(GetStrippedDexSrc1(), dex_location);
587
588 // Verify the status.
589 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
590
Richard Uhler95abd042015-03-24 09:51:28 -0700591 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800592
593 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
594 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
595 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
596 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
597 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
598 EXPECT_TRUE(oat_file_assistant.OatFileExists());
599 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
600 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700601 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800602
603 // Make the oat file up to date.
604 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700605 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800606
Richard Uhler95abd042015-03-24 09:51:28 -0700607 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800608
609 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
610 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
611 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
612 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
613 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
614 EXPECT_TRUE(oat_file_assistant.OatFileExists());
615 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
616 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
617 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700618 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800619
620 // Verify we can load the dex files from it.
621 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
622 ASSERT_TRUE(oat_file.get() != nullptr);
623 EXPECT_TRUE(oat_file->IsExecutable());
624 std::vector<std::unique_ptr<const DexFile>> dex_files;
625 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
626 EXPECT_EQ(1u, dex_files.size());
627}
628
Richard Uhler9b994ea2015-06-24 08:44:19 -0700629// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
630// OAT file. Expect: The status is kNoDexOptNeeded.
631TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
632 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
633
634 Copy(GetStrippedDexSrc1(), dex_location);
635
636 // Verify the status.
637 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
638
639 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
640
641 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
642 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
643 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
644 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
645 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
646 EXPECT_FALSE(oat_file_assistant.OatFileExists());
647 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
648 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
649 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
650
651 // Make the oat file up to date. This should have no effect.
652 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700653 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700654
655 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
656
657 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
658 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
659 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
660 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
661 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
662 EXPECT_FALSE(oat_file_assistant.OatFileExists());
663 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
664 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
665 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
666}
667
Richard Uhler95abd042015-03-24 09:51:28 -0700668// Case: We have a DEX file, no ODEX file and an OAT file that needs
669// relocation.
670// Expect: The status is kSelfPatchOatNeeded.
671TEST_F(OatFileAssistantTest, SelfRelocation) {
672 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
673 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
674
675 // Create the dex and odex files
676 Copy(GetDexSrc1(), dex_location);
677 GenerateOdexForTest(dex_location, oat_location);
678
679 OatFileAssistant oat_file_assistant(dex_location.c_str(),
680 oat_location.c_str(), kRuntimeISA, true);
681
682 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
683
684 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
685 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
686 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
687 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
688 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
689 EXPECT_TRUE(oat_file_assistant.OatFileExists());
690 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
691 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
692 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700693 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700694
695 // Make the oat file up to date.
696 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700697 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700698
699 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
700
701 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
702 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
703 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
704 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
705 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
706 EXPECT_TRUE(oat_file_assistant.OatFileExists());
707 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
708 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
709 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700710 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700711
712 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
713 ASSERT_TRUE(oat_file.get() != nullptr);
714 EXPECT_TRUE(oat_file->IsExecutable());
715 std::vector<std::unique_ptr<const DexFile>> dex_files;
716 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
717 EXPECT_EQ(1u, dex_files.size());
718}
719
Richard Uhler66d874d2015-01-15 09:37:19 -0800720// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
721// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700722// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800723TEST_F(OatFileAssistantTest, OdexOatOverlap) {
724 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700725 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
726 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800727
728 // Create the dex and odex files
729 Copy(GetDexSrc1(), dex_location);
730 GenerateOdexForTest(dex_location, odex_location);
731
732 // Create the oat file by copying the odex so they are located in the same
733 // place in memory.
734 Copy(odex_location, oat_location);
735
736 // Verify things don't go bad.
737 OatFileAssistant oat_file_assistant(dex_location.c_str(),
738 oat_location.c_str(), kRuntimeISA, true);
739
Richard Uhler95abd042015-03-24 09:51:28 -0700740 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800741
742 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
743 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
744 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
745 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
746 EXPECT_TRUE(oat_file_assistant.OatFileExists());
747 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
748 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700749 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800750
751 // Things aren't relocated, so it should fall back to interpreted.
752 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
753 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700754
Richard Uhler66d874d2015-01-15 09:37:19 -0800755 EXPECT_FALSE(oat_file->IsExecutable());
756 std::vector<std::unique_ptr<const DexFile>> dex_files;
757 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
758 EXPECT_EQ(1u, dex_files.size());
759}
760
761// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700762// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800763TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
764 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700765 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800766
767 // Create the dex and odex files
768 Copy(GetDexSrc1(), dex_location);
769 GeneratePicOdexForTest(dex_location, odex_location);
770
771 // Verify the status.
772 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
773
Richard Uhler95abd042015-03-24 09:51:28 -0700774 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800775
776 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
777 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
778 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
779 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
780 EXPECT_FALSE(oat_file_assistant.OatFileExists());
781 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
782 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700783 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800784}
785
786// Case: We have a DEX file and up-to-date OAT file for it.
787// Expect: We should load an executable dex file.
788TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
789 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
790
791 Copy(GetDexSrc1(), dex_location);
792 GenerateOatForTest(dex_location.c_str());
793
794 // Load the oat using an oat file assistant.
795 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
796
797 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
798 ASSERT_TRUE(oat_file.get() != nullptr);
799 EXPECT_TRUE(oat_file->IsExecutable());
800 std::vector<std::unique_ptr<const DexFile>> dex_files;
801 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
802 EXPECT_EQ(1u, dex_files.size());
803}
804
805// Case: We have a DEX file and up-to-date OAT file for it.
806// Expect: Loading non-executable should load the oat non-executable.
807TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
808 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
809
810 Copy(GetDexSrc1(), dex_location);
811 GenerateOatForTest(dex_location.c_str());
812
813 // Load the oat using an oat file assistant.
814 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
815
816 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
817 ASSERT_TRUE(oat_file.get() != nullptr);
818 EXPECT_FALSE(oat_file->IsExecutable());
819 std::vector<std::unique_ptr<const DexFile>> dex_files;
820 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
821 EXPECT_EQ(1u, dex_files.size());
822}
823
824// Case: We have a DEX file.
825// Expect: We should load an executable dex file from an alternative oat
826// location.
827TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
828 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
829 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
830
831 Copy(GetDexSrc1(), dex_location);
832
833 OatFileAssistant oat_file_assistant(
834 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
835 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700836 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800837
838 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
839 ASSERT_TRUE(oat_file.get() != nullptr);
840 EXPECT_TRUE(oat_file->IsExecutable());
841 std::vector<std::unique_ptr<const DexFile>> dex_files;
842 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
843 EXPECT_EQ(1u, dex_files.size());
844
845 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
846
847 // Verify it didn't create an oat in the default location.
848 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
849 EXPECT_FALSE(ofm.OatFileExists());
850}
851
Richard Uhler66d874d2015-01-15 09:37:19 -0800852// Turn an absolute path into a path relative to the current working
853// directory.
854static std::string MakePathRelative(std::string target) {
855 char buf[MAXPATHLEN];
856 std::string cwd = getcwd(buf, MAXPATHLEN);
857
858 // Split the target and cwd paths into components.
859 std::vector<std::string> target_path;
860 std::vector<std::string> cwd_path;
861 Split(target, '/', &target_path);
862 Split(cwd, '/', &cwd_path);
863
864 // Reverse the path components, so we can use pop_back().
865 std::reverse(target_path.begin(), target_path.end());
866 std::reverse(cwd_path.begin(), cwd_path.end());
867
868 // Drop the common prefix of the paths. Because we reversed the path
869 // components, this becomes the common suffix of target_path and cwd_path.
870 while (!target_path.empty() && !cwd_path.empty()
871 && target_path.back() == cwd_path.back()) {
872 target_path.pop_back();
873 cwd_path.pop_back();
874 }
875
876 // For each element of the remaining cwd_path, add '..' to the beginning
877 // of the target path. Because we reversed the path components, we add to
878 // the end of target_path.
879 for (unsigned int i = 0; i < cwd_path.size(); i++) {
880 target_path.push_back("..");
881 }
882
883 // Reverse again to get the right path order, and join to get the result.
884 std::reverse(target_path.begin(), target_path.end());
885 return Join(target_path, '/');
886}
887
888// Case: Non-absolute path to Dex location.
889// Expect: Not sure, but it shouldn't crash.
890TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
891 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
892 Copy(GetDexSrc1(), abs_dex_location);
893
894 std::string dex_location = MakePathRelative(abs_dex_location);
895 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
896
897 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700898 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800899 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
900 EXPECT_FALSE(oat_file_assistant.OatFileExists());
901 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
902 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
903 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
904 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
905}
906
907// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700908// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800909TEST_F(OatFileAssistantTest, ShortDexLocation) {
910 std::string dex_location = "/xx";
911
912 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
913
914 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700915 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800916 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
917 EXPECT_FALSE(oat_file_assistant.OatFileExists());
918 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
919 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
920 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
921 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700922 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800923
Richard Uhler9b994ea2015-06-24 08:44:19 -0700924 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -0800925 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700926 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700927 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800928}
929
930// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700931// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800932TEST_F(OatFileAssistantTest, LongDexExtension) {
933 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
934 Copy(GetDexSrc1(), dex_location);
935
936 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
937
Richard Uhler95abd042015-03-24 09:51:28 -0700938 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800939
940 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
941 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
942 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
943 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
944 EXPECT_FALSE(oat_file_assistant.OatFileExists());
945 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
946 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
947}
948
949// A task to generate a dex location. Used by the RaceToGenerate test.
950class RaceGenerateTask : public Task {
951 public:
952 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
953 : dex_location_(dex_location), oat_location_(oat_location),
954 loaded_oat_file_(nullptr)
955 {}
956
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100957 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800958 // Load the dex files, and save a pointer to the loaded oat file, so that
959 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800960 std::vector<std::unique_ptr<const DexFile>> dex_files;
961 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700962 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700963 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
964 dex_location_.c_str(),
965 oat_location_.c_str(),
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700966 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700967 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -0800968 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -0700969 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
970 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700971 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -0800972 }
973
974 const OatFile* GetLoadedOatFile() const {
975 return loaded_oat_file_;
976 }
977
978 private:
979 std::string dex_location_;
980 std::string oat_location_;
981 const OatFile* loaded_oat_file_;
982};
983
984// Test the case where multiple processes race to generate an oat file.
985// This simulates multiple processes using multiple threads.
986//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700987// We want unique Oat files to be loaded even when there is a race to load.
988// TODO: The test case no longer tests locking the way it was intended since we now get multiple
989// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -0800990TEST_F(OatFileAssistantTest, RaceToGenerate) {
991 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700992 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800993
994 // We use the lib core dex file, because it's large, and hopefully should
995 // take a while to generate.
996 Copy(GetLibCoreDexFileName(), dex_location);
997
998 const int kNumThreads = 32;
999 Thread* self = Thread::Current();
1000 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1001 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1002 for (int i = 0; i < kNumThreads; i++) {
1003 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1004 thread_pool.AddTask(self, task.get());
1005 tasks.push_back(std::move(task));
1006 }
1007 thread_pool.StartWorkers(self);
1008 thread_pool.Wait(self, true, false);
1009
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001010 // Verify every task got a unique oat file.
1011 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001012 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001013 const OatFile* oat_file = task->GetLoadedOatFile();
1014 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1015 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001016 }
1017}
1018
1019// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1020// disabled.
1021// Expect: We should load the odex file non-executable.
1022TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1023 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001024 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001025
1026 // Create the dex and odex files
1027 Copy(GetDexSrc1(), dex_location);
1028 GenerateOdexForTest(dex_location, odex_location);
1029
1030 // Load the oat using an executable oat file assistant.
1031 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
1032
1033 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1034 ASSERT_TRUE(oat_file.get() != nullptr);
1035 EXPECT_FALSE(oat_file->IsExecutable());
1036 std::vector<std::unique_ptr<const DexFile>> dex_files;
1037 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1038 EXPECT_EQ(1u, dex_files.size());
1039}
1040
1041// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1042// disabled.
1043// Expect: We should load the odex file non-executable.
1044TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1045 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001046 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001047
1048 // Create the dex and odex files
1049 Copy(GetMultiDexSrc1(), dex_location);
1050 GenerateOdexForTest(dex_location, odex_location);
1051
1052 // Load the oat using an executable oat file assistant.
1053 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
1054
1055 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1056 ASSERT_TRUE(oat_file.get() != nullptr);
1057 EXPECT_FALSE(oat_file->IsExecutable());
1058 std::vector<std::unique_ptr<const DexFile>> dex_files;
1059 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1060 EXPECT_EQ(2u, dex_files.size());
1061}
1062
1063TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1064 std::string error_msg;
1065 std::string odex_file;
1066
1067 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001068 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001069 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001070
1071 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001072 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001073 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001074
1075 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001076 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001077 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001078 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001079}
1080
Richard Uhler23cedd22015-04-08 13:17:29 -07001081// Verify the dexopt status values from dalvik.system.DexFile
1082// match the OatFileAssistant::DexOptStatus values.
1083TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1084 ScopedObjectAccess soa(Thread::Current());
1085 StackHandleScope<1> hs(soa.Self());
1086 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1087 Handle<mirror::Class> dexfile(
1088 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1089 ASSERT_FALSE(dexfile.Get() == nullptr);
1090 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1091
Mathieu Chartierc7853442015-03-27 14:35:38 -07001092 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001093 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1094 ASSERT_FALSE(no_dexopt_needed == nullptr);
1095 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1096 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1097
Mathieu Chartierc7853442015-03-27 14:35:38 -07001098 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001099 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1100 ASSERT_FALSE(dex2oat_needed == nullptr);
1101 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1102 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1103
Mathieu Chartierc7853442015-03-27 14:35:38 -07001104 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001105 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1106 ASSERT_FALSE(patchoat_needed == nullptr);
1107 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1108 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1109
Mathieu Chartierc7853442015-03-27 14:35:38 -07001110 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001111 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1112 ASSERT_FALSE(self_patchoat_needed == nullptr);
1113 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1114 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1115}
Richard Uhler66d874d2015-01-15 09:37:19 -08001116
1117// TODO: More Tests:
1118// * Test class linker falls back to unquickened dex for DexNoOat
1119// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001120// * Test using secondary isa
1121// * Test with profiling info?
1122// * Test for status of oat while oat is being generated (how?)
1123// * Test case where 32 and 64 bit boot class paths differ,
1124// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1125// 64 bit boot class paths.
1126// * Test unexpected scenarios (?):
1127// - Dex is stripped, don't have odex.
1128// - Oat file corrupted after status check, before reload unexecutable
1129// because it's unrelocated and no dex2oat
1130
1131} // namespace art