Skip to content
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

8319925: CSS.BackgroundImage incorrectly uses double-checked locking #16917

Closed
wants to merge 3 commits into from

Conversation

prsadhuk
Copy link
Contributor

@prsadhuk prsadhuk commented Dec 1, 2023

CSS.BackgroundImage.getImage uses double-checked locking but the loadedImage field isn't declared as volatile. Without the volatile modifier, double-checked locking implementation is broken.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8319925: CSS.BackgroundImage incorrectly uses double-checked locking (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/16917/head:pull/16917
$ git checkout pull/16917

Update a local copy of the PR:
$ git checkout pull/16917
$ git pull https://git.openjdk.org/jdk.git pull/16917/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 16917

View PR using the GUI difftool:
$ git pr show -t 16917

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/16917.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 1, 2023

👋 Welcome back psadhukhan! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 1, 2023
@openjdk
Copy link

openjdk bot commented Dec 1, 2023

@prsadhuk The following label will be automatically applied to this pull request:

  • client

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.

@openjdk openjdk bot added the client client-libs-dev@openjdk.org label Dec 1, 2023
@mlbridge
Copy link

mlbridge bot commented Dec 1, 2023

Webrevs

@openjdk
Copy link

openjdk bot commented Dec 1, 2023

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

8319925: CSS.BackgroundImage incorrectly uses double-checked locking

Reviewed-by: aivanov

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 177 new commits pushed to the master branch:

  • 49fff01: 8211238: @deprecated JFR event
  • 656b446: 8320969: Shenandoah: Enforce stable number of GC workers
  • 9a87e52: 8320538: Obsolete CSS styles in collection framework doc-file
  • 03c4595: 8321164: javac with annotation processor throws AssertionError: Filling jrt:/... during JarFileObject[/...]
  • 632a3c5: 8305825: getBounds API returns wrong value resulting in multiple Regression Test Failures on Ubuntu 23.04
  • 75a7c19: 8315827: Kitchensink.java and RenaissanceStressTest.java time out with jvmti module errors
  • 91ffdfb: 8320365: IPPPrintService.getAttributes() causes blanket re-initialisation
  • 50baaf4: 8321013: Parallel: Refactor ObjectStartArray
  • afb8964: 8320443: [macos] Test java/awt/print/PrinterJob/PrinterDevice.java fails on macOS
  • 82796bd: 8320570: NegativeArraySizeException decoding >1G UTF8 bytes with non-ascii characters
  • ... and 167 more: https://git.openjdk.org/jdk/compare/20aae3c4388ac33af54bbe25328c5fe817c0bd5f...master

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 master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 1, 2023
@mrserb
Copy link
Member

mrserb commented Dec 2, 2023

That code does not look like double-checked lock, it is something different. It checks/init/sets one field and then returns another one. Even if both will be marked as volatile the method may return null, since the loadedImage is set to true before init of image.

@prsadhuk
Copy link
Contributor Author

prsadhuk commented Dec 4, 2023

That code does not look like double-checked lock, it is something different. It checks/init/sets one field and then returns another one. Even if both will be marked as volatile the method may return null, since the loadedImage is set to true before init of image.

I think the field loadedImage still needs to be volatile as it is accessed multiple time even though it can be argued to be non-DCL in classical way

@aivanov-jdk
Copy link
Member

That code does not look like double-checked lock, it is something different. It checks/init/sets one field and then returns another one. Even if both will be marked as volatile the method may return null, since the loadedImage is set to true before init of image.

I think the field loadedImage still needs to be volatile as it is accessed multiple time even though it can be argued to be non-DCL in classical way

Yep.

It is a DCL, essentially, but it uses a different field to protect access to another field whereas the classic DCL uses the same field that it protects.

Even if both will be marked as volatile the method may return null, since the loadedImage is set to true before init of image.

This is correct. Good catch! I missed it.

For correctness, the assignment

has to be moved below the if-statement:

                    if (!loadedImage) {
                        URL url = CSS.getURL(base, svalue);
                        if (url != null) {
                            image = new ImageIcon();
                            Image tmpImg = Toolkit.getDefaultToolkit().createImage(url);
                            if (tmpImg != null) {
                                image.setImage(tmpImg);
                            }
                        }
                        loadedImage = true;
                    }

Otherwise, the returned image may still be in an inconsistent state. This way, loadedImage is assigned true after image is initialised; that way if another thread reads loadedImage and its value is true, it can safely access image.

Copy link
Member

@aivanov-jdk aivanov-jdk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my recent comment, the code needs updating.

Thanks @mrserb for catching another mistake.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Dec 4, 2023
@mrserb
Copy link
Member

mrserb commented Dec 4, 2023

It is probably easy just drop the usage of loadedImage and use the image instead?

@aivanov-jdk
Copy link
Member

It is probably easy just drop the usage of loadedImage and use the image instead?

Ideally. Yet there's a corner case: if url is null, there's nothing to load; and it doesn't make sense to re-try, the URL won't change. If it were not for this case, the image field alone would do the job.

@mrserb
Copy link
Member

mrserb commented Dec 4, 2023

It is probably easy just drop the usage of loadedImage and use the image instead?

Ideally. Yet there's a corner case: if url is null, there's nothing to load; and it doesn't make sense to re-try, the URL won't change. If it were not for this case, the image field alone would do the job.

Not sure how it is expected to work, the url is a parameter and can be changed on the fly(see the usage of base in the StyleSheet.java)

@prsadhuk
Copy link
Contributor Author

prsadhuk commented Dec 5, 2023

It is probably easy just drop the usage of loadedImage and use the image instead?

Ideally. Yet there's a corner case: if url is null, there's nothing to load; and it doesn't make sense to re-try, the URL won't change. If it were not for this case, the image field alone would do the job.

Not sure how it is expected to work, the url is a parameter and can be changed on the fly(see the usage of base in the StyleSheet.java)

Yes, seems like setBase can be used to change image URL on the fly...Modified to use image field for DCL...

@aivanov-jdk
Copy link
Member

It is probably easy just drop the usage of loadedImage and use the image instead?

Ideally. Yet there's a corner case: if url is null, there's nothing to load; and it doesn't make sense to re-try, the URL won't change. If it were not for this case, the image field alone would do the job.

Not sure how it is expected to work, the url is a parameter and can be changed on the fly(see the usage of base in the StyleSheet.java)

Yes, seems like setBase can be used to change image URL on the fly...Modified to use image field for DCL...

I am against this latest change: it doesn't fix much but it would cause creating the URL each time getImage is called if the URL is invalid for whatever reason.

Usually, setBase isn't changed on the fly, it's set once when the StyleSheet and/or HTMLDocument is created. If StyleSheet.setBase is called to modify the base, the image needs reloading whether it's already loaded or not — the latest change doesn't address this problem in any way: if the image is successfully loaded, it's what's returned even though the base has been changed; and only if the image couldn't be loaded with the old base, the image will be re-loaded again.

This is asymmetric: in one case, it's re-loaded, in another — it isn't.

I'm for keeping the current behaviour: if the URL is invalid, the image isn't loaded and will never be for this instance of CSS.BackgroundImage; if the URL is valid, the image gets loaded.

@prsadhuk
Copy link
Contributor Author

prsadhuk commented Dec 5, 2023

if the URL is invalid, the image isn't loaded

if (!loadedImage) {
                        URL url = CSS.getURL(base, svalue);
                        if (url != null) {
                            image = new ImageIcon();
                            Image tmpImg = Toolkit.getDefaultToolkit().createImage(url);
                            if (tmpImg != null) {
                                image.setImage(tmpImg);
                            }
                        }
                        loadedImage = true;
                    }

As per your change, if URL is invalid ie url = null, image is not loaded but loadedImage is set to true so it will not give another chance to load the URL again via CSS.getURL just in case user decides to call setBase with a valid URL (after finding getImage returning null)

@aivanov-jdk
Copy link
Member

if the URL is invalid, the image isn't loaded

As per your change, if URL is invalid ie url = null, image is not loaded but loadedImage is set to true so it will not give another chance to load the URL again via CSS.getURL just in case user decides to call setBase with a valid URL (after finding getImage returning null)

Exactly! The image is never given a chance to load for a second time.

It is how the code has always worked.

With your proposed change, the image has a chance to re-load if and only if url == null. And it tries re-loading over and over again until url becomes non-null. At the same time, the change to base is ignored if the url wasn't null… even though the image itself may not have been loaded successfully.

I do not think this is correct or desirable.

@prsadhuk
Copy link
Contributor Author

prsadhuk commented Dec 5, 2023

if the URL is invalid, the image isn't loaded

As per your change, if URL is invalid ie url = null, image is not loaded but loadedImage is set to true so it will not give another chance to load the URL again via CSS.getURL just in case user decides to call setBase with a valid URL (after finding getImage returning null)

Exactly! The image is never given a chance to load for a second time.

It is how the code has always worked.

But it can be a bug in the code that it never gave a chance second time..I think it should get a chance to reload the URL again in case it is invalid the 1st time so I guess the change should be the one where if url is invalid, loadedImage should not be set and it will need a relook at the url (to be set by user)

if (!loadedImage) {
                        URL url = CSS.getURL(base, svalue);
                        if (url != null) {
                            image = new ImageIcon();
                            Image tmpImg = Toolkit.getDefaultToolkit().createImage(url);
                            if (tmpImg != null) {
                                image.setImage(tmpImg);
                            }
                            loadedImage = true;
                        }
                    }

@aivanov-jdk
Copy link
Member

But it can be a bug in the code that it never gave a chance second time..I think it should get a chance to reload the URL again in case it is invalid the 1st time so I guess the change should be the one where if url is invalid, loadedImage should not be set and it will need a relook at the url (to be set by user)

My point is it must re-load the image whenever base is changed, as @mrserb pointed out.

If you want to implement this feature, submit a new bug — it is out of scope for this issue. Better yet, just submit the new bug to document the discovered problem.

Re-loading the image if and only if the url is null is inconsistent. I strongly believe the image is to be re-loaded either always or never at all rather than sometimes.

Currently, it's in never mode. I am for preserving the current behaviour rather than adding a half-baked solution.

@prsadhuk
Copy link
Contributor Author

prsadhuk commented Dec 6, 2023

But it can be a bug in the code that it never gave a chance second time..I think it should get a chance to reload the URL again in case it is invalid the 1st time so I guess the change should be the one where if url is invalid, loadedImage should not be set and it will need a relook at the url (to be set by user)

My point is it must re-load the image whenever base is changed, as @mrserb pointed out.

If you want to implement this feature, submit a new bug — it is out of scope for this issue. Better yet, just submit the new bug to document the discovered problem.

Re-loading the image if and only if the url is null is inconsistent. I strongly believe the image is to be re-loaded either always or never at all rather than sometimes.

Currently, it's in never mode. I am for preserving the current behaviour rather than adding a half-baked solution.

OK. I have had my doubts but I agree to keep it as similar to code we have it now..Modified the PR..

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 7, 2023
@prsadhuk
Copy link
Contributor Author

prsadhuk commented Dec 7, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Dec 7, 2023

Going to push as commit 0b0fa47.
Since your change was applied there have been 178 commits pushed to the master branch:

  • 0048f1d: 8263256: Test java/net/Inet6Address/serialize/Inet6AddressSerializationTest.java fails due to dynamic reconfigurations of network interface during test
  • 49fff01: 8211238: @deprecated JFR event
  • 656b446: 8320969: Shenandoah: Enforce stable number of GC workers
  • 9a87e52: 8320538: Obsolete CSS styles in collection framework doc-file
  • 03c4595: 8321164: javac with annotation processor throws AssertionError: Filling jrt:/... during JarFileObject[/...]
  • 632a3c5: 8305825: getBounds API returns wrong value resulting in multiple Regression Test Failures on Ubuntu 23.04
  • 75a7c19: 8315827: Kitchensink.java and RenaissanceStressTest.java time out with jvmti module errors
  • 91ffdfb: 8320365: IPPPrintService.getAttributes() causes blanket re-initialisation
  • 50baaf4: 8321013: Parallel: Refactor ObjectStartArray
  • afb8964: 8320443: [macos] Test java/awt/print/PrinterJob/PrinterDevice.java fails on macOS
  • ... and 168 more: https://git.openjdk.org/jdk/compare/20aae3c4388ac33af54bbe25328c5fe817c0bd5f...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 7, 2023
@openjdk openjdk bot closed this Dec 7, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Dec 7, 2023
@openjdk
Copy link

openjdk bot commented Dec 7, 2023

@prsadhuk Pushed as commit 0b0fa47.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@prsadhuk prsadhuk deleted the JDK-8319925 branch December 7, 2023 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client client-libs-dev@openjdk.org integrated Pull request has been integrated
3 participants