1 /*
2  * Copyright (C) 2021 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 "tools/tools.h"
18 
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 
23 #include <filesystem>
24 
25 #include "android-base/file.h"
26 #include "android-base/result.h"
27 #include "base/common_art_test.h"
28 #include "base/globals.h"
29 #include "base/pidfd.h"
30 #include "base/time_utils.h"
31 #include "gmock/gmock.h"
32 #include "gtest/gtest.h"
33 #include "testing.h"
34 
35 namespace art {
36 namespace tools {
37 namespace {
38 
39 using ::android::base::Result;
40 using ::android::base::WriteStringToFile;
41 using ::testing::UnorderedElementsAre;
42 
CreateFile(const std::string & filename,const std::string & content="")43 void CreateFile(const std::string& filename, const std::string& content = "") {
44   std::filesystem::path path(filename);
45   std::filesystem::create_directories(path.parent_path());
46   ASSERT_TRUE(WriteStringToFile(content, filename));
47 }
48 
49 class ArtToolsTest : public CommonArtTest {
50  protected:
SetUp()51   void SetUp() override {
52     CommonArtTest::SetUp();
53     scratch_dir_ = std::make_unique<ScratchDir>();
54     scratch_path_ = scratch_dir_->GetPath();
55     // Remove the trailing '/';
56     scratch_path_.resize(scratch_path_.length() - 1);
57   }
58 
TearDown()59   void TearDown() override {
60     scratch_dir_.reset();
61     CommonArtTest::TearDown();
62   }
63 
64   std::unique_ptr<ScratchDir> scratch_dir_;
65   std::string scratch_path_;
66 };
67 
TEST_F(ArtToolsTest,Glob)68 TEST_F(ArtToolsTest, Glob) {
69   CreateFile(scratch_path_ + "/abc/def/000.txt");
70   CreateFile(scratch_path_ + "/abc/def/ghi/123.txt");
71   CreateFile(scratch_path_ + "/abc/def/ghi/456.txt");
72   CreateFile(scratch_path_ + "/abc/def/ghi/456.pdf");
73   CreateFile(scratch_path_ + "/abc/def/ghi/jkl/456.txt");
74   CreateFile(scratch_path_ + "/789.txt");
75   CreateFile(scratch_path_ + "/abc/789.txt");
76   CreateFile(scratch_path_ + "/abc/aaa/789.txt");
77   CreateFile(scratch_path_ + "/abc/aaa/bbb/789.txt");
78   CreateFile(scratch_path_ + "/abc/mno/123.txt");
79   CreateFile(scratch_path_ + "/abc/aaa/mno/123.txt");
80   CreateFile(scratch_path_ + "/abc/aaa/bbb/mno/123.txt");
81   CreateFile(scratch_path_ + "/abc/aaa/bbb/mno/ccc/123.txt");
82   CreateFile(scratch_path_ + "/pqr/123.txt");
83   CreateFile(scratch_path_ + "/abc/pqr/123.txt");
84   CreateFile(scratch_path_ + "/abc/aaa/pqr/123.txt");
85   CreateFile(scratch_path_ + "/abc/aaa/bbb/pqr/123.txt");
86   CreateFile(scratch_path_ + "/abc/aaa/bbb/pqr/ccc/123.txt");
87   CreateFile(scratch_path_ + "/abc/aaa/bbb/pqr/ccc/ddd/123.txt");
88 
89   // This symlink will cause infinite recursion. It should not be followed.
90   std::filesystem::create_directory_symlink(scratch_path_ + "/abc/aaa/bbb/pqr",
91                                             scratch_path_ + "/abc/aaa/bbb/pqr/lnk");
92 
93   // This is a directory. It should not be included in the results.
94   std::filesystem::create_directory(scratch_path_ + "/abc/def/ghi/000.txt");
95 
96   std::vector<std::string> patterns = {
97       scratch_path_ + "/abc/def/000.txt",
98       scratch_path_ + "/abc/def/ghi/*.txt",
99       scratch_path_ + "/abc/**/789.txt",
100       scratch_path_ + "/abc/**/mno/*.txt",
101       scratch_path_ + "/abc/**/pqr/**",
102   };
103 
104   EXPECT_THAT(Glob(patterns, scratch_path_),
105               UnorderedElementsAre(scratch_path_ + "/abc/def/000.txt",
106                                    scratch_path_ + "/abc/def/ghi/123.txt",
107                                    scratch_path_ + "/abc/def/ghi/456.txt",
108                                    scratch_path_ + "/abc/789.txt",
109                                    scratch_path_ + "/abc/aaa/789.txt",
110                                    scratch_path_ + "/abc/aaa/bbb/789.txt",
111                                    scratch_path_ + "/abc/mno/123.txt",
112                                    scratch_path_ + "/abc/aaa/mno/123.txt",
113                                    scratch_path_ + "/abc/aaa/bbb/mno/123.txt",
114                                    scratch_path_ + "/abc/pqr/123.txt",
115                                    scratch_path_ + "/abc/aaa/pqr/123.txt",
116                                    scratch_path_ + "/abc/aaa/bbb/pqr/123.txt",
117                                    scratch_path_ + "/abc/aaa/bbb/pqr/ccc/123.txt",
118                                    scratch_path_ + "/abc/aaa/bbb/pqr/ccc/ddd/123.txt"));
119 }
120 
TEST_F(ArtToolsTest,EscapeGlob)121 TEST_F(ArtToolsTest, EscapeGlob) {
122   CreateFile(scratch_path_ + "/**");
123   CreateFile(scratch_path_ + "/*.txt");
124   CreateFile(scratch_path_ + "/?.txt");
125   CreateFile(scratch_path_ + "/[a-z].txt");
126   CreateFile(scratch_path_ + "/**.txt");
127   CreateFile(scratch_path_ + "/??.txt");
128   CreateFile(scratch_path_ + "/[a-z[a-z]][a-z].txt");
129 
130   // Paths that shouldn't be matched if the paths above are escaped.
131   CreateFile(scratch_path_ + "/abc/b.txt");
132   CreateFile(scratch_path_ + "/b.txt");
133   CreateFile(scratch_path_ + "/*b.txt");
134   CreateFile(scratch_path_ + "/?b.txt");
135   CreateFile(scratch_path_ + "/[a-zb]b.txt");
136 
137   // Verifies that the escaped path only matches the given path.
138   auto verify_escape = [this](const std::string& file) {
139     EXPECT_THAT(Glob({EscapeGlob(file)}, scratch_path_), UnorderedElementsAre(file));
140   };
141 
142   verify_escape(scratch_path_ + "/**");
143   verify_escape(scratch_path_ + "/*.txt");
144   verify_escape(scratch_path_ + "/?.txt");
145   verify_escape(scratch_path_ + "/[a-z].txt");
146   verify_escape(scratch_path_ + "/**.txt");
147   verify_escape(scratch_path_ + "/**");
148   verify_escape(scratch_path_ + "/??.txt");
149   verify_escape(scratch_path_ + "/[a-z[a-z]][a-z].txt");
150 }
151 
TEST_F(ArtToolsTest,PathStartsWith)152 TEST_F(ArtToolsTest, PathStartsWith) {
153   EXPECT_TRUE(PathStartsWith("/a/b", "/a"));
154   EXPECT_TRUE(PathStartsWith("/a/b", "/a/"));
155 
156   EXPECT_FALSE(PathStartsWith("/a/c", "/a/b"));
157   EXPECT_FALSE(PathStartsWith("/ab", "/a"));
158 
159   EXPECT_TRUE(PathStartsWith("/a", "/a"));
160   EXPECT_TRUE(PathStartsWith("/a/", "/a"));
161   EXPECT_TRUE(PathStartsWith("/a", "/a/"));
162 
163   EXPECT_TRUE(PathStartsWith("/a", "/"));
164   EXPECT_TRUE(PathStartsWith("/", "/"));
165   EXPECT_FALSE(PathStartsWith("/", "/a"));
166 }
167 
168 class ArtToolsEnsureNoProcessInDirTest : public ArtToolsTest {
169  protected:
SetUp()170   void SetUp() override {
171     ArtToolsTest::SetUp();
172 
173     if (!PidfdOpen(getpid(), /*flags=*/0).ok() && errno == ENOSYS) {
174       GTEST_SKIP() << "'pidfd_open' is not available";
175     }
176 
177     related_dir_ = scratch_path_ + "/related";
178     unrelated_dir_ = scratch_path_ + "/unrelated";
179 
180     std::string sleep_bin = GetSleepBin();
181     if (sleep_bin.empty()) {
182       GTEST_SKIP() << "'sleep' is not available";
183     }
184 
185     std::filesystem::create_directories(related_dir_);
186     std::filesystem::create_directories(unrelated_dir_);
187     std::filesystem::copy(sleep_bin, related_dir_ + "/sleep");
188     std::filesystem::copy(sleep_bin, unrelated_dir_ + "/sleep");
189   }
190 
191   std::string related_dir_;
192   std::string unrelated_dir_;
193 
194  private:
GetSleepBin()195   std::string GetSleepBin() {
196     if constexpr (kIsTargetAndroid) {
197       return GetBin("sleep");
198     }
199     if (access("/usr/bin/sleep", X_OK) == 0) {
200       return "/usr/bin/sleep";
201     }
202     return "";
203   }
204 };
205 
TEST_F(ArtToolsEnsureNoProcessInDirTest,WaitsProcesses)206 TEST_F(ArtToolsEnsureNoProcessInDirTest, WaitsProcesses) {
207   std::vector<std::string> args_1{related_dir_ + "/sleep", "0.3"};
208   auto [pid_1, scope_guard_1] = ScopedExec(args_1, /*wait=*/false);
209   std::vector<std::string> args_2{unrelated_dir_ + "/sleep", "2"};
210   auto [pid_2, scope_guard_2] = ScopedExec(args_2, /*wait=*/false);
211   NanoSleep(100'000'000);  // Wait for child processes to exec.
212 
213   ASSERT_RESULT_OK(EnsureNoProcessInDir(related_dir_, /*timeout_ms=*/5000, /*try_kill=*/false));
214 
215   // Check the current status of the process with `WNOHANG`. The related process should have exited,
216   // so `si_signo` should be `SIGCHLD`.
217   siginfo_t info;
218   ASSERT_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid_1, &info, WEXITED | WNOWAIT | WNOHANG)), 0);
219   EXPECT_EQ(info.si_signo, SIGCHLD);
220   EXPECT_EQ(info.si_code, CLD_EXITED);
221   EXPECT_EQ(info.si_status, 0);
222 
223   // The method should not wait on unrelated processes. The unrelated process should not have
224   // exited, so `si_signo` should be 0.
225   ASSERT_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid_2, &info, WEXITED | WNOWAIT | WNOHANG)), 0);
226   EXPECT_EQ(info.si_signo, 0);
227 }
228 
TEST_F(ArtToolsEnsureNoProcessInDirTest,TimesOut)229 TEST_F(ArtToolsEnsureNoProcessInDirTest, TimesOut) {
230   std::vector<std::string> args{related_dir_ + "/sleep", "5"};
231   auto [pid, scope_guard] = ScopedExec(args, /*wait=*/false);
232   NanoSleep(100'000'000);  // Wait for child processes to exec.
233 
234   Result<void> result = EnsureNoProcessInDir(related_dir_, /*timeout_ms=*/200, /*try_kill=*/false);
235   EXPECT_FALSE(result.ok());
236   EXPECT_EQ(result.error().message(), "Some process(es) are still running after 200ms");
237 
238   // The process should not have exited.
239   siginfo_t info;
240   ASSERT_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED | WNOWAIT | WNOHANG)), 0);
241   EXPECT_EQ(info.si_signo, 0);
242 }
243 
TEST_F(ArtToolsEnsureNoProcessInDirTest,KillsProcesses)244 TEST_F(ArtToolsEnsureNoProcessInDirTest, KillsProcesses) {
245   std::vector<std::string> args_1{related_dir_ + "/sleep", "5"};
246   auto [pid_1, scope_guard_1] = ScopedExec(args_1, /*wait=*/false);
247   std::vector<std::string> args_2{unrelated_dir_ + "/sleep", "5"};
248   auto [pid_2, scope_guard_2] = ScopedExec(args_2, /*wait=*/false);
249   NanoSleep(100'000'000);  // Wait for child processes to exec.
250 
251   ASSERT_RESULT_OK(EnsureNoProcessInDir(related_dir_, /*timeout_ms=*/200, /*try_kill=*/true));
252 
253   // The related process should have been killed.
254   siginfo_t info;
255   ASSERT_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid_1, &info, WEXITED | WNOWAIT | WNOHANG)), 0);
256   EXPECT_EQ(info.si_signo, SIGCHLD);
257   EXPECT_EQ(info.si_code, CLD_KILLED);
258   EXPECT_EQ(info.si_status, SIGKILL);
259 
260   // The unrelated process should still be running.
261   ASSERT_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid_2, &info, WEXITED | WNOWAIT | WNOHANG)), 0);
262   EXPECT_EQ(info.si_signo, 0);
263 }
264 
265 }  // namespace
266 }  // namespace tools
267 }  // namespace art
268