raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.31k stars 808 forks source link

Redefinition fails with "java.lang.IllegalStateException: Illegal parameter name" #1709

Closed mgroth0 closed 2 months ago

mgroth0 commented 2 months ago

I had a working redefinition of org.gradle.configurationcache.DefaultConfigurationCache using the gradle 8.8 API. This fails at runtime with gradle 8.9 (the class was renamed to org.gradle.internal.cc.impl.DefaultConfigurationCache in 8.9).

Reproducer below

Stack trace

Exception in thread "main" java.lang.IllegalStateException: Illegal parameter name of org.gradle.internal.cc.impl.DefaultConfigurationCache <this> for private static java.lang.Object org.gradle.internal.cc.impl.DefaultConfigurationCache.getStore$delegate(org.gradle.internal.cc.impl.DefaultConfigurationCache)
    at net.bytebuddy.dynamic.scaffold.InstrumentedType$Default.validated(InstrumentedType.java:1769)
    at net.bytebuddy.dynamic.scaffold.MethodRegistry$Default.prepare(MethodRegistry.java:519)
    at net.bytebuddy.dynamic.scaffold.inline.RedefinitionDynamicTypeBuilder.toTypeWriter(RedefinitionDynamicTypeBuilder.java:213)
    at net.bytebuddy.dynamic.scaffold.inline.AbstractInliningDynamicTypeBuilder.toTypeWriter(AbstractInliningDynamicTypeBuilder.java:127)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$UsingTypeWriter.make(DynamicType.java:4057)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:3741)
mgroth0 commented 2 months ago

https://github.com/mgroth0/byte-buddy-1709

In order to reproduce, edit gradle-wrapper.properties to specify the gradle version as 8.8 and execute ./gradlew run, then change the gradle version to 8.9 and run again. You should see a no error in 8.8, and an error in 8.9.

raphw commented 2 months ago

You can disable validation by setting a custom entry point in byteBuddy { }. Disable validation here.

mgroth0 commented 2 months ago

Thanks @raphw . I disabled validation and this seemed to prevent the error.

A couple notes

raphw commented 2 months ago

The closer you come to current versions of Gradle and Groovy or Kotlin, the less this is an issue. In the older versions, a lot of metadata in these classes is however corrupt, and since this can affect the outcome of instrumentation, this default is chosen.

mgroth0 commented 2 months ago

Oh that's interesting. But you realize that it was the other way around here, right? Everything worked fine up to and including Gradle 8.8. Broke in Gradle 8.9.

raphw commented 2 months ago

There is of course regressions. The idea is that you cannot call a parameter of a method this what is of course correct. Ideally, it captures only errors that are introduced by the user of Byte Buddy, but it does capture retained errors.