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