New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8280798: com.sun.jdi.ObjectReference::setValue spec should prohibit any final field modification #11279
Conversation
👋 Welcome back amenkov! A progress list of the required criteria for merging this PR into |
/csr needed |
@alexmenkov this pull request will not be integrated until the CSR request JDK-8281652 for issue JDK-8280798 has been approved. |
@alexmenkov The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
This issue need a release note. |
I commented in the bug report, but shouldn't a debugger be able to set a final field? |
I think this one is going to require more investigation as I can't immediately tell if this is a spec or implementation issue. I checked the JDWP spec and the ClassReference/SetValues has "Final fields cannot be set" in its spec, so this means that static finals can't be set. ObjectReference/SetValues command doesn't have this, so JDWP allows a tool to set final fields. So if the change proposed in this PR goes ahead it means that JDI and JDWP specs won't be aligned (which is okay, it just needs to be fully understood). Maybe start by digging into the history of ReferenceTypeImpl to see if the spec vs. implementation discrepancy dates goes back to JDK 1.2 (when JPDA was added) or it crept in at some other time. Maybe also dig into bug reports to see if there has been any complaints about the current behavior. If the outcome is that spec is changed as proposed then it will require the description for IllegalArgumentException to be expanded. If the outcome is to allow for changing of some final fields then the spec may need to be updated to take account of "non-modifiable final field", as in final fields in hidden classes and records. In both cases, the JDWP spec will need to looked at it too. |
ObjectReference.setValue() calls ReferenceType.validateFieldSet(), which does:
This code has been in place since the file was introduced on 2007-12-01. If there is a version that predates it, I don't know where to look. I'm not sure of the history relative to JDK 1.2 and the introduction of JPDA. Yes, what is being proposed makes the JDI and JDWP specs out of sync, but the implementations have always been out of sync. The proposed spec change is just clarifying what the implementation already does. However, Eclipse does allow the user to set a non-static final field, but only after clicking through a warning dialog. |
So changing the spec would make Eclipse non-compliant. Perhaps to bring the spec and implementation in-line we have to make the implementation behaviour allowable by the spec but not required - that is what we often do in other areas (e.g. JNI spec - my personal point of experience). But I remain surprised/perplexed that a debugger would not be allowed to change a final field. Can't we still change final fields via reflection anyway? (I know the constraints have been tightening in that area.) |
It is allowed, for the most part, but with the warning:
In JDK-8280798 Alex has documented a use case that shows the dangers of allowing this: |
The change makes Eclipse non-compliant, but it shouldn't be a problem as Eclipse JDI implementation is used only by Eclipse debugger (we discussed it with one of the Eclipse debugger contributors - he doesn't see problem here). And it's up to Eclipse team to decide if they want to update the implementation or keep it. Reflection allows to modify final instance fields, but its spec has a warning that changing final fields is meaningful only in certain cases (which are not common for debuggers). So if we decide to update implementation to modify final fields, it would be better to add similar warning to JDI spec (additionally to "non-modifiable final field" changes described by Alan) |
I also have to wonder about JIT behavior in this context (and this might be what the RMI warning is alluding to). What if the JIT sees a static final reference to an object with a final field. It probably generates code that references the value of the field rather than generating code that fetches from the field. If the field value changes, the JIT won't know and the existing code will continue to reference the old value. |
I dug into the pre-OpenJDK history as most of the action on this was in 1999. In 1999, ObjectReference.setValue's docs changed from "The field must not be final" to "If static, the field must not be final". The exception message started out as "Cannot set value of final field" and changed to "Cannot set value of static final field". jjh re-visited it in 2005 via JDK-623152 where he changed the exception message to "Cannot set value of final field". I don't think the ancient history sheds much more light on the original intent. I suspect the original intent was to disallow setValue only static final fields. That would have aligned with Field.set + setAccessible(true) at the time. In general I think the JDK is on the path to a world where "final means final". We see this with final fields on hidden classes and fields in records. So the proposal to change the JDI spec may be too bad but it require reaching out to IDE maintainers. Eclipse have their own JDI implementation. VS.Code uses the Eclipse JDI implementation. IntelliJ maintain a fork of JDI. So while the JDK's implementation of JDI has disallowed setting of all final fields for 20+ years, this is not the JDI implementation that is used by at least two of the main stream IDEs. |
IntelliJ currently does not allow setting final fields. I'm checking with a team member to make sure they are ok with the spec change. As mentioned earlier, we already discussed this with an Eclipse team member, and there was no objection to the proposed spec change. Eclipse currently does allow setting final fields, but you have to click through a warning dialog first. Eclipse can continue with this behavior if they wish. There is no requirement for them to be spec compliant. |
I decided to check if VS Code shows the warning as Eclipse does, so I installed VS Code and tried to set final fields in debugger. So VS Code DOES NOT allow to set static final fields. As far as I got VS Code uses Eclipse JDT only for editor (code navigation, auto completion, refactoring, etc), but debugging is implemented by another extension: Debugger for Java (https://github.com/Microsoft/java-debug). I.e. VS Code does not use Eclipse |
Egor at Jetbrains (Intellij) is ok with the proposed spec changes. |
I checked with the Microsoft team that maintain the Java Debugger in VS Code. The JDI implementation comes from the Eclipse JDT project. The summary is that the VS Code debugger doesn't allow final fields to be modified. It checks if the field is final so it won't call ObjectReference::setValue for final fields. |
I think we've got enough information now to support this change. |
One other thing to add is that we should survey the existing tests to make sure that there are tests to ensure IAE is thrown for both static and instance fields when they are final. |
I think a good way to find such tests would be to remove the !isFinal() check and see if any tests fail. |
@alexmenkov This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 170 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
The only test for final fields is vmTestbase/nsk/jdi/ObjectReference/setValue/setvalue004. |
test/hotspot/jtreg/vmTestbase/nsk/jdi/ObjectReference/setValue/setvalue004t.java
Outdated
Show resolved
Hide resolved
/integrate |
Going to push as commit 2821fa9.
Your commit was automatically rebased without conflicts. |
@alexmenkov Pushed as commit 2821fa9. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
com.sun.jdi.ObjectReference::setValue spec says that final static fields cannot be modified, but openjdk implementation throws IllegalArgumentException for any final fields (static or instance).
The fix updates the spec to prohibit any final field modification
CSR: JDK-8281652
Progress
Issues
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/11279/head:pull/11279
$ git checkout pull/11279
Update a local copy of the PR:
$ git checkout pull/11279
$ git pull https://git.openjdk.org/jdk pull/11279/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 11279
View PR using the GUI difftool:
$ git pr show -t 11279
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/11279.diff