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