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