blob: 0791e9a563c33e653d1f2861483ff3d7cdf781c3 [file] [log] [blame]
David Sehr55232f12017-04-19 14:06:49 -07001/*
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 Sehra8d23cb2019-04-08 11:29:11 -070020#include "base/common_art_test.h"
David Sehr891a50e2017-10-27 17:01:07 -070021#include "base/file_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "base/os.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070023#include "exec_utils.h"
24#include "oat_file.h"
David Sehr55232f12017-04-19 14:06:49 -070025
26namespace art {
27
28static const char* kDexDiagContains = "--contains=core.vdex";
29static const char* kDexDiagContainsFails = "--contains=anything_other_than_core.vdex";
30static const char* kDexDiagHelp = "--help";
31static const char* kDexDiagVerbose = "--verbose";
32static const char* kDexDiagBinaryName = "dexdiag";
33
David Sehra8d23cb2019-04-08 11:29:11 -070034class DexDiagTest : public CommonArtTest {
David Sehr55232f12017-04-19 14:06:49 -070035 protected:
Andreas Gampefa6a1b02018-09-07 08:11:55 -070036 void SetUp() override {
David Sehra8d23cb2019-04-08 11:29:11 -070037 CommonArtTest::SetUp();
David Sehr55232f12017-04-19 14:06:49 -070038 }
39
40 // Path to the dexdiag(d?)[32|64] binary.
41 std::string GetDexDiagFilePath() {
Roland Levillainfb6a5c02019-03-29 20:20:16 +000042 std::string path = GetAndroidRuntimeBinDir() + '/' + kDexDiagBinaryName;
43 std::string path32 = path + "32";
David Sehr55232f12017-04-19 14:06:49 -070044 // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix.
Roland Levillainfb6a5c02019-03-29 20:20:16 +000045 if (OS::FileExists(path32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) {
46 return path32;
David Sehr55232f12017-04-19 14:06:49 -070047 } else {
48 // This is a 64-bit build or only a single build exists.
Roland Levillainfb6a5c02019-03-29 20:20:16 +000049 return path;
David Sehr55232f12017-04-19 14:06:49 -070050 }
51 }
52
53 std::unique_ptr<OatFile> OpenOatAndVdexFiles() {
David Sehr55232f12017-04-19 14:06:49 -070054 // 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 Markof4efa9e2018-10-17 14:12:45 +010066 std::unique_ptr<OatFile> oat(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +010067 oat_location.c_str(),
David Sehr55232f12017-04-19 14:06:49 -070068 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +010069 /*executable=*/ false,
70 /*low_4gb=*/ false,
71 /*abs_dex_location=*/ nullptr,
72 /*reservation=*/ nullptr,
David Sehr55232f12017-04-19 14:06:49 -070073 &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.
104TEST_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)
112TEST_F(DexDiagTest, DexDiagContainsTest) {
113#else
114TEST_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)
124TEST_F(DexDiagTest, DexDiagContainsFailsTest) {
125#else
126TEST_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)
137TEST_F(DexDiagTest, DexDiagVerboseTest) {
138#else
139TEST_F(DexDiagTest, DISABLED_DexDiagVerboseTest) {
140#endif
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