blob: 27ac402120c0aa568bceddbbba31f759910a3716 [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
David Srbeckycf0c6ef2020-02-05 16:25:36 +000028static const char* kDexDiagContains = "--contains=boot.vdex";
David Sehr55232f12017-04-19 14:06:49 -070029static 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() {
Martin Stjernholme58624f2019-09-20 15:53:40 +010042 std::string path = GetArtBinDir() + '/' + kDexDiagBinaryName;
Roland Levillainfb6a5c02019-03-29 20:20:16 +000043 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,
David Sehr55232f12017-04-19 14:06:49 -070071 &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.
102TEST_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)
110TEST_F(DexDiagTest, DexDiagContainsTest) {
111#else
112TEST_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)
122TEST_F(DexDiagTest, DexDiagContainsFailsTest) {
123#else
124TEST_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)
135TEST_F(DexDiagTest, DexDiagVerboseTest) {
136#else
137TEST_F(DexDiagTest, DISABLED_DexDiagVerboseTest) {
138#endif
David Sehr55232f12017-04-19 14:06:49 -0700139 // 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