Compiler warnings under Java 17
Closed this issue · 35 comments
With Java 17, jpf-core
has numerous compiler warnings:
- Deprecated API usage in
src/main
orsrc/classes
,src/peers
: This requires code changes to move to an implementation using newer APIs. - Deprecated API usage in test cases:
(a) Some tests are becoming obsolete due to this, but a rewrite against a newer API may be possible.
(b) A few tests (e.g., forThread.suspend()
) have no equivalent successor API, and we therefore keep these tests and ignore the compiler warning in such cases.
How can I get those list of deprecated APIs ? I mean, last time you showed me .
Is it same we got during running ./gradlew clean test
?
Here is the Complier Warnings (We will use this as an index) :
ekla@Eklas-MacBook-Air jpf-core % ./gradlew clean test
> Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
> Task :compileClassesJava
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:170: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
static SecurityManager securityManager;
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:172: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
public static void setSecurityManager (SecurityManager newManager) {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:176: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
public static SecurityManager getSecurityManager() {
^
3 warnings
> Task :compilePeersJava
/Users/ekla/GSOC/JPFjava17/jpf-core/src/peers/gov/nasa/jpf/vm/JPF_java_util_Random.java:110: warning: [removal] getObject(Object,long) in Unsafe has been deprecated and marked for removal
AtomicLong al = (AtomicLong) unsafe.getObject(rand, seedFieldOffset);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/peers/gov/nasa/jpf/vm/JPF_java_util_Random.java:115: warning: [removal] getObject(Object,long) in Unsafe has been deprecated and marked for removal
AtomicLong al = (AtomicLong) unsafe.getObject(rand, seedFieldOffset);
^
2 warnings
> Task :compileTestJava
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/java11/StringConcatenationTest.java:134: warning: [removal] Character(char) in Character has been deprecated and marked for removal
Character ch = new Character('@');
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:138: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer i2 = new Integer(1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:152: warning: [removal] Character(char) in Character has been deprecated and marked for removal
Character c2 = new Character('?');
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:166: warning: [removal] Byte(byte) in Byte has been deprecated and marked for removal
Byte b2 = new Byte((byte)1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:180: warning: [removal] Short(short) in Short has been deprecated and marked for removal
Short s2 = new Short((short)1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:194: warning: [removal] Long(long) in Long has been deprecated and marked for removal
Long l2 = new Long(1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:208: warning: [removal] Boolean(boolean) in Boolean has been deprecated and marked for removal
Boolean b2 = new Boolean(true);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:55: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t1.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:76: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t1.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:82: warning: [removal] resume() in Thread has been deprecated and marked for removal
t1.resume();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:122: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t2.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:161: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t3.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:184: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t3.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:191: warning: [removal] resume() in Thread has been deprecated and marked for removal
t3.resume();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:242: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t4.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:267: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t4.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:272: warning: [removal] resume() in Thread has been deprecated and marked for removal
t4.resume();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/mc/basic/AttrsTest.java:371: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer o = new Integer(v);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/util/SparseClusterArrayTest.java:185: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
return new Integer(other);
^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
19 warnings
> Task :compileModules
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:25: warning: [removal] AccessControlException in java.security has been deprecated and marked for removal
public static void checkPermission (Permission p) throws AccessControlException {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:29: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
AccessControlContext context) {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:38: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
AccessControlContext context)
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:64: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
public static AccessControlContext getContext() {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:69: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
private static AccessControlContext getStackAccessControlContext() {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:73: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
static AccessControlContext getInheritedAccessControlContext() {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:170: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
static SecurityManager securityManager;
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:172: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
public static void setSecurityManager (SecurityManager newManager) {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:176: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
public static SecurityManager getSecurityManager() {
^
9 warnings
Our first set of warnings are :
> Task :compileClassesJava
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:170: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
static SecurityManager securityManager;
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:172: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
public static void setSecurityManager (SecurityManager newManager) {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/lang/System.java:176: warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
public static SecurityManager getSecurityManager() {
^
3 warnings
SecurityManager
has been marked for removal from Java-17.
"The security manager is a class that allows applications to implement a security policy. It allows an application to determine, before performing a possibly unsafe or sensitive operation, what the operation is and whether it is being attempted in a security context that allows the operation to be performed. The application can allow or disallow the operation."
The SecurityManager class contains many methods with names that begin with the word check. These methods are called by various methods in the Java libraries before those methods perform certain potentially sensitive operations.
Deprecated, for removal: This API element is subject to removal in a future version.
The Security Manager is deprecated and subject to removal in a future release. There is no replacement for the Security Manager. See JEP 411 for discussion and alternatives.
I guess the methods we support are still needed to make our tests work?
Please think of a strategy to gauge how to deal with such deprecations.
First, try removing the methods in question: If the code still compiles and all tests pass, then the code can be removed. If deprecated code is part of unit tests, check if the functionality has been removed in Java 21; in that case, we can remove it now.
In most cases, we still need to support the functionality.
Second, check if there is a replacement. If not (as is the case for SecurityManager
), we may have to keep the code as is and live the warnings for now.
"I guess the methods we support are still needed to make our tests work?" -----> Yes. We support these methods in System.java
and it is needed to pass the Tests.
//--- security manager
static SecurityManager securityManager;
public static void setSecurityManager (SecurityManager newManager) {
securityManager = newManager;
}
public static SecurityManager getSecurityManager() {
return securityManager;
}
If we remove these codes from System.java
, there is 765/1010 test failures.
"First, try removing the methods in question: If the code still compiles and all tests pass, then the code can be removed. If deprecated code is part of unit tests, check if the functionality has been removed in Java 21; in that case, we can remove it now." ----->
SecurityManager
has been used in System.java
and String.java
as per my finding. When we remove their usage, the code gets compiled but the unit tests fails (765/1010).
SecurityManager
is not the direct part of unit tests.
"Second, check if there is a replacement. If not (as is the case for SecurityManager), we may have to keep the code as is and live the warnings for now." ----->
SecurityManager
is not removed from Java 21 and also there is no alternatives suggested yet.
When we remove the SecurityManager
from System.java
and String.java
, we get 765/1010 failed tests. In most of them we get errors like :
====================================================== error 1
gov.nasa.jpf.vm.NoUncaughtExceptionsProperty
java.lang.NoSuchMethodException: java.lang.System.getSecurityManager()Ljava/lang/SecurityManager;
at sun.security.action.GetPropertyAction.privilegedGetProperties(GetPropertyAction.java:150)
at java.lang.invoke.MethodHandleStatics.<clinit>(MethodHandleStatics.java:66)
at java.lang.invoke.VarHandle.<clinit>(VarHandle.java:2194)
====================================================== snapshot #1
thread java.lang.Thread:{id:0,name:main,status:RUNNING,priority:5,isDaemon:false,lockCount:0,suspendCount:0}
owned locks:java.lang.Class@b2,java.lang.Class@1d3
call stack:
at java.lang.invoke.MethodHandleStatics.<clinit>(MethodHandleStatics.java:66)
at java.lang.invoke.VarHandle.<clinit>(VarHandle.java:2194)
====================================================== results
error #1: gov.nasa.jpf.vm.NoUncaughtExceptionsProperty "java.lang.NoSuchMethodException: java.lang.System...."
====================================================== search finished: 23/07/24, 2:06 pm
running jpf with args: +vm.class=InvalidVMClass +pass_exceptions
[SEVERE] JPF configuration error: class not found InvalidVMClass by classloader: gov.nasa.jpf.JPFClassLoader@609e8838
at java.lang.invoke.MethodHandleStatics.<clinit>(MethodHandleStatics.java:66)
:
at GetPropertyAction.java
:
/**
* Convenience method to call <code>System.getProperties</code> without
* having to go through doPrivileged if no security manager is present.
* This is unsafe for inclusion in a public API but allowable here since
* this class is now encapsulated.
*
* Note that this method performs a privileged action, and callers of
* this method should take care to ensure that the returned properties
* are not made accessible to untrusted code since it may contain
* sensitive information.
*/
@SuppressWarnings("removal")
public static Properties privilegedGetProperties() {
if (System.getSecurityManager() == null) {
return System.getProperties();
} else {
return AccessController.doPrivileged(
new PrivilegedAction<Properties>() {
public Properties run() {
return System.getProperties();
}
}
);
}
}
From here, we can see
It might be helpful to remove SecurityManager
by just returning the properties
. But the call to SecurityManager
is from OpenJDK classes.
Whenever there are dependencies from OpenJDK library classes, we have to keep a compatible API.
In our own code, where it is possible, please add @SuppressWarnings("removal") // SecurityManager
in front of the methods in question to squelch the warnings.
Next set of Compiler Warnings :
> Task :compilePeersJava
/Users/ekla/GSOC/JPFjava17/jpf-core/src/peers/gov/nasa/jpf/vm/JPF_java_util_Random.java:110: warning: [removal] getObject(Object,long) in Unsafe has been deprecated and marked for removal
AtomicLong al = (AtomicLong) unsafe.getObject(rand, seedFieldOffset);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/peers/gov/nasa/jpf/vm/JPF_java_util_Random.java:115: warning: [removal] getObject(Object,long) in Unsafe has been deprecated and marked for removal
AtomicLong al = (AtomicLong) unsafe.getObject(rand, seedFieldOffset);
^
2 warnings
at jpf-core/src/peers/gov/nasa/jpf/vm/JPF_java_util_Random.java:115
:
private static void setNativeSeed (Random rand, long seed) {
AtomicLong al = (AtomicLong) unsafe.getObject(rand, seedFieldOffset);
al.set(seed);
}
private static long getNativeSeed (Random rand){
AtomicLong al = (AtomicLong) unsafe.getObject(rand, seedFieldOffset);
return al.longValue();
}
in Unsafe.java
(It is a model class in JPF) :
public native Object getObject(Object obj, long l);
In OpenJDK 17 :
// The following deprecated methods are used by JSR 166.
@Deprecated(since="12", forRemoval=true)
public final Object getObject(Object o, long offset) {
return getReference(o, offset);
}
Please let me know the purpose of the message "The following deprecated methods are used by JSR 166".
Below source suggest to use VarHandle#get(Obj...)
[https://github.com/openjdk/jdk/blob/master/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java#L224] I found it from Google Search :
/**
* Fetches a reference value from a given Java variable.
*
* @deprecated Use {@link VarHandle#get(Object...)} instead.
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public Object getObject(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getReference(o, offset);
}
But the problem is that `VarHandle is an abstract class and get(Obj...) is non-static method so it is difficult to find the implementing class.
But, from the OpenJDK source code and also other sources, looks like we should replace it with getReference(o, offset);
@deprecated(since="12", forRemoval=true)
public final Object getObject(Object o, long offset) {
return getReference(o, offset);
}
But we have to write implementation of native method getReference(o, offset)
in our model class Unsafe.java
.
One thing I have noticed in OpenJDK
Unsafe.java
class that every method like getObject(...)
, setObject(...)
, setObjectVolatile(...)
etc. calls corresponding titled getReference(...)
, setReference(....)
, etc .
So do you think we should copy the code of native peer implementation of getObject(...)
to getReference(...)
and then we can remove getObject(....)
[Should I explain this part in more detail ?]
Please try this (removing getObject
etc. in favor of getReference
etc.), and let's see what happens with the tests.
Yes, removing and implementing the above changes works. All tests passed. Also, Compiler Warnings disappeared for getObject(...)
. I removed all those getObject..(...)
which were marked deprecated
in OpenJDK 17.
Should I share you an update over this using my forked repo ? I do not want to break it for future Tests .
Please make a PR. We can always find the PR again and revert the changes if needed, although I doubt that this is necessary.
Next set of Compiler Warnings are :
> Task :compileTestJava
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/java11/StringConcatenationTest.java:134: warning: [removal] Character(char) in Character has been deprecated and marked for removal
Character ch = new Character('@');
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:138: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer i2 = new Integer(1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:152: warning: [removal] Character(char) in Character has been deprecated and marked for removal
Character c2 = new Character('?');
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:166: warning: [removal] Byte(byte) in Byte has been deprecated and marked for removal
Byte b2 = new Byte((byte)1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:180: warning: [removal] Short(short) in Short has been deprecated and marked for removal
Short s2 = new Short((short)1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:194: warning: [removal] Long(long) in Long has been deprecated and marked for removal
Long l2 = new Long(1);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/java/lang/BoxObjectCacheTest.java:208: warning: [removal] Boolean(boolean) in Boolean has been deprecated and marked for removal
Boolean b2 = new Boolean(true);
^
.................
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/mc/basic/AttrsTest.java:371: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer o = new Integer(v);
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/util/SparseClusterArrayTest.java:185: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
return new Integer(other);
^
As you guided, changed with recommended WrapperClass.valueOf(...)
code. All the above Warnings disappeared.
But one Test Failed :
@Test public void testInteger() {
if (verifyNoPropertyViolation()) {
int v = 42;
Verify.setLocalAttribute("v", 4200);
// explicit
// Integer o = new Integer(v);
Integer o = Integer.valueOf(v);
int j = o.intValue();
int attr = Verify.getLocalAttribute("j");
assert attr == 4200;
// semi autoboxed
j = o;
boolean b = Verify.getBoolean(); // just cause some backtracking damage
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
/** this does not work because of cached, preallocated Integer objects)
// fully autoboxed
Integer a = v;
j = a;
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
**/
}
}
Thanks for replacing the constructors, which was the right way to handle these warnings.
Which assertion fails here, and what is the unexpected value?
Here is the output :
====================================================== system under test
gov.nasa.jpf.test.mc.basic.AttrsTest.runTestMethod()
====================================================== search started: 24/07/24, 10:21 pm
[WARNING] orphan NativePeer method: jdk.internal.misc.Unsafe.getUnsafe()Lsun/misc/Unsafe;
====================================================== error 1
gov.nasa.jpf.vm.NoUncaughtExceptionsProperty
java.lang.AssertionError
at gov.nasa.jpf.test.mc.basic.AttrsTest.testInteger(AttrsTest.java:375)
at java.lang.reflect.Method.invoke(gov.nasa.jpf.vm.JPF_java_lang_reflect_Method)
at gov.nasa.jpf.util.test.TestJPF.runTestMethod(TestJPF.java:648)
First one assert attr == 4200;
fails. (There is two, but here first one fails. But second one also fails).
What is the value of attr
in that place? You can rewrite the assertion slightly to include a diagnostic message with the value of attr
.
Here I tried to debug this :
Case 1 : When we use the old constructor type
@Test public void testInteger() {
if (verifyNoPropertyViolation()) {
int v = 42;
System.out.println("int v = " + v);
Verify.setLocalAttribute("v", 4200);
// explicit
Integer o = new Integer(v);
// Integer o = Integer.valueOf(v);
int j = o.intValue();
System.out.println("int j = " + j);
int attr = Verify.getLocalAttribute("j");
System.out.println("int attr = " + attr);
assert attr == 4200;
// semi autoboxed
j = o;
boolean b = Verify.getBoolean(); // just cause some backtracking damage
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
/** this does not work because of cached, preallocated Integer objects)
// fully autoboxed
Integer a = v;
j = a;
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
**/
}
}
Result
int v = 42
int j = 42
int attr = 4200
And Test Passes
Case 2 : When we use the new recommended method WrapperClass.valueOf(...)
@Test public void testInteger() {
if (verifyNoPropertyViolation()) {
int v = 42;
System.out.println("int v = " + v);
Verify.setLocalAttribute("v", 4200);
// explicit
// Integer o = new Integer(v);
Integer o = Integer.valueOf(v);
int j = o.intValue();
System.out.println("int j = " + j);
int attr = Verify.getLocalAttribute("j");
System.out.println("int attr = " + attr);
assert attr == 4200;
// semi autoboxed
j = o;
boolean b = Verify.getBoolean(); // just cause some backtracking damage
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
/** this does not work because of cached, preallocated Integer objects)
// fully autoboxed
Integer a = v;
j = a;
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
**/
}
}
Result
int v = 42
int j = 42
int attr = 0
And the test fails
Verify.setLocalAttribute("v", 4200);
and Verify.getLocalAttribute("j");
are native
methods. I am unable to locate their peer implementation.
Here one thing strange to me in case of Old Constructor Case, how can Verify.getLocalAttribute("j");
yield 4200 ?
The native peer of Verify
is in src/main/gov/nasa/jpf/vm/JPF_gov_nasa_jpf_vm_Verify.java
. The value of j
is set by setting v first with Verify.setLocalAttribute("v", 4200);
, and that value is then assigned to o
and then to j
.
Use the assertion message to see the actual value of j
where the assertion fails.
Wrote assertFalse(....)
to get the message when Test passes.
Case [Integer o = new Integer(v);
]
@Test public void testInteger() {
if (verifyNoPropertyViolation()) {
int v = 42;
Verify.setLocalAttribute("v", 4200);
System.out.println("Verify.setLocalAttribute(\"v\", 4200)\n" +
"int v = " + v);
// explicit
Integer o = new Integer(v);
int j = o.intValue();
System.out.println("Integer o = new Integer(v);\nint j = o.intValue();\n" +
"j = " + j);
int attr = Verify.getLocalAttribute("j");
assertTrue(("FAILED : assertTrue : Message [ j = " + j + " attr = " + attr + " ]"), (attr == 4200));
assertFalse(("FAILED : assertFalse : Message [ j = " + j + " attr = " + attr + " ]"), (attr == 4200));
// semi autoboxed
j = o;
boolean b = Verify.getBoolean(); // just cause some backtracking damage
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
/** this does not work because of cached, preallocated Integer objects)
// fully autoboxed
Integer a = v;
j = a;
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
**/
}
}
Output :
Verify.setLocalAttribute("v", 4200)
int v = 42
Integer o = new Integer(v);
int j = o.intValue();
j = 42
====================================================== error 1
gov.nasa.jpf.vm.NoUncaughtExceptionsProperty
java.lang.AssertionError: FAILED : assertFalse : Message [ j = 42 attr = 4200 ]
at gov.nasa.jpf.util.test.TestJPF.fail(TestJPF.java:164)
at gov.nasa.jpf.util.test.TestJPF.assertFalse(TestJPF.java:1247)
at gov.nasa.jpf.test.mc.basic.AttrsTest.testInteger(AttrsTest.java:379)
at java.lang.reflect.Method.invoke(gov.nasa.jpf.vm.JPF_java_lang_reflect_Method)
at gov.nasa.jpf.util.test.TestJPF.runTestMethod(TestJPF.java:648)
Case [Integer o = Integer.valueOf(v);
]
@Test public void testInteger() {
if (verifyNoPropertyViolation()) {
int v = 42;
Verify.setLocalAttribute("v", 4200);
System.out.println("Verify.setLocalAttribute(\"v\", 4200)\n" +
"int v = " + v);
// explicit
Integer o = Integer.valueOf(v);
int j = o.intValue();
System.out.println("Integer o = Integer.valueOf(v);\nint j = o.intValue();\n" +
"j = " + j);
int attr = Verify.getLocalAttribute("j");
assertTrue(("FAILED : assertTrue : Message [ j = " + j + " attr = " + attr + " ]"), (attr == 4200));
assertFalse(("FAILED : assertFalse : Message [ j = " + j + " attr = " + attr + " ]"), (attr == 4200));
// semi autoboxed
j = o;
boolean b = Verify.getBoolean(); // just cause some backtracking damage
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
/** this does not work because of cached, preallocated Integer objects)
// fully autoboxed
Integer a = v;
j = a;
attr = Verify.getLocalAttribute("j");
assert attr == 4200;
**/
}
}
Output :
Verify.setLocalAttribute("v", 4200)
int v = 42
Integer o = Integer.valueOf(v);
int j = o.intValue();
j = 42
====================================================== error 1
gov.nasa.jpf.vm.NoUncaughtExceptionsProperty
java.lang.AssertionError: FAILED : assertTrue : Message [ j = 42 attr = 0 ]
at gov.nasa.jpf.util.test.TestJPF.fail(TestJPF.java:164)
at gov.nasa.jpf.util.test.TestJPF.assertTrue(TestJPF.java:1257)
at gov.nasa.jpf.test.mc.basic.AttrsTest.testInteger(AttrsTest.java:378)
at java.lang.reflect.Method.invoke(gov.nasa.jpf.vm.JPF_java_lang_reflect_Method)
at gov.nasa.jpf.util.test.TestJPF.runTestMethod(TestJPF.java:648)
Some questions :
- "The value of
j
is set by settingv
first withVerify.setLocalAttribute("v", 4200);
" -----> Then why the value ofj
orv
does not reflect as 4200 ?
My guess is that attributes are not propagated when handling the method call Integer.valueOf(v), contrary to the constructor where it seems to work properly.
There is a native peer for valueOf in the class JPF_java_lang_Integer, which seemingly does not propagate the local attributes. We can try to fix it there, but I would have to look up the exact code and put it here (if it would be possible at all).
The values and local attributes are two different entities. A variable has its value, 42 in the case of variables v and j here, and then it can have some attributes in addition to the value. But the attributes do not have direct influence on the value.
See https://github.com/javapathfinder/jpf-core/wiki/Slot-and-field-attributes for the general overview.
You could try something like this
int v = env.valueOfInteger(val);
env.setReturnAttribute(new Integer(v))
return v;
in the native peer for Integer.valueOf, that means in the method valueOf__I__Ljava_lang_Integer_2 of the class JPF_java_lang_Integer.
But right now I have not found a way how to get attributes for method call arguments in the native peer. The needed API is likely missing in the MJIEnv class. I also checked the implementation of bytecode instructions for native calls, but there I could not find anything related to passing of attributes for arguments to native calls.
We can discuss that with Cyrille next week. Maybe we could think about some workaround.
Compiler Warning : WrapperClass.valueOf(...) :
Tried below code but the error is still same.
int v = env.valueOfInteger(val);
env.setReturnAttribute(new Integer(v))
return v;
Compiler Warning : AccessController :
Next set of Compiler Warnings are :
> Task :compileModules
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:25: warning: [removal] AccessControlException in java.security has been deprecated and marked for removal
public static void checkPermission (Permission p) throws AccessControlException {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:29: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
AccessControlContext context) {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:38: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
AccessControlContext context)
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:64: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
public static AccessControlContext getContext() {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:69: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
private static AccessControlContext getStackAccessControlContext() {
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/classes/modules/java.base/java/security/AccessController.java:73: warning: [removal] AccessControlContext in java.security has been deprecated and marked for removal
static AccessControlContext getInheritedAccessControlContext() {
^
AccessControl
is a class where AccessControlException
, AccessControlContext
has been used.
All these are used in conjunction with SecurityManager
. Because SecurityManager
has been deprecated , all these too.
(Code from OpenJDK-17)
* @deprecated This class is only useful in conjunction with
* {@linkplain SecurityManager the Security Manager}, which is deprecated
* and subject to removal in a future release. Consequently, this class
* is also deprecated and subject to removal. There is no replacement for
* the Security Manager or this class.
*/
@Deprecated(since="17", forRemoval=true)
public final class AccessController {
One more thing, this methods are being called from OpenJDK. We cannot easily remove them I think.
If we remove those deprecated
methods to look their usage we found below :
gov.nasa.jpf.vm.NoUncaughtExceptionsProperty
java.lang.ExceptionInInitializerError
at java.util.concurrent.atomic.Striped64.<clinit>(Striped64.java:402)
at java.util.concurrent.ConcurrentSkipListMap.addCount(ConcurrentSkipListMap.java:439)
at java.util.concurrent.ConcurrentSkipListMap.doPut(ConcurrentSkipListMap.java:683)
at java.util.concurrent.ConcurrentSkipListMap.put(ConcurrentSkipListMap.java:1347)
at gov.nasa.jpf.test.java.concurrent.ConcurrentSkipListMapTest.testFunctionality(ConcurrentSkipListMapTest.java:37)
at java.lang.reflect.Method.invoke(gov.nasa.jpf.vm.JPF_java_lang_reflect_Method)
at gov.nasa.jpf.util.test.TestJPF.runTestMethod(TestJPF.java:648)
Caused by: java.lang.NoSuchMethodException: java.security.AccessController.doPrivileged(Ljava/security/PrivilegedAction;)Ljava/lang/Object;
at java.util.concurrent.atomic.Striped64.<clinit>(Striped64.java:390)
at java.util.concurrent.ConcurrentSkipListMap.addCount(ConcurrentSkipListMap.java:439)
at java.util.concurrent.ConcurrentSkipListMap.doPut(ConcurrentSkipListMap.java:683)
at java.util.concurrent.ConcurrentSkipListMap.put(ConcurrentSkipListMap.java:1347)
at gov.nasa.jpf.test.java.concurrent.ConcurrentSkipListMapTest.testFunctionality(ConcurrentSkipListMapTest.java:37)
at java.lang.reflect.Method.invoke(gov.nasa.jpf.vm.JPF_java_lang_reflect_Method)
at gov.nasa.jpf.util.test.TestJPF.runTestMethod(TestJPF.java:648)
@cyrille-artho Please review
Our tests do (AFAIK) not use access control features, so we might provide a stub for doPrivileged
that simply executes the privileged action by calling its run
method.
Compiler Warning : AccessController :
Commented out some of the methods including doPrivileged
and then all those related Compiler Warning
disappeared.
package java.security;
/*
* MJI model class for java.security.AccessController library abstraction
*/
public class AccessController {
// public static void checkPermission (Permission p) throws AccessControlException {
// }
// public static <T> T doPrivileged (PrivilegedAction<T> action,
// AccessControlContext context) {
// return action.run();
// }
public static <T> T doPrivileged (PrivilegedAction<T> action) {
return action.run();
}
// public static <T> T doPrivileged (PrivilegedExceptionAction<T> action,
// AccessControlContext context)
// throws PrivilegedActionException {
//
// try {
// return action.run();
//
// } catch (RuntimeException rx){
// throw rx; // we have to let unchecked exceptions pass
// } catch (Exception x) {
// throw new PrivilegedActionException(x);
// }
// }
public static <T> T doPrivileged (PrivilegedExceptionAction<T> action)
throws PrivilegedActionException {
try {
return action.run();
} catch (RuntimeException rx){
throw rx; // we have to let unchecked exceptions pass
} catch (Exception x) {
throw new PrivilegedActionException(x);
}
}
// public static AccessControlContext getContext() {
// return null;
// }
// @SuppressWarnings("unused")
// private static AccessControlContext getStackAccessControlContext() {
// return null;
// }
// static AccessControlContext getInheritedAccessControlContext() {
// return null;
// }
}
As you confirm them, I will clean up them.
Compiler Warning : WrapperClass.valueOf(...) :
We still have to discuss with you
Next set of Compiler Warning :
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:55: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t1.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:76: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t1.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:82: warning: [removal] resume() in Thread has been deprecated and marked for removal
t1.resume();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:122: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t2.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:161: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t3.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:184: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t3.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:191: warning: [removal] resume() in Thread has been deprecated and marked for removal
t3.resume();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:242: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t4.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:267: warning: [removal] suspend() in Thread has been deprecated and marked for removal
t4.suspend();
^
/Users/ekla/GSOC/JPFjava17/jpf-core/src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java:272: warning: [removal] resume() in Thread has been deprecated and marked for removal
t4.resume();
^
The thread-related warnings are OK. These are tests covering an API that is deprecated, but we still have to support it as there is no replacement. So adding annotations to suppress the warnings is enough there.
Now we have only to see Compiler Warning : WrapperClass.valueOf(...)
.
At this point should we create a PR ?
Yes, if possible, make a separate PR for different groups of warnings. For example, the suppression of warnings in tests related to threads should be separate from code changes regarding constructors of classes representing primitive types.
PR #488
For WrapperClasses
Also, please review the commented Test
. That Test
was deprecated although.
Also close the Issue
Thanks!