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 | 7dfe5de | 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 | 7dfe5de | 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 | 7dfe5de | 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 | 7dfe5de | 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 | 7dfe5de | 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 | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 219 | const std::string& odex_location, |
Richard Uhler | 7dfe5de | 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 | 7dfe5de | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 229 | args.push_back("--oat-file=" + odex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 230 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
Richard Uhler | 7dfe5de | 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 | 7dfe5de | 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 | 7dfe5de | 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 | bcb6a72 | 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 | 7dfe5de | 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 | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 259 | EXPECT_EQ(filter, odex_file->GetCompilerFilter()); |
| 260 | |
Vladimir Marko | 8c185bf | 2016-05-23 15:32:42 +0100 | [diff] [blame] | 261 | if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) { |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 262 | const std::vector<gc::space::ImageSpace*> image_spaces = |
Richard Uhler | 7dfe5de | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 263 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Andreas Gampe | 7bcfcb8 | 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 | 4f351aa | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 276 | const std::string& odex_location, |
| 277 | CompilerFilter::Filter filter) { |
Richard Uhler | 7dfe5de | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 278 | GenerateOdexForTest(dex_location, odex_location, filter, true, false); |
Calin Juravle | d91b8a2 | 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 | 1c4eb04 | 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 | 1c4eb04 | 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 | 7dfe5de | 2016-04-26 10:24:38 -0700 | [diff] [blame^] | 286 | GenerateOdexForTest(dex_location, odex_location, filter, false, false); |
Richard Uhler | 1c4eb04 | 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 | 7dfe5de | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 424 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 425 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 426 | |
| 427 | // Trying to get the best oat file should fail, but not crash. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 428 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 429 | EXPECT_EQ(nullptr, oat_file.get()); |
| 430 | } |
| 431 | |
| 432 | // 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] | 433 | // Expect: The status is kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 434 | TEST_F(OatFileAssistantTest, OatUpToDate) { |
| 435 | std::string dex_location = GetScratchDir() + "/OatUpToDate.jar"; |
| 436 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 437 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 438 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 439 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 440 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 441 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 442 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 443 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 444 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 445 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 446 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 447 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 448 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
| 449 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 450 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 451 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 452 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 453 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 454 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 455 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 456 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 457 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 458 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 459 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 462 | // Case: We have a DEX file and speed-profile OAT file for it. |
| 463 | // Expect: The status is kNoDexOptNeeded if the profile hasn't changed. |
| 464 | TEST_F(OatFileAssistantTest, ProfileOatUpToDate) { |
| 465 | std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar"; |
| 466 | Copy(GetDexSrc1(), dex_location); |
| 467 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile); |
| 468 | |
| 469 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
| 470 | |
| 471 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 472 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
| 473 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 474 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 475 | |
| 476 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 477 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 478 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 479 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 480 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 481 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 482 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 483 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
| 484 | EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus()); |
| 485 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 486 | } |
| 487 | |
| 488 | // Case: We have a DEX file and speed-profile OAT file for it. |
| 489 | // Expect: The status is kNoDex2OatNeeded if the profile has changed. |
| 490 | TEST_F(OatFileAssistantTest, ProfileOatOutOfDate) { |
| 491 | std::string dex_location = GetScratchDir() + "/ProfileOatOutOfDate.jar"; |
| 492 | Copy(GetDexSrc1(), dex_location); |
| 493 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile); |
| 494 | |
| 495 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true, false); |
| 496 | |
| 497 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 498 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile)); |
| 499 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 500 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 501 | |
| 502 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 503 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 504 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 505 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 506 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 507 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 508 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 509 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 510 | EXPECT_EQ(OatFileAssistant::kOatOutOfDate, oat_file_assistant.OatFileStatus()); |
| 511 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 512 | } |
| 513 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 514 | // 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] | 515 | // Expect: The status is kNoDexOptNeeded and we load all dex files. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 516 | TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) { |
| 517 | std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar"; |
| 518 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 519 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 520 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 521 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
| 522 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 523 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 524 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 525 | |
| 526 | // Verify we can load both dex files. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 527 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 528 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 529 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 530 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 531 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 532 | EXPECT_EQ(2u, dex_files.size()); |
| 533 | } |
| 534 | |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 535 | // Case: We have a MultiDEX file where the secondary dex file is out of date. |
| 536 | // Expect: The status is kDex2OatNeeded. |
| 537 | TEST_F(OatFileAssistantTest, MultiDexSecondaryOutOfDate) { |
| 538 | std::string dex_location = GetScratchDir() + "/MultiDexSecondaryOutOfDate.jar"; |
| 539 | |
| 540 | // Compile code for GetMultiDexSrc1. |
| 541 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 542 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 543 | |
| 544 | // Now overwrite the dex file with GetMultiDexSrc2 so the secondary checksum |
| 545 | // is out of date. |
| 546 | Copy(GetMultiDexSrc2(), dex_location); |
| 547 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 548 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
| 549 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 550 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 551 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 554 | // Case: We have a MultiDEX file and up-to-date OAT file for it with relative |
| 555 | // encoded dex locations. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 556 | // Expect: The oat file status is kNoDexOptNeeded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 557 | TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) { |
| 558 | std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 559 | std::string oat_location = GetOdexDir() + "/RelativeEncodedDexLocation.oat"; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 560 | |
| 561 | // Create the dex file |
| 562 | Copy(GetMultiDexSrc1(), dex_location); |
| 563 | |
| 564 | // Create the oat file with relative encoded dex location. |
| 565 | std::vector<std::string> args; |
| 566 | args.push_back("--dex-file=" + dex_location); |
| 567 | args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar")); |
| 568 | args.push_back("--oat-file=" + oat_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 569 | args.push_back("--compiler-filter=speed"); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 570 | |
| 571 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 572 | ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 573 | |
| 574 | // Verify we can load both dex files. |
| 575 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 576 | oat_location.c_str(), |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 577 | kRuntimeISA, false, true); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 578 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 579 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 580 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 581 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 582 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 583 | EXPECT_EQ(2u, dex_files.size()); |
| 584 | } |
| 585 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 586 | // Case: We have a DEX file and out-of-date OAT file. |
| 587 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 588 | TEST_F(OatFileAssistantTest, OatOutOfDate) { |
| 589 | std::string dex_location = GetScratchDir() + "/OatOutOfDate.jar"; |
| 590 | |
| 591 | // We create a dex, generate an oat for it, then overwrite the dex with a |
| 592 | // different dex to make the oat out of date. |
| 593 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 594 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 595 | Copy(GetDexSrc2(), dex_location); |
| 596 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 597 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
| 598 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 599 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 600 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 601 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 602 | |
| 603 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 604 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 605 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 606 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 607 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 608 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 609 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 610 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // 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] | 614 | // Expect: The status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 615 | TEST_F(OatFileAssistantTest, DexOdexNoOat) { |
| 616 | std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 617 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 618 | |
| 619 | // Create the dex and odex files |
| 620 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 621 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 622 | |
| 623 | // Verify the status. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 624 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 625 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 626 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 627 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 628 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 629 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 630 | |
| 631 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 632 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 633 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 634 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 635 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 636 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 637 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 638 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 639 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 640 | |
| 641 | // We should still be able to get the non-executable odex file to run from. |
| 642 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 643 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | // 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] | 647 | // Expect: The status is kPatchOatNeeded |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 648 | TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) { |
| 649 | std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 650 | std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 651 | |
| 652 | // Create the dex and odex files |
| 653 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 654 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 655 | |
| 656 | // Strip the dex file |
| 657 | Copy(GetStrippedDexSrc1(), dex_location); |
| 658 | |
| 659 | // Verify the status. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 660 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 661 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 662 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 663 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 664 | |
| 665 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 666 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 667 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 668 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 669 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 670 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 671 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 672 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 673 | |
| 674 | // Make the oat file up to date. |
| 675 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 676 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 677 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 678 | |
Andreas Gampe | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | 7bcfcb8 | 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 | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 739 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 740 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 741 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 742 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 743 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 744 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, // Can't run dex2oat because dex file is stripped. |
| 745 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 746 | |
| 747 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 748 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 749 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 750 | EXPECT_TRUE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 751 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 752 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 753 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 754 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 755 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 756 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 757 | |
| 758 | // Verify we can load the dex files from it. |
| 759 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 760 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 761 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 762 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 763 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 764 | EXPECT_EQ(1u, dex_files.size()); |
| 765 | } |
| 766 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 767 | // Case: We have a stripped (or resource-only) DEX file, no ODEX file and no |
| 768 | // OAT file. Expect: The status is kNoDexOptNeeded. |
| 769 | TEST_F(OatFileAssistantTest, ResourceOnlyDex) { |
| 770 | std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar"; |
| 771 | |
| 772 | Copy(GetStrippedDexSrc1(), dex_location); |
| 773 | |
| 774 | // Verify the status. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 775 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 776 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 777 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 778 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 779 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 780 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 781 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 782 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 783 | |
| 784 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 785 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 786 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 787 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 788 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 789 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 790 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 791 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 792 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 793 | |
| 794 | // Make the oat file up to date. This should have no effect. |
| 795 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 796 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 797 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 798 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 799 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 800 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 801 | |
| 802 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 803 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 804 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 805 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 806 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 807 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 808 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 809 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 810 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
| 811 | } |
| 812 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 813 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 814 | // relocation. |
| 815 | // Expect: The status is kSelfPatchOatNeeded. |
| 816 | TEST_F(OatFileAssistantTest, SelfRelocation) { |
| 817 | std::string dex_location = GetScratchDir() + "/SelfRelocation.jar"; |
| 818 | std::string oat_location = GetOdexDir() + "/SelfRelocation.oat"; |
| 819 | |
| 820 | // Create the dex and odex files |
| 821 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 822 | GenerateOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 823 | |
| 824 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 825 | oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 826 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 827 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 828 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kInterpretOnly)); |
| 829 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, |
| 830 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 831 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 832 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 833 | |
| 834 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 835 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 836 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 837 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 838 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 839 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 840 | EXPECT_TRUE(oat_file_assistant.OatFileNeedsRelocation()); |
| 841 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 842 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 843 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 844 | |
| 845 | // Make the oat file up to date. |
| 846 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 847 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 848 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg; |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 849 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 850 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 851 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 852 | |
| 853 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 854 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 855 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 856 | EXPECT_FALSE(oat_file_assistant.OdexFileNeedsRelocation()); |
| 857 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 858 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 859 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 860 | EXPECT_FALSE(oat_file_assistant.OatFileNeedsRelocation()); |
| 861 | EXPECT_TRUE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 862 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 863 | |
| 864 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 865 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 866 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 867 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 868 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 869 | EXPECT_EQ(1u, dex_files.size()); |
| 870 | } |
| 871 | |
Richard Uhler | 1c4eb04 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 872 | // Case: We have a DEX file, no ODEX file and an OAT file that needs |
| 873 | // relocation but doesn't have patch info. |
| 874 | // Expect: The status is kDex2OatNeeded, because we can't run patchoat. |
| 875 | TEST_F(OatFileAssistantTest, NoSelfRelocation) { |
| 876 | std::string dex_location = GetScratchDir() + "/NoSelfRelocation.jar"; |
| 877 | std::string oat_location = GetOdexDir() + "/NoSelfRelocation.oat"; |
| 878 | |
| 879 | // Create the dex and odex files |
| 880 | Copy(GetDexSrc1(), dex_location); |
| 881 | GenerateNoPatchOdexForTest(dex_location, oat_location, CompilerFilter::kSpeed); |
| 882 | |
| 883 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
| 884 | oat_location.c_str(), kRuntimeISA, false, true); |
| 885 | |
| 886 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 887 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 888 | |
| 889 | // Make the oat file up to date. |
| 890 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 891 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 892 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg; |
Richard Uhler | 1c4eb04 | 2016-03-29 13:27:41 -0700 | [diff] [blame] | 893 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 894 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 895 | |
| 896 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 897 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 898 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 899 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 900 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 901 | EXPECT_EQ(1u, dex_files.size()); |
| 902 | } |
| 903 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 904 | // Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and |
| 905 | // OAT files both have patch delta of 0. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 906 | // Expect: It shouldn't crash, and status is kPatchOatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 907 | TEST_F(OatFileAssistantTest, OdexOatOverlap) { |
| 908 | std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 909 | std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex"; |
| 910 | std::string oat_location = GetOdexDir() + "/OdexOatOverlap.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 911 | |
| 912 | // Create the dex and odex files |
| 913 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 914 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 915 | |
| 916 | // Create the oat file by copying the odex so they are located in the same |
| 917 | // place in memory. |
| 918 | Copy(odex_location, oat_location); |
| 919 | |
| 920 | // Verify things don't go bad. |
| 921 | OatFileAssistant oat_file_assistant(dex_location.c_str(), |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 922 | oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 923 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 924 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, |
| 925 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 926 | |
| 927 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 928 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 929 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 930 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 931 | EXPECT_TRUE(oat_file_assistant.OatFileExists()); |
| 932 | EXPECT_FALSE(oat_file_assistant.OatFileIsOutOfDate()); |
| 933 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 934 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 935 | |
| 936 | // Things aren't relocated, so it should fall back to interpreted. |
| 937 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 938 | ASSERT_TRUE(oat_file.get() != nullptr); |
Richard Uhler | f16d572 | 2015-05-11 09:32:47 -0700 | [diff] [blame] | 939 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 940 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 941 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 942 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 943 | EXPECT_EQ(1u, dex_files.size()); |
| 944 | } |
| 945 | |
| 946 | // 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] | 947 | // Expect: The status is kNoDexOptNeeded, because PIC needs no relocation. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 948 | TEST_F(OatFileAssistantTest, DexPicOdexNoOat) { |
| 949 | std::string dex_location = GetScratchDir() + "/DexPicOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 950 | std::string odex_location = GetOdexDir() + "/DexPicOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 951 | |
| 952 | // Create the dex and odex files |
| 953 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 954 | GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 955 | |
| 956 | // Verify the status. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 957 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 958 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 959 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 960 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
| 961 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 962 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 963 | |
| 964 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 965 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 966 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 967 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 968 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 969 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 970 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 971 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 974 | // Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file. |
| 975 | // Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code. |
| 976 | TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) { |
| 977 | std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar"; |
| 978 | std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex"; |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 979 | |
| 980 | // Create the dex and odex files |
| 981 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 982 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kVerifyAtRuntime); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 983 | |
| 984 | // Verify the status. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 985 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 986 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 987 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 988 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kVerifyAtRuntime)); |
| 989 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 990 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 991 | |
| 992 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 993 | EXPECT_TRUE(oat_file_assistant.OdexFileExists()); |
| 994 | EXPECT_FALSE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 995 | EXPECT_TRUE(oat_file_assistant.OdexFileIsUpToDate()); |
| 996 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 997 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 998 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 999 | EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles()); |
| 1000 | } |
| 1001 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1002 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 1003 | // Expect: We should load an executable dex file. |
| 1004 | TEST_F(OatFileAssistantTest, LoadOatUpToDate) { |
| 1005 | std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar"; |
| 1006 | |
| 1007 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1008 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1009 | |
| 1010 | // Load the oat using an oat file assistant. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1011 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
| 1012 | |
| 1013 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1014 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1015 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1016 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1017 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1018 | EXPECT_EQ(1u, dex_files.size()); |
| 1019 | } |
| 1020 | |
| 1021 | // Case: We have a DEX file and up-to-date interpret-only OAT file for it. |
| 1022 | // Expect: We should still load the oat file as executable. |
| 1023 | TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) { |
| 1024 | std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar"; |
| 1025 | |
| 1026 | Copy(GetDexSrc1(), dex_location); |
| 1027 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kInterpretOnly); |
| 1028 | |
| 1029 | // Load the oat using an oat file assistant. |
| 1030 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1031 | |
| 1032 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1033 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1034 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1035 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1036 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1037 | EXPECT_EQ(1u, dex_files.size()); |
| 1038 | } |
| 1039 | |
| 1040 | // Case: We have a DEX file and up-to-date OAT file for it. |
| 1041 | // Expect: Loading non-executable should load the oat non-executable. |
| 1042 | TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) { |
| 1043 | std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar"; |
| 1044 | |
| 1045 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1046 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1047 | |
| 1048 | // Load the oat using an oat file assistant. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1049 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1050 | |
| 1051 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1052 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1053 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1054 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1055 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1056 | EXPECT_EQ(1u, dex_files.size()); |
| 1057 | } |
| 1058 | |
| 1059 | // Case: We have a DEX file. |
| 1060 | // Expect: We should load an executable dex file from an alternative oat |
| 1061 | // location. |
| 1062 | TEST_F(OatFileAssistantTest, LoadDexNoAlternateOat) { |
| 1063 | std::string dex_location = GetScratchDir() + "/LoadDexNoAlternateOat.jar"; |
| 1064 | std::string oat_location = GetScratchDir() + "/LoadDexNoAlternateOat.oat"; |
| 1065 | |
| 1066 | Copy(GetDexSrc1(), dex_location); |
| 1067 | |
| 1068 | OatFileAssistant oat_file_assistant( |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1069 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1070 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1071 | ASSERT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 1072 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)) << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1073 | |
| 1074 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1075 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1076 | EXPECT_TRUE(oat_file->IsExecutable()); |
| 1077 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1078 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1079 | EXPECT_EQ(1u, dex_files.size()); |
| 1080 | |
| 1081 | EXPECT_TRUE(OS::FileExists(oat_location.c_str())); |
| 1082 | |
| 1083 | // Verify it didn't create an oat in the default location. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1084 | OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1085 | EXPECT_FALSE(ofm.OatFileExists()); |
| 1086 | } |
| 1087 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1088 | // Case: We have a DEX file but can't write the oat file. |
| 1089 | // Expect: We should fail to make the oat file up to date. |
| 1090 | TEST_F(OatFileAssistantTest, LoadDexUnwriteableAlternateOat) { |
| 1091 | std::string dex_location = GetScratchDir() + "/LoadDexUnwriteableAlternateOat.jar"; |
| 1092 | |
| 1093 | // Make the oat location unwritable by inserting some non-existent |
| 1094 | // intermediate directories. |
| 1095 | std::string oat_location = GetScratchDir() + "/foo/bar/LoadDexUnwriteableAlternateOat.oat"; |
| 1096 | |
| 1097 | Copy(GetDexSrc1(), dex_location); |
| 1098 | |
| 1099 | OatFileAssistant oat_file_assistant( |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1100 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1101 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1102 | ASSERT_EQ(OatFileAssistant::kUpdateNotAttempted, |
| 1103 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1104 | |
| 1105 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1106 | ASSERT_TRUE(oat_file.get() == nullptr); |
| 1107 | } |
| 1108 | |
| 1109 | // Case: We don't have a DEX file and can't write the oat file. |
| 1110 | // Expect: We should fail to generate the oat file without crashing. |
| 1111 | TEST_F(OatFileAssistantTest, GenNoDex) { |
| 1112 | std::string dex_location = GetScratchDir() + "/GenNoDex.jar"; |
| 1113 | std::string oat_location = GetScratchDir() + "/GenNoDex.oat"; |
| 1114 | |
| 1115 | OatFileAssistant oat_file_assistant( |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1116 | dex_location.c_str(), oat_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1117 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1118 | EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted, |
| 1119 | oat_file_assistant.GenerateOatFile(CompilerFilter::kSpeed, &error_msg)); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1122 | // Turn an absolute path into a path relative to the current working |
| 1123 | // directory. |
| 1124 | static std::string MakePathRelative(std::string target) { |
| 1125 | char buf[MAXPATHLEN]; |
| 1126 | std::string cwd = getcwd(buf, MAXPATHLEN); |
| 1127 | |
| 1128 | // Split the target and cwd paths into components. |
| 1129 | std::vector<std::string> target_path; |
| 1130 | std::vector<std::string> cwd_path; |
| 1131 | Split(target, '/', &target_path); |
| 1132 | Split(cwd, '/', &cwd_path); |
| 1133 | |
| 1134 | // Reverse the path components, so we can use pop_back(). |
| 1135 | std::reverse(target_path.begin(), target_path.end()); |
| 1136 | std::reverse(cwd_path.begin(), cwd_path.end()); |
| 1137 | |
| 1138 | // Drop the common prefix of the paths. Because we reversed the path |
| 1139 | // components, this becomes the common suffix of target_path and cwd_path. |
| 1140 | while (!target_path.empty() && !cwd_path.empty() |
| 1141 | && target_path.back() == cwd_path.back()) { |
| 1142 | target_path.pop_back(); |
| 1143 | cwd_path.pop_back(); |
| 1144 | } |
| 1145 | |
| 1146 | // For each element of the remaining cwd_path, add '..' to the beginning |
| 1147 | // of the target path. Because we reversed the path components, we add to |
| 1148 | // the end of target_path. |
| 1149 | for (unsigned int i = 0; i < cwd_path.size(); i++) { |
| 1150 | target_path.push_back(".."); |
| 1151 | } |
| 1152 | |
| 1153 | // Reverse again to get the right path order, and join to get the result. |
| 1154 | std::reverse(target_path.begin(), target_path.end()); |
| 1155 | return Join(target_path, '/'); |
| 1156 | } |
| 1157 | |
| 1158 | // Case: Non-absolute path to Dex location. |
| 1159 | // Expect: Not sure, but it shouldn't crash. |
| 1160 | TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) { |
| 1161 | std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar"; |
| 1162 | Copy(GetDexSrc1(), abs_dex_location); |
| 1163 | |
| 1164 | std::string dex_location = MakePathRelative(abs_dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1165 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1166 | |
| 1167 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1168 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1169 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1170 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1171 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1172 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1173 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1174 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1175 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1176 | } |
| 1177 | |
| 1178 | // Case: Very short, non-existent Dex location. |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1179 | // Expect: kNoDexOptNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1180 | TEST_F(OatFileAssistantTest, ShortDexLocation) { |
| 1181 | std::string dex_location = "/xx"; |
| 1182 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1183 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1184 | |
| 1185 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1186 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, |
| 1187 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1188 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1189 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1190 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1191 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1192 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1193 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1194 | EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1195 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1196 | // Trying to make it up to date should have no effect. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1197 | std::string error_msg; |
Richard Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 1198 | EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, |
| 1199 | oat_file_assistant.MakeUpToDate(CompilerFilter::kSpeed, &error_msg)); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 1200 | EXPECT_TRUE(error_msg.empty()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | // Case: Non-standard extension for dex file. |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 1204 | // Expect: The status is kDex2OatNeeded. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1205 | TEST_F(OatFileAssistantTest, LongDexExtension) { |
| 1206 | std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx"; |
| 1207 | Copy(GetDexSrc1(), dex_location); |
| 1208 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1209 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1210 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1211 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, |
| 1212 | oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1213 | |
| 1214 | EXPECT_FALSE(oat_file_assistant.IsInBootClassPath()); |
| 1215 | EXPECT_FALSE(oat_file_assistant.OdexFileExists()); |
| 1216 | EXPECT_TRUE(oat_file_assistant.OdexFileIsOutOfDate()); |
| 1217 | EXPECT_FALSE(oat_file_assistant.OdexFileIsUpToDate()); |
| 1218 | EXPECT_FALSE(oat_file_assistant.OatFileExists()); |
| 1219 | EXPECT_TRUE(oat_file_assistant.OatFileIsOutOfDate()); |
| 1220 | EXPECT_FALSE(oat_file_assistant.OatFileIsUpToDate()); |
| 1221 | } |
| 1222 | |
| 1223 | // A task to generate a dex location. Used by the RaceToGenerate test. |
| 1224 | class RaceGenerateTask : public Task { |
| 1225 | public: |
| 1226 | explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location) |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1227 | : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr) |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1228 | {} |
| 1229 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1230 | void Run(Thread* self ATTRIBUTE_UNUSED) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1231 | // Load the dex files, and save a pointer to the loaded oat file, so that |
| 1232 | // 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] | 1233 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1234 | std::vector<std::string> error_msgs; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1235 | const OatFile* oat_file = nullptr; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1236 | dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat( |
| 1237 | dex_location_.c_str(), |
| 1238 | oat_location_.c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1239 | /*class_loader*/nullptr, |
| 1240 | /*dex_elements*/nullptr, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1241 | &oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1242 | &error_msgs); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1243 | CHECK(!dex_files.empty()) << Join(error_msgs, '\n'); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1244 | CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation(); |
| 1245 | loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 1246 | CHECK_EQ(loaded_oat_file_, oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | const OatFile* GetLoadedOatFile() const { |
| 1250 | return loaded_oat_file_; |
| 1251 | } |
| 1252 | |
| 1253 | private: |
| 1254 | std::string dex_location_; |
| 1255 | std::string oat_location_; |
| 1256 | const OatFile* loaded_oat_file_; |
| 1257 | }; |
| 1258 | |
| 1259 | // Test the case where multiple processes race to generate an oat file. |
| 1260 | // This simulates multiple processes using multiple threads. |
| 1261 | // |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1262 | // We want unique Oat files to be loaded even when there is a race to load. |
| 1263 | // TODO: The test case no longer tests locking the way it was intended since we now get multiple |
| 1264 | // copies of the same Oat files mapped at different locations. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1265 | TEST_F(OatFileAssistantTest, RaceToGenerate) { |
| 1266 | std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1267 | std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1268 | |
| 1269 | // We use the lib core dex file, because it's large, and hopefully should |
| 1270 | // take a while to generate. |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 1271 | Copy(GetLibCoreDexFileNames()[0], dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1272 | |
| 1273 | const int kNumThreads = 32; |
| 1274 | Thread* self = Thread::Current(); |
| 1275 | ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads); |
| 1276 | std::vector<std::unique_ptr<RaceGenerateTask>> tasks; |
| 1277 | for (int i = 0; i < kNumThreads; i++) { |
| 1278 | std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location)); |
| 1279 | thread_pool.AddTask(self, task.get()); |
| 1280 | tasks.push_back(std::move(task)); |
| 1281 | } |
| 1282 | thread_pool.StartWorkers(self); |
| 1283 | thread_pool.Wait(self, true, false); |
| 1284 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1285 | // Verify every task got a unique oat file. |
| 1286 | std::set<const OatFile*> oat_files; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1287 | for (auto& task : tasks) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1288 | const OatFile* oat_file = task->GetLoadedOatFile(); |
| 1289 | EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end()); |
| 1290 | oat_files.insert(oat_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | // Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is |
| 1295 | // disabled. |
| 1296 | // Expect: We should load the odex file non-executable. |
| 1297 | TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) { |
| 1298 | std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1299 | std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1300 | |
| 1301 | // Create the dex and odex files |
| 1302 | Copy(GetDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1303 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1304 | |
| 1305 | // Load the oat using an executable oat file assistant. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1306 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1307 | |
| 1308 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1309 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1310 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1311 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1312 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1313 | EXPECT_EQ(1u, dex_files.size()); |
| 1314 | } |
| 1315 | |
| 1316 | // Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is |
| 1317 | // disabled. |
| 1318 | // Expect: We should load the odex file non-executable. |
| 1319 | TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) { |
| 1320 | std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar"; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1321 | std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1322 | |
| 1323 | // Create the dex and odex files |
| 1324 | Copy(GetMultiDexSrc1(), dex_location); |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1325 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1326 | |
| 1327 | // Load the oat using an executable oat file assistant. |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1328 | OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false, true); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1329 | |
| 1330 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1331 | ASSERT_TRUE(oat_file.get() != nullptr); |
| 1332 | EXPECT_FALSE(oat_file->IsExecutable()); |
| 1333 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1334 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str()); |
| 1335 | EXPECT_EQ(2u, dex_files.size()); |
| 1336 | } |
| 1337 | |
| 1338 | TEST(OatFileAssistantUtilsTest, DexFilenameToOdexFilename) { |
| 1339 | std::string error_msg; |
| 1340 | std::string odex_file; |
| 1341 | |
| 1342 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1343 | "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1344 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1345 | |
| 1346 | EXPECT_TRUE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1347 | "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg; |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 1348 | EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1349 | |
| 1350 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1351 | "nopath.jar", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1352 | EXPECT_FALSE(OatFileAssistant::DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1353 | "/foo/bar/baz_noext", kArm, &odex_file, &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1356 | // Verify the dexopt status values from dalvik.system.DexFile |
| 1357 | // match the OatFileAssistant::DexOptStatus values. |
| 1358 | TEST_F(OatFileAssistantTest, DexOptStatusValues) { |
| 1359 | ScopedObjectAccess soa(Thread::Current()); |
| 1360 | StackHandleScope<1> hs(soa.Self()); |
| 1361 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1362 | Handle<mirror::Class> dexfile( |
| 1363 | hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;"))); |
| 1364 | ASSERT_FALSE(dexfile.Get() == nullptr); |
| 1365 | linker->EnsureInitialized(soa.Self(), dexfile, true, true); |
| 1366 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1367 | ArtField* no_dexopt_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1368 | soa.Self(), dexfile, "NO_DEXOPT_NEEDED", "I"); |
| 1369 | ASSERT_FALSE(no_dexopt_needed == nullptr); |
| 1370 | EXPECT_EQ(no_dexopt_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1371 | EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded, no_dexopt_needed->GetInt(dexfile.Get())); |
| 1372 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1373 | ArtField* dex2oat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1374 | soa.Self(), dexfile, "DEX2OAT_NEEDED", "I"); |
| 1375 | ASSERT_FALSE(dex2oat_needed == nullptr); |
| 1376 | EXPECT_EQ(dex2oat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1377 | EXPECT_EQ(OatFileAssistant::kDex2OatNeeded, dex2oat_needed->GetInt(dexfile.Get())); |
| 1378 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1379 | ArtField* patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1380 | soa.Self(), dexfile, "PATCHOAT_NEEDED", "I"); |
| 1381 | ASSERT_FALSE(patchoat_needed == nullptr); |
| 1382 | EXPECT_EQ(patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1383 | EXPECT_EQ(OatFileAssistant::kPatchOatNeeded, patchoat_needed->GetInt(dexfile.Get())); |
| 1384 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1385 | ArtField* self_patchoat_needed = mirror::Class::FindStaticField( |
Richard Uhler | 23cedd2 | 2015-04-08 13:17:29 -0700 | [diff] [blame] | 1386 | soa.Self(), dexfile, "SELF_PATCHOAT_NEEDED", "I"); |
| 1387 | ASSERT_FALSE(self_patchoat_needed == nullptr); |
| 1388 | EXPECT_EQ(self_patchoat_needed->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
| 1389 | EXPECT_EQ(OatFileAssistant::kSelfPatchOatNeeded, self_patchoat_needed->GetInt(dexfile.Get())); |
| 1390 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1391 | |
| 1392 | // TODO: More Tests: |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1393 | // * Image checksum change is out of date for kIntepretOnly, but not |
| 1394 | // kVerifyAtRuntime. But target of kVerifyAtRuntime still says current |
| 1395 | // kInterpretOnly is out of date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1396 | // * Test class linker falls back to unquickened dex for DexNoOat |
| 1397 | // * Test class linker falls back to unquickened dex for MultiDexNoOat |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1398 | // * Test using secondary isa |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1399 | // * Test for status of oat while oat is being generated (how?) |
| 1400 | // * Test case where 32 and 64 bit boot class paths differ, |
| 1401 | // and we ask IsInBootClassPath for a class in exactly one of the 32 or |
| 1402 | // 64 bit boot class paths. |
| 1403 | // * Test unexpected scenarios (?): |
| 1404 | // - Dex is stripped, don't have odex. |
| 1405 | // - Oat file corrupted after status check, before reload unexecutable |
| 1406 | // because it's unrelocated and no dex2oat |
Calin Juravle | d91b8a2 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 1407 | // * Test unrelocated specific target compilation type can be relocated to |
| 1408 | // make it up to date. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1409 | |
| 1410 | } // namespace art |