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. |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 221 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 222 | odex_location.c_str(), |
| 223 | nullptr, |
| 224 | nullptr, |
| 225 | false, |
| 226 | /*low_4gb*/false, |
| 227 | dex_location.c_str(), |
| 228 | &error_msg)); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 229 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 230 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 231 | const std::vector<gc::space::ImageSpace*> image_spaces = |
| 232 | runtime->GetHeap()->GetBootImageSpaces(); |
| 233 | ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr); |
| 234 | const ImageHeader& image_header = image_spaces[0]->GetImageHeader(); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 235 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
| 236 | EXPECT_FALSE(odex_file->IsPic()); |
| 237 | EXPECT_EQ(image_header.GetOatChecksum(), oat_header.GetImageFileLocationOatChecksum()); |
| 238 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 239 | oat_header.GetImageFileLocationOatDataBegin()); |
| 240 | EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 241 | } |
| 242 | |
| 243 | void GeneratePicOdexForTest(const std::string& dex_location, |
| 244 | const std::string& odex_location) { |
| 245 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 246 | // relocated image file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 247 | std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp"; |
| 248 | setenv("ANDROID_DATA", android_data_tmp.c_str(), 1); |
| 249 | std::vector<std::string> args; |
| 250 | args.push_back("--dex-file=" + dex_location); |
| 251 | args.push_back("--oat-file=" + odex_location); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 252 | args.push_back("--compile-pic"); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 253 | args.push_back("--runtime-arg"); |
| 254 | args.push_back("-Xnorelocate"); |
| 255 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 256 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 257 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 258 | |
| 259 | // Verify the odex file was generated as expected. |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 260 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 261 | odex_location.c_str(), |
| 262 | nullptr, |
| 263 | nullptr, |
| 264 | false, |
| 265 | /*low_4gb*/false, |
| 266 | dex_location.c_str(), |
| 267 | &error_msg)); |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 268 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 269 | EXPECT_TRUE(odex_file->IsPic()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 270 | } |
| 271 | |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 272 | void GenerateExtractOnlyOdexForTest(const std::string& dex_location, |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 273 | const std::string& odex_location) { |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 274 | std::vector<std::string> args; |
| 275 | args.push_back("--dex-file=" + dex_location); |
| 276 | args.push_back("--oat-file=" + odex_location); |
| 277 | args.push_back("--compiler-filter=verify-at-runtime"); |
| 278 | std::string error_msg; |
| 279 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 280 | |
| 281 | // Verify the odex file was generated as expected. |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 282 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 283 | odex_location.c_str(), |
| 284 | nullptr, |
| 285 | nullptr, |
| 286 | false, |
| 287 | /*low_4gb*/false, |
| 288 | dex_location.c_str(), |
| 289 | &error_msg)); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 290 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 291 | EXPECT_TRUE(odex_file->IsExtractOnly()); |
| 292 | EXPECT_EQ(odex_file->GetOatHeader().GetImageFileLocationOatChecksum(), 0u); |
| 293 | EXPECT_EQ(odex_file->GetOatHeader().GetImageFileLocationOatDataBegin(), 0u); |
| 294 | EXPECT_EQ(odex_file->GetOatHeader().GetImagePatchDelta(), 0); |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void GenerateProfileGuideOdexForTest(const std::string& dex_location, |
| 298 | const std::string& odex_location) { |
| 299 | std::vector<std::string> args; |
| 300 | args.push_back("--dex-file=" + dex_location); |
| 301 | args.push_back("--oat-file=" + odex_location); |
| 302 | ScratchFile profile_file; |
| 303 | args.push_back("--profile-file=" + profile_file.GetFilename()); |
| 304 | std::string error_msg; |
| 305 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 306 | |
| 307 | // Verify the odex file was generated as expected. |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 308 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 309 | odex_location.c_str(), |
| 310 | nullptr, |
| 311 | nullptr, |
| 312 | false, |
| 313 | /*low_4gb*/false, |
| 314 | dex_location.c_str(), |
| 315 | &error_msg)); |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 316 | printf("error %s", error_msg.c_str()); |
| 317 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 318 | EXPECT_TRUE(odex_file->IsProfileGuideCompiled()); |
| 319 | } |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 320 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 321 | private: |
| 322 | // Reserve memory around where the image will be loaded so other memory |
| 323 | // won't conflict when it comes time to load the image. |
| 324 | // This can be called with an already loaded image to reserve the space |
| 325 | // around it. |
| 326 | void ReserveImageSpace() { |
| 327 | MemMap::Init(); |
| 328 | |
| 329 | // Ensure a chunk of memory is reserved for the image space. |
| 330 | uintptr_t reservation_start = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MIN_DELTA; |
| 331 | uintptr_t reservation_end = ART_BASE_ADDRESS + ART_BASE_ADDRESS_MAX_DELTA |
Hiroshi Yamauchi | 3dbf234 | 2015-03-17 16:01:11 -0700 | [diff] [blame] | 332 | // Include the main space that has to come right after the |
| 333 | // image in case of the GSS collector. |
| 334 | + 384 * MB; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 335 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 336 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 337 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 338 | for (BacktraceMap::const_iterator it = map->begin(); |
| 339 | reservation_start < reservation_end && it != map->end(); ++it) { |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 340 | ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end)); |
| 341 | reservation_start = std::max(reservation_start, it->end); |
| 342 | } |
| 343 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 344 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 345 | |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 346 | // Reserve a chunk of memory for the image space in the given range. |
| 347 | // Only has effect for chunks with a positive number of bytes. |
| 348 | void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 349 | if (start < end) { |
| 350 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 351 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 352 | MemMap::MapAnonymous("image reservation", |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 353 | reinterpret_cast<uint8_t*>(start), end - start, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 354 | PROT_NONE, false, false, &error_msg))); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 355 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 356 | LOG(INFO) << "Reserved space for image " << |
| 357 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 358 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | |
| 363 | // Unreserve any memory reserved by ReserveImageSpace. This should be called |
| 364 | // before the image is loaded. |
| 365 | void UnreserveImageSpace() { |
| 366 | image_reservation_.clear(); |
| 367 | } |
| 368 | |
| 369 | std::string scratch_dir_; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 370 | std::string odex_oat_dir_; |
| 371 | std::string odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 372 | std::vector<std::unique_ptr<MemMap>> image_reservation_; |
| 373 | }; |
| 374 | |
| 375 | class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest { |
| 376 | public: |
| 377 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
| 378 | OatFileAssistantTest::SetUpRuntimeOptions(options); |
| 379 | options->push_back(std::make_pair("-Xnodex2oat", nullptr)); |
| 380 | } |
| 381 | }; |
| 382 | |
| 383 | // Generate an oat file for the purposes of test, as opposed to testing |
| 384 | // generation of oat files. |
| 385 | static void GenerateOatForTest(const char* dex_location) { |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 386 | OatFileAssistant oat_file_assistant(dex_location, |
| 387 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 388 | |
| 389 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 390 | ASSERT_TRUE(oat_file_assistant.GenerateOatFile(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Case: We have a DEX file, but no OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 394 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 395 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 396 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 397 | Copy(GetDexSrc1(), dex_location); |
| 398 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 399 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 400 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 401 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 402 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 403 | |
| 404 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 405 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 406 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 407 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 408 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 409 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 410 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 411 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 412 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 413 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 414 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 415 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Case: We have no DEX file and no OAT file. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 419 | // Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 420 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 421 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 422 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 423 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 424 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 425 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 426 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 427 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 428 | |
| 429 | // Trying to make the oat file up to date should not fail or crash. |
| 430 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 431 | EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 432 | |
| 433 | // Trying to get the best oat file should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 434 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 435 | EXPECT_EQ(nullptr, oat_file.get()); |
| 436 | } |
| 437 | |
| 438 | // 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] | 439 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 440 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 441 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 442 | Copy(GetDexSrc1(), dex_location); |
| 443 | GenerateOatForTest(dex_location.c_str()); |
| 444 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 445 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 446 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 447 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 448 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 449 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 450 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 451 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 452 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 453 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 454 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 455 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 456 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 457 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 458 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // 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] | 462 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 463 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 464 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 465 | Copy(GetMultiDexSrc1(), dex_location); |
| 466 | GenerateOatForTest(dex_location.c_str()); |
| 467 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 468 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 469 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 470 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 471 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 472 | |
| 473 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 474 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 475 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 476 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 477 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 478 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 479 | EXPECT_EQ(2u, dex_files.size()); |
| 480 | } |
| 481 | |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 482 | // Case: We have a MultiDEX file where the secondary dex file is out of date. |
| 483 | // Expect: The status is kDex2OatNeeded. |
| 484 | TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) { |
| 485 | std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar"; |
| 486 | |
| 487 | // Compile code for GetMultiDexSrc1. |
| 488 | Copy(GetMultiDexSrc1(), dex_location); |
| 489 | GenerateOatForTest(dex_location.c_str()); |
| 490 | |
| 491 | // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum |
| 492 | // is out of date. |
| 493 | Copy(GetMultiDexSrc2(), dex_location); |
| 494 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 495 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 496 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 497 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 498 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 501 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 502 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 503 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 504 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 505 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 506 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 507 | |
| 508 | // Create the dex file |
| 509 | Copy(GetMultiDexSrc1(), dex_location); |
| 510 | |
| 511 | // Create the oat file with relative encoded dex location. |
| 512 | std::vector<std::string> args; |
| 513 | args.push_back("--dex-file=" + dex_location); |
| 514 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 515 | args.push_back("--oat-file=" + oat_location); |
| 516 | |
| 517 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 518 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 519 | |
| 520 | // Verify we can load both dex files. |
| 521 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 522 | oat_location.c_str(), |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 523 | OatFileAssistant::kFullCompilation, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 524 | kRuntimeISA, true); |
| 525 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 526 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 527 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 528 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 529 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 530 | EXPECT_EQ(2u, dex_files.size()); |
| 531 | } |
| 532 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 533 | // Case: We have a DEX file and out-of-date OAT file. |
| 534 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 535 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 536 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 537 | |
| 538 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 539 | // different dex to make the oat out of date. |
| 540 | Copy(GetDexSrc1(), dex_location); |
| 541 | GenerateOatForTest(dex_location.c_str()); |
| 542 | Copy(GetDexSrc2(), dex_location); |
| 543 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 544 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 545 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 546 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 547 | |
| 548 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 549 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 550 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 551 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 552 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 553 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 554 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 555 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // 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] | 559 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 560 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 561 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 562 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 563 | |
| 564 | // Create the dex and odex files |
| 565 | Copy(GetDexSrc1(), dex_location); |
| 566 | GenerateOdexForTest(dex_location, odex_location); |
| 567 | |
| 568 | // Verify the status. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 569 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 570 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 571 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 572 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 573 | |
| 574 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 575 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 576 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 577 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 578 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 579 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 580 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 581 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 582 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 583 | |
| 584 | // We should still be able to get the non-executable odex file to run from. |
| 585 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 586 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // 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] | 590 | // Expect: The status is kPatchOatNeeded |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 591 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 592 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 593 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 594 | |
| 595 | // Create the dex and odex files |
| 596 | Copy(GetDexSrc1(), dex_location); |
| 597 | GenerateOdexForTest(dex_location, odex_location); |
| 598 | |
| 599 | // Strip the dex file |
| 600 | Copy(GetStrippedDexSrc1(), dex_location); |
| 601 | |
| 602 | // Verify the status. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 603 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 604 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 605 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 606 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 607 | |
| 608 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 609 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 610 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 611 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 612 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 613 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 614 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 615 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 616 | |
| 617 | // Make the oat file up to date. |
| 618 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 619 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 620 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 621 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 622 | |
| 623 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 624 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 625 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 626 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 627 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 628 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 629 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 630 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 631 | |
| 632 | // Verify we can load the dex files from it. |
| 633 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 634 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 635 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 636 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 637 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 638 | EXPECT_EQ(1u, dex_files.size()); |
| 639 | } |
| 640 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 641 | // Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file. |
| 642 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 643 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 644 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 645 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 646 | |
| 647 | // Create the oat file from a different dex file so it looks out of date. |
| 648 | Copy(GetDexSrc2(), dex_location); |
| 649 | GenerateOatForTest(dex_location.c_str()); |
| 650 | |
| 651 | // Create the odex file |
| 652 | Copy(GetDexSrc1(), dex_location); |
| 653 | GenerateOdexForTest(dex_location, odex_location); |
| 654 | |
| 655 | // Strip the dex file. |
| 656 | Copy(GetStrippedDexSrc1(), dex_location); |
| 657 | |
| 658 | // Verify the status. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 659 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 660 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 661 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 662 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 663 | |
| 664 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 665 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 666 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 667 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 668 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 669 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 670 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 671 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 672 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 673 | |
| 674 | // Make the oat file up to date. |
| 675 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 676 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 677 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 678 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 679 | |
| 680 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 681 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 682 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 683 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 684 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 685 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 686 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 687 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 688 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 689 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 690 | |
| 691 | // Verify we can load the dex files from it. |
| 692 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 693 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 694 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 695 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 696 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 697 | EXPECT_EQ(1u, dex_files.size()); |
| 698 | } |
| 699 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 700 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 701 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 702 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 703 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 704 | |
| 705 | Copy(GetStrippedDexSrc1(), dex_location); |
| 706 | |
| 707 | // Verify the status. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 708 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 709 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 710 | |
| 711 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 712 | |
| 713 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 714 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 715 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 716 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 717 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 718 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 719 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 720 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 721 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 722 | |
| 723 | // Make the oat file up to date. This should have no effect. |
| 724 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 725 | EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 726 | |
| 727 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 728 | |
| 729 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 730 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 731 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 732 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 733 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 734 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 735 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 736 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 737 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 738 | } |
| 739 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 740 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 741 | // relocation. |
| 742 | // Expect: The status is kSelfPatchOatNeeded. |
| 743 | TEST_F(OatFileAssistantTest, SelfRelocation) { |
| 744 | std::string dex_location = GetScratchDir() + "/SelfRelocation.jar"; |
| 745 | std::string oat_location = GetOdexDir() + "/SelfRelocation.oat"; |
| 746 | |
| 747 | // Create the dex and odex files |
| 748 | Copy(GetDexSrc1(), dex_location); |
| 749 | GenerateOdexForTest(dex_location, oat_location); |
| 750 | |
| 751 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 752 | oat_location.c_str(), OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 753 | |
| 754 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 755 | |
| 756 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 757 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 758 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 759 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 760 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 761 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 762 | EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation()); |
| 763 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 764 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 765 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 766 | |
| 767 | // Make the oat file up to date. |
| 768 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 769 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 770 | |
| 771 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 772 | |
| 773 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 774 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 775 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 776 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 777 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 778 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 779 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 780 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 781 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 782 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 783 | |
| 784 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 785 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 786 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 787 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 788 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 789 | EXPECT_EQ(1u, dex_files.size()); |
| 790 | } |
| 791 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 792 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 793 | // OAT files both have patch delta of 0. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 794 | // Expect: It shouldn't crash, and status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 795 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 796 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 797 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 798 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 799 | |
| 800 | // Create the dex and odex files |
| 801 | Copy(GetDexSrc1(), dex_location); |
| 802 | GenerateOdexForTest(dex_location, odex_location); |
| 803 | |
| 804 | // Create the oat file by copying the odex so they are located in the same |
| 805 | // place in memory. |
| 806 | Copy(odex_location, oat_location); |
| 807 | |
| 808 | // Verify things don't go bad. |
| 809 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 810 | oat_location.c_str(), OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 811 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 812 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, 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_TRUE(oat_file_assistant.OdexFileExists()); |
| 816 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 817 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 818 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 819 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 820 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 821 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 822 | |
| 823 | // Things aren't relocated, so it should fall back to interpreted. |
| 824 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 825 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 826 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 827 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 828 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 829 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 830 | EXPECT_EQ(1u, dex_files.size()); |
| 831 | } |
| 832 | |
| 833 | // 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] | 834 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 835 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 836 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 837 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 838 | |
| 839 | // Create the dex and odex files |
| 840 | Copy(GetDexSrc1(), dex_location); |
| 841 | GeneratePicOdexForTest(dex_location, odex_location); |
| 842 | |
| 843 | // Verify the status. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 844 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 845 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 846 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 847 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 848 | |
| 849 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 850 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 851 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 852 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 853 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 854 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 855 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 856 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 857 | } |
| 858 | |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 859 | // Case: We have a DEX file and a ExtractOnly ODEX file, but no OAT file. |
| 860 | // Expect: The status is kNoDexOptNeeded, because ExtractOnly contains no code. |
| 861 | TEST_F(OatFileAssistantTest, DexExtractOnlyOdexNoOat) { |
| 862 | std::string dex_location = GetScratchDir() + "/DexExtractOnlyOdexNoOat.jar"; |
| 863 | std::string odex_location = GetOdexDir() + "/DexExtractOnlyOdexNoOat.odex"; |
| 864 | |
| 865 | // Create the dex and odex files |
| 866 | Copy(GetDexSrc1(), dex_location); |
| 867 | GenerateExtractOnlyOdexForTest(dex_location, odex_location); |
| 868 | |
| 869 | // Verify the status. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 870 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 871 | OatFileAssistant::kFullCompilation | OatFileAssistant::kExtractOnly, |
| 872 | kRuntimeISA, false); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 873 | |
| 874 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
| 875 | |
| 876 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 877 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 878 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 879 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 880 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 881 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 882 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 883 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 884 | } |
| 885 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 886 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 887 | // Expect: We should load an executable dex file. |
| 888 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 889 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 890 | |
| 891 | Copy(GetDexSrc1(), dex_location); |
| 892 | GenerateOatForTest(dex_location.c_str()); |
| 893 | |
| 894 | // Load the oat using an oat file assistant. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 895 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 896 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 897 | |
| 898 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 899 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 900 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 901 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 902 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 903 | EXPECT_EQ(1u, dex_files.size()); |
| 904 | } |
| 905 | |
| 906 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 907 | // Expect: Loading non-executable should load the oat non-executable. |
| 908 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 909 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 910 | |
| 911 | Copy(GetDexSrc1(), dex_location); |
| 912 | GenerateOatForTest(dex_location.c_str()); |
| 913 | |
| 914 | // Load the oat using an oat file assistant. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 915 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 916 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 917 | |
| 918 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 919 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 920 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 921 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 922 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 923 | EXPECT_EQ(1u, dex_files.size()); |
| 924 | } |
| 925 | |
| 926 | // Case: We have a DEX file. |
| 927 | // Expect: We should load an executable dex file from an alternative oat |
| 928 | // location. |
| 929 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 930 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 931 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 932 | |
| 933 | Copy(GetDexSrc1(), dex_location); |
| 934 | |
| 935 | OatFileAssistant oat_file_assistant( |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 936 | dex_location.c_str(), oat_location.c_str(), |
| 937 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 938 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 939 | ASSERT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 940 | |
| 941 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 942 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 943 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 944 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 945 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 946 | EXPECT_EQ(1u, dex_files.size()); |
| 947 | |
| 948 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 949 | |
| 950 | // Verify it didn't create an oat in the default location. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 951 | OatFileAssistant ofm(dex_location.c_str(), |
| 952 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 953 | EXPECT_FALSE(ofm.OatFileExists()); |
| 954 | } |
| 955 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 956 | // Case: We have a DEX file but can't write the oat file. |
| 957 | // Expect: We should fail to make the oat file up to date. |
| 958 | TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) { |
| 959 | std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar"; |
| 960 | |
| 961 | // Make the oat location unwritable by inserting some non-existent |
| 962 | // intermediate directories. |
| 963 | std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat"; |
| 964 | |
| 965 | Copy(GetDexSrc1(), dex_location); |
| 966 | |
| 967 | OatFileAssistant oat_file_assistant( |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 968 | dex_location.c_str(), oat_location.c_str(), |
| 969 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 970 | std::string error_msg; |
| 971 | ASSERT_FALSE(oat_file_assistant.MakeUpToDate(&error_msg)); |
| 972 | |
| 973 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 974 | ASSERT_TRUE(oat_file.get() == nullptr); |
| 975 | } |
| 976 | |
| 977 | // Case: We don't have a DEX file and can't write the oat file. |
| 978 | // Expect: We should fail to generate the oat file without crashing. |
| 979 | TEST_F(OatFileAssistantTest, GenNoDex) { |
| 980 | std::string dex_location = GetScratchDir() + "/GenNoDex.jar"; |
| 981 | std::string oat_location = GetScratchDir() + "/GenNoDex.oat"; |
| 982 | |
| 983 | OatFileAssistant oat_file_assistant( |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 984 | dex_location.c_str(), oat_location.c_str(), |
| 985 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 986 | std::string error_msg; |
| 987 | ASSERT_FALSE(oat_file_assistant.GenerateOatFile(&error_msg)); |
| 988 | } |
| 989 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 990 | // Turn an absolute path into a path relative to the current working |
| 991 | // directory. |
| 992 | static std::string MakePathRelative(std::string target) { |
| 993 | char buf[MAXPATHLEN]; |
| 994 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 995 | |
| 996 | // Split the target and cwd paths into components. |
| 997 | std::vector<std::string> target_path; |
| 998 | std::vector<std::string> cwd_path; |
| 999 | Split(target, '/', &target_path); |
| 1000 | Split(cwd, '/', &cwd_path); |
| 1001 | |
| 1002 | // Reverse the path components, so we can use pop_back(). |
| 1003 | std::reverse(target_path.begin(), target_path.end()); |
| 1004 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 1005 | |
| 1006 | // Drop the common prefix of the paths. Because we reversed the path |
| 1007 | // components, this becomes the common suffix of target_path and cwd_path. |
| 1008 | while (!target_path.empty() && !cwd_path.empty() |
| 1009 | && target_path.back() == cwd_path.back()) { |
| 1010 | target_path.pop_back(); |
| 1011 | cwd_path.pop_back(); |
| 1012 | } |
| 1013 | |
| 1014 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 1015 | // of the target path. Because we reversed the path components, we add to |
| 1016 | // the end of target_path. |
| 1017 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 1018 | target_path.push_back(".."); |
| 1019 | } |
| 1020 | |
| 1021 | // Reverse again to get the right path order, and join to get the result. |
| 1022 | std::reverse(target_path.begin(), target_path.end()); |
| 1023 | return Join(target_path, '/'); |
| 1024 | } |
| 1025 | |
| 1026 | // Case: Non-absolute path to Dex location. |
| 1027 | // Expect: Not sure, but it shouldn't crash. |
| 1028 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 1029 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 1030 | Copy(GetDexSrc1(), abs_dex_location); |
| 1031 | |
| 1032 | std::string dex_location = MakePathRelative(abs_dex_location); |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1033 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 1034 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1035 | |
| 1036 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1037 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1038 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1039 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1040 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1041 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1042 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1043 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1044 | } |
| 1045 | |
| 1046 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1047 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1048 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 1049 | std::string dex_location = "/xx"; |
| 1050 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1051 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 1052 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1053 | |
| 1054 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1055 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1056 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1057 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1058 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1059 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1060 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1061 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1062 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1063 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1064 | // Trying to make it up to date should have no effect. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1065 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1066 | EXPECT_TRUE(oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1067 | EXPECT_TRUE(error_msg.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1071 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1072 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 1073 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 1074 | Copy(GetDexSrc1(), dex_location); |
| 1075 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1076 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 1077 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1078 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1079 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, oat_file_assistant.GetDexOptNeeded()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1080 | |
| 1081 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 1082 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1083 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1084 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1085 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1086 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1087 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1088 | } |
| 1089 | |
| 1090 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 1091 | class RaceGenerateTask : public Task { |
| 1092 | public: |
| 1093 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
| 1094 | : dex_location_(dex_location), oat_location_(oat_location), |
| 1095 | loaded_oat_file_(nullptr) |
| 1096 | {} |
| 1097 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1098 | void Run(Thread* self ATTRIBUTE_UNUSED) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1099 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 1100 | // 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] | 1101 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1102 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1103 | const OatFile* oat_file = nullptr; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1104 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 1105 | dex_location_.c_str(), |
| 1106 | oat_location_.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1107 | /*class_loader*/nullptr, |
| 1108 | /*dex_elements*/nullptr, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1109 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1110 | &error_msgs); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1111 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1112 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 1113 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1114 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | const OatFile* GetLoadedOatFile() const { |
| 1118 | return loaded_oat_file_; |
| 1119 | } |
| 1120 | |
| 1121 | private: |
| 1122 | std::string dex_location_; |
| 1123 | std::string oat_location_; |
| 1124 | const OatFile* loaded_oat_file_; |
| 1125 | }; |
| 1126 | |
| 1127 | // Test the case where multiple processes race to generate an oat file. |
| 1128 | // This simulates multiple processes using multiple threads. |
| 1129 | // |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1130 | // We want unique Oat files to be loaded even when there is a race to load. |
| 1131 | // TODO: The test case no longer tests locking the way it was intended since we now get multiple |
| 1132 | // copies of the same Oat files mapped at different locations. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1133 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 1134 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1135 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1136 | |
| 1137 | // We use the lib core dex file, because it's large, and hopefully should |
| 1138 | // take a while to generate. |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 1139 | Copy(GetLibCoreDexFileNames()[0], dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1140 | |
| 1141 | const int kNumThreads = 32; |
| 1142 | Thread* self = Thread::Current(); |
| 1143 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 1144 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 1145 | for (int i = 0; i < kNumThreads; i++) { |
| 1146 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 1147 | thread_pool.AddTask(self, task.get()); |
| 1148 | tasks.push_back(std::move(task)); |
| 1149 | } |
| 1150 | thread_pool.StartWorkers(self); |
| 1151 | thread_pool.Wait(self, true, false); |
| 1152 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1153 | // Verify every task got a unique oat file. |
| 1154 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1155 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1156 | const OatFile* oat_file = task->GetLoadedOatFile(); |
| 1157 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 1158 | oat_files.insert(oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 1163 | // disabled. |
| 1164 | // Expect: We should load the odex file non-executable. |
| 1165 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 1166 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1167 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1168 | |
| 1169 | // Create the dex and odex files |
| 1170 | Copy(GetDexSrc1(), dex_location); |
| 1171 | GenerateOdexForTest(dex_location, odex_location); |
| 1172 | |
| 1173 | // Load the oat using an executable oat file assistant. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1174 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 1175 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1176 | |
| 1177 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1178 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1179 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1180 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1181 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1182 | EXPECT_EQ(1u, dex_files.size()); |
| 1183 | } |
| 1184 | |
| 1185 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 1186 | // disabled. |
| 1187 | // Expect: We should load the odex file non-executable. |
| 1188 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 1189 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1190 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1191 | |
| 1192 | // Create the dex and odex files |
| 1193 | Copy(GetMultiDexSrc1(), dex_location); |
| 1194 | GenerateOdexForTest(dex_location, odex_location); |
| 1195 | |
| 1196 | // Load the oat using an executable oat file assistant. |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1197 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 1198 | OatFileAssistant::kFullCompilation, kRuntimeISA, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1199 | |
| 1200 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1201 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1202 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1203 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1204 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1205 | EXPECT_EQ(2u, dex_files.size()); |
| 1206 | } |
| 1207 | |
| 1208 | TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) { |
| 1209 | std::string error_msg; |
| 1210 | std::string odex_file; |
| 1211 | |
| 1212 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1213 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1214 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1215 | |
| 1216 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1217 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1218 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1219 | |
| 1220 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1221 | "nopath.jar", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1222 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1223 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1226 | // Case: We have a DEX file, extract-only ODEX, and fully compiled OAT. |
| 1227 | // Expect: The status depends on the target compilation type mask. |
| 1228 | TEST_F(OatFileAssistantTest, TargetCompilationType) { |
| 1229 | std::string dex_location = GetScratchDir() + "/TargetCompilationType.jar"; |
| 1230 | std::string odex_location = GetOdexDir() + "/TargetCompilationType.odex"; |
| 1231 | Copy(GetDexSrc1(), dex_location); |
| 1232 | GenerateExtractOnlyOdexForTest(dex_location, odex_location); |
| 1233 | GenerateOatForTest(dex_location.c_str()); |
| 1234 | |
| 1235 | OatFileAssistant ofa_full(dex_location.c_str(), |
| 1236 | OatFileAssistant::kFullCompilation, kRuntimeISA, false); |
| 1237 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, ofa_full.GetDexOptNeeded()); |
| 1238 | EXPECT_FALSE(ofa_full.IsInBootClassPath()); |
| 1239 | EXPECT_TRUE(ofa_full.OdexFileIsOutOfDate()); |
| 1240 | EXPECT_TRUE(ofa_full.OatFileIsUpToDate()); |
| 1241 | |
| 1242 | OatFileAssistant ofa_extract(dex_location.c_str(), |
| 1243 | OatFileAssistant::kExtractOnly, kRuntimeISA, false); |
| 1244 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, ofa_extract.GetDexOptNeeded()); |
| 1245 | EXPECT_FALSE(ofa_extract.IsInBootClassPath()); |
| 1246 | EXPECT_TRUE(ofa_extract.OdexFileIsUpToDate()); |
| 1247 | EXPECT_TRUE(ofa_extract.OatFileIsOutOfDate()); |
| 1248 | |
| 1249 | OatFileAssistant ofa_profile(dex_location.c_str(), |
| 1250 | OatFileAssistant::kProfileGuideCompilation, kRuntimeISA, false); |
| 1251 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, ofa_profile.GetDexOptNeeded()); |
| 1252 | EXPECT_FALSE(ofa_profile.IsInBootClassPath()); |
| 1253 | EXPECT_TRUE(ofa_profile.OdexFileIsOutOfDate()); |
| 1254 | EXPECT_TRUE(ofa_profile.OatFileIsOutOfDate()); |
| 1255 | |
| 1256 | OatFileAssistant ofa_extract_full(dex_location.c_str(), |
| 1257 | OatFileAssistant::kFullCompilation | OatFileAssistant::kExtractOnly, |
| 1258 | kRuntimeISA, false); |
| 1259 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, ofa_extract_full.GetDexOptNeeded()); |
| 1260 | EXPECT_FALSE(ofa_extract_full.IsInBootClassPath()); |
| 1261 | EXPECT_TRUE(ofa_extract_full.OdexFileIsUpToDate()); |
| 1262 | EXPECT_TRUE(ofa_extract_full.OatFileIsUpToDate()); |
| 1263 | } |
| 1264 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1265 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1266 | // match the OatFileAssistant::DexOptStatus values. |
| 1267 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
| 1268 | ScopedObjectAccess soa(Thread::Current()); |
| 1269 | StackHandleScope<1> hs(soa.Self()); |
| 1270 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1271 | Handle<mirror::Class> dexfile( |
| 1272 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
| 1273 | ASSERT_FALSE(dexfile.Get() == nullptr); |
| 1274 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1275 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1276 | ArtField* no_dexopt_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1277 | soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I"); |
| 1278 | ASSERT_FALSE(no_dexopt_needed == nullptr); |
| 1279 | EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1280 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get())); |
| 1281 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1282 | ArtField* dex2oat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1283 | soa.Self(), dexfile, "DEX2OAT_NEEDED", "I"); |
| 1284 | ASSERT_FALSE(dex2oat_needed == nullptr); |
| 1285 | EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1286 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get())); |
| 1287 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1288 | ArtField* patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1289 | soa.Self(), dexfile, "PATCHOAT_NEEDED", "I"); |
| 1290 | ASSERT_FALSE(patchoat_needed == nullptr); |
| 1291 | EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1292 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get())); |
| 1293 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1294 | ArtField* self_patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1295 | soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I"); |
| 1296 | ASSERT_FALSE(self_patchoat_needed == nullptr); |
| 1297 | EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1298 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get())); |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1299 | |
| 1300 | ArtField* compilation_type_full = mirror::Class::FindStaticField( |
| 1301 | soa.Self(), dexfile, "COMPILATION_TYPE_FULL", "I"); |
| 1302 | ASSERT_FALSE(compilation_type_full == nullptr); |
| 1303 | EXPECT_EQ(compilation_type_full->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1304 | EXPECT_EQ(OatFileAssistant::kFullCompilation, compilation_type_full->GetInt(dexfile.Get())); |
| 1305 | |
| 1306 | ArtField* compilation_type_profile_guide = mirror::Class::FindStaticField( |
| 1307 | soa.Self(), dexfile, "COMPILATION_TYPE_PROFILE_GUIDE", "I"); |
| 1308 | ASSERT_FALSE(compilation_type_profile_guide == nullptr); |
| 1309 | EXPECT_EQ(compilation_type_profile_guide->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1310 | EXPECT_EQ(OatFileAssistant::kProfileGuideCompilation, |
| 1311 | compilation_type_profile_guide->GetInt(dexfile.Get())); |
| 1312 | |
| 1313 | ArtField* compilation_type_extract_only = mirror::Class::FindStaticField( |
| 1314 | soa.Self(), dexfile, "COMPILATION_TYPE_EXTRACT_ONLY", "I"); |
| 1315 | ASSERT_FALSE(compilation_type_extract_only == nullptr); |
| 1316 | EXPECT_EQ(compilation_type_extract_only->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1317 | EXPECT_EQ(OatFileAssistant::kExtractOnly, compilation_type_extract_only->GetInt(dexfile.Get())); |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1318 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1319 | |
| 1320 | // TODO: More Tests: |
| 1321 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1322 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1323 | // * Test using secondary isa |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1324 | // * Test for status of oat while oat is being generated (how?) |
| 1325 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1326 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1327 | // 64 bit boot class paths. |
| 1328 | // * Test unexpected scenarios (?): |
| 1329 | // - Dex is stripped, don't have odex. |
| 1330 | // - Oat file corrupted after status check, before reload unexecutable |
| 1331 | // because it's unrelocated and no dex2oat |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1332 | // * Test unrelocated specific target compilation type can be relocated to |
| 1333 | // make it up to date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1334 | |
| 1335 | } // namespace art |