Skip to content

Commit 562a908

Browse files
committed
Fix DotNetHook
1 parent 00591f5 commit 562a908

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/BitMono.Protections/DotNetHook.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public DotNetHook(IRenamer renamer, RuntimeImplementations runtime)
1111
m_Random = runtime.Random;
1212
}
1313

14+
[SuppressMessage("ReSharper", "ForCanBeConvertedToForeach")]
15+
[SuppressMessage("ReSharper", "InvertIf")]
1416
public Task ExecuteAsync(ProtectionContext context, ProtectionParameters parameters)
1517
{
1618
var runtimeHookingType = context.RuntimeModule.ResolveOrThrow<TypeDefinition>(typeof(Hooking));
@@ -37,7 +39,7 @@ public Task ExecuteAsync(ProtectionContext context, ProtectionParameters paramet
3739
{
3840
var callingMethod = callingOperandMethod.Resolve();
3941
if (callingMethod is { CilMethodBody: { } }
40-
&& callingMethod.ParameterDefinitions.Any(p => p.IsIn || p.IsOut) == false)
42+
&& callingMethod.ParameterDefinitions.Any(p => p.IsIn || p.IsOut) == false && callingMethod.Managed)
4143
{
4244
if (context.Module.TryLookupMember(callingMethod.MetadataToken, out var callingMethodMetadata))
4345
{
@@ -50,16 +52,17 @@ public Task ExecuteAsync(ProtectionContext context, ProtectionParameters paramet
5052
var parameterDefinitions = callingMethod.ParameterDefinitions;
5153
for (var j = 0; j < parameterDefinitions.Count; j++)
5254
{
53-
var parameter = parameterDefinitions[i];
54-
dummyMethod.ParameterDefinitions.Add(new ParameterDefinition(parameter.Sequence, parameter.Name, parameter.Attributes));
55+
var actualParameter = parameterDefinitions[j];
56+
var parameter = new ParameterDefinition(actualParameter.Sequence,
57+
actualParameter.Name, actualParameter.Attributes);
58+
dummyMethod.ParameterDefinitions.Add(parameter);
5559
}
5660
var dummyMethodBody = dummyMethod.CilMethodBody = new CilMethodBody(dummyMethod);
5761
if (callingMethod.Signature.ReturnsValue)
5862
{
5963
dummyMethodBody.Instructions.Add(new CilInstruction(CilOpCodes.Ldnull));
6064
}
6165
dummyMethodBody.Instructions.Add(new CilInstruction(CilOpCodes.Ret));
62-
6366
var signature = MethodSignature.CreateStatic(systemVoid);
6467
var attributes = MethodAttributes.Assembly | MethodAttributes.Static;
6568
var initializationMethod = new MethodDefinition(m_Renamer.RenameUnsafely(), attributes, signature);

0 commit comments

Comments
 (0)