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