Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 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 | |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 17 | #include <sys/types.h> |
| 18 | #include <unistd.h> |
| 19 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <sstream> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 23 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 24 | #include "android-base/stringprintf.h" |
| 25 | |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 26 | #include "arch/instruction_set.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 27 | #include "base/os.h" |
| 28 | #include "base/utils.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 29 | #include "common_runtime_test.h" |
| 30 | #include "exec_utils.h" |
| 31 | #include "gc/heap.h" |
| 32 | #include "gc/space/image_space.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 33 | #include "runtime.h" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 37 | static const char* kImgDiagBinaryName = "imgdiag"; |
| 38 | |
Igor Murashkin | ddd2172 | 2015-12-03 10:47:08 -0800 | [diff] [blame] | 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 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 44 | class ImgDiagTest : public CommonRuntimeTest { |
| 45 | protected: |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 46 | void SetUp() override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 47 | CommonRuntimeTest::SetUp(); |
| 48 | |
| 49 | // We loaded the runtime with an explicit image. Therefore the image space must exist. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 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(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 56 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 57 | // Needs to live until CommonRuntimeTest::SetUp finishes, since we pass it a cstring. |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 58 | runtime_args_image_ = android::base::StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str()); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 59 | options->push_back(std::make_pair(runtime_args_image_, nullptr)); |
| 60 | } |
| 61 | |
| 62 | // Path to the imgdiag(d?)[32|64] binary. |
| 63 | std::string GetImgDiagFilePath() { |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 64 | std::string path = GetArtBinDir() + '/' + kImgDiagBinaryName; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 65 | if (kIsDebugBuild) { |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 66 | path += 'd'; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 67 | } |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 68 | std::string path32 = path + "32"; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 69 | // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix. |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 70 | if (OS::FileExists(path32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) { |
| 71 | return path32; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 72 | // Only a single build exists, so the filename never has an extra suffix. |
| 73 | } else { |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 74 | return path; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | // Run imgdiag with a custom boot image location. |
| 79 | bool Exec(pid_t image_diff_pid, const std::string& boot_image, std::string* error_msg) { |
| 80 | // Invoke 'img_diag' against the current process. |
| 81 | // This should succeed because we have a runtime and so it should |
| 82 | // be able to map in the boot.art and do a diff for it. |
| 83 | std::string file_path = GetImgDiagFilePath(); |
| 84 | EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path"; |
| 85 | |
| 86 | // Run imgdiag --image-diff-pid=$image_diff_pid and wait until it's done with a 0 exit code. |
Mathieu Chartier | 1398cf2 | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 87 | std::vector<std::string> exec_argv = { |
| 88 | file_path, |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 89 | "--image-diff-pid=" + std::to_string(image_diff_pid), |
| 90 | "--zygote-diff-pid=" + std::to_string(image_diff_pid), |
| 91 | "--runtime-arg", |
| 92 | GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), |
| 93 | "--runtime-arg", |
| 94 | GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), |
| 95 | "--boot-image=" + boot_image |
Mathieu Chartier | 1398cf2 | 2016-04-08 14:08:37 -0700 | [diff] [blame] | 96 | }; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 97 | |
| 98 | return ::art::Exec(exec_argv, error_msg); |
| 99 | } |
| 100 | |
| 101 | // Run imgdiag with the default boot image location. |
| 102 | bool ExecDefaultBootImage(pid_t image_diff_pid, std::string* error_msg) { |
| 103 | return Exec(image_diff_pid, boot_image_location_, error_msg); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | std::string runtime_args_image_; |
| 108 | std::string boot_image_location_; |
| 109 | }; |
| 110 | |
Vladimir Marko | be0d3cf | 2020-02-12 10:52:22 +0000 | [diff] [blame] | 111 | #if defined (ART_TARGET) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 112 | TEST_F(ImgDiagTest, ImageDiffPidSelf) { |
| 113 | #else |
| 114 | // Can't run this test on the host, it will fail when trying to open /proc/kpagestats |
| 115 | // because it's root read-only. |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 116 | TEST_F(ImgDiagTest, DISABLED_ImageDiffPidSelf) { |
| 117 | #endif |
| 118 | // Invoke 'img_diag' against the current process. |
| 119 | // This should succeed because we have a runtime and so it should |
| 120 | // be able to map in the boot.art and do a diff for it. |
| 121 | |
| 122 | // Run imgdiag --image-diff-pid=$(self pid) and wait until it's done with a 0 exit code. |
| 123 | std::string error_msg; |
| 124 | ASSERT_TRUE(ExecDefaultBootImage(getpid(), &error_msg)) << "Failed to execute -- because: " |
| 125 | << error_msg; |
| 126 | } |
| 127 | |
| 128 | TEST_F(ImgDiagTest, ImageDiffBadPid) { |
| 129 | // Invoke 'img_diag' against a non-existing process. This should fail. |
| 130 | |
| 131 | // Run imgdiag --image-diff-pid=some_bad_pid and wait until it's done with a 0 exit code. |
| 132 | std::string error_msg; |
Igor Murashkin | ddd2172 | 2015-12-03 10:47:08 -0800 | [diff] [blame] | 133 | ASSERT_FALSE(ExecDefaultBootImage(kImgDiagGuaranteedBadPid, |
| 134 | &error_msg)) << "Incorrectly executed"; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 135 | UNUSED(error_msg); |
| 136 | } |
| 137 | |
| 138 | } // namespace art |