[code-reflection] RFR: Issues with captures in quotable lambdas
Maurizio Cimadamore
mcimadamore at openjdk.org
Wed Apr 17 12:26:13 UTC 2024
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.
-------------
Commit messages:
- Initial push
Changes: https://git.openjdk.org/babylon/pull/53/files
Webrev: https://webrevs.openjdk.org/?repo=babylon&pr=53&range=00
Stats: 21 lines in 3 files changed: 19 ins; 0 del; 2 mod
Patch: https://git.openjdk.org/babylon/pull/53.diff
Fetch: git fetch https://git.openjdk.org/babylon.git pull/53/head:pull/53
PR: https://git.openjdk.org/babylon/pull/53
More information about the babylon-dev
mailing list