Skip to content

Commit a28fac8

Browse files
authored
Merge pull request #49937 from Sanne/JvmChecks
Check for --sun-misc-unsafe-memory-access=allow, log a user friendly warning when not
2 parents 095462d + 22d9fc8 commit a28fac8

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.quarkus.deployment.steps;
2+
3+
import io.quarkus.deployment.annotations.BuildStep;
4+
import io.quarkus.deployment.annotations.ExecutionTime;
5+
import io.quarkus.deployment.annotations.Record;
6+
import io.quarkus.deployment.pkg.steps.NativeBuild;
7+
import io.quarkus.runtime.JVMChecksRecorder;
8+
9+
public class CheckJVMparamsProcessor {
10+
11+
@BuildStep(onlyIfNot = NativeBuild.class)
12+
@Record(ExecutionTime.RUNTIME_INIT)
13+
public void recordJvmChecks(JVMChecksRecorder recorder) {
14+
recorder.check();
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.quarkus.runtime;
2+
3+
import java.lang.management.ManagementFactory;
4+
import java.lang.management.RuntimeMXBean;
5+
import java.util.List;
6+
7+
import org.jboss.logging.Logger;
8+
9+
import io.quarkus.runtime.annotations.Recorder;
10+
11+
@Recorder
12+
public class JVMChecksRecorder {
13+
14+
public void check() {
15+
if (!isUnsafeMemoryAccessAllowed()) {
16+
Logger.getLogger("JVM").warn(
17+
"Unsafe memory access is not going to be allowed in future versions of the JVM. Since Java 24, the JVM will print a warning on boot (most likely shown above), but several Quarkus extensions still require it. "
18+
+
19+
"There is currently no need to worry: please add the `--sun-misc-unsafe-memory-access=allow` JVM argument to avoid these warnings. "
20+
+
21+
"We are working with the maintainers of those libraries to get this resolved in future versions; when this is done, we will remove the need for this argument.");
22+
}
23+
}
24+
25+
public static boolean isUnsafeMemoryAccessAllowed() {
26+
if (Runtime.version().feature() < 24) {
27+
//Versions before Java 24 would not complain about the use of Unsafe.
28+
//Also, setting `--sun-misc-unsafe-memory-access=allow` isn't possible (not a valid argument) before Java 24.
29+
return true;
30+
}
31+
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
32+
List<String> arguments = runtimeMxBean.getInputArguments();
33+
return arguments.contains("--sun-misc-unsafe-memory-access=allow");
34+
}
35+
36+
}

0 commit comments

Comments
 (0)