8342380: Implement JEP 498: Warn upon Use of Memory-Access Methods in sun.misc.Unsafe

Reviewed-by: vklang, jpai
This commit is contained in:
Alan Bateman 2024-11-26 12:28:55 +00:00
parent fc2da15bce
commit f0b72f728d
4 changed files with 24 additions and 19 deletions

View File

@ -218,7 +218,7 @@ java.launcher.X.usage=\n\
\ --sun-misc-unsafe-memory-access=<value>\n\
\ allow or deny usage of unsupported API sun.misc.Unsafe\n\
\ <value> 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

View File

@ -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

View File

@ -1846,7 +1846,7 @@ public final class Unsafe {
DENY;
private static MemoryAccessOption defaultValue() {
return ALLOW;
return WARN;
}
/**

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8331670
* @bug 8331670 8338383
* @summary Basic test for --sun-misc-unsafe-memory-access=<value>
* @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);
}
}