-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
79 lines (61 loc) · 2.22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using MonoMod.Core.Platforms;
using MonoMod.RuntimeDetour;
using MonoMod.Utils;
namespace MonoModTest
{
public static class Program
{
public static void Main(string[] args)
{
Test();
MonoTest();
}
private static void Test()
{
unsafe {
var clib = PlatformDetection.OS switch {
OSKind.Windows => "msvcrt",
_ => "c"
};
var msvcrt = DynDll.OpenLibrary(clib);
var msvcrand = (delegate* unmanaged[Cdecl]<int>) DynDll.GetExport(msvcrt, "rand");
using (new NativeHook((IntPtr) msvcrand, FakeRandom)) {
Helpers.Assert(msvcrand() == 1);
}
using (new Hook(() => EvenMoreFakeRandom(), FakeRandom)) {
Helpers.Assert(EvenMoreFakeRandom() == 1);
}
}
}
private static int FakeRandom(msvcrandDelegate orig)
{
return 1;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int EvenMoreFakeRandom()
{
return 0;
}
private delegate int msvcrandDelegate();
private static void MonoTest()
{
if(Type.GetType ("Mono.Runtime") is null)
return;
var original = Assembly.GetExecutingAssembly();
var getAssemblyMethod = typeof(Assembly).GetMethod(nameof(Assembly.GetExecutingAssembly), new Type[] { });
using var getAssemblyHookNative = new NativeHook(PlatformTriple.Current.GetNativeMethodBody(getAssemblyMethod), GetAssemblyFix);
var patched = Assembly.GetExecutingAssembly();
Helpers.Assert(Equals(original, patched));
}
delegate Assembly GetAssemblyDelegate();
private static Assembly GetAssemblyFix(GetAssemblyDelegate orig)
{
// return orig();
return Assembly.GetAssembly(typeof(Program));
}
}
}