Skip to content

8353487: JShell LocalExecutionControl should allow decorating the execution task #24398

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

archiecobbs
Copy link
Contributor

@archiecobbs archiecobbs commented Apr 2, 2025

This PR adds the ability for subclasses of JShell's LocalExecutionControl to configure thread-local context in the execution thread that snippets execute in. Please see JDK-8353487 for more details & rationale.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Change requires CSR request JDK-8353590 to be approved
  • Commit message must refer to an issue

Issues

  • JDK-8353487: JShell LocalExecutionControl should allow decorating the execution task (Enhancement - P4)
  • JDK-8353590: JShell LocalExecutionControl should allow decorating the execution task (CSR)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24398

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@archiecobbs
Copy link
Contributor Author

/csr

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 2, 2025

👋 Welcome back acobbs! 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 Apr 2, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added csr Pull request needs approved CSR before integration rfr Pull request is ready for review labels Apr 2, 2025
@openjdk
Copy link

openjdk bot commented Apr 2, 2025

@archiecobbs an approved CSR request is already required for this pull request.

@openjdk
Copy link

openjdk bot commented Apr 2, 2025

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

  • kulla

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.

@mlbridge
Copy link

mlbridge bot commented Apr 2, 2025

Webrevs

@lahodaj
Copy link
Contributor

lahodaj commented Apr 4, 2025

I wonder how generic are the snippets used - for some types of snippets, it might be possible to put some code around the original snippet directly in the snippet. That would alleviate the need to add a new API. But presumably it would only work for some types of snippets.

For the API itself - just an idea, would having a method doInvoke along these lines work:

public Object doInvoke(Method m) throws Exception {
    return doitMethod.invoke(null, new Object[0]);
}

?

@archiecobbs
Copy link
Contributor Author

I wonder how generic are the snippets used - for some types of snippets, it might be possible to put some code around the original snippet directly in the snippet. That would alleviate the need to add a new API. But presumably it would only work for some types of snippets.

Yes, sort of - it's already possible to modify the snippet bytecode itself of course because the ExecutionProvider is who loads it. That works for things like imposing time limits, etc., but it doesn't work so well for "wrapping" the execution because (a) you have to know what the secret method is that is going to be invoked (not hard - it's called do_it(), but kludgey), and (b) you would have to insert an INVOKESTATIC that calls into your ExecutionProvider, which means your ExecutionProvider must be available via the class loader that the snippet uses, and this is not guaranteed.

For the API itself - just an idea, would having a method doInvoke along these lines work:

public Object doInvoke(Method m) throws Exception {
    return doitMethod.invoke(null, new Object[0]);
}

Yes that would work just as well. A third option would be to define a pair like executionStart() and executionFinish().

I think your idea of a doInvoke() is actually a bit simpler/better, so I've updated that in 8c47244 and also updated the CSR.

Thanks for taking a look.

@bridgekeeper
Copy link

bridgekeeper bot commented May 2, 2025

@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@xxDark
Copy link

xxDark commented May 19, 2025

import jdk.jshell.execution.LocalExecutionControl;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class LocalExecutionControlTest {

    public static void main(String[] args) throws Throwable {
        var control = new LocalExecutionControl((ClassLoader) null) {
            Object result;

            @Override
            protected Object doInvoke(Method method) throws IllegalAccessException, InvocationTargetException {
                return result = super.doInvoke(method);
            }
        };
        control.doInvoke(MethodHandles.class.getDeclaredMethod("lookup"));
        var lookup = (MethodHandles.Lookup) control.result;
        var unsafeClass = Class.forName("jdk.internal.misc.Unsafe");
        var unsafe = lookup.findStatic(unsafeClass, "getUnsafe", MethodType.methodType(unsafeClass)).invoke();
        var field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
        lookup = (MethodHandles.Lookup) lookup.findVirtual(unsafeClass, "getReference", MethodType.methodType(Object.class, Object.class, long.class))
                .invoke(
                        unsafe,
                        lookup.findVirtual(unsafeClass, "staticFieldBase", MethodType.methodType(Object.class, Field.class)).invoke(unsafe, field),
                        (long) lookup.findVirtual(unsafeClass, "staticFieldOffset", MethodType.methodType(long.class, Field.class)).invoke(unsafe, field)
                );
        System.out.println(lookup);
    }
}

exports jdk.internal.misc to
java.desktop,
java.logging,
java.management,
java.naming,
java.net.http,
java.rmi,
java.security.jgss,
jdk.attach,
jdk.charsets,
jdk.compiler,
jdk.crypto.cryptoki,
jdk.incubator.vector,
jdk.jfr,
jdk.jshell,
jdk.nio.mapmode,
jdk.unsupported,
jdk.internal.vm.ci,
jdk.graal.compiler;

Please reconsider use of reflection (or method handles)
I was looking into jshell before to achieve something like this using that exposes accessible java.lang.reflect.Method to user code, but it was not possible to get back the invocation result. With this PR, it is now possible.

@archiecobbs
Copy link
Contributor Author

that exposes accessible java.lang.reflect.Method to user code, but it was not possible to get back the invocation result. With this PR, it is now possible.

Good point. I believe my original patch which adds a protected Runnable decorateExecution(Runnable) method instead did not have this issue, so we could just revert to that approach.

Or there is the other approach (previously mentioned) which adds executionStart() and executionFinish() methods and should also be safe.

@lahodaj what's your opinion here?

Thanks.

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 16, 2025

@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@archiecobbs
Copy link
Contributor Author

/touch

@openjdk
Copy link

openjdk bot commented Jun 16, 2025

@archiecobbs The pull request is being re-evaluated and the inactivity timeout has been reset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
csr Pull request needs approved CSR before integration kulla [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants