Skip to content

Commit 7857af3

Browse files
authored
Merge 41ac88e into cfeddd0
2 parents cfeddd0 + 41ac88e commit 7857af3

File tree

11 files changed

+160
-19
lines changed

11 files changed

+160
-19
lines changed

Confuser.Renamer/Analyzers/VTableAnalyzer.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,22 @@ private static void SetupOverwriteReferences(INameService service, ICollection<M
306306
methodDef.Overrides.Add(new MethodOverride(methodDef, target));
307307
}
308308
else if (target is IMemberDef targetDef) {
309-
CreateOverrideReference(service, methodDef, targetDef);
309+
// Reaching this place means that a slot of the base type is overwritten by a specific interface.
310+
// In case the this type is implementing the interface responsible for this, we need to declare
311+
// this as an override reference. If the base type is implementing the interface (as well), this
312+
// declaration is redundant.
313+
var overrideRefRequired = true;
314+
if (targetDef.DeclaringType.IsInterface) {
315+
var baseTypeDef = thisType.BaseType?.ResolveTypeDef();
316+
if (!(baseTypeDef is null)) {
317+
var baseTypeVTable = service.GetVTables()[baseTypeDef];
318+
if (baseTypeVTable.InterfaceSlots.TryGetValue(targetDef.DeclaringType.ToTypeSig(), out var ifcSlots)) {
319+
overrideRefRequired = !ifcSlots.Contains(slot);
320+
}
321+
}
322+
}
323+
if (overrideRefRequired)
324+
CreateOverrideReference(service, methodDef, targetDef);
310325
}
311326
}
312327

Confuser.Renamer/VTable.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class VTable {
9898
internal VTable(TypeSig type) {
9999
Type = type;
100100
Slots = new List<VTableSlot>();
101-
InterfaceSlots = new Dictionary<TypeSig, IList<VTableSlot>>();
101+
InterfaceSlots = new Dictionary<TypeSig, IList<VTableSlot>>(TypeEqualityComparer.Instance);
102102
}
103103

104104
public TypeSig Type { get; private set; }
@@ -107,23 +107,11 @@ internal VTable(TypeSig type) {
107107
public IDictionary<TypeSig, IList<VTableSlot>> InterfaceSlots { get; private set; }
108108

109109
class VTableConstruction {
110-
class TypeSigComparer : IEqualityComparer<TypeSig> {
111-
public bool Equals(TypeSig x, TypeSig y) {
112-
return new SigComparer().Equals(x, y);
113-
}
114-
115-
public int GetHashCode(TypeSig obj) {
116-
return new SigComparer().GetHashCode(obj);
117-
}
118-
119-
public static readonly TypeSigComparer Instance = new TypeSigComparer();
120-
}
121-
122110
// All virtual method slots, excluding interfaces
123111
public List<VTableSlot> AllSlots = new List<VTableSlot>();
124112
// All visible virtual method slots (i.e. excluded those being shadowed)
125113
public Dictionary<VTableSignature, VTableSlot> SlotsMap = new Dictionary<VTableSignature, VTableSlot>();
126-
public Dictionary<TypeSig, ILookup<VTableSignature, VTableSlot>> InterfaceSlots = new Dictionary<TypeSig, ILookup<VTableSignature, VTableSlot>>(TypeSigComparer.Instance);
114+
public Dictionary<TypeSig, ILookup<VTableSignature, VTableSlot>> InterfaceSlots = new Dictionary<TypeSig, ILookup<VTableSignature, VTableSlot>>(TypeEqualityComparer.Instance);
127115
}
128116

129117
public IEnumerable<VTableSlot> FindSlots(IMethod method) {
@@ -252,7 +240,7 @@ public static VTable ConstructVTable(TypeDef typeDef, VTableStorage storage) {
252240

253241
// Populate result V-table
254242
ret.InterfaceSlots = vTbl.InterfaceSlots.ToDictionary(
255-
kvp => kvp.Key, kvp => (IList<VTableSlot>)kvp.Value.SelectMany(g => g).ToList());
243+
kvp => kvp.Key, kvp => (IList<VTableSlot>)kvp.Value.SelectMany(g => g).ToList(), TypeEqualityComparer.Instance);
256244

257245
foreach (var slot in vTbl.AllSlots) {
258246
ret.Slots.Add(slot);

Confuser2.sln

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "244_ClrProtection", "Tests\
111111
EndProject
112112
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "244_ClrProtection.Test", "Tests\244_ClrProtection.Test\244_ClrProtection.Test.csproj", "{3ADB8BB1-AE14-49DA-A7E1-1C0D9BEB76E9}"
113113
EndProject
114-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "306_ComplexClassStructureRename.Lib", "Tests\306_ComplexClassStructureRename.Lib\306_ComplexClassStructureRename.Lib.csproj", "{FD93D181-2EC5-4863-8A8F-5F8C84C06B35}"
114+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "306_ComplexClassStructureRename.Lib", "Tests\306_ComplexClassStructureRename.Lib\306_ComplexClassStructureRename.Lib.csproj", "{FD93D181-2EC5-4863-8A8F-5F8C84C06B35}"
115115
EndProject
116-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "306_ComplexClassStructureRename", "Tests\306_ComplexClassStructureRename\306_ComplexClassStructureRename.csproj", "{1B52A3D9-014C-4CBF-BB98-09080D9A8D16}"
116+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "306_ComplexClassStructureRename", "Tests\306_ComplexClassStructureRename\306_ComplexClassStructureRename.csproj", "{1B52A3D9-014C-4CBF-BB98-09080D9A8D16}"
117117
EndProject
118-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "306_ComplexClassStructureRename.Test", "Tests\306_ComplexClassStructureRename.Test\306_ComplexClassStructureRename.Test.csproj", "{13431429-2DB6-480F-B73F-CA019FE759E3}"
118+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "306_ComplexClassStructureRename.Test", "Tests\306_ComplexClassStructureRename.Test\306_ComplexClassStructureRename.Test.csproj", "{13431429-2DB6-480F-B73F-CA019FE759E3}"
119+
EndProject
120+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "342_InterfaceRenamingLoop", "Tests\342_InterfaceRenamingLoop\342_InterfaceRenamingLoop.csproj", "{382B6332-4A57-458D-96EB-B312688A7604}"
121+
EndProject
122+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "342_InterfaceRenamingLoop.Test", "Tests\342_InterfaceRenamingLoop.Test\342_InterfaceRenamingLoop.Test.csproj", "{EC62CE1D-ADD7-419A-84A9-D6A04E866197}"
119123
EndProject
120124
Global
121125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -727,6 +731,30 @@ Global
727731
{13431429-2DB6-480F-B73F-CA019FE759E3}.Release|x64.Build.0 = Release|Any CPU
728732
{13431429-2DB6-480F-B73F-CA019FE759E3}.Release|x86.ActiveCfg = Release|Any CPU
729733
{13431429-2DB6-480F-B73F-CA019FE759E3}.Release|x86.Build.0 = Release|Any CPU
734+
{382B6332-4A57-458D-96EB-B312688A7604}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
735+
{382B6332-4A57-458D-96EB-B312688A7604}.Debug|Any CPU.Build.0 = Debug|Any CPU
736+
{382B6332-4A57-458D-96EB-B312688A7604}.Debug|x64.ActiveCfg = Debug|Any CPU
737+
{382B6332-4A57-458D-96EB-B312688A7604}.Debug|x64.Build.0 = Debug|Any CPU
738+
{382B6332-4A57-458D-96EB-B312688A7604}.Debug|x86.ActiveCfg = Debug|Any CPU
739+
{382B6332-4A57-458D-96EB-B312688A7604}.Debug|x86.Build.0 = Debug|Any CPU
740+
{382B6332-4A57-458D-96EB-B312688A7604}.Release|Any CPU.ActiveCfg = Release|Any CPU
741+
{382B6332-4A57-458D-96EB-B312688A7604}.Release|Any CPU.Build.0 = Release|Any CPU
742+
{382B6332-4A57-458D-96EB-B312688A7604}.Release|x64.ActiveCfg = Release|Any CPU
743+
{382B6332-4A57-458D-96EB-B312688A7604}.Release|x64.Build.0 = Release|Any CPU
744+
{382B6332-4A57-458D-96EB-B312688A7604}.Release|x86.ActiveCfg = Release|Any CPU
745+
{382B6332-4A57-458D-96EB-B312688A7604}.Release|x86.Build.0 = Release|Any CPU
746+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
747+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Debug|Any CPU.Build.0 = Debug|Any CPU
748+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Debug|x64.ActiveCfg = Debug|Any CPU
749+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Debug|x64.Build.0 = Debug|Any CPU
750+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Debug|x86.ActiveCfg = Debug|Any CPU
751+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Debug|x86.Build.0 = Debug|Any CPU
752+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Release|Any CPU.ActiveCfg = Release|Any CPU
753+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Release|Any CPU.Build.0 = Release|Any CPU
754+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Release|x64.ActiveCfg = Release|Any CPU
755+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Release|x64.Build.0 = Release|Any CPU
756+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Release|x86.ActiveCfg = Release|Any CPU
757+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197}.Release|x86.Build.0 = Release|Any CPU
730758
EndGlobalSection
731759
GlobalSection(SolutionProperties) = preSolution
732760
HideSolutionNode = FALSE
@@ -774,6 +802,8 @@ Global
774802
{FD93D181-2EC5-4863-8A8F-5F8C84C06B35} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
775803
{1B52A3D9-014C-4CBF-BB98-09080D9A8D16} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
776804
{13431429-2DB6-480F-B73F-CA019FE759E3} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
805+
{382B6332-4A57-458D-96EB-B312688A7604} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
806+
{EC62CE1D-ADD7-419A-84A9-D6A04E866197} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
777807
EndGlobalSection
778808
GlobalSection(ExtensibilityGlobals) = postSolution
779809
SolutionGuid = {0D937D9E-E04B-4A68-B639-D4260473A388}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net461</TargetFramework>
5+
<RootNamespace>InterfaceRenamingLoop.Test</RootNamespace>
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\342_InterfaceRenamingLoop\342_InterfaceRenamingLoop.csproj" />
11+
<ProjectReference Include="..\Confuser.UnitTest\Confuser.UnitTest.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Confuser.Core;
5+
using Confuser.Core.Project;
6+
using Confuser.Renamer;
7+
using Confuser.UnitTest;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace InterfaceRenamingLoop.Test {
12+
public class InterfaceRenamingLoopTest : TestBase {
13+
public InterfaceRenamingLoopTest(ITestOutputHelper outputHelper) : base(outputHelper) { }
14+
15+
[Theory]
16+
[MemberData(nameof(RenameInterfaceLoopTestData))]
17+
[Trait("Category", "Protection")]
18+
[Trait("Protection", "rename")]
19+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/342")]
20+
public async Task RenameInterfaceLoop(string renameMode) =>
21+
await Run(
22+
"342_InterfaceRenamingLoop.exe",
23+
Array.Empty<string>(),
24+
new SettingItem<Protection>("rename") {
25+
{ "mode", renameMode }
26+
},
27+
$"_{renameMode}"
28+
);
29+
30+
public static IEnumerable<object[]> RenameInterfaceLoopTestData() {
31+
foreach (var renameMode in new[] { nameof(RenameMode.Unicode), nameof(RenameMode.Debug), nameof(RenameMode.Sequential) })
32+
yield return new object[] { renameMode };
33+
}
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net461</TargetFramework>
6+
<RootNamespace>InterfaceRenamingLoop</RootNamespace>
7+
<StartupObject>InterfaceRenamingLoop.Program</StartupObject>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace InterfaceRenamingLoop {
2+
internal class CBase {
3+
public virtual void TestEvent(int code, string description) {
4+
5+
}
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace InterfaceRenamingLoop {
2+
internal class ClassA : CBase, IAEvents {
3+
public void TestA(int errorCode) {
4+
5+
}
6+
public override void TestEvent(int code, string description) {
7+
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace InterfaceRenamingLoop {
8+
internal class ClassB : ClassA {
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace InterfaceRenamingLoop {
2+
internal interface IAEvents {
3+
void TestA(int errorCode);
4+
void TestEvent(int errorCode, string description);
5+
}
6+
}

0 commit comments

Comments
 (0)