From f0b72f728d357a257074177fbea2f1ff70cf70f2 Mon Sep 17 00:00:00 2001 From: Alan Bateman Date: Tue, 26 Nov 2024 12:28:55 +0000 Subject: [PATCH] 8342380: Implement JEP 498: Warn upon Use of Memory-Access Methods in sun.misc.Unsafe Reviewed-by: vklang, jpai --- .../launcher/resources/launcher.properties | 2 +- src/java.base/share/man/java.md | 2 +- .../share/classes/sun/misc/Unsafe.java | 2 +- .../sun/misc/UnsafeMemoryAccessWarnings.java | 37 +++++++++++-------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/java.base/share/classes/sun/launcher/resources/launcher.properties b/src/java.base/share/classes/sun/launcher/resources/launcher.properties index f493dfd36b6..61ff563cb24 100644 --- a/src/java.base/share/classes/sun/launcher/resources/launcher.properties +++ b/src/java.base/share/classes/sun/launcher/resources/launcher.properties @@ -218,7 +218,7 @@ java.launcher.X.usage=\n\ \ --sun-misc-unsafe-memory-access=\n\ \ allow or deny usage of unsupported API sun.misc.Unsafe\n\ \ is one of "allow", "warn", "debug", or "deny".\n\ -\ The default value is "allow".\n\n\ +\ The default value is "warn".\n\n\ These extra options are subject to change without notice.\n # Translators please note do not translate the options themselves diff --git a/src/java.base/share/man/java.md b/src/java.base/share/man/java.md index 5f1f9d8ef45..f1f1cc27e4c 100644 --- a/src/java.base/share/man/java.md +++ b/src/java.base/share/man/java.md @@ -979,7 +979,7 @@ the Java HotSpot Virtual Machine. : Disallow use of the memory-access methods by throwing an `UnsupportedOperationException` on every usage. - The default value when the option is not specified is `allow`. + The default value when the option is not specified is `warn`. ## Extra Options for macOS diff --git a/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java b/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java index 563f2039de9..043af1fc9b7 100644 --- a/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java +++ b/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java @@ -1846,7 +1846,7 @@ public final class Unsafe { DENY; private static MemoryAccessOption defaultValue() { - return ALLOW; + return WARN; } /** diff --git a/test/jdk/sun/misc/UnsafeMemoryAccessWarnings.java b/test/jdk/sun/misc/UnsafeMemoryAccessWarnings.java index 3d4b6965828..0e71dfdc194 100644 --- a/test/jdk/sun/misc/UnsafeMemoryAccessWarnings.java +++ b/test/jdk/sun/misc/UnsafeMemoryAccessWarnings.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8331670 + * @bug 8331670 8338383 * @summary Basic test for --sun-misc-unsafe-memory-access= * @library /test/lib * @compile TryUnsafeMemoryAccess.java @@ -43,19 +43,15 @@ import jdk.test.lib.process.OutputAnalyzer; class UnsafeMemoryAccessWarnings { /** - * Test default is "allow" + * Test default is "warn" */ - @Test - void testDefault() throws Exception { - test("allocateMemory+freeMemory+objectFieldOffset+putLong+getLong+invokeCleaner") - .shouldHaveExitValue(0) - .shouldNotContain("WARNING: A terminally deprecated method in sun.misc.Unsafe has been called") - .shouldNotContain("WARNING: sun.misc.Unsafe::allocateMemory") - .shouldNotContain("WARNING: sun.misc.Unsafe::freeMemory") - .shouldNotContain("WARNING: sun.misc.Unsafe::objectFieldOffset") - .shouldNotContain("WARNING: sun.misc.Unsafe::putLong") - .shouldNotContain("WARNING: sun.misc.Unsafe::getLong") - .shouldNotContain("WARNING: sun.misc.Unsafe::invokeCleaner"); + @ParameterizedTest + @ValueSource(strings = { + "allocateMemory+freeMemory", + "objectFieldOffset+putLong+getLong" + }) + void testDefault(String input) throws Exception { + testOneWarning(input); } /** @@ -81,11 +77,19 @@ class UnsafeMemoryAccessWarnings { @ParameterizedTest @ValueSource(strings = { "allocateMemory+freeMemory", - "objectFieldOffset+putLong+getLong", - "invokeCleaner" + "objectFieldOffset+putLong+getLong" }) void testWarn(String input) throws Exception { - var output = test(input, "--sun-misc-unsafe-memory-access=warn").shouldHaveExitValue(0); + testOneWarning(input, "--sun-misc-unsafe-memory-access=warn"); + } + + /** + * Test that a warning is printed by the first memory access method only. + * @param input comma separated list of Unsafe memory access methods to execute + * @param vmopts VM options + */ + private void testOneWarning(String input, String... vmopts) throws Exception { + var output = test(input, vmopts).shouldHaveExitValue(0); // should be warning printed for the first memory access method String[] methodNames = input.split("\\+"); @@ -99,6 +103,7 @@ class UnsafeMemoryAccessWarnings { int index = 1; while (index < methodNames.length) { String methodName = methodNames[index++]; + assertNotEquals(firstMethodName, methodName); output.shouldNotContain("WARNING: sun.misc.Unsafe::" + methodName); } }