xref: /llvm-project/llvm/unittests/Support/ProgramTest.cpp (revision 15a6e3c636977dc962a415c067182e6d57242116)
1 //===- unittest/Support/ProgramTest.cpp -----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/Program.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Support/ConvertUTF.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Signals.h"
16 #include "gtest/gtest.h"
17 #include <stdlib.h>
18 #include <thread>
19 #if defined(__APPLE__)
20 # include <crt_externs.h>
21 #elif !defined(_MSC_VER)
22 // Forward declare environ in case it's not provided by stdlib.h.
23 extern char **environ;
24 #endif
25 
26 #if defined(LLVM_ON_UNIX)
27 #include <unistd.h>
28 void sleep_for(unsigned int seconds) {
29   sleep(seconds);
30 }
31 #elif defined(_WIN32)
32 #include <windows.h>
33 void sleep_for(unsigned int seconds) {
34   Sleep(seconds * 1000);
35 }
36 #else
37 #error sleep_for is not implemented on your platform.
38 #endif
39 
40 #define ASSERT_NO_ERROR(x)                                                     \
41   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
42     SmallString<128> MessageStorage;                                           \
43     raw_svector_ostream Message(MessageStorage);                               \
44     Message << #x ": did not return errc::success.\n"                          \
45             << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"          \
46             << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";      \
47     GTEST_FATAL_FAILURE_(MessageStorage.c_str());                              \
48   } else {                                                                     \
49   }
50 // From TestMain.cpp.
51 extern const char *TestMainArgv0;
52 
53 namespace {
54 
55 using namespace llvm;
56 using namespace sys;
57 
58 static cl::opt<std::string>
59 ProgramTestStringArg1("program-test-string-arg1");
60 static cl::opt<std::string>
61 ProgramTestStringArg2("program-test-string-arg2");
62 
63 class ProgramEnvTest : public testing::Test {
64   std::vector<StringRef> EnvTable;
65   std::vector<std::string> EnvStorage;
66 
67 protected:
68   void SetUp() override {
69     auto EnvP = [] {
70 #if defined(_WIN32)
71       _wgetenv(L"TMP"); // Populate _wenviron, initially is null
72       return _wenviron;
73 #elif defined(__APPLE__)
74       return *_NSGetEnviron();
75 #else
76       return environ;
77 #endif
78     }();
79     ASSERT_TRUE(EnvP);
80 
81     auto prepareEnvVar = [this](decltype(*EnvP) Var) -> StringRef {
82 #if defined(_WIN32)
83       // On Windows convert UTF16 encoded variable to UTF8
84       auto Len = wcslen(Var);
85       ArrayRef<char> Ref{reinterpret_cast<char const *>(Var),
86                          Len * sizeof(*Var)};
87       EnvStorage.emplace_back();
88       auto convStatus = convertUTF16ToUTF8String(Ref, EnvStorage.back());
89       EXPECT_TRUE(convStatus);
90       return EnvStorage.back();
91 #else
92       (void)this;
93       return StringRef(Var);
94 #endif
95     };
96 
97     while (*EnvP != nullptr) {
98       auto S = prepareEnvVar(*EnvP);
99       if (!StringRef(S).startswith("GTEST_"))
100         EnvTable.emplace_back(S);
101       ++EnvP;
102     }
103   }
104 
105   void TearDown() override {
106     EnvTable.clear();
107     EnvStorage.clear();
108   }
109 
110   void addEnvVar(StringRef Var) { EnvTable.emplace_back(Var); }
111 
112   ArrayRef<StringRef> getEnviron() const { return EnvTable; }
113 };
114 
115 #ifdef _WIN32
116 void checkSeparators(StringRef Path) {
117   char UndesiredSeparator = sys::path::get_separator()[0] == '/' ? '\\' : '/';
118   ASSERT_EQ(Path.find(UndesiredSeparator), StringRef::npos);
119 }
120 
121 TEST_F(ProgramEnvTest, CreateProcessLongPath) {
122   if (getenv("LLVM_PROGRAM_TEST_LONG_PATH"))
123     exit(0);
124 
125   // getMainExecutable returns an absolute path; prepend the long-path prefix.
126   SmallString<128> MyAbsExe(
127       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1));
128   checkSeparators(MyAbsExe);
129   // Force a path with backslashes, when we are going to prepend the \\?\
130   // prefix.
131   sys::path::native(MyAbsExe, sys::path::Style::windows_backslash);
132   std::string MyExe;
133   if (!StringRef(MyAbsExe).startswith("\\\\?\\"))
134     MyExe.append("\\\\?\\");
135   MyExe.append(std::string(MyAbsExe.begin(), MyAbsExe.end()));
136 
137   StringRef ArgV[] = {MyExe,
138                       "--gtest_filter=ProgramEnvTest.CreateProcessLongPath"};
139 
140   // Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
141   addEnvVar("LLVM_PROGRAM_TEST_LONG_PATH=1");
142 
143   // Redirect stdout to a long path.
144   SmallString<128> TestDirectory;
145   ASSERT_NO_ERROR(
146     fs::createUniqueDirectory("program-redirect-test", TestDirectory));
147   SmallString<256> LongPath(TestDirectory);
148   LongPath.push_back('\\');
149   // MAX_PATH = 260
150   LongPath.append(260 - TestDirectory.size(), 'a');
151 
152   std::string Error;
153   bool ExecutionFailed;
154   std::optional<StringRef> Redirects[] = {std::nullopt, LongPath.str(),
155                                           std::nullopt};
156   int RC = ExecuteAndWait(MyExe, ArgV, getEnviron(), Redirects,
157     /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &Error,
158     &ExecutionFailed);
159   EXPECT_FALSE(ExecutionFailed) << Error;
160   EXPECT_EQ(0, RC);
161 
162   // Remove the long stdout.
163   ASSERT_NO_ERROR(fs::remove(Twine(LongPath)));
164   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory)));
165 }
166 #endif
167 
168 TEST_F(ProgramEnvTest, CreateProcessTrailingSlash) {
169   if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
170     if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
171         ProgramTestStringArg2 == "has\\\\ trailing\\") {
172       exit(0);  // Success!  The arguments were passed and parsed.
173     }
174     exit(1);
175   }
176 
177   std::string my_exe =
178       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
179   StringRef argv[] = {
180       my_exe,
181       "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
182       "-program-test-string-arg1",
183       "has\\\\ trailing\\",
184       "-program-test-string-arg2",
185       "has\\\\ trailing\\"};
186 
187   // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
188   addEnvVar("LLVM_PROGRAM_TEST_CHILD=1");
189 
190   std::string error;
191   bool ExecutionFailed;
192   // Redirect stdout and stdin to NUL, but let stderr through.
193 #ifdef _WIN32
194   StringRef nul("NUL");
195 #else
196   StringRef nul("/dev/null");
197 #endif
198   std::optional<StringRef> redirects[] = {nul, nul, std::nullopt};
199   int rc = ExecuteAndWait(my_exe, argv, getEnviron(), redirects,
200                           /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
201                           &ExecutionFailed);
202   EXPECT_FALSE(ExecutionFailed) << error;
203   EXPECT_EQ(0, rc);
204 }
205 
206 TEST_F(ProgramEnvTest, TestExecuteNoWait) {
207   using namespace llvm::sys;
208 
209   if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT")) {
210     sleep_for(/*seconds*/ 1);
211     exit(0);
212   }
213 
214   std::string Executable =
215       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
216   StringRef argv[] = {Executable,
217                       "--gtest_filter=ProgramEnvTest.TestExecuteNoWait"};
218 
219   // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
220   addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
221 
222   std::string Error;
223   bool ExecutionFailed;
224   ProcessInfo PI1 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
225                                   &ExecutionFailed);
226   ASSERT_FALSE(ExecutionFailed) << Error;
227   ASSERT_NE(PI1.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
228 
229   unsigned LoopCount = 0;
230 
231   // Test that Wait() with SecondsToWait=std::nullopt works. In this case,
232   // LoopCount should only be incremented once.
233   while (true) {
234     ++LoopCount;
235     ProcessInfo WaitResult =
236         llvm::sys::Wait(PI1, /*SecondsToWait=*/std::nullopt, &Error);
237     ASSERT_TRUE(Error.empty());
238     if (WaitResult.Pid == PI1.Pid)
239       break;
240   }
241 
242   EXPECT_EQ(LoopCount, 1u) << "LoopCount should be 1";
243 
244   ProcessInfo PI2 = ExecuteNoWait(Executable, argv, getEnviron(),
245                                   /*Redirects*/ {}, /*MemoryLimit*/ 0, &Error,
246                                   &ExecutionFailed);
247   ASSERT_FALSE(ExecutionFailed) << Error;
248   ASSERT_NE(PI2.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
249 
250   // Test that Wait() with SecondsToWait=0 performs a non-blocking wait. In this
251   // case, LoopCount should be greater than 1 (more than one increment occurs).
252   while (true) {
253     ++LoopCount;
254     ProcessInfo WaitResult = llvm::sys::Wait(PI2, /*SecondsToWait=*/0, &Error);
255     ASSERT_TRUE(Error.empty());
256     if (WaitResult.Pid == PI2.Pid)
257       break;
258   }
259 
260   ASSERT_GT(LoopCount, 1u) << "LoopCount should be >1";
261 }
262 
263 TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
264   using namespace llvm::sys;
265 
266   if (getenv("LLVM_PROGRAM_TEST_TIMEOUT")) {
267     sleep_for(/*seconds*/ 10);
268     exit(0);
269   }
270 
271   std::string Executable =
272       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
273   StringRef argv[] = {
274       Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout"};
275 
276   // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
277  addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
278 
279   std::string Error;
280   bool ExecutionFailed;
281   int RetCode =
282       ExecuteAndWait(Executable, argv, getEnviron(), {}, /*SecondsToWait=*/1,
283                      /*MemoryLimit*/ 0, &Error, &ExecutionFailed);
284   ASSERT_EQ(-2, RetCode);
285 }
286 
287 TEST(ProgramTest, TestExecuteNegative) {
288   std::string Executable = "i_dont_exist";
289   StringRef argv[] = {Executable};
290 
291   {
292     std::string Error;
293     bool ExecutionFailed;
294     int RetCode = ExecuteAndWait(Executable, argv, std::nullopt, {}, 0, 0,
295                                  &Error, &ExecutionFailed);
296     ASSERT_LT(RetCode, 0) << "On error ExecuteAndWait should return 0 or "
297                              "positive value indicating the result code";
298     ASSERT_TRUE(ExecutionFailed);
299     ASSERT_FALSE(Error.empty());
300   }
301 
302   {
303     std::string Error;
304     bool ExecutionFailed;
305     ProcessInfo PI = ExecuteNoWait(Executable, argv, std::nullopt, {}, 0,
306                                    &Error, &ExecutionFailed);
307     ASSERT_EQ(PI.Pid, ProcessInfo::InvalidPid)
308         << "On error ExecuteNoWait should return an invalid ProcessInfo";
309     ASSERT_TRUE(ExecutionFailed);
310     ASSERT_FALSE(Error.empty());
311   }
312 
313 }
314 
315 #ifdef _WIN32
316 const char utf16le_text[] =
317     "\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61\x00";
318 const char utf16be_text[] =
319     "\x00\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61";
320 #endif
321 const char utf8_text[] = "\x6c\x69\x6e\x67\xc3\xbc\x69\xc3\xa7\x61";
322 
323 TEST(ProgramTest, TestWriteWithSystemEncoding) {
324   SmallString<128> TestDirectory;
325   ASSERT_NO_ERROR(fs::createUniqueDirectory("program-test", TestDirectory));
326   errs() << "Test Directory: " << TestDirectory << '\n';
327   errs().flush();
328   SmallString<128> file_pathname(TestDirectory);
329   path::append(file_pathname, "international-file.txt");
330   // Only on Windows we should encode in UTF16. For other systems, use UTF8
331   ASSERT_NO_ERROR(sys::writeFileWithEncoding(file_pathname.c_str(), utf8_text,
332                                              sys::WEM_UTF16));
333   int fd = 0;
334   ASSERT_NO_ERROR(fs::openFileForRead(file_pathname.c_str(), fd));
335 #if defined(_WIN32)
336   char buf[18];
337   ASSERT_EQ(::read(fd, buf, 18), 18);
338   const char *utf16_text;
339   if (strncmp(buf, "\xfe\xff", 2) == 0) { // UTF16-BE
340     utf16_text = utf16be_text;
341   } else if (strncmp(buf, "\xff\xfe", 2) == 0) { // UTF16-LE
342     utf16_text = utf16le_text;
343   } else {
344     FAIL() << "Invalid BOM in UTF-16 file";
345   }
346   ASSERT_EQ(strncmp(&buf[2], utf16_text, 16), 0);
347 #else
348   char buf[10];
349   ASSERT_EQ(::read(fd, buf, 10), 10);
350   ASSERT_EQ(strncmp(buf, utf8_text, 10), 0);
351 #endif
352   ::close(fd);
353   ASSERT_NO_ERROR(fs::remove(file_pathname.str()));
354   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
355 }
356 
357 TEST_F(ProgramEnvTest, TestExecuteAndWaitStatistics) {
358   using namespace llvm::sys;
359 
360   if (getenv("LLVM_PROGRAM_TEST_STATISTICS"))
361     exit(0);
362 
363   std::string Executable =
364       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
365   StringRef argv[] = {
366       Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitStatistics"};
367 
368   // Add LLVM_PROGRAM_TEST_STATISTICS to the environment of the child.
369   addEnvVar("LLVM_PROGRAM_TEST_STATISTICS=1");
370 
371   std::string Error;
372   bool ExecutionFailed;
373   std::optional<ProcessStatistics> ProcStat;
374   int RetCode = ExecuteAndWait(Executable, argv, getEnviron(), {}, 0, 0, &Error,
375                                &ExecutionFailed, &ProcStat);
376   ASSERT_EQ(0, RetCode);
377   ASSERT_TRUE(ProcStat);
378   ASSERT_GE(ProcStat->UserTime, std::chrono::microseconds(0));
379   ASSERT_GE(ProcStat->TotalTime, ProcStat->UserTime);
380 }
381 
382 TEST_F(ProgramEnvTest, TestLockFile) {
383   using namespace llvm::sys;
384 
385   if (const char *LockedFile = getenv("LLVM_PROGRAM_TEST_LOCKED_FILE")) {
386     // Child process.
387     int FD2;
388     ASSERT_NO_ERROR(fs::openFileForReadWrite(LockedFile, FD2,
389                                              fs::CD_OpenExisting, fs::OF_None));
390 
391     std::error_code ErrC = fs::tryLockFile(FD2, std::chrono::seconds(5));
392     ASSERT_NO_ERROR(ErrC);
393     ASSERT_NO_ERROR(fs::unlockFile(FD2));
394     close(FD2);
395     exit(0);
396   }
397 
398   // Create file that will be locked.
399   SmallString<64> LockedFile;
400   int FD1;
401   ASSERT_NO_ERROR(
402       fs::createTemporaryFile("TestLockFile", "temp", FD1, LockedFile));
403 
404   std::string Executable =
405       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
406   StringRef argv[] = {Executable, "--gtest_filter=ProgramEnvTest.TestLockFile"};
407 
408   // Add LLVM_PROGRAM_TEST_LOCKED_FILE to the environment of the child.
409   std::string EnvVar = "LLVM_PROGRAM_TEST_LOCKED_FILE=";
410   EnvVar += LockedFile.str();
411   addEnvVar(EnvVar);
412 
413   // Lock the file.
414   ASSERT_NO_ERROR(fs::tryLockFile(FD1));
415 
416   std::string Error;
417   bool ExecutionFailed;
418   ProcessInfo PI2 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
419                                   &ExecutionFailed);
420   ASSERT_FALSE(ExecutionFailed) << Error;
421   ASSERT_TRUE(Error.empty());
422   ASSERT_NE(PI2.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
423 
424   // Wait some time to give the child process a chance to start.
425   std::this_thread::sleep_for(std::chrono::milliseconds(100));
426 
427   ASSERT_NO_ERROR(fs::unlockFile(FD1));
428   ProcessInfo WaitResult = llvm::sys::Wait(PI2, /*SecondsToWait=*/5, &Error);
429   ASSERT_TRUE(Error.empty());
430   ASSERT_EQ(0, WaitResult.ReturnCode);
431   ASSERT_EQ(WaitResult.Pid, PI2.Pid);
432   sys::fs::remove(LockedFile);
433 }
434 
435 TEST_F(ProgramEnvTest, TestExecuteWithNoStacktraceHandler) {
436   using namespace llvm::sys;
437 
438   if (getenv("LLVM_PROGRAM_TEST_NO_STACKTRACE_HANDLER")) {
439     sys::PrintStackTrace(errs());
440     exit(0);
441   }
442 
443   std::string Executable =
444       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
445   StringRef argv[] = {
446       Executable,
447       "--gtest_filter=ProgramEnvTest.TestExecuteWithNoStacktraceHandler"};
448 
449   addEnvVar("LLVM_PROGRAM_TEST_NO_STACKTRACE_HANDLER=1");
450 
451   std::string Error;
452   bool ExecutionFailed;
453   int RetCode = ExecuteAndWait(Executable, argv, getEnviron(), {}, 0, 0, &Error,
454                                &ExecutionFailed);
455   EXPECT_FALSE(ExecutionFailed) << Error;
456   ASSERT_EQ(0, RetCode);
457 }
458 
459 } // end anonymous namespace
460