David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
David Sehr | a8d23cb | 2019-04-08 11:29:11 -0700 | [diff] [blame] | 20 | #include "base/common_art_test.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 21 | #include "base/file_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "base/os.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 23 | #include "exec_utils.h" |
| 24 | #include "oat_file.h" |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 28 | static const char* kDexDiagContains = "--contains=boot.vdex"; |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 29 | static const char* kDexDiagContainsFails = "--contains=anything_other_than_core.vdex"; |
| 30 | static const char* kDexDiagHelp = "--help"; |
| 31 | static const char* kDexDiagVerbose = "--verbose"; |
| 32 | static const char* kDexDiagBinaryName = "dexdiag"; |
| 33 | |
David Sehr | a8d23cb | 2019-04-08 11:29:11 -0700 | [diff] [blame] | 34 | class DexDiagTest : public CommonArtTest { |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 35 | protected: |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 36 | void SetUp() override { |
David Sehr | a8d23cb | 2019-04-08 11:29:11 -0700 | [diff] [blame] | 37 | CommonArtTest::SetUp(); |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Path to the dexdiag(d?)[32|64] binary. |
| 41 | std::string GetDexDiagFilePath() { |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 42 | std::string path = GetArtBinDir() + '/' + kDexDiagBinaryName; |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 43 | std::string path32 = path + "32"; |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 44 | // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix. |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 45 | if (OS::FileExists(path32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) { |
| 46 | return path32; |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 47 | } else { |
| 48 | // This is a 64-bit build or only a single build exists. |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 49 | return path; |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | std::unique_ptr<OatFile> OpenOatAndVdexFiles() { |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 54 | // Open the core.oat file. |
| 55 | // This is a little convoluted because we have to |
| 56 | // get the location of the default core image (.../framework/core.oat), |
| 57 | // find it in the right architecture subdirectory (.../framework/arm/core.oat), |
| 58 | // Then, opening the oat file has the side-effect of opening the corresponding |
| 59 | // vdex file (.../framework/arm/core.vdex). |
| 60 | const std::string default_location = GetCoreOatLocation(); |
| 61 | EXPECT_TRUE(!default_location.empty()); |
| 62 | std::string oat_location = GetSystemImageFilename(default_location.c_str(), kRuntimeISA); |
| 63 | EXPECT_TRUE(!oat_location.empty()); |
| 64 | std::cout << "==" << oat_location << std::endl; |
| 65 | std::string error_msg; |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 66 | std::unique_ptr<OatFile> oat(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 67 | oat_location.c_str(), |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 68 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 69 | /*executable=*/ false, |
| 70 | /*low_4gb=*/ false, |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 71 | &error_msg)); |
| 72 | EXPECT_TRUE(oat != nullptr) << error_msg; |
| 73 | return oat; |
| 74 | } |
| 75 | |
| 76 | // Run dexdiag with a custom boot image location. |
| 77 | bool Exec(pid_t this_pid, const std::vector<std::string>& args, std::string* error_msg) { |
| 78 | // Invoke 'dexdiag' against the current process. |
| 79 | // This should succeed because we have a runtime and so it should |
| 80 | // be able to map in the boot.art and do a diff for it. |
| 81 | std::vector<std::string> exec_argv; |
| 82 | |
| 83 | // Build the command line "dexdiag <args> this_pid". |
| 84 | std::string executable_path = GetDexDiagFilePath(); |
| 85 | EXPECT_TRUE(OS::FileExists(executable_path.c_str())) << executable_path |
| 86 | << " should be a valid file path"; |
| 87 | exec_argv.push_back(executable_path); |
| 88 | for (const auto& arg : args) { |
| 89 | exec_argv.push_back(arg); |
| 90 | } |
| 91 | exec_argv.push_back(std::to_string(this_pid)); |
| 92 | |
| 93 | return ::art::Exec(exec_argv, error_msg); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | // We can't run these tests on the host, as they will fail when trying to open |
| 98 | // /proc/pid/pagemap. |
| 99 | // On the target, we invoke 'dexdiag' against the current process. |
| 100 | // This should succeed because we have a runtime and so dexdiag should |
| 101 | // be able to find the map for, e.g., boot.vdex and friends. |
| 102 | TEST_F(DexDiagTest, DexDiagHelpTest) { |
| 103 | // TODO: test the resulting output. |
| 104 | std::string error_msg; |
| 105 | ASSERT_TRUE(Exec(getpid(), { kDexDiagHelp }, &error_msg)) << "Failed to execute -- because: " |
| 106 | << error_msg; |
| 107 | } |
| 108 | |
| 109 | #if defined (ART_TARGET) |
| 110 | TEST_F(DexDiagTest, DexDiagContainsTest) { |
| 111 | #else |
| 112 | TEST_F(DexDiagTest, DISABLED_DexDiagContainsTest) { |
| 113 | #endif |
| 114 | std::unique_ptr<OatFile> oat = OpenOatAndVdexFiles(); |
| 115 | // TODO: test the resulting output. |
| 116 | std::string error_msg; |
| 117 | ASSERT_TRUE(Exec(getpid(), { kDexDiagContains }, &error_msg)) << "Failed to execute -- because: " |
| 118 | << error_msg; |
| 119 | } |
| 120 | |
| 121 | #if defined (ART_TARGET) |
| 122 | TEST_F(DexDiagTest, DexDiagContainsFailsTest) { |
| 123 | #else |
| 124 | TEST_F(DexDiagTest, DISABLED_DexDiagContainsFailsTest) { |
| 125 | #endif |
| 126 | std::unique_ptr<OatFile> oat = OpenOatAndVdexFiles(); |
| 127 | // TODO: test the resulting output. |
| 128 | std::string error_msg; |
| 129 | ASSERT_FALSE(Exec(getpid(), { kDexDiagContainsFails }, &error_msg)) |
| 130 | << "Failed to execute -- because: " |
| 131 | << error_msg; |
| 132 | } |
| 133 | |
| 134 | #if defined (ART_TARGET) |
| 135 | TEST_F(DexDiagTest, DexDiagVerboseTest) { |
| 136 | #else |
| 137 | TEST_F(DexDiagTest, DISABLED_DexDiagVerboseTest) { |
| 138 | #endif |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 139 | // TODO: test the resulting output. |
| 140 | std::unique_ptr<OatFile> oat = OpenOatAndVdexFiles(); |
| 141 | std::string error_msg; |
| 142 | ASSERT_TRUE(Exec(getpid(), { kDexDiagVerbose }, &error_msg)) << "Failed to execute -- because: " |
| 143 | << error_msg; |
| 144 | } |
| 145 | |
| 146 | } // namespace art |