question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to find lambda code at runtime using CFR?

See original GitHub issue

Hi,

first time user of this CFR library…

I have a problem where at runtime, I want to know the actual code of a lambda. An example:

Let’s say we have the following code:

private Worker worker = new Worker();

public void runLambda() {
    myLibrary.doWork(() -> worker.doWork());
}

I’m interested in finding out this peace of code: worker.doWork() at runtime (e.g. users will use my library, use some lambda’s, and I want to find out what lambda they passed to my library). Since the lambda they pass is Serializable, I already know the implMethodName, e.g.: lambda$runLambda$81c80a4a$1 using SerializedLambda… but how to then get access to the actual method within the lambda?

I know it is possible using CFR (debugged through it quite a lot to find a hint on how to do this) but in my opinion, I did not find a good way to do so.

Thanks in advance, Ronald

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rdehuysscommented, Feb 25, 2020

Well, you guys are correct…

The thing I need it for is as follows (only Java 8 and higher support is needed): In case of my first example: I want to catch the lambda, find the actual code inside (in this worker.doWork()), serialize it, send it to another physical machine where it then gets executed without the actual context (so, no use of getImplMethodName as this only specifies the context, the surrounding class could even not be available) but with the Worker class available.

Not a trivial task, I know. But thanks to @Col-E , I have some new insights.

Thank you all for the excellent support!

1reaction
leibnitz27commented, Feb 25, 2020

Ok - first - this really isn’t a CFR thing 😉

CFR is entirely about NOT using reflection, or any other such thing, or relying on the vagiaries of a particular JVM implementation - it’s about static analysis of bytecode. I strongly agree with @Lanchon

To state the obvious - a functional interface implementation may be a lambda, or it may be… any old thing. I assume you’ve already covered the non-lambda case.

I can’t stress enough that if you rely on this sort of thing in production code, you’re going to be basically buggered.

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;

public class HackedName {
    interface Foo extends Serializable {
        void accept(int i);
    }

    private static void doFoo(Foo f) throws Exception {
        Method m = f.getClass().getDeclaredMethod("writeReplace");
        m.setAccessible(true);
        SerializedLambda sl = (SerializedLambda) AccessController.doPrivileged((PrivilegedExceptionAction<Method>) () -> m).invoke(f);
        System.out.println("This lambda came from " + sl.getImplClass() + "::" + sl.getImplMethodName());
    }

    public static void main(String...args) throws Exception {
        doFoo(x -> { System.out.println(" first, "); System.out.println(x); });
        doFoo(x -> { System.out.println(" then, "); System.out.println(x); });
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom AWS Lambda runtimes
You can implement an AWS Lambda runtime in any programming language. ... To get started with custom runtimes, see Tutorial – Publishing a...
Read more >
Where can I find the source code for AWS Lambda's ...
You can find the full list in the AWS documentation: Defined runtime environment variables. Every Lambda has a environment variable called ...
Read more >
AWS Lambda Interview Questions and Answers
In this article, I'll go over some of the most commonly asked questions that come up in interviews about AWS Lambda.
Read more >
Deep Dive Into Lambda Layers and the Lambda Runtime API
To learn more, please visit: https://aws.amazon.com/ lambda / In November, AWS Lambda introduced Lambda Layers and the Lambda Runtime API.
Read more >
Lambda triggered by SQS question · Discussion #400 - GitHub
In GO for example, I'm using the sqsEvent type, but could not find an equivalent for Rust. Could someone point me in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found