Skip to content

Commit 6659953

Browse files
authored
Merge pull request #49979 from Sanne/DisableUnsafeWarnings
Fully disable the nagging about the unsafe usage of Unsafe
2 parents b9428a3 + 4ba35c0 commit 6659953

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

core/deployment/src/main/java/io/quarkus/deployment/steps/CheckJVMparamsProcessor.java renamed to core/deployment/src/main/java/io/quarkus/deployment/steps/JVMUnsafeWarningsProcessor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import io.quarkus.deployment.pkg.steps.NativeBuild;
77
import io.quarkus.runtime.JVMChecksRecorder;
88

9-
public class CheckJVMparamsProcessor {
9+
public class JVMUnsafeWarningsProcessor {
1010

1111
@BuildStep(onlyIfNot = NativeBuild.class)
12-
@Record(ExecutionTime.RUNTIME_INIT)
13-
public void recordJvmChecks(JVMChecksRecorder recorder) {
14-
recorder.check();
12+
@Record(ExecutionTime.STATIC_INIT) //We need this to trigger before Netty or other Unsafe users get to run: static init seems effective enough
13+
public void disableUnsafeRelatedWarnings(JVMChecksRecorder recorder) {
14+
recorder.disableUnsafeRelatedWarnings();
1515
}
16+
1617
}
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
package io.quarkus.runtime;
22

3-
import java.lang.management.ManagementFactory;
4-
import java.lang.management.RuntimeMXBean;
5-
import java.util.List;
6-
7-
import org.jboss.logging.Logger;
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
85

96
import io.quarkus.runtime.annotations.Recorder;
107

118
@Recorder
129
public class JVMChecksRecorder {
1310

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() {
11+
/**
12+
* This is a horrible hack to disable the Unsafe-related warnings that are printed on startup:
13+
* we know about the problem, we're working on it, and there's no need to print a warning scaring our
14+
* users with it.
15+
*/
16+
public void disableUnsafeRelatedWarnings() {
2617
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;
18+
return;
19+
}
20+
try {
21+
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
22+
Method trySetMemoryAccessWarnedMethod = unsafeClass.getDeclaredMethod("trySetMemoryAccessWarned");
23+
trySetMemoryAccessWarnedMethod.setAccessible(true);
24+
trySetMemoryAccessWarnedMethod.invoke(null);
25+
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
26+
//let's ignore it - if we failed with our horrible hack, worst that could happen is that the ugly warning is printed
3027
}
31-
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
32-
List<String> arguments = runtimeMxBean.getInputArguments();
33-
return arguments.contains("--sun-misc-unsafe-memory-access=allow");
3428
}
35-
3629
}

0 commit comments

Comments
 (0)