[compiler-rt][profile] Accept Unicode profile names on Windows#202335
Open
woodruffw wants to merge 1 commit into
Open
[compiler-rt][profile] Accept Unicode profile names on Windows#202335woodruffw wants to merge 1 commit into
woodruffw wants to merge 1 commit into
Conversation
|
@llvm/pr-subscribers-pgo Author: William Woodruff (woodruffw) ChangesThis addresses a small FIXME: when opening a profile file on Windows, we now use This fixes a relatively niche scenario in which the profile file contains Unicode codepoints that I've also added a small test for Full diff: https://github.com/llvm/llvm-project/pull/202335.diff 2 Files Affected:
diff --git a/compiler-rt/lib/profile/InstrProfilingUtil.c b/compiler-rt/lib/profile/InstrProfilingUtil.c
index a9d9df813764b..21005bc5ff87f 100644
--- a/compiler-rt/lib/profile/InstrProfilingUtil.c
+++ b/compiler-rt/lib/profile/InstrProfilingUtil.c
@@ -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;
diff --git a/compiler-rt/test/profile/Windows/instrprof-file-ex-unicode.c b/compiler-rt/test/profile/Windows/instrprof-file-ex-unicode.c
new file mode 100644
index 0000000000000..ebd3b3bc94488
--- /dev/null
+++ b/compiler-rt/test/profile/Windows/instrprof-file-ex-unicode.c
@@ -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;
+}
|
woodruffw
commented
Jun 8, 2026
| fputs("profile data", File); | ||
| fclose(File); | ||
|
|
||
| return GetFileAttributesW(L"profile-\u65e5.dump") == INVALID_FILE_ATTRIBUTES; |
Contributor
Author
There was a problem hiding this comment.
NB: This tests that the file is reachable by its UTF-16 filename, after being opened/created as UTF-8.
96ab6d4 to
171eb7b
Compare
This addresses a small FIXME: when opening a profile file on Windows, we now use `CreateFileW` instead of `CreateFileA`. This fixes a relatively niche scenario in which the profile file contains Unicode codepoints that `CreateFileA` can't handle. I've also added a small test for `lprofOpenFileEx` that exercises the change. Signed-off-by: William Woodruff <william@yossarian.net>
171eb7b to
72038b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This addresses a small FIXME: when opening a profile file on Windows, we now use
CreateFileWinstead ofCreateFileA.This fixes a relatively niche scenario in which the profile file contains Unicode codepoints that
CreateFileAcan't handle.I've also added a small test for
lprofOpenFileExthat exercises the change.