Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

8279011: JFR: JfrChunkWriter incorrectly handles int64_t chunk size as size_t #50

Closed

Conversation

shipilev
Copy link
Member

@shipilev shipilev commented Dec 20, 2021

See the investigation in the bug.

Spot the problem:

int64_t JfrChunkWriter::write_chunk_header_checkpoint(bool flushpoint) {
   ...
   const size_t sz_written = size_written(); // <-- returns int64_t
   write_be_at_offset(sz_written, chunk_size_offset); // <--- template instantiation with type=size_t
   return sz_written;
 }

This would have been nearly fine -- small size_t -> int64_t conversion is okay value-wise. But write_be_at_offset calculates the position for the writeout using sizeof(T), which silently borks the whole thing on at least 32-bit platforms, where sizeof(size_t) != sizeof(int64_t).

Additional testing:

  • Linux x86_64 jdk_jfr (no regressions)
  • Linux x86_32 jdk_jfr (many failing tests now pass)

Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8279011: JFR: JfrChunkWriter incorrectly handles int64_t chunk size as size_t

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk18 pull/50/head:pull/50
$ git checkout pull/50

Update a local copy of the PR:
$ git checkout pull/50
$ git pull https://git.openjdk.java.net/jdk18 pull/50/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 50

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk18/pull/50.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 20, 2021

👋 Welcome back shade! 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 20, 2021
@openjdk
Copy link

openjdk bot commented Dec 20, 2021

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

  • hotspot-jfr

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-jfr hotspot-jfr-dev@openjdk.java.net label Dec 20, 2021
@mlbridge
Copy link

mlbridge bot commented Dec 20, 2021

Webrevs

@mgronlun
Copy link

Nice find.

But surprised there is not a compile error here (both incompatible type size and sign), if sizeof(size_t) == 4 and sizeof(int64_t) == 8:

const size_t sz_written = size_written(); // <-- returns int64_t

So the value is written as a 4-byte quantity only, since it is dimensioned using sizeof(size_t), and therefore does not fill the full 8- bytes slot associated with the 'chunksize' header field. And uninitialized memory in the high 4-bytes leads to this high value?

@shipilev
Copy link
Member Author

But surprised there is not a compile error here (both incompatible type size and sign), if sizeof(size_t) == 4 and sizeof(int64_t) == 8:

const size_t sz_written = size_written(); // <-- returns int64_t

I believe narrowing conversions like these are implicit in C++, and do not produce warnings in modern compilers. See: https://godbolt.org/z/48oh91YE9

So the value is written as a 4-byte quantity only, since it is dimensioned using sizeof(size_t), and therefore does not fill the full 8- bytes slot associated with the 'chunksize' header field. And uninitialized memory in the high 4-bytes leads to this high value?

Actually, I think it is worse than that, since sizeof(T)*len + len can produce the pos that is way off, because of multiplication by len:

template <typename BE, typename IE, typename WriterPolicyImpl >
template <typename T>
inline void WriterHost<BE, IE, WriterPolicyImpl>::be_write(const T* value, size_t len) {
  assert(value != NULL, "invariant");
  assert(len > 0, "invariant");
  // Might need T + 1 size
  u1* const pos = ensure_size(sizeof(T) * len + len);
  if (pos) {
    this->set_current_pos(BE::be_write(value, len, pos));
  }

So it might just store the chunk_size somewhere else...

@mgronlun
Copy link

mgronlun commented Dec 20, 2021

Len is only internally derived to ensure the size to be written can be accommodated by the internal buffer system, it does not affect the position of where to write on disk.

Copy link

@mgronlun mgronlun 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. Thanks.

@openjdk
Copy link

openjdk bot commented Dec 20, 2021

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

8279011: JFR: JfrChunkWriter incorrectly handles int64_t chunk size as size_t

Reviewed-by: mgronlun

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 no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential 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 20, 2021
@shipilev
Copy link
Member Author

Len is only internally derived to ensure the size to be written can be accommodated by the internal buffer system, it does not affect the position of where to write on disk.

All right. Then it must be just uninitialized 32-bit head/tail in 64-bit value. In the bug report, we see 9896023423722239, which is 0x23286180dc22ff. Out of which, higher 32-bits 0x232861 looks like a real chunk size, 2304097. This is similar to the chunk size 64-bit VM reports in the same test.

@shipilev
Copy link
Member Author

Any other (R)eviewers needd? I think this is pretty trivial.

@mgronlun
Copy link

Any other (R)eviewers needd? I think this is pretty trivial.

Please go ahead Aleksey, thank you.

@shipilev
Copy link
Member Author

Please go ahead Aleksey, thank you.

Thanks. I followed up on GHA failures, those seem to be unrelated (Shenandoah and compiler bugs in JDK 18).

/integrate

@openjdk
Copy link

openjdk bot commented Dec 21, 2021

Going to push as commit 467f654.
Since your change was applied there has been 1 commit pushed to the master branch:

  • 819f9bd: 8274323: compiler/codegen/aes/TestAESMain.java failed with "Error: invalid offset: -1434443640" after 8273297

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Dec 21, 2021

@shipilev Pushed as commit 467f654.

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

@shipilev shipilev deleted the JDK-8279011-jfr-32bit-chunksize branch December 23, 2021 16:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hotspot-jfr hotspot-jfr-dev@openjdk.java.net integrated Pull request has been integrated
2 participants