Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions compiler-rt/lib/profile/InstrProfilingUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,26 @@ COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {

f = fdopen(fd, "r+b");
#elif defined(_WIN32)
// FIXME: Use the wide variants to handle Unicode filenames.
HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE,
int WideLength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
ProfileName, -1, NULL, 0);
if (WideLength == 0)
return NULL;

WCHAR *WideProfileName =
(WCHAR *)malloc((size_t)WideLength * sizeof(*WideProfileName));
if (!WideProfileName)
return NULL;

if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, ProfileName, -1,
WideProfileName, WideLength) == 0) {
free(WideProfileName);
return NULL;
}

HANDLE h = CreateFileW(WideProfileName, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
free(WideProfileName);
if (h == INVALID_HANDLE_VALUE)
return NULL;

Expand Down
22 changes: 22 additions & 0 deletions compiler-rt/test/profile/Windows/instrprof-file-ex-unicode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_profgen -o %t.exe %s
// RUN: rm -rf %t.dir
// RUN: mkdir %t.dir
// RUN: cd %t.dir
// RUN: %run %t.exe

#include <stdio.h>
#include <windows.h>

extern FILE *lprofOpenFileEx(const char *);

int main(void) {
const char *Filename = "profile-\xe6\x97\xa5.dump";
FILE *File = lprofOpenFileEx(Filename);
if (!File)
return 1;

fputs("profile data", File);
fclose(File);

return GetFileAttributesW(L"profile-\u65e5.dump") == INVALID_FILE_ATTRIBUTES;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: This tests that the file is reachable by its UTF-16 filename, after being opened/created as UTF-8.

}