blob: 25dcbe4c637b3ba898a4deea3736590817bad44e [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
Jeff Haodcdc85b2015-12-04 14:06:18 -0800226 const std::vector<gc::space::ImageSpace*> image_spaces =
227 runtime->GetHeap()->GetBootImageSpaces();
228 ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr);
229 const ImageHeader& image_header = image_spaces[0]->GetImageHeader();
Richard Uhler93aa2102015-08-10 14:47:41 -0700230 const OatHeader& oat_header = odex_file->GetOatHeader();
231 EXPECT_FALSE(odex_file->IsPic());
232 EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum());
233 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
234 oat_header.GetImageFileLocationOatDataBegin());
235 EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta());
236 }
237
238 void GeneratePicOdexForTest(const std::string& dex_location,
239 const std::string& odex_location) {
240 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
241 // relocated image file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800242 std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp";
243 setenv("ANDROID_DATA", android_data_tmp.c_str(), 1);
244 std::vector<std::string> args;
245 args.push_back("--dex-file=" + dex_location);
246 args.push_back("--oat-file=" + odex_location);
Richard Uhler93aa2102015-08-10 14:47:41 -0700247 args.push_back("--compile-pic");
Richard Uhler66d874d2015-01-15 09:37:19 -0800248 args.push_back("--runtime-arg");
249 args.push_back("-Xnorelocate");
250 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700251 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800252 setenv("ANDROID_DATA", android_data_.c_str(), 1);
Richard Uhler94f5bda2015-07-22 08:25:11 -0700253
254 // Verify the odex file was generated as expected.
255 std::unique_ptr<OatFile> odex_file(OatFile::Open(
256 odex_location.c_str(), odex_location.c_str(), nullptr, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700257 false, dex_location.c_str(), &error_msg));
Richard Uhler94f5bda2015-07-22 08:25:11 -0700258 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Richard Uhler93aa2102015-08-10 14:47:41 -0700259 EXPECT_TRUE(odex_file->IsPic());
Richard Uhler66d874d2015-01-15 09:37:19 -0800260 }
261
262 private:
263 // Reserve memory around where the image will be loaded so other memory
264 // won't conflict when it comes time to load the image.
265 // This can be called with an already loaded image to reserve the space
266 // around it.
267 void ReserveImageSpace() {
268 MemMap::Init();
269
270 // Ensure a chunk of memory is reserved for the image space.
271 uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA;
272 uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA
Hiroshi Yamauchi3dbf2342015-03-17 16:01:11 -0700273 // Include the main space that has to come right after the
274 // image in case of the GSS collector.
275 + 384 * MB;
Richard Uhler66d874d2015-01-15 09:37:19 -0800276
Richard Uhler66d874d2015-01-15 09:37:19 -0800277 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
278 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
279 for (BacktraceMap::const_iterator it = map->begin();
280 reservation_start < reservation_end && it != map->end(); ++it) {
Richard Uhler3efe9792015-03-30 16:18:03 -0700281 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
282 reservation_start = std::max(reservation_start, it->end);
283 }
284 ReserveImageSpaceChunk(reservation_start, reservation_end);
285 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800286
Richard Uhler3efe9792015-03-30 16:18:03 -0700287 // Reserve a chunk of memory for the image space in the given range.
288 // Only has effect for chunks with a positive number of bytes.
289 void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
290 if (start < end) {
291 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800292 image_reservation_.push_back(std::unique_ptr<MemMap>(
293 MemMap::MapAnonymous("image reservation",
Richard Uhler3efe9792015-03-30 16:18:03 -0700294 reinterpret_cast<uint8_t*>(start), end - start,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700295 PROT_NONE, false, false, &error_msg)));
Richard Uhler66d874d2015-01-15 09:37:19 -0800296 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
297 LOG(INFO) << "Reserved space for image " <<
298 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
299 reinterpret_cast<void*>(image_reservation_.back()->End());
Richard Uhler66d874d2015-01-15 09:37:19 -0800300 }
301 }
302
303
304 // Unreserve any memory reserved by ReserveImageSpace. This should be called
305 // before the image is loaded.
306 void UnreserveImageSpace() {
307 image_reservation_.clear();
308 }
309
310 std::string scratch_dir_;
Richard Uhler63434112015-03-16 14:32:16 -0700311 std::string odex_oat_dir_;
312 std::string odex_dir_;
Richard Uhler66d874d2015-01-15 09:37:19 -0800313 std::vector<std::unique_ptr<MemMap>> image_reservation_;
314};
315
316class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest {
317 public:
318 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
319 OatFileAssistantTest::SetUpRuntimeOptions(options);
320 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
321 }
322};
323
324// Generate an oat file for the purposes of test, as opposed to testing
325// generation of oat files.
326static void GenerateOatForTest(const char* dex_location) {
327 OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false);
328
329 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700330 ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800331}
332
333// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700334// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800335TEST_F(OatFileAssistantTest, DexNoOat) {
336 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
337 Copy(GetDexSrc1(), dex_location);
338
339 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
340
Richard Uhler95abd042015-03-24 09:51:28 -0700341 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800342
343 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
344 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
345 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
346 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
347 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700348 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -0800349 EXPECT_FALSE(oat_file_assistant.OatFileExists());
350 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
351 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
352 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700353 EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700354 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800355}
356
357// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700358// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800359TEST_F(OatFileAssistantTest, NoDexNoOat) {
360 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
361
362 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
363
Richard Uhler9b994ea2015-06-24 08:44:19 -0700364 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
365 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
366
367 // Trying to make the oat file up to date should not fail or crash.
368 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700369 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700370
371 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800372 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
373 EXPECT_EQ(nullptr, oat_file.get());
374}
375
376// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700377// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800378TEST_F(OatFileAssistantTest, OatUpToDate) {
379 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
380 Copy(GetDexSrc1(), dex_location);
381 GenerateOatForTest(dex_location.c_str());
382
383 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
384
Richard Uhler95abd042015-03-24 09:51:28 -0700385 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800386 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
387 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
388 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
389 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
390 EXPECT_TRUE(oat_file_assistant.OatFileExists());
391 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
392 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
393 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler95abd042015-03-24 09:51:28 -0700394 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700395 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800396}
397
398// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700399// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800400TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
401 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
402 Copy(GetMultiDexSrc1(), dex_location);
403 GenerateOatForTest(dex_location.c_str());
404
Richard Uhlere5fed032015-03-18 08:21:11 -0700405 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler95abd042015-03-24 09:51:28 -0700406 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700407 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700408
409 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700410 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800411 ASSERT_TRUE(oat_file.get() != nullptr);
412 EXPECT_TRUE(oat_file->IsExecutable());
413 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700414 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
415 EXPECT_EQ(2u, dex_files.size());
416}
417
Richard Uhler67ff7d12015-05-14 13:21:13 -0700418// Case: We have a MultiDEX file where the secondary dex file is out of date.
419// Expect: The status is kDex2OatNeeded.
420TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) {
421 std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar";
422
423 // Compile code for GetMultiDexSrc1.
424 Copy(GetMultiDexSrc1(), dex_location);
425 GenerateOatForTest(dex_location.c_str());
426
427 // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum
428 // is out of date.
429 Copy(GetMultiDexSrc2(), dex_location);
430
431 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
432 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700433 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700434}
435
Richard Uhlere5fed032015-03-18 08:21:11 -0700436// Case: We have a MultiDEX file and up-to-date OAT file for it with relative
437// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700438// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700439TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
440 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700441 std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat";
Richard Uhlere5fed032015-03-18 08:21:11 -0700442
443 // Create the dex file
444 Copy(GetMultiDexSrc1(), dex_location);
445
446 // Create the oat file with relative encoded dex location.
447 std::vector<std::string> args;
448 args.push_back("--dex-file=" + dex_location);
449 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
450 args.push_back("--oat-file=" + oat_location);
451
452 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700453 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700454
455 // Verify we can load both dex files.
456 OatFileAssistant oat_file_assistant(dex_location.c_str(),
457 oat_location.c_str(),
458 kRuntimeISA, true);
459 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
460 ASSERT_TRUE(oat_file.get() != nullptr);
461 EXPECT_TRUE(oat_file->IsExecutable());
462 std::vector<std::unique_ptr<const DexFile>> dex_files;
463 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800464 EXPECT_EQ(2u, dex_files.size());
465}
466
Richard Uhler95abd042015-03-24 09:51:28 -0700467// Case: We have a DEX file and out-of-date OAT file.
468// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800469TEST_F(OatFileAssistantTest, OatOutOfDate) {
470 std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar";
471
472 // We create a dex, generate an oat for it, then overwrite the dex with a
473 // different dex to make the oat out of date.
474 Copy(GetDexSrc1(), dex_location);
475 GenerateOatForTest(dex_location.c_str());
476 Copy(GetDexSrc2(), dex_location);
477
478 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler95abd042015-03-24 09:51:28 -0700479 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800480
481 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
482 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
483 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
484 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
485 EXPECT_TRUE(oat_file_assistant.OatFileExists());
486 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
487 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700488 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800489}
490
491// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700492// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800493TEST_F(OatFileAssistantTest, DexOdexNoOat) {
494 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700495 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800496
497 // Create the dex and odex files
498 Copy(GetDexSrc1(), dex_location);
499 GenerateOdexForTest(dex_location, odex_location);
500
501 // Verify the status.
502 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
503
Richard Uhler95abd042015-03-24 09:51:28 -0700504 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800505
506 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
507 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
508 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
509 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
510 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
Richard Uhler66d874d2015-01-15 09:37:19 -0800511 EXPECT_FALSE(oat_file_assistant.OatFileExists());
512 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
513 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700514 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700515
516 // We should still be able to get the non-executable odex file to run from.
517 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
518 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800519}
520
521// Case: We have a stripped DEX file and an ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700522// Expect: The status is kPatchOatNeeded
Richard Uhler66d874d2015-01-15 09:37:19 -0800523TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
524 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700525 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800526
527 // Create the dex and odex files
528 Copy(GetDexSrc1(), dex_location);
529 GenerateOdexForTest(dex_location, odex_location);
530
531 // Strip the dex file
532 Copy(GetStrippedDexSrc1(), dex_location);
533
534 // Verify the status.
535 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
536
Richard Uhler95abd042015-03-24 09:51:28 -0700537 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800538
539 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
540 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
541 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
542 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
543 EXPECT_FALSE(oat_file_assistant.OatFileExists());
544 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
545 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700546 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800547
548 // Make the oat file up to date.
549 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700550 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800551
Richard Uhler95abd042015-03-24 09:51:28 -0700552 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800553
554 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
555 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
556 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
557 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
558 EXPECT_TRUE(oat_file_assistant.OatFileExists());
559 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
560 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700561 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800562
563 // Verify we can load the dex files from it.
564 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
565 ASSERT_TRUE(oat_file.get() != nullptr);
566 EXPECT_TRUE(oat_file->IsExecutable());
567 std::vector<std::unique_ptr<const DexFile>> dex_files;
568 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
569 EXPECT_EQ(1u, dex_files.size());
570}
571
Richard Uhler95abd042015-03-24 09:51:28 -0700572// Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file.
573// Expect: The status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800574TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
575 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700576 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800577
578 // Create the oat file from a different dex file so it looks out of date.
579 Copy(GetDexSrc2(), dex_location);
580 GenerateOatForTest(dex_location.c_str());
581
582 // Create the odex file
583 Copy(GetDexSrc1(), dex_location);
584 GenerateOdexForTest(dex_location, odex_location);
585
586 // Strip the dex file.
587 Copy(GetStrippedDexSrc1(), dex_location);
588
589 // Verify the status.
590 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
591
Richard Uhler95abd042015-03-24 09:51:28 -0700592 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800593
594 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
595 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
596 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
597 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
598 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
599 EXPECT_TRUE(oat_file_assistant.OatFileExists());
600 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
601 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700602 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800603
604 // Make the oat file up to date.
605 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700606 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800607
Richard Uhler95abd042015-03-24 09:51:28 -0700608 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800609
610 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
611 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
612 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
613 EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation());
614 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
615 EXPECT_TRUE(oat_file_assistant.OatFileExists());
616 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
617 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
618 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700619 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800620
621 // Verify we can load the dex files from it.
622 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
623 ASSERT_TRUE(oat_file.get() != nullptr);
624 EXPECT_TRUE(oat_file->IsExecutable());
625 std::vector<std::unique_ptr<const DexFile>> dex_files;
626 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
627 EXPECT_EQ(1u, dex_files.size());
628}
629
Richard Uhler9b994ea2015-06-24 08:44:19 -0700630// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
631// OAT file. Expect: The status is kNoDexOptNeeded.
632TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
633 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
634
635 Copy(GetStrippedDexSrc1(), dex_location);
636
637 // Verify the status.
638 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
639
640 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
641
642 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
643 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
644 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
645 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
646 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
647 EXPECT_FALSE(oat_file_assistant.OatFileExists());
648 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
649 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
650 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
651
652 // Make the oat file up to date. This should have no effect.
653 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700654 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700655
656 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
657
658 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
659 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
660 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
661 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
662 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
663 EXPECT_FALSE(oat_file_assistant.OatFileExists());
664 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
665 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
666 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
667}
668
Richard Uhler95abd042015-03-24 09:51:28 -0700669// Case: We have a DEX file, no ODEX file and an OAT file that needs
670// relocation.
671// Expect: The status is kSelfPatchOatNeeded.
672TEST_F(OatFileAssistantTest, SelfRelocation) {
673 std::string dex_location = GetScratchDir() + "/SelfRelocation.jar";
674 std::string oat_location = GetOdexDir() + "/SelfRelocation.oat";
675
676 // Create the dex and odex files
677 Copy(GetDexSrc1(), dex_location);
678 GenerateOdexForTest(dex_location, oat_location);
679
680 OatFileAssistant oat_file_assistant(dex_location.c_str(),
681 oat_location.c_str(), kRuntimeISA, true);
682
683 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
684
685 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
686 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
687 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
688 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
689 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
690 EXPECT_TRUE(oat_file_assistant.OatFileExists());
691 EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation());
692 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
693 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700694 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700695
696 // Make the oat file up to date.
697 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700698 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler95abd042015-03-24 09:51:28 -0700699
700 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
701
702 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
703 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
704 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
705 EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation());
706 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
707 EXPECT_TRUE(oat_file_assistant.OatFileExists());
708 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
709 EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation());
710 EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700711 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700712
713 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
714 ASSERT_TRUE(oat_file.get() != nullptr);
715 EXPECT_TRUE(oat_file->IsExecutable());
716 std::vector<std::unique_ptr<const DexFile>> dex_files;
717 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
718 EXPECT_EQ(1u, dex_files.size());
719}
720
Richard Uhler66d874d2015-01-15 09:37:19 -0800721// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
722// OAT files both have patch delta of 0.
Richard Uhler95abd042015-03-24 09:51:28 -0700723// Expect: It shouldn't crash, and status is kPatchOatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800724TEST_F(OatFileAssistantTest, OdexOatOverlap) {
725 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700726 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
727 std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -0800728
729 // Create the dex and odex files
730 Copy(GetDexSrc1(), dex_location);
731 GenerateOdexForTest(dex_location, odex_location);
732
733 // Create the oat file by copying the odex so they are located in the same
734 // place in memory.
735 Copy(odex_location, oat_location);
736
737 // Verify things don't go bad.
738 OatFileAssistant oat_file_assistant(dex_location.c_str(),
739 oat_location.c_str(), kRuntimeISA, true);
740
Richard Uhler95abd042015-03-24 09:51:28 -0700741 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800742
743 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
744 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
745 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
746 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
747 EXPECT_TRUE(oat_file_assistant.OatFileExists());
748 EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate());
749 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700750 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800751
752 // Things aren't relocated, so it should fall back to interpreted.
753 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
754 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700755
Richard Uhler66d874d2015-01-15 09:37:19 -0800756 EXPECT_FALSE(oat_file->IsExecutable());
757 std::vector<std::unique_ptr<const DexFile>> dex_files;
758 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
759 EXPECT_EQ(1u, dex_files.size());
760}
761
762// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler95abd042015-03-24 09:51:28 -0700763// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
Richard Uhler66d874d2015-01-15 09:37:19 -0800764TEST_F(OatFileAssistantTest, DexPicOdexNoOat) {
765 std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700766 std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800767
768 // Create the dex and odex files
769 Copy(GetDexSrc1(), dex_location);
770 GeneratePicOdexForTest(dex_location, odex_location);
771
772 // Verify the status.
773 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
774
Richard Uhler95abd042015-03-24 09:51:28 -0700775 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800776
777 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
778 EXPECT_TRUE(oat_file_assistant.OdexFileExists());
779 EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate());
780 EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate());
781 EXPECT_FALSE(oat_file_assistant.OatFileExists());
782 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
783 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700784 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800785}
786
787// Case: We have a DEX file and up-to-date OAT file for it.
788// Expect: We should load an executable dex file.
789TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
790 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
791
792 Copy(GetDexSrc1(), dex_location);
793 GenerateOatForTest(dex_location.c_str());
794
795 // Load the oat using an oat file assistant.
796 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
797
798 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
799 ASSERT_TRUE(oat_file.get() != nullptr);
800 EXPECT_TRUE(oat_file->IsExecutable());
801 std::vector<std::unique_ptr<const DexFile>> dex_files;
802 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
803 EXPECT_EQ(1u, dex_files.size());
804}
805
806// Case: We have a DEX file and up-to-date OAT file for it.
807// Expect: Loading non-executable should load the oat non-executable.
808TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
809 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
810
811 Copy(GetDexSrc1(), dex_location);
812 GenerateOatForTest(dex_location.c_str());
813
814 // Load the oat using an oat file assistant.
815 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
816
817 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
818 ASSERT_TRUE(oat_file.get() != nullptr);
819 EXPECT_FALSE(oat_file->IsExecutable());
820 std::vector<std::unique_ptr<const DexFile>> dex_files;
821 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
822 EXPECT_EQ(1u, dex_files.size());
823}
824
825// Case: We have a DEX file.
826// Expect: We should load an executable dex file from an alternative oat
827// location.
828TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) {
829 std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar";
830 std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat";
831
832 Copy(GetDexSrc1(), dex_location);
833
834 OatFileAssistant oat_file_assistant(
835 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
836 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700837 ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800838
839 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
840 ASSERT_TRUE(oat_file.get() != nullptr);
841 EXPECT_TRUE(oat_file->IsExecutable());
842 std::vector<std::unique_ptr<const DexFile>> dex_files;
843 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
844 EXPECT_EQ(1u, dex_files.size());
845
846 EXPECT_TRUE(OS::FileExists(oat_location.c_str()));
847
848 // Verify it didn't create an oat in the default location.
849 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
850 EXPECT_FALSE(ofm.OatFileExists());
851}
852
Richard Uhler8327cf72015-10-13 16:34:59 -0700853// Case: We have a DEX file but can't write the oat file.
854// Expect: We should fail to make the oat file up to date.
855TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) {
856 std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar";
857
858 // Make the oat location unwritable by inserting some non-existent
859 // intermediate directories.
860 std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat";
861
862 Copy(GetDexSrc1(), dex_location);
863
864 OatFileAssistant oat_file_assistant(
865 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
866 std::string error_msg;
867 ASSERT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg));
868
869 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
870 ASSERT_TRUE(oat_file.get() == nullptr);
871}
872
873// Case: We don't have a DEX file and can't write the oat file.
874// Expect: We should fail to generate the oat file without crashing.
875TEST_F(OatFileAssistantTest, GenNoDex) {
876 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
877 std::string oat_location = GetScratchDir() + "/GenNoDex.oat";
878
879 OatFileAssistant oat_file_assistant(
880 dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true);
881 std::string error_msg;
882 ASSERT_FALSE(oat_file_assistant.GenerateOatFile(&error_msg));
883}
884
Richard Uhler66d874d2015-01-15 09:37:19 -0800885// Turn an absolute path into a path relative to the current working
886// directory.
887static std::string MakePathRelative(std::string target) {
888 char buf[MAXPATHLEN];
889 std::string cwd = getcwd(buf, MAXPATHLEN);
890
891 // Split the target and cwd paths into components.
892 std::vector<std::string> target_path;
893 std::vector<std::string> cwd_path;
894 Split(target, '/', &target_path);
895 Split(cwd, '/', &cwd_path);
896
897 // Reverse the path components, so we can use pop_back().
898 std::reverse(target_path.begin(), target_path.end());
899 std::reverse(cwd_path.begin(), cwd_path.end());
900
901 // Drop the common prefix of the paths. Because we reversed the path
902 // components, this becomes the common suffix of target_path and cwd_path.
903 while (!target_path.empty() && !cwd_path.empty()
904 && target_path.back() == cwd_path.back()) {
905 target_path.pop_back();
906 cwd_path.pop_back();
907 }
908
909 // For each element of the remaining cwd_path, add '..' to the beginning
910 // of the target path. Because we reversed the path components, we add to
911 // the end of target_path.
912 for (unsigned int i = 0; i < cwd_path.size(); i++) {
913 target_path.push_back("..");
914 }
915
916 // Reverse again to get the right path order, and join to get the result.
917 std::reverse(target_path.begin(), target_path.end());
918 return Join(target_path, '/');
919}
920
921// Case: Non-absolute path to Dex location.
922// Expect: Not sure, but it shouldn't crash.
923TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
924 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
925 Copy(GetDexSrc1(), abs_dex_location);
926
927 std::string dex_location = MakePathRelative(abs_dex_location);
928 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
929
930 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler95abd042015-03-24 09:51:28 -0700931 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800932 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
933 EXPECT_FALSE(oat_file_assistant.OatFileExists());
934 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
935 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
936 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
937 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
938}
939
940// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700941// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800942TEST_F(OatFileAssistantTest, ShortDexLocation) {
943 std::string dex_location = "/xx";
944
945 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
946
947 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700948 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800949 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
950 EXPECT_FALSE(oat_file_assistant.OatFileExists());
951 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
952 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
953 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
954 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700955 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800956
Richard Uhler9b994ea2015-06-24 08:44:19 -0700957 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -0800958 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700959 EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700960 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -0800961}
962
963// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -0700964// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800965TEST_F(OatFileAssistantTest, LongDexExtension) {
966 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
967 Copy(GetDexSrc1(), dex_location);
968
969 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
970
Richard Uhler95abd042015-03-24 09:51:28 -0700971 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded());
Richard Uhler66d874d2015-01-15 09:37:19 -0800972
973 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
974 EXPECT_FALSE(oat_file_assistant.OdexFileExists());
975 EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate());
976 EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate());
977 EXPECT_FALSE(oat_file_assistant.OatFileExists());
978 EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate());
979 EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate());
980}
981
982// A task to generate a dex location. Used by the RaceToGenerate test.
983class RaceGenerateTask : public Task {
984 public:
985 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
986 : dex_location_(dex_location), oat_location_(oat_location),
987 loaded_oat_file_(nullptr)
988 {}
989
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100990 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800991 // Load the dex files, and save a pointer to the loaded oat file, so that
992 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -0800993 std::vector<std::unique_ptr<const DexFile>> dex_files;
994 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700995 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700996 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
997 dex_location_.c_str(),
998 oat_location_.c_str(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800999 /*class_loader*/nullptr,
1000 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001001 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001002 &error_msgs);
Richard Uhler66d874d2015-01-15 09:37:19 -08001003 CHECK(!dex_files.empty()) << Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001004 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1005 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001006 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001007 }
1008
1009 const OatFile* GetLoadedOatFile() const {
1010 return loaded_oat_file_;
1011 }
1012
1013 private:
1014 std::string dex_location_;
1015 std::string oat_location_;
1016 const OatFile* loaded_oat_file_;
1017};
1018
1019// Test the case where multiple processes race to generate an oat file.
1020// This simulates multiple processes using multiple threads.
1021//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001022// We want unique Oat files to be loaded even when there is a race to load.
1023// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1024// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001025TEST_F(OatFileAssistantTest, RaceToGenerate) {
1026 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001027 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001028
1029 // We use the lib core dex file, because it's large, and hopefully should
1030 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001031 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001032
1033 const int kNumThreads = 32;
1034 Thread* self = Thread::Current();
1035 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1036 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1037 for (int i = 0; i < kNumThreads; i++) {
1038 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1039 thread_pool.AddTask(self, task.get());
1040 tasks.push_back(std::move(task));
1041 }
1042 thread_pool.StartWorkers(self);
1043 thread_pool.Wait(self, true, false);
1044
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001045 // Verify every task got a unique oat file.
1046 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001047 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001048 const OatFile* oat_file = task->GetLoadedOatFile();
1049 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1050 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001051 }
1052}
1053
1054// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1055// disabled.
1056// Expect: We should load the odex file non-executable.
1057TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1058 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001059 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001060
1061 // Create the dex and odex files
1062 Copy(GetDexSrc1(), dex_location);
1063 GenerateOdexForTest(dex_location, odex_location);
1064
1065 // Load the oat using an executable oat file assistant.
1066 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
1067
1068 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1069 ASSERT_TRUE(oat_file.get() != nullptr);
1070 EXPECT_FALSE(oat_file->IsExecutable());
1071 std::vector<std::unique_ptr<const DexFile>> dex_files;
1072 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1073 EXPECT_EQ(1u, dex_files.size());
1074}
1075
1076// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1077// disabled.
1078// Expect: We should load the odex file non-executable.
1079TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1080 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001081 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001082
1083 // Create the dex and odex files
1084 Copy(GetMultiDexSrc1(), dex_location);
1085 GenerateOdexForTest(dex_location, odex_location);
1086
1087 // Load the oat using an executable oat file assistant.
1088 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
1089
1090 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1091 ASSERT_TRUE(oat_file.get() != nullptr);
1092 EXPECT_FALSE(oat_file->IsExecutable());
1093 std::vector<std::unique_ptr<const DexFile>> dex_files;
1094 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1095 EXPECT_EQ(2u, dex_files.size());
1096}
1097
1098TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) {
1099 std::string error_msg;
1100 std::string odex_file;
1101
1102 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001103 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001104 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001105
1106 EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001107 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001108 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001109
1110 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001111 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001112 EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001113 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001114}
1115
Richard Uhler23cedd22015-04-08 13:17:29 -07001116// Verify the dexopt status values from dalvik.system.DexFile
1117// match the OatFileAssistant::DexOptStatus values.
1118TEST_F(OatFileAssistantTest, DexOptStatusValues) {
1119 ScopedObjectAccess soa(Thread::Current());
1120 StackHandleScope<1> hs(soa.Self());
1121 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1122 Handle<mirror::Class> dexfile(
1123 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
1124 ASSERT_FALSE(dexfile.Get() == nullptr);
1125 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1126
Mathieu Chartierc7853442015-03-27 14:35:38 -07001127 ArtField* no_dexopt_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001128 soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I");
1129 ASSERT_FALSE(no_dexopt_needed == nullptr);
1130 EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1131 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get()));
1132
Mathieu Chartierc7853442015-03-27 14:35:38 -07001133 ArtField* dex2oat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001134 soa.Self(), dexfile, "DEX2OAT_NEEDED", "I");
1135 ASSERT_FALSE(dex2oat_needed == nullptr);
1136 EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1137 EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get()));
1138
Mathieu Chartierc7853442015-03-27 14:35:38 -07001139 ArtField* patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001140 soa.Self(), dexfile, "PATCHOAT_NEEDED", "I");
1141 ASSERT_FALSE(patchoat_needed == nullptr);
1142 EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1143 EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get()));
1144
Mathieu Chartierc7853442015-03-27 14:35:38 -07001145 ArtField* self_patchoat_needed = mirror::Class::FindStaticField(
Richard Uhler23cedd22015-04-08 13:17:29 -07001146 soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I");
1147 ASSERT_FALSE(self_patchoat_needed == nullptr);
1148 EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1149 EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get()));
1150}
Richard Uhler66d874d2015-01-15 09:37:19 -08001151
1152// TODO: More Tests:
1153// * Test class linker falls back to unquickened dex for DexNoOat
1154// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001155// * Test using secondary isa
1156// * Test with profiling info?
1157// * Test for status of oat while oat is being generated (how?)
1158// * Test case where 32 and 64 bit boot class paths differ,
1159// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1160// 64 bit boot class paths.
1161// * Test unexpected scenarios (?):
1162// - Dex is stripped, don't have odex.
1163// - Oat file corrupted after status check, before reload unexecutable
1164// because it's unrelocated and no dex2oat
1165
1166} // namespace art