LLVM  8.0.1
Program.inc
Go to the documentation of this file.
1 //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides the Win32 specific implementation of the Program class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "WindowsSupport.h"
15 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Path.h"
22 #include <cstdio>
23 #include <fcntl.h>
24 #include <io.h>
25 #include <malloc.h>
26 #include <numeric>
27 
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only Win32 specific code
30 //=== and must not be UNIX code
31 //===----------------------------------------------------------------------===//
32 
33 namespace llvm {
34 
35 ProcessInfo::ProcessInfo() : Pid(0), Process(0), ReturnCode(0) {}
36 
37 ErrorOr<std::string> sys::findProgramByName(StringRef Name,
38  ArrayRef<StringRef> Paths) {
39  assert(!Name.empty() && "Must have a name!");
40 
41  if (Name.find_first_of("/\\") != StringRef::npos)
42  return std::string(Name);
43 
44  const wchar_t *Path = nullptr;
45  std::wstring PathStorage;
46  if (!Paths.empty()) {
47  PathStorage.reserve(Paths.size() * MAX_PATH);
48  for (unsigned i = 0; i < Paths.size(); ++i) {
49  if (i)
50  PathStorage.push_back(L';');
51  StringRef P = Paths[i];
52  SmallVector<wchar_t, MAX_PATH> TmpPath;
53  if (std::error_code EC = windows::UTF8ToUTF16(P, TmpPath))
54  return EC;
55  PathStorage.append(TmpPath.begin(), TmpPath.end());
56  }
57  Path = PathStorage.c_str();
58  }
59 
60  SmallVector<wchar_t, MAX_PATH> U16Name;
61  if (std::error_code EC = windows::UTF8ToUTF16(Name, U16Name))
62  return EC;
63 
64  SmallVector<StringRef, 12> PathExts;
65  PathExts.push_back("");
66  PathExts.push_back(".exe"); // FIXME: This must be in %PATHEXT%.
67  if (const char *PathExtEnv = std::getenv("PATHEXT"))
68  SplitString(PathExtEnv, PathExts, ";");
69 
70  SmallVector<wchar_t, MAX_PATH> U16Result;
71  DWORD Len = MAX_PATH;
72  for (StringRef Ext : PathExts) {
73  SmallVector<wchar_t, MAX_PATH> U16Ext;
74  if (std::error_code EC = windows::UTF8ToUTF16(Ext, U16Ext))
75  return EC;
76 
77  do {
78  U16Result.reserve(Len);
79  // Lets attach the extension manually. That is needed for files
80  // with a point in name like aaa.bbb. SearchPathW will not add extension
81  // from its argument to such files because it thinks they already had one.
82  SmallVector<wchar_t, MAX_PATH> U16NameExt;
83  if (std::error_code EC =
84  windows::UTF8ToUTF16(Twine(Name + Ext).str(), U16NameExt))
85  return EC;
86 
87  Len = ::SearchPathW(Path, c_str(U16NameExt), nullptr,
88  U16Result.capacity(), U16Result.data(), nullptr);
89  } while (Len > U16Result.capacity());
90 
91  if (Len != 0)
92  break; // Found it.
93  }
94 
95  if (Len == 0)
96  return mapWindowsError(::GetLastError());
97 
98  U16Result.set_size(Len);
99 
100  SmallVector<char, MAX_PATH> U8Result;
101  if (std::error_code EC =
102  windows::UTF16ToUTF8(U16Result.data(), U16Result.size(), U8Result))
103  return EC;
104 
105  return std::string(U8Result.begin(), U8Result.end());
106 }
107 
108 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
109  if (!ErrMsg)
110  return true;
111  char *buffer = NULL;
112  DWORD LastError = GetLastError();
113  DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
114  FORMAT_MESSAGE_FROM_SYSTEM |
115  FORMAT_MESSAGE_MAX_WIDTH_MASK,
116  NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
117  if (R)
118  *ErrMsg = prefix + ": " + buffer;
119  else
120  *ErrMsg = prefix + ": Unknown error";
121  *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
122 
123  LocalFree(buffer);
124  return R != 0;
125 }
126 
127 static HANDLE RedirectIO(Optional<StringRef> Path, int fd,
128  std::string *ErrMsg) {
129  HANDLE h;
130  if (!Path) {
131  if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
132  GetCurrentProcess(), &h,
133  0, TRUE, DUPLICATE_SAME_ACCESS))
134  return INVALID_HANDLE_VALUE;
135  return h;
136  }
137 
138  std::string fname;
139  if (Path->empty())
140  fname = "NUL";
141  else
142  fname = *Path;
143 
144  SECURITY_ATTRIBUTES sa;
145  sa.nLength = sizeof(sa);
146  sa.lpSecurityDescriptor = 0;
147  sa.bInheritHandle = TRUE;
148 
149  SmallVector<wchar_t, 128> fnameUnicode;
150  if (Path->empty()) {
151  // Don't play long-path tricks on "NUL".
152  if (windows::UTF8ToUTF16(fname, fnameUnicode))
153  return INVALID_HANDLE_VALUE;
154  } else {
155  if (path::widenPath(fname, fnameUnicode))
156  return INVALID_HANDLE_VALUE;
157  }
158  h = CreateFileW(fnameUnicode.data(), fd ? GENERIC_WRITE : GENERIC_READ,
159  FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
160  FILE_ATTRIBUTE_NORMAL, NULL);
161  if (h == INVALID_HANDLE_VALUE) {
162  MakeErrMsg(ErrMsg, fname + ": Can't open file for " +
163  (fd ? "input" : "output"));
164  }
165 
166  return h;
167 }
168 
169 }
170 
171 static bool Execute(ProcessInfo &PI, StringRef Program,
172  ArrayRef<StringRef> Args, Optional<ArrayRef<StringRef>> Env,
173  ArrayRef<Optional<StringRef>> Redirects,
174  unsigned MemoryLimit, std::string *ErrMsg) {
175  if (!sys::fs::can_execute(Program)) {
176  if (ErrMsg)
177  *ErrMsg = "program not executable";
178  return false;
179  }
180 
181  // can_execute may succeed by looking at Program + ".exe". CreateProcessW
182  // will implicitly add the .exe if we provide a command line without an
183  // executable path, but since we use an explicit executable, we have to add
184  // ".exe" ourselves.
185  SmallString<64> ProgramStorage;
186  if (!sys::fs::exists(Program))
187  Program = Twine(Program + ".exe").toStringRef(ProgramStorage);
188 
189  // Windows wants a command line, not an array of args, to pass to the new
190  // process. We have to concatenate them all, while quoting the args that
191  // have embedded spaces (or are empty).
192  std::string Command = flattenWindowsCommandLine(Args);
193 
194  // The pointer to the environment block for the new process.
195  std::vector<wchar_t> EnvBlock;
196 
197  if (Env) {
198  // An environment block consists of a null-terminated block of
199  // null-terminated strings. Convert the array of environment variables to
200  // an environment block by concatenating them.
201  for (const auto E : *Env) {
202  SmallVector<wchar_t, MAX_PATH> EnvString;
203  if (std::error_code ec = windows::UTF8ToUTF16(E, EnvString)) {
204  SetLastError(ec.value());
205  MakeErrMsg(ErrMsg, "Unable to convert environment variable to UTF-16");
206  return false;
207  }
208 
209  EnvBlock.insert(EnvBlock.end(), EnvString.begin(), EnvString.end());
210  EnvBlock.push_back(0);
211  }
212  EnvBlock.push_back(0);
213  }
214 
215  // Create a child process.
216  STARTUPINFOW si;
217  memset(&si, 0, sizeof(si));
218  si.cb = sizeof(si);
219  si.hStdInput = INVALID_HANDLE_VALUE;
220  si.hStdOutput = INVALID_HANDLE_VALUE;
221  si.hStdError = INVALID_HANDLE_VALUE;
222 
223  if (!Redirects.empty()) {
224  si.dwFlags = STARTF_USESTDHANDLES;
225 
226  si.hStdInput = RedirectIO(Redirects[0], 0, ErrMsg);
227  if (si.hStdInput == INVALID_HANDLE_VALUE) {
228  MakeErrMsg(ErrMsg, "can't redirect stdin");
229  return false;
230  }
231  si.hStdOutput = RedirectIO(Redirects[1], 1, ErrMsg);
232  if (si.hStdOutput == INVALID_HANDLE_VALUE) {
233  CloseHandle(si.hStdInput);
234  MakeErrMsg(ErrMsg, "can't redirect stdout");
235  return false;
236  }
237  if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) {
238  // If stdout and stderr should go to the same place, redirect stderr
239  // to the handle already open for stdout.
240  if (!DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
241  GetCurrentProcess(), &si.hStdError,
242  0, TRUE, DUPLICATE_SAME_ACCESS)) {
243  CloseHandle(si.hStdInput);
244  CloseHandle(si.hStdOutput);
245  MakeErrMsg(ErrMsg, "can't dup stderr to stdout");
246  return false;
247  }
248  } else {
249  // Just redirect stderr
250  si.hStdError = RedirectIO(Redirects[2], 2, ErrMsg);
251  if (si.hStdError == INVALID_HANDLE_VALUE) {
252  CloseHandle(si.hStdInput);
253  CloseHandle(si.hStdOutput);
254  MakeErrMsg(ErrMsg, "can't redirect stderr");
255  return false;
256  }
257  }
258  }
259 
260  PROCESS_INFORMATION pi;
261  memset(&pi, 0, sizeof(pi));
262 
263  fflush(stdout);
264  fflush(stderr);
265 
266  SmallVector<wchar_t, MAX_PATH> ProgramUtf16;
267  if (std::error_code ec = path::widenPath(Program, ProgramUtf16)) {
268  SetLastError(ec.value());
269  MakeErrMsg(ErrMsg,
270  std::string("Unable to convert application name to UTF-16"));
271  return false;
272  }
273 
274  SmallVector<wchar_t, MAX_PATH> CommandUtf16;
275  if (std::error_code ec = windows::UTF8ToUTF16(Command, CommandUtf16)) {
276  SetLastError(ec.value());
277  MakeErrMsg(ErrMsg,
278  std::string("Unable to convert command-line to UTF-16"));
279  return false;
280  }
281 
282  BOOL rc = CreateProcessW(ProgramUtf16.data(), CommandUtf16.data(), 0, 0,
283  TRUE, CREATE_UNICODE_ENVIRONMENT,
284  EnvBlock.empty() ? 0 : EnvBlock.data(), 0, &si,
285  &pi);
286  DWORD err = GetLastError();
287 
288  // Regardless of whether the process got created or not, we are done with
289  // the handles we created for it to inherit.
290  CloseHandle(si.hStdInput);
291  CloseHandle(si.hStdOutput);
292  CloseHandle(si.hStdError);
293 
294  // Now return an error if the process didn't get created.
295  if (!rc) {
296  SetLastError(err);
297  MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
298  Program.str() + "'");
299  return false;
300  }
301 
302  PI.Pid = pi.dwProcessId;
303  PI.Process = pi.hProcess;
304 
305  // Make sure these get closed no matter what.
306  ScopedCommonHandle hThread(pi.hThread);
307 
308  // Assign the process to a job if a memory limit is defined.
309  ScopedJobHandle hJob;
310  if (MemoryLimit != 0) {
311  hJob = CreateJobObjectW(0, 0);
312  bool success = false;
313  if (hJob) {
314  JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
315  memset(&jeli, 0, sizeof(jeli));
316  jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
317  jeli.ProcessMemoryLimit = uintptr_t(MemoryLimit) * 1048576;
318  if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
319  &jeli, sizeof(jeli))) {
320  if (AssignProcessToJobObject(hJob, pi.hProcess))
321  success = true;
322  }
323  }
324  if (!success) {
325  SetLastError(GetLastError());
326  MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
327  TerminateProcess(pi.hProcess, 1);
328  WaitForSingleObject(pi.hProcess, INFINITE);
329  return false;
330  }
331  }
332 
333  return true;
334 }
335 
336 static bool argNeedsQuotes(StringRef Arg) {
337  if (Arg.empty())
338  return true;
339  return StringRef::npos != Arg.find_first_of("\t \"&\'()*<>\\`^|\n");
340 }
341 
342 static std::string quoteSingleArg(StringRef Arg) {
343  std::string Result;
344  Result.push_back('"');
345 
346  while (!Arg.empty()) {
347  size_t FirstNonBackslash = Arg.find_first_not_of('\\');
348  size_t BackslashCount = FirstNonBackslash;
349  if (FirstNonBackslash == StringRef::npos) {
350  // The entire remainder of the argument is backslashes. Escape all of
351  // them and just early out.
352  BackslashCount = Arg.size();
353  Result.append(BackslashCount * 2, '\\');
354  break;
355  }
356 
357  if (Arg[FirstNonBackslash] == '\"') {
358  // This is an embedded quote. Escape all preceding backslashes, then
359  // add one additional backslash to escape the quote.
360  Result.append(BackslashCount * 2 + 1, '\\');
361  Result.push_back('\"');
362  } else {
363  // This is just a normal character. Don't escape any of the preceding
364  // backslashes, just append them as they are and then append the
365  // character.
366  Result.append(BackslashCount, '\\');
367  Result.push_back(Arg[FirstNonBackslash]);
368  }
369 
370  // Drop all the backslashes, plus the following character.
371  Arg = Arg.drop_front(FirstNonBackslash + 1);
372  }
373 
374  Result.push_back('"');
375  return Result;
376 }
377 
378 namespace llvm {
379 std::string sys::flattenWindowsCommandLine(ArrayRef<StringRef> Args) {
380  std::string Command;
381  for (StringRef Arg : Args) {
382  if (argNeedsQuotes(Arg))
383  Command += quoteSingleArg(Arg);
384  else
385  Command += Arg;
386 
387  Command.push_back(' ');
388  }
389 
390  return Command;
391 }
392 
393 ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
394  bool WaitUntilChildTerminates, std::string *ErrMsg) {
395  assert(PI.Pid && "invalid pid to wait on, process not started?");
396  assert((PI.Process && PI.Process != INVALID_HANDLE_VALUE) &&
397  "invalid process handle to wait on, process not started?");
398  DWORD milliSecondsToWait = 0;
399  if (WaitUntilChildTerminates)
400  milliSecondsToWait = INFINITE;
401  else if (SecondsToWait > 0)
402  milliSecondsToWait = SecondsToWait * 1000;
403 
404  ProcessInfo WaitResult = PI;
405  DWORD WaitStatus = WaitForSingleObject(PI.Process, milliSecondsToWait);
406  if (WaitStatus == WAIT_TIMEOUT) {
407  if (SecondsToWait) {
408  if (!TerminateProcess(PI.Process, 1)) {
409  if (ErrMsg)
410  MakeErrMsg(ErrMsg, "Failed to terminate timed-out program");
411 
412  // -2 indicates a crash or timeout as opposed to failure to execute.
413  WaitResult.ReturnCode = -2;
414  CloseHandle(PI.Process);
415  return WaitResult;
416  }
417  WaitForSingleObject(PI.Process, INFINITE);
418  CloseHandle(PI.Process);
419  } else {
420  // Non-blocking wait.
421  return ProcessInfo();
422  }
423  }
424 
425  // Get its exit status.
426  DWORD status;
427  BOOL rc = GetExitCodeProcess(PI.Process, &status);
428  DWORD err = GetLastError();
429  if (err != ERROR_INVALID_HANDLE)
430  CloseHandle(PI.Process);
431 
432  if (!rc) {
433  SetLastError(err);
434  if (ErrMsg)
435  MakeErrMsg(ErrMsg, "Failed getting status for program");
436 
437  // -2 indicates a crash or timeout as opposed to failure to execute.
438  WaitResult.ReturnCode = -2;
439  return WaitResult;
440  }
441 
442  if (!status)
443  return WaitResult;
444 
445  // Pass 10(Warning) and 11(Error) to the callee as negative value.
446  if ((status & 0xBFFF0000U) == 0x80000000U)
447  WaitResult.ReturnCode = static_cast<int>(status);
448  else if (status & 0xFF)
449  WaitResult.ReturnCode = status & 0x7FFFFFFF;
450  else
451  WaitResult.ReturnCode = 1;
452 
453  return WaitResult;
454 }
455 
456 std::error_code sys::ChangeStdinToBinary() {
457  int result = _setmode(_fileno(stdin), _O_BINARY);
458  if (result == -1)
459  return std::error_code(errno, std::generic_category());
460  return std::error_code();
461 }
462 
463 std::error_code sys::ChangeStdoutToBinary() {
464  int result = _setmode(_fileno(stdout), _O_BINARY);
465  if (result == -1)
466  return std::error_code(errno, std::generic_category());
467  return std::error_code();
468 }
469 
470 std::error_code
471 llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
472  WindowsEncodingMethod Encoding) {
473  std::error_code EC;
475  if (EC)
476  return EC;
477 
478  if (Encoding == WEM_UTF8) {
479  OS << Contents;
480  } else if (Encoding == WEM_CurrentCodePage) {
481  SmallVector<wchar_t, 1> ArgsUTF16;
482  SmallVector<char, 1> ArgsCurCP;
483 
484  if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16)))
485  return EC;
486 
487  if ((EC = windows::UTF16ToCurCP(
488  ArgsUTF16.data(), ArgsUTF16.size(), ArgsCurCP)))
489  return EC;
490 
491  OS.write(ArgsCurCP.data(), ArgsCurCP.size());
492  } else if (Encoding == WEM_UTF16) {
493  SmallVector<wchar_t, 1> ArgsUTF16;
494 
495  if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16)))
496  return EC;
497 
498  // Endianness guessing
499  char BOM[2];
500  uint16_t src = UNI_UTF16_BYTE_ORDER_MARK_NATIVE;
501  memcpy(BOM, &src, 2);
502  OS.write(BOM, 2);
503  OS.write((char *)ArgsUTF16.data(), ArgsUTF16.size() << 1);
504  } else {
505  llvm_unreachable("Unknown encoding");
506  }
507 
508  if (OS.has_error())
510 
511  return EC;
512 }
513 
514 bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program,
515  ArrayRef<StringRef> Args) {
516  // The documented max length of the command line passed to CreateProcess.
517  static const size_t MaxCommandStringLength = 32768;
518  SmallVector<StringRef, 8> FullArgs;
519  FullArgs.push_back(Program);
520  FullArgs.append(Args.begin(), Args.end());
521  std::string Result = flattenWindowsCommandLine(FullArgs);
522  return (Result.size() + 1) <= MaxCommandStringLength;
523 }
524 }
ScopedHandle< JobHandleTraits > ScopedJobHandle
bool can_execute(const Twine &Path)
Can we execute this file?
This class represents lattice values for constants.
Definition: AllocatorList.h:24
std::error_code ChangeStdoutToBinary()
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
UTF-8 is the LLVM native encoding, being the same as "do not perform encoding conversion"...
Definition: Program.h:149
amdgpu Simplify well known AMD library false Value Value const Twine & Name
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
std::error_code make_error_code(BitcodeError E)
void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \\\)
SplitString - Split up the specified string according to the specified delimiters, appending the result fragments to the output list.
#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE
Definition: ConvertUTF.h:124
#define rc(i)
#define P(N)
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
std::error_code mapWindowsError(unsigned EV)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
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.
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
WindowsEncodingMethod
File encoding options when writing contents that a non-UTF8 tool will read (on Windows systems)...
Definition: Program.h:146
std::error_code ChangeStdinToBinary()
amdgpu Simplify well known AMD library false Value Value * Arg
success
Parameters (see the expansion example below): (the builder, addr, loaded, new_val, ordering, /* OUT.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:366
static const size_t npos
Definition: StringRef.h:51
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=nullptr)
This function waits for the process specified by PI to finish.
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1022
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:125