Skip to content

JDK-8299790: os::print_hex_dump is racy #14895

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

Conversation

tstuefe
Copy link
Member

@tstuefe tstuefe commented Jul 16, 2023

Tiny fix for a tiny problem.

os::print_hex_dump uses os::is_readable_pointer to check the to-be-printed memory for readability; os::is_readable_pointer uses SafeFetch to probe the memory for access, which is good, but then, by the time we actually print that information, we reread the memory location again. It may be unreadable now (either because the region had been unmapped or protected by a concurrent thread), and we would crash the VM.

The patch rewrites the function to not use os::is_readable_pointer, but to use SafeFetch to read from memory directly and then use the result of that read for printing. That requires a bit of bit fiddling, since we only can read word-wise, but the hex-dump could be in units between bytes and qwords.

Tests: manual and GHA-driven gtests on all platforms. The gtests test this function exhaustively.


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

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 14895

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 16, 2023

👋 Welcome back stuefe! 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
Copy link

openjdk bot commented Jul 16, 2023

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

  • hotspot-runtime

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 hotspot-runtime hotspot-runtime-dev@openjdk.org label Jul 16, 2023
@tstuefe tstuefe force-pushed the JDK-8299790-os-print-hex-dump-should-use-SafeFetch branch 3 times, most recently from b0e3065 to d9d7a71 Compare July 16, 2023 13:26
@tstuefe tstuefe force-pushed the JDK-8299790-os-print-hex-dump-should-use-SafeFetch branch from d9d7a71 to 1c78457 Compare July 16, 2023 17:00
@tstuefe tstuefe marked this pull request as ready for review July 16, 2023 19:49
@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 16, 2023
@mlbridge
Copy link

mlbridge bot commented Jul 16, 2023

Webrevs

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Some comments...

@tstuefe
Copy link
Member Author

tstuefe commented Jul 17, 2023

Thank you @shipilev. I worked in your feedback and massaged the code a bit.

@tstuefe
Copy link
Member Author

tstuefe commented Aug 4, 2023

Friendly ping... still need a second review.

@tstuefe
Copy link
Member Author

tstuefe commented Aug 4, 2023

Oh, I don't even have the first one... @shipilev, any more suggestions?

(this is not terribly important, no pressure)

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Seems reasonable. Just one (hopefully) minor thing below.

Thanks.

@@ -34,6 +34,7 @@
#include "utilities/macros.hpp"
#include "utilities/ostream.hpp"
#include "unittest.hpp"
#include "gmock/gmock.h"
Copy link
Member

Choose a reason for hiding this comment

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

Is this a leftover?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, thank you. That slipped by me, it's from an earlier experiment.

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Looks good.

It's a pity that we would read full intptr_t for every unit, which would mean that reading byte-unit block of N bytes would issue 8*N SafeFetch reads. But I guess it is fine, given the old code did SafeFetch32 anyway.

That looks like fixing another implicit bug, if we are reading the unaligned 64-bit location at readable boundary: is_readable_pointer would read 32-bits and return true for first 4 bytes of the location, and then we go in and read 64 bits with print_hex_readable_pointer with unitsize = 8. Which might break if the last 4 bytes of that read are actually outside the readable range.

@openjdk
Copy link

openjdk bot commented Aug 7, 2023

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

8299790: os::print_hex_dump is racy

Reviewed-by: shade, dholmes

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

  • e080a0b: 8311508: ZGC: RAII use of IntelJccErratumAlignment
  • 242a2e6: 8308843: Generational ZGC: Remove gc/z/TestHighUsage.java
  • c822183: 8313768: Reduce interaction with volatile field in j.u.l.StreamHandler
  • cd16158: 8314075: Update JCov version for JDK 22
  • c307391: 8307184: Incorrect/inconsistent specification and implementation for Elements.getDocComment
  • 593ba2f: 8313693: Introduce an internal utility for the Damerau–Levenshtein distance calculation
  • 360f65d: 8314022: Problem-list tests failing with jtreg 7.3
  • 0eb0997: 8288936: Wrong lock ordering writing G1HeapRegionTypeChange JFR event
  • 19ae62a: 8311170: Simplify and modernize equals and hashCode in security area
  • e9f751a: 8311247: Some cpp files are compiled with -std:c11 flag
  • ... and 35 more: https://git.openjdk.org/jdk/compare/dc01604756c22889412f9f25b534488180327317...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 Aug 7, 2023
@tstuefe
Copy link
Member Author

tstuefe commented Aug 7, 2023

Looks good.

It's a pity that we would read full intptr_t for every unit, which would mean that reading byte-unit block of N bytes would issue 8*N SafeFetch reads. But I guess it is fine, given the old code did SafeFetch32 anyway.

I thought about this too but then refrained from implementing a "read intptr_t then print bytes" to keep the patch complexity low.

I think another, simpler, patch would be worthwhile where we split print_hex_dump into two variants, one using SafeFetch and one just reading, and use the former one only if we are unsure about the memory content. E.g. at hs-err file dumping. There is no need to pay for SafeFetch when hexdumping e.g. for logging.

Maybe for a future RFE.

That looks like fixing another implicit bug, if we are reading the unaligned 64-bit location at readable boundary: is_readable_pointer would read 32-bits and return true for first 4 bytes of the location, and then we go in and read 64 bits with print_hex_readable_pointer with unitsize = 8. Which might break if the last 4 bytes of that read are actually outside the readable range.

Oh, I missed that. Nice :)

Thanks for the review!

@tstuefe
Copy link
Member Author

tstuefe commented Aug 10, 2023

@dholmes-ora are you okay with the patch in its current form?

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Looks okay to me. Thanks

@tstuefe
Copy link
Member Author

tstuefe commented Aug 10, 2023

Thanks David!

/integrate

@openjdk
Copy link

openjdk bot commented Aug 10, 2023

Going to push as commit 8f28809.
Since your change was applied there have been 45 commits pushed to the master branch:

  • e080a0b: 8311508: ZGC: RAII use of IntelJccErratumAlignment
  • 242a2e6: 8308843: Generational ZGC: Remove gc/z/TestHighUsage.java
  • c822183: 8313768: Reduce interaction with volatile field in j.u.l.StreamHandler
  • cd16158: 8314075: Update JCov version for JDK 22
  • c307391: 8307184: Incorrect/inconsistent specification and implementation for Elements.getDocComment
  • 593ba2f: 8313693: Introduce an internal utility for the Damerau–Levenshtein distance calculation
  • 360f65d: 8314022: Problem-list tests failing with jtreg 7.3
  • 0eb0997: 8288936: Wrong lock ordering writing G1HeapRegionTypeChange JFR event
  • 19ae62a: 8311170: Simplify and modernize equals and hashCode in security area
  • e9f751a: 8311247: Some cpp files are compiled with -std:c11 flag
  • ... and 35 more: https://git.openjdk.org/jdk/compare/dc01604756c22889412f9f25b534488180327317...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Aug 10, 2023

@tstuefe Pushed as commit 8f28809.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants