1 //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Program class.
12 //===----------------------------------------------------------------------===//
14 #include "WindowsSupport.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/ConvertUTF.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/WindowsError.h"
20 #include "llvm/Support/raw_ostream.h"
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only Win32 specific code
28 //=== and must not be UNIX code
29 //===----------------------------------------------------------------------===//
34 ProcessInfo::ProcessInfo() : ProcessHandle(0), Pid(0), ReturnCode(0) {}
36 ErrorOr<std::string> sys::findProgramByName(StringRef Name,
37 ArrayRef<StringRef> Paths) {
38 assert(!Name.empty() && "Must have a name!");
40 if (Name.find_first_of("/\\") != StringRef::npos)
41 return std::string(Name);
43 const wchar_t *Path = nullptr;
44 std::wstring PathStorage;
46 PathStorage.reserve(Paths.size() * MAX_PATH);
47 for (unsigned i = 0; i < Paths.size(); ++i) {
49 PathStorage.push_back(L';');
50 StringRef P = Paths[i];
51 SmallVector<wchar_t, MAX_PATH> TmpPath;
52 if (std::error_code EC = windows::UTF8ToUTF16(P, TmpPath))
54 PathStorage.append(TmpPath.begin(), TmpPath.end());
56 Path = PathStorage.c_str();
59 SmallVector<wchar_t, MAX_PATH> U16Name;
60 if (std::error_code EC = windows::UTF8ToUTF16(Name, U16Name))
63 SmallVector<StringRef, 12> PathExts;
64 PathExts.push_back("");
65 PathExts.push_back(".exe"); // FIXME: This must be in %PATHEXT%.
66 if (const char *PathExtEnv = std::getenv("PATHEXT"))
67 SplitString(PathExtEnv, PathExts, ";");
69 SmallVector<wchar_t, MAX_PATH> U16Result;
71 for (StringRef Ext : PathExts) {
72 SmallVector<wchar_t, MAX_PATH> U16Ext;
73 if (std::error_code EC = windows::UTF8ToUTF16(Ext, U16Ext))
77 U16Result.reserve(Len);
78 // Lets attach the extension manually. That is needed for files
79 // with a point in name like aaa.bbb. SearchPathW will not add extension
80 // from its argument to such files because it thinks they already had one.
81 SmallVector<wchar_t, MAX_PATH> U16NameExt;
82 if (std::error_code EC =
83 windows::UTF8ToUTF16(Twine(Name + Ext).str(), U16NameExt))
86 Len = ::SearchPathW(Path, c_str(U16NameExt), nullptr,
87 U16Result.capacity(), U16Result.data(), nullptr);
88 } while (Len > U16Result.capacity());
95 return mapWindowsError(::GetLastError());
97 U16Result.set_size(Len);
99 SmallVector<char, MAX_PATH> U8Result;
100 if (std::error_code EC =
101 windows::UTF16ToUTF8(U16Result.data(), U16Result.size(), U8Result))
104 return std::string(U8Result.begin(), U8Result.end());
107 static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
110 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
111 GetCurrentProcess(), &h,
112 0, TRUE, DUPLICATE_SAME_ACCESS))
113 return INVALID_HANDLE_VALUE;
123 SECURITY_ATTRIBUTES sa;
124 sa.nLength = sizeof(sa);
125 sa.lpSecurityDescriptor = 0;
126 sa.bInheritHandle = TRUE;
128 SmallVector<wchar_t, 128> fnameUnicode;
130 // Don't play long-path tricks on "NUL".
131 if (windows::UTF8ToUTF16(fname, fnameUnicode))
132 return INVALID_HANDLE_VALUE;
134 if (path::widenPath(fname, fnameUnicode))
135 return INVALID_HANDLE_VALUE;
137 h = CreateFileW(fnameUnicode.data(), fd ? GENERIC_WRITE : GENERIC_READ,
138 FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
139 FILE_ATTRIBUTE_NORMAL, NULL);
140 if (h == INVALID_HANDLE_VALUE) {
141 MakeErrMsg(ErrMsg, fname + ": Can't open file for " +
142 (fd ? "input" : "output"));
148 /// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
150 static bool ArgNeedsQuotes(const char *Str) {
151 return Str[0] == '\0' || strpbrk(Str, "\t \"&\'()*<>\\`^|") != 0;
154 /// CountPrecedingBackslashes - Returns the number of backslashes preceding Cur
155 /// in the C string Start.
156 static unsigned int CountPrecedingBackslashes(const char *Start,
158 unsigned int Count = 0;
160 while (Cur >= Start && *Cur == '\\') {
167 /// EscapePrecedingEscapes - Append a backslash to Dst for every backslash
168 /// preceding Cur in the Start string. Assumes Dst has enough space.
169 static char *EscapePrecedingEscapes(char *Dst, const char *Start,
171 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Cur);
172 while (PrecedingEscapes > 0) {
179 /// ArgLenWithQuotes - Check whether argument needs to be quoted when calling
180 /// CreateProcess and returns length of quoted arg with escaped quotes
181 static unsigned int ArgLenWithQuotes(const char *Str) {
182 const char *Start = Str;
183 bool Quoted = ArgNeedsQuotes(Str);
184 unsigned int len = Quoted ? 2 : 0;
186 while (*Str != '\0') {
188 // We need to add a backslash, but ensure that it isn't escaped.
189 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
190 len += PrecedingEscapes + 1;
192 // Note that we *don't* need to escape runs of backslashes that don't
193 // precede a double quote! See MSDN:
194 // http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx
201 // Make sure the closing quote doesn't get escaped by a trailing backslash.
202 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
203 len += PrecedingEscapes + 1;
211 static std::unique_ptr<char[]> flattenArgs(const char **args) {
212 // First, determine the length of the command line.
214 for (unsigned i = 0; args[i]; i++) {
215 len += ArgLenWithQuotes(args[i]) + 1;
218 // Now build the command line.
219 std::unique_ptr<char[]> command(new char[len+1]);
220 char *p = command.get();
222 for (unsigned i = 0; args[i]; i++) {
223 const char *arg = args[i];
224 const char *start = arg;
226 bool needsQuoting = ArgNeedsQuotes(arg);
230 while (*arg != '\0') {
232 // Escape all preceding escapes (if any), and then escape the quote.
233 p = EscapePrecedingEscapes(p, start, arg);
241 // Make sure our quote doesn't get escaped by a trailing backslash.
242 p = EscapePrecedingEscapes(p, start, arg);
252 static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
253 const char **envp, const StringRef **redirects,
254 unsigned memoryLimit, std::string *ErrMsg) {
255 if (!sys::fs::can_execute(Program)) {
257 *ErrMsg = "program not executable";
261 // can_execute may succeed by looking at Program + ".exe". CreateProcessW
262 // will implicitly add the .exe if we provide a command line without an
263 // executable path, but since we use an explicit executable, we have to add
265 SmallString<64> ProgramStorage;
266 if (!sys::fs::exists(Program))
267 Program = Twine(Program + ".exe").toStringRef(ProgramStorage);
269 // Windows wants a command line, not an array of args, to pass to the new
270 // process. We have to concatenate them all, while quoting the args that
271 // have embedded spaces (or are empty).
272 std::unique_ptr<char[]> command = flattenArgs(args);
274 // The pointer to the environment block for the new process.
275 std::vector<wchar_t> EnvBlock;
278 // An environment block consists of a null-terminated block of
279 // null-terminated strings. Convert the array of environment variables to
280 // an environment block by concatenating them.
281 for (unsigned i = 0; envp[i]; ++i) {
282 SmallVector<wchar_t, MAX_PATH> EnvString;
283 if (std::error_code ec = windows::UTF8ToUTF16(envp[i], EnvString)) {
284 SetLastError(ec.value());
285 MakeErrMsg(ErrMsg, "Unable to convert environment variable to UTF-16");
289 EnvBlock.insert(EnvBlock.end(), EnvString.begin(), EnvString.end());
290 EnvBlock.push_back(0);
292 EnvBlock.push_back(0);
295 // Create a child process.
297 memset(&si, 0, sizeof(si));
299 si.hStdInput = INVALID_HANDLE_VALUE;
300 si.hStdOutput = INVALID_HANDLE_VALUE;
301 si.hStdError = INVALID_HANDLE_VALUE;
304 si.dwFlags = STARTF_USESTDHANDLES;
306 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
307 if (si.hStdInput == INVALID_HANDLE_VALUE) {
308 MakeErrMsg(ErrMsg, "can't redirect stdin");
311 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
312 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
313 CloseHandle(si.hStdInput);
314 MakeErrMsg(ErrMsg, "can't redirect stdout");
317 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
318 // If stdout and stderr should go to the same place, redirect stderr
319 // to the handle already open for stdout.
320 if (!DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
321 GetCurrentProcess(), &si.hStdError,
322 0, TRUE, DUPLICATE_SAME_ACCESS)) {
323 CloseHandle(si.hStdInput);
324 CloseHandle(si.hStdOutput);
325 MakeErrMsg(ErrMsg, "can't dup stderr to stdout");
329 // Just redirect stderr
330 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
331 if (si.hStdError == INVALID_HANDLE_VALUE) {
332 CloseHandle(si.hStdInput);
333 CloseHandle(si.hStdOutput);
334 MakeErrMsg(ErrMsg, "can't redirect stderr");
340 PROCESS_INFORMATION pi;
341 memset(&pi, 0, sizeof(pi));
346 SmallVector<wchar_t, MAX_PATH> ProgramUtf16;
347 if (std::error_code ec = path::widenPath(Program, ProgramUtf16)) {
348 SetLastError(ec.value());
350 std::string("Unable to convert application name to UTF-16"));
354 SmallVector<wchar_t, MAX_PATH> CommandUtf16;
355 if (std::error_code ec = windows::UTF8ToUTF16(command.get(), CommandUtf16)) {
356 SetLastError(ec.value());
358 std::string("Unable to convert command-line to UTF-16"));
362 BOOL rc = CreateProcessW(ProgramUtf16.data(), CommandUtf16.data(), 0, 0,
363 TRUE, CREATE_UNICODE_ENVIRONMENT,
364 EnvBlock.empty() ? 0 : EnvBlock.data(), 0, &si,
366 DWORD err = GetLastError();
368 // Regardless of whether the process got created or not, we are done with
369 // the handles we created for it to inherit.
370 CloseHandle(si.hStdInput);
371 CloseHandle(si.hStdOutput);
372 CloseHandle(si.hStdError);
374 // Now return an error if the process didn't get created.
377 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
378 Program.str() + "'");
382 PI.Pid = pi.dwProcessId;
383 PI.ProcessHandle = pi.hProcess;
385 // Make sure these get closed no matter what.
386 ScopedCommonHandle hThread(pi.hThread);
388 // Assign the process to a job if a memory limit is defined.
389 ScopedJobHandle hJob;
390 if (memoryLimit != 0) {
391 hJob = CreateJobObjectW(0, 0);
392 bool success = false;
394 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
395 memset(&jeli, 0, sizeof(jeli));
396 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
397 jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576;
398 if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
399 &jeli, sizeof(jeli))) {
400 if (AssignProcessToJobObject(hJob, pi.hProcess))
405 SetLastError(GetLastError());
406 MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
407 TerminateProcess(pi.hProcess, 1);
408 WaitForSingleObject(pi.hProcess, INFINITE);
417 ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
418 bool WaitUntilChildTerminates, std::string *ErrMsg) {
419 assert(PI.Pid && "invalid pid to wait on, process not started?");
420 assert(PI.ProcessHandle &&
421 "invalid process handle to wait on, process not started?");
422 DWORD milliSecondsToWait = 0;
423 if (WaitUntilChildTerminates)
424 milliSecondsToWait = INFINITE;
425 else if (SecondsToWait > 0)
426 milliSecondsToWait = SecondsToWait * 1000;
428 ProcessInfo WaitResult = PI;
429 DWORD WaitStatus = WaitForSingleObject(PI.ProcessHandle, milliSecondsToWait);
430 if (WaitStatus == WAIT_TIMEOUT) {
432 if (!TerminateProcess(PI.ProcessHandle, 1)) {
434 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program");
436 // -2 indicates a crash or timeout as opposed to failure to execute.
437 WaitResult.ReturnCode = -2;
438 CloseHandle(PI.ProcessHandle);
441 WaitForSingleObject(PI.ProcessHandle, INFINITE);
442 CloseHandle(PI.ProcessHandle);
444 // Non-blocking wait.
445 return ProcessInfo();
449 // Get its exit status.
451 BOOL rc = GetExitCodeProcess(PI.ProcessHandle, &status);
452 DWORD err = GetLastError();
453 if (err != ERROR_INVALID_HANDLE)
454 CloseHandle(PI.ProcessHandle);
459 MakeErrMsg(ErrMsg, "Failed getting status for program");
461 // -2 indicates a crash or timeout as opposed to failure to execute.
462 WaitResult.ReturnCode = -2;
469 // Pass 10(Warning) and 11(Error) to the callee as negative value.
470 if ((status & 0xBFFF0000U) == 0x80000000U)
471 WaitResult.ReturnCode = static_cast<int>(status);
472 else if (status & 0xFF)
473 WaitResult.ReturnCode = status & 0x7FFFFFFF;
475 WaitResult.ReturnCode = 1;
480 std::error_code sys::ChangeStdinToBinary() {
481 int result = _setmode(_fileno(stdin), _O_BINARY);
483 return std::error_code(errno, std::generic_category());
484 return std::error_code();
487 std::error_code sys::ChangeStdoutToBinary() {
488 int result = _setmode(_fileno(stdout), _O_BINARY);
490 return std::error_code(errno, std::generic_category());
491 return std::error_code();
495 llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
496 WindowsEncodingMethod Encoding) {
498 llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::F_Text);
502 if (Encoding == WEM_UTF8) {
504 } else if (Encoding == WEM_CurrentCodePage) {
505 SmallVector<wchar_t, 1> ArgsUTF16;
506 SmallVector<char, 1> ArgsCurCP;
508 if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16)))
511 if ((EC = windows::UTF16ToCurCP(
512 ArgsUTF16.data(), ArgsUTF16.size(), ArgsCurCP)))
515 OS.write(ArgsCurCP.data(), ArgsCurCP.size());
516 } else if (Encoding == WEM_UTF16) {
517 SmallVector<wchar_t, 1> ArgsUTF16;
519 if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16)))
522 // Endianness guessing
524 uint16_t src = UNI_UTF16_BYTE_ORDER_MARK_NATIVE;
525 memcpy(BOM, &src, 2);
527 OS.write((char *)ArgsUTF16.data(), ArgsUTF16.size() << 1);
529 llvm_unreachable("Unknown encoding");
533 return make_error_code(errc::io_error);
538 bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef<const char*> Args) {
539 // The documented max length of the command line passed to CreateProcess.
540 static const size_t MaxCommandStringLength = 32768;
541 // Account for the trailing space for the program path and the
542 // trailing NULL of the last argument.
543 size_t ArgLength = ArgLenWithQuotes(Program.str().c_str()) + 2;
544 for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
546 // Account for the trailing space for every arg
547 ArgLength += ArgLenWithQuotes(*I) + 1;
548 if (ArgLength > MaxCommandStringLength) {