Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
}

Expand All @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
47 changes: 34 additions & 13 deletions base/src/main/java/proguard/classfile/visitor/MethodCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
)
}
})
Loading
Loading