xref: /llvm-project/llvm/unittests/Support/ProgramTest.cpp (revision 3df88ec335da7c3ed1625014d6f9b05fd5b54f31)
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       EnvTable.emplace_back(prepareEnvVar(*EnvP));
99       ++EnvP;
100     }
101   }
102 
103   void TearDown() override {
104     EnvTable.clear();
105     EnvStorage.clear();
106   }
107 
108   void addEnvVar(StringRef Var) { EnvTable.emplace_back(Var); }
109 
110   ArrayRef<StringRef> getEnviron() const { return EnvTable; }
111 };
112 
113 #ifdef _WIN32
114 void checkSeparators(StringRef Path) {
115   char UndesiredSeparator = sys::path::get_separator()[0] == '/' ? '\\' : '/';
116   ASSERT_EQ(Path.find(UndesiredSeparator), StringRef::npos);
117 }
118 
119 TEST_F(ProgramEnvTest, CreateProcessLongPath) {
120   if (getenv("LLVM_PROGRAM_TEST_LONG_PATH"))
121     exit(0);
122 
123   // getMainExecutable returns an absolute path; prepend the long-path prefix.
124   SmallString<128> MyAbsExe(
125       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1));
126   checkSeparators(MyAbsExe);
127   // Force a path with backslashes, when we are going to prepend the \\?\
128   // prefix.
129   sys::path::native(MyAbsExe, sys::path::Style::windows_backslash);
130   std::string MyExe;
131   if (!StringRef(MyAbsExe).startswith("\\\\?\\"))
132     MyExe.append("\\\\?\\");
133   MyExe.append(std::string(MyAbsExe.begin(), MyAbsExe.end()));
134 
135   StringRef ArgV[] = {MyExe,
136                       "--gtest_filter=ProgramEnvTest.CreateProcessLongPath"};
137 
138   // Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
139   addEnvVar("LLVM_PROGRAM_TEST_LONG_PATH=1");
140 
141   // Redirect stdout to a long path.
142   SmallString<128> TestDirectory;
143   ASSERT_NO_ERROR(
144     fs::createUniqueDirectory("program-redirect-test", TestDirectory));
145   SmallString<256> LongPath(TestDirectory);
146   LongPath.push_back('\\');
147   // MAX_PATH = 260
148   LongPath.append(260 - TestDirectory.size(), 'a');
149 
150   std::string Error;
151   bool ExecutionFailed;
152   Optional<StringRef> Redirects[] = {None, LongPath.str(), None};
153   int RC = ExecuteAndWait(MyExe, ArgV, getEnviron(), Redirects,
154     /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &Error,
155     &ExecutionFailed);
156   EXPECT_FALSE(ExecutionFailed) << Error;
157   EXPECT_EQ(0, RC);
158 
159   // Remove the long stdout.
160   ASSERT_NO_ERROR(fs::remove(Twine(LongPath)));
161   ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory)));
162 }
163 #endif
164 
165 TEST_F(ProgramEnvTest, CreateProcessTrailingSlash) {
166   if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
167     if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
168         ProgramTestStringArg2 == "has\\\\ trailing\\") {
169       exit(0);  // Success!  The arguments were passed and parsed.
170     }
171     exit(1);
172   }
173 
174   std::string my_exe =
175       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
176   StringRef argv[] = {
177       my_exe,
178       "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
179       "-program-test-string-arg1",
180       "has\\\\ trailing\\",
181       "-program-test-string-arg2",
182       "has\\\\ trailing\\"};
183 
184   // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
185   addEnvVar("LLVM_PROGRAM_TEST_CHILD=1");
186 
187   std::string error;
188   bool ExecutionFailed;
189   // Redirect stdout and stdin to NUL, but let stderr through.
190 #ifdef _WIN32
191   StringRef nul("NUL");
192 #else
193   StringRef nul("/dev/null");
194 #endif
195   Optional<StringRef> redirects[] = { nul, nul, None };
196   int rc = ExecuteAndWait(my_exe, argv, getEnviron(), redirects,
197                           /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
198                           &ExecutionFailed);
199   EXPECT_FALSE(ExecutionFailed) << error;
200   EXPECT_EQ(0, rc);
201 }
202 
203 TEST_F(ProgramEnvTest, TestExecuteNoWait) {
204   using namespace llvm::sys;
205 
206   if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT")) {
207     sleep_for(/*seconds*/ 1);
208     exit(0);
209   }
210 
211   std::string Executable =
212       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
213   StringRef argv[] = {Executable,
214                       "--gtest_filter=ProgramEnvTest.TestExecuteNoWait"};
215 
216   // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
217   addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
218 
219   std::string Error;
220   bool ExecutionFailed;
221   ProcessInfo PI1 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
222                                   &ExecutionFailed);
223   ASSERT_FALSE(ExecutionFailed) << Error;
224   ASSERT_NE(PI1.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
225 
226   unsigned LoopCount = 0;
227 
228   // Test that Wait() with WaitUntilTerminates=true works. In this case,
229   // LoopCount should only be incremented once.
230   while (true) {
231     ++LoopCount;
232     ProcessInfo WaitResult = llvm::sys::Wait(PI1, 0, true, &Error);
233     ASSERT_TRUE(Error.empty());
234     if (WaitResult.Pid == PI1.Pid)
235       break;
236   }
237 
238   EXPECT_EQ(LoopCount, 1u) << "LoopCount should be 1";
239 
240   ProcessInfo PI2 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
241                                   &ExecutionFailed);
242   ASSERT_FALSE(ExecutionFailed) << Error;
243   ASSERT_NE(PI2.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
244 
245   // Test that Wait() with SecondsToWait=0 performs a non-blocking wait. In this
246   // cse, LoopCount should be greater than 1 (more than one increment occurs).
247   while (true) {
248     ++LoopCount;
249     ProcessInfo WaitResult = llvm::sys::Wait(PI2, 0, false, &Error);
250     ASSERT_TRUE(Error.empty());
251     if (WaitResult.Pid == PI2.Pid)
252       break;
253   }
254 
255   ASSERT_GT(LoopCount, 1u) << "LoopCount should be >1";
256 }
257 
258 TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
259   using namespace llvm::sys;
260 
261   if (getenv("LLVM_PROGRAM_TEST_TIMEOUT")) {
262     sleep_for(/*seconds*/ 10);
263     exit(0);
264   }
265 
266   std::string Executable =
267       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
268   StringRef argv[] = {
269       Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout"};
270 
271   // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
272  addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
273 
274   std::string Error;
275   bool ExecutionFailed;
276   int RetCode =
277       ExecuteAndWait(Executable, argv, getEnviron(), {}, /*secondsToWait=*/1, 0,
278                      &Error, &ExecutionFailed);
279   ASSERT_EQ(-2, RetCode);
280 }
281 
282 TEST(ProgramTest, TestExecuteNegative) {
283   std::string Executable = "i_dont_exist";
284   StringRef argv[] = {Executable};
285 
286   {
287     std::string Error;
288     bool ExecutionFailed;
289     int RetCode = ExecuteAndWait(Executable, argv, llvm::None, {}, 0, 0, &Error,
290                                  &ExecutionFailed);
291     ASSERT_LT(RetCode, 0) << "On error ExecuteAndWait should return 0 or "
292                              "positive value indicating the result code";
293     ASSERT_TRUE(ExecutionFailed);
294     ASSERT_FALSE(Error.empty());
295   }
296 
297   {
298     std::string Error;
299     bool ExecutionFailed;
300     ProcessInfo PI = ExecuteNoWait(Executable, argv, llvm::None, {}, 0, &Error,
301                                    &ExecutionFailed);
302     ASSERT_EQ(PI.Pid, ProcessInfo::InvalidPid)
303         << "On error ExecuteNoWait should return an invalid ProcessInfo";
304     ASSERT_TRUE(ExecutionFailed);
305     ASSERT_FALSE(Error.empty());
306   }
307 
308 }
309 
310 #ifdef _WIN32
311 const char utf16le_text[] =
312     "\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61\x00";
313 const char utf16be_text[] =
314     "\x00\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61";
315 #endif
316 const char utf8_text[] = "\x6c\x69\x6e\x67\xc3\xbc\x69\xc3\xa7\x61";
317 
318 TEST(ProgramTest, TestWriteWithSystemEncoding) {
319   SmallString<128> TestDirectory;
320   ASSERT_NO_ERROR(fs::createUniqueDirectory("program-test", TestDirectory));
321   errs() << "Test Directory: " << TestDirectory << '\n';
322   errs().flush();
323   SmallString<128> file_pathname(TestDirectory);
324   path::append(file_pathname, "international-file.txt");
325   // Only on Windows we should encode in UTF16. For other systems, use UTF8
326   ASSERT_NO_ERROR(sys::writeFileWithEncoding(file_pathname.c_str(), utf8_text,
327                                              sys::WEM_UTF16));
328   int fd = 0;
329   ASSERT_NO_ERROR(fs::openFileForRead(file_pathname.c_str(), fd));
330 #if defined(_WIN32)
331   char buf[18];
332   ASSERT_EQ(::read(fd, buf, 18), 18);
333   const char *utf16_text;
334   if (strncmp(buf, "\xfe\xff", 2) == 0) { // UTF16-BE
335     utf16_text = utf16be_text;
336   } else if (strncmp(buf, "\xff\xfe", 2) == 0) { // UTF16-LE
337     utf16_text = utf16le_text;
338   } else {
339     FAIL() << "Invalid BOM in UTF-16 file";
340   }
341   ASSERT_EQ(strncmp(&buf[2], utf16_text, 16), 0);
342 #else
343   char buf[10];
344   ASSERT_EQ(::read(fd, buf, 10), 10);
345   ASSERT_EQ(strncmp(buf, utf8_text, 10), 0);
346 #endif
347   ::close(fd);
348   ASSERT_NO_ERROR(fs::remove(file_pathname.str()));
349   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
350 }
351 
352 TEST_F(ProgramEnvTest, TestExecuteAndWaitStatistics) {
353   using namespace llvm::sys;
354 
355   if (getenv("LLVM_PROGRAM_TEST_STATISTICS"))
356     exit(0);
357 
358   std::string Executable =
359       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
360   StringRef argv[] = {
361       Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitStatistics"};
362 
363   // Add LLVM_PROGRAM_TEST_STATISTICS to the environment of the child.
364   addEnvVar("LLVM_PROGRAM_TEST_STATISTICS=1");
365 
366   std::string Error;
367   bool ExecutionFailed;
368   Optional<ProcessStatistics> ProcStat;
369   int RetCode = ExecuteAndWait(Executable, argv, getEnviron(), {}, 0, 0, &Error,
370                                &ExecutionFailed, &ProcStat);
371   ASSERT_EQ(0, RetCode);
372   ASSERT_TRUE(ProcStat);
373   ASSERT_GE(ProcStat->UserTime, std::chrono::microseconds(0));
374   ASSERT_GE(ProcStat->TotalTime, ProcStat->UserTime);
375 }
376 
377 TEST_F(ProgramEnvTest, TestLockFile) {
378   using namespace llvm::sys;
379 
380   if (const char *LockedFile = getenv("LLVM_PROGRAM_TEST_LOCKED_FILE")) {
381     // Child process.
382     int FD2;
383     ASSERT_NO_ERROR(fs::openFileForReadWrite(LockedFile, FD2,
384                                              fs::CD_OpenExisting, fs::OF_None));
385 
386     std::error_code ErrC = fs::tryLockFile(FD2, std::chrono::seconds(5));
387     ASSERT_NO_ERROR(ErrC);
388     ASSERT_NO_ERROR(fs::unlockFile(FD2));
389     close(FD2);
390     exit(0);
391   }
392 
393   // Create file that will be locked.
394   SmallString<64> LockedFile;
395   int FD1;
396   ASSERT_NO_ERROR(
397       fs::createTemporaryFile("TestLockFile", "temp", FD1, LockedFile));
398 
399   std::string Executable =
400       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
401   StringRef argv[] = {Executable, "--gtest_filter=ProgramEnvTest.TestLockFile"};
402 
403   // Add LLVM_PROGRAM_TEST_LOCKED_FILE to the environment of the child.
404   std::string EnvVar = "LLVM_PROGRAM_TEST_LOCKED_FILE=";
405   EnvVar += LockedFile.str();
406   addEnvVar(EnvVar);
407 
408   // Lock the file.
409   ASSERT_NO_ERROR(fs::tryLockFile(FD1));
410 
411   std::string Error;
412   bool ExecutionFailed;
413   ProcessInfo PI2 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
414                                   &ExecutionFailed);
415   ASSERT_FALSE(ExecutionFailed) << Error;
416   ASSERT_TRUE(Error.empty());
417   ASSERT_NE(PI2.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
418 
419   // Wait some time to give the child process a chance to start.
420   std::this_thread::sleep_for(std::chrono::milliseconds(100));
421 
422   ASSERT_NO_ERROR(fs::unlockFile(FD1));
423   ProcessInfo WaitResult = llvm::sys::Wait(PI2, 5 /* seconds */, true, &Error);
424   ASSERT_TRUE(Error.empty());
425   ASSERT_EQ(0, WaitResult.ReturnCode);
426   ASSERT_EQ(WaitResult.Pid, PI2.Pid);
427   sys::fs::remove(LockedFile);
428 }
429 
430 TEST_F(ProgramEnvTest, TestExecuteWithNoStacktraceHandler) {
431   using namespace llvm::sys;
432 
433   if (getenv("LLVM_PROGRAM_TEST_NO_STACKTRACE_HANDLER")) {
434     sys::PrintStackTrace(errs());
435     exit(0);
436   }
437 
438   std::string Executable =
439       sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
440   StringRef argv[] = {
441       Executable,
442       "--gtest_filter=ProgramEnvTest.TestExecuteWithNoStacktraceHandler"};
443 
444   addEnvVar("LLVM_PROGRAM_TEST_NO_STACKTRACE_HANDLER=1");
445 
446   std::string Error;
447   bool ExecutionFailed;
448   int RetCode = ExecuteAndWait(Executable, argv, getEnviron(), {}, 0, 0, &Error,
449                                &ExecutionFailed);
450   EXPECT_FALSE(ExecutionFailed) << Error;
451   ASSERT_EQ(0, RetCode);
452 }
453 
454 } // end anonymous namespace
455