8000 Build tools: pass --sun-misc-unsafe-memory-access=allow on JDK24+ · oracle/graalpython@69de2bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 69de2bf

Browse files
committed
Build tools: pass --sun-misc-unsafe-memory-access=allow on JDK24+
1 parent 74c0157 commit 69de2bf

File tree

3 files changed

+114
-57
lines changed

3 files changed

+114
-57
lines changed

graalpython/com.oracle.graal.python.test/src/tests/standalone/test_maven_plugin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,33 @@ def test_python_resources_dir_and_external_dir_error(self):
504504
util.check_ouput("Cannot use <externalDirectory> and <resourceDirectory> at the same time", out)
505505

506506

507+
@unittest.skipUnless(util.is_maven_plugin_test_enabled, "ENABLE_MAVEN_PLUGIN_UNITTESTS is not true")
508+
def test_java_home_change(self):
509+
with util.TemporaryTestDirectory() as tmpdir:
510+
target_name = "check_home_warning_test"
511+
target_dir = os.path.join(str(tmpdir), target_name)
512+
self.generate_app(tmpdir, target_dir, target_name)
513+
514+
mvnw_cmd = util.get_mvn_wrapper(target_dir, self.env)
515+
516+
# build the app
517+
process_resources_cmd = mvnw_cmd + ["package"]
518+
out, return_code = util.run_cmd(process_resources_cmd, self.env, cwd=target_dir)
519+
util.check_ouput("BUILD SUCCESS", out)
520+
521+
# update the generated launcher, so that it looks like it's been generated with different Java path
522+
pyenvcfg = os.path.join(target_dir, 'target', 'pyvenv.cfg')
523+
if os.path.exists(pyenvcfg):
524+
util.replace_in_file(pyenvcfg, os.environ['JAVA_HOME'], '/bogus/java/home')
525+
else:
526+
util.replace_in_file(os.path.join(target_dir, 'target', 'graalpy.sh'), os.environ['JAVA_HOME'], '/bogus/java/home')
527+
528+
util.replace_in_file(os.path.join(target_dir, 'pom.xml'), 'termcolor==2.2', 'termcolor==2')
529+
process_resources_cmd = mvnw_cmd + ["package"]
530+
out, return_code = util.run_cmd(process_resources_cmd, self.env, cwd=target_dir)
531+
util.check_ouput("BUILD SUCCESS", out)
532+
533+
507534
# Creates two Java apps from given pom templates, the dependencies between them
508535
# must be configured in the pom template(s)
509536
def _prepare_multi_project(self, tmpdir, app1_pom_template_name, app2_pom_template_name):

graalpython/org.graalvm.python.embedding.tools/src/org/graalvm/python/embedding/tools/exec/GraalPyRunner.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.nio.file.Path;
5252
import java.nio.file.Paths;
5353
import java.util.ArrayList;
54+
import java.util.Arrays;
5455
import java.util.List;
5556
import java.util.Set;
5657

@@ -60,6 +61,18 @@ public class GraalPyRunner {
6061
private static final String BIN_DIR = IS_WINDOWS ? "Scripts" : "bin";
6162
private static final String EXE_SUFFIX = IS_WINDOWS ? ".exe" : "";
6263

64+
public static String[] getExtraJavaOptions() {
65+
String javaVersion = System.getProperty("java.version");
66+
try {
67+
if (Integer.parseInt(javaVersion) >= 24) {
68+
return new String[]{"--sun-misc-unsafe-memory-access=allow"};
69+
}
70+
} catch (NumberFormatException ex) {
71+
// covers also javaVersion being 'null'
72+
}
73+
return new String[0];
74+
}
75+
6376
public static void run(Set<String> classpath, BuildToolLog log, String... args) throws IOException, InterruptedException {
6477
run(String.join(File.pathSeparator, classpath), log, args);
6578
}
@@ -69,6 +82,8 @@ public static void run(String classpath, BuildToolLog log, String... args) throw
6982
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
7083
List<String> cmd = new ArrayList<>();
7184
cmd.add(java.toString());
85+
cmd.add("--enable-native-access=ALL-UNNAMED");
86+
cmd.addAll(Arrays.asList(getExtraJavaOptions()));
7287
cmd.add("-classpath");
7388
cmd.add(classpath);
7489
cmd.add("com.oracle.graal.python.shell.GraalPythonMain");

graalpython/org.graalvm.python.embedding.tools/src/org/graalvm/python/embedding/tools/vfs/VFSUtils.java

Lines changed: 72 additions & 57 deletions
< A5A2 /tr>
Original file line numberDiff line numberDiff line change
@@ -690,65 +690,80 @@ private static Path ensureLauncher(Launcher launcherArgs, BuildToolLog log) thro
690690
}
691691
}
692692

693-
private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) throws IOException {
694-
if (!Files.exists(launcherArgs.launcherPath)) {
695-
info(log, "Generating GraalPy launchers");
696-
createParentDirectories(launcherArgs.launcherPath);
697-
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
698-
String classpath = String.join(File.pathSeparator, launcherArgs.computeClassPath());
699-
if (!IS_WINDOWS) {
700-
var script = String.format("""
701-
#!/usr/bin/env bash
702-
%s --enable-native-access=ALL-UNNAMED -classpath %s %s --python.Executable="$0" "$@"
703-
""",
704-
java,
705-
String.join(File.pathSeparator, classpath),
706-
GRAALPY_MAIN_CLASS);
707-
try {
708-
Files.writeString(launcherArgs.launcherPath, script);
709-
var perms = Files.getPosixFilePermissions(launcherArgs.launcherPath);
710-
perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_EXECUTE));
711-
Files.setPosixFilePermissions(launcherArgs.launcherPath, perms);
712-
} catch (IOException e) {
713-
throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e);
714-
}
715-
} else {
716-
// on windows, generate a venv launcher that executes our mvn target
717-
var script = String.format("""
718-
import os, shutil, struct, venv
719-
from pathlib import Path
720-
vl = os.path.join(venv.__path__[0], 'scripts', 'nt', 'graalpy.exe')
721-
tl = os.path.join(r'%s')
722-
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
723-
shutil.copy(vl, tl)
724-
cmd = r'%s --enable-native-access=ALL-UNNAMED -classpath "%s" %s'
725-
pyvenvcfg = os.path.join(os.path.dirname(tl), "pyvenv.cfg")
726-
with open(pyvenvcfg, 'w', encoding='utf-8') as f:
727-
f.write('venvlauncher_command = ')
728-
f.write(cmd)
729-
""",
730-
launcherArgs.launcherPath,
731-
java,
732-
classpath,
733-
GRAALPY_MAIN_CLASS);
734-
File tmp;
735-
try {
736-
tmp = File.createTempFile("create_launcher", ".py");
737-
} catch (IOException e) {
738-
throw new IOException("failed to create tmp launcher", e);
739-
}
740-
tmp.deleteOnExit();
741-
try (var wr = new FileWriter(tmp)) {
742-
wr.write(script);
743-
} catch (IOException e) {
744-
throw new IOException(String.format("failed to write tmp launcher %s", tmp), e);
693+
private static boolean checkWinLauncherJavaPath(Path venvCfg, Path java) {
694+
try {
695+
for (String line : Files.readAllLines(venvCfg)) {
696+
if (line.trim().startsWith("venvlauncher_command = " + java)) {
697+
return true;
745698
}
699+
}
700+
} catch (IOException ignore) {
701+
}
702+
return false;
703+
}
746704

747-
try {
748-
GraalPyRunner.run(classpath, log, tmp.getAbsolutePath());
749-
} catch (InterruptedException e) {
750-
throw new IOException(String.format("failed to run Graalpy launcher"), e);
751-
}
705+
private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) throws IOException {
706+
debug(log, "Generating GraalPy launchers");
707+
createParentDirectories(launcherArgs.launcherPath);
708+
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
709+
String classpath = String.join(File.pathSeparator, launcherArgs.computeClassPath());
710+
String extraJavaOptions = String.join(" ", GraalPyRunner.getExtraJavaOptions());
711+
if (!IS_WINDOWS) {
712+
// we do not bother checking if it exists and has correct java home, since it is simple
713+
// to regenerate the launcher
714+
var script = String.format("""
715+
#!/usr/bin/env bash
716+
%s --enable-native-access=ALL-UNNAMED %s -classpath %s %s --python.Executable="$0" "$@"
717+
""",
718+
java,
719+
extraJavaOptions,
720+
String.join(File.pathSeparator, classpath),
721+
GRAALPY_MAIN_CLASS);
722+
try {
723+
Files.writeString(launcherArgs.launcherPath, script);
724+
var perms = Files.getPosixFilePermissions(launcherArgs.launcherPath);
725+
perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_EXECUTE));
726+
Files.setPosixFilePermissions(launcherArgs.launcherPath, perms);
727+
} catch (IOException e) {
728+
throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e);
729+
}
730+
} else if (!Files.exists(launcherArgs.launcherPath) || !checkWinLauncherJavaPath(launcherArgs.launcherPath.getParent().resolve("pyenv.cfg"), java)) {
731+
// on windows, generate a venv launcher that executes the java command
732+
var script = String.format("""
733+
import os, shutil, struct, venv
734+
from pathlib import Path
735+
vl = os.path.join(venv.__path__[0], 'scripts', 'nt', 'graalpy.exe')
736+
tl = os.path.join(r'%s')
737+
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
738+
shutil.copy(vl, tl)
739+
cmd = r'%s --enable-native-access=ALL-UNNAMED %s -classpath "%s" %s'
740+
pyvenvcfg = os.path.join(os.path.dirname(tl), "pyvenv.cfg")
741+
with open(pyvenvcfg, 'w', encoding='utf-8') as f:
742+
f.write('venvlauncher_command = ')
743+
f.write(cmd)
744+
""",
745+
launcherArgs.launcherPath,
746+
java,
747+
extraJavaOptions,
748+
classpath,
749+
GRAALPY_MAIN_CLASS);
750+
File tmp;
751+
try {
752+
tmp = File.createTempFile("create_launcher", ".py");
753+
} catch (IOException e) {
754+
throw new IOException("failed to create tmp launcher", e);
755+
}
756+
tmp.deleteOnExit();
757+
try (var wr = new FileWriter(tmp)) {
758+
wr.write(script);
759+
} catch (IOException e) {
760+
throw new IOException(String.format("failed to write tmp launcher %s", tmp), e);
761+
}
762+
763+
try {
764+
GraalPyRunner.run(classpath, log, tmp.getAbsolutePath());
765+
} catch (InterruptedException e) {
766+
throw new IOException("failed to run Graalpy launcher", e);
752767
}
753768
}
754769
}

0 commit comments

Comments
 (0)
0