Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | #include "class_linker.h" |
| 29 | #include "common_runtime_test.h" |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 30 | #include "compiler_callbacks.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 31 | #include "mem_map.h" |
| 32 | #include "os.h" |
| 33 | #include "thread-inl.h" |
| 34 | #include "utils.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | class OatFileAssistantTest : public CommonRuntimeTest { |
| 39 | public: |
| 40 | virtual void SetUp() { |
| 41 | ReserveImageSpace(); |
| 42 | CommonRuntimeTest::SetUp(); |
| 43 | |
| 44 | // Create a scratch directory to work from. |
| 45 | scratch_dir_ = android_data_ + "/OatFileAssistantTest"; |
| 46 | ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700)); |
| 47 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 48 | // Create a subdirectory in scratch for odex files. |
| 49 | odex_oat_dir_ = scratch_dir_ + "/oat"; |
| 50 | ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700)); |
| 51 | |
| 52 | odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA)); |
| 53 | ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700)); |
| 54 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 55 | |
| 56 | // Verify the environment is as we expect |
| 57 | uint32_t checksum; |
| 58 | std::string error_msg; |
| 59 | ASSERT_TRUE(OS::FileExists(GetImageFile().c_str())) |
| 60 | << "Expected pre-compiled boot image to be at: " << GetImageFile(); |
| 61 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 62 | << "Expected dex file to be at: " << GetDexSrc1(); |
| 63 | ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str())) |
| 64 | << "Expected stripped dex file to be at: " << GetStrippedDexSrc1(); |
| 65 | ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg)) |
| 66 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
| 67 | ASSERT_TRUE(OS::FileExists(GetMultiDexSrc1().c_str())) |
| 68 | << "Expected multidex file to be at: " << GetMultiDexSrc1(); |
| 69 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 70 | << "Expected dex file to be at: " << GetDexSrc2(); |
| 71 | } |
| 72 | |
| 73 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
Richard Uhler | 892fc96 | 2015-03-10 16:57:05 +0000 | [diff] [blame] | 74 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 75 | |
| 76 | // Set up the image location. |
| 77 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 78 | nullptr)); |
| 79 | // Make sure compilercallbacks are not set so that relocation will be |
| 80 | // enabled. |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 81 | callbacks_.reset(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | virtual void PreRuntimeCreate() { |
| 85 | UnreserveImageSpace(); |
| 86 | } |
| 87 | |
| 88 | virtual void PostRuntimeCreate() { |
| 89 | ReserveImageSpace(); |
| 90 | } |
| 91 | |
| 92 | virtual void TearDown() { |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 93 | ClearDirectory(odex_dir_.c_str()); |
| 94 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 95 | |
| 96 | ClearDirectory(odex_oat_dir_.c_str()); |
| 97 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 98 | |
| 99 | ClearDirectory(scratch_dir_.c_str()); |
| 100 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 101 | |
| 102 | CommonRuntimeTest::TearDown(); |
| 103 | } |
| 104 | |
| 105 | void Copy(std::string src, std::string dst) { |
| 106 | std::ifstream src_stream(src, std::ios::binary); |
| 107 | std::ofstream dst_stream(dst, std::ios::binary); |
| 108 | |
| 109 | dst_stream << src_stream.rdbuf(); |
| 110 | } |
| 111 | |
| 112 | // Returns the directory where the pre-compiled core.art can be found. |
| 113 | // TODO: We should factor out this into common tests somewhere rather than |
| 114 | // re-hardcoding it here (This was copied originally from the elf writer |
| 115 | // test). |
| 116 | std::string GetImageDirectory() { |
| 117 | if (IsHost()) { |
| 118 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 119 | CHECK(host_dir != NULL); |
| 120 | return std::string(host_dir) + "/framework"; |
| 121 | } else { |
| 122 | return std::string("/data/art-test"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | std::string GetImageLocation() { |
| 127 | return GetImageDirectory() + "/core.art"; |
| 128 | } |
| 129 | |
| 130 | std::string GetImageFile() { |
| 131 | return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA) |
| 132 | + "/core.art"; |
| 133 | } |
| 134 | |
| 135 | std::string GetDexSrc1() { |
| 136 | return GetTestDexFileName("Main"); |
| 137 | } |
| 138 | |
| 139 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 140 | // file stripped. |
| 141 | std::string GetStrippedDexSrc1() { |
| 142 | return GetTestDexFileName("MainStripped"); |
| 143 | } |
| 144 | |
| 145 | std::string GetMultiDexSrc1() { |
| 146 | return GetTestDexFileName("MultiDex"); |
| 147 | } |
| 148 | |
| 149 | std::string GetDexSrc2() { |
| 150 | return GetTestDexFileName("Nested"); |
| 151 | } |
| 152 | |
| 153 | // Scratch directory, for dex and odex files (oat files will go in the |
| 154 | // dalvik cache). |
| 155 | std::string GetScratchDir() { |
| 156 | return scratch_dir_; |
| 157 | } |
| 158 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 159 | // Odex directory is the subdirectory in the scratch directory where odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 160 | // files should be located. |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 161 | std::string GetOdexDir() { |
| 162 | return odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Generate an odex file for the purposes of test. |
| 166 | // If pic is true, generates a PIC odex. |
| 167 | void GenerateOdexForTest(const std::string& dex_location, |
| 168 | const std::string& odex_location, |
| 169 | bool pic = false) { |
| 170 | // For this operation, we temporarily redirect the dalvik cache so dex2oat |
| 171 | // doesn't find the relocated image file. |
| 172 | std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp"; |
| 173 | setenv("ANDROID_DATA", android_data_tmp.c_str(), 1); |
| 174 | std::vector<std::string> args; |
| 175 | args.push_back("--dex-file=" + dex_location); |
| 176 | args.push_back("--oat-file=" + odex_location); |
| 177 | if (pic) { |
| 178 | args.push_back("--compile-pic"); |
| 179 | } else { |
| 180 | args.push_back("--include-patch-information"); |
Richard Uhler | 05dd8a6 | 2015-03-10 10:02:23 -0700 | [diff] [blame] | 181 | |
| 182 | // We need to use the quick compiler to generate non-PIC code, because |
| 183 | // the optimizing compiler always generates PIC. |
| 184 | args.push_back("--compiler-backend=Quick"); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 185 | } |
| 186 | args.push_back("--runtime-arg"); |
| 187 | args.push_back("-Xnorelocate"); |
| 188 | std::string error_msg; |
| 189 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 190 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
| 191 | } |
| 192 | |
| 193 | void GeneratePicOdexForTest(const std::string& dex_location, |
| 194 | const std::string& odex_location) { |
| 195 | GenerateOdexForTest(dex_location, odex_location, true); |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | // Reserve memory around where the image will be loaded so other memory |
| 200 | // won't conflict when it comes time to load the image. |
| 201 | // This can be called with an already loaded image to reserve the space |
| 202 | // around it. |
| 203 | void ReserveImageSpace() { |
| 204 | MemMap::Init(); |
| 205 | |
| 206 | // Ensure a chunk of memory is reserved for the image space. |
| 207 | uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA; |
| 208 | uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 209 | // Include the main space that has to come right after the |
| 210 | // image in case of the GSS collector. |
| 211 | + 384 * MB; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 212 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 213 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 214 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 215 | for (BacktraceMap::const_iterator it = map->begin(); |
| 216 | reservation_start < reservation_end && it != map->end(); ++it) { |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 217 | ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end)); |
| 218 | reservation_start = std::max(reservation_start, it->end); |
| 219 | } |
| 220 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 221 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 222 | |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 223 | // Reserve a chunk of memory for the image space in the given range. |
| 224 | // Only has effect for chunks with a positive number of bytes. |
| 225 | void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 226 | if (start < end) { |
| 227 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 228 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 229 | MemMap::MapAnonymous("image reservation", |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 230 | reinterpret_cast<uint8_t*>(start), end - start, |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 231 | PROT_NONE, false, false, &error_msg))); |
| 232 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 233 | LOG(INFO) << "Reserved space for image " << |
| 234 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 235 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | // Unreserve any memory reserved by ReserveImageSpace. This should be called |
| 241 | // before the image is loaded. |
| 242 | void UnreserveImageSpace() { |
| 243 | image_reservation_.clear(); |
| 244 | } |
| 245 | |
| 246 | std::string scratch_dir_; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 247 | std::string odex_oat_dir_; |
| 248 | std::string odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 249 | std::vector<std::unique_ptr<MemMap>> image_reservation_; |
| 250 | }; |
| 251 | |
| 252 | class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest { |
| 253 | public: |
| 254 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
| 255 | OatFileAssistantTest::SetUpRuntimeOptions(options); |
| 256 | options->push_back(std::make_pair("-Xnodex2oat", nullptr)); |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | // Generate an oat file for the purposes of test, as opposed to testing |
| 261 | // generation of oat files. |
| 262 | static void GenerateOatForTest(const char* dex_location) { |
| 263 | OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false); |
| 264 | |
| 265 | std::string error_msg; |
| 266 | ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg; |
| 267 | } |
| 268 | |
| 269 | // Case: We have a DEX file, but no OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 270 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 271 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 272 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 273 | Copy(GetDexSrc1(), dex_location); |
| 274 | |
| 275 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 276 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 277 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 278 | |
| 279 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 280 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 281 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 282 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 283 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 284 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 285 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 286 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 287 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 288 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 289 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // Case: We have no DEX file and no OAT file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 293 | // Expect: Status is kDex2OatNeeded. Loading should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 294 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 295 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 296 | |
| 297 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 298 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 299 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 300 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 301 | EXPECT_EQ(nullptr, oat_file.get()); |
| 302 | } |
| 303 | |
| 304 | // Case: We have a DEX file and up-to-date OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 305 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 306 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 307 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 308 | Copy(GetDexSrc1(), dex_location); |
| 309 | GenerateOatForTest(dex_location.c_str()); |
| 310 | |
| 311 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 312 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 313 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 314 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 315 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 316 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 317 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 318 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 319 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 320 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 321 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 322 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Case: We have a MultiDEX file and up-to-date OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 326 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 327 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 328 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 329 | Copy(GetMultiDexSrc1(), dex_location); |
| 330 | GenerateOatForTest(dex_location.c_str()); |
| 331 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 332 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 333 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 334 | |
| 335 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 336 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 337 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 338 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 339 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 340 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 341 | EXPECT_EQ(2u, dex_files.size()); |
| 342 | } |
| 343 | |
| 344 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 345 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 346 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 347 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 348 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 349 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 350 | |
| 351 | // Create the dex file |
| 352 | Copy(GetMultiDexSrc1(), dex_location); |
| 353 | |
| 354 | // Create the oat file with relative encoded dex location. |
| 355 | std::vector<std::string> args; |
| 356 | args.push_back("--dex-file=" + dex_location); |
| 357 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 358 | args.push_back("--oat-file=" + oat_location); |
| 359 | |
| 360 | std::string error_msg; |
| 361 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 362 | |
| 363 | // Verify we can load both dex files. |
| 364 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 365 | oat_location.c_str(), |
| 366 | kRuntimeISA, true); |
| 367 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 368 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 369 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 370 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 371 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 372 | EXPECT_EQ(2u, dex_files.size()); |
| 373 | } |
| 374 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 375 | // Case: We have a DEX file and out-of-date OAT file. |
| 376 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 377 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 378 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 379 | |
| 380 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 381 | // different dex to make the oat out of date. |
| 382 | Copy(GetDexSrc1(), dex_location); |
| 383 | GenerateOatForTest(dex_location.c_str()); |
| 384 | Copy(GetDexSrc2(), dex_location); |
| 385 | |
| 386 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 387 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 388 | |
| 389 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 390 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 391 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 392 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 393 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 394 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 395 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 396 | } |
| 397 | |
| 398 | // Case: We have a DEX file and an ODEX file, but no OAT file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 399 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 400 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 401 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 402 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 403 | |
| 404 | // Create the dex and odex files |
| 405 | Copy(GetDexSrc1(), dex_location); |
| 406 | GenerateOdexForTest(dex_location, odex_location); |
| 407 | |
| 408 | // Verify the status. |
| 409 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 410 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 411 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 412 | |
| 413 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 414 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 415 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 416 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 417 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 418 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 419 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 420 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 421 | } |
| 422 | |
| 423 | // Case: We have a stripped DEX file and an ODEX file, but no OAT file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 424 | // Expect: The status is kPatchOatNeeded |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 425 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 426 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 427 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 428 | |
| 429 | // Create the dex and odex files |
| 430 | Copy(GetDexSrc1(), dex_location); |
| 431 | GenerateOdexForTest(dex_location, odex_location); |
| 432 | |
| 433 | // Strip the dex file |
| 434 | Copy(GetStrippedDexSrc1(), dex_location); |
| 435 | |
| 436 | // Verify the status. |
| 437 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 438 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 439 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 440 | |
| 441 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 442 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 443 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 444 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 445 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 446 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 447 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 448 | |
| 449 | // Make the oat file up to date. |
| 450 | std::string error_msg; |
| 451 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 452 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 453 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 454 | |
| 455 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 456 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 457 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 458 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 459 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 460 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 461 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 462 | |
| 463 | // Verify we can load the dex files from it. |
| 464 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 465 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 466 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 467 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 468 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 469 | EXPECT_EQ(1u, dex_files.size()); |
| 470 | } |
| 471 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 472 | // Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file. |
| 473 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 474 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 475 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 476 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 477 | |
| 478 | // Create the oat file from a different dex file so it looks out of date. |
| 479 | Copy(GetDexSrc2(), dex_location); |
| 480 | GenerateOatForTest(dex_location.c_str()); |
| 481 | |
| 482 | // Create the odex file |
| 483 | Copy(GetDexSrc1(), dex_location); |
| 484 | GenerateOdexForTest(dex_location, odex_location); |
| 485 | |
| 486 | // Strip the dex file. |
| 487 | Copy(GetStrippedDexSrc1(), dex_location); |
| 488 | |
| 489 | // Verify the status. |
| 490 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 491 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 492 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 493 | |
| 494 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 495 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 496 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 497 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 498 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 499 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 500 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 501 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 502 | |
| 503 | // Make the oat file up to date. |
| 504 | std::string error_msg; |
| 505 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 506 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 507 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 508 | |
| 509 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 510 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 511 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 512 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 513 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 514 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 515 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 516 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 517 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 518 | |
| 519 | // Verify we can load the dex files from it. |
| 520 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 521 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 522 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 523 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 524 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 525 | EXPECT_EQ(1u, dex_files.size()); |
| 526 | } |
| 527 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 528 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 529 | // relocation. |
| 530 | // Expect: The status is kSelfPatchOatNeeded. |
| 531 | TEST_F(OatFileAssistantTest, SelfRelocation) { |
| 532 | std::string dex_location = GetScratchDir() + "/SelfRelocation.jar"; |
| 533 | std::string oat_location = GetOdexDir() + "/SelfRelocation.oat"; |
| 534 | |
| 535 | // Create the dex and odex files |
| 536 | Copy(GetDexSrc1(), dex_location); |
| 537 | GenerateOdexForTest(dex_location, oat_location); |
| 538 | |
| 539 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 540 | oat_location.c_str(), kRuntimeISA, true); |
| 541 | |
| 542 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 543 | |
| 544 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 545 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 546 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 547 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 548 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 549 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 550 | EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation()); |
| 551 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 552 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 553 | |
| 554 | // Make the oat file up to date. |
| 555 | std::string error_msg; |
| 556 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 557 | |
| 558 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 559 | |
| 560 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 561 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 562 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 563 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 564 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 565 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 566 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 567 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 568 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 569 | |
| 570 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 571 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 572 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 573 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 574 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 575 | EXPECT_EQ(1u, dex_files.size()); |
| 576 | } |
| 577 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 578 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 579 | // OAT files both have patch delta of 0. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 580 | // Expect: It shouldn't crash, and status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 581 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 582 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 583 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 584 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 585 | |
| 586 | // Create the dex and odex files |
| 587 | Copy(GetDexSrc1(), dex_location); |
| 588 | GenerateOdexForTest(dex_location, odex_location); |
| 589 | |
| 590 | // Create the oat file by copying the odex so they are located in the same |
| 591 | // place in memory. |
| 592 | Copy(odex_location, oat_location); |
| 593 | |
| 594 | // Verify things don't go bad. |
| 595 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 596 | oat_location.c_str(), kRuntimeISA, true); |
| 597 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 598 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 599 | |
| 600 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 601 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 602 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 603 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 604 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 605 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 606 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 607 | |
| 608 | // Things aren't relocated, so it should fall back to interpreted. |
| 609 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 610 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 611 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 612 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 613 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 614 | EXPECT_EQ(1u, dex_files.size()); |
| 615 | } |
| 616 | |
| 617 | // Case: We have a DEX file and a PIC ODEX file, but no OAT file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 618 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 619 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 620 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 621 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 622 | |
| 623 | // Create the dex and odex files |
| 624 | Copy(GetDexSrc1(), dex_location); |
| 625 | GeneratePicOdexForTest(dex_location, odex_location); |
| 626 | |
| 627 | // Verify the status. |
| 628 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 629 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 630 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 631 | |
| 632 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 633 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 634 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 635 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 636 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 637 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 638 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 639 | } |
| 640 | |
| 641 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 642 | // Expect: We should load an executable dex file. |
| 643 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 644 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 645 | |
| 646 | Copy(GetDexSrc1(), dex_location); |
| 647 | GenerateOatForTest(dex_location.c_str()); |
| 648 | |
| 649 | // Load the oat using an oat file assistant. |
| 650 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 651 | |
| 652 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 653 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 654 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 655 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 656 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 657 | EXPECT_EQ(1u, dex_files.size()); |
| 658 | } |
| 659 | |
| 660 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 661 | // Expect: Loading non-executable should load the oat non-executable. |
| 662 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 663 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 664 | |
| 665 | Copy(GetDexSrc1(), dex_location); |
| 666 | GenerateOatForTest(dex_location.c_str()); |
| 667 | |
| 668 | // Load the oat using an oat file assistant. |
| 669 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 670 | |
| 671 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 672 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 673 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 674 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 675 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 676 | EXPECT_EQ(1u, dex_files.size()); |
| 677 | } |
| 678 | |
| 679 | // Case: We have a DEX file. |
| 680 | // Expect: We should load an executable dex file from an alternative oat |
| 681 | // location. |
| 682 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 683 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 684 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 685 | |
| 686 | Copy(GetDexSrc1(), dex_location); |
| 687 | |
| 688 | OatFileAssistant oat_file_assistant( |
| 689 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
| 690 | std::string error_msg; |
| 691 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 692 | |
| 693 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 694 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 695 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 696 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 697 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 698 | EXPECT_EQ(1u, dex_files.size()); |
| 699 | |
| 700 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 701 | |
| 702 | // Verify it didn't create an oat in the default location. |
| 703 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false); |
| 704 | EXPECT_FALSE(ofm.OatFileExists()); |
| 705 | } |
| 706 | |
| 707 | // Case: Non-existent Dex location. |
| 708 | // Expect: The dex code is out of date, and trying to update it fails. |
| 709 | TEST_F(OatFileAssistantTest, NonExsistentDexLocation) { |
| 710 | std::string dex_location = GetScratchDir() + "/BadDexLocation.jar"; |
| 711 | |
| 712 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 713 | |
| 714 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 715 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 716 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 717 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 718 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 719 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 720 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 721 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 722 | |
| 723 | std::string error_msg; |
| 724 | EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg)); |
| 725 | EXPECT_FALSE(error_msg.empty()); |
| 726 | } |
| 727 | |
| 728 | // Turn an absolute path into a path relative to the current working |
| 729 | // directory. |
| 730 | static std::string MakePathRelative(std::string target) { |
| 731 | char buf[MAXPATHLEN]; |
| 732 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 733 | |
| 734 | // Split the target and cwd paths into components. |
| 735 | std::vector<std::string> target_path; |
| 736 | std::vector<std::string> cwd_path; |
| 737 | Split(target, '/', &target_path); |
| 738 | Split(cwd, '/', &cwd_path); |
| 739 | |
| 740 | // Reverse the path components, so we can use pop_back(). |
| 741 | std::reverse(target_path.begin(), target_path.end()); |
| 742 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 743 | |
| 744 | // Drop the common prefix of the paths. Because we reversed the path |
| 745 | // components, this becomes the common suffix of target_path and cwd_path. |
| 746 | while (!target_path.empty() && !cwd_path.empty() |
| 747 | && target_path.back() == cwd_path.back()) { |
| 748 | target_path.pop_back(); |
| 749 | cwd_path.pop_back(); |
| 750 | } |
| 751 | |
| 752 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 753 | // of the target path. Because we reversed the path components, we add to |
| 754 | // the end of target_path. |
| 755 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 756 | target_path.push_back(".."); |
| 757 | } |
| 758 | |
| 759 | // Reverse again to get the right path order, and join to get the result. |
| 760 | std::reverse(target_path.begin(), target_path.end()); |
| 761 | return Join(target_path, '/'); |
| 762 | } |
| 763 | |
| 764 | // Case: Non-absolute path to Dex location. |
| 765 | // Expect: Not sure, but it shouldn't crash. |
| 766 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 767 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 768 | Copy(GetDexSrc1(), abs_dex_location); |
| 769 | |
| 770 | std::string dex_location = MakePathRelative(abs_dex_location); |
| 771 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 772 | |
| 773 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 774 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 775 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 776 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 777 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 778 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 779 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 780 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 781 | } |
| 782 | |
| 783 | // Case: Very short, non-existent Dex location. |
| 784 | // Expect: Dex code is out of date, and trying to update it fails. |
| 785 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 786 | std::string dex_location = "/xx"; |
| 787 | |
| 788 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 789 | |
| 790 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 791 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 792 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 793 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 794 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 795 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 796 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 797 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 798 | |
| 799 | std::string error_msg; |
| 800 | EXPECT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg)); |
| 801 | EXPECT_FALSE(error_msg.empty()); |
| 802 | } |
| 803 | |
| 804 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 805 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 806 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 807 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 808 | Copy(GetDexSrc1(), dex_location); |
| 809 | |
| 810 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 811 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame^] | 812 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 813 | |
| 814 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 815 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 816 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 817 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 818 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 819 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 820 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 821 | } |
| 822 | |
| 823 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 824 | class RaceGenerateTask : public Task { |
| 825 | public: |
| 826 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
| 827 | : dex_location_(dex_location), oat_location_(oat_location), |
| 828 | loaded_oat_file_(nullptr) |
| 829 | {} |
| 830 | |
| 831 | void Run(Thread* self) { |
| 832 | UNUSED(self); |
| 833 | |
| 834 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 835 | // we can verify only one oat file was loaded for the dex location. |
| 836 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 837 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 838 | std::vector<std::string> error_msgs; |
| 839 | dex_files = linker->OpenDexFilesFromOat(dex_location_.c_str(), oat_location_.c_str(), &error_msgs); |
| 840 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 841 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 842 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | const OatFile* GetLoadedOatFile() const { |
| 846 | return loaded_oat_file_; |
| 847 | } |
| 848 | |
| 849 | private: |
| 850 | std::string dex_location_; |
| 851 | std::string oat_location_; |
| 852 | const OatFile* loaded_oat_file_; |
| 853 | }; |
| 854 | |
| 855 | // Test the case where multiple processes race to generate an oat file. |
| 856 | // This simulates multiple processes using multiple threads. |
| 857 | // |
| 858 | // We want only one Oat file to be loaded when there is a race to load, to |
| 859 | // avoid using up the virtual memory address space. |
| 860 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 861 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 862 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 863 | |
| 864 | // We use the lib core dex file, because it's large, and hopefully should |
| 865 | // take a while to generate. |
| 866 | Copy(GetLibCoreDexFileName(), dex_location); |
| 867 | |
| 868 | const int kNumThreads = 32; |
| 869 | Thread* self = Thread::Current(); |
| 870 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 871 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 872 | for (int i = 0; i < kNumThreads; i++) { |
| 873 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 874 | thread_pool.AddTask(self, task.get()); |
| 875 | tasks.push_back(std::move(task)); |
| 876 | } |
| 877 | thread_pool.StartWorkers(self); |
| 878 | thread_pool.Wait(self, true, false); |
| 879 | |
| 880 | // Verify every task got the same pointer. |
| 881 | const OatFile* expected = tasks[0]->GetLoadedOatFile(); |
| 882 | for (auto& task : tasks) { |
| 883 | EXPECT_EQ(expected, task->GetLoadedOatFile()); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 888 | // disabled. |
| 889 | // Expect: We should load the odex file non-executable. |
| 890 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 891 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 892 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 893 | |
| 894 | // Create the dex and odex files |
| 895 | Copy(GetDexSrc1(), dex_location); |
| 896 | GenerateOdexForTest(dex_location, odex_location); |
| 897 | |
| 898 | // Load the oat using an executable oat file assistant. |
| 899 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 900 | |
| 901 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 902 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 903 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 904 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 905 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 906 | EXPECT_EQ(1u, dex_files.size()); |
| 907 | } |
| 908 | |
| 909 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 910 | // disabled. |
| 911 | // Expect: We should load the odex file non-executable. |
| 912 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 913 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 914 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 915 | |
| 916 | // Create the dex and odex files |
| 917 | Copy(GetMultiDexSrc1(), dex_location); |
| 918 | GenerateOdexForTest(dex_location, odex_location); |
| 919 | |
| 920 | // Load the oat using an executable oat file assistant. |
| 921 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 922 | |
| 923 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 924 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 925 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 926 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 927 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 928 | EXPECT_EQ(2u, dex_files.size()); |
| 929 | } |
| 930 | |
| 931 | TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) { |
| 932 | std::string error_msg; |
| 933 | std::string odex_file; |
| 934 | |
| 935 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
| 936 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 937 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 938 | |
| 939 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
| 940 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 941 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 942 | |
| 943 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
| 944 | "nopath.jar", kArm, &odex_file, &error_msg)); |
| 945 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
| 946 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
| 947 | } |
| 948 | |
| 949 | |
| 950 | // TODO: More Tests: |
| 951 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 952 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
| 953 | // * Test multidex files: |
| 954 | // - Multidex with only classes2.dex out of date should have status |
| 955 | // kOutOfDate |
| 956 | // * Test using secondary isa |
| 957 | // * Test with profiling info? |
| 958 | // * Test for status of oat while oat is being generated (how?) |
| 959 | // * Test case where 32 and 64 bit boot class paths differ, |
| 960 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 961 | // 64 bit boot class paths. |
| 962 | // * Test unexpected scenarios (?): |
| 963 | // - Dex is stripped, don't have odex. |
| 964 | // - Oat file corrupted after status check, before reload unexecutable |
| 965 | // because it's unrelocated and no dex2oat |
| 966 | |
| 967 | } // namespace art |