Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 17 | #include <regex> |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 18 | #include <sstream> |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 21 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 27 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 28 | #include "common_runtime_test.h" |
| 29 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 30 | #include "base/macros.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 31 | #include "base/mutex-inl.h" |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 32 | #include "bytecode_utils.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 33 | #include "dex/art_dex_file_loader.h" |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 34 | #include "dex/base64_test_util.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 35 | #include "dex/code_item_accessors-inl.h" |
| 36 | #include "dex/dex_file-inl.h" |
| 37 | #include "dex/dex_file_loader.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 38 | #include "dex2oat_environment_test.h" |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 39 | #include "dex2oat_return_codes.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 40 | #include "jit/profile_compilation_info.h" |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 41 | #include "oat.h" |
| 42 | #include "oat_file.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 43 | #include "utils.h" |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 44 | #include "vdex_file.h" |
| 45 | #include "ziparchive/zip_writer.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 46 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 47 | namespace art { |
| 48 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 49 | static constexpr size_t kMaxMethodIds = 65535; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 50 | static constexpr bool kDebugArgs = false; |
Mathieu Chartier | 0212910 | 2017-12-22 11:04:01 -0800 | [diff] [blame] | 51 | static const char* kDisableCompactDex = "--compact-dex-level=none"; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 52 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 53 | using android::base::StringPrintf; |
| 54 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 55 | class Dex2oatTest : public Dex2oatEnvironmentTest { |
| 56 | public: |
| 57 | virtual void TearDown() OVERRIDE { |
| 58 | Dex2oatEnvironmentTest::TearDown(); |
| 59 | |
| 60 | output_ = ""; |
| 61 | error_msg_ = ""; |
| 62 | success_ = false; |
| 63 | } |
| 64 | |
| 65 | protected: |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 66 | int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations, |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 67 | const std::string& odex_location, |
| 68 | CompilerFilter::Filter filter, |
| 69 | std::string* error_msg, |
| 70 | const std::vector<std::string>& extra_args = {}, |
| 71 | bool use_fd = false) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 72 | std::unique_ptr<File> oat_file; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 73 | std::vector<std::string> args; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 74 | // Add dex file args. |
| 75 | for (const std::string& dex_location : dex_locations) { |
| 76 | args.push_back("--dex-file=" + dex_location); |
| 77 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 78 | if (use_fd) { |
| 79 | oat_file.reset(OS::CreateEmptyFile(odex_location.c_str())); |
| 80 | CHECK(oat_file != nullptr) << odex_location; |
| 81 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 82 | args.push_back("--oat-location=" + odex_location); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 83 | } else { |
| 84 | args.push_back("--oat-file=" + odex_location); |
| 85 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 86 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 87 | args.push_back("--runtime-arg"); |
| 88 | args.push_back("-Xnorelocate"); |
| 89 | |
| 90 | args.insert(args.end(), extra_args.begin(), extra_args.end()); |
| 91 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 92 | int status = Dex2Oat(args, error_msg); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 93 | if (oat_file != nullptr) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 94 | CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file"; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 95 | } |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 96 | return status; |
| 97 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 99 | void GenerateOdexForTest( |
| 100 | const std::string& dex_location, |
| 101 | const std::string& odex_location, |
| 102 | CompilerFilter::Filter filter, |
| 103 | const std::vector<std::string>& extra_args = {}, |
| 104 | bool expect_success = true, |
| 105 | bool use_fd = false) { |
| 106 | GenerateOdexForTest(dex_location, |
| 107 | odex_location, |
| 108 | filter, |
| 109 | extra_args, |
| 110 | expect_success, |
| 111 | use_fd, |
| 112 | [](const OatFile&) {}); |
| 113 | } |
| 114 | |
Andreas Gampe | 80ddf27 | 2018-01-11 09:41:00 -0800 | [diff] [blame] | 115 | bool test_accepts_odex_file_on_failure = false; |
| 116 | |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 117 | template <typename T> |
| 118 | void GenerateOdexForTest( |
| 119 | const std::string& dex_location, |
| 120 | const std::string& odex_location, |
| 121 | CompilerFilter::Filter filter, |
| 122 | const std::vector<std::string>& extra_args, |
| 123 | bool expect_success, |
| 124 | bool use_fd, |
| 125 | T check_oat) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 126 | std::string error_msg; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 127 | int status = GenerateOdexForTestWithStatus({dex_location}, |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 128 | odex_location, |
| 129 | filter, |
| 130 | &error_msg, |
| 131 | extra_args, |
| 132 | use_fd); |
Andreas Gampe | 80ddf27 | 2018-01-11 09:41:00 -0800 | [diff] [blame] | 133 | bool success = (WIFEXITED(status) && WEXITSTATUS(status) == 0); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 134 | if (expect_success) { |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 135 | ASSERT_TRUE(success) << error_msg << std::endl << output_; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 136 | |
| 137 | // Verify the odex file was generated as expected. |
| 138 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 139 | odex_location.c_str(), |
| 140 | nullptr, |
| 141 | nullptr, |
| 142 | false, |
| 143 | /*low_4gb*/false, |
| 144 | dex_location.c_str(), |
| 145 | &error_msg)); |
| 146 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 147 | |
| 148 | CheckFilter(filter, odex_file->GetCompilerFilter()); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 149 | check_oat(*(odex_file.get())); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 150 | } else { |
| 151 | ASSERT_FALSE(success) << output_; |
| 152 | |
| 153 | error_msg_ = error_msg; |
| 154 | |
Andreas Gampe | 80ddf27 | 2018-01-11 09:41:00 -0800 | [diff] [blame] | 155 | if (!test_accepts_odex_file_on_failure) { |
| 156 | // Verify there's no loadable odex file. |
| 157 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 158 | odex_location.c_str(), |
| 159 | nullptr, |
| 160 | nullptr, |
| 161 | false, |
| 162 | /*low_4gb*/false, |
| 163 | dex_location.c_str(), |
| 164 | &error_msg)); |
| 165 | ASSERT_TRUE(odex_file.get() == nullptr); |
| 166 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Calin Juravle | 1ccf613 | 2017-08-02 17:46:53 -0700 | [diff] [blame] | 170 | // Check the input compiler filter against the generated oat file's filter. May be overridden |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 171 | // in subclasses when equality is not expected. |
| 172 | virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) { |
| 173 | EXPECT_EQ(expected, actual); |
| 174 | } |
| 175 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 176 | int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 177 | Runtime* runtime = Runtime::Current(); |
| 178 | |
| 179 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 180 | runtime->GetHeap()->GetBootImageSpaces(); |
| 181 | if (image_spaces.empty()) { |
| 182 | *error_msg = "No image location found for Dex2Oat."; |
| 183 | return false; |
| 184 | } |
| 185 | std::string image_location = image_spaces[0]->GetImageLocation(); |
| 186 | |
| 187 | std::vector<std::string> argv; |
| 188 | argv.push_back(runtime->GetCompilerExecutable()); |
Calin Juravle | 1ccf613 | 2017-08-02 17:46:53 -0700 | [diff] [blame] | 189 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 190 | if (runtime->IsJavaDebuggable()) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 191 | argv.push_back("--debuggable"); |
| 192 | } |
| 193 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 194 | |
| 195 | if (!runtime->IsVerificationEnabled()) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 196 | argv.push_back("--compiler-filter=assume-verified"); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (runtime->MustRelocateIfPossible()) { |
| 200 | argv.push_back("--runtime-arg"); |
| 201 | argv.push_back("-Xrelocate"); |
| 202 | } else { |
| 203 | argv.push_back("--runtime-arg"); |
| 204 | argv.push_back("-Xnorelocate"); |
| 205 | } |
| 206 | |
| 207 | if (!kIsTargetBuild) { |
| 208 | argv.push_back("--host"); |
| 209 | } |
| 210 | |
| 211 | argv.push_back("--boot-image=" + image_location); |
| 212 | |
| 213 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 214 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 215 | |
| 216 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 217 | |
| 218 | // We must set --android-root. |
| 219 | const char* android_root = getenv("ANDROID_ROOT"); |
| 220 | CHECK(android_root != nullptr); |
| 221 | argv.push_back("--android-root=" + std::string(android_root)); |
| 222 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 223 | if (kDebugArgs) { |
| 224 | std::string all_args; |
| 225 | for (const std::string& arg : argv) { |
| 226 | all_args += arg + " "; |
| 227 | } |
| 228 | LOG(ERROR) << all_args; |
| 229 | } |
| 230 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 231 | int link[2]; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 232 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 233 | if (pipe(link) == -1) { |
| 234 | return false; |
| 235 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 236 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 237 | pid_t pid = fork(); |
| 238 | if (pid == -1) { |
| 239 | return false; |
| 240 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 241 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 242 | if (pid == 0) { |
| 243 | // We need dex2oat to actually log things. |
| 244 | setenv("ANDROID_LOG_TAGS", "*:d", 1); |
| 245 | dup2(link[1], STDERR_FILENO); |
| 246 | close(link[0]); |
| 247 | close(link[1]); |
| 248 | std::vector<const char*> c_args; |
| 249 | for (const std::string& str : argv) { |
| 250 | c_args.push_back(str.c_str()); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 251 | } |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 252 | c_args.push_back(nullptr); |
| 253 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 254 | exit(1); |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 255 | UNREACHABLE(); |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 256 | } else { |
| 257 | close(link[1]); |
| 258 | char buffer[128]; |
| 259 | memset(buffer, 0, 128); |
| 260 | ssize_t bytes_read = 0; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 261 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 262 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) { |
| 263 | output_ += std::string(buffer, bytes_read); |
| 264 | } |
| 265 | close(link[0]); |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 266 | int status = -1; |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 267 | if (waitpid(pid, &status, 0) != -1) { |
| 268 | success_ = (status == 0); |
| 269 | } |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 270 | return status; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 271 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | std::string output_ = ""; |
| 275 | std::string error_msg_ = ""; |
| 276 | bool success_ = false; |
| 277 | }; |
| 278 | |
| 279 | class Dex2oatSwapTest : public Dex2oatTest { |
| 280 | protected: |
| 281 | void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) { |
| 282 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 283 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 284 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 285 | Copy(GetTestDexFileName(), dex_location); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 286 | |
| 287 | std::vector<std::string> copy(extra_args); |
| 288 | |
| 289 | std::unique_ptr<ScratchFile> sf; |
| 290 | if (use_fd) { |
| 291 | sf.reset(new ScratchFile()); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 292 | copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd())); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 293 | } else { |
| 294 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 295 | copy.push_back("--swap-file=" + swap_location); |
| 296 | } |
| 297 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy); |
| 298 | |
| 299 | CheckValidity(); |
| 300 | ASSERT_TRUE(success_); |
| 301 | CheckResult(expect_use); |
| 302 | } |
| 303 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 304 | virtual std::string GetTestDexFileName() { |
Vladimir Marko | 1535770 | 2017-02-09 10:37:31 +0000 | [diff] [blame] | 305 | return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps"); |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | virtual void CheckResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 309 | if (kIsTargetBuild) { |
| 310 | CheckTargetResult(expect_use); |
| 311 | } else { |
| 312 | CheckHostResult(expect_use); |
| 313 | } |
| 314 | } |
| 315 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 316 | virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 317 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 318 | // something for variants with file descriptor where we can control the lifetime of |
| 319 | // the swap file and thus take a look at it. |
| 320 | } |
| 321 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 322 | virtual void CheckHostResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 323 | if (!kIsTargetBuild) { |
| 324 | if (expect_use) { |
| 325 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 326 | << output_; |
| 327 | } else { |
| 328 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 329 | << output_; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Check whether the dex2oat run was really successful. |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 335 | virtual void CheckValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 336 | if (kIsTargetBuild) { |
| 337 | CheckTargetValidity(); |
| 338 | } else { |
| 339 | CheckHostValidity(); |
| 340 | } |
| 341 | } |
| 342 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 343 | virtual void CheckTargetValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 344 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 345 | // something for variants with file descriptor where we can control the lifetime of |
| 346 | // the swap file and thus take a look at it. |
| 347 | } |
| 348 | |
| 349 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 350 | virtual void CheckHostValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 351 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 352 | } |
| 353 | }; |
| 354 | |
| 355 | TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) { |
| 356 | RunTest(false /* use_fd */, false /* expect_use */); |
| 357 | RunTest(true /* use_fd */, false /* expect_use */); |
| 358 | } |
| 359 | |
| 360 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) { |
| 361 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 362 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 363 | } |
| 364 | |
| 365 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) { |
| 366 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 367 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 368 | } |
| 369 | |
| 370 | TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) { |
| 371 | RunTest(false /* use_fd */, |
| 372 | true /* expect_use */, |
| 373 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 374 | RunTest(true /* use_fd */, |
| 375 | true /* expect_use */, |
| 376 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 377 | } |
| 378 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 379 | class Dex2oatSwapUseTest : public Dex2oatSwapTest { |
| 380 | protected: |
| 381 | void CheckHostResult(bool expect_use) OVERRIDE { |
| 382 | if (!kIsTargetBuild) { |
| 383 | if (expect_use) { |
| 384 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 385 | << output_; |
| 386 | } else { |
| 387 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 388 | << output_; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | std::string GetTestDexFileName() OVERRIDE { |
| 394 | // Use Statics as it has a handful of functions. |
| 395 | return CommonRuntimeTest::GetTestDexFileName("Statics"); |
| 396 | } |
| 397 | |
| 398 | void GrabResult1() { |
| 399 | if (!kIsTargetBuild) { |
| 400 | native_alloc_1_ = ParseNativeAlloc(); |
| 401 | swap_1_ = ParseSwap(false /* expected */); |
| 402 | } else { |
| 403 | native_alloc_1_ = std::numeric_limits<size_t>::max(); |
| 404 | swap_1_ = 0; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void GrabResult2() { |
| 409 | if (!kIsTargetBuild) { |
| 410 | native_alloc_2_ = ParseNativeAlloc(); |
| 411 | swap_2_ = ParseSwap(true /* expected */); |
| 412 | } else { |
| 413 | native_alloc_2_ = 0; |
| 414 | swap_2_ = std::numeric_limits<size_t>::max(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private: |
| 419 | size_t ParseNativeAlloc() { |
| 420 | std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)"); |
| 421 | std::smatch native_alloc_match; |
| 422 | bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex); |
| 423 | if (!found) { |
| 424 | EXPECT_TRUE(found); |
| 425 | return 0; |
| 426 | } |
| 427 | if (native_alloc_match.size() != 2U) { |
| 428 | EXPECT_EQ(native_alloc_match.size(), 2U); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | std::istringstream stream(native_alloc_match[1].str()); |
| 433 | size_t value; |
| 434 | stream >> value; |
| 435 | |
| 436 | return value; |
| 437 | } |
| 438 | |
| 439 | size_t ParseSwap(bool expected) { |
| 440 | std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)"); |
| 441 | std::smatch swap_match; |
| 442 | bool found = std::regex_search(output_, swap_match, swap_regex); |
| 443 | if (found != expected) { |
| 444 | EXPECT_EQ(expected, found); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | if (!found) { |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | if (swap_match.size() != 2U) { |
| 453 | EXPECT_EQ(swap_match.size(), 2U); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | std::istringstream stream(swap_match[1].str()); |
| 458 | size_t value; |
| 459 | stream >> value; |
| 460 | |
| 461 | return value; |
| 462 | } |
| 463 | |
| 464 | protected: |
| 465 | size_t native_alloc_1_; |
| 466 | size_t native_alloc_2_; |
| 467 | |
| 468 | size_t swap_1_; |
| 469 | size_t swap_2_; |
| 470 | }; |
| 471 | |
| 472 | TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) { |
Andreas Gampe | f4a67fd | 2017-05-04 09:55:36 -0700 | [diff] [blame] | 473 | // Native memory usage isn't correctly tracked under sanitization. |
| 474 | TEST_DISABLED_FOR_MEMORY_TOOL_ASAN(); |
| 475 | |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 476 | // The `native_alloc_2_ >= native_alloc_1_` assertion below may not |
Roland Levillain | 19772bf | 2017-02-16 11:28:10 +0000 | [diff] [blame] | 477 | // hold true on some x86 systems; disable this test while we |
| 478 | // investigate (b/29259363). |
| 479 | TEST_DISABLED_FOR_X86(); |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 480 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 481 | RunTest(false /* use_fd */, |
| 482 | false /* expect_use */); |
| 483 | GrabResult1(); |
| 484 | std::string output_1 = output_; |
| 485 | |
| 486 | output_ = ""; |
| 487 | |
| 488 | RunTest(false /* use_fd */, |
| 489 | true /* expect_use */, |
| 490 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 491 | GrabResult2(); |
| 492 | std::string output_2 = output_; |
| 493 | |
| 494 | if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) { |
| 495 | EXPECT_LT(native_alloc_2_, native_alloc_1_); |
| 496 | EXPECT_LT(swap_1_, swap_2_); |
| 497 | |
| 498 | LOG(ERROR) << output_1; |
| 499 | LOG(ERROR) << output_2; |
| 500 | } |
| 501 | } |
| 502 | |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 503 | class Dex2oatVeryLargeTest : public Dex2oatTest { |
| 504 | protected: |
| 505 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 506 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 507 | // Ignore, we'll do our own checks. |
| 508 | } |
| 509 | |
| 510 | void RunTest(CompilerFilter::Filter filter, |
| 511 | bool expect_large, |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 512 | bool expect_downgrade, |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 513 | const std::vector<std::string>& extra_args = {}) { |
| 514 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 515 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 516 | std::string app_image_file = GetScratchDir() + "/Test.art"; |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 517 | |
| 518 | Copy(GetDexSrc1(), dex_location); |
| 519 | |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 520 | std::vector<std::string> new_args(extra_args); |
| 521 | new_args.push_back("--app-image-file=" + app_image_file); |
| 522 | GenerateOdexForTest(dex_location, odex_location, filter, new_args); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 523 | |
| 524 | CheckValidity(); |
| 525 | ASSERT_TRUE(success_); |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 526 | CheckResult(dex_location, |
| 527 | odex_location, |
| 528 | app_image_file, |
| 529 | filter, |
| 530 | expect_large, |
| 531 | expect_downgrade); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void CheckResult(const std::string& dex_location, |
| 535 | const std::string& odex_location, |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 536 | const std::string& app_image_file, |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 537 | CompilerFilter::Filter filter, |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 538 | bool expect_large, |
| 539 | bool expect_downgrade) { |
| 540 | if (expect_downgrade) { |
| 541 | EXPECT_TRUE(expect_large); |
| 542 | } |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 543 | // Host/target independent checks. |
| 544 | std::string error_msg; |
| 545 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 546 | odex_location.c_str(), |
| 547 | nullptr, |
| 548 | nullptr, |
| 549 | false, |
| 550 | /*low_4gb*/false, |
| 551 | dex_location.c_str(), |
| 552 | &error_msg)); |
| 553 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 554 | EXPECT_GT(app_image_file.length(), 0u); |
| 555 | std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str())); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 556 | if (expect_large) { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 557 | // Note: we cannot check the following |
| 558 | // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter())); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 559 | // The reason is that the filter override currently happens when the dex files are |
| 560 | // loaded in dex2oat, which is after the oat file has been started. Thus, the header |
| 561 | // store cannot be changed, and the original filter is set in stone. |
| 562 | |
| 563 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 564 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 565 | ASSERT_TRUE(dex_file != nullptr); |
| 566 | uint32_t class_def_count = dex_file->NumClassDefs(); |
| 567 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
| 568 | for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) { |
| 569 | OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 570 | EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // If the input filter was "below," it should have been used. |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 575 | if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) { |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 576 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 577 | } |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 578 | |
| 579 | // If expect large, make sure the app image isn't generated or is empty. |
| 580 | if (file != nullptr) { |
| 581 | EXPECT_EQ(file->GetLength(), 0u); |
| 582 | } |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 583 | } else { |
| 584 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 585 | ASSERT_TRUE(file != nullptr) << app_image_file; |
| 586 | EXPECT_GT(file->GetLength(), 0u); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Host/target dependent checks. |
| 590 | if (kIsTargetBuild) { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 591 | CheckTargetResult(expect_downgrade); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 592 | } else { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 593 | CheckHostResult(expect_downgrade); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 597 | void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) { |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 598 | // TODO: Ignore for now. May do something for fd things. |
| 599 | } |
| 600 | |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 601 | void CheckHostResult(bool expect_downgrade) { |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 602 | if (!kIsTargetBuild) { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 603 | if (expect_downgrade) { |
| 604 | EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_; |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 605 | } else { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 606 | EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_; |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // Check whether the dex2oat run was really successful. |
| 612 | void CheckValidity() { |
| 613 | if (kIsTargetBuild) { |
| 614 | CheckTargetValidity(); |
| 615 | } else { |
| 616 | CheckHostValidity(); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void CheckTargetValidity() { |
| 621 | // TODO: Ignore for now. |
| 622 | } |
| 623 | |
| 624 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 625 | void CheckHostValidity() { |
| 626 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 627 | } |
| 628 | }; |
| 629 | |
| 630 | TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 631 | RunTest(CompilerFilter::kAssumeVerified, false, false); |
| 632 | RunTest(CompilerFilter::kExtract, false, false); |
| 633 | RunTest(CompilerFilter::kQuicken, false, false); |
| 634 | RunTest(CompilerFilter::kSpeed, false, false); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 635 | |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 636 | RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" }); |
| 637 | RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" }); |
| 638 | RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" }); |
| 639 | RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" }); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) { |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 643 | RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" }); |
| 644 | RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" }); |
| 645 | RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" }); |
| 646 | RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" }); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Mathieu Chartier | 97ab5e3 | 2017-02-22 13:35:44 -0800 | [diff] [blame] | 649 | // Regressin test for b/35665292. |
| 650 | TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) { |
| 651 | // Test that dex2oat doesn't crash with speed-profile but no input profile. |
Mathieu Chartier | 8cce65a | 2017-08-17 00:06:39 -0700 | [diff] [blame] | 652 | RunTest(CompilerFilter::kSpeedProfile, false, false); |
Mathieu Chartier | 97ab5e3 | 2017-02-22 13:35:44 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 655 | class Dex2oatLayoutTest : public Dex2oatTest { |
| 656 | protected: |
| 657 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 658 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 659 | // Ignore, we'll do our own checks. |
| 660 | } |
| 661 | |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 662 | // Emits a profile with a single dex file with the given location and a single class index of 1. |
| 663 | void GenerateProfile(const std::string& test_profile, |
| 664 | const std::string& dex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 665 | size_t num_classes, |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 666 | uint32_t checksum) { |
| 667 | int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 668 | CHECK_GE(profile_test_fd, 0); |
| 669 | |
| 670 | ProfileCompilationInfo info; |
| 671 | std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 672 | for (size_t i = 0; i < num_classes; ++i) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 673 | info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 674 | } |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 675 | bool result = info.Save(profile_test_fd); |
| 676 | close(profile_test_fd); |
| 677 | ASSERT_TRUE(result); |
| 678 | } |
| 679 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 680 | void CompileProfileOdex(const std::string& dex_location, |
| 681 | const std::string& odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 682 | const std::string& app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 683 | bool use_fd, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 684 | size_t num_profile_classes, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 685 | const std::vector<std::string>& extra_args = {}, |
| 686 | bool expect_success = true) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 687 | const std::string profile_location = GetScratchDir() + "/primary.prof"; |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 688 | const char* location = dex_location.c_str(); |
| 689 | std::string error_msg; |
| 690 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 691 | const ArtDexFileLoader dex_file_loader; |
| 692 | ASSERT_TRUE(dex_file_loader.Open( |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 693 | location, location, /* verify */ true, /* verify_checksum */ true, &error_msg, &dex_files)); |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 694 | EXPECT_EQ(dex_files.size(), 1U); |
| 695 | std::unique_ptr<const DexFile>& dex_file = dex_files[0]; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 696 | GenerateProfile(profile_location, |
| 697 | dex_location, |
| 698 | num_profile_classes, |
| 699 | dex_file->GetLocationChecksum()); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 700 | std::vector<std::string> copy(extra_args); |
| 701 | copy.push_back("--profile-file=" + profile_location); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 702 | std::unique_ptr<File> app_image_file; |
| 703 | if (!app_image_file_name.empty()) { |
| 704 | if (use_fd) { |
| 705 | app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str())); |
| 706 | copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd())); |
| 707 | } else { |
| 708 | copy.push_back("--app-image-file=" + app_image_file_name); |
| 709 | } |
| 710 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 711 | GenerateOdexForTest(dex_location, |
| 712 | odex_location, |
| 713 | CompilerFilter::kSpeedProfile, |
| 714 | copy, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 715 | expect_success, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 716 | use_fd); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 717 | if (app_image_file != nullptr) { |
| 718 | ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file"; |
| 719 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 720 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 721 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 722 | uint64_t GetImageObjectSectionSize(const std::string& image_file_name) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 723 | EXPECT_FALSE(image_file_name.empty()); |
| 724 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str())); |
| 725 | CHECK(file != nullptr); |
| 726 | ImageHeader image_header; |
| 727 | const bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 728 | CHECK(success); |
| 729 | CHECK(image_header.IsValid()); |
| 730 | ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 731 | return image_header.GetObjectsSection().Size(); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | void RunTest(bool app_image) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 735 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 736 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 737 | std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): ""; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 738 | Copy(GetDexSrc2(), dex_location); |
| 739 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 740 | uint64_t image_file_empty_profile = 0; |
| 741 | if (app_image) { |
| 742 | CompileProfileOdex(dex_location, |
| 743 | odex_location, |
| 744 | app_image_file, |
| 745 | /* use_fd */ false, |
| 746 | /* num_profile_classes */ 0); |
| 747 | CheckValidity(); |
| 748 | ASSERT_TRUE(success_); |
| 749 | // Don't check the result since CheckResult relies on the class being in the profile. |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 750 | image_file_empty_profile = GetImageObjectSectionSize(app_image_file); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 751 | EXPECT_GT(image_file_empty_profile, 0u); |
| 752 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 753 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 754 | // Small profile. |
| 755 | CompileProfileOdex(dex_location, |
| 756 | odex_location, |
| 757 | app_image_file, |
| 758 | /* use_fd */ false, |
| 759 | /* num_profile_classes */ 1); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 760 | CheckValidity(); |
| 761 | ASSERT_TRUE(success_); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 762 | CheckResult(dex_location, odex_location, app_image_file); |
| 763 | |
| 764 | if (app_image) { |
| 765 | // Test that the profile made a difference by adding more classes. |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 766 | const uint64_t image_file_small_profile = GetImageObjectSectionSize(app_image_file); |
| 767 | ASSERT_LT(image_file_empty_profile, image_file_small_profile); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 768 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 769 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 770 | |
| 771 | void RunTestVDex() { |
| 772 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 773 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 774 | std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex"; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 775 | std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art"; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 776 | Copy(GetDexSrc2(), dex_location); |
| 777 | |
| 778 | std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str())); |
| 779 | CHECK(vdex_file1 != nullptr) << vdex_location; |
| 780 | ScratchFile vdex_file2; |
| 781 | { |
| 782 | std::string input_vdex = "--input-vdex-fd=-1"; |
| 783 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 784 | CompileProfileOdex(dex_location, |
| 785 | odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 786 | app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 787 | /* use_fd */ true, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 788 | /* num_profile_classes */ 1, |
Mathieu Chartier | f160983 | 2018-01-31 03:09:56 -0800 | [diff] [blame] | 789 | { input_vdex, output_vdex }); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 790 | EXPECT_GT(vdex_file1->GetLength(), 0u); |
| 791 | } |
| 792 | { |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 793 | // Test that vdex and dexlayout fail gracefully. |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 794 | std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd()); |
| 795 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd()); |
| 796 | CompileProfileOdex(dex_location, |
| 797 | odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 798 | app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 799 | /* use_fd */ true, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 800 | /* num_profile_classes */ 1, |
Mathieu Chartier | d27923c | 2018-02-08 21:00:03 -0800 | [diff] [blame] | 801 | { input_vdex, output_vdex }, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 802 | /* expect_success */ true); |
| 803 | EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 804 | } |
| 805 | ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 806 | CheckValidity(); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 807 | ASSERT_TRUE(success_); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 810 | void CheckResult(const std::string& dex_location, |
| 811 | const std::string& odex_location, |
| 812 | const std::string& app_image_file_name) { |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 813 | // Host/target independent checks. |
| 814 | std::string error_msg; |
| 815 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 816 | odex_location.c_str(), |
| 817 | nullptr, |
| 818 | nullptr, |
| 819 | false, |
| 820 | /*low_4gb*/false, |
| 821 | dex_location.c_str(), |
| 822 | &error_msg)); |
| 823 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 824 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 825 | const char* location = dex_location.c_str(); |
| 826 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 827 | const ArtDexFileLoader dex_file_loader; |
| 828 | ASSERT_TRUE(dex_file_loader.Open( |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 829 | location, location, /* verify */ true, /* verify_checksum */ true, &error_msg, &dex_files)); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 830 | EXPECT_EQ(dex_files.size(), 1U); |
| 831 | std::unique_ptr<const DexFile>& old_dex_file = dex_files[0]; |
| 832 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 833 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 834 | std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 835 | ASSERT_TRUE(new_dex_file != nullptr); |
| 836 | uint32_t class_def_count = new_dex_file->NumClassDefs(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 837 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 838 | ASSERT_GE(class_def_count, 2U); |
| 839 | |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 840 | // Make sure the indexes stay the same. |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 841 | std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_); |
| 842 | std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_); |
| 843 | std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_); |
| 844 | std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_); |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 845 | EXPECT_EQ(old_class0, new_class0); |
| 846 | EXPECT_EQ(old_class1, new_class1); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Jeff Hao | c155b05 | 2017-01-17 17:43:29 -0800 | [diff] [blame] | 849 | EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 850 | |
| 851 | if (!app_image_file_name.empty()) { |
| 852 | // Go peek at the image header to make sure it was large enough to contain the class. |
| 853 | std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str())); |
| 854 | ImageHeader image_header; |
| 855 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 856 | ASSERT_TRUE(success); |
| 857 | ASSERT_TRUE(image_header.IsValid()); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 858 | EXPECT_GT(image_header.GetObjectsSection().Size(), 0u); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 859 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 862 | // Check whether the dex2oat run was really successful. |
| 863 | void CheckValidity() { |
| 864 | if (kIsTargetBuild) { |
| 865 | CheckTargetValidity(); |
| 866 | } else { |
| 867 | CheckHostValidity(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 868 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 869 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 870 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 871 | void CheckTargetValidity() { |
| 872 | // TODO: Ignore for now. |
| 873 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 874 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 875 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 876 | void CheckHostValidity() { |
| 877 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 878 | } |
| 879 | }; |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 880 | |
| 881 | TEST_F(Dex2oatLayoutTest, TestLayout) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 882 | RunTest(/* app-image */ false); |
| 883 | } |
| 884 | |
| 885 | TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) { |
| 886 | RunTest(/* app-image */ true); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 889 | TEST_F(Dex2oatLayoutTest, TestVdexLayout) { |
| 890 | RunTestVDex(); |
| 891 | } |
| 892 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 893 | class Dex2oatUnquickenTest : public Dex2oatTest { |
| 894 | protected: |
| 895 | void RunUnquickenMultiDex() { |
| 896 | std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar"; |
| 897 | std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex"; |
| 898 | std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex"; |
| 899 | Copy(GetTestDexFileName("MultiDex"), dex_location); |
| 900 | |
| 901 | std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str())); |
| 902 | CHECK(vdex_file1 != nullptr) << vdex_location; |
| 903 | // Quicken the dex file into a vdex file. |
| 904 | { |
| 905 | std::string input_vdex = "--input-vdex-fd=-1"; |
| 906 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 907 | GenerateOdexForTest(dex_location, |
| 908 | odex_location, |
| 909 | CompilerFilter::kQuicken, |
Mathieu Chartier | f160983 | 2018-01-31 03:09:56 -0800 | [diff] [blame] | 910 | { input_vdex, output_vdex }, |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 911 | /* expect_success */ true, |
| 912 | /* use_fd */ true); |
| 913 | EXPECT_GT(vdex_file1->GetLength(), 0u); |
| 914 | } |
| 915 | // Unquicken by running the verify compiler filter on the vdex file. |
| 916 | { |
| 917 | std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd()); |
| 918 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 919 | GenerateOdexForTest(dex_location, |
| 920 | odex_location, |
| 921 | CompilerFilter::kVerify, |
Mathieu Chartier | 0212910 | 2017-12-22 11:04:01 -0800 | [diff] [blame] | 922 | { input_vdex, output_vdex, kDisableCompactDex }, |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 923 | /* expect_success */ true, |
| 924 | /* use_fd */ true); |
| 925 | } |
| 926 | ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 927 | CheckResult(dex_location, odex_location); |
| 928 | ASSERT_TRUE(success_); |
| 929 | } |
| 930 | |
Mathieu Chartier | d27923c | 2018-02-08 21:00:03 -0800 | [diff] [blame] | 931 | void RunUnquickenMultiDexCDex() { |
| 932 | std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar"; |
| 933 | std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex"; |
| 934 | std::string odex_location2 = GetOdexDir() + "/UnquickenMultiDex2.odex"; |
| 935 | std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex"; |
| 936 | std::string vdex_location2 = GetOdexDir() + "/UnquickenMultiDex2.vdex"; |
| 937 | Copy(GetTestDexFileName("MultiDex"), dex_location); |
| 938 | |
| 939 | std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str())); |
| 940 | std::unique_ptr<File> vdex_file2(OS::CreateEmptyFile(vdex_location2.c_str())); |
| 941 | CHECK(vdex_file1 != nullptr) << vdex_location; |
| 942 | CHECK(vdex_file2 != nullptr) << vdex_location2; |
| 943 | |
| 944 | // Quicken the dex file into a vdex file. |
| 945 | { |
| 946 | std::string input_vdex = "--input-vdex-fd=-1"; |
| 947 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 948 | GenerateOdexForTest(dex_location, |
| 949 | odex_location, |
| 950 | CompilerFilter::kQuicken, |
| 951 | { input_vdex, output_vdex, "--compact-dex-level=fast"}, |
| 952 | /* expect_success */ true, |
| 953 | /* use_fd */ true); |
| 954 | EXPECT_GT(vdex_file1->GetLength(), 0u); |
| 955 | } |
| 956 | |
| 957 | // Unquicken by running the verify compiler filter on the vdex file. |
| 958 | { |
| 959 | std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd()); |
| 960 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2->Fd()); |
| 961 | GenerateOdexForTest(dex_location, |
| 962 | odex_location2, |
| 963 | CompilerFilter::kVerify, |
| 964 | { input_vdex, output_vdex, "--compact-dex-level=none"}, |
| 965 | /* expect_success */ true, |
| 966 | /* use_fd */ true); |
| 967 | } |
| 968 | ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 969 | ASSERT_EQ(vdex_file2->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 970 | CheckResult(dex_location, odex_location2); |
| 971 | ASSERT_TRUE(success_); |
| 972 | } |
| 973 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 974 | void CheckResult(const std::string& dex_location, const std::string& odex_location) { |
| 975 | std::string error_msg; |
| 976 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 977 | odex_location.c_str(), |
| 978 | nullptr, |
| 979 | nullptr, |
| 980 | false, |
| 981 | /*low_4gb*/false, |
| 982 | dex_location.c_str(), |
| 983 | &error_msg)); |
| 984 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 985 | ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u); |
| 986 | |
| 987 | // Iterate over the dex files and ensure there is no quickened instruction. |
| 988 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 989 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 990 | for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 991 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 992 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 993 | if (class_data != nullptr) { |
| 994 | for (ClassDataItemIterator class_it(*dex_file, class_data); |
| 995 | class_it.HasNext(); |
| 996 | class_it.Next()) { |
| 997 | if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) { |
Mathieu Chartier | 0021feb | 2017-11-07 00:08:52 -0800 | [diff] [blame] | 998 | for (const DexInstructionPcPair& inst : |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 999 | CodeItemInstructionAccessor(*dex_file, class_it.GetMethodCodeItem())) { |
Mathieu Chartier | 0212910 | 2017-12-22 11:04:01 -0800 | [diff] [blame] | 1000 | ASSERT_FALSE(inst->IsQuickened()) << output_; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | }; |
| 1009 | |
| 1010 | TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) { |
| 1011 | RunUnquickenMultiDex(); |
| 1012 | } |
| 1013 | |
Mathieu Chartier | d27923c | 2018-02-08 21:00:03 -0800 | [diff] [blame] | 1014 | TEST_F(Dex2oatUnquickenTest, UnquickenMultiDexCDex) { |
| 1015 | RunUnquickenMultiDexCDex(); |
| 1016 | } |
| 1017 | |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 1018 | class Dex2oatWatchdogTest : public Dex2oatTest { |
| 1019 | protected: |
| 1020 | void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) { |
| 1021 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 1022 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 1023 | |
| 1024 | Copy(GetTestDexFileName(), dex_location); |
| 1025 | |
| 1026 | std::vector<std::string> copy(extra_args); |
| 1027 | |
| 1028 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 1029 | copy.push_back("--swap-file=" + swap_location); |
Andreas Gampe | 22fa376 | 2017-10-23 20:58:12 -0700 | [diff] [blame] | 1030 | copy.push_back("-j512"); // Excessive idle threads just slow down dex2oat. |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 1031 | GenerateOdexForTest(dex_location, |
| 1032 | odex_location, |
| 1033 | CompilerFilter::kSpeed, |
| 1034 | copy, |
| 1035 | expect_success); |
| 1036 | } |
| 1037 | |
| 1038 | std::string GetTestDexFileName() { |
| 1039 | return GetDexSrc1(); |
| 1040 | } |
| 1041 | }; |
| 1042 | |
| 1043 | TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) { |
| 1044 | // Check with default. |
| 1045 | RunTest(true); |
| 1046 | |
| 1047 | // Check with ten minutes. |
| 1048 | RunTest(true, { "--watchdog-timeout=600000" }); |
| 1049 | } |
| 1050 | |
| 1051 | TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) { |
Roland Levillain | 68db225 | 2017-08-14 12:48:47 +0100 | [diff] [blame] | 1052 | TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624 |
Andreas Gampe | 80ddf27 | 2018-01-11 09:41:00 -0800 | [diff] [blame] | 1053 | |
| 1054 | // The watchdog is independent of dex2oat and will not delete intermediates. It is possible |
| 1055 | // that the compilation succeeds and the file is completely written by the time the watchdog |
| 1056 | // kills dex2oat (but the dex2oat threads must have been scheduled pretty badly). |
| 1057 | test_accepts_odex_file_on_failure = true; |
| 1058 | |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 1059 | // Check with ten milliseconds. |
| 1060 | RunTest(false, { "--watchdog-timeout=10" }); |
| 1061 | } |
| 1062 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1063 | class Dex2oatReturnCodeTest : public Dex2oatTest { |
| 1064 | protected: |
| 1065 | int RunTest(const std::vector<std::string>& extra_args = {}) { |
| 1066 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 1067 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 1068 | |
| 1069 | Copy(GetTestDexFileName(), dex_location); |
| 1070 | |
| 1071 | std::string error_msg; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 1072 | return GenerateOdexForTestWithStatus({dex_location}, |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1073 | odex_location, |
| 1074 | CompilerFilter::kSpeed, |
| 1075 | &error_msg, |
| 1076 | extra_args); |
| 1077 | } |
| 1078 | |
| 1079 | std::string GetTestDexFileName() { |
| 1080 | return GetDexSrc1(); |
| 1081 | } |
| 1082 | }; |
| 1083 | |
| 1084 | TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) { |
Andreas Gampe | fd80b17 | 2017-04-26 22:25:31 -0700 | [diff] [blame] | 1085 | TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793 |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1086 | int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" }); |
| 1087 | EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_; |
| 1088 | } |
| 1089 | |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1090 | class Dex2oatClassLoaderContextTest : public Dex2oatTest { |
| 1091 | protected: |
| 1092 | void RunTest(const char* class_loader_context, |
| 1093 | const char* expected_classpath_key, |
| 1094 | bool expected_success, |
| 1095 | bool use_second_source = false) { |
| 1096 | std::string dex_location = GetUsedDexLocation(); |
| 1097 | std::string odex_location = GetUsedOatLocation(); |
| 1098 | |
| 1099 | Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location); |
| 1100 | |
| 1101 | std::string error_msg; |
| 1102 | std::vector<std::string> extra_args; |
| 1103 | if (class_loader_context != nullptr) { |
| 1104 | extra_args.push_back(std::string("--class-loader-context=") + class_loader_context); |
| 1105 | } |
| 1106 | auto check_oat = [expected_classpath_key](const OatFile& oat_file) { |
| 1107 | ASSERT_TRUE(expected_classpath_key != nullptr); |
| 1108 | const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey); |
| 1109 | ASSERT_TRUE(classpath != nullptr); |
| 1110 | ASSERT_STREQ(expected_classpath_key, classpath); |
| 1111 | }; |
| 1112 | |
| 1113 | GenerateOdexForTest(dex_location, |
| 1114 | odex_location, |
| 1115 | CompilerFilter::kQuicken, |
| 1116 | extra_args, |
| 1117 | expected_success, |
| 1118 | /*use_fd*/ false, |
| 1119 | check_oat); |
| 1120 | } |
| 1121 | |
| 1122 | std::string GetUsedDexLocation() { |
| 1123 | return GetScratchDir() + "/Context.jar"; |
| 1124 | } |
| 1125 | |
| 1126 | std::string GetUsedOatLocation() { |
| 1127 | return GetOdexDir() + "/Context.odex"; |
| 1128 | } |
| 1129 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1130 | const char* kEmptyClassPathKey = "PCL[]"; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1131 | }; |
| 1132 | |
| 1133 | TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) { |
| 1134 | RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false); |
| 1135 | } |
| 1136 | |
| 1137 | TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) { |
| 1138 | RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true); |
| 1139 | } |
| 1140 | |
| 1141 | TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) { |
| 1142 | RunTest(OatFile::kSpecialSharedLibrary, |
| 1143 | OatFile::kSpecialSharedLibrary, |
| 1144 | /*expected_success*/ true); |
| 1145 | } |
| 1146 | |
| 1147 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) { |
| 1148 | std::string context = "PCL[" + GetUsedDexLocation() + "]"; |
| 1149 | RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true); |
| 1150 | } |
| 1151 | |
| 1152 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) { |
| 1153 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested"); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1154 | |
| 1155 | std::string context = "PCL[" + dex_files[0]->GetLocation() + "]"; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1156 | std::string expected_classpath_key = "PCL[" + |
| 1157 | dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]"; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1158 | RunTest(context.c_str(), expected_classpath_key.c_str(), true); |
| 1159 | } |
| 1160 | |
| 1161 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) { |
| 1162 | std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar"; |
| 1163 | Copy(GetStrippedDexSrc1(), stripped_classpath); |
| 1164 | |
| 1165 | std::string context = "PCL[" + stripped_classpath + "]"; |
| 1166 | // Expect an empty context because stripped dex files cannot be open. |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1167 | RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) { |
| 1171 | std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar"; |
| 1172 | std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex"; |
| 1173 | |
| 1174 | Copy(GetDexSrc1(), stripped_classpath); |
| 1175 | |
| 1176 | GenerateOdexForTest(stripped_classpath, |
| 1177 | odex_for_classpath, |
| 1178 | CompilerFilter::kQuicken, |
| 1179 | {}, |
| 1180 | true); |
| 1181 | |
| 1182 | // Strip the dex file |
| 1183 | Copy(GetStrippedDexSrc1(), stripped_classpath); |
| 1184 | |
| 1185 | std::string context = "PCL[" + stripped_classpath + "]"; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1186 | std::string expected_classpath_key; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1187 | { |
| 1188 | // Open the oat file to get the expected classpath. |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 1189 | OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false, false); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1190 | std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile()); |
| 1191 | std::vector<std::unique_ptr<const DexFile>> oat_dex_files = |
| 1192 | OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str()); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1193 | expected_classpath_key = "PCL["; |
| 1194 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 1195 | if (i > 0) { |
| 1196 | expected_classpath_key + ":"; |
| 1197 | } |
| 1198 | expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" + |
| 1199 | std::to_string(oat_dex_files[i]->GetLocationChecksum()); |
| 1200 | } |
| 1201 | expected_classpath_key += "]"; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | RunTest(context.c_str(), |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1205 | expected_classpath_key.c_str(), |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1206 | /*expected_success*/ true, |
| 1207 | /*use_second_source*/ true); |
| 1208 | } |
| 1209 | |
| 1210 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) { |
| 1211 | std::string context = "PCL[does_not_exists.dex]"; |
| 1212 | // Expect an empty context because stripped dex files cannot be open. |
| 1213 | RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true); |
| 1214 | } |
| 1215 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 1216 | TEST_F(Dex2oatClassLoaderContextTest, ChainContext) { |
| 1217 | std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested"); |
| 1218 | std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex"); |
| 1219 | |
| 1220 | std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" + |
| 1221 | "DLC[" + GetTestDexFileName("MultiDex") + "]"; |
| 1222 | std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" + |
| 1223 | "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]"; |
| 1224 | |
| 1225 | RunTest(context.c_str(), expected_classpath_key.c_str(), true); |
| 1226 | } |
| 1227 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 1228 | class Dex2oatDeterminism : public Dex2oatTest {}; |
| 1229 | |
| 1230 | TEST_F(Dex2oatDeterminism, UnloadCompile) { |
| 1231 | if (!kUseReadBarrier && |
| 1232 | gc::kCollectorTypeDefault != gc::kCollectorTypeCMS && |
| 1233 | gc::kCollectorTypeDefault != gc::kCollectorTypeMS) { |
| 1234 | LOG(INFO) << "Test requires determinism support."; |
| 1235 | return; |
| 1236 | } |
| 1237 | Runtime* const runtime = Runtime::Current(); |
| 1238 | std::string out_dir = GetScratchDir(); |
| 1239 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1240 | const std::string base_vdex_name = out_dir + "/base.vdex"; |
| 1241 | const std::string unload_oat_name = out_dir + "/unload.oat"; |
| 1242 | const std::string unload_vdex_name = out_dir + "/unload.vdex"; |
| 1243 | const std::string no_unload_oat_name = out_dir + "/nounload.oat"; |
| 1244 | const std::string no_unload_vdex_name = out_dir + "/nounload.vdex"; |
| 1245 | const std::string app_image_name = out_dir + "/unload.art"; |
| 1246 | std::string error_msg; |
| 1247 | const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces(); |
| 1248 | ASSERT_GT(spaces.size(), 0u); |
| 1249 | const std::string image_location = spaces[0]->GetImageLocation(); |
| 1250 | // Without passing in an app image, it will unload in between compilations. |
| 1251 | const int res = GenerateOdexForTestWithStatus( |
| 1252 | GetLibCoreDexFileNames(), |
| 1253 | base_oat_name, |
| 1254 | CompilerFilter::Filter::kQuicken, |
| 1255 | &error_msg, |
| 1256 | {"--force-determinism", "--avoid-storing-invocation"}); |
| 1257 | EXPECT_EQ(res, 0); |
| 1258 | Copy(base_oat_name, unload_oat_name); |
| 1259 | Copy(base_vdex_name, unload_vdex_name); |
| 1260 | std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str())); |
| 1261 | std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str())); |
| 1262 | ASSERT_TRUE(unload_oat != nullptr); |
| 1263 | ASSERT_TRUE(unload_vdex != nullptr); |
| 1264 | EXPECT_GT(unload_oat->GetLength(), 0u); |
| 1265 | EXPECT_GT(unload_vdex->GetLength(), 0u); |
| 1266 | // Regenerate with an app image to disable the dex2oat unloading and verify that the output is |
| 1267 | // the same. |
| 1268 | const int res2 = GenerateOdexForTestWithStatus( |
| 1269 | GetLibCoreDexFileNames(), |
| 1270 | base_oat_name, |
| 1271 | CompilerFilter::Filter::kQuicken, |
| 1272 | &error_msg, |
| 1273 | {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name}); |
| 1274 | EXPECT_EQ(res2, 0); |
| 1275 | Copy(base_oat_name, no_unload_oat_name); |
| 1276 | Copy(base_vdex_name, no_unload_vdex_name); |
| 1277 | std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str())); |
| 1278 | std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str())); |
| 1279 | ASSERT_TRUE(no_unload_oat != nullptr); |
| 1280 | ASSERT_TRUE(no_unload_vdex != nullptr); |
| 1281 | EXPECT_GT(no_unload_oat->GetLength(), 0u); |
| 1282 | EXPECT_GT(no_unload_vdex->GetLength(), 0u); |
| 1283 | // Verify that both of the files are the same (odex and vdex). |
| 1284 | EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength()); |
| 1285 | EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength()); |
| 1286 | EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0) |
| 1287 | << unload_oat_name << " " << no_unload_oat_name; |
| 1288 | EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0) |
| 1289 | << unload_vdex_name << " " << no_unload_vdex_name; |
| 1290 | // App image file. |
| 1291 | std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str())); |
| 1292 | ASSERT_TRUE(app_image_file != nullptr); |
| 1293 | EXPECT_GT(app_image_file->GetLength(), 0u); |
| 1294 | } |
| 1295 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1296 | // Test that dexlayout section info is correctly written to the oat file for profile based |
| 1297 | // compilation. |
| 1298 | TEST_F(Dex2oatTest, LayoutSections) { |
| 1299 | using Hotness = ProfileCompilationInfo::MethodHotness; |
| 1300 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods")); |
| 1301 | ScratchFile profile_file; |
| 1302 | // We can only layout method indices with code items, figure out which ones have this property |
| 1303 | // first. |
| 1304 | std::vector<uint16_t> methods; |
| 1305 | { |
| 1306 | const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;"); |
| 1307 | dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id); |
| 1308 | const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx); |
| 1309 | ClassDataItemIterator it(*dex, dex->GetClassData(*class_def)); |
| 1310 | it.SkipAllFields(); |
| 1311 | std::set<size_t> code_item_offsets; |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 1312 | for (; it.HasNextMethod(); it.Next()) { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1313 | const uint16_t method_idx = it.GetMemberIndex(); |
| 1314 | const size_t code_item_offset = it.GetMethodCodeItemOffset(); |
| 1315 | if (code_item_offsets.insert(code_item_offset).second) { |
| 1316 | // Unique code item, add the method index. |
| 1317 | methods.push_back(method_idx); |
| 1318 | } |
| 1319 | } |
| 1320 | DCHECK(!it.HasNext()); |
| 1321 | } |
| 1322 | ASSERT_GE(methods.size(), 8u); |
| 1323 | std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]}; |
| 1324 | std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]}; |
| 1325 | std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]}; |
| 1326 | // Here, we build the profile from the method lists. |
| 1327 | ProfileCompilationInfo info; |
| 1328 | info.AddMethodsForDex( |
| 1329 | static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup), |
| 1330 | dex.get(), |
| 1331 | hot_methods.begin(), |
| 1332 | hot_methods.end()); |
| 1333 | info.AddMethodsForDex( |
| 1334 | Hotness::kFlagStartup, |
| 1335 | dex.get(), |
| 1336 | startup_methods.begin(), |
| 1337 | startup_methods.end()); |
| 1338 | info.AddMethodsForDex( |
| 1339 | Hotness::kFlagPostStartup, |
| 1340 | dex.get(), |
| 1341 | post_methods.begin(), |
| 1342 | post_methods.end()); |
| 1343 | for (uint16_t id : hot_methods) { |
| 1344 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot()); |
| 1345 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup()); |
| 1346 | } |
| 1347 | for (uint16_t id : startup_methods) { |
| 1348 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup()); |
| 1349 | } |
| 1350 | for (uint16_t id : post_methods) { |
| 1351 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup()); |
| 1352 | } |
| 1353 | // Save the profile since we want to use it with dex2oat to produce an oat file. |
| 1354 | ASSERT_TRUE(info.Save(profile_file.GetFd())); |
| 1355 | // Generate a profile based odex. |
| 1356 | const std::string dir = GetScratchDir(); |
| 1357 | const std::string oat_filename = dir + "/base.oat"; |
| 1358 | const std::string vdex_filename = dir + "/base.vdex"; |
| 1359 | std::string error_msg; |
| 1360 | const int res = GenerateOdexForTestWithStatus( |
| 1361 | {dex->GetLocation()}, |
| 1362 | oat_filename, |
| 1363 | CompilerFilter::Filter::kQuicken, |
| 1364 | &error_msg, |
| 1365 | {"--profile-file=" + profile_file.GetFilename()}); |
| 1366 | EXPECT_EQ(res, 0); |
| 1367 | |
| 1368 | // Open our generated oat file. |
| 1369 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(), |
| 1370 | oat_filename.c_str(), |
| 1371 | nullptr, |
| 1372 | nullptr, |
| 1373 | false, |
| 1374 | /*low_4gb*/false, |
| 1375 | dex->GetLocation().c_str(), |
| 1376 | &error_msg)); |
| 1377 | ASSERT_TRUE(odex_file != nullptr); |
| 1378 | std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles(); |
| 1379 | ASSERT_EQ(oat_dex_files.size(), 1u); |
| 1380 | // Check that the code sections match what we expect. |
| 1381 | for (const OatDexFile* oat_dex : oat_dex_files) { |
| 1382 | const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections(); |
| 1383 | // Testing of logging the sections. |
| 1384 | ASSERT_TRUE(sections != nullptr); |
| 1385 | LOG(INFO) << *sections; |
| 1386 | |
| 1387 | // Load the sections into temporary variables for convenience. |
| 1388 | const DexLayoutSection& code_section = |
| 1389 | sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)]; |
| 1390 | const DexLayoutSection::Subsection& section_hot_code = |
| 1391 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)]; |
| 1392 | const DexLayoutSection::Subsection& section_sometimes_used = |
| 1393 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)]; |
| 1394 | const DexLayoutSection::Subsection& section_startup_only = |
| 1395 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)]; |
| 1396 | const DexLayoutSection::Subsection& section_unused = |
| 1397 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)]; |
| 1398 | |
| 1399 | // All the sections should be non-empty. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1400 | EXPECT_GT(section_hot_code.Size(), 0u); |
| 1401 | EXPECT_GT(section_sometimes_used.Size(), 0u); |
| 1402 | EXPECT_GT(section_startup_only.Size(), 0u); |
| 1403 | EXPECT_GT(section_unused.Size(), 0u); |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1404 | |
| 1405 | // Open the dex file since we need to peek at the code items to verify the layout matches what |
| 1406 | // we expect. |
| 1407 | std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg)); |
| 1408 | ASSERT_TRUE(dex_file != nullptr) << error_msg; |
| 1409 | const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;"); |
| 1410 | ASSERT_TRUE(type_id != nullptr); |
| 1411 | dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 1412 | const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx); |
| 1413 | ASSERT_TRUE(class_def != nullptr); |
| 1414 | |
| 1415 | // Count how many code items are for each category, there should be at least one per category. |
| 1416 | size_t hot_count = 0; |
| 1417 | size_t post_startup_count = 0; |
| 1418 | size_t startup_count = 0; |
| 1419 | size_t unused_count = 0; |
| 1420 | // Visit all of the methdos of the main class and cross reference the method indices to their |
| 1421 | // corresponding code item offsets to verify the layout. |
| 1422 | ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def)); |
| 1423 | it.SkipAllFields(); |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 1424 | for (; it.HasNextMethod(); it.Next()) { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1425 | const size_t method_idx = it.GetMemberIndex(); |
| 1426 | const size_t code_item_offset = it.GetMethodCodeItemOffset(); |
| 1427 | const bool is_hot = ContainsElement(hot_methods, method_idx); |
| 1428 | const bool is_startup = ContainsElement(startup_methods, method_idx); |
| 1429 | const bool is_post_startup = ContainsElement(post_methods, method_idx); |
| 1430 | if (is_hot) { |
| 1431 | // Hot is highest precedence, check that the hot methods are in the hot section. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1432 | EXPECT_TRUE(section_hot_code.Contains(code_item_offset)); |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1433 | ++hot_count; |
| 1434 | } else if (is_post_startup) { |
| 1435 | // Post startup is sometimes used section. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1436 | EXPECT_TRUE(section_sometimes_used.Contains(code_item_offset)); |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1437 | ++post_startup_count; |
| 1438 | } else if (is_startup) { |
| 1439 | // Startup at this point means not hot or post startup, these must be startup only then. |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1440 | EXPECT_TRUE(section_startup_only.Contains(code_item_offset)); |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1441 | ++startup_count; |
| 1442 | } else { |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 1443 | if (section_unused.Contains(code_item_offset)) { |
Alan Leung | 9595fd3 | 2017-10-17 17:08:19 -0700 | [diff] [blame] | 1444 | // If no flags are set, the method should be unused ... |
| 1445 | ++unused_count; |
| 1446 | } else { |
| 1447 | // or this method is part of the last code item and the end is 4 byte aligned. |
| 1448 | ClassDataItemIterator it2(*dex_file, dex_file->GetClassData(*class_def)); |
| 1449 | it2.SkipAllFields(); |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 1450 | for (; it2.HasNextMethod(); it2.Next()) { |
Alan Leung | 9595fd3 | 2017-10-17 17:08:19 -0700 | [diff] [blame] | 1451 | EXPECT_LE(it2.GetMethodCodeItemOffset(), code_item_offset); |
| 1452 | } |
| 1453 | uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx); |
| 1454 | EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u); |
| 1455 | } |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | DCHECK(!it.HasNext()); |
| 1459 | EXPECT_GT(hot_count, 0u); |
| 1460 | EXPECT_GT(post_startup_count, 0u); |
| 1461 | EXPECT_GT(startup_count, 0u); |
| 1462 | EXPECT_GT(unused_count, 0u); |
| 1463 | } |
| 1464 | } |
| 1465 | |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 1466 | // Test that generating compact dex works. |
| 1467 | TEST_F(Dex2oatTest, GenerateCompactDex) { |
| 1468 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods")); |
| 1469 | // Generate a compact dex based odex. |
| 1470 | const std::string dir = GetScratchDir(); |
| 1471 | const std::string oat_filename = dir + "/base.oat"; |
| 1472 | const std::string vdex_filename = dir + "/base.vdex"; |
| 1473 | std::string error_msg; |
| 1474 | const int res = GenerateOdexForTestWithStatus( |
| 1475 | {dex->GetLocation()}, |
| 1476 | oat_filename, |
| 1477 | CompilerFilter::Filter::kQuicken, |
| 1478 | &error_msg, |
| 1479 | {"--compact-dex-level=fast"}); |
| 1480 | EXPECT_EQ(res, 0); |
| 1481 | // Open our generated oat file. |
| 1482 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(), |
| 1483 | oat_filename.c_str(), |
| 1484 | nullptr, |
| 1485 | nullptr, |
| 1486 | false, |
| 1487 | /*low_4gb*/false, |
| 1488 | dex->GetLocation().c_str(), |
| 1489 | &error_msg)); |
| 1490 | ASSERT_TRUE(odex_file != nullptr); |
| 1491 | std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles(); |
| 1492 | ASSERT_EQ(oat_dex_files.size(), 1u); |
| 1493 | // Check that each dex is a compact dex. |
| 1494 | for (const OatDexFile* oat_dex : oat_dex_files) { |
| 1495 | std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg)); |
| 1496 | ASSERT_TRUE(dex_file != nullptr) << error_msg; |
| 1497 | ASSERT_TRUE(dex_file->IsCompactDexFile()); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Andreas Gampe | f39208f | 2017-10-19 15:06:59 -0700 | [diff] [blame] | 1501 | class Dex2oatVerifierAbort : public Dex2oatTest {}; |
| 1502 | |
| 1503 | TEST_F(Dex2oatVerifierAbort, HardFail) { |
| 1504 | // Use VerifierDeps as it has hard-failing classes. |
| 1505 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps")); |
| 1506 | std::string out_dir = GetScratchDir(); |
| 1507 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1508 | std::string error_msg; |
| 1509 | const int res_fail = GenerateOdexForTestWithStatus( |
| 1510 | {dex->GetLocation()}, |
| 1511 | base_oat_name, |
| 1512 | CompilerFilter::Filter::kQuicken, |
| 1513 | &error_msg, |
| 1514 | {"--abort-on-hard-verifier-error"}); |
| 1515 | EXPECT_NE(0, res_fail); |
| 1516 | |
| 1517 | const int res_no_fail = GenerateOdexForTestWithStatus( |
| 1518 | {dex->GetLocation()}, |
| 1519 | base_oat_name, |
| 1520 | CompilerFilter::Filter::kQuicken, |
| 1521 | &error_msg, |
| 1522 | {"--no-abort-on-hard-verifier-error"}); |
| 1523 | EXPECT_EQ(0, res_no_fail); |
| 1524 | } |
| 1525 | |
| 1526 | TEST_F(Dex2oatVerifierAbort, SoftFail) { |
| 1527 | // Use VerifierDepsMulti as it has hard-failing classes. |
| 1528 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDepsMulti")); |
| 1529 | std::string out_dir = GetScratchDir(); |
| 1530 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1531 | std::string error_msg; |
| 1532 | const int res_fail = GenerateOdexForTestWithStatus( |
| 1533 | {dex->GetLocation()}, |
| 1534 | base_oat_name, |
| 1535 | CompilerFilter::Filter::kQuicken, |
| 1536 | &error_msg, |
| 1537 | {"--abort-on-soft-verifier-error"}); |
| 1538 | EXPECT_NE(0, res_fail); |
| 1539 | |
| 1540 | const int res_no_fail = GenerateOdexForTestWithStatus( |
| 1541 | {dex->GetLocation()}, |
| 1542 | base_oat_name, |
| 1543 | CompilerFilter::Filter::kQuicken, |
| 1544 | &error_msg, |
| 1545 | {"--no-abort-on-soft-verifier-error"}); |
| 1546 | EXPECT_EQ(0, res_no_fail); |
| 1547 | } |
| 1548 | |
Andreas Gampe | cac31ad | 2017-11-06 20:01:17 -0800 | [diff] [blame] | 1549 | class Dex2oatDedupeCode : public Dex2oatTest {}; |
| 1550 | |
| 1551 | TEST_F(Dex2oatDedupeCode, DedupeTest) { |
| 1552 | // Use MyClassNatives. It has lots of native methods that will produce deduplicate-able code. |
| 1553 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("MyClassNatives")); |
| 1554 | std::string out_dir = GetScratchDir(); |
| 1555 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1556 | size_t no_dedupe_size = 0; |
| 1557 | GenerateOdexForTest(dex->GetLocation(), |
| 1558 | base_oat_name, |
| 1559 | CompilerFilter::Filter::kSpeed, |
| 1560 | { "--deduplicate-code=false" }, |
| 1561 | true, // expect_success |
| 1562 | false, // use_fd |
| 1563 | [&no_dedupe_size](const OatFile& o) { |
| 1564 | no_dedupe_size = o.Size(); |
| 1565 | }); |
| 1566 | |
| 1567 | size_t dedupe_size = 0; |
| 1568 | GenerateOdexForTest(dex->GetLocation(), |
| 1569 | base_oat_name, |
| 1570 | CompilerFilter::Filter::kSpeed, |
| 1571 | { "--deduplicate-code=true" }, |
| 1572 | true, // expect_success |
| 1573 | false, // use_fd |
| 1574 | [&dedupe_size](const OatFile& o) { |
| 1575 | dedupe_size = o.Size(); |
| 1576 | }); |
| 1577 | |
| 1578 | EXPECT_LT(dedupe_size, no_dedupe_size); |
| 1579 | } |
| 1580 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 1581 | TEST_F(Dex2oatTest, UncompressedTest) { |
| 1582 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("MainUncompressed")); |
| 1583 | std::string out_dir = GetScratchDir(); |
| 1584 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1585 | GenerateOdexForTest(dex->GetLocation(), |
| 1586 | base_oat_name, |
| 1587 | CompilerFilter::Filter::kQuicken, |
| 1588 | { }, |
| 1589 | true, // expect_success |
| 1590 | false, // use_fd |
| 1591 | [](const OatFile& o) { |
| 1592 | CHECK(!o.ContainsDexCode()); |
| 1593 | }); |
| 1594 | } |
| 1595 | |
Mathieu Chartier | 700a985 | 2018-02-06 18:27:38 -0800 | [diff] [blame] | 1596 | TEST_F(Dex2oatTest, EmptyUncompressedDexTest) { |
| 1597 | std::string out_dir = GetScratchDir(); |
| 1598 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1599 | std::string error_msg; |
| 1600 | int status = GenerateOdexForTestWithStatus( |
| 1601 | { GetTestDexFileName("MainEmptyUncompressed") }, |
| 1602 | base_oat_name, |
| 1603 | CompilerFilter::Filter::kQuicken, |
| 1604 | &error_msg, |
| 1605 | { }, |
| 1606 | /*use_fd*/ false); |
| 1607 | // Expect to fail with code 1 and not SIGSEGV or SIGABRT. |
| 1608 | ASSERT_TRUE(WIFEXITED(status)); |
| 1609 | ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg; |
| 1610 | } |
| 1611 | |
Mathieu Chartier | 05f90d1 | 2018-02-07 13:47:17 -0800 | [diff] [blame] | 1612 | // Dex file that has duplicate methods have different code items and debug info. |
| 1613 | static const char kDuplicateMethodInputDex[] = |
| 1614 | "ZGV4CjAzOQDEy8VPdj4qHpgPYFWtLCtOykfFP4kB8tGYDAAAcAAAAHhWNBIAAAAAAAAAANALAABI" |
| 1615 | "AAAAcAAAAA4AAACQAQAABQAAAMgBAAANAAAABAIAABkAAABsAgAABAAAADQDAADgCAAAuAMAADgI" |
| 1616 | "AABCCAAASggAAE8IAABcCAAAaggAAHkIAACICAAAlggAAKQIAACyCAAAwAgAAM4IAADcCAAA6ggA" |
| 1617 | "APgIAAD7CAAA/wgAABcJAAAuCQAARQkAAFQJAAB4CQAAmAkAALsJAADSCQAA5gkAAPoJAAAVCgAA" |
| 1618 | "KQoAADsKAABCCgAASgoAAFIKAABbCgAAZAoAAGwKAAB0CgAAfAoAAIQKAACMCgAAlAoAAJwKAACk" |
| 1619 | "CgAArQoAALcKAADACgAAwwoAAMcKAADcCgAA6QoAAPEKAAD3CgAA/QoAAAMLAAAJCwAAEAsAABcL" |
| 1620 | "AAAdCwAAIwsAACkLAAAvCwAANQsAADsLAABBCwAARwsAAE0LAABSCwAAWwsAAF4LAABoCwAAbwsA" |
| 1621 | "ABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAC4AAAAwAAAA" |
| 1622 | "DwAAAAkAAAAAAAAAEAAAAAoAAACoBwAALgAAAAwAAAAAAAAALwAAAAwAAACoBwAALwAAAAwAAACw" |
| 1623 | "BwAAAgAJADUAAAACAAkANgAAAAIACQA3AAAAAgAJADgAAAACAAkAOQAAAAIACQA6AAAAAgAJADsA" |
| 1624 | "AAACAAkAPAAAAAIACQA9AAAAAgAJAD4AAAACAAkAPwAAAAIACQBAAAAACwAHAEIAAAAAAAIAAQAA" |
| 1625 | "AAAAAwAeAAAAAQACAAEAAAABAAMAHgAAAAIAAgAAAAAAAgACAAEAAAADAAIAAQAAAAMAAgAfAAAA" |
| 1626 | "AwACACAAAAADAAIAIQAAAAMAAgAiAAAAAwACACMAAAADAAIAJAAAAAMAAgAlAAAAAwACACYAAAAD" |
| 1627 | "AAIAJwAAAAMAAgAoAAAAAwACACkAAAADAAIAKgAAAAMABAA0AAAABwADAEMAAAAIAAIAAQAAAAoA" |
| 1628 | "AgABAAAACgABADIAAAAKAAAARQAAAAAAAAAAAAAACAAAAAAAAAAdAAAAaAcAALYHAAAAAAAAAQAA" |
| 1629 | "AAAAAAAIAAAAAAAAAB0AAAB4BwAAxAcAAAAAAAACAAAAAAAAAAgAAAAAAAAAHQAAAIgHAADSBwAA" |
| 1630 | "AAAAAAMAAAAAAAAACAAAAAAAAAAdAAAAmAcAAPoHAAAAAAAAAAAAAAEAAAAAAAAArAYAADEAAAAa" |
| 1631 | "AAMAaQAAABoABABpAAEAGgAHAGkABAAaAAgAaQAFABoACQBpAAYAGgAKAGkABwAaAAsAaQAIABoA" |
| 1632 | "DABpAAkAGgANAGkACgAaAA4AaQALABoABQBpAAIAGgAGAGkAAwAOAAAAAQABAAEAAACSBgAABAAA" |
| 1633 | "AHAQFQAAAA4ABAABAAIAAACWBgAAFwAAAGIADAAiAQoAcBAWAAEAGgICAG4gFwAhAG4gFwAxAG4Q" |
| 1634 | "GAABAAwBbiAUABAADgAAAAEAAQABAAAAngYAAAQAAABwEBUAAAAOAAIAAQACAAAAogYAAAYAAABi" |
| 1635 | "AAwAbiAUABAADgABAAEAAQAAAKgGAAAEAAAAcBAVAAAADgABAAEAAQAAALsGAAAEAAAAcBAVAAAA" |
| 1636 | "DgABAAAAAQAAAL8GAAAGAAAAYgAAAHEQAwAAAA4AAQAAAAEAAADEBgAABgAAAGIAAQBxEAMAAAAO" |
| 1637 | "AAEAAAABAAAA8QYAAAYAAABiAAIAcRABAAAADgABAAAAAQAAAPYGAAAGAAAAYgADAHEQAwAAAA4A" |
| 1638 | "AQAAAAEAAADJBgAABgAAAGIABABxEAMAAAAOAAEAAAABAAAAzgYAAAYAAABiAAEAcRADAAAADgAB" |
| 1639 | "AAAAAQAAANMGAAAGAAAAYgAGAHEQAwAAAA4AAQAAAAEAAADYBgAABgAAAGIABwBxEAMAAAAOAAEA" |
| 1640 | "AAABAAAA3QYAAAYAAABiAAgAcRABAAAADgABAAAAAQAAAOIGAAAGAAAAYgAJAHEQAwAAAA4AAQAA" |
| 1641 | "AAEAAADnBgAABgAAAGIACgBxEAMAAAAOAAEAAAABAAAA7AYAAAYAAABiAAsAcRABAAAADgABAAEA" |
| 1642 | "AAAAAPsGAAAlAAAAcQAHAAAAcQAIAAAAcQALAAAAcQAMAAAAcQANAAAAcQAOAAAAcQAPAAAAcQAQ" |
| 1643 | "AAAAcQARAAAAcQASAAAAcQAJAAAAcQAKAAAADgAnAA4AKQFFDgEWDwAhAA4AIwFFDloAEgAOABMA" |
| 1644 | "DktLS0tLS0tLS0tLABEADgAuAA5aADIADloANgAOWgA6AA5aAD4ADloAQgAOWgBGAA5aAEoADloA" |
| 1645 | "TgAOWgBSAA5aAFYADloAWgAOWgBeATQOPDw8PDw8PDw8PDw8AAIEAUYYAwIFAjEECEEXLAIFAjEE" |
| 1646 | "CEEXKwIFAjEECEEXLQIGAUYcAxgAGAEYAgAAAAIAAAAMBwAAEgcAAAIAAAAMBwAAGwcAAAIAAAAM" |
| 1647 | "BwAAJAcAAAEAAAAtBwAAPAcAAAAAAAAAAAAAAAAAAEgHAAAAAAAAAAAAAAAAAABUBwAAAAAAAAAA" |
| 1648 | "AAAAAAAAYAcAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAQAAAA0AAAACAACAgASsCAEIxAgAAAIAAoCA" |
| 1649 | "BIQJAQicCQwAAgAACQEJAQkBCQEJAQkBCQEJAQkBCQEJAQkEiIAEuAcBgIAEuAkAAA4ABoCABNAJ" |
| 1650 | "AQnoCQAJhAoACaAKAAm8CgAJ2AoACfQKAAmQCwAJrAsACcgLAAnkCwAJgAwACZwMAAm4DAg8Y2xp" |
| 1651 | "bml0PgAGPGluaXQ+AANBQUEAC0hlbGxvIFdvcmxkAAxIZWxsbyBXb3JsZDEADUhlbGxvIFdvcmxk" |
| 1652 | "MTAADUhlbGxvIFdvcmxkMTEADEhlbGxvIFdvcmxkMgAMSGVsbG8gV29ybGQzAAxIZWxsbyBXb3Js" |
| 1653 | "ZDQADEhlbGxvIFdvcmxkNQAMSGVsbG8gV29ybGQ2AAxIZWxsbyBXb3JsZDcADEhlbGxvIFdvcmxk" |
| 1654 | "OAAMSGVsbG8gV29ybGQ5AAFMAAJMTAAWTE1hbnlNZXRob2RzJFByaW50ZXIyOwAVTE1hbnlNZXRo" |
| 1655 | "b2RzJFByaW50ZXI7ABVMTWFueU1ldGhvZHMkU3RyaW5nczsADUxNYW55TWV0aG9kczsAIkxkYWx2" |
| 1656 | "aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5lckNs" |
| 1657 | "YXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABVMamF2YS9pby9QcmludFN0" |
| 1658 | "cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABlMamF2YS9sYW5n" |
| 1659 | "L1N0cmluZ0J1aWxkZXI7ABJMamF2YS9sYW5nL1N5c3RlbTsAEE1hbnlNZXRob2RzLmphdmEABVBy" |
| 1660 | "aW50AAZQcmludDAABlByaW50MQAHUHJpbnQxMAAHUHJpbnQxMQAGUHJpbnQyAAZQcmludDMABlBy" |
| 1661 | "aW50NAAGUHJpbnQ1AAZQcmludDYABlByaW50NwAGUHJpbnQ4AAZQcmludDkAB1ByaW50ZXIACFBy" |
| 1662 | "aW50ZXIyAAdTdHJpbmdzAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwALYWNjZXNzRmxhZ3MA" |
| 1663 | "BmFwcGVuZAAEYXJncwAEbWFpbgAEbXNnMAAEbXNnMQAFbXNnMTAABW1zZzExAARtc2cyAARtc2cz" |
| 1664 | "AARtc2c0AARtc2c1AARtc2c2AARtc2c3AARtc2c4AARtc2c5AARuYW1lAANvdXQAB3ByaW50bG4A" |
| 1665 | "AXMACHRvU3RyaW5nAAV2YWx1ZQBffn5EOHsibWluLWFwaSI6MTAwMDAsInNoYS0xIjoiZmViODZj" |
| 1666 | "MDA2ZWZhY2YxZDc5ODRiODVlMTc5MGZlZjdhNzY3YWViYyIsInZlcnNpb24iOiJ2MS4xLjUtZGV2" |
| 1667 | "In0AEAAAAAAAAAABAAAAAAAAAAEAAABIAAAAcAAAAAIAAAAOAAAAkAEAAAMAAAAFAAAAyAEAAAQA" |
| 1668 | "AAANAAAABAIAAAUAAAAZAAAAbAIAAAYAAAAEAAAANAMAAAEgAAAUAAAAuAMAAAMgAAAUAAAAkgYA" |
| 1669 | "AAQgAAAFAAAADAcAAAMQAAAEAAAAOQcAAAYgAAAEAAAAaAcAAAEQAAACAAAAqAcAAAAgAAAEAAAA" |
| 1670 | "tgcAAAIgAABIAAAAOAgAAAAQAAABAAAA0AsAAAAAAAA="; |
| 1671 | |
| 1672 | static void WriteBase64ToFile(const char* base64, File* file) { |
| 1673 | // Decode base64. |
| 1674 | CHECK(base64 != nullptr); |
| 1675 | size_t length; |
| 1676 | std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length)); |
| 1677 | CHECK(bytes != nullptr); |
| 1678 | if (!file->WriteFully(bytes.get(), length)) { |
| 1679 | PLOG(FATAL) << "Failed to write base64 as file"; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | TEST_F(Dex2oatTest, CompactDexGenerationFailure) { |
| 1684 | ScratchFile temp_dex; |
| 1685 | WriteBase64ToFile(kDuplicateMethodInputDex, temp_dex.GetFile()); |
| 1686 | std::string out_dir = GetScratchDir(); |
| 1687 | const std::string oat_filename = out_dir + "/base.oat"; |
| 1688 | // The dex won't pass the method verifier, only use the verify filter. |
| 1689 | GenerateOdexForTest(temp_dex.GetFilename(), |
| 1690 | oat_filename, |
| 1691 | CompilerFilter::Filter::kVerify, |
| 1692 | { }, |
| 1693 | true, // expect_success |
| 1694 | false, // use_fd |
| 1695 | [](const OatFile& o) { |
| 1696 | CHECK(o.ContainsDexCode()); |
| 1697 | }); |
| 1698 | // Open our generated oat file. |
| 1699 | std::string error_msg; |
| 1700 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(), |
| 1701 | oat_filename.c_str(), |
| 1702 | nullptr, |
| 1703 | nullptr, |
| 1704 | false, |
| 1705 | /*low_4gb*/false, |
| 1706 | temp_dex.GetFilename().c_str(), |
| 1707 | &error_msg)); |
| 1708 | ASSERT_TRUE(odex_file != nullptr); |
| 1709 | std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles(); |
| 1710 | ASSERT_EQ(oat_dex_files.size(), 1u); |
| 1711 | // The dexes should have failed to convert to compact dex. |
| 1712 | for (const OatDexFile* oat_dex : oat_dex_files) { |
| 1713 | std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg)); |
| 1714 | ASSERT_TRUE(dex_file != nullptr) << error_msg; |
| 1715 | ASSERT_TRUE(!dex_file->IsCompactDexFile()); |
| 1716 | } |
| 1717 | } |
| 1718 | |
Andreas Gampe | 25419b5 | 2018-02-08 21:30:26 -0800 | [diff] [blame] | 1719 | TEST_F(Dex2oatTest, StderrLoggerOutput) { |
| 1720 | std::string dex_location = GetScratchDir() + "/Dex2OatStderrLoggerTest.jar"; |
| 1721 | std::string odex_location = GetOdexDir() + "/Dex2OatStderrLoggerTest.odex"; |
| 1722 | |
| 1723 | // Test file doesn't matter. |
| 1724 | Copy(GetDexSrc1(), dex_location); |
| 1725 | |
| 1726 | GenerateOdexForTest(dex_location, |
| 1727 | odex_location, |
| 1728 | CompilerFilter::kQuicken, |
| 1729 | { "--runtime-arg", "-Xuse-stderr-logger" }, |
| 1730 | true); |
| 1731 | // Look for some random part of dex2oat logging. With the stderr logger this should be captured, |
| 1732 | // even on device. |
| 1733 | EXPECT_NE(std::string::npos, output_.find("dex2oat took")); |
| 1734 | } |
| 1735 | |
Calin Juravle | 0e09dfc | 2018-02-12 19:01:09 -0800 | [diff] [blame] | 1736 | TEST_F(Dex2oatTest, VerifyCompilationReason) { |
| 1737 | std::string dex_location = GetScratchDir() + "/Dex2OatCompilationReason.jar"; |
| 1738 | std::string odex_location = GetOdexDir() + "/Dex2OatCompilationReason.odex"; |
| 1739 | |
| 1740 | // Test file doesn't matter. |
| 1741 | Copy(GetDexSrc1(), dex_location); |
| 1742 | |
| 1743 | GenerateOdexForTest(dex_location, |
| 1744 | odex_location, |
| 1745 | CompilerFilter::kVerify, |
| 1746 | { "--compilation-reason=install" }, |
| 1747 | true); |
| 1748 | std::string error_msg; |
| 1749 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 1750 | odex_location.c_str(), |
| 1751 | nullptr, |
| 1752 | nullptr, |
| 1753 | false, |
| 1754 | /*low_4gb*/false, |
| 1755 | dex_location.c_str(), |
| 1756 | &error_msg)); |
| 1757 | ASSERT_TRUE(odex_file != nullptr); |
| 1758 | ASSERT_STREQ("install", odex_file->GetCompilationReason()); |
| 1759 | } |
| 1760 | |
| 1761 | TEST_F(Dex2oatTest, VerifyNoCompilationReason) { |
| 1762 | std::string dex_location = GetScratchDir() + "/Dex2OatNoCompilationReason.jar"; |
| 1763 | std::string odex_location = GetOdexDir() + "/Dex2OatNoCompilationReason.odex"; |
| 1764 | |
| 1765 | // Test file doesn't matter. |
| 1766 | Copy(GetDexSrc1(), dex_location); |
| 1767 | |
| 1768 | GenerateOdexForTest(dex_location, |
| 1769 | odex_location, |
| 1770 | CompilerFilter::kVerify, |
| 1771 | {}, |
| 1772 | true); |
| 1773 | std::string error_msg; |
| 1774 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 1775 | odex_location.c_str(), |
| 1776 | nullptr, |
| 1777 | nullptr, |
| 1778 | false, |
| 1779 | /*low_4gb*/false, |
| 1780 | dex_location.c_str(), |
| 1781 | &error_msg)); |
| 1782 | ASSERT_TRUE(odex_file != nullptr); |
| 1783 | ASSERT_EQ(nullptr, odex_file->GetCompilationReason()); |
| 1784 | } |
| 1785 | |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 1786 | TEST_F(Dex2oatTest, DontExtract) { |
| 1787 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods")); |
| 1788 | std::string error_msg; |
| 1789 | const std::string out_dir = GetScratchDir(); |
| 1790 | const std::string dex_location = dex->GetLocation(); |
| 1791 | const std::string odex_location = out_dir + "/base.oat"; |
| 1792 | const std::string vdex_location = out_dir + "/base.vdex"; |
| 1793 | GenerateOdexForTest(dex_location, |
| 1794 | odex_location, |
| 1795 | CompilerFilter::Filter::kVerify, |
| 1796 | { "--copy-dex-files=false" }, |
| 1797 | true, // expect_success |
| 1798 | false, // use_fd |
| 1799 | [](const OatFile&) { |
| 1800 | }); |
| 1801 | { |
| 1802 | // Check the vdex doesn't have dex. |
| 1803 | std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_location.c_str(), |
| 1804 | /*writable*/ false, |
| 1805 | /*low_4gb*/ false, |
| 1806 | /*unquicken*/ false, |
| 1807 | &error_msg)); |
| 1808 | ASSERT_TRUE(vdex != nullptr); |
| 1809 | EXPECT_EQ(vdex->GetHeader().GetDexSize(), 0u) << output_; |
| 1810 | } |
| 1811 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 1812 | odex_location.c_str(), |
| 1813 | nullptr, |
| 1814 | nullptr, |
| 1815 | false, |
| 1816 | /*low_4gb*/ false, |
| 1817 | dex_location.c_str(), |
| 1818 | &error_msg)); |
| 1819 | ASSERT_TRUE(odex_file != nullptr) << dex_location; |
| 1820 | std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles(); |
| 1821 | ASSERT_EQ(oat_dex_files.size(), 1u); |
| 1822 | // Verify that the oat file can still open the dex files. |
| 1823 | for (const OatDexFile* oat_dex : oat_dex_files) { |
| 1824 | std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg)); |
| 1825 | ASSERT_TRUE(dex_file != nullptr) << error_msg; |
| 1826 | } |
| 1827 | // Create a dm file and use it to verify. |
| 1828 | // Add produced artifacts to a zip file that doesn't contain the classes.dex. |
| 1829 | ScratchFile dm_file; |
| 1830 | { |
| 1831 | std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_location.c_str())); |
| 1832 | ASSERT_TRUE(vdex_file != nullptr); |
| 1833 | ASSERT_GT(vdex_file->GetLength(), 0u); |
| 1834 | FILE* file = fdopen(dm_file.GetFd(), "w+b"); |
| 1835 | ZipWriter writer(file); |
| 1836 | auto write_all_bytes = [&](File* file) { |
| 1837 | std::unique_ptr<uint8_t[]> bytes(new uint8_t[file->GetLength()]); |
| 1838 | ASSERT_TRUE(file->ReadFully(&bytes[0], file->GetLength())); |
| 1839 | ASSERT_GE(writer.WriteBytes(&bytes[0], file->GetLength()), 0); |
| 1840 | }; |
| 1841 | // Add vdex to zip. |
| 1842 | writer.StartEntry(VdexFile::kVdexNameInDmFile, ZipWriter::kCompress); |
| 1843 | write_all_bytes(vdex_file.get()); |
| 1844 | writer.FinishEntry(); |
| 1845 | writer.Finish(); |
| 1846 | ASSERT_EQ(dm_file.GetFile()->Flush(), 0); |
| 1847 | } |
| 1848 | |
| 1849 | // Generate a quickened dex by using the input dm file to verify. |
| 1850 | GenerateOdexForTest(dex_location, |
| 1851 | odex_location, |
| 1852 | CompilerFilter::Filter::kQuicken, |
Mathieu Chartier | 026dc0b | 2018-02-20 10:07:51 -0800 | [diff] [blame] | 1853 | { "--dump-timings", |
| 1854 | "--dm-file=" + dm_file.GetFilename(), |
| 1855 | // Pass -Xuse-stderr-logger have dex2oat output in output_ on target. |
| 1856 | "--runtime-arg", |
| 1857 | "-Xuse-stderr-logger" }, |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 1858 | true, // expect_success |
| 1859 | false, // use_fd |
| 1860 | [](const OatFile& o) { |
| 1861 | CHECK(o.ContainsDexCode()); |
| 1862 | }); |
Mathieu Chartier | 026dc0b | 2018-02-20 10:07:51 -0800 | [diff] [blame] | 1863 | // Check the output for "Fast verify", this is printed from --dump-timings. |
| 1864 | std::istringstream iss(output_); |
| 1865 | std::string line; |
| 1866 | bool found_fast_verify = false; |
| 1867 | const std::string kFastVerifyString = "Fast Verify"; |
| 1868 | while (std::getline(iss, line) && !found_fast_verify) { |
| 1869 | found_fast_verify = found_fast_verify || line.find(kFastVerifyString) != std::string::npos; |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 1870 | } |
Mathieu Chartier | 026dc0b | 2018-02-20 10:07:51 -0800 | [diff] [blame] | 1871 | EXPECT_TRUE(found_fast_verify) << "Expected to find " << kFastVerifyString << "\n" << output_; |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 1874 | } // namespace art |