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/FileSystem.h"
18 #include "llvm/Support/WindowsError.h"
19 #include "llvm/Support/raw_ostream.h"
25 //===----------------------------------------------------------------------===//
26 //=== WARNING: Implementation here must contain only Win32 specific code
27 //=== and must not be UNIX code
28 //===----------------------------------------------------------------------===//
33 ProcessInfo::ProcessInfo() : ProcessHandle(0), Pid(0), ReturnCode(0) {}
35 ErrorOr<std::string> sys::findProgramByName(StringRef Name,
36 ArrayRef<StringRef> Paths) {
37 assert(!Name.empty() && "Must have a name!");
39 if (Name.find_first_of("/\\") != StringRef::npos)
40 return std::string(Name);
42 const wchar_t *Path = nullptr;
43 std::wstring PathStorage;
45 PathStorage.reserve(Paths.size() * MAX_PATH);
46 for (unsigned i = 0; i < Paths.size(); ++i) {
48 PathStorage.push_back(L';');
49 StringRef P = Paths[i];
50 SmallVector<wchar_t, MAX_PATH> TmpPath;
51 if (std::error_code EC = windows::UTF8ToUTF16(P, TmpPath))
53 PathStorage.append(TmpPath.begin(), TmpPath.end());
55 Path = PathStorage.c_str();
58 SmallVector<wchar_t, MAX_PATH> U16Name;
59 if (std::error_code EC = windows::UTF8ToUTF16(Name, U16Name))
62 SmallVector<StringRef, 12> PathExts;
63 PathExts.push_back("");
64 PathExts.push_back(".exe"); // FIXME: This must be in %PATHEXT%.
65 if (const char *PathExtEnv = std::getenv("PATHEXT"))
66 SplitString(PathExtEnv, PathExts, ";");
68 SmallVector<wchar_t, MAX_PATH> U16Result;
70 for (StringRef Ext : PathExts) {
71 SmallVector<wchar_t, MAX_PATH> U16Ext;
72 if (std::error_code EC = windows::UTF8ToUTF16(Ext, U16Ext))
76 U16Result.reserve(Len);
77 Len = ::SearchPathW(Path, c_str(U16Name),
78 U16Ext.empty() ? nullptr : c_str(U16Ext),
79 U16Result.capacity(), U16Result.data(), nullptr);
80 } while (Len > U16Result.capacity());
87 return mapWindowsError(::GetLastError());
89 U16Result.set_size(Len);
91 SmallVector<char, MAX_PATH> U8Result;
92 if (std::error_code EC =
93 windows::UTF16ToUTF8(U16Result.data(), U16Result.size(), U8Result))
96 return std::string(U8Result.begin(), U8Result.end());
99 static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
102 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
103 GetCurrentProcess(), &h,
104 0, TRUE, DUPLICATE_SAME_ACCESS))
105 return INVALID_HANDLE_VALUE;
115 SECURITY_ATTRIBUTES sa;
116 sa.nLength = sizeof(sa);
117 sa.lpSecurityDescriptor = 0;
118 sa.bInheritHandle = TRUE;
120 SmallVector<wchar_t, 128> fnameUnicode;
122 // Don't play long-path tricks on "NUL".
123 if (windows::UTF8ToUTF16(fname, fnameUnicode))
124 return INVALID_HANDLE_VALUE;
126 if (path::widenPath(fname, fnameUnicode))
127 return INVALID_HANDLE_VALUE;
129 h = CreateFileW(fnameUnicode.data(), fd ? GENERIC_WRITE : GENERIC_READ,
130 FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
131 FILE_ATTRIBUTE_NORMAL, NULL);
132 if (h == INVALID_HANDLE_VALUE) {
133 MakeErrMsg(ErrMsg, fname + ": Can't open file for " +
134 (fd ? "input: " : "output: "));
140 /// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
142 static bool ArgNeedsQuotes(const char *Str) {
143 return Str[0] == '\0' || strpbrk(Str, "\t \"&\'()*<>\\`^|") != 0;
146 /// CountPrecedingBackslashes - Returns the number of backslashes preceding Cur
147 /// in the C string Start.
148 static unsigned int CountPrecedingBackslashes(const char *Start,
150 unsigned int Count = 0;
152 while (Cur >= Start && *Cur == '\\') {
159 /// EscapePrecedingEscapes - Append a backslash to Dst for every backslash
160 /// preceding Cur in the Start string. Assumes Dst has enough space.
161 static char *EscapePrecedingEscapes(char *Dst, const char *Start,
163 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Cur);
164 while (PrecedingEscapes > 0) {
171 /// ArgLenWithQuotes - Check whether argument needs to be quoted when calling
172 /// CreateProcess and returns length of quoted arg with escaped quotes
173 static unsigned int ArgLenWithQuotes(const char *Str) {
174 const char *Start = Str;
175 bool Quoted = ArgNeedsQuotes(Str);
176 unsigned int len = Quoted ? 2 : 0;
178 while (*Str != '\0') {
180 // We need to add a backslash, but ensure that it isn't escaped.
181 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
182 len += PrecedingEscapes + 1;
184 // Note that we *don't* need to escape runs of backslashes that don't
185 // precede a double quote! See MSDN:
186 // http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx
193 // Make sure the closing quote doesn't get escaped by a trailing backslash.
194 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
195 len += PrecedingEscapes + 1;
203 static std::unique_ptr<char[]> flattenArgs(const char **args) {
204 // First, determine the length of the command line.
206 for (unsigned i = 0; args[i]; i++) {
207 len += ArgLenWithQuotes(args[i]) + 1;
210 // Now build the command line.
211 std::unique_ptr<char[]> command(new char[len+1]);
212 char *p = command.get();
214 for (unsigned i = 0; args[i]; i++) {
215 const char *arg = args[i];
216 const char *start = arg;
218 bool needsQuoting = ArgNeedsQuotes(arg);
222 while (*arg != '\0') {
224 // Escape all preceding escapes (if any), and then escape the quote.
225 p = EscapePrecedingEscapes(p, start, arg);
233 // Make sure our quote doesn't get escaped by a trailing backslash.
234 p = EscapePrecedingEscapes(p, start, arg);
244 static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
245 const char **envp, const StringRef **redirects,
246 unsigned memoryLimit, std::string *ErrMsg) {
247 if (!sys::fs::can_execute(Program)) {
249 *ErrMsg = "program not executable";
253 // Windows wants a command line, not an array of args, to pass to the new
254 // process. We have to concatenate them all, while quoting the args that
255 // have embedded spaces (or are empty).
256 std::unique_ptr<char[]> command = flattenArgs(args);
258 // The pointer to the environment block for the new process.
259 std::vector<wchar_t> EnvBlock;
262 // An environment block consists of a null-terminated block of
263 // null-terminated strings. Convert the array of environment variables to
264 // an environment block by concatenating them.
265 for (unsigned i = 0; envp[i]; ++i) {
266 SmallVector<wchar_t, MAX_PATH> EnvString;
267 if (std::error_code ec = windows::UTF8ToUTF16(envp[i], EnvString)) {
268 SetLastError(ec.value());
269 MakeErrMsg(ErrMsg, "Unable to convert environment variable to UTF-16");
273 EnvBlock.insert(EnvBlock.end(), EnvString.begin(), EnvString.end());
274 EnvBlock.push_back(0);
276 EnvBlock.push_back(0);
279 // Create a child process.
281 memset(&si, 0, sizeof(si));
283 si.hStdInput = INVALID_HANDLE_VALUE;
284 si.hStdOutput = INVALID_HANDLE_VALUE;
285 si.hStdError = INVALID_HANDLE_VALUE;
288 si.dwFlags = STARTF_USESTDHANDLES;
290 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
291 if (si.hStdInput == INVALID_HANDLE_VALUE) {
292 MakeErrMsg(ErrMsg, "can't redirect stdin");
295 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
296 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
297 CloseHandle(si.hStdInput);
298 MakeErrMsg(ErrMsg, "can't redirect stdout");
301 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
302 // If stdout and stderr should go to the same place, redirect stderr
303 // to the handle already open for stdout.
304 if (!DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
305 GetCurrentProcess(), &si.hStdError,
306 0, TRUE, DUPLICATE_SAME_ACCESS)) {
307 CloseHandle(si.hStdInput);
308 CloseHandle(si.hStdOutput);
309 MakeErrMsg(ErrMsg, "can't dup stderr to stdout");
313 // Just redirect stderr
314 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
315 if (si.hStdError == INVALID_HANDLE_VALUE) {
316 CloseHandle(si.hStdInput);
317 CloseHandle(si.hStdOutput);
318 MakeErrMsg(ErrMsg, "can't redirect stderr");
324 PROCESS_INFORMATION pi;
325 memset(&pi, 0, sizeof(pi));
330 SmallVector<wchar_t, MAX_PATH> ProgramUtf16;
331 if (std::error_code ec = path::widenPath(Program, ProgramUtf16)) {
332 SetLastError(ec.value());
334 std::string("Unable to convert application name to UTF-16"));
338 SmallVector<wchar_t, MAX_PATH> CommandUtf16;
339 if (std::error_code ec = windows::UTF8ToUTF16(command.get(), CommandUtf16)) {
340 SetLastError(ec.value());
342 std::string("Unable to convert command-line to UTF-16"));
346 BOOL rc = CreateProcessW(ProgramUtf16.data(), CommandUtf16.data(), 0, 0,
347 TRUE, CREATE_UNICODE_ENVIRONMENT,
348 EnvBlock.empty() ? 0 : EnvBlock.data(), 0, &si,
350 DWORD err = GetLastError();
352 // Regardless of whether the process got created or not, we are done with
353 // the handles we created for it to inherit.
354 CloseHandle(si.hStdInput);
355 CloseHandle(si.hStdOutput);
356 CloseHandle(si.hStdError);
358 // Now return an error if the process didn't get created.
361 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
362 Program.str() + "'");
366 PI.Pid = pi.dwProcessId;
367 PI.ProcessHandle = pi.hProcess;
369 // Make sure these get closed no matter what.
370 ScopedCommonHandle hThread(pi.hThread);
372 // Assign the process to a job if a memory limit is defined.
373 ScopedJobHandle hJob;
374 if (memoryLimit != 0) {
375 hJob = CreateJobObjectW(0, 0);
376 bool success = false;
378 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
379 memset(&jeli, 0, sizeof(jeli));
380 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
381 jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576;
382 if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
383 &jeli, sizeof(jeli))) {
384 if (AssignProcessToJobObject(hJob, pi.hProcess))
389 SetLastError(GetLastError());
390 MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
391 TerminateProcess(pi.hProcess, 1);
392 WaitForSingleObject(pi.hProcess, INFINITE);
401 ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
402 bool WaitUntilChildTerminates, std::string *ErrMsg) {
403 assert(PI.Pid && "invalid pid to wait on, process not started?");
404 assert(PI.ProcessHandle &&
405 "invalid process handle to wait on, process not started?");
406 DWORD milliSecondsToWait = 0;
407 if (WaitUntilChildTerminates)
408 milliSecondsToWait = INFINITE;
409 else if (SecondsToWait > 0)
410 milliSecondsToWait = SecondsToWait * 1000;
412 ProcessInfo WaitResult = PI;
413 DWORD WaitStatus = WaitForSingleObject(PI.ProcessHandle, milliSecondsToWait);
414 if (WaitStatus == WAIT_TIMEOUT) {
416 if (!TerminateProcess(PI.ProcessHandle, 1)) {
418 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program.");
420 // -2 indicates a crash or timeout as opposed to failure to execute.
421 WaitResult.ReturnCode = -2;
422 CloseHandle(PI.ProcessHandle);
425 WaitForSingleObject(PI.ProcessHandle, INFINITE);
426 CloseHandle(PI.ProcessHandle);
428 // Non-blocking wait.
429 return ProcessInfo();
433 // Get its exit status.
435 BOOL rc = GetExitCodeProcess(PI.ProcessHandle, &status);
436 DWORD err = GetLastError();
437 if (err != ERROR_INVALID_HANDLE)
438 CloseHandle(PI.ProcessHandle);
443 MakeErrMsg(ErrMsg, "Failed getting status for program.");
445 // -2 indicates a crash or timeout as opposed to failure to execute.
446 WaitResult.ReturnCode = -2;
453 // Pass 10(Warning) and 11(Error) to the callee as negative value.
454 if ((status & 0xBFFF0000U) == 0x80000000U)
455 WaitResult.ReturnCode = static_cast<int>(status);
456 else if (status & 0xFF)
457 WaitResult.ReturnCode = status & 0x7FFFFFFF;
459 WaitResult.ReturnCode = 1;
464 std::error_code sys::ChangeStdinToBinary() {
465 int result = _setmode(_fileno(stdin), _O_BINARY);
467 return std::error_code(errno, std::generic_category());
468 return std::error_code();
471 std::error_code sys::ChangeStdoutToBinary() {
472 int result = _setmode(_fileno(stdout), _O_BINARY);
474 return std::error_code(errno, std::generic_category());
475 return std::error_code();
479 llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
480 WindowsEncodingMethod Encoding) {
482 llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::F_Text);
486 if (Encoding == WEM_UTF8) {
488 } else if (Encoding == WEM_CurrentCodePage) {
489 SmallVector<wchar_t, 1> ArgsUTF16;
490 SmallVector<char, 1> ArgsCurCP;
492 if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16)))
495 if ((EC = windows::UTF16ToCurCP(
496 ArgsUTF16.data(), ArgsUTF16.size(), ArgsCurCP)))
499 OS.write(ArgsCurCP.data(), ArgsCurCP.size());
500 } else if (Encoding == WEM_UTF16) {
501 SmallVector<wchar_t, 1> ArgsUTF16;
503 if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16)))
506 // Endianness guessing
508 uint16_t src = UNI_UTF16_BYTE_ORDER_MARK_NATIVE;
509 memcpy(BOM, &src, 2);
511 OS.write((char *)ArgsUTF16.data(), ArgsUTF16.size() << 1);
513 llvm_unreachable("Unknown encoding");
517 return std::make_error_code(std::errc::io_error);
522 bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) {
523 // The documented max length of the command line passed to CreateProcess.
524 static const size_t MaxCommandStringLength = 32768;
525 size_t ArgLength = 0;
526 for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
528 // Account for the trailing space for every arg but the last one and the
529 // trailing NULL of the last argument.
530 ArgLength += ArgLenWithQuotes(*I) + 1;
531 if (ArgLength > MaxCommandStringLength) {