Skip to content

Commit 7bb8d2b

Browse files
fmeumcopybara-github
authored andcommitted
Force the JVM to use UTF-8 on Windows
This change patches the app manifest of the `java.exe` launcher in the embedded JDK to always use the UTF-8 codepage on Windows 1903 and later. This is necessary because the launcher sets sun.jnu.encoding to the system code page, which by default is a legacy code page such as Cp1252 on Windows. This causes the JVM to be unable to interact with files whose paths contain Unicode characters not representable in the system code page, as well as command-line arguments and environment variables containing such characters. The Windows VMs in CI are not running Windows 1903 or later yet, so this change can currently only be tested locally by running `bazel info character-encoding` and verifying that it prints `sun.jnu.encoding = UTF-8`. Work towards #374 Work towards #18293 Work towards #23859 Closes #24172. PiperOrigin-RevId: 693466466 Change-Id: I4914c21e846493a8880ac8c6f5e1afa9fae87366
1 parent 47e5502 commit 7bb8d2b

File tree

4 files changed

+145
-5
lines changed

4 files changed

+145
-5
lines changed

src/BUILD

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,32 +179,57 @@ filegroup(
179179
visibility = ["//src/test/shell/bazel:__pkg__"],
180180
)
181181

182+
# This program patches the app manifest of the java.exe launcher to force its
183+
# active code page to UTF-8 on Windows 1903 and later, which is required for
184+
# proper support of Unicode characters outside the system code page.
185+
# The JDK currently (as of JDK 23) doesn't support this natively:
186+
# https://mail.openjdk.org/pipermail/core-libs-dev/2024-November/133773.html
187+
cc_binary(
188+
name = "patch_java_manifest_for_utf8",
189+
srcs = ["patch_java_manifest_for_utf8.cc"],
190+
tags = ["manual"],
191+
target_compatible_with = ["@platforms//os:windows"],
192+
)
193+
182194
sh_binary(
183195
name = "minimize_jdk",
184196
srcs = ["minimize_jdk.sh"],
197+
data = select({
198+
"@platforms//os:windows": [
199+
":patch_java_manifest_for_utf8",
200+
],
201+
"//conditions:default": [],
202+
}),
203+
deps = [
204+
"@bazel_tools//tools/bash/runfiles",
205+
],
185206
)
186207

187208
genrule(
188209
name = "embedded_jdk_minimal",
189210
srcs = [
190211
":embedded_jdk_vanilla",
191-
":minimize_jdk.sh",
192212
":jdeps_modules.golden",
193213
],
194214
outs = ["minimal_jdk.zip"],
195-
cmd = "$(location :minimize_jdk.sh) $(location :embedded_jdk_vanilla) $(location :jdeps_modules.golden) $(OUTS)",
215+
cmd = "$(location :minimize_jdk) $(location :embedded_jdk_vanilla) $(location :jdeps_modules.golden) $(OUTS)",
216+
tools = [
217+
":minimize_jdk",
218+
],
196219
visibility = ["//src/test/shell/bazel:__pkg__"],
197220
)
198221

199222
genrule(
200223
name = "embedded_jdk_allmodules",
201224
srcs = [
202225
":embedded_jdk_vanilla",
203-
":minimize_jdk.sh",
204226
":jdeps_modules.golden",
205227
],
206228
outs = ["allmodules_jdk.zip"],
207-
cmd = "$(location :minimize_jdk.sh) --allmodules $(location :embedded_jdk_vanilla) $(location :jdeps_modules.golden) $(OUTS)",
229+
cmd = "$(location :minimize_jdk) --allmodules $(location :embedded_jdk_vanilla) $(location :jdeps_modules.golden) $(OUTS)",
230+
tools = [
231+
":minimize_jdk",
232+
],
208233
visibility = ["//src/test/shell/bazel:__pkg__"],
209234
)
210235

src/minimize_jdk.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@
1717
# This script creates from the full JDK a minimized version that only contains
1818
# the specified JDK modules.
1919

20-
set -euo pipefail
20+
# --- begin runfiles.bash initialization v3 ---
21+
# Copy-pasted from the Bazel Bash runfiles library v3.
22+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
23+
# shellcheck disable=SC1090
24+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
25+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
26+
source "$0.runfiles/$f" 2>/dev/null || \
27+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
28+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
29+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
30+
# --- end runfiles.bash initialization v3 ---
2131

2232
if [ "$1" == "--allmodules" ]; then
2333
shift
@@ -54,6 +64,7 @@ if [[ "$UNAME" =~ msys_nt* ]]; then
5464
./bin/jlink --module-path ./jmods/ --add-modules "$modules" \
5565
--vm=server --strip-debug --no-man-pages \
5666
--output reduced
67+
"$(rlocation "io_bazel/src/patch_java_manifest_for_utf8.exe")" reduced/bin/java.exe
5768
cp $DOCS legal/java.base/ASSEMBLY_EXCEPTION \
5869
reduced/
5970
# These are necessary for --host_jvm_debug to work.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2024 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
18+
#define WIN32_LEAN_AND_MEAN
19+
#include <windows.h>
20+
21+
#include <string>
22+
23+
// This program patches the app manifest of the java.exe launcher to force its
24+
// active code page to UTF-8 on Windows 1903 and later.
25+
// https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page#set-a-process-code-page-to-utf-8
26+
//
27+
// This is necessary because the launcher sets sun.jnu.encoding to the system
28+
// code page, which by default is a legacy code page such as Cp1252 on Windows.
29+
// This causes the JVM to be unable to interact with files whose paths contain
30+
// Unicode characters not representable in the system code page, as well as
31+
// command-line arguments and environment variables containing such characters.
32+
//
33+
// Usage in the libjava.dll code:
34+
// https://github.com/openjdk/jdk/blob/e7f0bf11ff0e89b6b156d5e88ca3771c706aa46a/src/java.base/windows/native/libjava/java_props_md.c#L63-L65
35+
int wmain(int argc, wchar_t *argv[]) {
36+
if (argc != 2) {
37+
fwprintf(stderr, L"Usage: %ls <filename>\n", argv[0]);
38+
return 1;
39+
}
40+
41+
// Read the app manifest (aka side-by-side or fusion manifest) from the
42+
// executable, which requires loading it as a "module".
43+
HMODULE exe = LoadLibraryExW(argv[1], nullptr, LOAD_LIBRARY_AS_DATAFILE);
44+
if (!exe) {
45+
fwprintf(stderr, L"Error loading file %ls: %d\n", argv[1], GetLastError());
46+
return 1;
47+
}
48+
HRSRC manifest_resource = FindResourceA(exe, MAKEINTRESOURCE(1), RT_MANIFEST);
49+
if (!manifest_resource) {
50+
fwprintf(stderr, L"Resource not found: %d\n", GetLastError());
51+
return 1;
52+
}
53+
HGLOBAL manifest_handle = LoadResource(exe, manifest_resource);
54+
if (!manifest_handle) {
55+
fwprintf(stderr, L"Error loading resource: %d\n", GetLastError());
56+
return 1;
57+
}
58+
LPVOID manifest_data = LockResource(manifest_handle);
59+
if (!manifest_data) {
60+
fwprintf(stderr, L"Error locking resource: %d\n", GetLastError());
61+
return 1;
62+
}
63+
DWORD manifest_len = SizeofResource(exe, manifest_resource);
64+
std::string manifest((char *)manifest_data, manifest_len);
65+
UnlockResource(manifest_handle);
66+
FreeResource(manifest_handle);
67+
FreeLibrary(exe);
68+
69+
// Insert the activeCodePage element into the manifest at the end of the
70+
// windowsSettings element.
71+
// https://github.com/openjdk/jdk/blob/29882bfe7b7e76446a96862cd0a5e81c7e054415/src/java.base/windows/native/launcher/java.manifest#L43
72+
std::size_t insert_pos = manifest.find("</asmv3:windowsSettings>");
73+
if (insert_pos == std::wstring::npos) {
74+
fwprintf(stderr, L"End tag not found in manifest:\n%hs", manifest.c_str());
75+
return 1;
76+
}
77+
std::string new_manifest = manifest.substr(0, insert_pos) +
78+
"<activeCodePage "
79+
"xmlns=\"http://schemas.microsoft.com/SMI/2019/"
80+
"WindowsSettings\">UTF-8</activeCodePage>" +
81+
manifest.substr(insert_pos);
82+
83+
// Write back the modified app manifest.
84+
HANDLE update_handle = BeginUpdateResourceW(argv[1], false);
85+
if (!update_handle) {
86+
fwprintf(stderr, L"Error opening file %ls for update: %d\n", argv[1],
87+
GetLastError());
88+
return 1;
89+
}
90+
if (!UpdateResourceA(update_handle, RT_MANIFEST, MAKEINTRESOURCE(1),
91+
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
92+
const_cast<char *>(new_manifest.c_str()),
93+
new_manifest.size())) {
94+
fwprintf(stderr, L"Error updating resource: %d\n", GetLastError());
95+
return 1;
96+
}
97+
if (!EndUpdateResourceW(update_handle, false)) {
98+
fwprintf(stderr, L"Error finalizing update: %d\n", GetLastError());
99+
return 1;
100+
}
101+
102+
return 0;
103+
}

src/test/shell/bazel/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ sh_test(
726726
genquery(
727727
name = "embedded_tools_deps",
728728
expression = "kind(\"cc_(binary|library)\", deps(//src:embedded_tools_jdk_allmodules_srcs))",
729+
opts = ["--notool_deps"],
729730
scope = ["//src:embedded_tools_jdk_allmodules_srcs"],
730731
)
731732

0 commit comments

Comments
 (0)