1//===- Win32/Program.cpp - Win32 Program Implementation ------- -*- 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 provides the Win32 specific implementation of the Program class. 10// 11//===----------------------------------------------------------------------===// 12 13#include "llvm/ADT/StringExtras.h" 14#include "llvm/Support/ConvertUTF.h" 15#include "llvm/Support/Errc.h" 16#include "llvm/Support/FileSystem.h" 17#include "llvm/Support/Path.h" 18#include "llvm/Support/Windows/WindowsSupport.h" 19#include "llvm/Support/WindowsError.h" 20#include "llvm/Support/raw_ostream.h" 21#include <psapi.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 33namespace llvm { 34 35ProcessInfo::ProcessInfo() : Pid(0), Process(0), ReturnCode(0) {} 36 37ErrorOr<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 108bool 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 127static 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 = std::string(*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 (sys::windows::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 171static 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 auto Result = flattenWindowsCommandLine(Args); 193 if (std::error_code ec = Result.getError()) { 194 SetLastError(ec.value()); 195 MakeErrMsg(ErrMsg, std::string("Unable to convert command-line to UTF-16")); 196 return false; 197 } 198 std::wstring Command = *Result; 199 200 // The pointer to the environment block for the new process. 201 std::vector<wchar_t> EnvBlock; 202 203 if (Env) { 204 // An environment block consists of a null-terminated block of 205 // null-terminated strings. Convert the array of environment variables to 206 // an environment block by concatenating them. 207 for (StringRef E : *Env) { 208 SmallVector<wchar_t, MAX_PATH> EnvString; 209 if (std::error_code ec = windows::UTF8ToUTF16(E, EnvString)) { 210 SetLastError(ec.value()); 211 MakeErrMsg(ErrMsg, "Unable to convert environment variable to UTF-16"); 212 return false; 213 } 214 215 EnvBlock.insert(EnvBlock.end(), EnvString.begin(), EnvString.end()); 216 EnvBlock.push_back(0); 217 } 218 EnvBlock.push_back(0); 219 } 220 221 // Create a child process. 222 STARTUPINFOW si; 223 memset(&si, 0, sizeof(si)); 224 si.cb = sizeof(si); 225 si.hStdInput = INVALID_HANDLE_VALUE; 226 si.hStdOutput = INVALID_HANDLE_VALUE; 227 si.hStdError = INVALID_HANDLE_VALUE; 228 229 if (!Redirects.empty()) { 230 si.dwFlags = STARTF_USESTDHANDLES; 231 232 si.hStdInput = RedirectIO(Redirects[0], 0, ErrMsg); 233 if (si.hStdInput == INVALID_HANDLE_VALUE) { 234 MakeErrMsg(ErrMsg, "can't redirect stdin"); 235 return false; 236 } 237 si.hStdOutput = RedirectIO(Redirects[1], 1, ErrMsg); 238 if (si.hStdOutput == INVALID_HANDLE_VALUE) { 239 CloseHandle(si.hStdInput); 240 MakeErrMsg(ErrMsg, "can't redirect stdout"); 241 return false; 242 } 243 if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) { 244 // If stdout and stderr should go to the same place, redirect stderr 245 // to the handle already open for stdout. 246 if (!DuplicateHandle(GetCurrentProcess(), si.hStdOutput, 247 GetCurrentProcess(), &si.hStdError, 248 0, TRUE, DUPLICATE_SAME_ACCESS)) { 249 CloseHandle(si.hStdInput); 250 CloseHandle(si.hStdOutput); 251 MakeErrMsg(ErrMsg, "can't dup stderr to stdout"); 252 return false; 253 } 254 } else { 255 // Just redirect stderr 256 si.hStdError = RedirectIO(Redirects[2], 2, ErrMsg); 257 if (si.hStdError == INVALID_HANDLE_VALUE) { 258 CloseHandle(si.hStdInput); 259 CloseHandle(si.hStdOutput); 260 MakeErrMsg(ErrMsg, "can't redirect stderr"); 261 return false; 262 } 263 } 264 } 265 266 PROCESS_INFORMATION pi; 267 memset(&pi, 0, sizeof(pi)); 268 269 fflush(stdout); 270 fflush(stderr); 271 272 SmallVector<wchar_t, MAX_PATH> ProgramUtf16; 273 if (std::error_code ec = sys::windows::widenPath(Program, ProgramUtf16)) { 274 SetLastError(ec.value()); 275 MakeErrMsg(ErrMsg, 276 std::string("Unable to convert application name to UTF-16")); 277 return false; 278 } 279 280 std::vector<wchar_t> CommandUtf16(Command.size() + 1, 0); 281 std::copy(Command.begin(), Command.end(), CommandUtf16.begin()); 282 BOOL rc = CreateProcessW(ProgramUtf16.data(), CommandUtf16.data(), 0, 0, TRUE, 283 CREATE_UNICODE_ENVIRONMENT, 284 EnvBlock.empty() ? 0 : EnvBlock.data(), 0, &si, &pi); 285 DWORD err = GetLastError(); 286 287 // Regardless of whether the process got created or not, we are done with 288 // the handles we created for it to inherit. 289 CloseHandle(si.hStdInput); 290 CloseHandle(si.hStdOutput); 291 CloseHandle(si.hStdError); 292 293 // Now return an error if the process didn't get created. 294 if (!rc) { 295 SetLastError(err); 296 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") + 297 Program.str() + "'"); 298 return false; 299 } 300 301 PI.Pid = pi.dwProcessId; 302 PI.Process = pi.hProcess; 303 304 // Make sure these get closed no matter what. 305 ScopedCommonHandle hThread(pi.hThread); 306 307 // Assign the process to a job if a memory limit is defined. 308 ScopedJobHandle hJob; 309 if (MemoryLimit != 0) { 310 hJob = CreateJobObjectW(0, 0); 311 bool success = false; 312 if (hJob) { 313 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli; 314 memset(&jeli, 0, sizeof(jeli)); 315 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY; 316 jeli.ProcessMemoryLimit = uintptr_t(MemoryLimit) * 1048576; 317 if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, 318 &jeli, sizeof(jeli))) { 319 if (AssignProcessToJobObject(hJob, pi.hProcess)) 320 success = true; 321 } 322 } 323 if (!success) { 324 SetLastError(GetLastError()); 325 MakeErrMsg(ErrMsg, std::string("Unable to set memory limit")); 326 TerminateProcess(pi.hProcess, 1); 327 WaitForSingleObject(pi.hProcess, INFINITE); 328 return false; 329 } 330 } 331 332 return true; 333} 334 335static bool argNeedsQuotes(StringRef Arg) { 336 if (Arg.empty()) 337 return true; 338 return StringRef::npos != Arg.find_first_of("\t \"&\'()*<>\\`^|\n"); 339} 340 341static std::string quoteSingleArg(StringRef Arg) { 342 std::string Result; 343 Result.push_back('"'); 344 345 while (!Arg.empty()) { 346 size_t FirstNonBackslash = Arg.find_first_not_of('\\'); 347 size_t BackslashCount = FirstNonBackslash; 348 if (FirstNonBackslash == StringRef::npos) { 349 // The entire remainder of the argument is backslashes. Escape all of 350 // them and just early out. 351 BackslashCount = Arg.size(); 352 Result.append(BackslashCount * 2, '\\'); 353 break; 354 } 355 356 if (Arg[FirstNonBackslash] == '\"') { 357 // This is an embedded quote. Escape all preceding backslashes, then 358 // add one additional backslash to escape the quote. 359 Result.append(BackslashCount * 2 + 1, '\\'); 360 Result.push_back('\"'); 361 } else { 362 // This is just a normal character. Don't escape any of the preceding 363 // backslashes, just append them as they are and then append the 364 // character. 365 Result.append(BackslashCount, '\\'); 366 Result.push_back(Arg[FirstNonBackslash]); 367 } 368 369 // Drop all the backslashes, plus the following character. 370 Arg = Arg.drop_front(FirstNonBackslash + 1); 371 } 372 373 Result.push_back('"'); 374 return Result; 375} 376 377namespace llvm { 378ErrorOr<std::wstring> sys::flattenWindowsCommandLine(ArrayRef<StringRef> Args) { 379 std::string Command; 380 for (StringRef Arg : Args) { 381 if (argNeedsQuotes(Arg)) 382 Command += quoteSingleArg(Arg); 383 else 384 Command += Arg; 385 386 Command.push_back(' '); 387 } 388 389 SmallVector<wchar_t, MAX_PATH> CommandUtf16; 390 if (std::error_code ec = windows::UTF8ToUTF16(Command, CommandUtf16)) 391 return ec; 392 393 return std::wstring(CommandUtf16.begin(), CommandUtf16.end()); 394} 395 396ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait, 397 bool WaitUntilChildTerminates, std::string *ErrMsg, 398 Optional<ProcessStatistics> *ProcStat) { 399 assert(PI.Pid && "invalid pid to wait on, process not started?"); 400 assert((PI.Process && PI.Process != INVALID_HANDLE_VALUE) && 401 "invalid process handle to wait on, process not started?"); 402 DWORD milliSecondsToWait = 0; 403 if (WaitUntilChildTerminates) 404 milliSecondsToWait = INFINITE; 405 else if (SecondsToWait > 0) 406 milliSecondsToWait = SecondsToWait * 1000; 407 408 ProcessInfo WaitResult = PI; 409 if (ProcStat) 410 ProcStat->reset(); 411 DWORD WaitStatus = WaitForSingleObject(PI.Process, milliSecondsToWait); 412 if (WaitStatus == WAIT_TIMEOUT) { 413 if (SecondsToWait) { 414 if (!TerminateProcess(PI.Process, 1)) { 415 if (ErrMsg) 416 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program"); 417 418 // -2 indicates a crash or timeout as opposed to failure to execute. 419 WaitResult.ReturnCode = -2; 420 CloseHandle(PI.Process); 421 return WaitResult; 422 } 423 WaitForSingleObject(PI.Process, INFINITE); 424 CloseHandle(PI.Process); 425 } else { 426 // Non-blocking wait. 427 return ProcessInfo(); 428 } 429 } 430 431 // Get process execution statistics. 432 if (ProcStat) { 433 FILETIME CreationTime, ExitTime, KernelTime, UserTime; 434 PROCESS_MEMORY_COUNTERS MemInfo; 435 if (GetProcessTimes(PI.Process, &CreationTime, &ExitTime, &KernelTime, 436 &UserTime) && 437 GetProcessMemoryInfo(PI.Process, &MemInfo, sizeof(MemInfo))) { 438 auto UserT = std::chrono::duration_cast<std::chrono::microseconds>( 439 toDuration(UserTime)); 440 auto KernelT = std::chrono::duration_cast<std::chrono::microseconds>( 441 toDuration(KernelTime)); 442 uint64_t PeakMemory = MemInfo.PeakPagefileUsage / 1024; 443 *ProcStat = ProcessStatistics{UserT + KernelT, UserT, PeakMemory}; 444 } 445 } 446 447 // Get its exit status. 448 DWORD status; 449 BOOL rc = GetExitCodeProcess(PI.Process, &status); 450 DWORD err = GetLastError(); 451 if (err != ERROR_INVALID_HANDLE) 452 CloseHandle(PI.Process); 453 454 if (!rc) { 455 SetLastError(err); 456 if (ErrMsg) 457 MakeErrMsg(ErrMsg, "Failed getting status for program"); 458 459 // -2 indicates a crash or timeout as opposed to failure to execute. 460 WaitResult.ReturnCode = -2; 461 return WaitResult; 462 } 463 464 if (!status) 465 return WaitResult; 466 467 // Pass 10(Warning) and 11(Error) to the callee as negative value. 468 if ((status & 0xBFFF0000U) == 0x80000000U) 469 WaitResult.ReturnCode = static_cast<int>(status); 470 else if (status & 0xFF) 471 WaitResult.ReturnCode = status & 0x7FFFFFFF; 472 else 473 WaitResult.ReturnCode = 1; 474 475 return WaitResult; 476} 477 478std::error_code sys::ChangeStdinToBinary() { 479 int result = _setmode(_fileno(stdin), _O_BINARY); 480 if (result == -1) 481 return std::error_code(errno, std::generic_category()); 482 return std::error_code(); 483} 484 485std::error_code sys::ChangeStdoutToBinary() { 486 int result = _setmode(_fileno(stdout), _O_BINARY); 487 if (result == -1) 488 return std::error_code(errno, std::generic_category()); 489 return std::error_code(); 490} 491 492std::error_code 493llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents, 494 WindowsEncodingMethod Encoding) { 495 std::error_code EC; 496 llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OF_Text); 497 if (EC) 498 return EC; 499 500 if (Encoding == WEM_UTF8) { 501 OS << Contents; 502 } else if (Encoding == WEM_CurrentCodePage) { 503 SmallVector<wchar_t, 1> ArgsUTF16; 504 SmallVector<char, 1> ArgsCurCP; 505 506 if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16))) 507 return EC; 508 509 if ((EC = windows::UTF16ToCurCP( 510 ArgsUTF16.data(), ArgsUTF16.size(), ArgsCurCP))) 511 return EC; 512 513 OS.write(ArgsCurCP.data(), ArgsCurCP.size()); 514 } else if (Encoding == WEM_UTF16) { 515 SmallVector<wchar_t, 1> ArgsUTF16; 516 517 if ((EC = windows::UTF8ToUTF16(Contents, ArgsUTF16))) 518 return EC; 519 520 // Endianness guessing 521 char BOM[2]; 522 uint16_t src = UNI_UTF16_BYTE_ORDER_MARK_NATIVE; 523 memcpy(BOM, &src, 2); 524 OS.write(BOM, 2); 525 OS.write((char *)ArgsUTF16.data(), ArgsUTF16.size() << 1); 526 } else { 527 llvm_unreachable("Unknown encoding"); 528 } 529 530 if (OS.has_error()) 531 return make_error_code(errc::io_error); 532 533 return EC; 534} 535 536bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program, 537 ArrayRef<StringRef> Args) { 538 // The documentation on CreateProcessW states that the size of the argument 539 // lpCommandLine must not be greater than 32767 characters, including the 540 // Unicode terminating null character. We use smaller value to reduce risk 541 // of getting invalid command line due to unaccounted factors. 542 static const size_t MaxCommandStringLength = 32000; 543 SmallVector<StringRef, 8> FullArgs; 544 FullArgs.push_back(Program); 545 FullArgs.append(Args.begin(), Args.end()); 546 auto Result = flattenWindowsCommandLine(FullArgs); 547 assert(!Result.getError()); 548 return (Result->size() + 1) <= MaxCommandStringLength; 549} 550} 551