Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 1 | //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===// |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Program class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 14 | #include "Win32.h" |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 15 | #include <cstdio> |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 16 | #include <malloc.h> |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 17 | #include <io.h> |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 21 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 22 | //=== and must not be UNIX code |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | using namespace sys; |
| 27 | |
| 28 | // This function just uses the PATH environment variable to find the program. |
| 29 | Path |
| 30 | Program::FindProgramByName(const std::string& progName) { |
| 31 | |
| 32 | // Check some degenerate cases |
| 33 | if (progName.length() == 0) // no program |
| 34 | return Path(); |
| 35 | Path temp; |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 36 | if (!temp.set(progName)) // invalid name |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 37 | return Path(); |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 38 | if (temp.canExecute()) // already executable as is |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 39 | return temp; |
| 40 | |
| 41 | // At this point, the file name is valid and its not executable. |
| 42 | // Let Windows search for it. |
| 43 | char buffer[MAX_PATH]; |
| 44 | char *dummy = NULL; |
| 45 | DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH, |
| 46 | buffer, &dummy); |
| 47 | |
| 48 | // See if it wasn't found. |
| 49 | if (len == 0) |
| 50 | return Path(); |
| 51 | |
| 52 | // See if we got the entire path. |
| 53 | if (len < MAX_PATH) |
| 54 | return Path(buffer); |
| 55 | |
| 56 | // Buffer was too small; grow and retry. |
| 57 | while (true) { |
| 58 | char *b = reinterpret_cast<char *>(_alloca(len+1)); |
| 59 | DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy); |
| 60 | |
| 61 | // It is unlikely the search failed, but it's always possible some file |
| 62 | // was added or removed since the last search, so be paranoid... |
| 63 | if (len2 == 0) |
| 64 | return Path(); |
| 65 | else if (len2 <= len) |
| 66 | return Path(b); |
| 67 | |
| 68 | len = len2; |
| 69 | } |
| 70 | } |
| 71 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 72 | static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) { |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 73 | HANDLE h; |
| 74 | if (path == 0) { |
| 75 | DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd), |
| 76 | GetCurrentProcess(), &h, |
| 77 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 78 | return h; |
| 79 | } |
| 80 | |
| 81 | const char *fname = path->toString().c_str(); |
| 82 | if (*fname == 0) |
| 83 | fname = "NUL"; |
| 84 | |
| 85 | SECURITY_ATTRIBUTES sa; |
| 86 | sa.nLength = sizeof(sa); |
| 87 | sa.lpSecurityDescriptor = 0; |
| 88 | sa.bInheritHandle = TRUE; |
| 89 | |
| 90 | h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ, |
Jeff Cohen | 45a1b26 | 2005-02-20 02:48:51 +0000 | [diff] [blame] | 91 | &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS, |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 92 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 93 | if (h == INVALID_HANDLE_VALUE) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 94 | MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " + |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 95 | (fd ? "input: " : "output: ")); |
| 96 | } |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 97 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 98 | return h; |
| 99 | } |
| 100 | |
Anton Korobeynikov | 349ec1c | 2007-03-09 11:53:34 +0000 | [diff] [blame] | 101 | #ifdef __MINGW32__ |
| 102 | // Due to unknown reason, mingw32's w32api doesn't have this declaration. |
Anton Korobeynikov | d437db1 | 2007-03-19 20:19:08 +0000 | [diff] [blame] | 103 | extern "C" |
Anton Korobeynikov | 349ec1c | 2007-03-09 11:53:34 +0000 | [diff] [blame] | 104 | BOOL WINAPI SetInformationJobObject(HANDLE hJob, |
| 105 | JOBOBJECTINFOCLASS JobObjectInfoClass, |
| 106 | LPVOID lpJobObjectInfo, |
| 107 | DWORD cbJobObjectInfoLength); |
| 108 | #endif |
| 109 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 110 | int |
| 111 | Program::ExecuteAndWait(const Path& path, |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 112 | const char** args, |
| 113 | const char** envp, |
| 114 | const Path** redirects, |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 115 | unsigned secondsToWait, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 116 | unsigned memoryLimit, |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 117 | std::string* ErrMsg) { |
| 118 | if (!path.canExecute()) { |
| 119 | if (ErrMsg) |
| 120 | *ErrMsg = "program not executable"; |
| 121 | return -1; |
| 122 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 123 | |
| 124 | // Windows wants a command line, not an array of args, to pass to the new |
| 125 | // process. We have to concatenate them all, while quoting the args that |
| 126 | // have embedded spaces. |
| 127 | |
| 128 | // First, determine the length of the command line. |
Jeff Cohen | e5f7e65 | 2005-02-16 04:43:45 +0000 | [diff] [blame] | 129 | unsigned len = 0; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 130 | for (unsigned i = 0; args[i]; i++) { |
| 131 | len += strlen(args[i]) + 1; |
| 132 | if (strchr(args[i], ' ')) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 133 | len += 2; |
| 134 | } |
| 135 | |
| 136 | // Now build the command line. |
| 137 | char *command = reinterpret_cast<char *>(_alloca(len)); |
| 138 | char *p = command; |
| 139 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 140 | for (unsigned i = 0; args[i]; i++) { |
| 141 | const char *arg = args[i]; |
Jeff Cohen | 01c5513 | 2005-04-11 03:44:22 +0000 | [diff] [blame] | 142 | size_t len = strlen(arg); |
Jeff Cohen | e5f7e65 | 2005-02-16 04:43:45 +0000 | [diff] [blame] | 143 | bool needsQuoting = strchr(arg, ' ') != 0; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 144 | if (needsQuoting) |
| 145 | *p++ = '"'; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 146 | memcpy(p, arg, len); |
| 147 | p += len; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 148 | if (needsQuoting) |
| 149 | *p++ = '"'; |
| 150 | *p++ = ' '; |
| 151 | } |
| 152 | |
| 153 | *p = 0; |
| 154 | |
| 155 | // Create a child process. |
| 156 | STARTUPINFO si; |
| 157 | memset(&si, 0, sizeof(si)); |
| 158 | si.cb = sizeof(si); |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 159 | si.hStdInput = INVALID_HANDLE_VALUE; |
| 160 | si.hStdOutput = INVALID_HANDLE_VALUE; |
| 161 | si.hStdError = INVALID_HANDLE_VALUE; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 162 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 163 | if (redirects) { |
| 164 | si.dwFlags = STARTF_USESTDHANDLES; |
| 165 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 166 | si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg); |
| 167 | if (si.hStdInput == INVALID_HANDLE_VALUE) { |
| 168 | MakeErrMsg(ErrMsg, "can't redirect stdin"); |
| 169 | return -1; |
| 170 | } |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 171 | si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 172 | if (si.hStdOutput == INVALID_HANDLE_VALUE) { |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 173 | CloseHandle(si.hStdInput); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 174 | MakeErrMsg(ErrMsg, "can't redirect stdout"); |
| 175 | return -1; |
| 176 | } |
| 177 | if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) { |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 178 | si.hStdError = RedirectIO(redirects[2], 2, ErrMsg); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 179 | if (si.hStdError == INVALID_HANDLE_VALUE) { |
| 180 | CloseHandle(si.hStdInput); |
| 181 | CloseHandle(si.hStdOutput); |
| 182 | MakeErrMsg(ErrMsg, "can't redirect stderr"); |
| 183 | return -1; |
| 184 | } |
| 185 | } else { |
| 186 | DuplicateHandle(GetCurrentProcess(), si.hStdOutput, |
| 187 | GetCurrentProcess(), &si.hStdError, |
| 188 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 191 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 192 | PROCESS_INFORMATION pi; |
| 193 | memset(&pi, 0, sizeof(pi)); |
| 194 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 195 | fflush(stdout); |
| 196 | fflush(stderr); |
| 197 | BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0, |
| 198 | envp, NULL, &si, &pi); |
| 199 | DWORD err = GetLastError(); |
| 200 | |
| 201 | // Regardless of whether the process got created or not, we are done with |
| 202 | // the handles we created for it to inherit. |
| 203 | CloseHandle(si.hStdInput); |
| 204 | CloseHandle(si.hStdOutput); |
| 205 | CloseHandle(si.hStdError); |
| 206 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 207 | // Now return an error if the process didn't get created. |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 208 | if (!rc) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 209 | { |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 210 | SetLastError(err); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 211 | MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") + |
Reid Spencer | 707a27c | 2004-12-11 17:37:01 +0000 | [diff] [blame] | 212 | path.toString() + "'"); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 213 | return -1; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 216 | // Make sure these get closed no matter what. |
| 217 | AutoHandle hProcess(pi.hProcess); |
| 218 | AutoHandle hThread(pi.hThread); |
| 219 | |
| 220 | // Assign the process to a job if a memory limit is defined. |
| 221 | AutoHandle hJob(0); |
| 222 | if (memoryLimit != 0) { |
| 223 | hJob = CreateJobObject(0, 0); |
| 224 | bool success = false; |
| 225 | if (hJob != 0) { |
| 226 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli; |
| 227 | memset(&jeli, 0, sizeof(jeli)); |
| 228 | jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY; |
Jeff Cohen | 413bc82 | 2007-03-05 05:45:08 +0000 | [diff] [blame] | 229 | jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576; |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 230 | if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, |
| 231 | &jeli, sizeof(jeli))) { |
| 232 | if (AssignProcessToJobObject(hJob, pi.hProcess)) |
| 233 | success = true; |
| 234 | } |
| 235 | } |
| 236 | if (!success) { |
| 237 | SetLastError(GetLastError()); |
| 238 | MakeErrMsg(ErrMsg, std::string("Unable to set memory limit")); |
| 239 | TerminateProcess(pi.hProcess, 1); |
| 240 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 241 | return -1; |
| 242 | } |
| 243 | } |
| 244 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 245 | // Wait for it to terminate. |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 246 | DWORD millisecondsToWait = INFINITE; |
| 247 | if (secondsToWait > 0) |
| 248 | millisecondsToWait = secondsToWait * 1000; |
| 249 | |
| 250 | if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) { |
| 251 | if (!TerminateProcess(pi.hProcess, 1)) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 252 | MakeErrMsg(ErrMsg, std::string("Failed to terminate timed-out program '") |
| 253 | + path.toString() + "'"); |
| 254 | return -1; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 255 | } |
| 256 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 257 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 258 | |
| 259 | // Get its exit status. |
| 260 | DWORD status; |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 261 | rc = GetExitCodeProcess(pi.hProcess, &status); |
| 262 | err = GetLastError(); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 263 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 264 | if (!rc) { |
| 265 | SetLastError(err); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 266 | MakeErrMsg(ErrMsg, std::string("Failed getting status for program '") + |
Reid Spencer | 707a27c | 2004-12-11 17:37:01 +0000 | [diff] [blame] | 267 | path.toString() + "'"); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 268 | return -1; |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 269 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 270 | |
| 271 | return status; |
| 272 | } |
| 273 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 274 | bool Program::ChangeStdinToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 275 | int result = _setmode( _fileno(stdin), _O_BINARY ); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 276 | return result == -1; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 279 | bool Program::ChangeStdoutToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 280 | int result = _setmode( _fileno(stdout), _O_BINARY ); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 281 | return result == -1; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 284 | } |