Merge branch 'feature/78_sig_mismatch'

This commit is contained in:
Martin Karing 2019-09-07 13:07:02 +02:00
Родитель f3f47f628e 58c3dd457f
Коммит 410d433931
7 изменённых файлов: 189 добавлений и 8 удалений

Просмотреть файл

@ -112,7 +112,7 @@ namespace Confuser.Renamer.Analyzers {
var importer = new Importer(module, ImporterOptions.TryToUseTypeDefs);
IMethod target;
IMethodDefOrRef target;
if (baseSlot.MethodDefDeclType is GenericInstSig declType) {
var signature = SetupSignatureReferences(context, service, module, declType);
MemberRef targetRef = new MemberRefUser(module, baseMethodDef.Name, baseMethodDef.MethodSig, signature.ToTypeDefOrRef());
@ -124,7 +124,7 @@ namespace Confuser.Renamer.Analyzers {
else {
target = baseMethodDef;
if (target.Module != module) {
target = importer.Import(baseMethodDef);
target = (IMethodDefOrRef)importer.Import(baseMethodDef);
if (target is MemberRef memberRef)
service.AddReference(baseMethodDef, new MemberRefReference(memberRef, baseMethodDef));
}
@ -134,12 +134,27 @@ namespace Confuser.Renamer.Analyzers {
if (target is MemberRef methodRef)
AddImportReference(context, service, module, baseMethodDef, methodRef);
if (methodDef.Overrides.Any(impl =>
new SigComparer().Equals(impl.MethodDeclaration.MethodSig, target.MethodSig) &&
new SigComparer().Equals(impl.MethodDeclaration.DeclaringType.ResolveTypeDef(), target.DeclaringType.ResolveTypeDef())))
if (methodDef.Overrides.Any(impl => IsMatchingOverride(impl, target)))
return;
methodDef.Overrides.Add(new MethodOverride(methodDef, (IMethodDefOrRef)target));
methodDef.Overrides.Add(new MethodOverride(methodDef, target));
}
private static bool IsMatchingOverride(MethodOverride methodOverride, IMethodDefOrRef targetMethod) {
SigComparer comparer = default;
var targetDeclTypeDef = targetMethod.DeclaringType.ResolveTypeDef();
var overrideDeclTypeDef = methodOverride.MethodDeclaration.DeclaringType.ResolveTypeDef();
if (!comparer.Equals(targetDeclTypeDef, overrideDeclTypeDef))
return false;
var targetMethodSig = targetMethod.MethodSig;
var overrideMethodSig = methodOverride.MethodDeclaration.MethodSig;
if (methodOverride.MethodDeclaration.DeclaringType is TypeSpec spec && spec.TypeSig is GenericInstSig genericInstSig) {
overrideMethodSig = GenericArgumentResolver.Resolve(overrideMethodSig, genericInstSig.GenericArguments);
}
return comparer.Equals(targetMethodSig, overrideMethodSig);
}
public void PreRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) {

Просмотреть файл

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2035
# Visual Studio Version 16
VisualStudioVersion = 16.0.29215.179
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Confuser.Core", "Confuser.Core\Confuser.Core.csproj", "{BEB67A6E-4C54-4DE5-8C6B-2C12F44A7B92}"
EndProject
@ -59,6 +59,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VisualBasicRenamingResx.Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Confuser.Renamer.Test", "Tests\Confuser.Renamer.Test\Confuser.Renamer.Test.csproj", "{A1F54816-3FBA-4A71-9D26-D31C6BE9CF01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "78_SignatureMismatch", "Tests\78_SignatureMismatch\78_SignatureMismatch.csproj", "{CD257C0A-9071-42B4-A2FF-180622DBCA96}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "78_SignatureMismatch.Test", "Tests\78_SignatureMismatch.Test\78_SignatureMismatch.Test.csproj", "{8489A9CE-AB1A-4D8D-8824-D9E18B9945FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -149,6 +153,14 @@ Global
{A1F54816-3FBA-4A71-9D26-D31C6BE9CF01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1F54816-3FBA-4A71-9D26-D31C6BE9CF01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1F54816-3FBA-4A71-9D26-D31C6BE9CF01}.Release|Any CPU.Build.0 = Release|Any CPU
{CD257C0A-9071-42B4-A2FF-180622DBCA96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD257C0A-9071-42B4-A2FF-180622DBCA96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD257C0A-9071-42B4-A2FF-180622DBCA96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD257C0A-9071-42B4-A2FF-180622DBCA96}.Release|Any CPU.Build.0 = Release|Any CPU
{8489A9CE-AB1A-4D8D-8824-D9E18B9945FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8489A9CE-AB1A-4D8D-8824-D9E18B9945FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8489A9CE-AB1A-4D8D-8824-D9E18B9945FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8489A9CE-AB1A-4D8D-8824-D9E18B9945FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -166,6 +178,8 @@ Global
{4B2CE997-8157-40B4-B42F-51CE33954AAC} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
{40C6A1BB-69AA-4869-81EE-41917D0B009A} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
{A1F54816-3FBA-4A71-9D26-D31C6BE9CF01} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
{CD257C0A-9071-42B4-A2FF-180622DBCA96} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
{8489A9CE-AB1A-4D8D-8824-D9E18B9945FE} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0D937D9E-E04B-4A68-B639-D4260473A388}

Просмотреть файл

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<RootNamespace>SignatureMismatch.Test</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Confuser.Core\Confuser.Core.csproj" />
<ProjectReference Include="..\..\Confuser.Renamer\Confuser.Renamer.csproj" />
<ProjectReference Include="..\78_SignatureMismatch\78_SignatureMismatch.csproj" />
<ProjectReference Include="..\Confuser.UnitTest\Confuser.UnitTest.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,63 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Confuser.Core;
using Confuser.Core.Project;
using Confuser.UnitTest;
using Xunit;
using Xunit.Abstractions;
namespace SignatureMismatch.Test {
public class SignatureMismatchTest {
private readonly ITestOutputHelper outputHelper;
public SignatureMismatchTest(ITestOutputHelper outputHelper) =>
this.outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper));
[Fact]
[Trait("Category", "Protection")]
[Trait("Protection", "rename")]
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/78")]
public async Task SignatureMismatch() {
var baseDir = Environment.CurrentDirectory;
var outputDir = Path.Combine(baseDir, "testtmp");
var inputFile = Path.Combine(baseDir, "78_SignatureMismatch.exe");
var outputFile = Path.Combine(outputDir, "78_SignatureMismatch.exe");
FileUtilities.ClearOutput(outputFile);
var proj = new ConfuserProject {
BaseDirectory = baseDir,
OutputDirectory = outputDir
};
proj.Add(new ProjectModule() { Path = inputFile });
var parameters = new ConfuserParameters {
Project = proj,
Logger = new XunitLogger(outputHelper)
};
await ConfuserEngine.Run(parameters);
Assert.True(File.Exists(outputFile));
Assert.NotEqual(FileUtilities.ComputeFileChecksum(inputFile), FileUtilities.ComputeFileChecksum(outputFile));
var info = new ProcessStartInfo(outputFile) {
RedirectStandardOutput = true,
UseShellExecute = false
};
using (var process = Process.Start(info)) {
var stdout = process.StandardOutput;
Assert.Equal("START", await stdout.ReadLineAsync());
Assert.Equal("Dictionary created", await stdout.ReadLineAsync());
Assert.Equal("Dictionary count: 1", await stdout.ReadLineAsync());
Assert.Equal("[Test1] = Test2", await stdout.ReadLineAsync());
Assert.Equal("END", await stdout.ReadLineAsync());
Assert.Empty(await stdout.ReadToEndAsync());
Assert.True(process.HasExited);
Assert.Equal(42, process.ExitCode);
}
FileUtilities.ClearOutput(outputFile);
}
}
}

Просмотреть файл

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
<RootNamespace>SignatureMismatch</RootNamespace>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
</Project>

Просмотреть файл

@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
namespace SignatureMismatch {
public class EasyDict<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> {
private readonly Dictionary<TKey, TValue> _dict;
public EasyDict() => _dict = new Dictionary<TKey, TValue>();
public EasyDict(int capacity) => _dict = new Dictionary<TKey, TValue>(capacity);
public int Count => _dict.Count;
public IEnumerable<TValue> Values => _dict.Values;
public TValue this[TKey key] {
get => _dict.TryGetValue(key, out var value) ? value : default;
set => _dict[key] = value;
}
public void Add(TKey key, TValue value) => _dict[key] = value;
public void Clear() => _dict.Clear();
public bool ContainsKey(TKey key) => _dict.ContainsKey(key);
IEnumerator IEnumerable.GetEnumerator() => _dict.GetEnumerator();
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => _dict.GetEnumerator();
public bool Remove(TKey key) => _dict.Remove(key);
public bool TryGetValue(TKey key, out TValue value) => _dict.TryGetValue(key, out value);
}
}

Просмотреть файл

@ -0,0 +1,21 @@
using System;
namespace SignatureMismatch {
class Program {
static int Main(string[] args) {
Console.WriteLine("START");
var dict = new EasyDict<string, string> {
{ "Test1", "Test2" }
};
Console.WriteLine("Dictionary created");
Console.WriteLine($"Dictionary count: {dict.Count:d}");
foreach (var kvp in dict)
Console.WriteLine($"[{kvp.Key}] = {kvp.Value}");
Console.WriteLine("END");
return 42;
}
}
}