xref: /aosp_15_r20/art/imgdiag/imgdiag_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <sys/types.h>
18*795d594fSAndroid Build Coastguard Worker #include <unistd.h>
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <sstream>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker #include <vector>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/os.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
29*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
30*795d594fSAndroid Build Coastguard Worker #include "exec_utils.h"
31*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
32*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
33*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker namespace art {
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker static const char* kImgDiagBinaryName = "imgdiag";
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker // from kernel <include/linux/threads.h>
40*795d594fSAndroid Build Coastguard Worker #define PID_MAX_LIMIT (4*1024*1024)  // Upper bound. Most kernel configs will have smaller max pid.
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker static const pid_t kImgDiagGuaranteedBadPid = (PID_MAX_LIMIT + 1);
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker class ImgDiagTest : public CommonRuntimeTest {
45*795d594fSAndroid Build Coastguard Worker  protected:
SetUp()46*795d594fSAndroid Build Coastguard Worker   void SetUp() override {
47*795d594fSAndroid Build Coastguard Worker     CommonRuntimeTest::SetUp();
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker     // We loaded the runtime with an explicit image. Therefore the image space must exist.
50*795d594fSAndroid Build Coastguard Worker     std::vector<gc::space::ImageSpace*> image_spaces =
51*795d594fSAndroid Build Coastguard Worker         Runtime::Current()->GetHeap()->GetBootImageSpaces();
52*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(!image_spaces.empty());
53*795d594fSAndroid Build Coastguard Worker     boot_image_location_ = image_spaces[0]->GetImageLocation();
54*795d594fSAndroid Build Coastguard Worker   }
55*795d594fSAndroid Build Coastguard Worker 
SetUpRuntimeOptions(RuntimeOptions * options)56*795d594fSAndroid Build Coastguard Worker   void SetUpRuntimeOptions(RuntimeOptions* options) override {
57*795d594fSAndroid Build Coastguard Worker     // Needs to live until CommonRuntimeTest::SetUp finishes, since we pass it a cstring.
58*795d594fSAndroid Build Coastguard Worker     runtime_args_image_ = android::base::StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str());
59*795d594fSAndroid Build Coastguard Worker     options->push_back(std::make_pair(runtime_args_image_, nullptr));
60*795d594fSAndroid Build Coastguard Worker   }
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker   // Path to the imgdiag(d?)[32|64] binary.
GetImgDiagFilePath()63*795d594fSAndroid Build Coastguard Worker   std::string GetImgDiagFilePath() {
64*795d594fSAndroid Build Coastguard Worker     std::string path = GetArtBinDir() + '/' + kImgDiagBinaryName;
65*795d594fSAndroid Build Coastguard Worker     if (kIsDebugBuild) {
66*795d594fSAndroid Build Coastguard Worker       path += 'd';
67*795d594fSAndroid Build Coastguard Worker     }
68*795d594fSAndroid Build Coastguard Worker     std::string path32 = path + "32";
69*795d594fSAndroid Build Coastguard Worker     // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix.
70*795d594fSAndroid Build Coastguard Worker     if (OS::FileExists(path32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) {
71*795d594fSAndroid Build Coastguard Worker       return path32;
72*795d594fSAndroid Build Coastguard Worker     // Only a single build exists, so the filename never has an extra suffix.
73*795d594fSAndroid Build Coastguard Worker     } else {
74*795d594fSAndroid Build Coastguard Worker       return path;
75*795d594fSAndroid Build Coastguard Worker     }
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker   // Run imgdiag with a custom boot image location.
Exec(pid_t image_diff_pid,const std::string & boot_image,std::string * error_msg)79*795d594fSAndroid Build Coastguard Worker   bool Exec(pid_t image_diff_pid, const std::string& boot_image, std::string* error_msg) {
80*795d594fSAndroid Build Coastguard Worker     // Invoke 'img_diag' against the current process.
81*795d594fSAndroid Build Coastguard Worker     // This should succeed because we have a runtime and so it should
82*795d594fSAndroid Build Coastguard Worker     // be able to map in the boot.art and do a diff for it.
83*795d594fSAndroid Build Coastguard Worker     std::string file_path = GetImgDiagFilePath();
84*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker     // Run imgdiag --image-diff-pid=$image_diff_pid and wait until it's done with a 0 exit code.
87*795d594fSAndroid Build Coastguard Worker     std::vector<std::string> exec_argv = {
88*795d594fSAndroid Build Coastguard Worker         file_path,
89*795d594fSAndroid Build Coastguard Worker         "--image-diff-pid=" + std::to_string(image_diff_pid),
90*795d594fSAndroid Build Coastguard Worker         "--zygote-diff-pid=" + std::to_string(image_diff_pid),
91*795d594fSAndroid Build Coastguard Worker         "--runtime-arg",
92*795d594fSAndroid Build Coastguard Worker         GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()),
93*795d594fSAndroid Build Coastguard Worker         "--runtime-arg",
94*795d594fSAndroid Build Coastguard Worker         GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()),
95*795d594fSAndroid Build Coastguard Worker         "--boot-image=" + boot_image
96*795d594fSAndroid Build Coastguard Worker     };
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker     return ::art::Exec(exec_argv, error_msg);
99*795d594fSAndroid Build Coastguard Worker   }
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker   // Run imgdiag with the default boot image location.
ExecDefaultBootImage(pid_t image_diff_pid,std::string * error_msg)102*795d594fSAndroid Build Coastguard Worker   bool ExecDefaultBootImage(pid_t image_diff_pid, std::string* error_msg) {
103*795d594fSAndroid Build Coastguard Worker     return Exec(image_diff_pid, boot_image_location_, error_msg);
104*795d594fSAndroid Build Coastguard Worker   }
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker  private:
107*795d594fSAndroid Build Coastguard Worker   std::string runtime_args_image_;
108*795d594fSAndroid Build Coastguard Worker   std::string boot_image_location_;
109*795d594fSAndroid Build Coastguard Worker };
110*795d594fSAndroid Build Coastguard Worker 
111*795d594fSAndroid Build Coastguard Worker #if defined (ART_TARGET)
TEST_F(ImgDiagTest,ImageDiffPidSelf)112*795d594fSAndroid Build Coastguard Worker TEST_F(ImgDiagTest, ImageDiffPidSelf) {
113*795d594fSAndroid Build Coastguard Worker #else
114*795d594fSAndroid Build Coastguard Worker // Can't run this test on the host, it will fail when trying to open /proc/kpagestats
115*795d594fSAndroid Build Coastguard Worker // because it's root read-only.
116*795d594fSAndroid Build Coastguard Worker TEST_F(ImgDiagTest, DISABLED_ImageDiffPidSelf) {
117*795d594fSAndroid Build Coastguard Worker #endif
118*795d594fSAndroid Build Coastguard Worker   // Invoke 'img_diag' against the current process.
119*795d594fSAndroid Build Coastguard Worker   // This should succeed because we have a runtime and so it should
120*795d594fSAndroid Build Coastguard Worker   // be able to map in the boot.art and do a diff for it.
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker   // Not allowed to read /proc/kpagestats on VM (in the same way as on host).
123*795d594fSAndroid Build Coastguard Worker   TEST_DISABLED_ON_VM();
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker   // Run imgdiag --image-diff-pid=$(self pid) and wait until it's done with a 0 exit code.
126*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
127*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(ExecDefaultBootImage(getpid(), &error_msg)) << "Failed to execute -- because: "
128*795d594fSAndroid Build Coastguard Worker                                                           << error_msg;
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker TEST_F(ImgDiagTest, ImageDiffBadPid) {
132*795d594fSAndroid Build Coastguard Worker   // Invoke 'img_diag' against a non-existing process. This should fail.
133*795d594fSAndroid Build Coastguard Worker 
134*795d594fSAndroid Build Coastguard Worker   // Run imgdiag --image-diff-pid=some_bad_pid and wait until it's done with a 0 exit code.
135*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
136*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(ExecDefaultBootImage(kImgDiagGuaranteedBadPid,
137*795d594fSAndroid Build Coastguard Worker                                     &error_msg)) << "Incorrectly executed";
138*795d594fSAndroid Build Coastguard Worker   UNUSED(error_msg);
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker }  // namespace art
142