From f24ca9da841eb51171c61b55223f582a85938731 Mon Sep 17 00:00:00 2001 From: Dennis Titze Date: Wed, 28 Oct 2026 15:23:14 +0100 Subject: [PATCH 1/4] Update stack/variable sizes only after instruction widening --- .../editor/CodeAttributeComposer.java | 10 +-- .../editor/CodeAttributeComposerTest.kt | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/base/src/main/java/proguard/classfile/editor/CodeAttributeComposer.java b/base/src/main/java/proguard/classfile/editor/CodeAttributeComposer.java index d4ffe2114..2f4f191b8 100644 --- a/base/src/main/java/proguard/classfile/editor/CodeAttributeComposer.java +++ b/base/src/main/java/proguard/classfile/editor/CodeAttributeComposer.java @@ -638,10 +638,6 @@ public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAtt codeAttribute.u2exceptionTableLength = exceptionTableLength; System.arraycopy(exceptionTable, 0, codeAttribute.exceptionTable, 0, exceptionTableLength); - // Update the maximum stack size and local variable frame size. - stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute); - variableSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute); - // Add a new line number table for the line numbers, if necessary. if (lineNumberTableLength > 0 && codeAttribute.getAttribute(clazz, Attribute.LINE_NUMBER_TABLE) == null) { @@ -674,6 +670,12 @@ public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAtt // Make sure instructions are widened if necessary. instructionWriter.visitCodeAttribute(clazz, method, codeAttribute); + // Update the maximum stack size and local variable frame size, after + // widening the instructions, since widening may make additional code + // reachable and change the required stack size. + stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute); + variableSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute); + level--; if (DEBUG) { diff --git a/base/src/test/kotlin/proguard/classfile/editor/CodeAttributeComposerTest.kt b/base/src/test/kotlin/proguard/classfile/editor/CodeAttributeComposerTest.kt index 7f04f32a5..a4f4b64f0 100644 --- a/base/src/test/kotlin/proguard/classfile/editor/CodeAttributeComposerTest.kt +++ b/base/src/test/kotlin/proguard/classfile/editor/CodeAttributeComposerTest.kt @@ -83,4 +83,66 @@ class CodeAttributeComposerTest : FunSpec({ }, ) } + + test("The computed maximum stack size accounts for widened branch instructions") { + val programClass = + ProgramClass( + VersionConstants.CLASS_VERSION_1_8, + 1, + arrayOfNulls(10), + AccessConstants.PUBLIC, + 0, + 0, + ) + val constantPoolEditor = ConstantPoolEditor(programClass) + val programMethod = + ProgramMethod( + AccessConstants.PUBLIC, + constantPoolEditor.addUtf8Constant("foo"), + constantPoolEditor.addUtf8Constant("()V"), + null, + ) + + val composer = + CompactCodeAttributeComposer( + constantPoolEditor, + CodeAttributeComposer(false, true, true), + ) + composer.beginCodeFragment(40000) + + val farBlock = composer.createLabel() + val end = composer.createLabel() + + // A block that only reaches the far block through a conditional branch whose + // remapped distance requires widening. + composer.iconst_0() + composer.ifeq(farBlock) + composer.return_() + // Filler to push the far block beyond the reach of a short branch. + repeat(32770) { composer.nop() } + // The far block pushes two values onto the stack. + composer.label(farBlock) + composer.iconst_0() + composer.iconst_0() + composer.goto_(end) + composer.label(end) + composer.return_() + + composer.endCodeFragment() + composer.addCodeAttribute(programClass, programMethod) + + programMethod.attributesAccept( + programClass, + object : AttributeVisitor { + override fun visitAnyAttribute(clazz: Clazz?, attribute: Attribute?) { + } + + override fun visitCodeAttribute(clazz: Clazz?, method: Method?, codeAttribute: CodeAttribute?) { + // The widened branch makes the far block reachable, which needs a + // stack size of 2. + codeAttribute!!.u2maxStack shouldBe 2 + } + }, + ) + } }) From bf42c17c37e2e28733d2bdd5095d62e11cc99d45 Mon Sep 17 00:00:00 2001 From: Robin Lefever Date: Mon, 2 Nov 2026 16:29:55 +0100 Subject: [PATCH 2/4] Apply PR #176: Guard against unresolved Kotlin metadata references in ClassReferenceFixer --- .../classfile/editor/ClassReferenceFixer.java | 62 +++++++++++----- ...lassReferenceFixerDanglingReferenceTest.kt | 72 +++++++++++++++++++ docs/md/releasenotes.md | 6 ++ 3 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 base/src/test/kotlin/proguard/classfile/editor/ClassReferenceFixerDanglingReferenceTest.kt diff --git a/base/src/main/java/proguard/classfile/editor/ClassReferenceFixer.java b/base/src/main/java/proguard/classfile/editor/ClassReferenceFixer.java index b5e76d65e..3a822285b 100644 --- a/base/src/main/java/proguard/classfile/editor/ClassReferenceFixer.java +++ b/base/src/main/java/proguard/classfile/editor/ClassReferenceFixer.java @@ -600,9 +600,6 @@ public void visitKotlinClassMetadata( kotlinClassKindMetadata.referencedCompanionClass); } - kotlinClassKindMetadata.enumEntries.forEach( - enumEntry -> enumEntry.name = enumEntry.referencedEnumEntry.getName(clazz)); - for (int k = 0; k < kotlinClassKindMetadata.nestedClassNames.size(); k++) { kotlinClassKindMetadata.nestedClassNames.set( k, @@ -620,6 +617,7 @@ public void visitKotlinClassMetadata( kotlinClassKindMetadata.referencedSealedSubClasses.get(k))); } + kotlinClassKindMetadata.enumEntriesAccept(clazz, this); kotlinClassKindMetadata.typeParametersAccept(clazz, this); kotlinClassKindMetadata.contextReceiverTypesAccept(clazz, this); kotlinClassKindMetadata.superTypesAccept(clazz, this); @@ -676,8 +674,10 @@ public void visitAnyProperty( if (kotlinPropertyMetadata.backingFieldSignature != null) { Clazz backingFieldClass = kotlinPropertyMetadata.referencedBackingFieldClass; Field backingField = kotlinPropertyMetadata.referencedBackingField; - kotlinPropertyMetadata.backingFieldSignature = - new FieldSignature(backingFieldClass, backingField); + if (backingFieldClass != null && backingField != null) { + kotlinPropertyMetadata.backingFieldSignature = + new FieldSignature(backingFieldClass, backingField); + } } kotlinPropertyMetadata.getterMetadata.signature = @@ -737,7 +737,9 @@ public void visitAnyFunction( if (kotlinFunctionMetadata.jvmSignature != null) { Clazz jvmClass = kotlinFunctionMetadata.referencedMethodClass; Method jvmMethod = kotlinFunctionMetadata.referencedMethod; - kotlinFunctionMetadata.jvmSignature = new MethodSignature(jvmClass, jvmMethod); + if (jvmClass != null && jvmMethod != null) { + kotlinFunctionMetadata.jvmSignature = new MethodSignature(jvmClass, jvmMethod); + } } kotlinFunctionMetadata.typeParametersAccept(clazz, kotlinMetadata, this); @@ -749,14 +751,16 @@ public void visitAnyFunction( kotlinFunctionMetadata.contractsAccept(clazz, kotlinMetadata, new AllTypeVisitor(this)); kotlinFunctionMetadata.annotationsAccept(clazz, this); - String newFunctionName = - newKotlinFunctionName( - kotlinFunctionMetadata.name, - kotlinFunctionMetadata.referencedMethod.getName( - kotlinFunctionMetadata.referencedMethodClass)); + Clazz referencedMethodClass = kotlinFunctionMetadata.referencedMethodClass; + Method referencedMethod = kotlinFunctionMetadata.referencedMethod; + if (referencedMethodClass != null && referencedMethod != null) { + String newFunctionName = + newKotlinFunctionName( + kotlinFunctionMetadata.name, referencedMethod.getName(referencedMethodClass)); - if (!kotlinFunctionMetadata.name.equals(newFunctionName)) { - kotlinFunctionMetadata.name = newFunctionName; + if (!kotlinFunctionMetadata.name.equals(newFunctionName)) { + kotlinFunctionMetadata.name = newFunctionName; + } } } @@ -769,7 +773,9 @@ public void visitConstructor( KotlinConstructorMetadata kotlinConstructorMetadata) { if (kotlinConstructorMetadata.jvmSignature != null) { Method jvmMethod = kotlinConstructorMetadata.referencedMethod; - kotlinConstructorMetadata.jvmSignature = new MethodSignature(clazz, jvmMethod); + if (jvmMethod != null) { + kotlinConstructorMetadata.jvmSignature = new MethodSignature(clazz, jvmMethod); + } } kotlinConstructorMetadata.valueParametersAccept(clazz, kotlinClassKindMetadata, this); @@ -780,7 +786,7 @@ public void visitConstructor( @Override public void visitAnyType(Clazz clazz, KotlinTypeMetadata kotlinTypeMetadata) { - if (kotlinTypeMetadata.className != null) { + if (kotlinTypeMetadata.className != null && kotlinTypeMetadata.referencedClass != null) { kotlinTypeMetadata.className = kotlinTypeMetadata.referencedClass.getName(); } @@ -877,11 +883,16 @@ public void visitAnyTypeParameter( kotlinTypeParameterMetadata.annotationsAccept(clazz, this); } + // Implementations for KotlinEnumEntryVisitor. + @Override public void visitAnyEnumEntry( Clazz clazz, KotlinClassKindMetadata kotlinClassKindMetadata, KotlinEnumEntryMetadata kotlinEnumEntryMetadata) { + if (kotlinEnumEntryMetadata.referencedEnumEntry != null) { + kotlinEnumEntryMetadata.name = kotlinEnumEntryMetadata.referencedEnumEntry.getName(clazz); + } kotlinEnumEntryMetadata.annotationsAccept(clazz, this); } @@ -915,8 +926,11 @@ public void visitAnyArgument( KotlinAnnotation annotation, KotlinAnnotationArgument argument, Value value) { - argument.name = - argument.referencedAnnotationMethod.getName(argument.referencedAnnotationMethodClass); + if (argument.referencedAnnotationMethod != null + && argument.referencedAnnotationMethodClass != null) { + argument.name = + argument.referencedAnnotationMethod.getName(argument.referencedAnnotationMethodClass); + } } @Override @@ -928,7 +942,9 @@ public void visitClassArgument( KotlinAnnotationArgument.ClassValue value) { this.visitAnyArgument(clazz, annotatable, annotation, argument, value); - value.className = value.referencedClass.getName(); + if (value.referencedClass != null) { + value.className = value.referencedClass.getName(); + } } @Override @@ -940,7 +956,9 @@ public void visitEnumArgument( KotlinAnnotationArgument.EnumValue value) { this.visitAnyArgument(clazz, annotatable, annotation, argument, value); - value.className = value.referencedClass.getName(); + if (value.referencedClass != null) { + value.className = value.referencedClass.getName(); + } } } @@ -1136,6 +1154,12 @@ private static MethodSignature fixPropertyMethod( return null; } + // If the referenced method didn't resolve (e.g. an incomplete class pool), leave the existing + // signature as-is rather than dereferencing a null reference. + if (referencedMethodClass == null || referencedMethod == null) { + return oldSignature; + } + MethodSignature newSignature = new MethodSignature(referencedMethodClass, referencedMethod); if (!oldSignature.equals(newSignature) && accessorMetadata != null) { diff --git a/base/src/test/kotlin/proguard/classfile/editor/ClassReferenceFixerDanglingReferenceTest.kt b/base/src/test/kotlin/proguard/classfile/editor/ClassReferenceFixerDanglingReferenceTest.kt new file mode 100644 index 000000000..8e41011d4 --- /dev/null +++ b/base/src/test/kotlin/proguard/classfile/editor/ClassReferenceFixerDanglingReferenceTest.kt @@ -0,0 +1,72 @@ +package proguard.classfile.editor + +import io.kotest.assertions.throwables.shouldNotThrowAny +import io.kotest.core.spec.style.FunSpec +import proguard.classfile.util.ClassReferenceInitializer +import proguard.testutils.ClassPoolBuilder +import proguard.testutils.KotlinSource + +/** + * Regression tests for https://github.com/Guardsquare/proguard-core/issues/174. + * + * When the class pool is incomplete -- e.g. a class referenced only from `@Metadata` isn't on the + * classpath -- [ClassReferenceInitializer] leaves the matching `referenced*` field null. + * [ClassReferenceFixer] must handle this case gracefully rather than throwing a + * [NullPointerException] while fixing the Kotlin metadata. + */ +class ClassReferenceFixerDanglingReferenceTest : FunSpec({ + + test("A dangling Kotlin type reference does not crash the ClassReferenceFixer") { + val (programClassPool, libraryClassPool) = ClassPoolBuilder.fromSource( + KotlinSource( + "Holder.kt", + """ + class Referenced + + class Holder { + val held: Referenced = Referenced() + fun make(): Referenced = Referenced() + } + """.trimIndent(), + ), + ) + + // Simulate an incomplete class pool: remove the referenced class and reinitialize so + // Holder's Kotlin type metadata is left with null referencedClass fields. + programClassPool.removeClass("Referenced") + programClassPool.classesAccept(ClassReferenceInitializer(programClassPool, libraryClassPool)) + + shouldNotThrowAny { + programClassPool.classesAccept(ClassReferenceFixer(false)) + } + } + + test("A dangling Kotlin annotation-argument reference does not crash the ClassReferenceFixer") { + val (programClassPool, libraryClassPool) = ClassPoolBuilder.fromSource( + KotlinSource( + "Holder.kt", + """ + import kotlin.reflect.KClass + + @Target(AnnotationTarget.TYPE) + annotation class Anno(val cls: KClass<*>) + + class Referenced + + class Holder { + val annotated: @Anno(Referenced::class) String = "" + } + """.trimIndent(), + ), + ) + + // Simulate an incomplete class pool: remove the class referenced by the annotation argument + // and reinitialize so the KClass argument's referencedClass is left null. + programClassPool.removeClass("Referenced") + programClassPool.classesAccept(ClassReferenceInitializer(programClassPool, libraryClassPool)) + + shouldNotThrowAny { + programClassPool.classesAccept(ClassReferenceFixer(false)) + } + } +}) diff --git a/docs/md/releasenotes.md b/docs/md/releasenotes.md index 418832e6c..ecba49420 100644 --- a/docs/md/releasenotes.md +++ b/docs/md/releasenotes.md @@ -1,3 +1,9 @@ +## Version 9.4.2 + +### Bugfixes + +- Fix `NullPointerException` in `ClassReferenceFixer` when a Kotlin metadata reference left unresolved by an incomplete class pool is dereferenced. + ## Version 9.4.1 ### Improved From 52a429a4268e91848cf2250deaac0793bce0c855 Mon Sep 17 00:00:00 2001 From: Nadeesh T V Date: Wed, 4 Nov 2026 11:54:58 +0100 Subject: [PATCH 3/4] Mark the injected extra static methods with initializer instructions as injected --- .../main/java/proguard/classfile/editor/InitializerEditor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/main/java/proguard/classfile/editor/InitializerEditor.java b/base/src/main/java/proguard/classfile/editor/InitializerEditor.java index 708b16ebe..26a3f8c32 100644 --- a/base/src/main/java/proguard/classfile/editor/InitializerEditor.java +++ b/base/src/main/java/proguard/classfile/editor/InitializerEditor.java @@ -128,7 +128,7 @@ public Method addStaticInitializerInstructions( // Make sure that such methods are not optimized (inlined) to prevent potential // overflow errors during conversion. - new ProcessingFlagSetter(ProcessingFlags.DONT_OPTIMIZE)); + new ProcessingFlagSetter(ProcessingFlags.DONT_OPTIMIZE | ProcessingFlags.INJECTED)); // Retrieve the newly created extra initialization method Method extraInitMethod = From ac95a93560dc9ab40ecaea348aeb636ec6774297 Mon Sep 17 00:00:00 2001 From: "niccolo.piazzesi" Date: Fri, 6 Nov 2026 12:03:07 +0100 Subject: [PATCH 4/4] Throw in MethodCreator when trying to override final method from library class --- .../classfile/visitor/MethodCreator.java | 47 ++++++++++++++----- .../classfile/visitor/MethodCreatorTest.kt | 24 +++++++++- docs/md/releasenotes.md | 1 + 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/base/src/main/java/proguard/classfile/visitor/MethodCreator.java b/base/src/main/java/proguard/classfile/visitor/MethodCreator.java index 6a71fad37..146316397 100644 --- a/base/src/main/java/proguard/classfile/visitor/MethodCreator.java +++ b/base/src/main/java/proguard/classfile/visitor/MethodCreator.java @@ -3,6 +3,7 @@ import proguard.classfile.AccessConstants; import proguard.classfile.ClassPool; import proguard.classfile.Clazz; +import proguard.classfile.LibraryMethod; import proguard.classfile.Method; import proguard.classfile.ProgramClass; import proguard.classfile.ProgramMethod; @@ -85,6 +86,39 @@ public void visitProgramClass(ProgramClass programClass) { return; } + // Find the superclass and method. Note that the hierarchy should be initialized for this. + Method superMethod = memberFinder.findMethod(programClass, name, descriptor); + Clazz superClass = memberFinder.correspondingClass(); + + // The super-method cannot be final. If it is defined in a library class we cannot continue. + if (superMethod instanceof LibraryMethod + && (superMethod.getAccessFlags() & AccessConstants.FINAL) != 0) { + throw new RuntimeException( + "You cannot override a final library method. " + + "New override: " + + programClass.getName() + + "." + + name + + descriptor + + ". " + + "Super final method: " + + superClass.getName() + + "." + + name + + descriptor + + "."); + } + + // The super-method cannot be final. If it is defined in a program class we strip the final + // flag. + if (superMethod instanceof ProgramMethod + && (superMethod.getAccessFlags() & AccessConstants.FINAL) != 0) { + ((ProgramMethod) superMethod).u2accessFlags &= ~AccessConstants.FINAL; + // We should always call super when we are making the super-method non-final, as it was + // marked to not change through inheritance. + callSuper = true; + } + ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor(programClass); ProgramMethod method = @@ -98,19 +132,6 @@ public void visitProgramClass(ProgramClass programClass) { System.out.println("Creating method " + programClass.getName() + "." + name + descriptor); } - // Find the superclass and method. Note that the hierarchy should be initialized for this. - Method superMethod = memberFinder.findMethod(programClass, name, descriptor); - Clazz superClass = memberFinder.correspondingClass(); - - // The super-method cannot be final. - if (superMethod instanceof ProgramMethod - && (((ProgramMethod) superMethod).u2accessFlags & AccessConstants.FINAL) != 0) { - ((ProgramMethod) superMethod).u2accessFlags &= ~AccessConstants.FINAL; - // We should always call super when we are making the super-method non-final, as it was - // marked to not change through inheritance. - callSuper = true; - } - InstructionSequenceBuilder builder = new InstructionSequenceBuilder(programClass, programClassPool, libraryClassPool); diff --git a/base/src/test/kotlin/proguard/classfile/visitor/MethodCreatorTest.kt b/base/src/test/kotlin/proguard/classfile/visitor/MethodCreatorTest.kt index bfe65f0f8..746d6e170 100644 --- a/base/src/test/kotlin/proguard/classfile/visitor/MethodCreatorTest.kt +++ b/base/src/test/kotlin/proguard/classfile/visitor/MethodCreatorTest.kt @@ -1,10 +1,14 @@ package proguard.classfile.visitor +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.BehaviorSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe +import io.kotest.matchers.string.shouldContain import proguard.classfile.AccessConstants +import proguard.classfile.ClassConstants import proguard.classfile.ClassPool +import proguard.classfile.editor.LibraryClassBuilder import proguard.classfile.util.ClassReferenceInitializer import proguard.testutils.ClassPoolBuilder import proguard.testutils.classfile.extensions.addMethod @@ -38,7 +42,7 @@ class MethodCreatorTest : BehaviorSpec({ } } - Given("a class which extends a class with a final method") { + Given("a class which extends a program class with a final method") { val (classB, methodB) = buildClass("B") { addMethod(accessFlags = AccessConstants.PUBLIC or AccessConstants.FINAL, name = "method", descriptor = "()V") { @@ -59,4 +63,22 @@ class MethodCreatorTest : BehaviorSpec({ } } } + Given("a class which extends a library class with a final method") { + + val builder = LibraryClassBuilder(AccessConstants.PUBLIC, "B", ClassConstants.NAME_JAVA_LANG_OBJECT) + builder.addAndReturnMethod(AccessConstants.PUBLIC or AccessConstants.FINAL, "aMethod", "()V") + val classB = builder.libraryClass + val classA = buildClass("A", superName = classB.name) + + val programClassPool = ClassPoolBuilder.fromClasses(classA) + val libraryClassPool = ClassPoolBuilder.fromClasses(classB) + programClassPool.classesAccept(ClassReferenceInitializer(programClassPool, libraryClassPool)) + + Then("The MethodCreator should throw an exception") { + val exc = shouldThrow { + classA.accept(MethodCreator(programClassPool, libraryClassPool, "aMethod", "()V", 0, 0, true)) + } + exc.message shouldContain "You cannot override a final library method." + } + } }) diff --git a/docs/md/releasenotes.md b/docs/md/releasenotes.md index ecba49420..f4de5c400 100644 --- a/docs/md/releasenotes.md +++ b/docs/md/releasenotes.md @@ -3,6 +3,7 @@ ### Bugfixes - Fix `NullPointerException` in `ClassReferenceFixer` when a Kotlin metadata reference left unresolved by an incomplete class pool is dereferenced. +- `MethodCreator` will now throw a `RuntimeException` when trying to override a final method from a library class. ## Version 9.4.1