Skip to content

Commit 5a52243

Browse files
authored
Merge c33dae8 into cfccc32
2 parents cfccc32 + c33dae8 commit 5a52243

File tree

16 files changed

+732
-34
lines changed

16 files changed

+732
-34
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--
2+
This file as to be present, otherwise the Microsoft.DiaSymReader.Native package does not copy it's files to the output.
3+
The content of this file does not matter.
4+
-->
5+
<appSettings />

Confuser.Core/ConfuserContext.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,28 +161,36 @@ public void CheckCancellation() {
161161
/// Requests the current module to be written as mix-mode module, and return the native writer options.
162162
/// </summary>
163163
/// <returns>The native writer options.</returns>
164-
public NativeModuleWriterOptions RequestNative() {
164+
public NativeModuleWriterOptions RequestNative(bool optimizeImageSize) {
165165
if (CurrentModule == null)
166166
return null;
167167
if (CurrentModuleWriterOptions == null)
168-
CurrentModuleWriterOptions = new NativeModuleWriterOptions(CurrentModule, true);
169-
170-
if (CurrentModuleWriterOptions is NativeModuleWriterOptions)
171-
return (NativeModuleWriterOptions)CurrentModuleWriterOptions;
172-
var newOptions = new NativeModuleWriterOptions(CurrentModule, true);
168+
CurrentModuleWriterOptions = new NativeModuleWriterOptions(CurrentModule, optimizeImageSize);
169+
173170
// Clone the current options to the new options
174-
newOptions.AddCheckSum = CurrentModuleWriterOptions.AddCheckSum;
175-
newOptions.Cor20HeaderOptions = CurrentModuleWriterOptions.Cor20HeaderOptions;
176-
newOptions.Logger = CurrentModuleWriterOptions.Logger;
177-
newOptions.MetadataLogger = CurrentModuleWriterOptions.MetadataLogger;
178-
newOptions.MetadataOptions = CurrentModuleWriterOptions.MetadataOptions;
179-
newOptions.ModuleKind = CurrentModuleWriterOptions.ModuleKind;
180-
newOptions.PEHeadersOptions = CurrentModuleWriterOptions.PEHeadersOptions;
181-
newOptions.ShareMethodBodies = CurrentModuleWriterOptions.ShareMethodBodies;
182-
newOptions.DelaySign = CurrentModuleWriterOptions.DelaySign;
183-
newOptions.StrongNameKey = CurrentModuleWriterOptions.StrongNameKey;
184-
newOptions.StrongNamePublicKey = CurrentModuleWriterOptions.StrongNamePublicKey;
185-
newOptions.Win32Resources = CurrentModuleWriterOptions.Win32Resources;
171+
var newOptions = new NativeModuleWriterOptions(CurrentModule, optimizeImageSize) {
172+
AddCheckSum = CurrentModuleWriterOptions.AddCheckSum,
173+
AddMvidSection = CurrentModuleWriterOptions.AddMvidSection,
174+
Cor20HeaderOptions = CurrentModuleWriterOptions.Cor20HeaderOptions,
175+
GetPdbContentId = CurrentModuleWriterOptions.GetPdbContentId,
176+
Logger = CurrentModuleWriterOptions.Logger,
177+
MetadataLogger = CurrentModuleWriterOptions.MetadataLogger,
178+
MetadataOptions = CurrentModuleWriterOptions.MetadataOptions,
179+
ModuleKind = CurrentModuleWriterOptions.ModuleKind,
180+
NoWin32Resources = CurrentModuleWriterOptions.NoWin32Resources,
181+
PdbChecksumAlgorithm = CurrentModuleWriterOptions.PdbChecksumAlgorithm,
182+
PdbFileName = CurrentModuleWriterOptions.PdbFileName,
183+
PdbFileNameInDebugDirectory = CurrentModuleWriterOptions.PdbFileNameInDebugDirectory,
184+
PdbOptions = CurrentModuleWriterOptions.PdbOptions,
185+
PdbStream = CurrentModuleWriterOptions.PdbStream,
186+
PEHeadersOptions = CurrentModuleWriterOptions.PEHeadersOptions,
187+
ShareMethodBodies = CurrentModuleWriterOptions.ShareMethodBodies,
188+
DelaySign = CurrentModuleWriterOptions.DelaySign,
189+
StrongNameKey = CurrentModuleWriterOptions.StrongNameKey,
190+
StrongNamePublicKey = CurrentModuleWriterOptions.StrongNamePublicKey,
191+
Win32Resources = CurrentModuleWriterOptions.Win32Resources,
192+
WritePdb = CurrentModuleWriterOptions.WritePdb,
193+
};
186194
CurrentModuleWriterOptions = newOptions;
187195
return newOptions;
188196
}

Confuser.Core/ConfuserEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,10 @@ static void BeginModule(ConfuserContext context) {
332332
context.Logger.InfoFormat("Processing module '{0}'...", context.CurrentModule.Name);
333333

334334
context.CurrentModuleWriterOptions = new ModuleWriterOptions(context.CurrentModule);
335-
context.CurrentModuleWriterOptions.WriterEvent += (sender, e) => context.CheckCancellation();
336335
CopyPEHeaders(context.CurrentModuleWriterOptions.PEHeadersOptions, context.CurrentModule);
337336

338337
if (!context.CurrentModule.IsILOnly || context.CurrentModule.VTableFixups != null)
339-
context.RequestNative();
338+
context.RequestNative(true);
340339

341340
var snKey = context.Annotations.Get<StrongNameKey>(context.CurrentModule, Marker.SNKey);
342341
var snPubKey = context.Annotations.Get<StrongNamePublicKey>(context.CurrentModule, Marker.SNPubKey);
@@ -367,7 +366,8 @@ static void BeginModule(ConfuserContext context) {
367366
}
368367
}
369368

370-
static void ProcessModule(ConfuserContext context) { }
369+
static void ProcessModule(ConfuserContext context) =>
370+
context.CurrentModuleWriterOptions.WriterEvent += (sender, e) => context.CheckCancellation();
371371

372372
static void OptimizeMethods(ConfuserContext context) {
373373
foreach (TypeDef type in context.CurrentModule.GetTypes())

Confuser.Protections/AntiTamper/AntiTamperProtection.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Confuser.Core;
44
using Confuser.Protections.AntiTamper;
55
using dnlib.DotNet;
6+
using dnlib.DotNet.Writer;
67

78
namespace Confuser.Protections {
89
public interface IAntiTamperService {
@@ -41,6 +42,7 @@ protected override void Initialize(ConfuserContext context) {
4142
}
4243

4344
protected override void PopulatePipeline(ProtectionPipeline pipeline) {
45+
pipeline.InsertPostStage(PipelineStage.BeginModule, new ModuleWriterSetupPhase(this));
4446
pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new InjectPhase(this));
4547
pipeline.InsertPreStage(PipelineStage.EndModule, new MDPhase(this));
4648
}
@@ -49,6 +51,25 @@ public void ExcludeMethod(ConfuserContext context, MethodDef method) {
4951
ProtectionParameters.GetParameters(context, method).Remove(this);
5052
}
5153

54+
class ModuleWriterSetupPhase : ProtectionPhase {
55+
public ModuleWriterSetupPhase(AntiTamperProtection parent) : base(parent) { }
56+
57+
/// <inheritdoc />
58+
public override ProtectionTargets Targets => ProtectionTargets.Methods;
59+
60+
/// <inheritdoc />
61+
public override string Name => "Anti-tamper module writer preparation";
62+
63+
/// <inheritdoc />
64+
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) {
65+
if (!parameters.Targets.Any()) return;
66+
67+
if (context.CurrentModuleWriterOptions is NativeModuleWriterOptions nativeOptions) {
68+
context.RequestNative(false);
69+
}
70+
}
71+
}
72+
5273
class InjectPhase : ProtectionPhase {
5374
public InjectPhase(AntiTamperProtection parent)
5475
: base(parent) { }

Confuser.Protections/AntiTamper/NormalMode.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ public void HandleMD(AntiTamperProtection parent, ConfuserContext context, Prote
100100
}
101101

102102
void WriterEvent(object sender, ModuleWriterEventArgs e) {
103-
var writer = (ModuleWriterBase)sender;
104-
if (e.Event == ModuleWriterEvent.MDEndCreateTables) {
105-
CreateSections(writer);
106-
}
107-
else if (e.Event == ModuleWriterEvent.BeginStrongNameSign) {
108-
EncryptSection(writer);
103+
switch (e.Event)
104+
{
105+
case ModuleWriterEvent.MDEndCreateTables:
106+
CreateSections(e.Writer);
107+
break;
108+
case ModuleWriterEvent.BeginStrongNameSign:
109+
EncryptSection(e.Writer);
110+
break;
109111
}
110112
}
111113

Confuser.Protections/Resources/MDPhase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ void OnWriterEvent(object sender, ModuleWriterEventArgs e) {
5656
}
5757
byte[] moduleBuff;
5858
using (var ms = new MemoryStream()) {
59-
module.Write(ms, new ModuleWriterOptions(e.Writer.Module) { StrongNameKey = writer.TheOptions.StrongNameKey, StrongNamePublicKey = writer.TheOptions.StrongNamePublicKey, DelaySign = writer.TheOptions.DelaySign });
59+
var options = new ModuleWriterOptions(module) {
60+
StrongNameKey = writer.TheOptions.StrongNameKey,
61+
StrongNamePublicKey = writer.TheOptions.StrongNamePublicKey,
62+
DelaySign = writer.TheOptions.DelaySign
63+
};
64+
module.Write(ms, options);
6065
moduleBuff = ms.ToArray();
6166
}
6267

Confuser.Protections/TypeScrambler/ScramblePhase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using dnlib.DotNet;
66

77
namespace Confuser.Protections.TypeScrambler {
8-
internal sealed class ScramblePhase : ProtectionPhase {
8+
sealed class ScramblePhase : ProtectionPhase {
99
public ScramblePhase(TypeScrambleProtection parent) : base(parent) { }
1010

1111
public override ProtectionTargets Targets => ProtectionTargets.Types | ProtectionTargets.Methods;
@@ -30,7 +30,8 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa
3030
foreach (var def in context.CurrentModule.FindDefinitions().WithProgress(context.Logger)) {
3131
switch (def) {
3232
case MethodDef md:
33-
md.ReturnType = rewriter.UpdateSignature(md.ReturnType);
33+
if (md.HasReturnType)
34+
md.ReturnType = rewriter.UpdateSignature(md.ReturnType);
3435
if (md.HasBody) {
3536
rewriter.ProcessBody(md);
3637
}

Confuser.Protections/TypeScrambler/Scrambler/ScannedItem.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ internal bool RegisterGeneric(TypeSig t) {
3535
// Get proper type.
3636
t = SignatureUtils.GetLeaf(t);
3737

38+
// scrambling voids leads to peverify errors, better leave them out.
39+
if (t.ElementType == ElementType.Void)
40+
return false;
41+
3842
if (!Generics.ContainsKey(t)) {
3943
GenericParam newGenericParam;
4044
if (t.IsGenericMethodParameter) {

Confuser.Protections/TypeScrambler/Scrambler/ScannedMethod.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ internal override void Scan() {
3939
}
4040
}
4141

42-
if (TargetMethod.ReturnType != TargetMethod.Module.CorLibTypes.Void) {
42+
if (TargetMethod.HasReturnType) {
4343
RegisterGeneric(TargetMethod.ReturnType);
4444
}
45+
4546
foreach (var param in TargetMethod.Parameters.Where(ProcessParameter))
4647
RegisterGeneric(param.Type);
4748

@@ -89,6 +90,8 @@ private static bool CanScrambleMethod(MethodDef method, bool scramblePublic) {
8990
// PInvoke implementations won't work with this.
9091
if (method.IsPinvokeImpl) return false;
9192

93+
if (method.DeclaringType.IsGlobalModuleType) return false;
94+
9295
return true;
9396
}
9497

@@ -129,7 +132,7 @@ protected override void PrepareGenerics(IEnumerable<GenericParam> scrambleParams
129132
$"{nameof(parameter)}.Type == {nameof(TargetMethod)}.MethodSig.Params[{nameof(parameter)}.MethodSigIndex]");
130133
}
131134

132-
if (TargetMethod.ReturnType != TargetMethod.Module.CorLibTypes.Void)
135+
if (TargetMethod.HasReturnType)
133136
TargetMethod.ReturnType = ConvertToGenericIfAvalible(TargetMethod.ReturnType);
134137

135138
Debug.Assert(TargetMethod.ReturnType == TargetMethod.MethodSig.RetType,

Confuser.Protections/TypeScrambler/Scrambler/ScannedType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ private static bool CanScrambleType(TypeDef type) {
3232
// Delegates are something that shouldn't be touched.
3333
if (type.IsDelegate) return false;
3434

35-
// No entrypoints
36-
if (type.Methods.Any(x => x.Module.EntryPoint == x)) return false;
35+
// No entrypoints or global types
36+
if (type.Methods.Any(x => x.Module.EntryPoint == x) || type.IsGlobalModuleType) return false;
3737

3838
if (type.IsValueType) return false;
3939

0 commit comments

Comments
 (0)