llvm.org GIT mirror llvm / 5a1a185
Windows: Add support for unicode command lines Summary: The MSVCRT deliberately sends main() code-page specific characters. This isn't too useful to LLVM as we end up converting the arguments to UTF-16 and subsequently attempt to use the result as, for example, a file name. Instead, we need to have the ability to access the Unicode command line and transform it to UTF-8. This has the distinct advantage over using the MSVC-specific wmain() function as our entry point because: - It doesn't work on cygwin. - It only work on MinGW with caveats and only then on certain versions. - We get to keep our entry point as main(). :) N.B. This patch includes fixes to other parts of lib/Support/Windows s.t. we would be able to take advantage of getting the Unicode paths. E.G. clang spawning clang -cc1 would want to give it Unicode arguments. Reviewers: aaron.ballman, Bigcheese, rnk, ruiu Reviewed By: rnk CC: llvm-commits, ygao Differential Revision: http://llvm-reviews.chandlerc.com/D1834 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@192069 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 5 years ago
18 changed file(s) with 187 addition(s) and 94 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)
14011402 fi
14021403
14031404 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)
417418 # TODO: Check existence of libraries.
418419 # include(CheckLibraryExists)
419420 # 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)
4 set(system_libs ${system_libs} imagehlp psapi shell32)
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)
130 target_link_libraries(${target} imagehlp psapi shell32)
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
205208 /* Define to 1 if you have the `udis86' library (-ludis86). */
206209 #undef HAVE_LIBUDIS86
207210
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
219222 /* Define to 1 if you have the `udis86' library (-ludis86). */
220223 #undef HAVE_LIBUDIS86
221224
2424 #ifndef LLVM_SUPPORT_PROCESS_H
2525 #define LLVM_SUPPORT_PROCESS_H
2626
27 #include "llvm/ADT/ArrayRef.h"
2728 #include "llvm/ADT/Optional.h"
2829 #include "llvm/Config/llvm-config.h"
30 #include "llvm/Support/Allocator.h"
31 #include "llvm/Support/system_error.h"
2932 #include "llvm/Support/DataTypes.h"
3033 #include "llvm/Support/TimeValue.h"
3134
167170 // string. \arg Name is assumed to be in UTF-8 encoding too.
168171 static Optional GetEnv(StringRef name);
169172
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
170181 /// This function determines if the standard input is connected directly
171182 /// to a user's input (keyboard probably), rather than coming from a file
172183 /// 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
192200 bool Process::StandardInIsUserInput() {
193201 return FileDescriptorIsDisplayed(STDIN_FILENO);
194202 }
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 }
8592
86 HMODULE a_handle = LoadLibrary(filename);
93 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
8794
8895 if (a_handle == 0) {
8996 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("0123456789abcdef"[val & 15]);
130 random_path_utf16.push_back(L"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 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()) {
243 SmallVector cur_path;
244 DWORD len = MAX_PATH;
245
246 do {
255247 cur_path.reserve(len);
256 goto retry_cur_dir;
257 }
258
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.
259260 cur_path.set_size(len);
260261 return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
261262 }
99 // This file provides the Win32 specific implementation of the Process class.
1010 //
1111 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/Allocator.h"
1214
1315 #include "Windows.h"
1416 #include
1517 #include
1618 #include
1719 #include
20 #include
1821
1922 #ifdef __MINGW32__
2023 #if (HAVE_LIBPSAPI != 1)
2124 #error "libpsapi.a should be present"
2225 #endif
26 #if (HAVE_LIBSHELL32 != 1)
27 #error "libshell32.a should be present"
28 #endif
2329 #else
24 #pragma comment(lib, "psapi.lib")
30 #pragma comment(lib, "psapi.lib")
31 #pragma comment(lib, "Shell32.lib")
2532 #endif
2633
2734 //===----------------------------------------------------------------------===//
150157 // Environment variable can be encoded in non-UTF8 encoding, and there's no
151158 // way to know what the encoding is. The only reliable way to look up
152159 // multibyte environment variable is to use GetEnvironmentVariableW().
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;
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
159168 // Try again with larger buffer.
160 Buf.resize(Size + 1);
161 }
162 if (Size == 0)
163 return None;
169 } while (Size > Buf.capacity());
170 Buf.set_size(Size);
164171
165172 // Convert the result from UTF-16 to UTF-8.
166 SmallVector128> Res;
173 SmallVectorMAX_PATH> Res;
167174 if (error_code ec = windows::UTF16ToUTF8(&Buf[0], Size, Res))
168175 return None;
169176 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();
170210 }
171211
172212 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 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)
44 SmallVector progNameUnicode;
45 if (windows::UTF8ToUTF16(progName, progNameUnicode))
5246 return "";
5347
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)
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)
6657 return "";
67 else if (len2 <= len)
68 return buffer;
69
70 len = len2;
71 }
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());
7268 }
7369
7470 static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
7571 HANDLE h;
7672 if (path == 0) {
77 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
78 GetCurrentProcess(), &h,
79 0, TRUE, DUPLICATE_SAME_ACCESS);
73 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
74 GetCurrentProcess(), &h,
75 0, TRUE, DUPLICATE_SAME_ACCESS))
76 return INVALID_HANDLE_VALUE;
8077 return h;
8178 }
8279
9188 sa.lpSecurityDescriptor = 0;
9289 sa.bInheritHandle = TRUE;
9390
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);
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);
9798 if (h == INVALID_HANDLE_VALUE) {
9899 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
99100 (fd ? "input: " : "output: "));
217218 *p = 0;
218219
219220 // The pointer to the environment block for the new process.
220 OwningArrayPtr envblock;
221 std::vector EnvBlock;
221222
222223 if (envp) {
223224 // An environment block consists of a null-terminated block of
224225 // null-terminated strings. Convert the array of environment variables to
225226 // an environment block by concatenating them.
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;
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);
244239 }
245240
246241 // Create a child process.
247 STARTUPINFO si;
242 STARTUPINFOW si;
248243 memset(&si, 0, sizeof(si));
249244 si.cb = sizeof(si);
250245 si.hStdInput = INVALID_HANDLE_VALUE;
268263 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
269264 // If stdout and stderr should go to the same place, redirect stderr
270265 // to the handle already open for stdout.
271 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
272 GetCurrentProcess(), &si.hStdError,
273 0, TRUE, DUPLICATE_SAME_ACCESS);
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 }
274274 } else {
275275 // Just redirect stderr
276276 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
288288
289289 fflush(stdout);
290290 fflush(stderr);
291 std::string ProgramStr = Program;
292 BOOL rc = CreateProcess(ProgramStr.c_str(), command.get(), NULL, NULL, TRUE,
293 0, envblock.get(), NULL, &si, &pi);
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);
294312 DWORD err = GetLastError();
295313
296314 // Regardless of whether the process got created or not, we are done with
303321 if (!rc) {
304322 SetLastError(err);
305323 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
306 ProgramStr + "'");
324 Program.str() + "'");
307325 return false;
308326 }
309327
134134 static fpSymFunctionTableAccess64 SymFunctionTableAccess64;
135135
136136 static bool load64BitDebugHelp(void) {
137 HMODULE hLib = ::LoadLibrary("Dbghelp.dll");
137 HMODULE hLib = ::LoadLibrary(TEXT("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
3433 #include
3534 #include
3635 #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)
10891090 fi
10901091
10911092 dnl dlopen() is required for plugin support.
33
44 target_link_libraries(FileCheck LLVMSupport)
55 if( MINGW )
6 target_link_libraries(FileCheck imagehlp psapi)
6 target_link_libraries(FileCheck imagehlp psapi shell32)
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)
6 target_link_libraries(FileUpdate imagehlp psapi shell32)
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)
6 target_link_libraries(not imagehlp psapi shell32)
77 endif( MINGW )
88 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
99 target_link_libraries(not pthread)