LLVM 17.0.0git
Program.inc
Go to the documentation of this file.
1//===- llvm/Support/Unix/Program.inc ----------------------------*- C++ -*-===//
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// This file implements the Unix specific portion of the Program class.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14//=== WARNING: Implementation here must contain only generic UNIX
15//=== code that is guaranteed to work on *all* UNIX variants.
16//===----------------------------------------------------------------------===//
17
19
20#include "Unix.h"
22#include "llvm/Config/config.h"
24#include "llvm/Support/Errc.h"
26#include "llvm/Support/Path.h"
29#if HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#if HAVE_SYS_RESOURCE_H
33#include <sys/resource.h>
34#endif
35#if HAVE_SIGNAL_H
36#include <signal.h>
37#endif
38#if HAVE_FCNTL_H
39#include <fcntl.h>
40#endif
41#if HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44#ifdef HAVE_POSIX_SPAWN
45#include <spawn.h>
46
47#if defined(__APPLE__)
48#include <TargetConditionals.h>
49#endif
50
51#if defined(__APPLE__) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
52#define USE_NSGETENVIRON 1
53#else
54#define USE_NSGETENVIRON 0
55#endif
56
57#if !USE_NSGETENVIRON
58extern char **environ;
59#else
60#include <crt_externs.h> // _NSGetEnviron
61#endif
62#endif
63
64using namespace llvm;
65using namespace sys;
66
67ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
68
69ErrorOr<std::string> sys::findProgramByName(StringRef Name,
71 assert(!Name.empty() && "Must have a name!");
72 // Use the given path verbatim if it contains any slashes; this matches
73 // the behavior of sh(1) and friends.
74 if (Name.contains('/'))
75 return std::string(Name);
76
77 SmallVector<StringRef, 16> EnvironmentPaths;
78 if (Paths.empty())
79 if (const char *PathEnv = std::getenv("PATH")) {
80 SplitString(PathEnv, EnvironmentPaths, ":");
81 Paths = EnvironmentPaths;
82 }
83
84 for (auto Path : Paths) {
85 if (Path.empty())
86 continue;
87
88 // Check to see if this first directory contains the executable...
89 SmallString<128> FilePath(Path);
90 sys::path::append(FilePath, Name);
91 if (sys::fs::can_execute(FilePath.c_str()))
92 return std::string(FilePath.str()); // Found the executable!
93 }
94 return errc::no_such_file_or_directory;
95}
96
97static bool RedirectIO(std::optional<StringRef> Path, int FD, std::string *ErrMsg) {
98 if (!Path) // Noop
99 return false;
100 std::string File;
101 if (Path->empty())
102 // Redirect empty paths to /dev/null
103 File = "/dev/null";
104 else
105 File = std::string(*Path);
106
107 // Open the file
108 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666);
109 if (InFD == -1) {
110 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for " +
111 (FD == 0 ? "input" : "output"));
112 return true;
113 }
114
115 // Install it as the requested FD
116 if (dup2(InFD, FD) == -1) {
117 MakeErrMsg(ErrMsg, "Cannot dup2");
118 close(InFD);
119 return true;
120 }
121 close(InFD); // Close the original FD
122 return false;
123}
124
125#ifdef HAVE_POSIX_SPAWN
126static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
127 posix_spawn_file_actions_t *FileActions) {
128 if (!Path) // Noop
129 return false;
130 const char *File;
131 if (Path->empty())
132 // Redirect empty paths to /dev/null
133 File = "/dev/null";
134 else
135 File = Path->c_str();
136
137 if (int Err = posix_spawn_file_actions_addopen(
138 FileActions, FD, File, FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
139 return MakeErrMsg(ErrMsg, "Cannot posix_spawn_file_actions_addopen", Err);
140 return false;
141}
142#endif
143
144static void TimeOutHandler(int Sig) {}
145
146static void SetMemoryLimits(unsigned size) {
147#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
148 struct rlimit r;
149 __typeof__(r.rlim_cur) limit = (__typeof__(r.rlim_cur))(size)*1048576;
150
151 // Heap size
152 getrlimit(RLIMIT_DATA, &r);
153 r.rlim_cur = limit;
154 setrlimit(RLIMIT_DATA, &r);
155#ifdef RLIMIT_RSS
156 // Resident set size.
157 getrlimit(RLIMIT_RSS, &r);
158 r.rlim_cur = limit;
159 setrlimit(RLIMIT_RSS, &r);
160#endif
161#endif
162}
163
164static std::vector<const char *>
165toNullTerminatedCStringArray(ArrayRef<StringRef> Strings, StringSaver &Saver) {
166 std::vector<const char *> Result;
167 for (StringRef S : Strings)
168 Result.push_back(Saver.save(S).data());
169 Result.push_back(nullptr);
170 return Result;
171}
172
173static bool Execute(ProcessInfo &PI, StringRef Program,
174 ArrayRef<StringRef> Args, std::optional<ArrayRef<StringRef>> Env,
175 ArrayRef<std::optional<StringRef>> Redirects,
176 unsigned MemoryLimit, std::string *ErrMsg,
177 BitVector *AffinityMask) {
178 if (!llvm::sys::fs::exists(Program)) {
179 if (ErrMsg)
180 *ErrMsg = std::string("Executable \"") + Program.str() +
181 std::string("\" doesn't exist!");
182 return false;
183 }
184
185 assert(!AffinityMask && "Starting a process with an affinity mask is "
186 "currently not supported on Unix!");
187
189 StringSaver Saver(Allocator);
190 std::vector<const char *> ArgVector, EnvVector;
191 const char **Argv = nullptr;
192 const char **Envp = nullptr;
193 ArgVector = toNullTerminatedCStringArray(Args, Saver);
194 Argv = ArgVector.data();
195 if (Env) {
196 EnvVector = toNullTerminatedCStringArray(*Env, Saver);
197 Envp = EnvVector.data();
198 }
199
200 // If this OS has posix_spawn and there is no memory limit being implied, use
201 // posix_spawn. It is more efficient than fork/exec.
202#ifdef HAVE_POSIX_SPAWN
203 if (MemoryLimit == 0) {
204 posix_spawn_file_actions_t FileActionsStore;
205 posix_spawn_file_actions_t *FileActions = nullptr;
206
207 // If we call posix_spawn_file_actions_addopen we have to make sure the
208 // c strings we pass to it stay alive until the call to posix_spawn,
209 // so we copy any StringRefs into this variable.
210 std::string RedirectsStorage[3];
211
212 if (!Redirects.empty()) {
213 assert(Redirects.size() == 3);
214 std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
215 for (int I = 0; I < 3; ++I) {
216 if (Redirects[I]) {
217 RedirectsStorage[I] = std::string(*Redirects[I]);
218 RedirectsStr[I] = &RedirectsStorage[I];
219 }
220 }
221
222 FileActions = &FileActionsStore;
223 posix_spawn_file_actions_init(FileActions);
224
225 // Redirect stdin/stdout.
226 if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
227 RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
228 return false;
229 if (!Redirects[1] || !Redirects[2] || *Redirects[1] != *Redirects[2]) {
230 // Just redirect stderr
231 if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
232 return false;
233 } else {
234 // If stdout and stderr should go to the same place, redirect stderr
235 // to the FD already open for stdout.
236 if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
237 return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
238 }
239 }
240
241 if (!Envp)
242#if !USE_NSGETENVIRON
243 Envp = const_cast<const char **>(environ);
244#else
245 // environ is missing in dylibs.
246 Envp = const_cast<const char **>(*_NSGetEnviron());
247#endif
248
249 constexpr int maxRetries = 8;
250 int retries = 0;
251 pid_t PID;
252 int Err;
253 do {
254 PID = 0; // Make Valgrind happy.
255 Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
256 /*attrp*/ nullptr, const_cast<char **>(Argv),
257 const_cast<char **>(Envp));
258 } while (Err == EINTR && ++retries < maxRetries);
259
260 if (FileActions)
261 posix_spawn_file_actions_destroy(FileActions);
262
263 if (Err)
264 return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
265
266 PI.Pid = PID;
267 PI.Process = PID;
268
269 return true;
270 }
271#endif
272
273 // Create a child process.
274 int child = fork();
275 switch (child) {
276 // An error occurred: Return to the caller.
277 case -1:
278 MakeErrMsg(ErrMsg, "Couldn't fork");
279 return false;
280
281 // Child process: Execute the program.
282 case 0: {
283 // Redirect file descriptors...
284 if (!Redirects.empty()) {
285 // Redirect stdin
286 if (RedirectIO(Redirects[0], 0, ErrMsg)) {
287 return false;
288 }
289 // Redirect stdout
290 if (RedirectIO(Redirects[1], 1, ErrMsg)) {
291 return false;
292 }
293 if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) {
294 // If stdout and stderr should go to the same place, redirect stderr
295 // to the FD already open for stdout.
296 if (-1 == dup2(1, 2)) {
297 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
298 return false;
299 }
300 } else {
301 // Just redirect stderr
302 if (RedirectIO(Redirects[2], 2, ErrMsg)) {
303 return false;
304 }
305 }
306 }
307
308 // Set memory limits
309 if (MemoryLimit != 0) {
310 SetMemoryLimits(MemoryLimit);
311 }
312
313 // Execute!
314 std::string PathStr = std::string(Program);
315 if (Envp != nullptr)
316 execve(PathStr.c_str(), const_cast<char **>(Argv),
317 const_cast<char **>(Envp));
318 else
319 execv(PathStr.c_str(), const_cast<char **>(Argv));
320 // If the execve() failed, we should exit. Follow Unix protocol and
321 // return 127 if the executable was not found, and 126 otherwise.
322 // Use _exit rather than exit so that atexit functions and static
323 // object destructors cloned from the parent process aren't
324 // redundantly run, and so that any data buffered in stdio buffers
325 // cloned from the parent aren't redundantly written out.
326 _exit(errno == ENOENT ? 127 : 126);
327 }
328
329 // Parent process: Break out of the switch to do our processing.
330 default:
331 break;
332 }
333
334 PI.Pid = child;
335 PI.Process = child;
336
337 return true;
338}
339
340namespace llvm {
341namespace sys {
342
343#ifndef _AIX
344using ::wait4;
345#else
346static pid_t(wait4)(pid_t pid, int *status, int options, struct rusage *usage);
347#endif
348
349} // namespace sys
350} // namespace llvm
351
352#ifdef _AIX
353#ifndef _ALL_SOURCE
354extern "C" pid_t(wait4)(pid_t pid, int *status, int options,
355 struct rusage *usage);
356#endif
357pid_t(llvm::sys::wait4)(pid_t pid, int *status, int options,
358 struct rusage *usage) {
359 assert(pid > 0 && "Only expecting to handle actual PID values!");
360 assert((options & ~WNOHANG) == 0 && "Expecting WNOHANG at most!");
361 assert(usage && "Expecting usage collection!");
362
363 // AIX wait4 does not work well with WNOHANG.
364 if (!(options & WNOHANG))
365 return ::wait4(pid, status, options, usage);
366
367 // For WNOHANG, we use waitid (which supports WNOWAIT) until the child process
368 // has terminated.
369 siginfo_t WaitIdInfo;
370 WaitIdInfo.si_pid = 0;
371 int WaitIdRetVal =
372 waitid(P_PID, pid, &WaitIdInfo, WNOWAIT | WEXITED | options);
373
374 if (WaitIdRetVal == -1 || WaitIdInfo.si_pid == 0)
375 return WaitIdRetVal;
376
377 assert(WaitIdInfo.si_pid == pid);
378
379 // The child has already terminated, so a blocking wait on it is okay in the
380 // absence of indiscriminate `wait` calls from the current process (which
381 // would cause the call here to fail with ECHILD).
382 return ::wait4(pid, status, options & ~WNOHANG, usage);
383}
384#endif
385
387 std::optional<unsigned> SecondsToWait,
388 std::string *ErrMsg,
389 std::optional<ProcessStatistics> *ProcStat,
390 bool Polling) {
391 struct sigaction Act, Old;
392 assert(PI.Pid && "invalid pid to wait on, process not started?");
393
394 int WaitPidOptions = 0;
395 pid_t ChildPid = PI.Pid;
396 bool WaitUntilTerminates = false;
397 if (!SecondsToWait) {
398 WaitUntilTerminates = true;
399 } else {
400 if (*SecondsToWait == 0)
401 WaitPidOptions = WNOHANG;
402
403 // Install a timeout handler. The handler itself does nothing, but the
404 // simple fact of having a handler at all causes the wait below to return
405 // with EINTR, unlike if we used SIG_IGN.
406 memset(&Act, 0, sizeof(Act));
407 Act.sa_handler = TimeOutHandler;
408 sigemptyset(&Act.sa_mask);
409 sigaction(SIGALRM, &Act, &Old);
410 // FIXME The alarm signal may be delivered to another thread.
411 alarm(*SecondsToWait);
412 }
413
414 // Parent process: Wait for the child process to terminate.
415 int status = 0;
416 ProcessInfo WaitResult;
417 rusage Info;
418 if (ProcStat)
419 ProcStat->reset();
420
421 do {
422 WaitResult.Pid = sys::wait4(ChildPid, &status, WaitPidOptions, &Info);
423 } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
424
425 if (WaitResult.Pid != PI.Pid) {
426 if (WaitResult.Pid == 0) {
427 // Non-blocking wait.
428 return WaitResult;
429 } else {
430 if (SecondsToWait && errno == EINTR && !Polling) {
431 // Kill the child.
432 kill(PI.Pid, SIGKILL);
433
434 // Turn off the alarm and restore the signal handler
435 alarm(0);
436 sigaction(SIGALRM, &Old, nullptr);
437
438 // Wait for child to die
439 // FIXME This could grab some other child process out from another
440 // waiting thread and then leave a zombie anyway.
441 if (wait(&status) != ChildPid)
442 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
443 else
444 MakeErrMsg(ErrMsg, "Child timed out", 0);
445
446 WaitResult.ReturnCode = -2; // Timeout detected
447 return WaitResult;
448 } else if (errno != EINTR) {
449 MakeErrMsg(ErrMsg, "Error waiting for child process");
450 WaitResult.ReturnCode = -1;
451 return WaitResult;
452 }
453 }
454 }
455
456 // We exited normally without timeout, so turn off the timer.
457 if (SecondsToWait && !WaitUntilTerminates) {
458 alarm(0);
459 sigaction(SIGALRM, &Old, nullptr);
460 }
461
462 if (ProcStat) {
463 std::chrono::microseconds UserT = toDuration(Info.ru_utime);
464 std::chrono::microseconds KernelT = toDuration(Info.ru_stime);
465 uint64_t PeakMemory = 0;
466#ifndef __HAIKU__
467 PeakMemory = static_cast<uint64_t>(Info.ru_maxrss);
468#endif
469 *ProcStat = ProcessStatistics{UserT + KernelT, UserT, PeakMemory};
470 }
471
472 // Return the proper exit status. Detect error conditions
473 // so we can return -1 for them and set ErrMsg informatively.
474 int result = 0;
475 if (WIFEXITED(status)) {
476 result = WEXITSTATUS(status);
477 WaitResult.ReturnCode = result;
478
479 if (result == 127) {
480 if (ErrMsg)
481 *ErrMsg = llvm::sys::StrError(ENOENT);
482 WaitResult.ReturnCode = -1;
483 return WaitResult;
484 }
485 if (result == 126) {
486 if (ErrMsg)
487 *ErrMsg = "Program could not be executed";
488 WaitResult.ReturnCode = -1;
489 return WaitResult;
490 }
491 } else if (WIFSIGNALED(status)) {
492 if (ErrMsg) {
493 *ErrMsg = strsignal(WTERMSIG(status));
494#ifdef WCOREDUMP
495 if (WCOREDUMP(status))
496 *ErrMsg += " (core dumped)";
497#endif
498 }
499 // Return a special value to indicate that the process received an unhandled
500 // signal during execution as opposed to failing to execute.
501 WaitResult.ReturnCode = -2;
502 }
503 return WaitResult;
504}
505
507 if (!(Flags & fs::OF_Text))
508 return ChangeStdinToBinary();
509 return std::error_code();
510}
511
513 if (!(Flags & fs::OF_Text))
514 return ChangeStdoutToBinary();
515 return std::error_code();
516}
517
518std::error_code llvm::sys::ChangeStdinToBinary() {
519 // Do nothing, as Unix doesn't differentiate between text and binary.
520 return std::error_code();
521}
522
523std::error_code llvm::sys::ChangeStdoutToBinary() {
524 // Do nothing, as Unix doesn't differentiate between text and binary.
525 return std::error_code();
526}
527
528std::error_code
530 WindowsEncodingMethod Encoding /*unused*/) {
531 std::error_code EC;
532 llvm::raw_fd_ostream OS(FileName, EC,
534
535 if (EC)
536 return EC;
537
538 OS << Contents;
539
540 if (OS.has_error())
541 return make_error_code(errc::io_error);
542
543 return EC;
544}
545
547 ArrayRef<StringRef> Args) {
548 static long ArgMax = sysconf(_SC_ARG_MAX);
549 // POSIX requires that _POSIX_ARG_MAX is 4096, which is the lowest possible
550 // value for ARG_MAX on a POSIX compliant system.
551 static long ArgMin = _POSIX_ARG_MAX;
552
553 // This the same baseline used by xargs.
554 long EffectiveArgMax = 128 * 1024;
555
556 if (EffectiveArgMax > ArgMax)
557 EffectiveArgMax = ArgMax;
558 else if (EffectiveArgMax < ArgMin)
559 EffectiveArgMax = ArgMin;
560
561 // System says no practical limit.
562 if (ArgMax == -1)
563 return true;
564
565 // Conservatively account for space required by environment variables.
566 long HalfArgMax = EffectiveArgMax / 2;
567
568 size_t ArgLength = Program.size() + 1;
569 for (StringRef Arg : Args) {
570 // Ensure that we do not exceed the MAX_ARG_STRLEN constant on Linux, which
571 // does not have a constant unlike what the man pages would have you
572 // believe. Since this limit is pretty high, perform the check
573 // unconditionally rather than trying to be aggressive and limiting it to
574 // Linux only.
575 if (Arg.size() >= (32 * 4096))
576 return false;
577
578 ArgLength += Arg.size() + 1;
579 if (ArgLength > size_t(HalfArgMax)) {
580 return false;
581 }
582 }
583
584 return true;
585}
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
static bool Execute(ProcessInfo &PI, StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env, ArrayRef< std::optional< StringRef > > Redirects, unsigned MemoryLimit, std::string *ErrMsg, BitVector *AffinityMask)
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
@ Paths
Definition: TextStubV5.cpp:120
@ Flags
Definition: TextStubV5.cpp:93
static bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix, int errnum=-1)
This function builds an error message into ErrMsg using the prefix string and the Unix error number g...
Definition: Unix.h:59
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Represents either an error or a value T.
Definition: ErrorOr.h:56
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:21
StringRef save(const char *S)
Definition: StringSaver.h:30
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
std::vector< std::string > ArgVector
LVOptions & options()
Definition: LVOptions.h:445
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1078
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
std::chrono::nanoseconds toDuration(FILETIME Time)
std::string StrError()
Returns a string representation of the errno value, using whatever thread-safe variant of strerror() ...
std::error_code ChangeStdinMode(fs::OpenFlags Flags)
std::error_code ChangeStdinToBinary()
ProcessInfo Wait(const ProcessInfo &PI, std::optional< unsigned > SecondsToWait, std::string *ErrMsg=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, bool Polling=false)
This function waits for the process specified by PI to finish.
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
std::error_code ChangeStdoutMode(fs::OpenFlags Flags)
WindowsEncodingMethod
File encoding options when writing contents that a non-UTF8 tool will read (on Windows systems).
Definition: Program.h:169
std::error_code ChangeStdoutToBinary()
std::error_code writeFileWithEncoding(StringRef FileName, StringRef Contents, WindowsEncodingMethod Encoding=WEM_UTF8)
Saves the UTF8-encoded contents string into the file FileName using a specific encoding.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code make_error_code(BitcodeError E)
This struct encapsulates information about a process.
Definition: Program.h:46
process_t Process
The process identifier.
Definition: Program.h:50
int ReturnCode
Platform-dependent process object.
Definition: Program.h:53
This struct encapsulates information about a process execution.
Definition: Program.h:59