1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 19:12:56 +02:00

Windows: Be more explicit with Win32 APIs

This addresses several issues in a similar vein:
 - Use the Unicode APIs when possible, running nm on clang shows that we
   only use Unicode APIs except for FormatMessage, CreateSemaphore, and
   GetModuleHandle.  AFAICT, the latter two are coming from MinGW and
   not LLVM itself.
 - Make getMainExecutable more resilient.  It previously considered
   return values of zero from ::GetModuleFileNameA to be acceptable.

llvm-svn: 192096
This commit is contained in:
David Majnemer 2013-10-07 09:52:36 +00:00
parent 724835dff7
commit 14815ac119
4 changed files with 33 additions and 15 deletions

View File

@ -46,9 +46,9 @@ namespace {
/*__in*/ LPCWSTR lpTargetFileName, /*__in*/ LPCWSTR lpTargetFileName,
/*__in*/ DWORD dwFlags); /*__in*/ DWORD dwFlags);
PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW( PtrCreateSymbolicLinkW create_symbolic_link_api =
::GetProcAddress(::GetModuleHandleA("kernel32.dll"), PtrCreateSymbolicLinkW(::GetProcAddress(
"CreateSymbolicLinkW")); ::GetModuleHandleW(L"Kernel32.dll"), "CreateSymbolicLinkW"));
error_code TempDir(SmallVectorImpl<wchar_t> &result) { error_code TempDir(SmallVectorImpl<wchar_t> &result) {
retry_temp_dir: retry_temp_dir:
@ -216,9 +216,28 @@ namespace sys {
namespace fs { namespace fs {
std::string getMainExecutable(const char *argv0, void *MainExecAddr) { std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
char pathname[MAX_PATH]; SmallVector<wchar_t, MAX_PATH> PathName;
DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH); DWORD Size = ::GetModuleFileNameW(NULL, PathName.data(), PathName.capacity());
return ret != MAX_PATH ? pathname : "";
// A zero return value indicates a failure other than insufficient space.
if (Size == 0)
return "";
// Insufficient space is determined by a return value equal to the size of
// the buffer passed in.
if (Size == PathName.capacity())
return "";
// On success, GetModuleFileNameW returns the number of characters written to
// the buffer not including the NULL terminator.
PathName.set_size(Size);
// Convert the result from UTF-16 to UTF-8.
SmallVector<char, MAX_PATH> PathNameUTF8;
if (UTF16ToUTF8(PathName.data(), PathName.size(), PathNameUTF8))
return "";
return std::string(PathNameUTF8.data());
} }
UniqueID file_status::getUniqueID() const { UniqueID file_status::getUniqueID() const {
@ -672,12 +691,11 @@ error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
case priv: flprotect = PAGE_WRITECOPY; break; case priv: flprotect = PAGE_WRITECOPY; break;
} }
FileMappingHandle = ::CreateFileMapping(FileHandle, FileMappingHandle =
0, ::CreateFileMappingW(FileHandle, 0, flprotect,
flprotect, (Offset + Size) >> 32,
(Offset + Size) >> 32, (Offset + Size) & 0xffffffff,
(Offset + Size) & 0xffffffff, 0);
0);
if (FileMappingHandle == NULL) { if (FileMappingHandle == NULL) {
error_code ec = windows_error(GetLastError()); error_code ec = windows_error(GetLastError());
if (FileDescriptor) { if (FileDescriptor) {

View File

@ -335,7 +335,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
// Assign the process to a job if a memory limit is defined. // Assign the process to a job if a memory limit is defined.
ScopedJobHandle hJob; ScopedJobHandle hJob;
if (memoryLimit != 0) { if (memoryLimit != 0) {
hJob = CreateJobObject(0, 0); hJob = CreateJobObjectW(0, 0);
bool success = false; bool success = false;
if (hJob) { if (hJob) {
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli; JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;

View File

@ -48,7 +48,7 @@ static bool loadSRW() {
if (!sChecked) { if (!sChecked) {
sChecked = true; sChecked = true;
HMODULE hLib = ::LoadLibrary(TEXT("Kernel32")); HMODULE hLib = ::LoadLibraryW(L"Kernel32.dll");
if (hLib) { if (hLib) {
fpInitializeSRWLock = fpInitializeSRWLock =
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib, (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,

View File

@ -135,7 +135,7 @@ typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
static fpSymFunctionTableAccess64 SymFunctionTableAccess64; static fpSymFunctionTableAccess64 SymFunctionTableAccess64;
static bool load64BitDebugHelp(void) { static bool load64BitDebugHelp(void) {
HMODULE hLib = ::LoadLibrary(TEXT("Dbghelp.dll")); HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
if (hLib) { if (hLib) {
StackWalk64 = (fpStackWalk64) StackWalk64 = (fpStackWalk64)
::GetProcAddress(hLib, "StackWalk64"); ::GetProcAddress(hLib, "StackWalk64");