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