Skip to content

Generic Interface Implementation Could be Broken After Renaming #1

@tshcherban

Description

@tshcherban
  • ConfuserEx Version: 1.7.0-alpha.4+657fb58dcf
  • Target Framework: 4.6.1, 6
  • Operating System: Win10

Simmilar to:
mkaring#78
mkaring#87

The problem:
Generic interface's methods are broken after renaming. Parameters/return types are remain unchanged in declaration, but renamed in the body.
Confuser yields broken assembly after obfuscating the following code:

public interface IInterfaceReturnType<T>
{
	External Method();
}

public class ClassReturnType : IInterfaceReturnType<object>
{
	public External Method()
	{
		Console.WriteLine("ReturnExternal");
		return new External();
	}
}

I've added a test for this issue.

Currently to workaround this problem I've edited a MemberRefReference type, added the following method:

private void TryApplyGenericInterfaceImplFix(ConfuserContext context) {
	try {
		var md = memberDef as MethodDef;
		if (md == null)
			return;

		if (!md.DeclaringType.HasGenericParameters)
			return;

		if (md.HasReturnType && md.ReturnType.FullName != memberRef.ReturnType.FullName) {
			context.Logger.Warn($"Fixing method impl return type to match method declaration:\r\n" +
				$"  type '{md.DeclaringType.FullName}'\r\n" +
				$"  repalce '{memberRef.ReturnType.FullName}'\r\n" +
				$"  with '{md.ReturnType.FullName}'");

			memberRef.ReturnType = md.ReturnType;
		}

		for (var i = 0; i < memberRef.MethodSig.Params.Count; i++) {
			var refParameterType = memberRef.MethodSig.Params[i];
			var defParameter = md.Parameters.First(x => x.MethodSigIndex == i);
			if (refParameterType.FullName!=defParameter.Type.FullName) {
				context.Logger.Warn($"Fixing method parameter {i} type to match method declaration:\r\n" +
				$"  type '{md.DeclaringType.FullName}'\r\n" +
				$"  repalce '{refParameterType.FullName}'\r\n" +
				$"  with '{defParameter.Type.FullName}'");

				memberRef.MethodSig.Params[i] = defParameter.Type;
			}
		}
	}
	catch (Exception e) {
		context.Logger.WarnException($"Failed to ${nameof(TryApplyGenericInterfaceImplFix)}", e);
	}
}

But that looks to me more as a hack which fixes aftermath rather than finding a real issue.
Any ideas where the original issue is?
@KvanTTT

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions