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 | |
| 22 | #include "common_runtime_test.h" |
| 23 | |
| 24 | #include "base/logging.h" |
| 25 | #include "base/macros.h" |
| 26 | #include "base/stringprintf.h" |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 27 | #include "dex_file-inl.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 28 | #include "dex2oat_environment_test.h" |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 29 | #include "oat.h" |
| 30 | #include "oat_file.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
| 33 | #include <sys/wait.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | class Dex2oatTest : public Dex2oatEnvironmentTest { |
| 39 | public: |
| 40 | virtual void TearDown() OVERRIDE { |
| 41 | Dex2oatEnvironmentTest::TearDown(); |
| 42 | |
| 43 | output_ = ""; |
| 44 | error_msg_ = ""; |
| 45 | success_ = false; |
| 46 | } |
| 47 | |
| 48 | protected: |
| 49 | void GenerateOdexForTest(const std::string& dex_location, |
| 50 | const std::string& odex_location, |
| 51 | CompilerFilter::Filter filter, |
| 52 | const std::vector<std::string>& extra_args = {}, |
| 53 | bool expect_success = true) { |
| 54 | std::vector<std::string> args; |
| 55 | args.push_back("--dex-file=" + dex_location); |
| 56 | args.push_back("--oat-file=" + odex_location); |
| 57 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 58 | args.push_back("--runtime-arg"); |
| 59 | args.push_back("-Xnorelocate"); |
| 60 | |
| 61 | args.insert(args.end(), extra_args.begin(), extra_args.end()); |
| 62 | |
| 63 | std::string error_msg; |
| 64 | bool success = Dex2Oat(args, &error_msg); |
| 65 | |
| 66 | if (expect_success) { |
| 67 | ASSERT_TRUE(success) << error_msg; |
| 68 | |
| 69 | // Verify the odex file was generated as expected. |
| 70 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 71 | odex_location.c_str(), |
| 72 | nullptr, |
| 73 | nullptr, |
| 74 | false, |
| 75 | /*low_4gb*/false, |
| 76 | dex_location.c_str(), |
| 77 | &error_msg)); |
| 78 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 79 | |
| 80 | CheckFilter(filter, odex_file->GetCompilerFilter()); |
| 81 | } else { |
| 82 | ASSERT_FALSE(success) << output_; |
| 83 | |
| 84 | error_msg_ = error_msg; |
| 85 | |
| 86 | // Verify there's no loadable odex file. |
| 87 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 88 | odex_location.c_str(), |
| 89 | nullptr, |
| 90 | nullptr, |
| 91 | false, |
| 92 | /*low_4gb*/false, |
| 93 | dex_location.c_str(), |
| 94 | &error_msg)); |
| 95 | ASSERT_TRUE(odex_file.get() == nullptr); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Check the input compiler filter against the generated oat file's filter. Mayb be overridden |
| 100 | // in subclasses when equality is not expected. |
| 101 | virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) { |
| 102 | EXPECT_EQ(expected, actual); |
| 103 | } |
| 104 | |
| 105 | bool Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) { |
| 106 | Runtime* runtime = Runtime::Current(); |
| 107 | |
| 108 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 109 | runtime->GetHeap()->GetBootImageSpaces(); |
| 110 | if (image_spaces.empty()) { |
| 111 | *error_msg = "No image location found for Dex2Oat."; |
| 112 | return false; |
| 113 | } |
| 114 | std::string image_location = image_spaces[0]->GetImageLocation(); |
| 115 | |
| 116 | std::vector<std::string> argv; |
| 117 | argv.push_back(runtime->GetCompilerExecutable()); |
| 118 | argv.push_back("--runtime-arg"); |
| 119 | argv.push_back("-classpath"); |
| 120 | argv.push_back("--runtime-arg"); |
| 121 | std::string class_path = runtime->GetClassPathString(); |
| 122 | if (class_path == "") { |
| 123 | class_path = OatFile::kSpecialSharedLibrary; |
| 124 | } |
| 125 | argv.push_back(class_path); |
| 126 | if (runtime->IsDebuggable()) { |
| 127 | argv.push_back("--debuggable"); |
| 128 | } |
| 129 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 130 | |
| 131 | if (!runtime->IsVerificationEnabled()) { |
| 132 | argv.push_back("--compiler-filter=verify-none"); |
| 133 | } |
| 134 | |
| 135 | if (runtime->MustRelocateIfPossible()) { |
| 136 | argv.push_back("--runtime-arg"); |
| 137 | argv.push_back("-Xrelocate"); |
| 138 | } else { |
| 139 | argv.push_back("--runtime-arg"); |
| 140 | argv.push_back("-Xnorelocate"); |
| 141 | } |
| 142 | |
| 143 | if (!kIsTargetBuild) { |
| 144 | argv.push_back("--host"); |
| 145 | } |
| 146 | |
| 147 | argv.push_back("--boot-image=" + image_location); |
| 148 | |
| 149 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 150 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 151 | |
| 152 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 153 | |
| 154 | // We must set --android-root. |
| 155 | const char* android_root = getenv("ANDROID_ROOT"); |
| 156 | CHECK(android_root != nullptr); |
| 157 | argv.push_back("--android-root=" + std::string(android_root)); |
| 158 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 159 | int link[2]; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 160 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 161 | if (pipe(link) == -1) { |
| 162 | return false; |
| 163 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 164 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 165 | pid_t pid = fork(); |
| 166 | if (pid == -1) { |
| 167 | return false; |
| 168 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 169 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 170 | if (pid == 0) { |
| 171 | // We need dex2oat to actually log things. |
| 172 | setenv("ANDROID_LOG_TAGS", "*:d", 1); |
| 173 | dup2(link[1], STDERR_FILENO); |
| 174 | close(link[0]); |
| 175 | close(link[1]); |
| 176 | std::vector<const char*> c_args; |
| 177 | for (const std::string& str : argv) { |
| 178 | c_args.push_back(str.c_str()); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 179 | } |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 180 | c_args.push_back(nullptr); |
| 181 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 182 | exit(1); |
| 183 | } else { |
| 184 | close(link[1]); |
| 185 | char buffer[128]; |
| 186 | memset(buffer, 0, 128); |
| 187 | ssize_t bytes_read = 0; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 188 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 189 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) { |
| 190 | output_ += std::string(buffer, bytes_read); |
| 191 | } |
| 192 | close(link[0]); |
| 193 | int status = 0; |
| 194 | if (waitpid(pid, &status, 0) != -1) { |
| 195 | success_ = (status == 0); |
| 196 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 197 | } |
| 198 | return success_; |
| 199 | } |
| 200 | |
| 201 | std::string output_ = ""; |
| 202 | std::string error_msg_ = ""; |
| 203 | bool success_ = false; |
| 204 | }; |
| 205 | |
| 206 | class Dex2oatSwapTest : public Dex2oatTest { |
| 207 | protected: |
| 208 | void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) { |
| 209 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 210 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 211 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 212 | Copy(GetTestDexFileName(), dex_location); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 213 | |
| 214 | std::vector<std::string> copy(extra_args); |
| 215 | |
| 216 | std::unique_ptr<ScratchFile> sf; |
| 217 | if (use_fd) { |
| 218 | sf.reset(new ScratchFile()); |
| 219 | copy.push_back(StringPrintf("--swap-fd=%d", sf->GetFd())); |
| 220 | } else { |
| 221 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 222 | copy.push_back("--swap-file=" + swap_location); |
| 223 | } |
| 224 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy); |
| 225 | |
| 226 | CheckValidity(); |
| 227 | ASSERT_TRUE(success_); |
| 228 | CheckResult(expect_use); |
| 229 | } |
| 230 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 231 | virtual std::string GetTestDexFileName() { |
| 232 | return GetDexSrc1(); |
| 233 | } |
| 234 | |
| 235 | virtual void CheckResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 236 | if (kIsTargetBuild) { |
| 237 | CheckTargetResult(expect_use); |
| 238 | } else { |
| 239 | CheckHostResult(expect_use); |
| 240 | } |
| 241 | } |
| 242 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 243 | virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 244 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 245 | // something for variants with file descriptor where we can control the lifetime of |
| 246 | // the swap file and thus take a look at it. |
| 247 | } |
| 248 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 249 | virtual void CheckHostResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 250 | if (!kIsTargetBuild) { |
| 251 | if (expect_use) { |
| 252 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 253 | << output_; |
| 254 | } else { |
| 255 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 256 | << output_; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Check whether the dex2oat run was really successful. |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 262 | virtual void CheckValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 263 | if (kIsTargetBuild) { |
| 264 | CheckTargetValidity(); |
| 265 | } else { |
| 266 | CheckHostValidity(); |
| 267 | } |
| 268 | } |
| 269 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 270 | virtual void CheckTargetValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 271 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 272 | // something for variants with file descriptor where we can control the lifetime of |
| 273 | // the swap file and thus take a look at it. |
| 274 | } |
| 275 | |
| 276 | // 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] | 277 | virtual void CheckHostValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 278 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) { |
| 283 | RunTest(false /* use_fd */, false /* expect_use */); |
| 284 | RunTest(true /* use_fd */, false /* expect_use */); |
| 285 | } |
| 286 | |
| 287 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) { |
| 288 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 289 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 290 | } |
| 291 | |
| 292 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) { |
| 293 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 294 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 295 | } |
| 296 | |
| 297 | TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) { |
| 298 | RunTest(false /* use_fd */, |
| 299 | true /* expect_use */, |
| 300 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 301 | RunTest(true /* use_fd */, |
| 302 | true /* expect_use */, |
| 303 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 304 | } |
| 305 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 306 | class Dex2oatSwapUseTest : public Dex2oatSwapTest { |
| 307 | protected: |
| 308 | void CheckHostResult(bool expect_use) OVERRIDE { |
| 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 | std::string GetTestDexFileName() OVERRIDE { |
| 321 | // Use Statics as it has a handful of functions. |
| 322 | return CommonRuntimeTest::GetTestDexFileName("Statics"); |
| 323 | } |
| 324 | |
| 325 | void GrabResult1() { |
| 326 | if (!kIsTargetBuild) { |
| 327 | native_alloc_1_ = ParseNativeAlloc(); |
| 328 | swap_1_ = ParseSwap(false /* expected */); |
| 329 | } else { |
| 330 | native_alloc_1_ = std::numeric_limits<size_t>::max(); |
| 331 | swap_1_ = 0; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void GrabResult2() { |
| 336 | if (!kIsTargetBuild) { |
| 337 | native_alloc_2_ = ParseNativeAlloc(); |
| 338 | swap_2_ = ParseSwap(true /* expected */); |
| 339 | } else { |
| 340 | native_alloc_2_ = 0; |
| 341 | swap_2_ = std::numeric_limits<size_t>::max(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private: |
| 346 | size_t ParseNativeAlloc() { |
| 347 | std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)"); |
| 348 | std::smatch native_alloc_match; |
| 349 | bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex); |
| 350 | if (!found) { |
| 351 | EXPECT_TRUE(found); |
| 352 | return 0; |
| 353 | } |
| 354 | if (native_alloc_match.size() != 2U) { |
| 355 | EXPECT_EQ(native_alloc_match.size(), 2U); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | std::istringstream stream(native_alloc_match[1].str()); |
| 360 | size_t value; |
| 361 | stream >> value; |
| 362 | |
| 363 | return value; |
| 364 | } |
| 365 | |
| 366 | size_t ParseSwap(bool expected) { |
| 367 | std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)"); |
| 368 | std::smatch swap_match; |
| 369 | bool found = std::regex_search(output_, swap_match, swap_regex); |
| 370 | if (found != expected) { |
| 371 | EXPECT_EQ(expected, found); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | if (!found) { |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | if (swap_match.size() != 2U) { |
| 380 | EXPECT_EQ(swap_match.size(), 2U); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | std::istringstream stream(swap_match[1].str()); |
| 385 | size_t value; |
| 386 | stream >> value; |
| 387 | |
| 388 | return value; |
| 389 | } |
| 390 | |
| 391 | protected: |
| 392 | size_t native_alloc_1_; |
| 393 | size_t native_alloc_2_; |
| 394 | |
| 395 | size_t swap_1_; |
| 396 | size_t swap_2_; |
| 397 | }; |
| 398 | |
| 399 | TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) { |
Roland Levillain | 63b6eb4 | 2016-07-28 16:37:28 +0100 | [diff] [blame] | 400 | // The `native_alloc_2_ >= native_alloc_1_` assertion below may not |
| 401 | // hold true on some x86 systems when read barriers are enabled; |
| 402 | // disable this test while we investigate (b/29259363). |
| 403 | TEST_DISABLED_FOR_READ_BARRIER_ON_X86(); |
| 404 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 405 | RunTest(false /* use_fd */, |
| 406 | false /* expect_use */); |
| 407 | GrabResult1(); |
| 408 | std::string output_1 = output_; |
| 409 | |
| 410 | output_ = ""; |
| 411 | |
| 412 | RunTest(false /* use_fd */, |
| 413 | true /* expect_use */, |
| 414 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 415 | GrabResult2(); |
| 416 | std::string output_2 = output_; |
| 417 | |
| 418 | if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) { |
| 419 | EXPECT_LT(native_alloc_2_, native_alloc_1_); |
| 420 | EXPECT_LT(swap_1_, swap_2_); |
| 421 | |
| 422 | LOG(ERROR) << output_1; |
| 423 | LOG(ERROR) << output_2; |
| 424 | } |
| 425 | } |
| 426 | |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 427 | class Dex2oatVeryLargeTest : public Dex2oatTest { |
| 428 | protected: |
| 429 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 430 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 431 | // Ignore, we'll do our own checks. |
| 432 | } |
| 433 | |
| 434 | void RunTest(CompilerFilter::Filter filter, |
| 435 | bool expect_large, |
| 436 | const std::vector<std::string>& extra_args = {}) { |
| 437 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 438 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 439 | |
| 440 | Copy(GetDexSrc1(), dex_location); |
| 441 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 442 | GenerateOdexForTest(dex_location, odex_location, filter, extra_args); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 443 | |
| 444 | CheckValidity(); |
| 445 | ASSERT_TRUE(success_); |
| 446 | CheckResult(dex_location, odex_location, filter, expect_large); |
| 447 | } |
| 448 | |
| 449 | void CheckResult(const std::string& dex_location, |
| 450 | const std::string& odex_location, |
| 451 | CompilerFilter::Filter filter, |
| 452 | bool expect_large) { |
| 453 | // Host/target independent checks. |
| 454 | std::string error_msg; |
| 455 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 456 | odex_location.c_str(), |
| 457 | nullptr, |
| 458 | nullptr, |
| 459 | false, |
| 460 | /*low_4gb*/false, |
| 461 | dex_location.c_str(), |
| 462 | &error_msg)); |
| 463 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 464 | if (expect_large) { |
| 465 | // Note: we cannot check the following: |
| 466 | // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, |
| 467 | // odex_file->GetCompilerFilter())); |
| 468 | // The reason is that the filter override currently happens when the dex files are |
| 469 | // loaded in dex2oat, which is after the oat file has been started. Thus, the header |
| 470 | // store cannot be changed, and the original filter is set in stone. |
| 471 | |
| 472 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 473 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 474 | ASSERT_TRUE(dex_file != nullptr); |
| 475 | uint32_t class_def_count = dex_file->NumClassDefs(); |
| 476 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
| 477 | for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) { |
| 478 | OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 479 | EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // If the input filter was "below," it should have been used. |
| 484 | if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, filter)) { |
| 485 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 486 | } |
| 487 | } else { |
| 488 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 489 | } |
| 490 | |
| 491 | // Host/target dependent checks. |
| 492 | if (kIsTargetBuild) { |
| 493 | CheckTargetResult(expect_large); |
| 494 | } else { |
| 495 | CheckHostResult(expect_large); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) { |
| 500 | // TODO: Ignore for now. May do something for fd things. |
| 501 | } |
| 502 | |
| 503 | void CheckHostResult(bool expect_large) { |
| 504 | if (!kIsTargetBuild) { |
| 505 | if (expect_large) { |
| 506 | EXPECT_NE(output_.find("Very large app, downgrading to verify-at-runtime."), |
| 507 | std::string::npos) |
| 508 | << output_; |
| 509 | } else { |
| 510 | EXPECT_EQ(output_.find("Very large app, downgrading to verify-at-runtime."), |
| 511 | std::string::npos) |
| 512 | << output_; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // Check whether the dex2oat run was really successful. |
| 518 | void CheckValidity() { |
| 519 | if (kIsTargetBuild) { |
| 520 | CheckTargetValidity(); |
| 521 | } else { |
| 522 | CheckHostValidity(); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void CheckTargetValidity() { |
| 527 | // TODO: Ignore for now. |
| 528 | } |
| 529 | |
| 530 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 531 | void CheckHostValidity() { |
| 532 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 533 | } |
| 534 | }; |
| 535 | |
| 536 | TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) { |
| 537 | RunTest(CompilerFilter::kVerifyNone, false); |
| 538 | RunTest(CompilerFilter::kVerifyAtRuntime, false); |
| 539 | RunTest(CompilerFilter::kInterpretOnly, false); |
| 540 | RunTest(CompilerFilter::kSpeed, false); |
| 541 | |
| 542 | RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=1000000" }); |
| 543 | RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=1000000" }); |
| 544 | RunTest(CompilerFilter::kInterpretOnly, false, { "--very-large-app-threshold=1000000" }); |
| 545 | RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" }); |
| 546 | } |
| 547 | |
| 548 | TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) { |
| 549 | RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=100" }); |
| 550 | RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=100" }); |
| 551 | RunTest(CompilerFilter::kInterpretOnly, true, { "--very-large-app-threshold=100" }); |
| 552 | RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" }); |
| 553 | } |
| 554 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame^] | 555 | static const char kDexFileLayoutInputProfile[] = |
| 556 | "cHJvADAwMgABAAsAAAABAPUpbf5jbGFzc2VzLmRleAEA"; |
| 557 | |
| 558 | static void WriteFileBase64(const char* base64, const char* location) { |
| 559 | // Decode base64. |
| 560 | CHECK(base64 != nullptr); |
| 561 | size_t length; |
| 562 | std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length)); |
| 563 | CHECK(bytes.get() != nullptr); |
| 564 | |
| 565 | // Write to provided file. |
| 566 | std::unique_ptr<File> file(OS::CreateEmptyFile(location)); |
| 567 | CHECK(file.get() != nullptr); |
| 568 | if (!file->WriteFully(bytes.get(), length)) { |
| 569 | PLOG(FATAL) << "Failed to write base64 as file"; |
| 570 | } |
| 571 | if (file->FlushCloseOrErase() != 0) { |
| 572 | PLOG(FATAL) << "Could not flush and close test file."; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | class Dex2oatLayoutTest : public Dex2oatTest { |
| 577 | protected: |
| 578 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 579 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 580 | // Ignore, we'll do our own checks. |
| 581 | } |
| 582 | |
| 583 | void RunTest() { |
| 584 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 585 | std::string profile_location = GetScratchDir() + "/primary.prof"; |
| 586 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 587 | |
| 588 | Copy(GetDexSrc2(), dex_location); |
| 589 | WriteFileBase64(kDexFileLayoutInputProfile, profile_location.c_str()); |
| 590 | |
| 591 | const std::vector<std::string>& extra_args = { "--profile-file=" + profile_location }; |
| 592 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kLayoutProfile, extra_args); |
| 593 | |
| 594 | CheckValidity(); |
| 595 | ASSERT_TRUE(success_); |
| 596 | CheckResult(dex_location, odex_location); |
| 597 | } |
| 598 | void CheckResult(const std::string& dex_location, const std::string& odex_location) { |
| 599 | // Host/target independent checks. |
| 600 | std::string error_msg; |
| 601 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 602 | odex_location.c_str(), |
| 603 | nullptr, |
| 604 | nullptr, |
| 605 | false, |
| 606 | /*low_4gb*/false, |
| 607 | dex_location.c_str(), |
| 608 | &error_msg)); |
| 609 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 610 | |
| 611 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 612 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 613 | ASSERT_TRUE(dex_file != nullptr); |
| 614 | uint32_t class_def_count = dex_file->NumClassDefs(); |
| 615 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
| 616 | } |
| 617 | |
| 618 | EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kLayoutProfile); |
| 619 | } |
| 620 | |
| 621 | // Check whether the dex2oat run was really successful. |
| 622 | void CheckValidity() { |
| 623 | if (kIsTargetBuild) { |
| 624 | CheckTargetValidity(); |
| 625 | } else { |
| 626 | CheckHostValidity(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | void CheckTargetValidity() { |
| 631 | // TODO: Ignore for now. |
| 632 | } |
| 633 | |
| 634 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 635 | void CheckHostValidity() { |
| 636 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 637 | } |
| 638 | }; |
| 639 | |
| 640 | TEST_F(Dex2oatLayoutTest, TestLayout) { |
| 641 | RunTest(); |
| 642 | } |
| 643 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 644 | } // namespace art |