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 | // Verify the environment is as we expect |
| 60 | uint32_t checksum; |
| 61 | std::string error_msg; |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 62 | ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str())) |
| 63 | << "Expected pre-compiled boot image to be at: " << GetSystemImageFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 64 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 65 | << "Expected dex file to be at: " << GetDexSrc1(); |
| 66 | ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str())) |
| 67 | << "Expected stripped dex file to be at: " << GetStrippedDexSrc1(); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 68 | ASSERT_FALSE(DexFile::GetChecksum(GetStrippedDexSrc1().c_str(), &checksum, &error_msg)) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 69 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 70 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 71 | << "Expected dex file to be at: " << GetDexSrc2(); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 72 | |
| 73 | // GetMultiDexSrc2 should have the same primary dex checksum as |
| 74 | // GetMultiDexSrc1, but a different secondary dex checksum. |
| 75 | std::vector<std::unique_ptr<const DexFile>> multi1; |
| 76 | ASSERT_TRUE(DexFile::Open(GetMultiDexSrc1().c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 77 | GetMultiDexSrc1().c_str(), &error_msg, &multi1)) << error_msg; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 78 | ASSERT_GT(multi1.size(), 1u); |
| 79 | |
| 80 | std::vector<std::unique_ptr<const DexFile>> multi2; |
| 81 | ASSERT_TRUE(DexFile::Open(GetMultiDexSrc2().c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 82 | GetMultiDexSrc2().c_str(), &error_msg, &multi2)) << error_msg; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 83 | ASSERT_GT(multi2.size(), 1u); |
| 84 | |
| 85 | ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum()); |
| 86 | ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 89 | // Pre-Relocate the image to a known non-zero offset so we don't have to |
| 90 | // deal with the runtime randomly relocating the image by 0 and messing up |
| 91 | // the expected results of the tests. |
| 92 | bool PreRelocateImage(std::string* error_msg) { |
| 93 | std::string image; |
| 94 | if (!GetCachedImageFile(&image, error_msg)) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | std::string patchoat = GetAndroidRoot(); |
| 99 | patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"; |
| 100 | |
| 101 | std::vector<std::string> argv; |
| 102 | argv.push_back(patchoat); |
| 103 | argv.push_back("--input-image-location=" + GetImageLocation()); |
| 104 | argv.push_back("--output-image-file=" + image); |
| 105 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA))); |
| 106 | argv.push_back("--base-offset-delta=0x00008000"); |
| 107 | return Exec(argv, error_msg); |
| 108 | } |
| 109 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 110 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
Richard Uhler | 892fc96 | 2015-03-10 16:57:05 +0000 | [diff] [blame] | 111 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 112 | |
| 113 | // Set up the image location. |
| 114 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 115 | nullptr)); |
| 116 | // Make sure compilercallbacks are not set so that relocation will be |
| 117 | // enabled. |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 118 | callbacks_.reset(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | virtual void PreRuntimeCreate() { |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 122 | std::string error_msg; |
| 123 | ASSERT_TRUE(PreRelocateImage(&error_msg)) << error_msg; |
| 124 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 125 | UnreserveImageSpace(); |
| 126 | } |
| 127 | |
| 128 | virtual void PostRuntimeCreate() { |
| 129 | ReserveImageSpace(); |
| 130 | } |
| 131 | |
| 132 | virtual void TearDown() { |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 133 | ClearDirectory(odex_dir_.c_str()); |
| 134 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 135 | |
| 136 | ClearDirectory(odex_oat_dir_.c_str()); |
| 137 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 138 | |
| 139 | ClearDirectory(scratch_dir_.c_str()); |
| 140 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 141 | |
| 142 | CommonRuntimeTest::TearDown(); |
| 143 | } |
| 144 | |
| 145 | void Copy(std::string src, std::string dst) { |
| 146 | std::ifstream src_stream(src, std::ios::binary); |
| 147 | std::ofstream dst_stream(dst, std::ios::binary); |
| 148 | |
| 149 | dst_stream << src_stream.rdbuf(); |
| 150 | } |
| 151 | |
| 152 | // Returns the directory where the pre-compiled core.art can be found. |
| 153 | // TODO: We should factor out this into common tests somewhere rather than |
| 154 | // re-hardcoding it here (This was copied originally from the elf writer |
| 155 | // test). |
| 156 | std::string GetImageDirectory() { |
| 157 | if (IsHost()) { |
| 158 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 159 | CHECK(host_dir != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 160 | return std::string(host_dir) + "/framework"; |
| 161 | } else { |
| 162 | return std::string("/data/art-test"); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | std::string GetImageLocation() { |
| 167 | return GetImageDirectory() + "/core.art"; |
| 168 | } |
| 169 | |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 170 | std::string GetSystemImageFile() { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 171 | return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA) |
| 172 | + "/core.art"; |
| 173 | } |
| 174 | |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 175 | bool GetCachedImageFile(/*out*/std::string* image, std::string* error_msg) { |
| 176 | std::string cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA), true); |
| 177 | return GetDalvikCacheFilename(GetImageLocation().c_str(), cache.c_str(), image, error_msg); |
| 178 | } |
| 179 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 180 | std::string GetDexSrc1() { |
| 181 | return GetTestDexFileName("Main"); |
| 182 | } |
| 183 | |
| 184 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 185 | // file stripped. |
| 186 | std::string GetStrippedDexSrc1() { |
| 187 | return GetTestDexFileName("MainStripped"); |
| 188 | } |
| 189 | |
| 190 | std::string GetMultiDexSrc1() { |
| 191 | return GetTestDexFileName("MultiDex"); |
| 192 | } |
| 193 | |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 194 | // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but |
| 195 | // with the contents of the secondary dex file changed. |
| 196 | std::string GetMultiDexSrc2() { |
| 197 | return GetTestDexFileName("MultiDexModifiedSecondary"); |
| 198 | } |
| 199 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 200 | std::string GetDexSrc2() { |
| 201 | return GetTestDexFileName("Nested"); |
| 202 | } |
| 203 | |
| 204 | // Scratch directory, for dex and odex files (oat files will go in the |
| 205 | // dalvik cache). |
| 206 | std::string GetScratchDir() { |
| 207 | return scratch_dir_; |
| 208 | } |
| 209 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 210 | // Odex directory is the subdirectory in the scratch directory where odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 211 | // files should be located. |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 212 | std::string GetOdexDir() { |
| 213 | return odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 216 | // Generate a non-PIC odex file for the purposes of test. |
Richard Uhler | 94f5bda | 2015-07-22 08:25:11 -0700 | [diff] [blame] | 217 | // The generated odex file will be un-relocated. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 218 | void GenerateOdexForTest(const std::string& dex_location, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 219 | const std::string& odex_location, |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 220 | CompilerFilter::Filter filter, |
| 221 | bool pic = false, |
| 222 | bool with_patch_info = true) { |
| 223 | // Temporarily redirect the dalvik cache so dex2oat doesn't find the |
| 224 | // relocated image file. |
| 225 | std::string android_data_tmp = GetScratchDir() + "AndroidDataTmp"; |
| 226 | setenv("ANDROID_DATA", android_data_tmp.c_str(), 1); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 227 | std::vector<std::string> args; |
| 228 | args.push_back("--dex-file=" + dex_location); |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 229 | args.push_back("--oat-file=" + odex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 230 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 231 | args.push_back("--runtime-arg"); |
| 232 | args.push_back("-Xnorelocate"); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 233 | |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 234 | if (pic) { |
| 235 | args.push_back("--compile-pic"); |
| 236 | } |
| 237 | |
| 238 | if (with_patch_info) { |
| 239 | args.push_back("--include-patch-information"); |
| 240 | } |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 241 | |
| 242 | std::string error_msg; |
| 243 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 244 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 245 | |
| 246 | // Verify the odex file was generated as expected and really is |
| 247 | // unrelocated. |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 248 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 249 | odex_location.c_str(), |
| 250 | nullptr, |
| 251 | nullptr, |
| 252 | false, |
| 253 | /*low_4gb*/false, |
| 254 | dex_location.c_str(), |
| 255 | &error_msg)); |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 256 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 257 | EXPECT_EQ(pic, odex_file->IsPic()); |
| 258 | EXPECT_EQ(with_patch_info, odex_file->HasPatchInfo()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 259 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 260 | |
Vladimir Marko | f6d1e0f | 2016-05-23 15:32:42 +0100 | [diff] [blame] | 261 | if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 262 | const std::vector<gc::space::ImageSpace*> image_spaces = |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 263 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 264 | ASSERT_TRUE(!image_spaces.empty() && image_spaces[0] != nullptr); |
| 265 | const ImageHeader& image_header = image_spaces[0]->GetImageHeader(); |
| 266 | const OatHeader& oat_header = odex_file->GetOatHeader(); |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 267 | uint32_t combined_checksum = OatFileAssistant::CalculateCombinedImageChecksum(); |
| 268 | EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 269 | EXPECT_NE(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 270 | oat_header.GetImageFileLocationOatDataBegin()); |
| 271 | EXPECT_NE(image_header.GetPatchDelta(), oat_header.GetImagePatchDelta()); |
| 272 | } |
Richard Uhler | 93aa210 | 2015-08-10 14:47:41 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void GeneratePicOdexForTest(const std::string& dex_location, |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 276 | const std::string& odex_location, |
| 277 | CompilerFilter::Filter filter) { |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 278 | GenerateOdexForTest(dex_location, odex_location, filter, true, false); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 279 | } |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 280 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 281 | // Generate a non-PIC odex file without patch information for the purposes |
| 282 | // of test. The generated odex file will be un-relocated. |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 283 | void GenerateNoPatchOdexForTest(const std::string& dex_location, |
| 284 | const std::string& odex_location, |
| 285 | CompilerFilter::Filter filter) { |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 286 | GenerateOdexForTest(dex_location, odex_location, filter, false, false); |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 289 | private: |
| 290 | // Reserve memory around where the image will be loaded so other memory |
| 291 | // won't conflict when it comes time to load the image. |
| 292 | // This can be called with an already loaded image to reserve the space |
| 293 | // around it. |
| 294 | void ReserveImageSpace() { |
| 295 | MemMap::Init(); |
| 296 | |
| 297 | // Ensure a chunk of memory is reserved for the image space. |
Richard Uhler | a48403e | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 298 | // The reservation_end includes room for the main space that has to come |
| 299 | // right after the image in case of the GSS collector. |
| 300 | uintptr_t reservation_start = ART_BASE_ADDRESS; |
| 301 | uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 302 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 303 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true)); |
| 304 | ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map"; |
| 305 | for (BacktraceMap::const_iterator it = map->begin(); |
| 306 | reservation_start < reservation_end && it != map->end(); ++it) { |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 307 | ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end)); |
| 308 | reservation_start = std::max(reservation_start, it->end); |
| 309 | } |
| 310 | ReserveImageSpaceChunk(reservation_start, reservation_end); |
| 311 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 312 | |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 313 | // Reserve a chunk of memory for the image space in the given range. |
| 314 | // Only has effect for chunks with a positive number of bytes. |
| 315 | void ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) { |
| 316 | if (start < end) { |
| 317 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 318 | image_reservation_.push_back(std::unique_ptr<MemMap>( |
| 319 | MemMap::MapAnonymous("image reservation", |
Richard Uhler | 3efe979 | 2015-03-30 16:18:03 -0700 | [diff] [blame] | 320 | reinterpret_cast<uint8_t*>(start), end - start, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 321 | PROT_NONE, false, false, &error_msg))); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 322 | ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg; |
| 323 | LOG(INFO) << "Reserved space for image " << |
| 324 | reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" << |
| 325 | reinterpret_cast<void*>(image_reservation_.back()->End()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | // Unreserve any memory reserved by ReserveImageSpace. This should be called |
| 331 | // before the image is loaded. |
| 332 | void UnreserveImageSpace() { |
| 333 | image_reservation_.clear(); |
| 334 | } |
| 335 | |
| 336 | std::string scratch_dir_; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 337 | std::string odex_oat_dir_; |
| 338 | std::string odex_dir_; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 339 | std::vector<std::unique_ptr<MemMap>> image_reservation_; |
| 340 | }; |
| 341 | |
| 342 | class OatFileAssistantNoDex2OatTest : public OatFileAssistantTest { |
| 343 | public: |
| 344 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) { |
| 345 | OatFileAssistantTest::SetUpRuntimeOptions(options); |
| 346 | options->push_back(std::make_pair("-Xnodex2oat", nullptr)); |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | // Generate an oat file for the purposes of test, as opposed to testing |
| 351 | // generation of oat files. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 352 | static void GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) { |
| 353 | // Use an oat file assistant to find the proper oat location. |
| 354 | OatFileAssistant ofa(dex_location, kRuntimeISA, false, false); |
| 355 | const std::string* oat_location = ofa.OatFileName(); |
| 356 | ASSERT_TRUE(oat_location != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 357 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 358 | std::vector<std::string> args; |
| 359 | args.push_back("--dex-file=" + std::string(dex_location)); |
| 360 | args.push_back("--oat-file=" + *oat_location); |
| 361 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 362 | args.push_back("--runtime-arg"); |
| 363 | args.push_back("-Xnorelocate"); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 364 | std::string error_msg; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 365 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
| 366 | |
| 367 | // Verify the oat file was generated as expected. |
| 368 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location->c_str(), |
| 369 | oat_location->c_str(), |
| 370 | nullptr, |
| 371 | nullptr, |
| 372 | false, |
| 373 | /*low_4gb*/false, |
| 374 | dex_location, |
| 375 | &error_msg)); |
| 376 | ASSERT_TRUE(oat_file.get() != nullptr) << error_msg; |
| 377 | EXPECT_EQ(filter, oat_file->GetCompilerFilter()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Case: We have a DEX file, but no OAT file for it. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 381 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 382 | TEST_F(OatFileAssistantTest, DexNoOat) { |
| 383 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 384 | Copy(GetDexSrc1(), dex_location); |
| 385 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 386 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 387 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 388 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 389 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 390 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 391 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 392 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 393 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
| 394 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 395 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 396 | |
| 397 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 398 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 399 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 400 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 401 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 402 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OdexFileStatus()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 403 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 404 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 405 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 406 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 407 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 408 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Case: We have no DEX file and no OAT file. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 412 | // Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 413 | TEST_F(OatFileAssistantTest, NoDexNoOat) { |
| 414 | std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar"; |
| 415 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 416 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 417 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 418 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 419 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 420 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 421 | |
| 422 | // Trying to make the oat file up to date should not fail or crash. |
| 423 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 424 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 425 | |
| 426 | // Trying to get the best oat file should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 427 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 428 | EXPECT_EQ(nullptr, oat_file.get()); |
| 429 | } |
| 430 | |
| 431 | // 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] | 432 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 433 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 434 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 435 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 436 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 437 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 438 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 439 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 440 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 441 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 442 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 443 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 444 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 445 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 446 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 447 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 448 | |
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 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 461 | // Case: We have a DEX file and speed-profile OAT file for it. |
| 462 | // Expect: The status is kNoDexOptNeeded if the profile hasn't changed. |
| 463 | TEST_F(OatFileAssistantTest, ProfileOatUpToDate) { |
| 464 | std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar"; |
| 465 | Copy(GetDexSrc1(), dex_location); |
| 466 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile); |
| 467 | |
| 468 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
| 469 | |
| 470 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 471 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
| 472 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 473 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 474 | |
| 475 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 476 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 477 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 478 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 479 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 480 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 481 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 482 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 483 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
| 484 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 485 | } |
| 486 | |
| 487 | // Case: We have a DEX file and speed-profile OAT file for it. |
| 488 | // Expect: The status is kNoDex2OatNeeded if the profile has changed. |
| 489 | TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) { |
| 490 | std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar"; |
| 491 | Copy(GetDexSrc1(), dex_location); |
| 492 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile); |
| 493 | |
| 494 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false); |
| 495 | |
| 496 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 497 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
| 498 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 499 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 500 | |
| 501 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 502 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 503 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 504 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 505 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 506 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 507 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 508 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 509 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
| 510 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 511 | } |
| 512 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 513 | // 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] | 514 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 515 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 516 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 517 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 518 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 519 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 520 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
| 521 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 522 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 523 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 524 | |
| 525 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 526 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 527 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 528 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 529 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 530 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 531 | EXPECT_EQ(2u, dex_files.size()); |
| 532 | } |
| 533 | |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 534 | // Case: We have a MultiDEX file where the secondary dex file is out of date. |
| 535 | // Expect: The status is kDex2OatNeeded. |
| 536 | TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) { |
| 537 | std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar"; |
| 538 | |
| 539 | // Compile code for GetMultiDexSrc1. |
| 540 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 541 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 542 | |
| 543 | // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum |
| 544 | // is out of date. |
| 545 | Copy(GetMultiDexSrc2(), dex_location); |
| 546 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 547 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
| 548 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 549 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 550 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 553 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 554 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 555 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 556 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 557 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 558 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 559 | |
| 560 | // Create the dex file |
| 561 | Copy(GetMultiDexSrc1(), dex_location); |
| 562 | |
| 563 | // Create the oat file with relative encoded dex location. |
| 564 | std::vector<std::string> args; |
| 565 | args.push_back("--dex-file=" + dex_location); |
| 566 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 567 | args.push_back("--oat-file=" + oat_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 568 | args.push_back("--compiler-filter=speed"); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 569 | |
| 570 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 571 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 572 | |
| 573 | // Verify we can load both dex files. |
| 574 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 575 | oat_location.c_str(), |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 576 | kRuntimeISA, false, true); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 577 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 578 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 579 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 580 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 581 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 582 | EXPECT_EQ(2u, dex_files.size()); |
| 583 | } |
| 584 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 585 | // Case: We have a DEX file and out-of-date OAT file. |
| 586 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 587 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 588 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 589 | |
| 590 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 591 | // different dex to make the oat out of date. |
| 592 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 593 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 594 | Copy(GetDexSrc2(), dex_location); |
| 595 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 596 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
| 597 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 598 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 599 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 600 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 601 | |
| 602 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 603 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 604 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 605 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 606 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 607 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 608 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 609 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | // 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] | 613 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 614 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 615 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 616 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 617 | |
| 618 | // Create the dex and odex files |
| 619 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 620 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 621 | |
| 622 | // Verify the status. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 623 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 624 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 625 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 626 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 627 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 628 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 629 | |
| 630 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 631 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 632 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 633 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 634 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 635 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 636 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 637 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 638 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 639 | |
| 640 | // We should still be able to get the non-executable odex file to run from. |
| 641 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 642 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | // 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] | 646 | // Expect: The status is kPatchOatNeeded |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 647 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 648 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 649 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 650 | |
| 651 | // Create the dex and odex files |
| 652 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 653 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 654 | |
| 655 | // Strip the dex file |
| 656 | Copy(GetStrippedDexSrc1(), dex_location); |
| 657 | |
| 658 | // Verify the status. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 659 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 660 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 661 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 662 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
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_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 668 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 669 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 670 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 671 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 672 | |
| 673 | // Make the oat file up to date. |
| 674 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 675 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 676 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 677 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 678 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 679 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 680 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 681 | |
| 682 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 683 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 684 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 685 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 686 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 687 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 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 | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 700 | // Case: We have a stripped DEX file, an ODEX file, and an out-of-date OAT file. |
| 701 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 702 | TEST_F(OatFileAssistantTest, StrippedDexOdexOat) { |
| 703 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 704 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 705 | |
| 706 | // Create the oat file from a different dex file so it looks out of date. |
| 707 | Copy(GetDexSrc2(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 708 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 709 | |
| 710 | // Create the odex file |
| 711 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 712 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 713 | |
| 714 | // Strip the dex file. |
| 715 | Copy(GetStrippedDexSrc1(), dex_location); |
| 716 | |
| 717 | // Verify the status. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 718 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 719 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 720 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 721 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 722 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 723 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 724 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped. |
| 725 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 726 | |
| 727 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 728 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 729 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 730 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 731 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 732 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 733 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 734 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 735 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 736 | |
| 737 | // Make the oat file up to date. |
| 738 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 739 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 740 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 741 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 742 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 743 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 744 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 745 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped. |
| 746 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 747 | |
| 748 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 749 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 750 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 751 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 752 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 753 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 754 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 755 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 756 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 757 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 758 | |
| 759 | // Verify we can load the dex files from it. |
| 760 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 761 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 762 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 763 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 764 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 765 | EXPECT_EQ(1u, dex_files.size()); |
| 766 | } |
| 767 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 768 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 769 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 770 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 771 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 772 | |
| 773 | Copy(GetStrippedDexSrc1(), dex_location); |
| 774 | |
| 775 | // Verify the status. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 776 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 777 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 778 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 779 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 780 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 781 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 782 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 783 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 784 | |
| 785 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 786 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 787 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 788 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 789 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 790 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 791 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 792 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 793 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 794 | |
| 795 | // Make the oat file up to date. This should have no effect. |
| 796 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 797 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 798 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 799 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 800 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 801 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 802 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 803 | |
| 804 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 805 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 806 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 807 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 808 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 809 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 810 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 811 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 812 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 813 | } |
| 814 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 815 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 816 | // relocation. |
| 817 | // Expect: The status is kSelfPatchOatNeeded. |
| 818 | TEST_F(OatFileAssistantTest, SelfRelocation) { |
| 819 | std::string dex_location = GetScratchDir() + "/SelfRelocation.jar"; |
| 820 | std::string oat_location = GetOdexDir() + "/SelfRelocation.oat"; |
| 821 | |
| 822 | // Create the dex and odex files |
| 823 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 824 | GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 825 | |
| 826 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 827 | oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 828 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 829 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 830 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 831 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, |
| 832 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 833 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 834 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 835 | |
| 836 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 837 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 838 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 839 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 840 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 841 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 842 | EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation()); |
| 843 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 844 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 845 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 846 | |
| 847 | // Make the oat file up to date. |
| 848 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 849 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 850 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 851 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 852 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 853 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 854 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 855 | |
| 856 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 857 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 858 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 859 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 860 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 861 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 862 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 863 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 864 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 865 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 866 | |
| 867 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 868 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 869 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 870 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 871 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 872 | EXPECT_EQ(1u, dex_files.size()); |
| 873 | } |
| 874 | |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 875 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 876 | // relocation but doesn't have patch info. |
| 877 | // Expect: The status is kDex2OatNeeded, because we can't run patchoat. |
| 878 | TEST_F(OatFileAssistantTest, NoSelfRelocation) { |
| 879 | std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar"; |
| 880 | std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat"; |
| 881 | |
| 882 | // Create the dex and odex files |
| 883 | Copy(GetDexSrc1(), dex_location); |
| 884 | GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
| 885 | |
| 886 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 887 | oat_location.c_str(), kRuntimeISA, false, true); |
| 888 | |
| 889 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 890 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 891 | |
| 892 | // Make the oat file up to date. |
| 893 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 894 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 895 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 896 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | d1537b5 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 897 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 898 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 899 | |
| 900 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 901 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 902 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 903 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 904 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 905 | EXPECT_EQ(1u, dex_files.size()); |
| 906 | } |
| 907 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 908 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 909 | // OAT files both have patch delta of 0. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 910 | // Expect: It shouldn't crash, and status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 911 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 912 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 913 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 914 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 915 | |
| 916 | // Create the dex and odex files |
| 917 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 918 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 919 | |
| 920 | // Create the oat file by copying the odex so they are located in the same |
| 921 | // place in memory. |
| 922 | Copy(odex_location, oat_location); |
| 923 | |
| 924 | // Verify things don't go bad. |
| 925 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 926 | oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 927 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 928 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 929 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 930 | |
| 931 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 932 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 933 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 934 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 935 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 936 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 937 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 938 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 939 | |
| 940 | // Things aren't relocated, so it should fall back to interpreted. |
| 941 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 942 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 943 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 944 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 945 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 946 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 947 | EXPECT_EQ(1u, dex_files.size()); |
| 948 | } |
| 949 | |
| 950 | // 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] | 951 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 952 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 953 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 954 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 955 | |
| 956 | // Create the dex and odex files |
| 957 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 958 | GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 959 | |
| 960 | // Verify the status. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 961 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 962 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 963 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 964 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 965 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 966 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 967 | |
| 968 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 969 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 970 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 971 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 972 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 973 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 974 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 975 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 978 | // Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file. |
| 979 | // Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code. |
| 980 | TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) { |
| 981 | std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar"; |
| 982 | std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex"; |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 983 | |
| 984 | // Create the dex and odex files |
| 985 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 986 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 987 | |
| 988 | // Verify the status. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 989 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 990 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 991 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 992 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 993 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 994 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 995 | |
| 996 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 997 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 998 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
Nicolas Geoffray | 845e506 | 2016-03-23 06:42:05 +0000 | [diff] [blame] | 999 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 1000 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1001 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1002 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1003 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 1004 | } |
| 1005 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1006 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 1007 | // Expect: We should load an executable dex file. |
| 1008 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 1009 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 1010 | |
| 1011 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1012 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1013 | |
| 1014 | // Load the oat using an oat file assistant. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1015 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
| 1016 | |
| 1017 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1018 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1019 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1020 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1021 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1022 | EXPECT_EQ(1u, dex_files.size()); |
| 1023 | } |
| 1024 | |
| 1025 | // Case: We have a DEX file and up-to-date interpret-only OAT file for it. |
| 1026 | // Expect: We should still load the oat file as executable. |
| 1027 | TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) { |
| 1028 | std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar"; |
| 1029 | |
| 1030 | Copy(GetDexSrc1(), dex_location); |
| 1031 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly); |
| 1032 | |
| 1033 | // Load the oat using an oat file assistant. |
| 1034 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1035 | |
| 1036 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1037 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1038 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1039 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1040 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1041 | EXPECT_EQ(1u, dex_files.size()); |
| 1042 | } |
| 1043 | |
| 1044 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 1045 | // Expect: Loading non-executable should load the oat non-executable. |
| 1046 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 1047 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 1048 | |
| 1049 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1050 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1051 | |
| 1052 | // Load the oat using an oat file assistant. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1053 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1054 | |
| 1055 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1056 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1057 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1058 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1059 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1060 | EXPECT_EQ(1u, dex_files.size()); |
| 1061 | } |
| 1062 | |
| 1063 | // Case: We have a DEX file. |
| 1064 | // Expect: We should load an executable dex file from an alternative oat |
| 1065 | // location. |
| 1066 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 1067 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 1068 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 1069 | |
| 1070 | Copy(GetDexSrc1(), dex_location); |
| 1071 | |
| 1072 | OatFileAssistant oat_file_assistant( |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1073 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1074 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1075 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1076 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1077 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1078 | |
| 1079 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1080 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1081 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1082 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1083 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1084 | EXPECT_EQ(1u, dex_files.size()); |
| 1085 | |
| 1086 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 1087 | |
| 1088 | // Verify it didn't create an oat in the default location. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1089 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1090 | EXPECT_FALSE(ofm.OatFileExists()); |
| 1091 | } |
| 1092 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1093 | // Case: We have a DEX file but can't write the oat file. |
| 1094 | // Expect: We should fail to make the oat file up to date. |
| 1095 | TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) { |
| 1096 | std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar"; |
| 1097 | |
| 1098 | // Make the oat location unwritable by inserting some non-existent |
| 1099 | // intermediate directories. |
| 1100 | std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat"; |
| 1101 | |
| 1102 | Copy(GetDexSrc1(), dex_location); |
| 1103 | |
| 1104 | OatFileAssistant oat_file_assistant( |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1105 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1106 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1107 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1108 | ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1109 | oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1110 | |
| 1111 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1112 | ASSERT_TRUE(oat_file.get() == nullptr); |
| 1113 | } |
| 1114 | |
| 1115 | // Case: We don't have a DEX file and can't write the oat file. |
| 1116 | // Expect: We should fail to generate the oat file without crashing. |
| 1117 | TEST_F(OatFileAssistantTest, GenNoDex) { |
| 1118 | std::string dex_location = GetScratchDir() + "/GenNoDex.jar"; |
| 1119 | std::string oat_location = GetScratchDir() + "/GenNoDex.oat"; |
| 1120 | |
| 1121 | OatFileAssistant oat_file_assistant( |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1122 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1123 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1124 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1125 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1126 | oat_file_assistant.GenerateOatFile(&error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1129 | // Turn an absolute path into a path relative to the current working |
| 1130 | // directory. |
| 1131 | static std::string MakePathRelative(std::string target) { |
| 1132 | char buf[MAXPATHLEN]; |
| 1133 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 1134 | |
| 1135 | // Split the target and cwd paths into components. |
| 1136 | std::vector<std::string> target_path; |
| 1137 | std::vector<std::string> cwd_path; |
| 1138 | Split(target, '/', &target_path); |
| 1139 | Split(cwd, '/', &cwd_path); |
| 1140 | |
| 1141 | // Reverse the path components, so we can use pop_back(). |
| 1142 | std::reverse(target_path.begin(), target_path.end()); |
| 1143 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 1144 | |
| 1145 | // Drop the common prefix of the paths. Because we reversed the path |
| 1146 | // components, this becomes the common suffix of target_path and cwd_path. |
| 1147 | while (!target_path.empty() && !cwd_path.empty() |
| 1148 | && target_path.back() == cwd_path.back()) { |
| 1149 | target_path.pop_back(); |
| 1150 | cwd_path.pop_back(); |
| 1151 | } |
| 1152 | |
| 1153 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 1154 | // of the target path. Because we reversed the path components, we add to |
| 1155 | // the end of target_path. |
| 1156 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 1157 | target_path.push_back(".."); |
| 1158 | } |
| 1159 | |
| 1160 | // Reverse again to get the right path order, and join to get the result. |
| 1161 | std::reverse(target_path.begin(), target_path.end()); |
| 1162 | return Join(target_path, '/'); |
| 1163 | } |
| 1164 | |
| 1165 | // Case: Non-absolute path to Dex location. |
| 1166 | // Expect: Not sure, but it shouldn't crash. |
| 1167 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 1168 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 1169 | Copy(GetDexSrc1(), abs_dex_location); |
| 1170 | |
| 1171 | std::string dex_location = MakePathRelative(abs_dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1172 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1173 | |
| 1174 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1175 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1176 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1177 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1178 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1179 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1180 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1181 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1182 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1183 | } |
| 1184 | |
| 1185 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1186 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1187 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 1188 | std::string dex_location = "/xx"; |
| 1189 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1190 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1191 | |
| 1192 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1193 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1194 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1195 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1196 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1197 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1198 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1199 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1200 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1201 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1202 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1203 | // Trying to make it up to date should have no effect. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1204 | std::string error_msg; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1205 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1206 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1207 | oat_file_assistant.MakeUpToDate(&error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1208 | EXPECT_TRUE(error_msg.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1212 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1213 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 1214 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 1215 | Copy(GetDexSrc1(), dex_location); |
| 1216 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1217 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1218 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1219 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1220 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1221 | |
| 1222 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 1223 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1224 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1225 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1226 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1227 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1228 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1229 | } |
| 1230 | |
| 1231 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 1232 | class RaceGenerateTask : public Task { |
| 1233 | public: |
| 1234 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 1235 | : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1236 | {} |
| 1237 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1238 | void Run(Thread* self ATTRIBUTE_UNUSED) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1239 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 1240 | // 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] | 1241 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1242 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1243 | const OatFile* oat_file = nullptr; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1244 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 1245 | dex_location_.c_str(), |
| 1246 | oat_location_.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1247 | /*class_loader*/nullptr, |
| 1248 | /*dex_elements*/nullptr, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1249 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1250 | &error_msgs); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1251 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1252 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 1253 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1254 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | const OatFile* GetLoadedOatFile() const { |
| 1258 | return loaded_oat_file_; |
| 1259 | } |
| 1260 | |
| 1261 | private: |
| 1262 | std::string dex_location_; |
| 1263 | std::string oat_location_; |
| 1264 | const OatFile* loaded_oat_file_; |
| 1265 | }; |
| 1266 | |
| 1267 | // Test the case where multiple processes race to generate an oat file. |
| 1268 | // This simulates multiple processes using multiple threads. |
| 1269 | // |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1270 | // We want unique Oat files to be loaded even when there is a race to load. |
| 1271 | // TODO: The test case no longer tests locking the way it was intended since we now get multiple |
| 1272 | // copies of the same Oat files mapped at different locations. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1273 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 1274 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1275 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1276 | |
| 1277 | // We use the lib core dex file, because it's large, and hopefully should |
| 1278 | // take a while to generate. |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 1279 | Copy(GetLibCoreDexFileNames()[0], dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1280 | |
| 1281 | const int kNumThreads = 32; |
| 1282 | Thread* self = Thread::Current(); |
| 1283 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 1284 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 1285 | for (int i = 0; i < kNumThreads; i++) { |
| 1286 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 1287 | thread_pool.AddTask(self, task.get()); |
| 1288 | tasks.push_back(std::move(task)); |
| 1289 | } |
| 1290 | thread_pool.StartWorkers(self); |
| 1291 | thread_pool.Wait(self, true, false); |
| 1292 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1293 | // Verify every task got a unique oat file. |
| 1294 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1295 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1296 | const OatFile* oat_file = task->GetLoadedOatFile(); |
| 1297 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 1298 | oat_files.insert(oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 1303 | // disabled. |
| 1304 | // Expect: We should load the odex file non-executable. |
| 1305 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 1306 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1307 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1308 | |
| 1309 | // Create the dex and odex files |
| 1310 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1311 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1312 | |
| 1313 | // Load the oat using an executable oat file assistant. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1314 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1315 | |
| 1316 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1317 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1318 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1319 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1320 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1321 | EXPECT_EQ(1u, dex_files.size()); |
| 1322 | } |
| 1323 | |
| 1324 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 1325 | // disabled. |
| 1326 | // Expect: We should load the odex file non-executable. |
| 1327 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 1328 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1329 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1330 | |
| 1331 | // Create the dex and odex files |
| 1332 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1333 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1334 | |
| 1335 | // Load the oat using an executable oat file assistant. |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1336 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1337 | |
| 1338 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1339 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1340 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1341 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1342 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1343 | EXPECT_EQ(2u, dex_files.size()); |
| 1344 | } |
| 1345 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 1346 | TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) { |
| 1347 | std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar"; |
| 1348 | Copy(GetDexSrc1(), dex_location); |
| 1349 | |
| 1350 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
| 1351 | |
| 1352 | std::string error_msg; |
| 1353 | Runtime::Current()->AddCompilerOption("--compiler-filter=interpret-only"); |
| 1354 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 1355 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 1356 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1357 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 1358 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1359 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 1360 | |
| 1361 | Runtime::Current()->AddCompilerOption("--compiler-filter=speed"); |
| 1362 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 1363 | oat_file_assistant.MakeUpToDate(&error_msg)) << error_msg; |
| 1364 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1365 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 1366 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1367 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 1368 | |
| 1369 | Runtime::Current()->AddCompilerOption("--compiler-filter=bogus"); |
| 1370 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
| 1371 | oat_file_assistant.MakeUpToDate(&error_msg)); |
| 1372 | } |
| 1373 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1374 | TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) { |
| 1375 | std::string error_msg; |
| 1376 | std::string odex_file; |
| 1377 | |
| 1378 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1379 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1380 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1381 | |
| 1382 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1383 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1384 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1385 | |
| 1386 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1387 | "nopath.jar", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1388 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1389 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1392 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1393 | // match the OatFileAssistant::DexOptStatus values. |
| 1394 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
| 1395 | ScopedObjectAccess soa(Thread::Current()); |
| 1396 | StackHandleScope<1> hs(soa.Self()); |
| 1397 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1398 | Handle<mirror::Class> dexfile( |
| 1399 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
| 1400 | ASSERT_FALSE(dexfile.Get() == nullptr); |
| 1401 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1402 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1403 | ArtField* no_dexopt_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1404 | soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I"); |
| 1405 | ASSERT_FALSE(no_dexopt_needed == nullptr); |
| 1406 | EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1407 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get())); |
| 1408 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1409 | ArtField* dex2oat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1410 | soa.Self(), dexfile, "DEX2OAT_NEEDED", "I"); |
| 1411 | ASSERT_FALSE(dex2oat_needed == nullptr); |
| 1412 | EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1413 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get())); |
| 1414 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1415 | ArtField* patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1416 | soa.Self(), dexfile, "PATCHOAT_NEEDED", "I"); |
| 1417 | ASSERT_FALSE(patchoat_needed == nullptr); |
| 1418 | EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1419 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get())); |
| 1420 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1421 | ArtField* self_patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1422 | soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I"); |
| 1423 | ASSERT_FALSE(self_patchoat_needed == nullptr); |
| 1424 | EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1425 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get())); |
| 1426 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1427 | |
| 1428 | // TODO: More Tests: |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1429 | // * Image checksum change is out of date for kIntepretOnly, but not |
| 1430 | // kVerifyAtRuntime. But target of kVerifyAtRuntime still says current |
| 1431 | // kInterpretOnly is out of date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1432 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1433 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1434 | // * Test using secondary isa |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1435 | // * Test for status of oat while oat is being generated (how?) |
| 1436 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1437 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1438 | // 64 bit boot class paths. |
| 1439 | // * Test unexpected scenarios (?): |
| 1440 | // - Dex is stripped, don't have odex. |
| 1441 | // - Oat file corrupted after status check, before reload unexecutable |
| 1442 | // because it's unrelocated and no dex2oat |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1443 | // * Test unrelocated specific target compilation type can be relocated to |
| 1444 | // make it up to date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1445 | |
| 1446 | } // namespace art |