8342380: Implement JEP 498: Warn upon Use of Memory-Access Methods in sun.misc.Unsafe
Reviewed-by: vklang, jpai
This commit is contained in:
parent
fc2da15bce
commit
f0b72f728d
@ -218,7 +218,7 @@ java.launcher.X.usage=\n\
|
|||||||
\ --sun-misc-unsafe-memory-access=<value>\n\
|
\ --sun-misc-unsafe-memory-access=<value>\n\
|
||||||
\ allow or deny usage of unsupported API sun.misc.Unsafe\n\
|
\ allow or deny usage of unsupported API sun.misc.Unsafe\n\
|
||||||
\ <value> is one of "allow", "warn", "debug", or "deny".\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
|
These extra options are subject to change without notice.\n
|
||||||
|
|
||||||
# Translators please note do not translate the options themselves
|
# Translators please note do not translate the options themselves
|
||||||
|
@ -979,7 +979,7 @@ the Java HotSpot Virtual Machine.
|
|||||||
: Disallow use of the memory-access methods by throwing an
|
: Disallow use of the memory-access methods by throwing an
|
||||||
`UnsupportedOperationException` on every usage.
|
`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
|
## Extra Options for macOS
|
||||||
|
@ -1846,7 +1846,7 @@ public final class Unsafe {
|
|||||||
DENY;
|
DENY;
|
||||||
|
|
||||||
private static MemoryAccessOption defaultValue() {
|
private static MemoryAccessOption defaultValue() {
|
||||||
return ALLOW;
|
return WARN;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8331670
|
* @bug 8331670 8338383
|
||||||
* @summary Basic test for --sun-misc-unsafe-memory-access=<value>
|
* @summary Basic test for --sun-misc-unsafe-memory-access=<value>
|
||||||
* @library /test/lib
|
* @library /test/lib
|
||||||
* @compile TryUnsafeMemoryAccess.java
|
* @compile TryUnsafeMemoryAccess.java
|
||||||
@ -43,19 +43,15 @@ import jdk.test.lib.process.OutputAnalyzer;
|
|||||||
class UnsafeMemoryAccessWarnings {
|
class UnsafeMemoryAccessWarnings {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test default is "allow"
|
* Test default is "warn"
|
||||||
*/
|
*/
|
||||||
@Test
|
@ParameterizedTest
|
||||||
void testDefault() throws Exception {
|
@ValueSource(strings = {
|
||||||
test("allocateMemory+freeMemory+objectFieldOffset+putLong+getLong+invokeCleaner")
|
"allocateMemory+freeMemory",
|
||||||
.shouldHaveExitValue(0)
|
"objectFieldOffset+putLong+getLong"
|
||||||
.shouldNotContain("WARNING: A terminally deprecated method in sun.misc.Unsafe has been called")
|
})
|
||||||
.shouldNotContain("WARNING: sun.misc.Unsafe::allocateMemory")
|
void testDefault(String input) throws Exception {
|
||||||
.shouldNotContain("WARNING: sun.misc.Unsafe::freeMemory")
|
testOneWarning(input);
|
||||||
.shouldNotContain("WARNING: sun.misc.Unsafe::objectFieldOffset")
|
|
||||||
.shouldNotContain("WARNING: sun.misc.Unsafe::putLong")
|
|
||||||
.shouldNotContain("WARNING: sun.misc.Unsafe::getLong")
|
|
||||||
.shouldNotContain("WARNING: sun.misc.Unsafe::invokeCleaner");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,11 +77,19 @@ class UnsafeMemoryAccessWarnings {
|
|||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = {
|
@ValueSource(strings = {
|
||||||
"allocateMemory+freeMemory",
|
"allocateMemory+freeMemory",
|
||||||
"objectFieldOffset+putLong+getLong",
|
"objectFieldOffset+putLong+getLong"
|
||||||
"invokeCleaner"
|
|
||||||
})
|
})
|
||||||
void testWarn(String input) throws Exception {
|
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
|
// should be warning printed for the first memory access method
|
||||||
String[] methodNames = input.split("\\+");
|
String[] methodNames = input.split("\\+");
|
||||||
@ -99,6 +103,7 @@ class UnsafeMemoryAccessWarnings {
|
|||||||
int index = 1;
|
int index = 1;
|
||||||
while (index < methodNames.length) {
|
while (index < methodNames.length) {
|
||||||
String methodName = methodNames[index++];
|
String methodName = methodNames[index++];
|
||||||
|
assertNotEquals(firstMethodName, methodName);
|
||||||
output.shouldNotContain("WARNING: sun.misc.Unsafe::" + methodName);
|
output.shouldNotContain("WARNING: sun.misc.Unsafe::" + methodName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user