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