Skip to content

Fix vtable slot bounds checking for refined receiver types - #24069

Merged
mpirvu merged 1 commit into
eclipse-openj9:masterfrom
nbhuiyan:fix-24018
Jun 8, 2026
Merged

mpirvu merged 1 commit into
eclipse-openj9:masterfrom
nbhuiyan:fix-24018

Conversation

@nbhuiyan

@nbhuiyan nbhuiyan commented Jun 3, 2026

Copy link
Copy Markdown
Member

When receiver types are refined during compilation (e.g., from an interface to a concrete implementor), the vtable slot offset computed against the original resolved class may fall outside the bounds of the refined class's vtable. This can lead to reading arbitrary memory values as J9Method pointers, causing crashes or incorrect devirtualization.

This commit adds vtable slot validation in three critical locations:

  1. TR_PersistentCHTable::findSingleAbstractImplementer
    • Added bounds check
  2. TR_J9VMBase::getResolvedVirtualMethod
    • Added bounds check
  3. TR_J9VirtualCallSite::findCallSiteForAbstractClass
    • Added name/signature verification

This fixes the root cause of a class of failures observed with invalid vtable slot indices being used post-refinement, and we run into such issues in the scenario described below:

  • A virtual call is resolved against one class (e.g., interface implementor A)
  • The receiver type is refined to a different class (e.g., implementor B)
  • The vtable slot from class A is used to index into class B's vtable
  • Class B's vtable may be smaller or have different layout, causing OOB access

Issue: #24018, #24009

@nbhuiyan

nbhuiyan commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@vijaysun-omr @mpirvu requesting review

@nbhuiyan
nbhuiyan force-pushed the fix-24018 branch 3 times, most recently from ab04eba to cda5193 Compare June 3, 2026 22:23
When receiver types are refined during compilation (e.g., from an interface
to a concrete implementor), the vtable slot offset computed against the
original resolved class may fall outside the bounds of the refined class's
vtable. This can lead to reading arbitrary memory values as J9Method pointers,
causing crashes or incorrect devirtualization.

This commit adds vtable slot validation in three critical locations:

1. TR_PersistentCHTable::findSingleAbstractImplementer
   - Added bounds check
2. TR_J9VMBase::getResolvedVirtualMethod
   - Added bounds check
3. TR_J9VirtualCallSite::findCallSiteForAbstractClass
   - Added name/signature verification

This fixes the root cause of a class of failures observed with invalid
vtable slot indices being used post-refinement, and we run into such issues
in the scenario described below:
- A virtual call is resolved against one class (e.g., interface implementor A)
- The receiver type is refined to a different class (e.g., implementor B)
- The vtable slot from class A is used to index into class B's vtable
- Class B's vtable may be smaller or have different layout, causing OOB access

Signed-off-by: Nazim Bhuiyan <nubhuiyan@ibm.com>

@mpirvu mpirvu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. I have one small question (inline).

@mpirvu mpirvu self-assigned this Jun 4, 2026
@mpirvu

mpirvu commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

jenkins test sanity,extended all jdk25

@vijaysun-omr

vijaysun-omr commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

A virtual call is resolved against one class (e.g., interface implementor A)

Just so I understand this comment, the case being talked about here is that the call was an interface call I.foo originally and was changed to a virtual call A,foo ? Presumably by value propagation or some other place where some non-fixed type was in hand ?

@nbhuiyan

nbhuiyan commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

Just so I understand this comment, the case being talked about here is that the call was an interface call I.foo originally and was changed to a virtual call A,foo ? Presumably by value propagation or some other place where some non-fixed type was in hand ?

It was never an invokeinterface that got converted. It is an invokevirtual call from the start, whose target is an interface method I.foo() that the implementor class A inherits without overriding. ILGen's genInvokeVirtual resolves that to a virtual method symref whose vft slot is foo's vtable index in A (pointing to A.foo()). IAP + arg-info propagation as part of peeking ILGen narrows the call's receiver to a different implementor B, which is a sibling of A under I, not a subtype of A, and therefore, a different vtable layout, and if A's slot is reused to index into B's vtable, we can run into the crash this PR aims to address.

@nbhuiyan

nbhuiyan commented Jun 5, 2026 •

Copy link
Copy Markdown
Member Author

jdk25 ppc64le linux sanity.openjdk failure:

15:44:10  ACTION: junit -- Error. "junit" action timed out with a timeout of 960 seconds on agent 24

It is a known issue: #23435

jdk25 x86-64 linux sanity.functional failure:

16:22:09  ////////////////////////////////////////////////////////////////////////
16:22:09  //// can't bind server address: Address already in use
16:22:09  //// Failed to open server socket on port 55496
16:22:09  ////////////////////////////////////////////////////////////////////////
...
16:22:09  FAILED: testServerUnreachableForAWhile
16:22:09  java.lang.AssertionError: Failed to properly start server, it terminated prematurely with exit value: 1

A similar known issue: #14706

@vijaysun-omr

vijaysun-omr commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Still trying to understand here...

It was never an invokeinterface that got converted. It is an invokevirtual call from the start, whose target is an interface method I.foo() that the implementor class A inherits without overriding. ILGen's genInvokeVirtual resolves that to a virtual method symref whose vft slot is foo's vtable index in A (pointing to A.foo()).

If the bytecodes said invokevirtual I.foo then where did A enter the picture from at all ? i.e. how does genInvokeVirtual get its hands on A (or A.foo) for this call if it is not mentioned in the bytecode for the call ?

@nbhuiyan

nbhuiyan commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

The bytecode says invokevirtual A.foo, not I.foo. genInvokeVirtual obtains A.foo from the methodref, and gets a vft index that's meant for A.foo(). I believe I.foo() enters the picture during resolution. I am not very familiar with the exact mechanism of that, but from what I could tell, since A did not implement foo(), J9Method obtained from cp was pointing to I.foo() the symref ends up as I.foo() virtual method with an offset valid for A.foo(). This is how that appears in the JIT log (in the problem I was investigating):

#454:  org/jetbrains/kotlin/ir/declarations/IrDeclarationWithName.getName()Lorg/jetbrains/kotlin/name/Name;[ virtual Method -464] (Interface class) [flags 0x500 0x0 ] [0x796d113b4030] (NoType)

Also see the following:

// Refine receiver class based on CP class
// When we have an invokevirtual on an abstract method defined in an interface class,
// the call site's class will be more concrete than class of method.
// This happens when an abstract class implements an interface class without providing
// implementation for the given method, and the call site is refering to the method of
// the abstract class, the cp entry of the method ref will be resolved to j9method of
// the interface class. However, the class ref from cp will be resolved to the abstract
// class, which is more concrete

@nbhuiyan

nbhuiyan commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

jdk25 x86-64 mac sanity.openjdk was aborted due to timeout running test jdk_security4_1. I am not sure if that is a known issue.

@mpirvu

mpirvu commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

I started a 1x grinder for mac just for the failing testList_0 here: https://openj9-jenkins.osuosl.org/job/Grinder/5220/

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Thanks, that comment you pasted seems to describe something very similar to your case.

Having said that, it seems that the only reason I.foo enters the picture here is that it is where the method is actually implemented, i.e. not in A whereas the invokevirtual was referring to A.foo In such a case I would have expected us to never get a B type at the invokevirtual even via profiling since according the bytecodes we should be guaranteed only type values that extend A. Do you know if it is value profiling that introduces B (sibling of, and so not a subclass of A) into the mix ?

@nbhuiyan

nbhuiyan commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

Do you know if it is value profiling that introduces B (sibling of, and so not a subclass of A) into the mix ?

Value profiling is not why B gets introduced. It happens during arg info propagation during peeking ILGen. From what I can tell from the logs, what we have in the method body is something like if (x is A) { (x as A).foo() }, where the declared type of x is a common superclass of A and B. During peeking ILGen, the type of x can get propagated as B (due to arg info propagation) even though that path would not actually be reached at runtime.

@vijaysun-omr

vijaysun-omr commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Okay, interesting (and subtle) issue with arg propagation. I wonder if another fix could have been if we did a check on the method in the call I.foo and if the class involved was an interface (as it would be in that case) then we decline to index into the vtable with the index on the call (since it could be for the wrong class as you found). Would this be an alternate fix instead of bounds checking ?

Just out of curiosity, could nothing go wrong if B vtable was big enough to handle the access, i.e. could we not get to some unrelated method's slot and while that won't crash, it could cause some other subtle bug ?

@nbhuiyan

nbhuiyan commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

Okay, interesting (and subtle) issue with arg propagation. I wonder if another fix could have been if we did a check on the method in the call I.foo and if the class involved was an interface (as it would be in that case) then we decline to index into the vtable with the index on the call (since it could be for the wrong class as you found). Would this be an alternate fix instead of bounds checking ?

I suppose that would work too. However, I went with the bounds checking approach because I wanted to minimize the impact that the fix would have. It would terminate class walk when the vtable index is obviously wrong, while the otherwise correct vtable accesses would be still allowed. This seemed like the safer approach to me.

Just out of curiosity, could nothing go wrong if B vtable was big enough to handle the access, i.e. could we not get to some unrelated method's slot and while that won't crash, it could cause some other subtle bug ?

I did wonder this as well, which was the motivation behind the name and signature matching that's included in the fix.

@vijaysun-omr

vijaysun-omr commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Okay thanks, that clarifies the thought process much more. While the name/signature match help in being "more correct" (if there is such a thing), I wonder if the refined class (B in your case) could be completely unrelated to I and A if it is getting into some unreachable path as you were showing earlier. In such a case, even though the name and signature match, they aren't really talking about methods related through the class hierarchy in any way, i.e. they would be completely unrelated methods that happen to have the same name/signature. Maybe nothing can go wrong in even such a case, and maybe there is a plus (the chances of a problem are much lower than before) and a minus (the bug if it ever happens will be way subtler than even your existing bug). Do you see what I am thinking about ?

@nbhuiyan

nbhuiyan commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

Okay thanks, that clarifies the thought process much more. While the name/signature match help in being "more correct" (if there is such a thing), I wonder if the refined class (B in your case) could be completely unrelated to I and A if it is getting into some unreachable path as you were showing earlier. In such a case, even though the name and signature match, they aren't really talking about methods related through the class hierarchy in any way, i.e. they would be completely unrelated methods that happen to have the same name/signature. Maybe nothing can go wrong in even such a case, and maybe there is a plus (the chances of a problem are much lower than before) and a minus (the bug if it ever happens will be way subtler than even your existing bug). Do you see what I am thinking about ?

My understanding is that this sort of situation is only possible when the classes involved are all related in some way. The scenario you described, from what I understand, would mean that it's possible for I.foo to have its receiver refined to a completely incompatible type. I'm not sure if there is a way for that to happen.

If we somehow did end up in that situation, would peeking ILGen ending up with the wrong method in a not-taken path affect the outcome of the compilation? Perhaps we could design some tests to try and figure out what happens. It is possible that this sort of issue was happening for as long as peeking + IAP has been around, but hidden from us because the wrong vtable offset was within bounds of the unintended receiver's vtable, so flying completely under the radar.

@vijaysun-omr

vijaysun-omr commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

The reason I was thinking of the "completely incompatible type" case was from considering a scenario like this one that you mentioned earlier

what we have in the method body is something like if (x is A) { (x as A).foo() }, where the declared type of x is a common superclass of A and B. During peeking ILGen, the type of x can get propagated as B (due to arg info propagation) even though that path would not actually be reached at runtime.

In such a scenario, is it not possible that one has some if (x is C) { (x as C).foo() } where C is a completely incompatible class with the type B that is coming in through arg propagation, i.e. the Java code is free to do this kind of checking and dispatch. Of course, for type B coming in, this path won't be reachable since it is incompatible with C but that proibably won't prevent the analysis of the code path from happening and potential issues like the one you are looking at from arising (except for the fact that the classes would be completely incompatible in this case).

An instanceof check at compile time in the case of any call that has an interface I.foo as the resolved method symbol to ensure that the type B that is coming in is compatible may help prevent all these kinds of issues (?) I agree with your comments about the fact that maybe the scenario under discussion was possible (though still not completely right) all along.

@nbhuiyan

nbhuiyan commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

@vijaysun-omr I agree that this problem is possible. This will require some additional investigation that goes beyond what this fix is addressing, i.e., validation of accesses into the vtable entries, where the classes involved are related, preventing crashes during JIT compilation. Do you think compatibility checks can be worked on in a follow-up PR, or does it make more sense to be part of this one?

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Thanks Nazim. I don't have an issue with that follow-on PR potentially changing the logic delivered in this PR, e.g. maybe an instanceof test at the right place(s) may eliminate the need for a bounds check. But we can cross that bridge when we get to it, i.e. this change itself is fixing a set of problems I can approve it with the understanding that things will be re-visited as part of the follow-on work.

@vijaysun-omr

Copy link
Copy Markdown
Contributor

I have approved this PR, thanks

@mpirvu

mpirvu commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

The grinder on mac was successful. Since none of the test failures are related to this PR I am going to merge it.

@mpirvu
mpirvu merged commit aeb79a4 into eclipse-openj9:master Jun 8, 2026
32 of 36 checks passed
@mpirvu

mpirvu commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

@nbhuiyan Please port this PR to 0.60.0 release branch. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants