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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 28 | #include "art_field-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 29 | #include "class_linker-inl.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 30 | #include "common_runtime_test.h" |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 31 | #include "compiler_callbacks.h" |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 32 | #include "gc/space/image_space.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 33 | #include "mem_map.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 34 | #include "oat_file_manager.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 35 | #include "os.h" |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 36 | #include "scoped_thread_state_change.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 37 | #include "thread-inl.h" |
| 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
| 42 | class 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 Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 52 | // 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 Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 59 | |
| 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 Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 69 | ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg)) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 70 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 71 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 72 | << "Expected dex file to be at: " << GetDexSrc2(); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 73 | |
| 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 Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 78 | GetMultiDexSrc1().c_str(), &error_msg, &multi1)) << error_msg; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 79 | ASSERT_GT(multi1.size(), 1u); |
| 80 | |
| 81 | std::vector<std::unique_ptr<const DexFile>> multi2; |
| 82 | ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 83 | GetMultiDexSrc2().c_str(), &error_msg, &multi2)) << error_msg; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 84 | 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 Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
Richard Uhler | 892fc96 | 2015-03-10 16:57:05 +0000 | [diff] [blame] | 91 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 92 | |
| 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 Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 98 | callbacks_.reset(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | virtual void PreRuntimeCreate() { |
| 102 | UnreserveImageSpace(); |
| 103 | } |
| 104 | |
| 105 | virtual void PostRuntimeCreate() { |
| 106 | ReserveImageSpace(); |
| 107 | } |
| 108 | |
| 109 | virtual void TearDown() { |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 110 | 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 Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 115 | |
| 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 136 | CHECK(host_dir != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 137 | 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 Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 166 | // 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 Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 172 | 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 Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 182 | // Odex directory is the subdirectory in the scratch directory where odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 183 | // files should be located. |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 184 | std::string GetOdexDir() { |
| 185 | return odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 188 | // Generate a non-PIC odex file for the purposes of test. |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 189 | // The generated odex file will be un-relocated. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 190 | void GenerateOdexForTest(const std::string& dex_location, |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 191 | const std::string& odex_location) { |
| 192 | // To generate an un-relocated odex file, we first compile a relocated |
| 193 | // version of the file, then manually call patchoat to make it look as if |
| 194 | // it is unrelocated. |
| 195 | std::string relocated_odex_location = odex_location + ".relocated"; |
| 196 | std::vector<std::string> args; |
| 197 | args.push_back("--dex-file=" + dex_location); |
| 198 | args.push_back("--oat-file=" + relocated_odex_location); |
| 199 | args.push_back("--include-patch-information"); |
| 200 | |
| 201 | // We need to use the quick compiler to generate non-PIC code, because |
| 202 | // the optimizing compiler always generates PIC. |
| 203 | args.push_back("--compiler-backend=Quick"); |
| 204 | |
| 205 | std::string error_msg; |
| 206 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 207 | |
| 208 | // Use patchoat to unrelocate the relocated odex file. |
| 209 | Runtime* runtime = Runtime::Current(); |
| 210 | std::vector<std::string> argv; |
| 211 | argv.push_back(runtime->GetPatchoatExecutable()); |
| 212 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA))); |
| 213 | argv.push_back("--input-oat-file=" + relocated_odex_location); |
| 214 | argv.push_back("--output-oat-file=" + odex_location); |
| 215 | argv.push_back("--base-offset-delta=0x00008000"); |
| 216 | std::string command_line(Join(argv, ' ')); |
| 217 | ASSERT_TRUE(Exec(argv, &error_msg)) << error_msg; |
| 218 | |
| 219 | // Verify the odex file was generated as expected and really is |
| 220 | // unrelocated. |
| 221 | std::unique_ptr<OatFile> odex_file(OatFile::Open( |
| 222 | odex_location.c_str(), odex_location.c_str(), nullptr, nullptr, |
| 223 | false, dex_location.c_str(), &error_msg)); |
| 224 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 225 | |
| 226 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 227 | ASSERT_TRUE(image_space != nullptr); |
| 228 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 229 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
| 230 | EXPECT_FALSE(odex_file->IsPic()); |
| 231 | EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum()); |
| 232 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 233 | oat_header.GetImageFileLocationOatDataBegin()); |
| 234 | EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 235 | } |
| 236 | |
| 237 | void GeneratePicOdexForTest(const std::string& dex_location, |
| 238 | const std::string& odex_location) { |
| 239 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 240 | // relocated image file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 241 | std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp"; |
| 242 | setenv("ANDROID_DATA", android_data_tmp.c_str(), 1); |
| 243 | std::vector<std::string> args; |
| 244 | args.push_back("--dex-file=" + dex_location); |
| 245 | args.push_back("--oat-file=" + odex_location); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 246 | args.push_back("--compile-pic"); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 247 | args.push_back("--runtime-arg"); |
| 248 | args.push_back("-Xnorelocate"); |
| 249 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 250 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 251 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 252 | |
| 253 | // Verify the odex file was generated as expected. |
| 254 | std::unique_ptr<OatFile> odex_file(OatFile::Open( |
| 255 | odex_location.c_str(), odex_location.c_str(), nullptr, nullptr, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 256 | false, dex_location.c_str(), &error_msg)); |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 257 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 258 | EXPECT_TRUE(odex_file->IsPic()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | private: |
| 262 | // Reserve memory around where the image will be loaded so other memory |
| 263 | // won't conflict when it comes time to load the image. |
| 264 | // This can be called with an already loaded image to reserve the space |
| 265 | // around it. |
| 266 | void ReserveImageSpace() { |
| 267 | MemMap::Init(); |
| 268 | |
| 269 | // Ensure a chunk of memory is reserved for the image space. |
| 270 | uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA; |
| 271 | uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 272 | // Include the main space that has to come right after the |
| 273 | // image in case of the GSS collector. |
| 274 | + 384 * MB; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 275 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 276 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 277 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 278 | for (BacktraceMap::const_iterator it = map->begin(); |
| 279 | reservation_start < reservation_end && it != map->end(); ++it) { |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 280 | ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end)); |
| 281 | reservation_start = std::max(reservation_start, it->end); |
| 282 | } |
| 283 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 284 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 285 | |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 286 | // Reserve a chunk of memory for the image space in the given range. |
| 287 | // Only has effect for chunks with a positive number of bytes. |
| 288 | void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 289 | if (start < end) { |
| 290 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 291 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 292 | MemMap::MapAnonymous("image reservation", |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 293 | reinterpret_cast<uint8_t*>(start), end - start, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 294 | PROT_NONE, false, false, &error_msg))); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 295 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 296 | LOG(INFO) << "Reserved space for image " << |
| 297 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 298 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | // Unreserve any memory reserved by ReserveImageSpace. This should be called |
| 304 | // before the image is loaded. |
| 305 | void UnreserveImageSpace() { |
| 306 | image_reservation_.clear(); |
| 307 | } |
| 308 | |
| 309 | std::string scratch_dir_; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 310 | std::string odex_oat_dir_; |
| 311 | std::string odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 312 | std::vector<std::unique_ptr<MemMap>> image_reservation_; |
| 313 | }; |
| 314 | |
| 315 | class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest { |
| 316 | public: |
| 317 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
| 318 | OatFileAssistantTest::SetUpRuntimeOptions(options); |
| 319 | options->push_back(std::make_pair("-Xnodex2oat", nullptr)); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | // Generate an oat file for the purposes of test, as opposed to testing |
| 324 | // generation of oat files. |
| 325 | static void GenerateOatForTest(const char* dex_location) { |
| 326 | OatFileAssistant oat_file_assistant(dex_location, kRuntimeISA, false); |
| 327 | |
| 328 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 329 | ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Case: We have a DEX file, but no OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 333 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 334 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 335 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 336 | Copy(GetDexSrc1(), dex_location); |
| 337 | |
| 338 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 339 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 340 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 341 | |
| 342 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 343 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 344 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 345 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 346 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 347 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 348 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 349 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 350 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 351 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 352 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 353 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // Case: We have no DEX file and no OAT file. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 357 | // Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 358 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 359 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 360 | |
| 361 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 362 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 363 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 364 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 365 | |
| 366 | // Trying to make the oat file up to date should not fail or crash. |
| 367 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 368 | EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 369 | |
| 370 | // Trying to get the best oat file should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 371 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 372 | EXPECT_EQ(nullptr, oat_file.get()); |
| 373 | } |
| 374 | |
| 375 | // Case: We have a DEX file and up-to-date OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 376 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 377 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 378 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 379 | Copy(GetDexSrc1(), dex_location); |
| 380 | GenerateOatForTest(dex_location.c_str()); |
| 381 | |
| 382 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 383 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 384 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 385 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 386 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 387 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 388 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 389 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 390 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 391 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 392 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 393 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 394 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // 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] | 398 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 399 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 400 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 401 | Copy(GetMultiDexSrc1(), dex_location); |
| 402 | GenerateOatForTest(dex_location.c_str()); |
| 403 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 404 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 405 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 406 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 407 | |
| 408 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 409 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 410 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 411 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 412 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 413 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 414 | EXPECT_EQ(2u, dex_files.size()); |
| 415 | } |
| 416 | |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 417 | // Case: We have a MultiDEX file where the secondary dex file is out of date. |
| 418 | // Expect: The status is kDex2OatNeeded. |
| 419 | TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) { |
| 420 | std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar"; |
| 421 | |
| 422 | // Compile code for GetMultiDexSrc1. |
| 423 | Copy(GetMultiDexSrc1(), dex_location); |
| 424 | GenerateOatForTest(dex_location.c_str()); |
| 425 | |
| 426 | // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum |
| 427 | // is out of date. |
| 428 | Copy(GetMultiDexSrc2(), dex_location); |
| 429 | |
| 430 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 431 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 432 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 435 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 436 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 437 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 438 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 439 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 440 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 441 | |
| 442 | // Create the dex file |
| 443 | Copy(GetMultiDexSrc1(), dex_location); |
| 444 | |
| 445 | // Create the oat file with relative encoded dex location. |
| 446 | std::vector<std::string> args; |
| 447 | args.push_back("--dex-file=" + dex_location); |
| 448 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 449 | args.push_back("--oat-file=" + oat_location); |
| 450 | |
| 451 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 452 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 453 | |
| 454 | // Verify we can load both dex files. |
| 455 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 456 | oat_location.c_str(), |
| 457 | kRuntimeISA, true); |
| 458 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 459 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 460 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 461 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 462 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 463 | EXPECT_EQ(2u, dex_files.size()); |
| 464 | } |
| 465 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 466 | // Case: We have a DEX file and out-of-date OAT file. |
| 467 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 468 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 469 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 470 | |
| 471 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 472 | // different dex to make the oat out of date. |
| 473 | Copy(GetDexSrc1(), dex_location); |
| 474 | GenerateOatForTest(dex_location.c_str()); |
| 475 | Copy(GetDexSrc2(), dex_location); |
| 476 | |
| 477 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 478 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 479 | |
| 480 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 481 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 482 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 483 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 484 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 485 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 486 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 487 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // 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] | 491 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 492 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 493 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 494 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 495 | |
| 496 | // Create the dex and odex files |
| 497 | Copy(GetDexSrc1(), dex_location); |
| 498 | GenerateOdexForTest(dex_location, odex_location); |
| 499 | |
| 500 | // Verify the status. |
| 501 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 502 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 503 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 504 | |
| 505 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 506 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 507 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 508 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 509 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 510 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 511 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 512 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 513 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 514 | |
| 515 | // We should still be able to get the non-executable odex file to run from. |
| 516 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 517 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // 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] | 521 | // Expect: The status is kPatchOatNeeded |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 522 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 523 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 524 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 525 | |
| 526 | // Create the dex and odex files |
| 527 | Copy(GetDexSrc1(), dex_location); |
| 528 | GenerateOdexForTest(dex_location, odex_location); |
| 529 | |
| 530 | // Strip the dex file |
| 531 | Copy(GetStrippedDexSrc1(), dex_location); |
| 532 | |
| 533 | // Verify the status. |
| 534 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 535 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 536 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 537 | |
| 538 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 539 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 540 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 541 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 542 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 543 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 544 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 545 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 546 | |
| 547 | // Make the oat file up to date. |
| 548 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 549 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 550 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 551 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 552 | |
| 553 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 554 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 555 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 556 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 557 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 558 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 559 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 560 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 561 | |
| 562 | // Verify we can load the dex files from it. |
| 563 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 564 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 565 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 566 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 567 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 568 | EXPECT_EQ(1u, dex_files.size()); |
| 569 | } |
| 570 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 571 | // Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file. |
| 572 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 573 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 574 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 575 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 576 | |
| 577 | // Create the oat file from a different dex file so it looks out of date. |
| 578 | Copy(GetDexSrc2(), dex_location); |
| 579 | GenerateOatForTest(dex_location.c_str()); |
| 580 | |
| 581 | // Create the odex file |
| 582 | Copy(GetDexSrc1(), dex_location); |
| 583 | GenerateOdexForTest(dex_location, odex_location); |
| 584 | |
| 585 | // Strip the dex file. |
| 586 | Copy(GetStrippedDexSrc1(), dex_location); |
| 587 | |
| 588 | // Verify the status. |
| 589 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 590 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 591 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 592 | |
| 593 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 594 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 595 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 596 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 597 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 598 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 599 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 600 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 601 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 602 | |
| 603 | // Make the oat file up to date. |
| 604 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 605 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 606 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 607 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 608 | |
| 609 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 610 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 611 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 612 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 613 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 614 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 615 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 616 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 617 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 618 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 619 | |
| 620 | // Verify we can load the dex files from it. |
| 621 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 622 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 623 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 624 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 625 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 626 | EXPECT_EQ(1u, dex_files.size()); |
| 627 | } |
| 628 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 629 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 630 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 631 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 632 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 633 | |
| 634 | Copy(GetStrippedDexSrc1(), dex_location); |
| 635 | |
| 636 | // Verify the status. |
| 637 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 638 | |
| 639 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 640 | |
| 641 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 642 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 643 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 644 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 645 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 646 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 647 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 648 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 649 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 650 | |
| 651 | // Make the oat file up to date. This should have no effect. |
| 652 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 653 | EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 654 | |
| 655 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 656 | |
| 657 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 658 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 659 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 660 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 661 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 662 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 663 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 664 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 665 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 666 | } |
| 667 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 668 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 669 | // relocation. |
| 670 | // Expect: The status is kSelfPatchOatNeeded. |
| 671 | TEST_F(OatFileAssistantTest, SelfRelocation) { |
| 672 | std::string dex_location = GetScratchDir() + "/SelfRelocation.jar"; |
| 673 | std::string oat_location = GetOdexDir() + "/SelfRelocation.oat"; |
| 674 | |
| 675 | // Create the dex and odex files |
| 676 | Copy(GetDexSrc1(), dex_location); |
| 677 | GenerateOdexForTest(dex_location, oat_location); |
| 678 | |
| 679 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 680 | oat_location.c_str(), kRuntimeISA, true); |
| 681 | |
| 682 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 683 | |
| 684 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 685 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 686 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 687 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 688 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 689 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 690 | EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation()); |
| 691 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 692 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 693 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 694 | |
| 695 | // Make the oat file up to date. |
| 696 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 697 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 698 | |
| 699 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 700 | |
| 701 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 702 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 703 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 704 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 705 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 706 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 707 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 708 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 709 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 710 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 711 | |
| 712 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 713 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 714 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 715 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 716 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 717 | EXPECT_EQ(1u, dex_files.size()); |
| 718 | } |
| 719 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 720 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 721 | // OAT files both have patch delta of 0. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 722 | // Expect: It shouldn't crash, and status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 723 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 724 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 725 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 726 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 727 | |
| 728 | // Create the dex and odex files |
| 729 | Copy(GetDexSrc1(), dex_location); |
| 730 | GenerateOdexForTest(dex_location, odex_location); |
| 731 | |
| 732 | // Create the oat file by copying the odex so they are located in the same |
| 733 | // place in memory. |
| 734 | Copy(odex_location, oat_location); |
| 735 | |
| 736 | // Verify things don't go bad. |
| 737 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 738 | oat_location.c_str(), kRuntimeISA, true); |
| 739 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 740 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 741 | |
| 742 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 743 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 744 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 745 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 746 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 747 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 748 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 749 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 750 | |
| 751 | // Things aren't relocated, so it should fall back to interpreted. |
| 752 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 753 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 754 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 755 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 756 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 757 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 758 | EXPECT_EQ(1u, dex_files.size()); |
| 759 | } |
| 760 | |
| 761 | // Case: We have a DEX file and a PIC ODEX file, but no OAT file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 762 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 763 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 764 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 765 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 766 | |
| 767 | // Create the dex and odex files |
| 768 | Copy(GetDexSrc1(), dex_location); |
| 769 | GeneratePicOdexForTest(dex_location, odex_location); |
| 770 | |
| 771 | // Verify the status. |
| 772 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 773 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 774 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 775 | |
| 776 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 777 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 778 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 779 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 780 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 781 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 782 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 783 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 787 | // Expect: We should load an executable dex file. |
| 788 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 789 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 790 | |
| 791 | Copy(GetDexSrc1(), dex_location); |
| 792 | GenerateOatForTest(dex_location.c_str()); |
| 793 | |
| 794 | // Load the oat using an oat file assistant. |
| 795 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 796 | |
| 797 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 798 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 799 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 800 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 801 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 802 | EXPECT_EQ(1u, dex_files.size()); |
| 803 | } |
| 804 | |
| 805 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 806 | // Expect: Loading non-executable should load the oat non-executable. |
| 807 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 808 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 809 | |
| 810 | Copy(GetDexSrc1(), dex_location); |
| 811 | GenerateOatForTest(dex_location.c_str()); |
| 812 | |
| 813 | // Load the oat using an oat file assistant. |
| 814 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 815 | |
| 816 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 817 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 818 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 819 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 820 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 821 | EXPECT_EQ(1u, dex_files.size()); |
| 822 | } |
| 823 | |
| 824 | // Case: We have a DEX file. |
| 825 | // Expect: We should load an executable dex file from an alternative oat |
| 826 | // location. |
| 827 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 828 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 829 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 830 | |
| 831 | Copy(GetDexSrc1(), dex_location); |
| 832 | |
| 833 | OatFileAssistant oat_file_assistant( |
| 834 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, true); |
| 835 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 836 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 837 | |
| 838 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 839 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 840 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 841 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 842 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 843 | EXPECT_EQ(1u, dex_files.size()); |
| 844 | |
| 845 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 846 | |
| 847 | // Verify it didn't create an oat in the default location. |
| 848 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false); |
| 849 | EXPECT_FALSE(ofm.OatFileExists()); |
| 850 | } |
| 851 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 852 | // Turn an absolute path into a path relative to the current working |
| 853 | // directory. |
| 854 | static std::string MakePathRelative(std::string target) { |
| 855 | char buf[MAXPATHLEN]; |
| 856 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 857 | |
| 858 | // Split the target and cwd paths into components. |
| 859 | std::vector<std::string> target_path; |
| 860 | std::vector<std::string> cwd_path; |
| 861 | Split(target, '/', &target_path); |
| 862 | Split(cwd, '/', &cwd_path); |
| 863 | |
| 864 | // Reverse the path components, so we can use pop_back(). |
| 865 | std::reverse(target_path.begin(), target_path.end()); |
| 866 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 867 | |
| 868 | // Drop the common prefix of the paths. Because we reversed the path |
| 869 | // components, this becomes the common suffix of target_path and cwd_path. |
| 870 | while (!target_path.empty() && !cwd_path.empty() |
| 871 | && target_path.back() == cwd_path.back()) { |
| 872 | target_path.pop_back(); |
| 873 | cwd_path.pop_back(); |
| 874 | } |
| 875 | |
| 876 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 877 | // of the target path. Because we reversed the path components, we add to |
| 878 | // the end of target_path. |
| 879 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 880 | target_path.push_back(".."); |
| 881 | } |
| 882 | |
| 883 | // Reverse again to get the right path order, and join to get the result. |
| 884 | std::reverse(target_path.begin(), target_path.end()); |
| 885 | return Join(target_path, '/'); |
| 886 | } |
| 887 | |
| 888 | // Case: Non-absolute path to Dex location. |
| 889 | // Expect: Not sure, but it shouldn't crash. |
| 890 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 891 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 892 | Copy(GetDexSrc1(), abs_dex_location); |
| 893 | |
| 894 | std::string dex_location = MakePathRelative(abs_dex_location); |
| 895 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 896 | |
| 897 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 898 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 899 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 900 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 901 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 902 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 903 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 904 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 905 | } |
| 906 | |
| 907 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 908 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 909 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 910 | std::string dex_location = "/xx"; |
| 911 | |
| 912 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 913 | |
| 914 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 915 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 916 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 917 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 918 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 919 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 920 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 921 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 922 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 923 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 924 | // Trying to make it up to date should have no effect. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 925 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 926 | EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 927 | EXPECT_TRUE(error_msg.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 931 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 932 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 933 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 934 | Copy(GetDexSrc1(), dex_location); |
| 935 | |
| 936 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false); |
| 937 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 938 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 939 | |
| 940 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 941 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 942 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 943 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 944 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 945 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 946 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 947 | } |
| 948 | |
| 949 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 950 | class RaceGenerateTask : public Task { |
| 951 | public: |
| 952 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
| 953 | : dex_location_(dex_location), oat_location_(oat_location), |
| 954 | loaded_oat_file_(nullptr) |
| 955 | {} |
| 956 | |
| 957 | void Run(Thread* self) { |
| 958 | UNUSED(self); |
| 959 | |
| 960 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 961 | // we can verify only one oat file was loaded for the dex location. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 962 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 963 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame^] | 964 | const OatFile* oat_file = nullptr; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 965 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 966 | dex_location_.c_str(), |
| 967 | oat_location_.c_str(), |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame^] | 968 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 969 | &error_msgs); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 970 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 971 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 972 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame^] | 973 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | const OatFile* GetLoadedOatFile() const { |
| 977 | return loaded_oat_file_; |
| 978 | } |
| 979 | |
| 980 | private: |
| 981 | std::string dex_location_; |
| 982 | std::string oat_location_; |
| 983 | const OatFile* loaded_oat_file_; |
| 984 | }; |
| 985 | |
| 986 | // Test the case where multiple processes race to generate an oat file. |
| 987 | // This simulates multiple processes using multiple threads. |
| 988 | // |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 989 | // We want unique Oat files to be loaded even when there is a race to load. |
| 990 | // TODO: The test case no longer tests locking the way it was intended since we now get multiple |
| 991 | // copies of the same Oat files mapped at different locations. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 992 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 993 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 994 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 995 | |
| 996 | // We use the lib core dex file, because it's large, and hopefully should |
| 997 | // take a while to generate. |
| 998 | Copy(GetLibCoreDexFileName(), dex_location); |
| 999 | |
| 1000 | const int kNumThreads = 32; |
| 1001 | Thread* self = Thread::Current(); |
| 1002 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 1003 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 1004 | for (int i = 0; i < kNumThreads; i++) { |
| 1005 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 1006 | thread_pool.AddTask(self, task.get()); |
| 1007 | tasks.push_back(std::move(task)); |
| 1008 | } |
| 1009 | thread_pool.StartWorkers(self); |
| 1010 | thread_pool.Wait(self, true, false); |
| 1011 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1012 | // Verify every task got a unique oat file. |
| 1013 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1014 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1015 | const OatFile* oat_file = task->GetLoadedOatFile(); |
| 1016 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 1017 | oat_files.insert(oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 1022 | // disabled. |
| 1023 | // Expect: We should load the odex file non-executable. |
| 1024 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 1025 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1026 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1027 | |
| 1028 | // Create the dex and odex files |
| 1029 | Copy(GetDexSrc1(), dex_location); |
| 1030 | GenerateOdexForTest(dex_location, odex_location); |
| 1031 | |
| 1032 | // Load the oat using an executable oat file assistant. |
| 1033 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 1034 | |
| 1035 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1036 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1037 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1038 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1039 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1040 | EXPECT_EQ(1u, dex_files.size()); |
| 1041 | } |
| 1042 | |
| 1043 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 1044 | // disabled. |
| 1045 | // Expect: We should load the odex file non-executable. |
| 1046 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 1047 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1048 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1049 | |
| 1050 | // Create the dex and odex files |
| 1051 | Copy(GetMultiDexSrc1(), dex_location); |
| 1052 | GenerateOdexForTest(dex_location, odex_location); |
| 1053 | |
| 1054 | // Load the oat using an executable oat file assistant. |
| 1055 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true); |
| 1056 | |
| 1057 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1058 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1059 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1060 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1061 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1062 | EXPECT_EQ(2u, dex_files.size()); |
| 1063 | } |
| 1064 | |
| 1065 | TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) { |
| 1066 | std::string error_msg; |
| 1067 | std::string odex_file; |
| 1068 | |
| 1069 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1070 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1071 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1072 | |
| 1073 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1074 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1075 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1076 | |
| 1077 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1078 | "nopath.jar", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1079 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1080 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1083 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1084 | // match the OatFileAssistant::DexOptStatus values. |
| 1085 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
| 1086 | ScopedObjectAccess soa(Thread::Current()); |
| 1087 | StackHandleScope<1> hs(soa.Self()); |
| 1088 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1089 | Handle<mirror::Class> dexfile( |
| 1090 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
| 1091 | ASSERT_FALSE(dexfile.Get() == nullptr); |
| 1092 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1093 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1094 | ArtField* no_dexopt_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1095 | soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I"); |
| 1096 | ASSERT_FALSE(no_dexopt_needed == nullptr); |
| 1097 | EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1098 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get())); |
| 1099 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1100 | ArtField* dex2oat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1101 | soa.Self(), dexfile, "DEX2OAT_NEEDED", "I"); |
| 1102 | ASSERT_FALSE(dex2oat_needed == nullptr); |
| 1103 | EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1104 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get())); |
| 1105 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1106 | ArtField* patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1107 | soa.Self(), dexfile, "PATCHOAT_NEEDED", "I"); |
| 1108 | ASSERT_FALSE(patchoat_needed == nullptr); |
| 1109 | EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1110 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get())); |
| 1111 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1112 | ArtField* self_patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1113 | soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I"); |
| 1114 | ASSERT_FALSE(self_patchoat_needed == nullptr); |
| 1115 | EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1116 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get())); |
| 1117 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1118 | |
| 1119 | // TODO: More Tests: |
| 1120 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1121 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1122 | // * Test using secondary isa |
| 1123 | // * Test with profiling info? |
| 1124 | // * Test for status of oat while oat is being generated (how?) |
| 1125 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1126 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1127 | // 64 bit boot class paths. |
| 1128 | // * Test unexpected scenarios (?): |
| 1129 | // - Dex is stripped, don't have odex. |
| 1130 | // - Oat file corrupted after status check, before reload unexecutable |
| 1131 | // because it's unrelocated and no dex2oat |
| 1132 | |
| 1133 | } // namespace art |