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 | |
| 28 | static const char* kDexDiagContains = "--contains=core.vdex"; |
| 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, |
| 71 | /*abs_dex_location=*/ nullptr, |
| 72 | /*reservation=*/ nullptr, |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 73 | &error_msg)); |
| 74 | EXPECT_TRUE(oat != nullptr) << error_msg; |
| 75 | return oat; |
| 76 | } |
| 77 | |
| 78 | // Run dexdiag with a custom boot image location. |
| 79 | bool Exec(pid_t this_pid, const std::vector<std::string>& args, std::string* error_msg) { |
| 80 | // Invoke 'dexdiag' against the current process. |
| 81 | // This should succeed because we have a runtime and so it should |
| 82 | // be able to map in the boot.art and do a diff for it. |
| 83 | std::vector<std::string> exec_argv; |
| 84 | |
| 85 | // Build the command line "dexdiag <args> this_pid". |
| 86 | std::string executable_path = GetDexDiagFilePath(); |
| 87 | EXPECT_TRUE(OS::FileExists(executable_path.c_str())) << executable_path |
| 88 | << " should be a valid file path"; |
| 89 | exec_argv.push_back(executable_path); |
| 90 | for (const auto& arg : args) { |
| 91 | exec_argv.push_back(arg); |
| 92 | } |
| 93 | exec_argv.push_back(std::to_string(this_pid)); |
| 94 | |
| 95 | return ::art::Exec(exec_argv, error_msg); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | // We can't run these tests on the host, as they will fail when trying to open |
| 100 | // /proc/pid/pagemap. |
| 101 | // On the target, we invoke 'dexdiag' against the current process. |
| 102 | // This should succeed because we have a runtime and so dexdiag should |
| 103 | // be able to find the map for, e.g., boot.vdex and friends. |
| 104 | TEST_F(DexDiagTest, DexDiagHelpTest) { |
| 105 | // TODO: test the resulting output. |
| 106 | std::string error_msg; |
| 107 | ASSERT_TRUE(Exec(getpid(), { kDexDiagHelp }, &error_msg)) << "Failed to execute -- because: " |
| 108 | << error_msg; |
| 109 | } |
| 110 | |
| 111 | #if defined (ART_TARGET) |
| 112 | TEST_F(DexDiagTest, DexDiagContainsTest) { |
| 113 | #else |
| 114 | TEST_F(DexDiagTest, DISABLED_DexDiagContainsTest) { |
| 115 | #endif |
| 116 | std::unique_ptr<OatFile> oat = OpenOatAndVdexFiles(); |
| 117 | // TODO: test the resulting output. |
| 118 | std::string error_msg; |
| 119 | ASSERT_TRUE(Exec(getpid(), { kDexDiagContains }, &error_msg)) << "Failed to execute -- because: " |
| 120 | << error_msg; |
| 121 | } |
| 122 | |
| 123 | #if defined (ART_TARGET) |
| 124 | TEST_F(DexDiagTest, DexDiagContainsFailsTest) { |
| 125 | #else |
| 126 | TEST_F(DexDiagTest, DISABLED_DexDiagContainsFailsTest) { |
| 127 | #endif |
| 128 | std::unique_ptr<OatFile> oat = OpenOatAndVdexFiles(); |
| 129 | // TODO: test the resulting output. |
| 130 | std::string error_msg; |
| 131 | ASSERT_FALSE(Exec(getpid(), { kDexDiagContainsFails }, &error_msg)) |
| 132 | << "Failed to execute -- because: " |
| 133 | << error_msg; |
| 134 | } |
| 135 | |
| 136 | #if defined (ART_TARGET) |
| 137 | TEST_F(DexDiagTest, DexDiagVerboseTest) { |
| 138 | #else |
| 139 | TEST_F(DexDiagTest, DISABLED_DexDiagVerboseTest) { |
| 140 | #endif |
David Sehr | 55232f1 | 2017-04-19 14:06:49 -0700 | [diff] [blame] | 141 | // TODO: test the resulting output. |
| 142 | std::unique_ptr<OatFile> oat = OpenOatAndVdexFiles(); |
| 143 | std::string error_msg; |
| 144 | ASSERT_TRUE(Exec(getpid(), { kDexDiagVerbose }, &error_msg)) << "Failed to execute -- because: " |
| 145 | << error_msg; |
| 146 | } |
| 147 | |
| 148 | } // namespace art |