blob: 3245795132f0e1a1f8720ead1e6fb6002933c18a [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include <sstream>
Igor Murashkin37743352014-11-13 14:38:00 -080018#include <string>
19#include <vector>
Igor Murashkin37743352014-11-13 14:38:00 -080020
21#include "common_runtime_test.h"
22
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Igor Murashkin37743352014-11-13 14:38:00 -080025#include "runtime/arch/instruction_set.h"
David Sehr97c381e2017-02-01 15:09:58 -080026#include "runtime/exec_utils.h"
Igor Murashkin37743352014-11-13 14:38:00 -080027#include "runtime/gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "runtime/gc/space/image_space.h"
29#include "runtime/os.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070030#include "runtime/runtime.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "runtime/utils.h"
Igor Murashkin37743352014-11-13 14:38:00 -080032
33#include <sys/types.h>
34#include <unistd.h>
35
36namespace art {
37
38static const char* kImgDiagDiffPid = "--image-diff-pid";
39static const char* kImgDiagBootImage = "--boot-image";
40static const char* kImgDiagBinaryName = "imgdiag";
41
Mathieu Chartier1398cf22016-04-08 14:08:37 -070042static const char* kImgDiagZygoteDiffPid = "--zygote-diff-pid";
43
Igor Murashkinddd21722015-12-03 10:47:08 -080044// from kernel <include/linux/threads.h>
45#define PID_MAX_LIMIT (4*1024*1024) // Upper bound. Most kernel configs will have smaller max pid.
46
47static const pid_t kImgDiagGuaranteedBadPid = (PID_MAX_LIMIT + 1);
48
Igor Murashkin37743352014-11-13 14:38:00 -080049class ImgDiagTest : public CommonRuntimeTest {
50 protected:
51 virtual void SetUp() {
52 CommonRuntimeTest::SetUp();
53
54 // We loaded the runtime with an explicit image. Therefore the image space must exist.
Jeff Haodcdc85b2015-12-04 14:06:18 -080055 std::vector<gc::space::ImageSpace*> image_spaces =
56 Runtime::Current()->GetHeap()->GetBootImageSpaces();
57 ASSERT_TRUE(!image_spaces.empty());
58 boot_image_location_ = image_spaces[0]->GetImageLocation();
Igor Murashkin37743352014-11-13 14:38:00 -080059 }
60
61 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
62 // Needs to live until CommonRuntimeTest::SetUp finishes, since we pass it a cstring.
Andreas Gampe46ee31b2016-12-14 10:11:49 -080063 runtime_args_image_ = android::base::StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str());
Igor Murashkin37743352014-11-13 14:38:00 -080064 options->push_back(std::make_pair(runtime_args_image_, nullptr));
65 }
66
67 // Path to the imgdiag(d?)[32|64] binary.
68 std::string GetImgDiagFilePath() {
69 std::string root = GetTestAndroidRoot();
70
71 root += "/bin/";
72 root += kImgDiagBinaryName;
73
74 if (kIsDebugBuild) {
75 root += "d";
76 }
77
78 std::string root32 = root + "32";
79 // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix.
80 if (OS::FileExists(root32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) {
81 return root32;
82 // Only a single build exists, so the filename never has an extra suffix.
83 } else {
84 return root;
85 }
86 }
87
88 // Run imgdiag with a custom boot image location.
89 bool Exec(pid_t image_diff_pid, const std::string& boot_image, std::string* error_msg) {
90 // Invoke 'img_diag' against the current process.
91 // This should succeed because we have a runtime and so it should
92 // be able to map in the boot.art and do a diff for it.
93 std::string file_path = GetImgDiagFilePath();
94 EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
95
96 // Run imgdiag --image-diff-pid=$image_diff_pid and wait until it's done with a 0 exit code.
97 std::string diff_pid_args;
Mathieu Chartier1398cf22016-04-08 14:08:37 -070098 std::string zygote_diff_pid_args;
Igor Murashkin37743352014-11-13 14:38:00 -080099 {
100 std::stringstream diff_pid_args_ss;
101 diff_pid_args_ss << kImgDiagDiffPid << "=" << image_diff_pid;
102 diff_pid_args = diff_pid_args_ss.str();
103 }
Igor Murashkin37743352014-11-13 14:38:00 -0800104 {
Mathieu Chartier1398cf22016-04-08 14:08:37 -0700105 std::stringstream zygote_pid_args_ss;
106 zygote_pid_args_ss << kImgDiagZygoteDiffPid << "=" << image_diff_pid;
107 zygote_diff_pid_args = zygote_pid_args_ss.str();
Igor Murashkin37743352014-11-13 14:38:00 -0800108 }
Mathieu Chartier1398cf22016-04-08 14:08:37 -0700109 std::string boot_image_args = std::string(kImgDiagBootImage) + "=" + boot_image;
Igor Murashkin37743352014-11-13 14:38:00 -0800110
Mathieu Chartier1398cf22016-04-08 14:08:37 -0700111 std::vector<std::string> exec_argv = {
112 file_path,
113 diff_pid_args,
114 zygote_diff_pid_args,
115 boot_image_args
116 };
Igor Murashkin37743352014-11-13 14:38:00 -0800117
118 return ::art::Exec(exec_argv, error_msg);
119 }
120
121 // Run imgdiag with the default boot image location.
122 bool ExecDefaultBootImage(pid_t image_diff_pid, std::string* error_msg) {
123 return Exec(image_diff_pid, boot_image_location_, error_msg);
124 }
125
126 private:
127 std::string runtime_args_image_;
128 std::string boot_image_location_;
129};
130
Nicolas Geoffray51c6c182015-10-02 14:41:18 +0100131#if defined (ART_TARGET) && !defined(__mips__)
Igor Murashkin37743352014-11-13 14:38:00 -0800132TEST_F(ImgDiagTest, ImageDiffPidSelf) {
133#else
134// Can't run this test on the host, it will fail when trying to open /proc/kpagestats
135// because it's root read-only.
Nicolas Geoffray51c6c182015-10-02 14:41:18 +0100136// Also test fails on mips. b/24596015.
Igor Murashkin37743352014-11-13 14:38:00 -0800137TEST_F(ImgDiagTest, DISABLED_ImageDiffPidSelf) {
138#endif
139 // Invoke 'img_diag' against the current process.
140 // This should succeed because we have a runtime and so it should
141 // be able to map in the boot.art and do a diff for it.
142
143 // Run imgdiag --image-diff-pid=$(self pid) and wait until it's done with a 0 exit code.
144 std::string error_msg;
145 ASSERT_TRUE(ExecDefaultBootImage(getpid(), &error_msg)) << "Failed to execute -- because: "
146 << error_msg;
147}
148
149TEST_F(ImgDiagTest, ImageDiffBadPid) {
150 // Invoke 'img_diag' against a non-existing process. This should fail.
151
152 // Run imgdiag --image-diff-pid=some_bad_pid and wait until it's done with a 0 exit code.
153 std::string error_msg;
Igor Murashkinddd21722015-12-03 10:47:08 -0800154 ASSERT_FALSE(ExecDefaultBootImage(kImgDiagGuaranteedBadPid,
155 &error_msg)) << "Incorrectly executed";
Igor Murashkin37743352014-11-13 14:38:00 -0800156 UNUSED(error_msg);
157}
158
159} // namespace art