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 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 17 | #include <regex> |
| 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 | |
| 25 | #include "android-base/stringprintf.h" |
| 26 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 27 | #include "common_runtime_test.h" |
| 28 | |
| 29 | #include "base/logging.h" |
| 30 | #include "base/macros.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 31 | #include "base/mutex-inl.h" |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 32 | #include "bytecode_utils.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 33 | #include "dex2oat_environment_test.h" |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 34 | #include "dex2oat_return_codes.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 35 | #include "dex_file-inl.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 36 | #include "jit/profile_compilation_info.h" |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 37 | #include "oat.h" |
| 38 | #include "oat_file.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 39 | #include "utils.h" |
| 40 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 43 | static constexpr size_t kMaxMethodIds = 65535; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 44 | static constexpr bool kDebugArgs = false; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 45 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 46 | using android::base::StringPrintf; |
| 47 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 48 | class Dex2oatTest : public Dex2oatEnvironmentTest { |
| 49 | public: |
| 50 | virtual void TearDown() OVERRIDE { |
| 51 | Dex2oatEnvironmentTest::TearDown(); |
| 52 | |
| 53 | output_ = ""; |
| 54 | error_msg_ = ""; |
| 55 | success_ = false; |
| 56 | } |
| 57 | |
| 58 | protected: |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 59 | int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations, |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 60 | const std::string& odex_location, |
| 61 | CompilerFilter::Filter filter, |
| 62 | std::string* error_msg, |
| 63 | const std::vector<std::string>& extra_args = {}, |
| 64 | bool use_fd = false) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 65 | std::unique_ptr<File> oat_file; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 66 | std::vector<std::string> args; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 67 | // Add dex file args. |
| 68 | for (const std::string& dex_location : dex_locations) { |
| 69 | args.push_back("--dex-file=" + dex_location); |
| 70 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 71 | if (use_fd) { |
| 72 | oat_file.reset(OS::CreateEmptyFile(odex_location.c_str())); |
| 73 | CHECK(oat_file != nullptr) << odex_location; |
| 74 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 75 | args.push_back("--oat-location=" + odex_location); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 76 | } else { |
| 77 | args.push_back("--oat-file=" + odex_location); |
| 78 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 79 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 80 | args.push_back("--runtime-arg"); |
| 81 | args.push_back("-Xnorelocate"); |
| 82 | |
| 83 | args.insert(args.end(), extra_args.begin(), extra_args.end()); |
| 84 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 85 | int status = Dex2Oat(args, error_msg); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 86 | if (oat_file != nullptr) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 87 | 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] | 88 | } |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 89 | return status; |
| 90 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 91 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 92 | void GenerateOdexForTest(const std::string& dex_location, |
| 93 | const std::string& odex_location, |
| 94 | CompilerFilter::Filter filter, |
| 95 | const std::vector<std::string>& extra_args = {}, |
| 96 | bool expect_success = true, |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 97 | bool use_fd = false, |
| 98 | std::function<void(const OatFile&)> check_oat = [](const OatFile&) {}) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 99 | std::string error_msg; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 100 | int status = GenerateOdexForTestWithStatus({dex_location}, |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 101 | odex_location, |
| 102 | filter, |
| 103 | &error_msg, |
| 104 | extra_args, |
| 105 | use_fd); |
| 106 | bool success = (status == 0); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 107 | if (expect_success) { |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 108 | ASSERT_TRUE(success) << error_msg << std::endl << output_; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 109 | |
| 110 | // Verify the odex file was generated as expected. |
| 111 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 112 | odex_location.c_str(), |
| 113 | nullptr, |
| 114 | nullptr, |
| 115 | false, |
| 116 | /*low_4gb*/false, |
| 117 | dex_location.c_str(), |
| 118 | &error_msg)); |
| 119 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 120 | |
| 121 | CheckFilter(filter, odex_file->GetCompilerFilter()); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 122 | check_oat(*(odex_file.get())); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 123 | } else { |
| 124 | ASSERT_FALSE(success) << output_; |
| 125 | |
| 126 | error_msg_ = error_msg; |
| 127 | |
| 128 | // Verify there's no loadable odex file. |
| 129 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 130 | odex_location.c_str(), |
| 131 | nullptr, |
| 132 | nullptr, |
| 133 | false, |
| 134 | /*low_4gb*/false, |
| 135 | dex_location.c_str(), |
| 136 | &error_msg)); |
| 137 | ASSERT_TRUE(odex_file.get() == nullptr); |
| 138 | } |
| 139 | } |
| 140 | |
Calin Juravle | 1ccf613 | 2017-08-02 17:46:53 -0700 | [diff] [blame] | 141 | // 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] | 142 | // in subclasses when equality is not expected. |
| 143 | virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) { |
| 144 | EXPECT_EQ(expected, actual); |
| 145 | } |
| 146 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 147 | int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 148 | Runtime* runtime = Runtime::Current(); |
| 149 | |
| 150 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 151 | runtime->GetHeap()->GetBootImageSpaces(); |
| 152 | if (image_spaces.empty()) { |
| 153 | *error_msg = "No image location found for Dex2Oat."; |
| 154 | return false; |
| 155 | } |
| 156 | std::string image_location = image_spaces[0]->GetImageLocation(); |
| 157 | |
| 158 | std::vector<std::string> argv; |
| 159 | argv.push_back(runtime->GetCompilerExecutable()); |
Calin Juravle | 1ccf613 | 2017-08-02 17:46:53 -0700 | [diff] [blame] | 160 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 161 | if (runtime->IsJavaDebuggable()) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 162 | argv.push_back("--debuggable"); |
| 163 | } |
| 164 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 165 | |
| 166 | if (!runtime->IsVerificationEnabled()) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 167 | argv.push_back("--compiler-filter=assume-verified"); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (runtime->MustRelocateIfPossible()) { |
| 171 | argv.push_back("--runtime-arg"); |
| 172 | argv.push_back("-Xrelocate"); |
| 173 | } else { |
| 174 | argv.push_back("--runtime-arg"); |
| 175 | argv.push_back("-Xnorelocate"); |
| 176 | } |
| 177 | |
| 178 | if (!kIsTargetBuild) { |
| 179 | argv.push_back("--host"); |
| 180 | } |
| 181 | |
| 182 | argv.push_back("--boot-image=" + image_location); |
| 183 | |
| 184 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 185 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 186 | |
| 187 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 188 | |
| 189 | // We must set --android-root. |
| 190 | const char* android_root = getenv("ANDROID_ROOT"); |
| 191 | CHECK(android_root != nullptr); |
| 192 | argv.push_back("--android-root=" + std::string(android_root)); |
| 193 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 194 | if (kDebugArgs) { |
| 195 | std::string all_args; |
| 196 | for (const std::string& arg : argv) { |
| 197 | all_args += arg + " "; |
| 198 | } |
| 199 | LOG(ERROR) << all_args; |
| 200 | } |
| 201 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 202 | int link[2]; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 203 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 204 | if (pipe(link) == -1) { |
| 205 | return false; |
| 206 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 207 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 208 | pid_t pid = fork(); |
| 209 | if (pid == -1) { |
| 210 | return false; |
| 211 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 212 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 213 | if (pid == 0) { |
| 214 | // We need dex2oat to actually log things. |
| 215 | setenv("ANDROID_LOG_TAGS", "*:d", 1); |
| 216 | dup2(link[1], STDERR_FILENO); |
| 217 | close(link[0]); |
| 218 | close(link[1]); |
| 219 | std::vector<const char*> c_args; |
| 220 | for (const std::string& str : argv) { |
| 221 | c_args.push_back(str.c_str()); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 222 | } |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 223 | c_args.push_back(nullptr); |
| 224 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 225 | exit(1); |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 226 | UNREACHABLE(); |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 227 | } else { |
| 228 | close(link[1]); |
| 229 | char buffer[128]; |
| 230 | memset(buffer, 0, 128); |
| 231 | ssize_t bytes_read = 0; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 232 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 233 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) { |
| 234 | output_ += std::string(buffer, bytes_read); |
| 235 | } |
| 236 | close(link[0]); |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 237 | int status = -1; |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 238 | if (waitpid(pid, &status, 0) != -1) { |
| 239 | success_ = (status == 0); |
| 240 | } |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 241 | return status; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 242 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | std::string output_ = ""; |
| 246 | std::string error_msg_ = ""; |
| 247 | bool success_ = false; |
| 248 | }; |
| 249 | |
| 250 | class Dex2oatSwapTest : public Dex2oatTest { |
| 251 | protected: |
| 252 | void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) { |
| 253 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 254 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 255 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 256 | Copy(GetTestDexFileName(), dex_location); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 257 | |
| 258 | std::vector<std::string> copy(extra_args); |
| 259 | |
| 260 | std::unique_ptr<ScratchFile> sf; |
| 261 | if (use_fd) { |
| 262 | sf.reset(new ScratchFile()); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 263 | copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd())); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 264 | } else { |
| 265 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 266 | copy.push_back("--swap-file=" + swap_location); |
| 267 | } |
| 268 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy); |
| 269 | |
| 270 | CheckValidity(); |
| 271 | ASSERT_TRUE(success_); |
| 272 | CheckResult(expect_use); |
| 273 | } |
| 274 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 275 | virtual std::string GetTestDexFileName() { |
Vladimir Marko | 1535770 | 2017-02-09 10:37:31 +0000 | [diff] [blame] | 276 | return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps"); |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | virtual void CheckResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 280 | if (kIsTargetBuild) { |
| 281 | CheckTargetResult(expect_use); |
| 282 | } else { |
| 283 | CheckHostResult(expect_use); |
| 284 | } |
| 285 | } |
| 286 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 287 | virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 288 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 289 | // something for variants with file descriptor where we can control the lifetime of |
| 290 | // the swap file and thus take a look at it. |
| 291 | } |
| 292 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 293 | virtual void CheckHostResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 294 | if (!kIsTargetBuild) { |
| 295 | if (expect_use) { |
| 296 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 297 | << output_; |
| 298 | } else { |
| 299 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 300 | << output_; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Check whether the dex2oat run was really successful. |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 306 | virtual void CheckValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 307 | if (kIsTargetBuild) { |
| 308 | CheckTargetValidity(); |
| 309 | } else { |
| 310 | CheckHostValidity(); |
| 311 | } |
| 312 | } |
| 313 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 314 | virtual void CheckTargetValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 315 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 316 | // something for variants with file descriptor where we can control the lifetime of |
| 317 | // the swap file and thus take a look at it. |
| 318 | } |
| 319 | |
| 320 | // 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] | 321 | virtual void CheckHostValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 322 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) { |
| 327 | RunTest(false /* use_fd */, false /* expect_use */); |
| 328 | RunTest(true /* use_fd */, false /* expect_use */); |
| 329 | } |
| 330 | |
| 331 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) { |
| 332 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 333 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 334 | } |
| 335 | |
| 336 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) { |
| 337 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 338 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 339 | } |
| 340 | |
| 341 | TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) { |
| 342 | RunTest(false /* use_fd */, |
| 343 | true /* expect_use */, |
| 344 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 345 | RunTest(true /* use_fd */, |
| 346 | true /* expect_use */, |
| 347 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 348 | } |
| 349 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 350 | class Dex2oatSwapUseTest : public Dex2oatSwapTest { |
| 351 | protected: |
| 352 | void CheckHostResult(bool expect_use) OVERRIDE { |
| 353 | if (!kIsTargetBuild) { |
| 354 | if (expect_use) { |
| 355 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 356 | << output_; |
| 357 | } else { |
| 358 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 359 | << output_; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | std::string GetTestDexFileName() OVERRIDE { |
| 365 | // Use Statics as it has a handful of functions. |
| 366 | return CommonRuntimeTest::GetTestDexFileName("Statics"); |
| 367 | } |
| 368 | |
| 369 | void GrabResult1() { |
| 370 | if (!kIsTargetBuild) { |
| 371 | native_alloc_1_ = ParseNativeAlloc(); |
| 372 | swap_1_ = ParseSwap(false /* expected */); |
| 373 | } else { |
| 374 | native_alloc_1_ = std::numeric_limits<size_t>::max(); |
| 375 | swap_1_ = 0; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void GrabResult2() { |
| 380 | if (!kIsTargetBuild) { |
| 381 | native_alloc_2_ = ParseNativeAlloc(); |
| 382 | swap_2_ = ParseSwap(true /* expected */); |
| 383 | } else { |
| 384 | native_alloc_2_ = 0; |
| 385 | swap_2_ = std::numeric_limits<size_t>::max(); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | private: |
| 390 | size_t ParseNativeAlloc() { |
| 391 | std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)"); |
| 392 | std::smatch native_alloc_match; |
| 393 | bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex); |
| 394 | if (!found) { |
| 395 | EXPECT_TRUE(found); |
| 396 | return 0; |
| 397 | } |
| 398 | if (native_alloc_match.size() != 2U) { |
| 399 | EXPECT_EQ(native_alloc_match.size(), 2U); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | std::istringstream stream(native_alloc_match[1].str()); |
| 404 | size_t value; |
| 405 | stream >> value; |
| 406 | |
| 407 | return value; |
| 408 | } |
| 409 | |
| 410 | size_t ParseSwap(bool expected) { |
| 411 | std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)"); |
| 412 | std::smatch swap_match; |
| 413 | bool found = std::regex_search(output_, swap_match, swap_regex); |
| 414 | if (found != expected) { |
| 415 | EXPECT_EQ(expected, found); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | if (!found) { |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | if (swap_match.size() != 2U) { |
| 424 | EXPECT_EQ(swap_match.size(), 2U); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | std::istringstream stream(swap_match[1].str()); |
| 429 | size_t value; |
| 430 | stream >> value; |
| 431 | |
| 432 | return value; |
| 433 | } |
| 434 | |
| 435 | protected: |
| 436 | size_t native_alloc_1_; |
| 437 | size_t native_alloc_2_; |
| 438 | |
| 439 | size_t swap_1_; |
| 440 | size_t swap_2_; |
| 441 | }; |
| 442 | |
| 443 | TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) { |
Andreas Gampe | f4a67fd | 2017-05-04 09:55:36 -0700 | [diff] [blame] | 444 | // Native memory usage isn't correctly tracked under sanitization. |
| 445 | TEST_DISABLED_FOR_MEMORY_TOOL_ASAN(); |
| 446 | |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 447 | // The `native_alloc_2_ >= native_alloc_1_` assertion below may not |
Roland Levillain | 19772bf | 2017-02-16 11:28:10 +0000 | [diff] [blame] | 448 | // hold true on some x86 systems; disable this test while we |
| 449 | // investigate (b/29259363). |
| 450 | TEST_DISABLED_FOR_X86(); |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 451 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 452 | RunTest(false /* use_fd */, |
| 453 | false /* expect_use */); |
| 454 | GrabResult1(); |
| 455 | std::string output_1 = output_; |
| 456 | |
| 457 | output_ = ""; |
| 458 | |
| 459 | RunTest(false /* use_fd */, |
| 460 | true /* expect_use */, |
| 461 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 462 | GrabResult2(); |
| 463 | std::string output_2 = output_; |
| 464 | |
| 465 | if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) { |
| 466 | EXPECT_LT(native_alloc_2_, native_alloc_1_); |
| 467 | EXPECT_LT(swap_1_, swap_2_); |
| 468 | |
| 469 | LOG(ERROR) << output_1; |
| 470 | LOG(ERROR) << output_2; |
| 471 | } |
| 472 | } |
| 473 | |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 474 | class Dex2oatVeryLargeTest : public Dex2oatTest { |
| 475 | protected: |
| 476 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 477 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 478 | // Ignore, we'll do our own checks. |
| 479 | } |
| 480 | |
| 481 | void RunTest(CompilerFilter::Filter filter, |
| 482 | bool expect_large, |
| 483 | const std::vector<std::string>& extra_args = {}) { |
| 484 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 485 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 486 | |
| 487 | Copy(GetDexSrc1(), dex_location); |
| 488 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 489 | GenerateOdexForTest(dex_location, odex_location, filter, extra_args); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 490 | |
| 491 | CheckValidity(); |
| 492 | ASSERT_TRUE(success_); |
| 493 | CheckResult(dex_location, odex_location, filter, expect_large); |
| 494 | } |
| 495 | |
| 496 | void CheckResult(const std::string& dex_location, |
| 497 | const std::string& odex_location, |
| 498 | CompilerFilter::Filter filter, |
| 499 | bool expect_large) { |
| 500 | // Host/target independent checks. |
| 501 | std::string error_msg; |
| 502 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 503 | odex_location.c_str(), |
| 504 | nullptr, |
| 505 | nullptr, |
| 506 | false, |
| 507 | /*low_4gb*/false, |
| 508 | dex_location.c_str(), |
| 509 | &error_msg)); |
| 510 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 511 | if (expect_large) { |
| 512 | // Note: we cannot check the following: |
| 513 | // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, |
| 514 | // odex_file->GetCompilerFilter())); |
| 515 | // The reason is that the filter override currently happens when the dex files are |
| 516 | // loaded in dex2oat, which is after the oat file has been started. Thus, the header |
| 517 | // store cannot be changed, and the original filter is set in stone. |
| 518 | |
| 519 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 520 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 521 | ASSERT_TRUE(dex_file != nullptr); |
| 522 | uint32_t class_def_count = dex_file->NumClassDefs(); |
| 523 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
| 524 | for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) { |
| 525 | OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 526 | EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | // If the input filter was "below," it should have been used. |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 531 | if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) { |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 532 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 533 | } |
| 534 | } else { |
| 535 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 536 | } |
| 537 | |
| 538 | // Host/target dependent checks. |
| 539 | if (kIsTargetBuild) { |
| 540 | CheckTargetResult(expect_large); |
| 541 | } else { |
| 542 | CheckHostResult(expect_large); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) { |
| 547 | // TODO: Ignore for now. May do something for fd things. |
| 548 | } |
| 549 | |
| 550 | void CheckHostResult(bool expect_large) { |
| 551 | if (!kIsTargetBuild) { |
| 552 | if (expect_large) { |
Mathieu Chartier | 010126f | 2017-07-15 15:44:18 -0700 | [diff] [blame] | 553 | EXPECT_NE(output_.find("Very large app, downgrading to"), |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 554 | std::string::npos) |
| 555 | << output_; |
| 556 | } else { |
Mathieu Chartier | 010126f | 2017-07-15 15:44:18 -0700 | [diff] [blame] | 557 | EXPECT_EQ(output_.find("Very large app, downgrading to"), |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 558 | std::string::npos) |
| 559 | << output_; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // Check whether the dex2oat run was really successful. |
| 565 | void CheckValidity() { |
| 566 | if (kIsTargetBuild) { |
| 567 | CheckTargetValidity(); |
| 568 | } else { |
| 569 | CheckHostValidity(); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void CheckTargetValidity() { |
| 574 | // TODO: Ignore for now. |
| 575 | } |
| 576 | |
| 577 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 578 | void CheckHostValidity() { |
| 579 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 580 | } |
| 581 | }; |
| 582 | |
| 583 | TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 584 | RunTest(CompilerFilter::kAssumeVerified, false); |
| 585 | RunTest(CompilerFilter::kExtract, false); |
| 586 | RunTest(CompilerFilter::kQuicken, false); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 587 | RunTest(CompilerFilter::kSpeed, false); |
| 588 | |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 589 | RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=1000000" }); |
| 590 | RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=1000000" }); |
| 591 | RunTest(CompilerFilter::kQuicken, false, { "--very-large-app-threshold=1000000" }); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 592 | RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" }); |
| 593 | } |
| 594 | |
| 595 | TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 596 | RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=100" }); |
| 597 | RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=100" }); |
| 598 | RunTest(CompilerFilter::kQuicken, true, { "--very-large-app-threshold=100" }); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 599 | RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" }); |
| 600 | } |
| 601 | |
Mathieu Chartier | 97ab5e3 | 2017-02-22 13:35:44 -0800 | [diff] [blame] | 602 | // Regressin test for b/35665292. |
| 603 | TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) { |
| 604 | // Test that dex2oat doesn't crash with speed-profile but no input profile. |
| 605 | RunTest(CompilerFilter::kSpeedProfile, false); |
| 606 | } |
| 607 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 608 | class Dex2oatLayoutTest : public Dex2oatTest { |
| 609 | protected: |
| 610 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 611 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 612 | // Ignore, we'll do our own checks. |
| 613 | } |
| 614 | |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 615 | // Emits a profile with a single dex file with the given location and a single class index of 1. |
| 616 | void GenerateProfile(const std::string& test_profile, |
| 617 | const std::string& dex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 618 | size_t num_classes, |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 619 | uint32_t checksum) { |
| 620 | int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 621 | CHECK_GE(profile_test_fd, 0); |
| 622 | |
| 623 | ProfileCompilationInfo info; |
| 624 | std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 625 | for (size_t i = 0; i < num_classes; ++i) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 626 | info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 627 | } |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 628 | bool result = info.Save(profile_test_fd); |
| 629 | close(profile_test_fd); |
| 630 | ASSERT_TRUE(result); |
| 631 | } |
| 632 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 633 | void CompileProfileOdex(const std::string& dex_location, |
| 634 | const std::string& odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 635 | const std::string& app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 636 | bool use_fd, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 637 | size_t num_profile_classes, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 638 | const std::vector<std::string>& extra_args = {}, |
| 639 | bool expect_success = true) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 640 | const std::string profile_location = GetScratchDir() + "/primary.prof"; |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 641 | const char* location = dex_location.c_str(); |
| 642 | std::string error_msg; |
| 643 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 644 | ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files)); |
| 645 | EXPECT_EQ(dex_files.size(), 1U); |
| 646 | std::unique_ptr<const DexFile>& dex_file = dex_files[0]; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 647 | GenerateProfile(profile_location, |
| 648 | dex_location, |
| 649 | num_profile_classes, |
| 650 | dex_file->GetLocationChecksum()); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 651 | std::vector<std::string> copy(extra_args); |
| 652 | copy.push_back("--profile-file=" + profile_location); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 653 | std::unique_ptr<File> app_image_file; |
| 654 | if (!app_image_file_name.empty()) { |
| 655 | if (use_fd) { |
| 656 | app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str())); |
| 657 | copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd())); |
| 658 | } else { |
| 659 | copy.push_back("--app-image-file=" + app_image_file_name); |
| 660 | } |
| 661 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 662 | GenerateOdexForTest(dex_location, |
| 663 | odex_location, |
| 664 | CompilerFilter::kSpeedProfile, |
| 665 | copy, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 666 | expect_success, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 667 | use_fd); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 668 | if (app_image_file != nullptr) { |
| 669 | ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file"; |
| 670 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 671 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 672 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 673 | uint64_t GetImageSize(const std::string& image_file_name) { |
| 674 | EXPECT_FALSE(image_file_name.empty()); |
| 675 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str())); |
| 676 | CHECK(file != nullptr); |
| 677 | ImageHeader image_header; |
| 678 | const bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 679 | CHECK(success); |
| 680 | CHECK(image_header.IsValid()); |
| 681 | ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_); |
| 682 | return image_header.GetImageSize(); |
| 683 | } |
| 684 | |
| 685 | void RunTest(bool app_image) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 686 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 687 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 688 | std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): ""; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 689 | Copy(GetDexSrc2(), dex_location); |
| 690 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 691 | uint64_t image_file_empty_profile = 0; |
| 692 | if (app_image) { |
| 693 | CompileProfileOdex(dex_location, |
| 694 | odex_location, |
| 695 | app_image_file, |
| 696 | /* use_fd */ false, |
| 697 | /* num_profile_classes */ 0); |
| 698 | CheckValidity(); |
| 699 | ASSERT_TRUE(success_); |
| 700 | // Don't check the result since CheckResult relies on the class being in the profile. |
| 701 | image_file_empty_profile = GetImageSize(app_image_file); |
| 702 | EXPECT_GT(image_file_empty_profile, 0u); |
| 703 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 704 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 705 | // Small profile. |
| 706 | CompileProfileOdex(dex_location, |
| 707 | odex_location, |
| 708 | app_image_file, |
| 709 | /* use_fd */ false, |
| 710 | /* num_profile_classes */ 1); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 711 | CheckValidity(); |
| 712 | ASSERT_TRUE(success_); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 713 | CheckResult(dex_location, odex_location, app_image_file); |
| 714 | |
| 715 | if (app_image) { |
| 716 | // Test that the profile made a difference by adding more classes. |
| 717 | const uint64_t image_file_small_profile = GetImageSize(app_image_file); |
| 718 | CHECK_LT(image_file_empty_profile, image_file_small_profile); |
| 719 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 720 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 721 | |
| 722 | void RunTestVDex() { |
| 723 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 724 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 725 | std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex"; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 726 | std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art"; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 727 | Copy(GetDexSrc2(), dex_location); |
| 728 | |
| 729 | std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str())); |
| 730 | CHECK(vdex_file1 != nullptr) << vdex_location; |
| 731 | ScratchFile vdex_file2; |
| 732 | { |
| 733 | std::string input_vdex = "--input-vdex-fd=-1"; |
| 734 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 735 | CompileProfileOdex(dex_location, |
| 736 | odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 737 | app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 738 | /* use_fd */ true, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 739 | /* num_profile_classes */ 1, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 740 | { input_vdex, output_vdex }); |
| 741 | EXPECT_GT(vdex_file1->GetLength(), 0u); |
| 742 | } |
| 743 | { |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 744 | // Test that vdex and dexlayout fail gracefully. |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 745 | std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd()); |
| 746 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd()); |
| 747 | CompileProfileOdex(dex_location, |
| 748 | odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 749 | app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 750 | /* use_fd */ true, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 751 | /* num_profile_classes */ 1, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 752 | { input_vdex, output_vdex }, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 753 | /* expect_success */ true); |
| 754 | EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 755 | } |
| 756 | ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 757 | CheckValidity(); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 758 | ASSERT_TRUE(success_); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 761 | void CheckResult(const std::string& dex_location, |
| 762 | const std::string& odex_location, |
| 763 | const std::string& app_image_file_name) { |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 764 | // Host/target independent checks. |
| 765 | std::string error_msg; |
| 766 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 767 | odex_location.c_str(), |
| 768 | nullptr, |
| 769 | nullptr, |
| 770 | false, |
| 771 | /*low_4gb*/false, |
| 772 | dex_location.c_str(), |
| 773 | &error_msg)); |
| 774 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 775 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 776 | const char* location = dex_location.c_str(); |
| 777 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 778 | ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files)); |
| 779 | EXPECT_EQ(dex_files.size(), 1U); |
| 780 | std::unique_ptr<const DexFile>& old_dex_file = dex_files[0]; |
| 781 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 782 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 783 | std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 784 | ASSERT_TRUE(new_dex_file != nullptr); |
| 785 | uint32_t class_def_count = new_dex_file->NumClassDefs(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 786 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 787 | ASSERT_GE(class_def_count, 2U); |
| 788 | |
| 789 | // The new layout swaps the classes at indexes 0 and 1. |
| 790 | std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_); |
| 791 | std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_); |
| 792 | std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_); |
| 793 | std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_); |
| 794 | EXPECT_EQ(old_class0, new_class1); |
| 795 | EXPECT_EQ(old_class1, new_class0); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Jeff Hao | c155b05 | 2017-01-17 17:43:29 -0800 | [diff] [blame] | 798 | EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 799 | |
| 800 | if (!app_image_file_name.empty()) { |
| 801 | // Go peek at the image header to make sure it was large enough to contain the class. |
| 802 | std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str())); |
| 803 | ImageHeader image_header; |
| 804 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 805 | ASSERT_TRUE(success); |
| 806 | ASSERT_TRUE(image_header.IsValid()); |
| 807 | EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u); |
| 808 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 811 | // Check whether the dex2oat run was really successful. |
| 812 | void CheckValidity() { |
| 813 | if (kIsTargetBuild) { |
| 814 | CheckTargetValidity(); |
| 815 | } else { |
| 816 | CheckHostValidity(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 817 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 818 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 819 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 820 | void CheckTargetValidity() { |
| 821 | // TODO: Ignore for now. |
| 822 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 823 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 824 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 825 | void CheckHostValidity() { |
| 826 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 827 | } |
| 828 | }; |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 829 | |
| 830 | TEST_F(Dex2oatLayoutTest, TestLayout) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 831 | RunTest(/* app-image */ false); |
| 832 | } |
| 833 | |
| 834 | TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) { |
| 835 | RunTest(/* app-image */ true); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 838 | TEST_F(Dex2oatLayoutTest, TestVdexLayout) { |
| 839 | RunTestVDex(); |
| 840 | } |
| 841 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 842 | class Dex2oatUnquickenTest : public Dex2oatTest { |
| 843 | protected: |
| 844 | void RunUnquickenMultiDex() { |
| 845 | std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar"; |
| 846 | std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex"; |
| 847 | std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex"; |
| 848 | Copy(GetTestDexFileName("MultiDex"), dex_location); |
| 849 | |
| 850 | std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str())); |
| 851 | CHECK(vdex_file1 != nullptr) << vdex_location; |
| 852 | // Quicken the dex file into a vdex file. |
| 853 | { |
| 854 | std::string input_vdex = "--input-vdex-fd=-1"; |
| 855 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 856 | GenerateOdexForTest(dex_location, |
| 857 | odex_location, |
| 858 | CompilerFilter::kQuicken, |
| 859 | { input_vdex, output_vdex }, |
| 860 | /* expect_success */ true, |
| 861 | /* use_fd */ true); |
| 862 | EXPECT_GT(vdex_file1->GetLength(), 0u); |
| 863 | } |
| 864 | // Unquicken by running the verify compiler filter on the vdex file. |
| 865 | { |
| 866 | std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd()); |
| 867 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 868 | GenerateOdexForTest(dex_location, |
| 869 | odex_location, |
| 870 | CompilerFilter::kVerify, |
| 871 | { input_vdex, output_vdex }, |
| 872 | /* expect_success */ true, |
| 873 | /* use_fd */ true); |
| 874 | } |
| 875 | ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 876 | CheckResult(dex_location, odex_location); |
| 877 | ASSERT_TRUE(success_); |
| 878 | } |
| 879 | |
| 880 | void CheckResult(const std::string& dex_location, const std::string& odex_location) { |
| 881 | std::string error_msg; |
| 882 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 883 | odex_location.c_str(), |
| 884 | nullptr, |
| 885 | nullptr, |
| 886 | false, |
| 887 | /*low_4gb*/false, |
| 888 | dex_location.c_str(), |
| 889 | &error_msg)); |
| 890 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 891 | ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u); |
| 892 | |
| 893 | // Iterate over the dex files and ensure there is no quickened instruction. |
| 894 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 895 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 896 | for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 897 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 898 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 899 | if (class_data != nullptr) { |
| 900 | for (ClassDataItemIterator class_it(*dex_file, class_data); |
| 901 | class_it.HasNext(); |
| 902 | class_it.Next()) { |
| 903 | if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) { |
| 904 | for (CodeItemIterator it(*class_it.GetMethodCodeItem()); !it.Done(); it.Advance()) { |
| 905 | Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction()); |
| 906 | ASSERT_FALSE(inst->IsQuickened()); |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | }; |
| 915 | |
| 916 | TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) { |
| 917 | RunUnquickenMultiDex(); |
| 918 | } |
| 919 | |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 920 | class Dex2oatWatchdogTest : public Dex2oatTest { |
| 921 | protected: |
| 922 | void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) { |
| 923 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 924 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 925 | |
| 926 | Copy(GetTestDexFileName(), dex_location); |
| 927 | |
| 928 | std::vector<std::string> copy(extra_args); |
| 929 | |
| 930 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 931 | copy.push_back("--swap-file=" + swap_location); |
| 932 | GenerateOdexForTest(dex_location, |
| 933 | odex_location, |
| 934 | CompilerFilter::kSpeed, |
| 935 | copy, |
| 936 | expect_success); |
| 937 | } |
| 938 | |
| 939 | std::string GetTestDexFileName() { |
| 940 | return GetDexSrc1(); |
| 941 | } |
| 942 | }; |
| 943 | |
| 944 | TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) { |
| 945 | // Check with default. |
| 946 | RunTest(true); |
| 947 | |
| 948 | // Check with ten minutes. |
| 949 | RunTest(true, { "--watchdog-timeout=600000" }); |
| 950 | } |
| 951 | |
| 952 | TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) { |
Roland Levillain | 68db225 | 2017-08-14 12:48:47 +0100 | [diff] [blame] | 953 | TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624 |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 954 | // Check with ten milliseconds. |
| 955 | RunTest(false, { "--watchdog-timeout=10" }); |
| 956 | } |
| 957 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 958 | class Dex2oatReturnCodeTest : public Dex2oatTest { |
| 959 | protected: |
| 960 | int RunTest(const std::vector<std::string>& extra_args = {}) { |
| 961 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 962 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 963 | |
| 964 | Copy(GetTestDexFileName(), dex_location); |
| 965 | |
| 966 | std::string error_msg; |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 967 | return GenerateOdexForTestWithStatus({dex_location}, |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 968 | odex_location, |
| 969 | CompilerFilter::kSpeed, |
| 970 | &error_msg, |
| 971 | extra_args); |
| 972 | } |
| 973 | |
| 974 | std::string GetTestDexFileName() { |
| 975 | return GetDexSrc1(); |
| 976 | } |
| 977 | }; |
| 978 | |
| 979 | TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) { |
Andreas Gampe | fd80b17 | 2017-04-26 22:25:31 -0700 | [diff] [blame] | 980 | TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793 |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 981 | int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" }); |
| 982 | EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_; |
| 983 | } |
| 984 | |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 985 | class Dex2oatClassLoaderContextTest : public Dex2oatTest { |
| 986 | protected: |
| 987 | void RunTest(const char* class_loader_context, |
| 988 | const char* expected_classpath_key, |
| 989 | bool expected_success, |
| 990 | bool use_second_source = false) { |
| 991 | std::string dex_location = GetUsedDexLocation(); |
| 992 | std::string odex_location = GetUsedOatLocation(); |
| 993 | |
| 994 | Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location); |
| 995 | |
| 996 | std::string error_msg; |
| 997 | std::vector<std::string> extra_args; |
| 998 | if (class_loader_context != nullptr) { |
| 999 | extra_args.push_back(std::string("--class-loader-context=") + class_loader_context); |
| 1000 | } |
| 1001 | auto check_oat = [expected_classpath_key](const OatFile& oat_file) { |
| 1002 | ASSERT_TRUE(expected_classpath_key != nullptr); |
| 1003 | const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey); |
| 1004 | ASSERT_TRUE(classpath != nullptr); |
| 1005 | ASSERT_STREQ(expected_classpath_key, classpath); |
| 1006 | }; |
| 1007 | |
| 1008 | GenerateOdexForTest(dex_location, |
| 1009 | odex_location, |
| 1010 | CompilerFilter::kQuicken, |
| 1011 | extra_args, |
| 1012 | expected_success, |
| 1013 | /*use_fd*/ false, |
| 1014 | check_oat); |
| 1015 | } |
| 1016 | |
| 1017 | std::string GetUsedDexLocation() { |
| 1018 | return GetScratchDir() + "/Context.jar"; |
| 1019 | } |
| 1020 | |
| 1021 | std::string GetUsedOatLocation() { |
| 1022 | return GetOdexDir() + "/Context.odex"; |
| 1023 | } |
| 1024 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1025 | const char* kEmptyClassPathKey = "PCL[]"; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1026 | }; |
| 1027 | |
| 1028 | TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) { |
| 1029 | RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false); |
| 1030 | } |
| 1031 | |
| 1032 | TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) { |
| 1033 | RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true); |
| 1034 | } |
| 1035 | |
| 1036 | TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) { |
| 1037 | RunTest(OatFile::kSpecialSharedLibrary, |
| 1038 | OatFile::kSpecialSharedLibrary, |
| 1039 | /*expected_success*/ true); |
| 1040 | } |
| 1041 | |
| 1042 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) { |
| 1043 | std::string context = "PCL[" + GetUsedDexLocation() + "]"; |
| 1044 | RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true); |
| 1045 | } |
| 1046 | |
| 1047 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) { |
| 1048 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested"); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1049 | |
| 1050 | std::string context = "PCL[" + dex_files[0]->GetLocation() + "]"; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1051 | std::string expected_classpath_key = "PCL[" + |
| 1052 | dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]"; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1053 | RunTest(context.c_str(), expected_classpath_key.c_str(), true); |
| 1054 | } |
| 1055 | |
| 1056 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) { |
| 1057 | std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar"; |
| 1058 | Copy(GetStrippedDexSrc1(), stripped_classpath); |
| 1059 | |
| 1060 | std::string context = "PCL[" + stripped_classpath + "]"; |
| 1061 | // Expect an empty context because stripped dex files cannot be open. |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1062 | RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true); |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) { |
| 1066 | std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar"; |
| 1067 | std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex"; |
| 1068 | |
| 1069 | Copy(GetDexSrc1(), stripped_classpath); |
| 1070 | |
| 1071 | GenerateOdexForTest(stripped_classpath, |
| 1072 | odex_for_classpath, |
| 1073 | CompilerFilter::kQuicken, |
| 1074 | {}, |
| 1075 | true); |
| 1076 | |
| 1077 | // Strip the dex file |
| 1078 | Copy(GetStrippedDexSrc1(), stripped_classpath); |
| 1079 | |
| 1080 | std::string context = "PCL[" + stripped_classpath + "]"; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1081 | std::string expected_classpath_key; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1082 | { |
| 1083 | // Open the oat file to get the expected classpath. |
| 1084 | OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false); |
| 1085 | std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile()); |
| 1086 | std::vector<std::unique_ptr<const DexFile>> oat_dex_files = |
| 1087 | OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str()); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1088 | expected_classpath_key = "PCL["; |
| 1089 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 1090 | if (i > 0) { |
| 1091 | expected_classpath_key + ":"; |
| 1092 | } |
| 1093 | expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" + |
| 1094 | std::to_string(oat_dex_files[i]->GetLocationChecksum()); |
| 1095 | } |
| 1096 | expected_classpath_key += "]"; |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | RunTest(context.c_str(), |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 1100 | expected_classpath_key.c_str(), |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1101 | /*expected_success*/ true, |
| 1102 | /*use_second_source*/ true); |
| 1103 | } |
| 1104 | |
| 1105 | TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) { |
| 1106 | std::string context = "PCL[does_not_exists.dex]"; |
| 1107 | // Expect an empty context because stripped dex files cannot be open. |
| 1108 | RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true); |
| 1109 | } |
| 1110 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 1111 | TEST_F(Dex2oatClassLoaderContextTest, ChainContext) { |
| 1112 | std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested"); |
| 1113 | std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex"); |
| 1114 | |
| 1115 | std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" + |
| 1116 | "DLC[" + GetTestDexFileName("MultiDex") + "]"; |
| 1117 | std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" + |
| 1118 | "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]"; |
| 1119 | |
| 1120 | RunTest(context.c_str(), expected_classpath_key.c_str(), true); |
| 1121 | } |
| 1122 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 1123 | class Dex2oatDeterminism : public Dex2oatTest {}; |
| 1124 | |
| 1125 | TEST_F(Dex2oatDeterminism, UnloadCompile) { |
| 1126 | if (!kUseReadBarrier && |
| 1127 | gc::kCollectorTypeDefault != gc::kCollectorTypeCMS && |
| 1128 | gc::kCollectorTypeDefault != gc::kCollectorTypeMS) { |
| 1129 | LOG(INFO) << "Test requires determinism support."; |
| 1130 | return; |
| 1131 | } |
| 1132 | Runtime* const runtime = Runtime::Current(); |
| 1133 | std::string out_dir = GetScratchDir(); |
| 1134 | const std::string base_oat_name = out_dir + "/base.oat"; |
| 1135 | const std::string base_vdex_name = out_dir + "/base.vdex"; |
| 1136 | const std::string unload_oat_name = out_dir + "/unload.oat"; |
| 1137 | const std::string unload_vdex_name = out_dir + "/unload.vdex"; |
| 1138 | const std::string no_unload_oat_name = out_dir + "/nounload.oat"; |
| 1139 | const std::string no_unload_vdex_name = out_dir + "/nounload.vdex"; |
| 1140 | const std::string app_image_name = out_dir + "/unload.art"; |
| 1141 | std::string error_msg; |
| 1142 | const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces(); |
| 1143 | ASSERT_GT(spaces.size(), 0u); |
| 1144 | const std::string image_location = spaces[0]->GetImageLocation(); |
| 1145 | // Without passing in an app image, it will unload in between compilations. |
| 1146 | const int res = GenerateOdexForTestWithStatus( |
| 1147 | GetLibCoreDexFileNames(), |
| 1148 | base_oat_name, |
| 1149 | CompilerFilter::Filter::kQuicken, |
| 1150 | &error_msg, |
| 1151 | {"--force-determinism", "--avoid-storing-invocation"}); |
| 1152 | EXPECT_EQ(res, 0); |
| 1153 | Copy(base_oat_name, unload_oat_name); |
| 1154 | Copy(base_vdex_name, unload_vdex_name); |
| 1155 | std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str())); |
| 1156 | std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str())); |
| 1157 | ASSERT_TRUE(unload_oat != nullptr); |
| 1158 | ASSERT_TRUE(unload_vdex != nullptr); |
| 1159 | EXPECT_GT(unload_oat->GetLength(), 0u); |
| 1160 | EXPECT_GT(unload_vdex->GetLength(), 0u); |
| 1161 | // Regenerate with an app image to disable the dex2oat unloading and verify that the output is |
| 1162 | // the same. |
| 1163 | const int res2 = GenerateOdexForTestWithStatus( |
| 1164 | GetLibCoreDexFileNames(), |
| 1165 | base_oat_name, |
| 1166 | CompilerFilter::Filter::kQuicken, |
| 1167 | &error_msg, |
| 1168 | {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name}); |
| 1169 | EXPECT_EQ(res2, 0); |
| 1170 | Copy(base_oat_name, no_unload_oat_name); |
| 1171 | Copy(base_vdex_name, no_unload_vdex_name); |
| 1172 | std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str())); |
| 1173 | std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str())); |
| 1174 | ASSERT_TRUE(no_unload_oat != nullptr); |
| 1175 | ASSERT_TRUE(no_unload_vdex != nullptr); |
| 1176 | EXPECT_GT(no_unload_oat->GetLength(), 0u); |
| 1177 | EXPECT_GT(no_unload_vdex->GetLength(), 0u); |
| 1178 | // Verify that both of the files are the same (odex and vdex). |
| 1179 | EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength()); |
| 1180 | EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength()); |
| 1181 | EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0) |
| 1182 | << unload_oat_name << " " << no_unload_oat_name; |
| 1183 | EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0) |
| 1184 | << unload_vdex_name << " " << no_unload_vdex_name; |
| 1185 | // App image file. |
| 1186 | std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str())); |
| 1187 | ASSERT_TRUE(app_image_file != nullptr); |
| 1188 | EXPECT_GT(app_image_file->GetLength(), 0u); |
| 1189 | } |
| 1190 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame^] | 1191 | // Test that dexlayout section info is correctly written to the oat file for profile based |
| 1192 | // compilation. |
| 1193 | TEST_F(Dex2oatTest, LayoutSections) { |
| 1194 | using Hotness = ProfileCompilationInfo::MethodHotness; |
| 1195 | std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods")); |
| 1196 | ScratchFile profile_file; |
| 1197 | // We can only layout method indices with code items, figure out which ones have this property |
| 1198 | // first. |
| 1199 | std::vector<uint16_t> methods; |
| 1200 | { |
| 1201 | const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;"); |
| 1202 | dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id); |
| 1203 | const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx); |
| 1204 | ClassDataItemIterator it(*dex, dex->GetClassData(*class_def)); |
| 1205 | it.SkipAllFields(); |
| 1206 | std::set<size_t> code_item_offsets; |
| 1207 | for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) { |
| 1208 | const uint16_t method_idx = it.GetMemberIndex(); |
| 1209 | const size_t code_item_offset = it.GetMethodCodeItemOffset(); |
| 1210 | if (code_item_offsets.insert(code_item_offset).second) { |
| 1211 | // Unique code item, add the method index. |
| 1212 | methods.push_back(method_idx); |
| 1213 | } |
| 1214 | } |
| 1215 | DCHECK(!it.HasNext()); |
| 1216 | } |
| 1217 | ASSERT_GE(methods.size(), 8u); |
| 1218 | std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]}; |
| 1219 | std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]}; |
| 1220 | std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]}; |
| 1221 | // Here, we build the profile from the method lists. |
| 1222 | ProfileCompilationInfo info; |
| 1223 | info.AddMethodsForDex( |
| 1224 | static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup), |
| 1225 | dex.get(), |
| 1226 | hot_methods.begin(), |
| 1227 | hot_methods.end()); |
| 1228 | info.AddMethodsForDex( |
| 1229 | Hotness::kFlagStartup, |
| 1230 | dex.get(), |
| 1231 | startup_methods.begin(), |
| 1232 | startup_methods.end()); |
| 1233 | info.AddMethodsForDex( |
| 1234 | Hotness::kFlagPostStartup, |
| 1235 | dex.get(), |
| 1236 | post_methods.begin(), |
| 1237 | post_methods.end()); |
| 1238 | for (uint16_t id : hot_methods) { |
| 1239 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot()); |
| 1240 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup()); |
| 1241 | } |
| 1242 | for (uint16_t id : startup_methods) { |
| 1243 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup()); |
| 1244 | } |
| 1245 | for (uint16_t id : post_methods) { |
| 1246 | EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup()); |
| 1247 | } |
| 1248 | // Save the profile since we want to use it with dex2oat to produce an oat file. |
| 1249 | ASSERT_TRUE(info.Save(profile_file.GetFd())); |
| 1250 | // Generate a profile based odex. |
| 1251 | const std::string dir = GetScratchDir(); |
| 1252 | const std::string oat_filename = dir + "/base.oat"; |
| 1253 | const std::string vdex_filename = dir + "/base.vdex"; |
| 1254 | std::string error_msg; |
| 1255 | const int res = GenerateOdexForTestWithStatus( |
| 1256 | {dex->GetLocation()}, |
| 1257 | oat_filename, |
| 1258 | CompilerFilter::Filter::kQuicken, |
| 1259 | &error_msg, |
| 1260 | {"--profile-file=" + profile_file.GetFilename()}); |
| 1261 | EXPECT_EQ(res, 0); |
| 1262 | |
| 1263 | // Open our generated oat file. |
| 1264 | std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(), |
| 1265 | oat_filename.c_str(), |
| 1266 | nullptr, |
| 1267 | nullptr, |
| 1268 | false, |
| 1269 | /*low_4gb*/false, |
| 1270 | dex->GetLocation().c_str(), |
| 1271 | &error_msg)); |
| 1272 | ASSERT_TRUE(odex_file != nullptr); |
| 1273 | std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles(); |
| 1274 | ASSERT_EQ(oat_dex_files.size(), 1u); |
| 1275 | // Check that the code sections match what we expect. |
| 1276 | for (const OatDexFile* oat_dex : oat_dex_files) { |
| 1277 | const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections(); |
| 1278 | // Testing of logging the sections. |
| 1279 | ASSERT_TRUE(sections != nullptr); |
| 1280 | LOG(INFO) << *sections; |
| 1281 | |
| 1282 | // Load the sections into temporary variables for convenience. |
| 1283 | const DexLayoutSection& code_section = |
| 1284 | sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)]; |
| 1285 | const DexLayoutSection::Subsection& section_hot_code = |
| 1286 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)]; |
| 1287 | const DexLayoutSection::Subsection& section_sometimes_used = |
| 1288 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)]; |
| 1289 | const DexLayoutSection::Subsection& section_startup_only = |
| 1290 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)]; |
| 1291 | const DexLayoutSection::Subsection& section_unused = |
| 1292 | code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)]; |
| 1293 | |
| 1294 | // All the sections should be non-empty. |
| 1295 | EXPECT_GT(section_hot_code.size_, 0u); |
| 1296 | EXPECT_GT(section_sometimes_used.size_, 0u); |
| 1297 | EXPECT_GT(section_startup_only.size_, 0u); |
| 1298 | EXPECT_GT(section_unused.size_, 0u); |
| 1299 | |
| 1300 | // Open the dex file since we need to peek at the code items to verify the layout matches what |
| 1301 | // we expect. |
| 1302 | std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg)); |
| 1303 | ASSERT_TRUE(dex_file != nullptr) << error_msg; |
| 1304 | const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;"); |
| 1305 | ASSERT_TRUE(type_id != nullptr); |
| 1306 | dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 1307 | const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx); |
| 1308 | ASSERT_TRUE(class_def != nullptr); |
| 1309 | |
| 1310 | // Count how many code items are for each category, there should be at least one per category. |
| 1311 | size_t hot_count = 0; |
| 1312 | size_t post_startup_count = 0; |
| 1313 | size_t startup_count = 0; |
| 1314 | size_t unused_count = 0; |
| 1315 | // Visit all of the methdos of the main class and cross reference the method indices to their |
| 1316 | // corresponding code item offsets to verify the layout. |
| 1317 | ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def)); |
| 1318 | it.SkipAllFields(); |
| 1319 | for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) { |
| 1320 | const size_t method_idx = it.GetMemberIndex(); |
| 1321 | const size_t code_item_offset = it.GetMethodCodeItemOffset(); |
| 1322 | const bool is_hot = ContainsElement(hot_methods, method_idx); |
| 1323 | const bool is_startup = ContainsElement(startup_methods, method_idx); |
| 1324 | const bool is_post_startup = ContainsElement(post_methods, method_idx); |
| 1325 | if (is_hot) { |
| 1326 | // Hot is highest precedence, check that the hot methods are in the hot section. |
| 1327 | EXPECT_LT(code_item_offset - section_hot_code.offset_, section_hot_code.size_); |
| 1328 | ++hot_count; |
| 1329 | } else if (is_post_startup) { |
| 1330 | // Post startup is sometimes used section. |
| 1331 | EXPECT_LT(code_item_offset - section_sometimes_used.offset_, section_sometimes_used.size_); |
| 1332 | ++post_startup_count; |
| 1333 | } else if (is_startup) { |
| 1334 | // Startup at this point means not hot or post startup, these must be startup only then. |
| 1335 | EXPECT_LT(code_item_offset - section_startup_only.offset_, section_startup_only.size_); |
| 1336 | ++startup_count; |
| 1337 | } else { |
| 1338 | // If no flags are set, the method should be unused. |
| 1339 | EXPECT_LT(code_item_offset - section_unused.offset_, section_unused.size_); |
| 1340 | ++unused_count; |
| 1341 | } |
| 1342 | } |
| 1343 | DCHECK(!it.HasNext()); |
| 1344 | EXPECT_GT(hot_count, 0u); |
| 1345 | EXPECT_GT(post_startup_count, 0u); |
| 1346 | EXPECT_GT(startup_count, 0u); |
| 1347 | EXPECT_GT(unused_count, 0u); |
| 1348 | } |
| 1349 | } |
| 1350 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 1351 | } // namespace art |