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 "base/common_art_test.h"
21 #include "base/file_utils.h"
22 #include "base/os.h"
23 #include "exec_utils.h"
24 #include "oat_file.h"
25 
26 namespace art {
27 
28 static const char* kDexDiagContains = "--contains=boot.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 
34 class DexDiagTest : public CommonArtTest {
35  protected:
SetUp()36   void SetUp() override {
37     CommonArtTest::SetUp();
38   }
39 
40   // Path to the dexdiag(d?)[32|64] binary.
GetDexDiagFilePath()41   std::string GetDexDiagFilePath() {
42     std::string path = GetArtBinDir() + '/' + kDexDiagBinaryName;
43     std::string path32 = path + "32";
44     // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix.
45     if (OS::FileExists(path32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) {
46       return path32;
47     } else {
48       // This is a 64-bit build or only a single build exists.
49       return path;
50     }
51   }
52 
OpenOatAndVdexFiles()53   std::unique_ptr<OatFile> OpenOatAndVdexFiles() {
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;
66     std::unique_ptr<OatFile> oat(OatFile::Open(/*zip_fd=*/ -1,
67                                                oat_location.c_str(),
68                                                oat_location.c_str(),
69                                                /*executable=*/ false,
70                                                /*low_4gb=*/ false,
71                                                &error_msg));
72     EXPECT_TRUE(oat != nullptr) << error_msg;
73     return oat;
74   }
75 
76   // Run dexdiag with a custom boot image location.
Exec(pid_t this_pid,const std::vector<std::string> & args,std::string * error_msg)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.
TEST_F(DexDiagTest,DexDiagHelpTest)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)
TEST_F(DexDiagTest,DexDiagContainsTest)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
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
147