proguard icon indicating copy to clipboard operation
proguard copied to clipboard

`Invalid instruction offset` when trying to optimise ambiguous method calls disambiguated with casting

Open soundasleep opened this issue 8 months ago • 2 comments

With Proguard 7.3+ I was trying to optimise essentially the following code:

public void load(CustomProperties prop) {
  this.field = prop.getValue("field", (CustomValue) null); // doesn't appear to work!
}

with CustomProperties having two methods with the same name, but different parameters:

public CustomValue getValue(String key, CustomValue def) {
  if (containsKey(key)) {
    return requireValue(key);
  } else {
    return def;
  }
}

public CustomValue getValue(String key, String def) {
  if (containsKey(key)) {
    return requireValue(key);
  } else {
    return new CustomValue(def);
  }
}

Without the (CustomValue) null cast in function(), the code would not compile, as the method call was ambiguous.

However trying to optimise with Proguard out of the box was failing with:

Unexpected error while inlining method:
  Target class   = [org/.../CustomClassName]
  Target method  = [load(...)V]
  Exception      = [java.lang.IllegalArgumentException] (Invalid instruction offset [691] in code fragment at level 0)
java.lang.IllegalArgumentException: Invalid instruction offset [691] in code fragment at level 0
        at proguard.classfile.editor.CodeAttributeComposer.newInstructionOffset(CodeAttributeComposer.java:1159) ~[proguard-core-9.0.3.jar:9.0.3]
        at proguard.classfile.editor.CodeAttributeComposer.visitTypeArgumentTargetInfo(CodeAttributeComposer.java:1069) ~[proguard-core-9.0.3.jar:9.0.3]
        at proguard.classfile.attribute.annotation.target.TypeArgumentTargetInfo.accept(TypeArgumentTargetInfo.java:80) ~[proguard-core-9.0.3.jar:9.0.3]
        at proguard.classfile.attribute.annotation.TypeAnnotation.targetInfoAccept(TypeAnnotation.java:101) ~[proguard-core-9.0.3.jar:9.0.3]
        at proguard.classfile.editor.CodeAttributeComposer.visitTypeAnnotation(CodeAttributeComposer.java:1037) ~[proguard-core-9.0.3.jar:9.0.3]

If instead I remove the public CustomValue getValue(String key, String def) method in CustomProperties, and remove the (CustomValue) null cast to disambiguate the method call - Proguard seems to work fine!

public void load(CustomProperties prop) {
  this.field = prop.getValue("field", null); // now works!
}

Tried with Gradle + Proguard 7.3, 7.5, 7.7.

Gradle:      7.3
Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17 (Oracle Corporation 17+35-2724)
OS:           Windows 10 10.0 amd64

soundasleep avatar May 12 '25 01:05 soundasleep

Yeah that is definitely weird. Can you first try the following workaround:

-optimizations !method/inlining/**

It should at least unblock this while disabling only the fault optimization.

Do you have a minimally reproducing sample to share? I tried to add the classes in a sample but it's processing correctly.

piazzesiNiccolo-GS avatar Aug 19 '25 07:08 piazzesiNiccolo-GS

I ended up using my workaround instead, so I don't have a reproducible setup anymore - but I'll see if it turns up again!

soundasleep avatar Aug 30 '25 04:08 soundasleep