Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Unix/Program.inc
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/Support/Unix/Program.cpp -----------------------------*- 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 code that
15
//===          is guaranteed to work on *all* UNIX variants.
16
//===----------------------------------------------------------------------===//
17
18
#include "Unix.h"
19
#include "llvm/ADT/StringExtras.h"
20
#include "llvm/Config/config.h"
21
#include "llvm/Support/Compiler.h"
22
#include "llvm/Support/Errc.h"
23
#include "llvm/Support/FileSystem.h"
24
#include "llvm/Support/Path.h"
25
#include "llvm/Support/StringSaver.h"
26
#include "llvm/Support/raw_ostream.h"
27
#if HAVE_SYS_STAT_H
28
#include <sys/stat.h>
29
#endif
30
#if HAVE_SYS_RESOURCE_H
31
#include <sys/resource.h>
32
#endif
33
#if HAVE_SIGNAL_H
34
#include <signal.h>
35
#endif
36
#if HAVE_FCNTL_H
37
#include <fcntl.h>
38
#endif
39
#if HAVE_UNISTD_H
40
#include <unistd.h>
41
#endif
42
#ifdef HAVE_POSIX_SPAWN
43
#include <spawn.h>
44
45
#if defined(__APPLE__)
46
#include <TargetConditionals.h>
47
#endif
48
49
#if defined(__APPLE__) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
50
#define USE_NSGETENVIRON 1
51
#else
52
#define USE_NSGETENVIRON 0
53
#endif
54
55
#if !USE_NSGETENVIRON
56
  extern char **environ;
57
#else
58
#include <crt_externs.h> // _NSGetEnviron
59
#endif
60
#endif
61
62
namespace llvm {
63
64
using namespace sys;
65
66
810k
ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
67
68
ErrorOr<std::string> sys::findProgramByName(StringRef Name,
69
40.4k
                                            ArrayRef<StringRef> Paths) {
70
40.4k
  assert(!Name.empty() && "Must have a name!");
71
40.4k
  // Use the given path verbatim if it contains any slashes; this matches
72
40.4k
  // the behavior of sh(1) and friends.
73
40.4k
  if (Name.find('/') != StringRef::npos)
74
5
    return std::string(Name);
75
40.4k
76
40.4k
  SmallVector<StringRef, 16> EnvironmentPaths;
77
40.4k
  if (Paths.empty())
78
40.2k
    if (const char *PathEnv = std::getenv("PATH")) {
79
40.2k
      SplitString(PathEnv, EnvironmentPaths, ":");
80
40.2k
      Paths = EnvironmentPaths;
81
40.2k
    }
82
40.4k
83
276k
  for (auto Path : Paths) {
84
276k
    if (Path.empty())
85
0
      continue;
86
276k
87
276k
    // Check to see if this first directory contains the executable...
88
276k
    SmallString<128> FilePath(Path);
89
276k
    sys::path::append(FilePath, Name);
90
276k
    if (sys::fs::can_execute(FilePath.c_str()))
91
4.11k
      return std::string(FilePath.str()); // Found the executable!
92
276k
  }
93
40.4k
  
return errc::no_such_file_or_directory36.2k
;
94
40.4k
}
95
96
0
static bool RedirectIO(Optional<StringRef> Path, int FD, std::string* ErrMsg) {
97
0
  if (!Path) // Noop
98
0
    return false;
99
0
  std::string File;
100
0
  if (Path->empty())
101
0
    // Redirect empty paths to /dev/null
102
0
    File = "/dev/null";
103
0
  else
104
0
    File = *Path;
105
0
106
0
  // Open the file
107
0
  int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
108
0
  if (InFD == -1) {
109
0
    MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
110
0
              + (FD == 0 ? "input" : "output"));
111
0
    return true;
112
0
  }
113
0
114
0
  // Install it as the requested FD
115
0
  if (dup2(InFD, FD) == -1) {
116
0
    MakeErrMsg(ErrMsg, "Cannot dup2");
117
0
    close(InFD);
118
0
    return true;
119
0
  }
120
0
  close(InFD);      // Close the original FD
121
0
  return false;
122
0
}
123
124
#ifdef HAVE_POSIX_SPAWN
125
static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
126
65
                          posix_spawn_file_actions_t *FileActions) {
127
65
  if (!Path) // Noop
128
26
    return false;
129
39
  const char *File;
130
39
  if (Path->empty())
131
29
    // Redirect empty paths to /dev/null
132
29
    File = "/dev/null";
133
10
  else
134
10
    File = Path->c_str();
135
39
136
39
  if (int Err = posix_spawn_file_actions_addopen(
137
0
          FileActions, FD, File,
138
0
          FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
139
0
    return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
140
39
  return false;
141
39
}
142
#endif
143
144
1
static void TimeOutHandler(int Sig) {
145
1
}
146
147
0
static void SetMemoryLimits(unsigned size) {
148
0
#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
149
0
  struct rlimit r;
150
0
  __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
151
0
152
0
  // Heap size
153
0
  getrlimit (RLIMIT_DATA, &r);
154
0
  r.rlim_cur = limit;
155
0
  setrlimit (RLIMIT_DATA, &r);
156
0
#ifdef RLIMIT_RSS
157
0
  // Resident set size.
158
0
  getrlimit (RLIMIT_RSS, &r);
159
0
  r.rlim_cur = limit;
160
0
  setrlimit (RLIMIT_RSS, &r);
161
0
#endif
162
0
#endif
163
0
}
164
165
}
166
167
static std::vector<const char *>
168
18.3k
toNullTerminatedCStringArray(ArrayRef<StringRef> Strings, StringSaver &Saver) {
169
18.3k
  std::vector<const char *> Result;
170
18.3k
  for (StringRef S : Strings)
171
1.36M
    Result.push_back(Saver.save(S).data());
172
18.3k
  Result.push_back(nullptr);
173
18.3k
  return Result;
174
18.3k
}
175
176
static bool Execute(ProcessInfo &PI, StringRef Program,
177
                    ArrayRef<StringRef> Args, Optional<ArrayRef<StringRef>> Env,
178
                    ArrayRef<Optional<StringRef>> Redirects,
179
18.3k
                    unsigned MemoryLimit, std::string *ErrMsg) {
180
18.3k
  if (!llvm::sys::fs::exists(Program)) {
181
3
    if (ErrMsg)
182
3
      *ErrMsg = std::string("Executable \"") + Program.str() +
183
3
                std::string("\" doesn't exist!");
184
3
    return false;
185
3
  }
186
18.3k
187
18.3k
  BumpPtrAllocator Allocator;
188
18.3k
  StringSaver Saver(Allocator);
189
18.3k
  std::vector<const char *> ArgVector, EnvVector;
190
18.3k
  const char **Argv = nullptr;
191
18.3k
  const char **Envp = nullptr;
192
18.3k
  ArgVector = toNullTerminatedCStringArray(Args, Saver);
193
18.3k
  Argv = ArgVector.data();
194
18.3k
  if (Env) {
195
4
    EnvVector = toNullTerminatedCStringArray(*Env, Saver);
196
4
    Envp = EnvVector.data();
197
4
  }
198
18.3k
199
18.3k
  // If this OS has posix_spawn and there is no memory limit being implied, use
200
18.3k
  // posix_spawn.  It is more efficient than fork/exec.
201
18.3k
#ifdef HAVE_POSIX_SPAWN
202
18.3k
  if (MemoryLimit == 0) {
203
18.1k
    posix_spawn_file_actions_t FileActionsStore;
204
18.1k
    posix_spawn_file_actions_t *FileActions = nullptr;
205
18.1k
206
18.1k
    // If we call posix_spawn_file_actions_addopen we have to make sure the
207
18.1k
    // c strings we pass to it stay alive until the call to posix_spawn,
208
18.1k
    // so we copy any StringRefs into this variable.
209
18.1k
    std::string RedirectsStorage[3];
210
18.1k
211
18.1k
    if (!Redirects.empty()) {
212
30
      assert(Redirects.size() == 3);
213
30
      std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
214
120
      for (int I = 0; I < 3; 
++I90
) {
215
90
        if (Redirects[I]) {
216
64
          RedirectsStorage[I] = *Redirects[I];
217
64
          RedirectsStr[I] = &RedirectsStorage[I];
218
64
        }
219
90
      }
220
30
221
30
      FileActions = &FileActionsStore;
222
30
      posix_spawn_file_actions_init(FileActions);
223
30
224
30
      // Redirect stdin/stdout.
225
30
      if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
226
30
          RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
227
0
        return false;
228
30
      if (!Redirects[1] || !Redirects[2] || 
*Redirects[1] != *Redirects[2]28
) {
229
5
        // Just redirect stderr
230
5
        if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
231
0
          return false;
232
25
      } else {
233
25
        // If stdout and stderr should go to the same place, redirect stderr
234
25
        // to the FD already open for stdout.
235
25
        if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
236
0
          return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
237
18.1k
      }
238
30
    }
239
18.1k
240
18.1k
    if (!Envp)
241
#if !USE_NSGETENVIRON
242
      Envp = const_cast<const char **>(environ);
243
#else
244
      // environ is missing in dylibs.
245
18.1k
      Envp = const_cast<const char **>(*_NSGetEnviron());
246
18.1k
#endif
247
18.1k
248
18.1k
    constexpr int maxRetries = 8;
249
18.1k
    int retries = 0;
250
18.1k
    pid_t PID;
251
18.1k
    int Err;
252
18.1k
    do {
253
18.1k
      PID = 0; // Make Valgrind happy.
254
18.1k
      Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
255
18.1k
                        /*attrp*/ nullptr, const_cast<char **>(Argv),
256
18.1k
                        const_cast<char **>(Envp));
257
18.1k
    } while (Err == EINTR && 
++retries < maxRetries0
);
258
18.1k
259
18.1k
    if (FileActions)
260
30
      posix_spawn_file_actions_destroy(FileActions);
261
18.1k
262
18.1k
    if (Err)
263
0
     return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
264
18.1k
265
18.1k
    PI.Pid = PID;
266
18.1k
    PI.Process = PID;
267
18.1k
268
18.1k
    return true;
269
18.1k
  }
270
205
#endif
271
205
272
205
  // Create a child process.
273
205
  int child = fork();
274
205
  switch (child) {
275
205
    // An error occurred:  Return to the caller.
276
205
    case -1:
277
0
      MakeErrMsg(ErrMsg, "Couldn't fork");
278
0
      return false;
279
205
280
205
    // Child process: Execute the program.
281
205
    case 0: {
282
0
      // Redirect file descriptors...
283
0
      if (!Redirects.empty()) {
284
0
        // Redirect stdin
285
0
        if (RedirectIO(Redirects[0], 0, ErrMsg)) { return false; }
286
0
        // Redirect stdout
287
0
        if (RedirectIO(Redirects[1], 1, ErrMsg)) { return false; }
288
0
        if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) {
289
0
          // If stdout and stderr should go to the same place, redirect stderr
290
0
          // to the FD already open for stdout.
291
0
          if (-1 == dup2(1,2)) {
292
0
            MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
293
0
            return false;
294
0
          }
295
0
        } else {
296
0
          // Just redirect stderr
297
0
          if (RedirectIO(Redirects[2], 2, ErrMsg)) { return false; }
298
0
        }
299
0
      }
300
0
301
0
      // Set memory limits
302
0
      if (MemoryLimit!=0) {
303
0
        SetMemoryLimits(MemoryLimit);
304
0
      }
305
0
306
0
      // Execute!
307
0
      std::string PathStr = Program;
308
0
      if (Envp != nullptr)
309
0
        execve(PathStr.c_str(), const_cast<char **>(Argv),
310
0
               const_cast<char **>(Envp));
311
0
      else
312
0
        execv(PathStr.c_str(), const_cast<char **>(Argv));
313
0
      // If the execve() failed, we should exit. Follow Unix protocol and
314
0
      // return 127 if the executable was not found, and 126 otherwise.
315
0
      // Use _exit rather than exit so that atexit functions and static
316
0
      // object destructors cloned from the parent process aren't
317
0
      // redundantly run, and so that any data buffered in stdio buffers
318
0
      // cloned from the parent aren't redundantly written out.
319
0
      _exit(errno == ENOENT ? 127 : 126);
320
0
    }
321
0
322
0
    // Parent process: Break out of the switch to do our processing.
323
205
    default:
324
205
      break;
325
205
  }
326
205
327
205
  PI.Pid = child;
328
205
  PI.Process = child;
329
205
330
205
  return true;
331
205
}
332
333
namespace llvm {
334
335
ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
336
792k
                      bool WaitUntilTerminates, std::string *ErrMsg) {
337
792k
  struct sigaction Act, Old;
338
792k
  assert(PI.Pid && "invalid pid to wait on, process not started?");
339
792k
340
792k
  int WaitPidOptions = 0;
341
792k
  pid_t ChildPid = PI.Pid;
342
792k
  if (WaitUntilTerminates) {
343
18.1k
    SecondsToWait = 0;
344
774k
  } else if (SecondsToWait) {
345
207
    // Install a timeout handler.  The handler itself does nothing, but the
346
207
    // simple fact of having a handler at all causes the wait below to return
347
207
    // with EINTR, unlike if we used SIG_IGN.
348
207
    memset(&Act, 0, sizeof(Act));
349
207
    Act.sa_handler = TimeOutHandler;
350
207
    sigemptyset(&Act.sa_mask);
351
207
    sigaction(SIGALRM, &Act, &Old);
352
207
    alarm(SecondsToWait);
353
774k
  } else if (SecondsToWait == 0)
354
774k
    WaitPidOptions = WNOHANG;
355
792k
356
792k
  // Parent process: Wait for the child process to terminate.
357
792k
  int status;
358
792k
  ProcessInfo WaitResult;
359
792k
360
792k
  do {
361
792k
    WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions);
362
792k
  } while (WaitUntilTerminates && 
WaitResult.Pid == -118.1k
&& errno
== EINTR0
);
363
792k
364
792k
  if (WaitResult.Pid != PI.Pid) {
365
774k
    if (WaitResult.Pid == 0) {
366
774k
      // Non-blocking wait.
367
774k
      return WaitResult;
368
774k
    } else {
369
1
      if (SecondsToWait && errno == EINTR) {
370
1
        // Kill the child.
371
1
        kill(PI.Pid, SIGKILL);
372
1
373
1
        // Turn off the alarm and restore the signal handler
374
1
        alarm(0);
375
1
        sigaction(SIGALRM, &Old, nullptr);
376
1
377
1
        // Wait for child to die
378
1
        if (wait(&status) != ChildPid)
379
0
          MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
380
1
        else
381
1
          MakeErrMsg(ErrMsg, "Child timed out", 0);
382
1
383
1
        WaitResult.ReturnCode = -2; // Timeout detected
384
1
        return WaitResult;
385
1
      } else 
if (errno0
!= EINTR0
) {
386
0
        MakeErrMsg(ErrMsg, "Error waiting for child process");
387
0
        WaitResult.ReturnCode = -1;
388
0
        return WaitResult;
389
0
      }
390
18.3k
    }
391
774k
  }
392
18.3k
393
18.3k
  // We exited normally without timeout, so turn off the timer.
394
18.3k
  if (SecondsToWait && 
!WaitUntilTerminates206
) {
395
206
    alarm(0);
396
206
    sigaction(SIGALRM, &Old, nullptr);
397
206
  }
398
18.3k
399
18.3k
  // Return the proper exit status. Detect error conditions
400
18.3k
  // so we can return -1 for them and set ErrMsg informatively.
401
18.3k
  int result = 0;
402
18.3k
  if (WIFEXITED(status)) {
403
18.2k
    result = WEXITSTATUS(status);
404
18.2k
    WaitResult.ReturnCode = result;
405
18.2k
406
18.2k
    if (result == 127) {
407
0
      if (ErrMsg)
408
0
        *ErrMsg = llvm::sys::StrError(ENOENT);
409
0
      WaitResult.ReturnCode = -1;
410
0
      return WaitResult;
411
0
    }
412
18.2k
    if (result == 126) {
413
0
      if (ErrMsg)
414
0
        *ErrMsg = "Program could not be executed";
415
0
      WaitResult.ReturnCode = -1;
416
0
      return WaitResult;
417
0
    }
418
101
  } else if (WIFSIGNALED(status)) {
419
101
    if (ErrMsg) {
420
101
      *ErrMsg = strsignal(WTERMSIG(status));
421
101
#ifdef WCOREDUMP
422
101
      if (WCOREDUMP(status))
423
101
        
*ErrMsg += " (core dumped)"0
;
424
101
#endif
425
101
    }
426
101
    // Return a special value to indicate that the process received an unhandled
427
101
    // signal during execution as opposed to failing to execute.
428
101
    WaitResult.ReturnCode = -2;
429
101
  }
430
18.3k
  return WaitResult;
431
18.3k
}
432
433
40.8k
std::error_code sys::ChangeStdinToBinary() {
434
40.8k
  // Do nothing, as Unix doesn't differentiate between text and binary.
435
40.8k
  return std::error_code();
436
40.8k
}
437
438
34.9k
std::error_code sys::ChangeStdoutToBinary() {
439
34.9k
  // Do nothing, as Unix doesn't differentiate between text and binary.
440
34.9k
  return std::error_code();
441
34.9k
}
442
443
std::error_code
444
llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
445
2
                                 WindowsEncodingMethod Encoding /*unused*/) {
446
2
  std::error_code EC;
447
2
  llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::F_Text);
448
2
449
2
  if (EC)
450
0
    return EC;
451
2
452
2
  OS << Contents;
453
2
454
2
  if (OS.has_error())
455
0
    return make_error_code(errc::io_error);
456
2
457
2
  return EC;
458
2
}
459
460
bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program,
461
17.2k
                                                  ArrayRef<StringRef> Args) {
462
17.2k
  static long ArgMax = sysconf(_SC_ARG_MAX);
463
17.2k
  // POSIX requires that _POSIX_ARG_MAX is 4096, which is the lowest possible
464
17.2k
  // value for ARG_MAX on a POSIX compliant system.
465
17.2k
  static long ArgMin = _POSIX_ARG_MAX;
466
17.2k
467
17.2k
  // This the same baseline used by xargs.
468
17.2k
  long EffectiveArgMax = 128 * 1024;
469
17.2k
470
17.2k
  if (EffectiveArgMax > ArgMax)
471
0
    EffectiveArgMax = ArgMax;
472
17.2k
  else if (EffectiveArgMax < ArgMin)
473
0
    EffectiveArgMax = ArgMin;
474
17.2k
475
17.2k
  // System says no practical limit.
476
17.2k
  if (ArgMax == -1)
477
0
    return true;
478
17.2k
479
17.2k
  // Conservatively account for space required by environment variables.
480
17.2k
  long HalfArgMax = EffectiveArgMax / 2;
481
17.2k
482
17.2k
  size_t ArgLength = Program.size() + 1;
483
1.35M
  for (StringRef Arg : Args) {
484
1.35M
    // Ensure that we do not exceed the MAX_ARG_STRLEN constant on Linux, which
485
1.35M
    // does not have a constant unlike what the man pages would have you
486
1.35M
    // believe. Since this limit is pretty high, perform the check
487
1.35M
    // unconditionally rather than trying to be aggressive and limiting it to
488
1.35M
    // Linux only.
489
1.35M
    if (Arg.size() >= (32 * 4096))
490
1
      return false;
491
1.35M
492
1.35M
    ArgLength += Arg.size() + 1;
493
1.35M
    if (ArgLength > size_t(HalfArgMax)) {
494
1
      return false;
495
1
    }
496
1.35M
  }
497
17.2k
498
17.2k
  
return true17.2k
;
499
17.2k
}
500
}