Skip to content

Fix handling of interface methods in JIT linkToVirtual() - #13578

Merged
0xdaryl merged 6 commits into
eclipse-openj9:masterfrom
jdmpapin:linkToVirtual-interface
Oct 15, 2021
Merged

0xdaryl merged 6 commits into
eclipse-openj9:masterfrom
jdmpapin:linkToVirtual-interface

Conversation

@jdmpapin

Copy link
Copy Markdown
Contributor
  • Return uintptr_t from vTableOrITableIndexFromMemberName()
  • Avoid refining linkToVirtual to interface method in InterpreterEmulator
  • Avoid refining linkToVirtual to interface method in MH transformer
  • Clean up dispatchVitual() VFT slot-related subtrees
  • Look up interface method VFT offset from iTable in JIT linkToVirtual()

Please see the individual commits for more detail

This requires eclipse-omr/omr#6186

Fixes #13472

@jdmpapin
jdmpapin force-pushed the linkToVirtual-interface branch from f52699d to ec77938 Compare September 27, 2021 18:30
@jdmpapin

Copy link
Copy Markdown
Contributor Author

Pushed a small update to the commit messages, rephrasing from "iTable offset" to "iTable index," since a vTableOrITableIndexFromMemberName() tagged with J9_JNI_MID_INTERFACE is (once the flag is cleared) an index, not an offset. (This is as opposed to regular virtual methods, for which the result is an interpreter VFT offset from the start of the J9Class)

@0xdaryl

0xdaryl commented Sep 28, 2021

Copy link
Copy Markdown
Contributor

Jenkins test sanity,extended all jdk17 depends eclipse-omr/omr#6186

Pre-emptively launching tests before review is finished.

@0xdaryl

0xdaryl commented Sep 28, 2021

Copy link
Copy Markdown
Contributor

@gacholio @tajila : would you mind reviewing the code for the new helper in cnathelp.cpp please?

Comment thread runtime/codert_vm/cnathelp.cpp Resolved
@0xdaryl

0xdaryl commented Sep 28, 2021

Copy link
Copy Markdown
Contributor

Windows CI failure may be #10732.

@jdmpapin

Copy link
Copy Markdown
Contributor Author

Pushed an update to split the helper into two. I didn't bother to factor out common logic because the call to convertITableOffsetToVTableOffset() already covers most of it. At least, that's true if we wouldn't factor out the helper linkage code, i.e. OLD_JIT_HELPER_PROLOGUE, DECLARE_JIT_PARM, JIT_RETURN_UDATA, and I imagine that we wouldn't

@jdmpapin
jdmpapin marked this pull request as draft October 13, 2021 21:42
@jdmpapin

Copy link
Copy Markdown
Contributor Author

Converted to draft while I re-test

The result is the value of J9JNIMethodID::vTableIndex, which is UDATA.
The high bit of this value is a flag that distinguishes VFT offsets from
iTable indices. The previous return type (int32_t) required this value
to be truncated on 64-bit platforms, losing the value of the flag,
leaving callers with no way to distinguish the cases.
vTableOrITableIndexFromMemberName() returns either a VFT offset or an
iTable index tagged with J9_JNI_MID_INTERFACE. In the latter case,
treating it as a VFT offset and generating a virtual call is incorrect.
We could determine the interface callee and replace linkToVirtual() with
an interface call, but interface call sites currently require a CP
index in order to find call targets.

Additionally, vTableOrITableIndexFromMemberName() can return zero to
indicate that the method should be dispached directly. This case is now
handled properly by setting isIndirectCall to false, which results in
the creation of a direct call site. Previously we would get a virtual
call site, which would find the correct (nonoverridden) target method,
but that method would not be inlined with the correct guard (in
particular, an HCR guard in HCR mode, which is the default).
vTableOrITableIndexFromMemberName() returns either a VFT offset or an
iTable index tagged with J9_JNI_MID_INTERFACE. In the latter case,
treating it as a VFT offset and generating a virtual call is incorrect.
In case an attempt to do so results in a positive JIT VFT offset and
consequently a direct call, that is also incorrect. We could determine
the interface callee and replace linkToVirtual() with an interface call,
but interface call nodes must currently be unresolved. Code generation
requires a CP index in the dispatch.
...created by recognized call transformer for linkToVirtual().

These subtrees were previously of the following form:

    calli com/ibm/jit/JITHelpers.dispatchVirtual(JJLjava/lang/Object;...)
      lloadi <generic int shadow>
        l2a (**)
          lsub (**)
            aloadi <vft-symbol>
              aload receiverTemp
n1n         lsub (*)
              lload vftOffsetTemp (**)
              lconst 376 (*)
      lmul (**)
n1n     ==>lsub (*)
        lconst -1 (*)
      ...args

There are a few problems with the above trees:

- They do integer subtraction on an address when they should instead use
  an address offset operation

- Nodes marked (**) vary in type according to bitness, but nodes marked
  (*) are always 64-bit, which would result in ill-typed trees on 32-bit

- Tree simplification rewrites (mul (sub a (const k)) (const -1)) into
  (sub (mul a (const -1)) (const -k)), which causes the compiler to
  generate two separate subtractions

These subtrees are changed to use aladd or aiadd, perform the
subtraction labelled n1n in the opposite direction (since both uses
now actually need the negative of the old result), and to use
pointer-sized integers throughout:

    calli com/ibm/jit/JITHelpers.dispatchVirtual(JJLjava/lang/Object;...)
      lloadi <generic int shadow>
        aladd (**)
          aloadi <vft-symbol>
            aload receiverTemp
n1n       lsub (**)
            lconst 376 (**)
            lload vftOffsetTemp (**)
n1n   ==>lsub (**)
      ...args

These subtrees are well-typed on 64-bit, and they allow the commoning to
survive all the way to code generation. They should also be well-typed
on 32-bit, but this has not yet been tested because only Java 8 still
supports 32-bit builds, but Java 8 doesn't have support yet for OpenJDK
method handles.

Additionally, within recognized method transformer's linkToVirtual()
transformation, a few variables are renamed for clarity:
- mhSymRef -> receiverSymRef
- xcmpne -> ifxcmpne
- vtableOffset -> jitVFTOffset (negated)
- jittedMethodAddressNode -> jitVFTSlotPtr
- jittedMethodAddressLoadNode -> jittedMethodEntryPoint
@jdmpapin
jdmpapin force-pushed the linkToVirtual-interface branch from 028e263 to 86db6e5 Compare October 13, 2021 22:58
@jdmpapin

Copy link
Copy Markdown
Contributor Author

Rebased to bring the branch up to date

@0xdaryl

0xdaryl commented Oct 14, 2021

Copy link
Copy Markdown
Contributor

Jenkins test sanity,extended all jdk17 depends eclipse-omr/omr#6186

@jdmpapin

jdmpapin commented Oct 14, 2021 •

Copy link
Copy Markdown
Contributor Author

@gacholio, would you mind reviewing the new helpers again? (noting #13578 (comment))

Comment thread runtime/codert_vm/cnathelp.cpp Outdated

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.

Odd spacing and lack of brackets. Suggest:

UDATA iTableOffset = sizeof(struct J9ITable) + (iTableIndex * sizeof(UDATA));

@gacholio gacholio 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.

Looks good (one trivial formatting change).

@jdmpapin

Copy link
Copy Markdown
Contributor Author

@0xdaryl, the test failures appear to be just "java.lang.OutOfMemoryError: Java heap space", which I doubt is related to my changes. What do you think? I'll push the formatting change once you've had a chance to take a look

@0xdaryl

0xdaryl commented Oct 14, 2021

Copy link
Copy Markdown
Contributor

I tend to agree. However, it is suspicious that similar jobs on different builds (Mac vs Linux) fail in a similar manner.

Can you make the formatting change and push it? I'll re-run just these two failing jobs and make a determination from that. All other testing passed successfully.

@0xdaryl 0xdaryl self-assigned this Oct 14, 2021
@0xdaryl

0xdaryl commented Oct 14, 2021

Copy link
Copy Markdown
Contributor

The OOM failures may be related to #13584.

...as transformed by recognized call transformer. Previously, the
generated code would ignore the J9_JNI_MID_INTERFACE flag and treat a
tagged iTable index instead as a VFT offset, resulting in a segfault.

The generated code will now detect this flag and, if it is set, call a
new VM helper jitLookupDynamicInterfaceMethod() to find the VFT offset.
This behaves in the same way as jitLookupDynamicInterfaceMethod(),
except that it checks to see whether the selected callee is public,
throwing IllegalAccessError if not.

This should make it straightforward to transform linkToInterface() in
recognized call transformer in almost exactly the same way as we
currently do for linkToVirtual().
@pshipton

Copy link
Copy Markdown
Member

The OOM failures may be related to #13584.

I agree. This failure is specific to the head stream. If there is a concern, a PR can be created against 0.29 and PR tested against this branch, but I don't think it's necessary.

@jdmpapin
jdmpapin force-pushed the linkToVirtual-interface branch from 86db6e5 to 61fd734 Compare October 14, 2021 19:38
@jdmpapin

Copy link
Copy Markdown
Contributor Author

Updated the formatting of the iTableOffset expression as requested, removed an extra space I had introduced into a sizeof elsewhere, and deleted a stale comment

@0xdaryl

0xdaryl commented Oct 14, 2021

Copy link
Copy Markdown
Contributor

Given @pshipton's remarks above I won't re-run full CI testing for a simple formatting change.

@0xdaryl

0xdaryl commented Oct 14, 2021

Copy link
Copy Markdown
Contributor

Dependent upstream PR has merged.

@jdmpapin

Copy link
Copy Markdown
Contributor Author

OK, I'm just waiting for my own builds to complete before I take this back out of draft status. (They were supposed to run after I rebased last night, but they didn't and I had to launch new ones this morning)

I'm not expecting the code here to change much (or at all, really) as a result of those builds though, so if any more review is needed in the meantime, I don't think it has to wait

@0xdaryl

0xdaryl commented Oct 14, 2021

Copy link
Copy Markdown
Contributor

I am satisfied with the changes. Just un-draft it when you're comfortable with your testing.

@jdmpapin
jdmpapin marked this pull request as ready for review October 15, 2021 03:19
@0xdaryl

0xdaryl commented Oct 15, 2021

Copy link
Copy Markdown
Contributor

Previous CI testing was successful prior to the last push of a simple formatting change. Merging.

@0xdaryl
0xdaryl merged commit 116a6c8 into eclipse-openj9:master Oct 15, 2021
@knn-k

knn-k commented Oct 15, 2021 •

Copy link
Copy Markdown
Contributor

This change causes a build break with AArch64 Linux. -- Correction: Not only on AArch64 Linux.

@knn-k

knn-k commented Oct 15, 2021

Copy link
Copy Markdown
Contributor

The build break seems to be caused by Helpers.inc in the OMR project.
The change in eclipse-omr/omr#6186 is not reflected to https://github.com/eclipse-openj9/openj9-omr/blob/openj9/compiler/runtime/Helpers.inc yet.

@jdmpapin

Copy link
Copy Markdown
Contributor Author

Ah, that would do it

@pshipton

Copy link
Copy Markdown
Member

I'm going to revert this temporarily until we get OMR promoted. Artifactory is down atm, not sure when we'll have the OMR promotion build running again.

@pshipton

Copy link
Copy Markdown
Member

Merged again via #13716

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NPE running DefaultStaticInvokeTest

6 participants