-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
8347006: LoadRangeNode floats above array guard in arraycopy intrinsic #22967
Conversation
👋 Welcome back thartmann! A progress list of the required criteria for merging this PR into |
@TobiHartmann 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 77 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 |
@TobiHartmann The following labels 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 lists. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
generate_non_array_guard(load_object_klass(src), slow_region); | ||
const Type* tary = TypeAryPtr::make(TypePtr::BotPTR, TypeAry::make(Type::BOTTOM, TypeInt::POS), nullptr, false, Type::OffsetBot); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this never used elsewhere? Should it a static field in TypeAryPtr
same as TypeAryPtr::BYTES
and friends?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered as well and no, we don't use this type anywhere else (the closest would be TypeAryPtr::RANGE
). We only create it when meeting arrays of primitive and non-primitive element type. Do you think this should still go to TypeAryPtr::*
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add it to TypeAryPtr
(maybe as TypeAryPtr::BOTTOM
) . The main benefit I see is that the new code would be more readable if it referred to TypeAryPtr::BOTTOM
rather than the long type creation expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I'll update the patch accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a question about the test :)
* @summary Deoptimization between array allocation and arraycopy may result in non initialized array | ||
* | ||
* @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:TypeProfileLevel=020 | ||
* compiler.arraycopy.TestArrayCopyNoInit | ||
* @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:TypeProfileLevel=020 | ||
* -XX:+UnlockExperimentalVMOptions -XX:+UseCompactObjectHeaders -XX:-UseTLAB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have been able to reproduce this with -UseCompressedClassPointers
, right? If so, I'd suggest we do a run config with -UseCCP
instead of +UseCOH
, because this gives us a cleaner way for backports, if we need one later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have been able to reproduce this with -UseCompressedClassPointers, right?
No, I was never able to reproduce this with -XX:-UseCompressedClassPointers
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I was confused by this in PR body then:
I was able to reliably reproduce the issue with compiler/arraycopy/TestArrayCopyNoInit.java and -XX:-UseTLAB -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedClassPointers on Linux AArch64 and verified that the fix solves the problem.
But fine, if it reproduces with +UCOH, let it be there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's actually a typo, good catch. Should be -XX:+UseCompactObjectHeaders
. I'll fix it in the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cant you please add this '@run' as a separate testcase with it's own id. So it is easier to identify and exclude the failures.
Thanks for the review, Roland! |
I think bug should target 25 and then we backport it to 24. |
Since we should fix the issue in JDK 24, the bug should remain targeted to JDK 24. The Skara bot will then take care of updating the fix version to JDK 25 and creating a backport to JDK 24 once we push this into master. |
I updated the fix according to Roland's suggestions and also added missing stopped checks (otherwise, the Cast will become TOP and below code does not like that). |
generate_non_array_guard(load_object_klass(src), slow_region); | ||
if (!stopped()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we simply return then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we need to set up the slow_region
path, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we need to set up the
slow_region
path, right?
By that you mean have the slow_region
feed into the uncommon trap that's only created later. It does feel weird that we know we have reached a dead end and we keep trying to add stuff, but ok then.
The other thing is shouldn't the cast be added in generate_non_array_guard()
? I see it's used elsewhere (LibraryCallKit::inline_native_getLength()
): couldn't the same bug occur there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By that you mean have the slow_region feed into the uncommon trap that's only created later.
Right. It's a bit weird but probably still the best solution in terms of complexity. The trap will lead to recompilation and then the too_many_traps
check will trigger.
The other thing is shouldn't the cast be added in generate_non_array_guard()? I see it's used elsewhere (LibraryCallKit::inline_native_getLength()): couldn't the same bug occur there?
Right, good catch. I think the use in LibraryCallKit::inline_native_getLength
has the same problem. We can't easily put the cast into generate_non_array_guard
though because it operates on the Klass and not on the object. The other generate*array*guard
methods potentially have the same issue but current uses look good. I guess it's best to fix the LibraryCallKit::inline_native_getLength
as well, i.e., make it the caller's responsibility to add a cast. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe inline_getObjectSize
is affected as well:
jdk/src/hotspot/share/opto/library_call.cpp
Lines 8535 to 8543 in afe5434
Node* array_ctl = generate_array_guard(klass_node, nullptr); | |
if (array_ctl != nullptr) { | |
// Array case: size is round(header + element_size*arraylength). | |
// Since arraylength is different for every array instance, we have to | |
// compute the whole thing at runtime. | |
PreserveJVMState pjvms(this); | |
set_control(array_ctl); | |
Node* arr_length = load_array_length(obj); |
And LibraryCallKit::inline_native_clone
as well:
jdk/src/hotspot/share/opto/library_call.cpp
Lines 5257 to 5262 in afe5434
Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)nullptr); | |
if (array_ctl != nullptr) { | |
// It's an array. | |
PreserveJVMState pjvms(this); | |
set_control(array_ctl); | |
Node* obj_length = load_array_length(obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's best to fix the
LibraryCallKit::inline_native_getLength
as well, i.e., make it the caller's responsibility to add a cast. What do you think?
Maybe the methods need to take an extra parameter (the object to cast)?
Having the cast in the method would lead to less code duplication and a lower risk of forgetting the cast when new calls of the method are added so that's what I would go with unless it's really a pain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'll give that a try.
generate_non_array_guard(load_object_klass(src), slow_region); | ||
if (!stopped()) { | ||
src = _gvn.transform(new CheckCastPPNode(control(), src, TypeAryPtr::BOTTOM)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a CheckCastPP
and not a CastPP
? My understanding is that a CheckCastPP
is used when we force changing the type of a node (e.g a raw pointer of Allocate
into a typed pointer), so we do not join the type of the input with that of the output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I changed that.
@@ -1473,6 +1473,7 @@ class TypeAryPtr : public TypeOopPtr { | |||
virtual const TypeKlassPtr* as_klass_type(bool try_for_exact = false) const; | |||
|
|||
// Convenience common pre-built types. | |||
static const TypeAryPtr* BOTTOM; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you are here it may be better to change the other constant to TypeAryPtr*
instead of TypeAryPtr *
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, done.
Roland, Quan Anh, thanks for the reviews! I pushed a new version that should address all comments. |
Thanks again for the review, @merykitty! |
if (obj != nullptr && is_array_ctrl != nullptr && is_array_ctrl != top()) { | ||
// Keep track of the fact that 'obj' is an array to prevent | ||
// array specific accesses from floating above the guard. | ||
*obj = _gvn.transform(new CastPPNode(is_array_ctrl, *obj, TypeAryPtr::BOTTOM)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do this for above code when layout is known for compiler (layout_con
is checked)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this as well but I don't think it's necessary because:
- No cast is needed if we know the type already
- We don't emit a guard and only one branch remains, so there is no risk of the array specific access floating above
So I went with the simplest changes for now, also since we need to backport this change (it got already much more complicated than I was aiming for).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good.
Thanks for the review, Vladimir! @rwestrel are you okay with the changes as well? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thanks again, Roland! |
/integrate |
Going to push as commit 82e2a79.
Your commit was automatically rebased without conflicts. |
@TobiHartmann Pushed as commit 82e2a79. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
/backport :jdk24 |
@TobiHartmann the backport was successfully created on the branch backport-TobiHartmann-82e2a791-jdk24 in my personal fork of openjdk/jdk. To create a pull request with this backport targeting openjdk/jdk:jdk24, just click the following link: The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:
If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk:
|
C2's arraycopy intrinsic adds guards that check that the source and destination objects are arrays:
jdk/src/hotspot/share/opto/library_call.cpp
Lines 5917 to 5919 in afe5434
If these guards pass, the array length is loaded:
jdk/src/hotspot/share/opto/library_call.cpp
Lines 5930 to 5933 in afe5434
But since the
LoadRangeNode
is not pinned, it might float above the array guard:jdk/src/hotspot/share/opto/graphKit.cpp
Line 1214 in afe5434
If the object is not an array, we will read garbage. That's usually fine because the result will not be used (the array guard will trigger) but with
-XX:+UseCompactObjectHeaders
it can happen that the memory right after the header is not mapped and we crash.The fix is to add a
CheckCastPPNode
to propagate the information that the operand is an array and prevent the load from floating.Thanks to @shipilev for identifying the root cause!
I was able to reliably reproduce the issue with
compiler/arraycopy/TestArrayCopyNoInit.java
and-XX:-UseTLAB -XX:+UnlockExperimentalVMOptions -XX:+UseCompactObjectHeaders
on Linux AArch64 and verified that the fix solves the problem.Best regards,
Tobias
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/22967/head:pull/22967
$ git checkout pull/22967
Update a local copy of the PR:
$ git checkout pull/22967
$ git pull https://git.openjdk.org/jdk.git pull/22967/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 22967
View PR using the GUI difftool:
$ git pr show -t 22967
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/22967.diff
Using Webrev
Link to Webrev Comment