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