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