-
Notifications
You must be signed in to change notification settings - Fork 753
/
Copy pathOpCode.cs
185 lines (152 loc) · 6.37 KB
/
OpCode.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace NetMQ.Core.Utils
{
internal static class Opcode
{
private static IntPtr s_codeBuffer;
private static ulong s_size;
public static bool Open()
{
if (SocketOptions.DoNotUseRDTSC)
return false;
#if NETSTANDARD1_1_OR_GREATER || NET471_OR_GREATER
if (RuntimeInformation.ProcessArchitecture != Architecture.X86 &&
RuntimeInformation.ProcessArchitecture != Architecture.X64)
{
return false; // RDTSC instruction not supported
}
#endif
var p = (int)Environment.OSVersion.Platform;
byte[] rdtscCode = IntPtr.Size == 4 ? RDTSC_32 : RDTSC_64;
s_size = (ulong)(rdtscCode.Length);
if ((p == 4) || (p == 128)) // Unix || Mono on Unix
{
// Unix
if (IsARMArchitecture()) return false;
Assembly assembly = Assembly.Load("Mono.Posix");
Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
MethodInfo mmap = syscall.GetMethod("mmap");
Type mmapProts = assembly.GetType("Mono.Unix.Native.MmapProts");
object mmapProtsParam = Enum.ToObject(mmapProts,
(int)mmapProts.GetField("PROT_READ").GetValue(null) |
(int)mmapProts.GetField("PROT_WRITE").GetValue(null) |
(int)mmapProts.GetField("PROT_EXEC").GetValue(null));
Type mmapFlags = assembly.GetType("Mono.Unix.Native.MmapFlags");
object mmapFlagsParam = Enum.ToObject(mmapFlags,
(int)mmapFlags.GetField("MAP_ANONYMOUS").GetValue(null) |
(int)mmapFlags.GetField("MAP_PRIVATE").GetValue(null));
s_codeBuffer = (IntPtr)mmap.Invoke(null,
new[] { IntPtr.Zero, s_size, mmapProtsParam, mmapFlagsParam, -1, 0 });
if (s_codeBuffer == IntPtr.Zero || s_codeBuffer == (IntPtr)(-1))
{
throw new InvalidOperationException("Mmap failed");
}
}
else
{
// Windows
s_codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
(UIntPtr)s_size, AllocationType.COMMIT | AllocationType.RESERVE,
MemoryProtection.EXECUTE_READWRITE);
}
Marshal.Copy(rdtscCode, 0, s_codeBuffer, rdtscCode.Length);
Rdtsc = Marshal.GetDelegateForFunctionPointer(s_codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;
return true;
}
private static bool IsARMArchitecture()
{
// force to load from mono gac
Assembly currentAssembly = Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
Type syscall = currentAssembly.GetType("Mono.Unix.Native.Syscall");
Type utsname = currentAssembly.GetType("Mono.Unix.Native.Utsname");
MethodInfo uname = syscall.GetMethod("uname");
object?[] parameters = { null };
var invokeResult = (int)uname.Invoke(null, parameters);
if (invokeResult != 0)
return false;
var currentValues = parameters[0];
var machineValue = (string)utsname.GetField("machine").GetValue(currentValues);
return machineValue.ToLower().Contains("arm");
}
public static void Close()
{
Rdtsc = null;
var p = (int)Environment.OSVersion.Platform;
if ((p == 4) || (p == 128))
{
// Unix
Assembly assembly =
Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " +
"PublicKeyToken=0738eb9f132ed756");
Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
MethodInfo munmap = syscall.GetMethod("munmap");
munmap.Invoke(null, new object[] { s_codeBuffer, s_size });
}
else
{
// Windows
NativeMethods.VirtualFree(s_codeBuffer, UIntPtr.Zero, FreeType.RELEASE);
}
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate ulong RdtscDelegate();
public static RdtscDelegate? Rdtsc { get; private set; }
// unsigned __int64 __stdcall rdtsc() {
// return __rdtsc();
// }
private static readonly byte[] RDTSC_32 = {
0x0F, 0x31, // rdtsc
0xC3 // ret
};
private static readonly byte[] RDTSC_64 = {
0x0F, 0x31, // rdtsc
0x48, 0xC1, 0xE2, 0x20, // shl rdx, 20h
0x48, 0x0B, 0xC2, // or rax, rdx
0xC3 // ret
};
[Flags]
public enum AllocationType : uint
{
COMMIT = 0x1000,
RESERVE = 0x2000,
RESET = 0x80000,
LARGE_PAGES = 0x20000000,
PHYSICAL = 0x400000,
TOP_DOWN = 0x100000,
WRITE_WATCH = 0x200000
}
[Flags]
public enum MemoryProtection : uint
{
EXECUTE = 0x10,
EXECUTE_READ = 0x20,
EXECUTE_READWRITE = 0x40,
EXECUTE_WRITECOPY = 0x80,
NOACCESS = 0x01,
READONLY = 0x02,
READWRITE = 0x04,
WRITECOPY = 0x08,
GUARD = 0x100,
NOCACHE = 0x200,
WRITECOMBINE = 0x400
}
[Flags]
public enum FreeType
{
DECOMMIT = 0x4000,
RELEASE = 0x8000
}
private static class NativeMethods
{
private const string Kernel = "kernel32.dll";
[DllImport(Kernel, CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize,
AllocationType flAllocationType, MemoryProtection flProtect);
[DllImport(Kernel, CallingConvention = CallingConvention.Winapi)]
public static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize,
FreeType dwFreeType);
}
}
}