Skip to content

Commit 4a0c244

Browse files
authored
Merge 5e485e5 into 36c020c
2 parents 36c020c + 5e485e5 commit 4a0c244

File tree

6 files changed

+113
-9
lines changed

6 files changed

+113
-9
lines changed

Confuser.Core/Helpers/MutationHelper.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,34 @@ public static void ReplacePlaceholder(MethodDef method, Func<Instruction[], Inst
8686
var operand = (IMethod)instr.Operand;
8787
if (operand.DeclaringType.FullName == mutationType &&
8888
operand.Name == "Placeholder") {
89-
int[] argIndexes = trace.TraceArguments(instr);
90-
if (argIndexes == null)
91-
throw new ArgumentException("Failed to trace placeholder argument.");
89+
var initialLoadInstructions = new List<Instruction>();
90+
var pendingInstructions = new Queue<Instruction>();
91+
pendingInstructions.Enqueue(instr);
92+
while (pendingInstructions.Count > 0) {
93+
var currentInstr = pendingInstructions.Dequeue();
94+
int[] argIndexes = trace.TraceArguments(currentInstr);
95+
if (argIndexes == null)
96+
throw new ArgumentException("Failed to trace placeholder argument.");
9297

93-
int argIndex = argIndexes[0];
94-
Instruction[] arg = method.Body.Instructions.Skip(argIndex).Take(i - argIndex).ToArray();
98+
if (argIndexes.Length == 0)
99+
initialLoadInstructions.Add(currentInstr);
100+
101+
foreach (int argIndex in argIndexes)
102+
pendingInstructions.Enqueue(method.Body.Instructions[argIndex]);
103+
}
104+
105+
var firstArgIndex = initialLoadInstructions.Select(method.Body.Instructions.IndexOf).Min();
106+
Instruction[] arg = method.Body.Instructions.Skip(firstArgIndex).Take(i - firstArgIndex).ToArray();
95107
for (int j = 0; j < arg.Length; j++)
96-
method.Body.Instructions.RemoveAt(argIndex);
97-
method.Body.Instructions.RemoveAt(argIndex);
108+
method.Body.Instructions.RemoveAt(firstArgIndex);
109+
method.Body.Instructions.RemoveAt(firstArgIndex);
98110
arg = repl(arg);
99111
for (int j = arg.Length - 1; j >= 0; j--)
100-
method.Body.Instructions.Insert(argIndex, arg[j]);
112+
method.Body.Instructions.Insert(firstArgIndex, arg[j]);
101113
return;
102114
}
103115
}
104116
}
105117
}
106118
}
107-
}
119+
}

Confuser2.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "252_ComplexInterfaceRenamin
9999
EndProject
100100
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "252_ComplexInterfaceRenaming.Test", "Tests\252_ComplexInterfaceRenaming.Test\252_ComplexInterfaceRenaming.Test.csproj", "{C10599E3-5A79-484F-940B-E4B61F256466}"
101101
EndProject
102+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReferenceProxy", "Tests\ReferenceProxy\ReferenceProxy.csproj", "{FE068381-F170-4C37-82C4-11A81FE60F1A}"
103+
EndProject
104+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReferenceProxy.Test", "Tests\ReferenceProxy.Test\ReferenceProxy.Test.csproj", "{2C059FE7-C868-4C6D-AFA0-D62BA3C1B2E1}"
105+
EndProject
102106
Global
103107
GlobalSection(SolutionConfigurationPlatforms) = preSolution
104108
Debug|Any CPU = Debug|Any CPU
@@ -269,6 +273,14 @@ Global
269273
{C10599E3-5A79-484F-940B-E4B61F256466}.Debug|Any CPU.Build.0 = Debug|Any CPU
270274
{C10599E3-5A79-484F-940B-E4B61F256466}.Release|Any CPU.ActiveCfg = Release|Any CPU
271275
{C10599E3-5A79-484F-940B-E4B61F256466}.Release|Any CPU.Build.0 = Release|Any CPU
276+
{FE068381-F170-4C37-82C4-11A81FE60F1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
277+
{FE068381-F170-4C37-82C4-11A81FE60F1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
278+
{FE068381-F170-4C37-82C4-11A81FE60F1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
279+
{FE068381-F170-4C37-82C4-11A81FE60F1A}.Release|Any CPU.Build.0 = Release|Any CPU
280+
{2C059FE7-C868-4C6D-AFA0-D62BA3C1B2E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
281+
{2C059FE7-C868-4C6D-AFA0-D62BA3C1B2E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
282+
{2C059FE7-C868-4C6D-AFA0-D62BA3C1B2E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
283+
{2C059FE7-C868-4C6D-AFA0-D62BA3C1B2E1}.Release|Any CPU.Build.0 = Release|Any CPU
272284
EndGlobalSection
273285
GlobalSection(SolutionProperties) = preSolution
274286
HideSolutionNode = FALSE
@@ -307,6 +319,8 @@ Global
307319
{B7FF0EE8-6C68-46C6-AADB-58C0E3309FB2} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
308320
{F233D36D-B64A-4F14-A9F9-B8557C2D4F5D} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
309321
{C10599E3-5A79-484F-940B-E4B61F256466} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
322+
{FE068381-F170-4C37-82C4-11A81FE60F1A} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
323+
{2C059FE7-C868-4C6D-AFA0-D62BA3C1B2E1} = {356BDB31-853E-43BB-8F9A-D8AC08F69EBB}
310324
EndGlobalSection
311325
GlobalSection(ExtensibilityGlobals) = postSolution
312326
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+
<IsPackable>False</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\Confuser.UnitTest\Confuser.UnitTest.csproj" />
10+
<ProjectReference Include="..\ReferenceProxy\ReferenceProxy.csproj" />
11+
</ItemGroup>
12+
13+
14+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.UnitTest;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace ReferenceProxy.Test {
11+
public class ReferenceProxyTest : TestBase {
12+
public ReferenceProxyTest(ITestOutputHelper outputHelper) : base(outputHelper) { }
13+
14+
[Theory]
15+
[MemberData(nameof(ReferenceProxyData))]
16+
[Trait("Category", "Protection")]
17+
[Trait("Protection", "ref proxy")]
18+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/229")]
19+
public async Task ReferenceProxy(string mode, string encoding, bool internalRefs, bool typeErasure) =>
20+
await Run(
21+
"ReferenceProxy.exe",
22+
Array.Empty<string>(),
23+
new SettingItem<Protection>("ref proxy") {
24+
["mode"] = mode,
25+
["encoding"] = encoding,
26+
["internal"] = internalRefs.ToString(),
27+
["typeErasure"] = typeErasure.ToString()
28+
},
29+
outputDirSuffix: $"_{mode}_{encoding}_{internalRefs}_{typeErasure}"
30+
);
31+
32+
public static IEnumerable<object[]> ReferenceProxyData() {
33+
foreach (var mode in new[] { "mild", "strong" })
34+
foreach (var encoding in new[] { "normal", "expression", "x86" })
35+
foreach (var internalRefs in new[] { true, false })
36+
foreach (var typeErasure in new[] { true, false }) {
37+
if (mode.Equals("mild") && !encoding.Equals("normal")) continue;
38+
yield return new object[] { mode, encoding, internalRefs, typeErasure };
39+
}
40+
}
41+
}
42+
}

Tests/ReferenceProxy/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace ReferenceProxy {
4+
class Program {
5+
static int Main(string[] args) {
6+
Console.WriteLine("START");
7+
foreach (var arg in args)
8+
Console.WriteLine(arg);
9+
Console.WriteLine("END");
10+
return 42;
11+
}
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net461</TargetFramework>
6+
<StartupObject>ReferenceProxy.Program</StartupObject>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)