llvm.org GIT mirror llvm / b262556
Revert "Windows: Add support for unicode command lines" This is causing MinGW bots to fail. This reverts commit r192069. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@192070 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 5 years ago
18 changed file(s) with 94 addition(s) and 187 deletion(s). Raw diff Collapse all Expand all
13981398 if test "$llvm_cv_os_type" = "MingW" ; then
13991399 AC_CHECK_LIB(imagehlp, main)
14001400 AC_CHECK_LIB(psapi, main)
1401 AC_CHECK_LIB(shell32, main)
14021401 fi
14031402
14041403 dnl dlopen() is required for plugin support.
414414 if( MINGW )
415415 set(HAVE_LIBIMAGEHLP 1)
416416 set(HAVE_LIBPSAPI 1)
417 set(HAVE_LIBSHELL32 1)
418417 # TODO: Check existence of libraries.
419418 # include(CheckLibraryExists)
420419 # CHECK_LIBRARY_EXISTS(imagehlp ??? . HAVE_LIBIMAGEHLP)
11 # Returns in `return_var' a list of system libraries used by LLVM.
22 if( NOT MSVC )
33 if( MINGW )
4 set(system_libs ${system_libs} imagehlp psapi shell32)
4 set(system_libs ${system_libs} imagehlp psapi)
55 elseif( CMAKE_HOST_UNIX )
66 if( HAVE_LIBRT )
77 set(system_libs ${system_libs} rt)
127127 endif()
128128
129129 if( MINGW )
130 target_link_libraries(${target} imagehlp psapi shell32)
130 target_link_libraries(${target} imagehlp psapi)
131131 if(CMAKE_SIZEOF_VOID_P MATCHES "8")
132132 set_target_properties(${target} PROPERTIES LINK_FLAGS -Wl,--stack,16777216)
133133 endif(CMAKE_SIZEOF_VOID_P MATCHES "8")
202202 /* Define to 1 if you have the `pthread' library (-lpthread). */
203203 #cmakedefine HAVE_LIBPTHREAD ${HAVE_LIBPTHREAD}
204204
205 /* Define to 1 if you have the `shell32' library (-lshell32). */
206 #cmakedefine HAVE_LIBSHELL32 ${HAVE_LIBSHELL32}
207
208205 /* Define to 1 if you have the `udis86' library (-ludis86). */
209206 #undef HAVE_LIBUDIS86
210207
216216 /* Define to 1 if you have the `pthread' library (-lpthread). */
217217 #undef HAVE_LIBPTHREAD
218218
219 /* Define to 1 if you have the `shell32' library (-lshell32). */
220 #undef HAVE_LIBSHELL32
221
222219 /* Define to 1 if you have the `udis86' library (-ludis86). */
223220 #undef HAVE_LIBUDIS86
224221
2424 #ifndef LLVM_SUPPORT_PROCESS_H
2525 #define LLVM_SUPPORT_PROCESS_H
2626
27 #include "llvm/ADT/ArrayRef.h"
2827 #include "llvm/ADT/Optional.h"
2928 #include "llvm/Config/llvm-config.h"
30 #include "llvm/Support/Allocator.h"
31 #include "llvm/Support/system_error.h"
3229 #include "llvm/Support/DataTypes.h"
3330 #include "llvm/Support/TimeValue.h"
3431
170167 // string. \arg Name is assumed to be in UTF-8 encoding too.
171168 static Optional GetEnv(StringRef name);
172169
173 /// This function returns a SmallVector containing the arguments passed from
174 /// the operating system to the program. This function expects to be handed
175 /// the vector passed in from main.
176 static error_code
177 GetArgumentVector(SmallVectorImpl &Args,
178 ArrayRef ArgsFromMain,
179 SpecificBumpPtrAllocator &ArgAllocator);
180
181170 /// This function determines if the standard input is connected directly
182171 /// to a user's input (keyboard probably), rather than coming from a file
183172 /// or pipe.
189189 return std::string(Val);
190190 }
191191
192 error_code Process::GetArgumentVector(SmallVectorImpl &ArgsOut,
193 ArrayRef ArgsIn,
194 SpecificBumpPtrAllocator &) {
195 ArgsOut.append(ArgsIn.begin(), ArgsIn.end());
196
197 return error_code::success();
198 }
199
200192 bool Process::StandardInIsUserInput() {
201193 return FileDescriptorIsDisplayed(STDIN_FILENO);
202194 }
8282 // This is mostly to ensure that the return value still shows up as "valid".
8383 return DynamicLibrary(&OpenedHandles);
8484 }
85
86 SmallVector filenameUnicode;
87 if (error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
88 SetLastError(ec.value());
89 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: ");
90 return DynamicLibrary();
91 }
9285
93 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
86 HMODULE a_handle = LoadLibrary(filename);
9487
9588 if (a_handle == 0) {
9689 MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
127127 BYTE val = 0;
128128 if (!::CryptGenRandom(CryptoProvider, 1, &val))
129129 return windows_error(::GetLastError());
130 random_path_utf16.push_back(L"0123456789abcdef"[val & 15]);
130 random_path_utf16.push_back("0123456789abcdef"[val & 15]);
131131 }
132132 else
133133 random_path_utf16.push_back(*i);
240240 }
241241
242242 error_code current_path(SmallVectorImpl &result) {
243 SmallVector cur_path;
244 DWORD len = MAX_PATH;
245
246 do {
243 SmallVector cur_path;
244 cur_path.reserve(128);
245 retry_cur_dir:
246 DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
247
248 // A zero return value indicates a failure other than insufficient space.
249 if (len == 0)
250 return windows_error(::GetLastError());
251
252 // If there's insufficient space, the len returned is larger than the len
253 // given.
254 if (len > cur_path.capacity()) {
247255 cur_path.reserve(len);
248 len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data());
249
250 // A zero return value indicates a failure other than insufficient space.
251 if (len == 0)
252 return windows_error(::GetLastError());
253
254 // If there's insufficient space, the len returned is larger than the len
255 // given.
256 } while (len > cur_path.capacity());
257
258 // On success, GetCurrentDirectoryW returns the number of characters not
259 // including the null-terminator.
256 goto retry_cur_dir;
257 }
258
260259 cur_path.set_size(len);
261260 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
262261 }
99 // This file provides the Win32 specific implementation of the Process class.
1010 //
1111 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/Allocator.h"
1412
1513 #include "Windows.h"
1614 #include
1715 #include
1816 #include
1917 #include
20 #include
2118
2219 #ifdef __MINGW32__
2320 #if (HAVE_LIBPSAPI != 1)
2421 #error "libpsapi.a should be present"
2522 #endif
26 #if (HAVE_LIBSHELL32 != 1)
27 #error "libshell32.a should be present"
28 #endif
2923 #else
30 #pragma comment(lib, "psapi.lib")
31 #pragma comment(lib, "Shell32.lib")
24 #pragma comment(lib, "psapi.lib")
3225 #endif
3326
3427 //===----------------------------------------------------------------------===//
157150 // Environment variable can be encoded in non-UTF8 encoding, and there's no
158151 // way to know what the encoding is. The only reliable way to look up
159152 // multibyte environment variable is to use GetEnvironmentVariableW().
160 SmallVector Buf;
161 size_t Size = MAX_PATH;
162 do {
163 Buf.reserve(Size);
164 Size = GetEnvironmentVariableW(&NameUTF16[0], &Buf[0], Buf.capacity());
165 if (Size == 0)
166 return None;
167
153 std::vector Buf(16);
154 size_t Size = 0;
155 for (;;) {
156 Size = GetEnvironmentVariableW(&NameUTF16[0], &Buf[0], Buf.size());
157 if (Size < Buf.size())
158 break;
168159 // Try again with larger buffer.
169 } while (Size > Buf.capacity());
170 Buf.set_size(Size);
160 Buf.resize(Size + 1);
161 }
162 if (Size == 0)
163 return None;
171164
172165 // Convert the result from UTF-16 to UTF-8.
173 SmallVectorMAX_PATH> Res;
166 SmallVector128> Res;
174167 if (error_code ec = windows::UTF16ToUTF8(&Buf[0], Size, Res))
175168 return None;
176169 return std::string(&Res[0]);
177 }
178
179 error_code
180 Process::GetArgumentVector(SmallVectorImpl &Args,
181 ArrayRef,
182 SpecificBumpPtrAllocator &ArgAllocator) {
183 int NewArgCount;
184 error_code ec;
185
186 wchar_t **UnicodeCommandLine = CommandLineToArgvW(GetCommandLineW(),
187 &NewArgCount);
188 if (!UnicodeCommandLine)
189 return windows_error(::GetLastError());
190
191 Args.reserve(NewArgCount);
192
193 for (int i = 0; i < NewArgCount; ++i) {
194 SmallVector NewArgString;
195 ec = windows::UTF16ToUTF8(UnicodeCommandLine[i],
196 wcslen(UnicodeCommandLine[i]),
197 NewArgString);
198 if (ec)
199 break;
200
201 char *Buffer = ArgAllocator.Allocate(NewArgString.size() + 1);
202 ::memcpy(Buffer, NewArgString.data(), NewArgString.size() + 1);
203 Args.push_back(Buffer);
204 }
205 LocalFree(UnicodeCommandLine);
206 if (ec)
207 return ec;
208
209 return error_code::success();
210170 }
211171
212172 bool Process::StandardInIsUserInput() {
4141
4242 // At this point, the file name is valid and does not contain slashes.
4343 // Let Windows search for it.
44 SmallVector progNameUnicode;
45 if (windows::UTF8ToUTF16(progName, progNameUnicode))
44 std::string buffer;
45 buffer.resize(MAX_PATH);
46 char *dummy = NULL;
47 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
48 &buffer[0], &dummy);
49
50 // See if it wasn't found.
51 if (len == 0)
4652 return "";
4753
48 SmallVector buffer;
49 DWORD len = MAX_PATH;
50 do {
51 buffer.reserve(len);
52 len = ::SearchPathW(NULL, progNameUnicode.data(), L".exe",
53 buffer.capacity(), buffer.data(), NULL);
54
55 // See if it wasn't found.
56 if (len == 0)
54 // See if we got the entire path.
55 if (len < MAX_PATH)
56 return buffer;
57
58 // Buffer was too small; grow and retry.
59 while (true) {
60 buffer.resize(len+1);
61 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, &buffer[0], &dummy);
62
63 // It is unlikely the search failed, but it's always possible some file
64 // was added or removed since the last search, so be paranoid...
65 if (len2 == 0)
5766 return "";
58
59 // Buffer was too small; grow and retry.
60 } while (len > buffer.capacity());
61
62 buffer.set_size(len);
63 SmallVector result;
64 if (windows::UTF16ToUTF8(buffer.begin(), buffer.size(), result))
65 return "";
66
67 return std::string(result.data(), result.size());
67 else if (len2 <= len)
68 return buffer;
69
70 len = len2;
71 }
6872 }
6973
7074 static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
7175 HANDLE h;
7276 if (path == 0) {
73 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
74 GetCurrentProcess(), &h,
75 0, TRUE, DUPLICATE_SAME_ACCESS))
76 return INVALID_HANDLE_VALUE;
77 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
78 GetCurrentProcess(), &h,
79 0, TRUE, DUPLICATE_SAME_ACCESS);
7780 return h;
7881 }
7982
8891 sa.lpSecurityDescriptor = 0;
8992 sa.bInheritHandle = TRUE;
9093
91 SmallVector fnameUnicode;
92 if (windows::UTF8ToUTF16(fname, fnameUnicode))
93 return INVALID_HANDLE_VALUE;
94
95 h = CreateFileW(fnameUnicode.data(), fd ? GENERIC_WRITE : GENERIC_READ,
96 FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
97 FILE_ATTRIBUTE_NORMAL, NULL);
94 h = CreateFile(fname.c_str(), fd ? GENERIC_WRITE : GENERIC_READ,
95 FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
96 FILE_ATTRIBUTE_NORMAL, NULL);
9897 if (h == INVALID_HANDLE_VALUE) {
9998 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
10099 (fd ? "input: " : "output: "));
218217 *p = 0;
219218
220219 // The pointer to the environment block for the new process.
221 std::vector EnvBlock;
220 OwningArrayPtr envblock;
222221
223222 if (envp) {
224223 // An environment block consists of a null-terminated block of
225224 // null-terminated strings. Convert the array of environment variables to
226225 // an environment block by concatenating them.
227 for (unsigned i = 0; envp[i]; ++i) {
228 SmallVector EnvString;
229 if (error_code ec = windows::UTF8ToUTF16(envp[i], EnvString)) {
230 SetLastError(ec.value());
231 MakeErrMsg(ErrMsg, "Unable to convert environment variable to UTF-16");
232 return false;
233 }
234
235 EnvBlock.insert(EnvBlock.end(), EnvString.begin(), EnvString.end());
236 EnvBlock.push_back(0);
237 }
238 EnvBlock.push_back(0);
226
227 // First, determine the length of the environment block.
228 len = 0;
229 for (unsigned i = 0; envp[i]; i++)
230 len += strlen(envp[i]) + 1;
231
232 // Now build the environment block.
233 envblock.reset(new char[len+1]);
234 p = envblock.get();
235
236 for (unsigned i = 0; envp[i]; i++) {
237 const char *ev = envp[i];
238 size_t len = strlen(ev) + 1;
239 memcpy(p, ev, len);
240 p += len;
241 }
242
243 *p = 0;
239244 }
240245
241246 // Create a child process.
242 STARTUPINFOW si;
247 STARTUPINFO si;
243248 memset(&si, 0, sizeof(si));
244249 si.cb = sizeof(si);
245250 si.hStdInput = INVALID_HANDLE_VALUE;
263268 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
264269 // If stdout and stderr should go to the same place, redirect stderr
265270 // to the handle already open for stdout.
266 if (!DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
267 GetCurrentProcess(), &si.hStdError,
268 0, TRUE, DUPLICATE_SAME_ACCESS)) {
269 CloseHandle(si.hStdInput);
270 CloseHandle(si.hStdOutput);
271 MakeErrMsg(ErrMsg, "can't dup stderr to stdout");
272 return false;
273 }
271 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
272 GetCurrentProcess(), &si.hStdError,
273 0, TRUE, DUPLICATE_SAME_ACCESS);
274274 } else {
275275 // Just redirect stderr
276276 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
288288
289289 fflush(stdout);
290290 fflush(stderr);
291
292 SmallVector ProgramUtf16;
293 if (error_code ec = windows::UTF8ToUTF16(Program, ProgramUtf16)) {
294 SetLastError(ec.value());
295 MakeErrMsg(ErrMsg,
296 std::string("Unable to convert application name to UTF-16"));
297 return false;
298 }
299
300 SmallVector CommandUtf16;
301 if (error_code ec = windows::UTF8ToUTF16(command.get(), CommandUtf16)) {
302 SetLastError(ec.value());
303 MakeErrMsg(ErrMsg,
304 std::string("Unable to convert command-line to UTF-16"));
305 return false;
306 }
307
308 BOOL rc = CreateProcessW(ProgramUtf16.data(), CommandUtf16.data(), 0, 0,
309 TRUE, CREATE_UNICODE_ENVIRONMENT,
310 EnvBlock.empty() ? 0 : EnvBlock.data(), 0, &si,
311 &pi);
291 std::string ProgramStr = Program;
292 BOOL rc = CreateProcess(ProgramStr.c_str(), command.get(), NULL, NULL, TRUE,
293 0, envblock.get(), NULL, &si, &pi);
312294 DWORD err = GetLastError();
313295
314296 // Regardless of whether the process got created or not, we are done with
321303 if (!rc) {
322304 SetLastError(err);
323305 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
324 Program.str() + "'");
306 ProgramStr + "'");
325307 return false;
326308 }
327309
134134 static fpSymFunctionTableAccess64 SymFunctionTableAccess64;
135135
136136 static bool load64BitDebugHelp(void) {
137 HMODULE hLib = ::LoadLibrary(TEXT("Dbghelp.dll"));
137 HMODULE hLib = ::LoadLibrary("Dbghelp.dll");
138138 if (hLib) {
139139 StackWalk64 = (fpStackWalk64)
140140 ::GetProcAddress(hLib, "StackWalk64");
3030 #include "llvm/Support/system_error.h"
3131 #include
3232 #include
33 #include
3334 #include
3435 #include
3536 #include
10861086 if test "$llvm_cv_os_type" = "MingW" ; then
10871087 AC_CHECK_LIB(imagehlp, main)
10881088 AC_CHECK_LIB(psapi, main)
1089 AC_CHECK_LIB(shell32, main)
10901089 fi
10911090
10921091 dnl dlopen() is required for plugin support.
33
44 target_link_libraries(FileCheck LLVMSupport)
55 if( MINGW )
6 target_link_libraries(FileCheck imagehlp psapi shell32)
6 target_link_libraries(FileCheck imagehlp psapi)
77 endif( MINGW )
88 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
99 target_link_libraries(FileCheck pthread)
33
44 target_link_libraries(FileUpdate LLVMSupport)
55 if( MINGW )
6 target_link_libraries(FileUpdate imagehlp psapi shell32)
6 target_link_libraries(FileUpdate imagehlp psapi)
77 endif( MINGW )
88 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
99 target_link_libraries(FileUpdate pthread)
33
44 target_link_libraries(not LLVMSupport)
55 if( MINGW )
6 target_link_libraries(not imagehlp psapi shell32)
6 target_link_libraries(not imagehlp psapi)
77 endif( MINGW )
88 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
99 target_link_libraries(not pthread)