[code-reflection] Integrated: Issues with captures in quotable lambdas

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Apr 17 15:58:12 UTC 2024


On Wed, 17 Apr 2024 12:20:48 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> Two issues were found in the code for capturing values in quotable lambdas:
> 
> * If the captured value is a constant (e.g. a local variable that is declared as `final`), we end up generating bad code. The problem is that constant variables are not normally captured by `LambdaToMethod` and `Lower`, on the basis that they will "disappear" from the bytecode, and replaced from their constant value. But in the code model IR, such variables are very much real, so the generated `Quoted` op will feature them as captured values nevertheless. This issue is easily fixed by tweaking the code which creates the captured values in `ReflectMethods` so that, instead of generating a reference to a variable that no longer exists, we generate a literal (the constant value we want).
> * There's also a latent issue in `InnerClassLambdaMetafactory.ForwardingMethodGenerator`: before calling the lambda method generated by javac, this generator will attempt to load all the captured fields in the generated class. But, for quoted lambdas, some fields are used for generating the corresponding quoted ops and should not be used to invoke the lambda bytecode. Surprisingly, in most cases, the verifier does not detect the additional field loads, as long as the type match (which is always the case if the quoted capture list is identical to the bytecode capture list). But in some cases (e.g. when `final` are involved), the two lists might be different, and it is possible for the verifier to detect type mismatches. This issue can be fixed by making sure that `ForwardingGenerator` only loads the captured fields that matters.

This pull request has now been integrated.

Changeset: 0f1e4e1c
Author:    Maurizio Cimadamore <mcimadamore at openjdk.org>
URL:       https://git.openjdk.org/babylon/commit/0f1e4e1c0c3c44ac7f54814e4ebc81273f946c04
Stats:     21 lines in 3 files changed: 19 ins; 0 del; 2 mod

Issues with captures in quotable lambdas

Reviewed-by: psandoz

-------------

PR: https://git.openjdk.org/babylon/pull/53


More information about the babylon-dev mailing list