1 /*
2  * Copyright (C) 2014 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 <sys/types.h>
18 #include <unistd.h>
19 
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "android-base/stringprintf.h"
25 
26 #include "arch/instruction_set.h"
27 #include "base/os.h"
28 #include "base/utils.h"
29 #include "common_runtime_test.h"
30 #include "exec_utils.h"
31 #include "gc/heap.h"
32 #include "gc/space/image_space.h"
33 #include "runtime.h"
34 
35 namespace art {
36 
37 static const char* kImgDiagBinaryName = "imgdiag";
38 
39 // from kernel <include/linux/threads.h>
40 #define PID_MAX_LIMIT (4*1024*1024)  // Upper bound. Most kernel configs will have smaller max pid.
41 
42 static const pid_t kImgDiagGuaranteedBadPid = (PID_MAX_LIMIT + 1);
43 
44 class ImgDiagTest : public CommonRuntimeTest {
45  protected:
SetUp()46   void SetUp() override {
47     CommonRuntimeTest::SetUp();
48 
49     // We loaded the runtime with an explicit image. Therefore the image space must exist.
50     std::vector<gc::space::ImageSpace*> image_spaces =
51         Runtime::Current()->GetHeap()->GetBootImageSpaces();
52     ASSERT_TRUE(!image_spaces.empty());
53     boot_image_location_ = image_spaces[0]->GetImageLocation();
54   }
55 
SetUpRuntimeOptions(RuntimeOptions * options)56   void SetUpRuntimeOptions(RuntimeOptions* options) override {
57     // Needs to live until CommonRuntimeTest::SetUp finishes, since we pass it a cstring.
58     runtime_args_image_ = android::base::StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str());
59     options->push_back(std::make_pair(runtime_args_image_, nullptr));
60   }
61 
62   // Path to the imgdiag(d?)[32|64] binary.
GetImgDiagFilePath()63   std::string GetImgDiagFilePath() {
64     std::string root = GetTestAndroidRoot();
65 
66     root += "/bin/";
67     root += kImgDiagBinaryName;
68 
69     if (kIsDebugBuild) {
70       root += "d";
71     }
72 
73     std::string root32 = root + "32";
74     // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix.
75     if (OS::FileExists(root32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) {
76       return root32;
77     // Only a single build exists, so the filename never has an extra suffix.
78     } else {
79       return root;
80     }
81   }
82 
83   // Run imgdiag with a custom boot image location.
Exec(pid_t image_diff_pid,const std::string & boot_image,std::string * error_msg)84   bool Exec(pid_t image_diff_pid, const std::string& boot_image, std::string* error_msg) {
85     // Invoke 'img_diag' against the current process.
86     // This should succeed because we have a runtime and so it should
87     // be able to map in the boot.art and do a diff for it.
88     std::string file_path = GetImgDiagFilePath();
89     EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
90 
91     // Run imgdiag --image-diff-pid=$image_diff_pid and wait until it's done with a 0 exit code.
92     std::vector<std::string> exec_argv = {
93         file_path,
94         "--image-diff-pid=" + std::to_string(image_diff_pid),
95         "--zygote-diff-pid=" + std::to_string(image_diff_pid),
96         "--runtime-arg",
97         GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()),
98         "--runtime-arg",
99         GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()),
100         "--boot-image=" + boot_image
101     };
102 
103     return ::art::Exec(exec_argv, error_msg);
104   }
105 
106   // Run imgdiag with the default boot image location.
ExecDefaultBootImage(pid_t image_diff_pid,std::string * error_msg)107   bool ExecDefaultBootImage(pid_t image_diff_pid, std::string* error_msg) {
108     return Exec(image_diff_pid, boot_image_location_, error_msg);
109   }
110 
111  private:
112   std::string runtime_args_image_;
113   std::string boot_image_location_;
114 };
115 
116 #if defined (ART_TARGET) && !defined(__mips__)
TEST_F(ImgDiagTest,ImageDiffPidSelf)117 TEST_F(ImgDiagTest, ImageDiffPidSelf) {
118 #else
119 // Can't run this test on the host, it will fail when trying to open /proc/kpagestats
120 // because it's root read-only.
121 // Also test fails on mips. b/24596015.
122 TEST_F(ImgDiagTest, DISABLED_ImageDiffPidSelf) {
123 #endif
124   // Invoke 'img_diag' against the current process.
125   // This should succeed because we have a runtime and so it should
126   // be able to map in the boot.art and do a diff for it.
127 
128   // Run imgdiag --image-diff-pid=$(self pid) and wait until it's done with a 0 exit code.
129   std::string error_msg;
130   ASSERT_TRUE(ExecDefaultBootImage(getpid(), &error_msg)) << "Failed to execute -- because: "
131                                                           << error_msg;
132 }
133 
134 TEST_F(ImgDiagTest, ImageDiffBadPid) {
135   // Invoke 'img_diag' against a non-existing process. This should fail.
136 
137   // Run imgdiag --image-diff-pid=some_bad_pid and wait until it's done with a 0 exit code.
138   std::string error_msg;
139   ASSERT_FALSE(ExecDefaultBootImage(kImgDiagGuaranteedBadPid,
140                                     &error_msg)) << "Incorrectly executed";
141   UNUSED(error_msg);
142 }
143 
144 }  // namespace art
145