aws / aws-lambda-java-libs

Official mirror for interface definitions and helper classes for Java code running on the AWS Lambda platform.
https://aws.amazon.com/lambda/
Apache License 2.0
521 stars 231 forks source link

Lambda RIC use of reflection prevents simple native image compilation (with graale-vm native-image) and cause Error at run #205

Open fafriat opened 3 years ago

fafriat commented 3 years ago

This is the error I got when running the executable generated from native-image:

./hello-lambda-1.0-SNAPSHOT example.App::sayHello

Exception in thread "main" java.lang.Error: com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil$ReflectException: java.lang.NoSuchFieldException: logger
        at com.amazonaws.services.lambda.runtime.api.client.AWSLambda.startRuntime(AWSLambda.java:195)
        at com.amazonaws.services.lambda.runtime.api.client.AWSLambda.main(AWSLambda.java:188)
Caused by: com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil$ReflectException: java.lang.NoSuchFieldException: logger
        at com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil.setStaticField(ReflectUtil.java:660)
        at com.amazonaws.services.lambda.runtime.api.client.AWSLambda.setupRuntimeLogger(AWSLambda.java:153)
        at com.amazonaws.services.lambda.runtime.api.client.AWSLambda.startRuntime(AWSLambda.java:204)
        at com.amazonaws.services.lambda.runtime.api.client.AWSLambda.startRuntime(AWSLambda.java:193)
        ... 1 more
Caused by: java.lang.NoSuchFieldException: logger
        at java.lang.Class.getDeclaredField(DynamicHub.java:2411)
        at com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil.setStaticField(ReflectUtil.java:654)
        ... 4 more

And effectively AWSLambda code contains this:

    public static void setupRuntimeLogger(LambdaLogger lambdaLogger)
            throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
        ReflectUtil.setStaticField(
                Class.forName("com.amazonaws.services.lambda.runtime.LambdaRuntime"),
                "logger",
                true,
                lambdaLogger
        );
    }
cameronbjork-tbg commented 1 year ago

Hello, was this issue ever resolved w/ LambdaLogger? Thanks!

MaximMaltsev94 commented 2 months ago

Meanwhile there is no fix for the issue, following approach helped me resolve my issue. Append following content to reflect-config.json file and compile native with -H:ReflectionConfigurationFiles=reflect-config.json

reflect-config.json:

[
  {
    "name":"com.amazonaws.services.lambda.runtime.LambdaRuntime",
    "fields":[{"name":"logger"}]
  },
  {
    "name":"jdk.internal.module.IllegalAccessLogger"
  },
  {
    "name":"sun.misc.Unsafe",
    "fields":[{"name":"theUnsafe"}]
  }
]