This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeCodePage.cs
More file actions
291 lines (258 loc) · 10.7 KB
/
NativeCodePage.cs
File metadata and controls
291 lines (258 loc) · 10.7 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Python.Runtime.Platform
{
class NativeCodePageHelper
{
/// <summary>
/// Gets the operating system as reported by python's platform.system().
/// </summary>
public static OperatingSystemType OperatingSystem { get; private set; }
/// <summary>
/// Gets the operating system as reported by python's platform.system().
/// </summary>
[Obsolete]
public static string OperatingSystemName => PythonEngine.Platform;
/// <summary>
/// Gets the machine architecture as reported by python's platform.machine().
/// </summary>
public static MachineType Machine { get; private set; }/* set in Initialize using python's platform.machine */
/// <summary>
/// Gets the machine architecture as reported by python's platform.machine().
/// </summary>
[Obsolete]
public static string MachineName { get; private set; }
/// <summary>
/// Initialized by InitializeNativeCodePage.
///
/// This points to a page of memory allocated using mmap or VirtualAlloc
/// (depending on the system), and marked read and execute (not write).
/// Very much on purpose, the page is *not* released on a shutdown and
/// is instead leaked. See the TestDomainReload test case.
///
/// The contents of the page are two native functions: one that returns 0,
/// one that returns 1.
///
/// If python didn't keep its gc list through a Py_Finalize we could remove
/// this entire section.
/// </summary>
internal static IntPtr NativeCodePage = IntPtr.Zero;
static readonly Dictionary<string, OperatingSystemType> OperatingSystemTypeMapping = new Dictionary<string, OperatingSystemType>()
{
{ "Windows", OperatingSystemType.Windows },
{ "Darwin", OperatingSystemType.Darwin },
{ "Linux", OperatingSystemType.Linux },
};
/// <summary>
/// Map lower-case version of the python machine name to the processor
/// type. There are aliases, e.g. x86_64 and amd64 are two names for
/// the same thing. Make sure to lower-case the search string, because
/// capitalization can differ.
/// </summary>
static readonly Dictionary<string, MachineType> MachineTypeMapping = new Dictionary<string, MachineType>()
{
["i386"] = MachineType.i386,
["i686"] = MachineType.i386,
["x86"] = MachineType.i386,
["x86_64"] = MachineType.x86_64,
["amd64"] = MachineType.x86_64,
["x64"] = MachineType.x86_64,
["em64t"] = MachineType.x86_64,
["armv7l"] = MachineType.armv7l,
["armv8"] = MachineType.armv8,
["aarch64"] = MachineType.aarch64,
};
/// <summary>
/// Structure to describe native code.
///
/// Use NativeCode.Active to get the native code for the current platform.
///
/// Generate the code by creating the following C code:
/// <code>
/// int Return0() { return 0; }
/// int Return1() { return 1; }
/// </code>
/// Then compiling on the target platform, e.g. with gcc or clang:
/// <code>cc -c -fomit-frame-pointer -O2 foo.c</code>
/// And then analyzing the resulting functions with a hex editor, e.g.:
/// <code>objdump -disassemble foo.o</code>
/// </summary>
internal class NativeCode
{
/// <summary>
/// The code, as a string of bytes.
/// </summary>
public byte[] Code { get; private set; }
/// <summary>
/// Where does the "return 0" function start?
/// </summary>
public int Return0 { get; private set; }
/// <summary>
/// Where does the "return 1" function start?
/// </summary>
public int Return1 { get; private set; }
public static NativeCode Active
{
get
{
switch (Machine)
{
case MachineType.i386:
return I386;
case MachineType.x86_64:
return X86_64;
default:
return null;
}
}
}
/// <summary>
/// Code for x86_64. See the class comment for how it was generated.
/// </summary>
public static readonly NativeCode X86_64 = new NativeCode()
{
Return0 = 0x10,
Return1 = 0,
Code = new byte[]
{
// First Return1:
0xb8, 0x01, 0x00, 0x00, 0x00, // movl $1, %eax
0xc3, // ret
// Now some padding so that Return0 can be 16-byte-aligned.
// I put Return1 first so there's not as much padding to type in.
0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, // nop
// Now Return0.
0x31, 0xc0, // xorl %eax, %eax
0xc3, // ret
}
};
/// <summary>
/// Code for X86.
///
/// It's bitwise identical to X86_64, so we just point to it.
/// <see cref="NativeCode.X86_64"/>
/// </summary>
public static readonly NativeCode I386 = X86_64;
}
/// <summary>
/// Platform-dependent mmap and mprotect.
/// </summary>
internal interface IMemoryMapper
{
/// <summary>
/// Map at least numBytes of memory. Mark the page read-write (but not exec).
/// </summary>
IntPtr MapWriteable(int numBytes);
/// <summary>
/// Sets the mapped memory to be read-exec (but not write).
/// </summary>
void SetReadExec(IntPtr mappedMemory, int numBytes);
}
class WindowsMemoryMapper : IMemoryMapper
{
const UInt32 MEM_COMMIT = 0x1000;
const UInt32 MEM_RESERVE = 0x2000;
const UInt32 PAGE_READWRITE = 0x04;
const UInt32 PAGE_EXECUTE_READ = 0x20;
[DllImport("kernel32.dll")]
static extern IntPtr VirtualAlloc(IntPtr lpAddress, IntPtr dwSize, UInt32 flAllocationType, UInt32 flProtect);
[DllImport("kernel32.dll")]
static extern bool VirtualProtect(IntPtr lpAddress, IntPtr dwSize, UInt32 flNewProtect, out UInt32 lpflOldProtect);
public IntPtr MapWriteable(int numBytes)
{
return VirtualAlloc(IntPtr.Zero, new IntPtr(numBytes),
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
}
public void SetReadExec(IntPtr mappedMemory, int numBytes)
{
UInt32 _;
VirtualProtect(mappedMemory, new IntPtr(numBytes), PAGE_EXECUTE_READ, out _);
}
}
class UnixMemoryMapper : IMemoryMapper
{
const int PROT_READ = 0x1;
const int PROT_WRITE = 0x2;
const int PROT_EXEC = 0x4;
const int MAP_PRIVATE = 0x2;
int MAP_ANONYMOUS
{
get
{
switch (OperatingSystem)
{
case OperatingSystemType.Darwin:
return 0x1000;
case OperatingSystemType.Linux:
return 0x20;
default:
throw new NotImplementedException($"mmap is not supported on {OperatingSystemName}");
}
}
}
[DllImport("libc")]
static extern IntPtr mmap(IntPtr addr, IntPtr len, int prot, int flags, int fd, IntPtr offset);
[DllImport("libc")]
static extern int mprotect(IntPtr addr, IntPtr len, int prot);
public IntPtr MapWriteable(int numBytes)
{
// MAP_PRIVATE must be set on linux, even though MAP_ANON implies it.
// It doesn't hurt on darwin, so just do it.
return mmap(IntPtr.Zero, new IntPtr(numBytes), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, IntPtr.Zero);
}
public void SetReadExec(IntPtr mappedMemory, int numBytes)
{
mprotect(mappedMemory, new IntPtr(numBytes), PROT_READ | PROT_EXEC);
}
}
/// <summary>
/// Initializes the data about platforms.
///
/// This must be the last step when initializing the runtime:
/// GetManagedString needs to have the cached values for types.
/// But it must run before initializing anything outside the runtime
/// because those rely on the platform data.
/// </summary>
public static void InitializePlatformData()
{
MachineName = SystemInfo.GetArchitecture();
Machine = SystemInfo.GetMachineType();
OperatingSystem = SystemInfo.GetSystemType();
}
internal static IMemoryMapper CreateMemoryMapper()
{
switch (OperatingSystem)
{
case OperatingSystemType.Darwin:
case OperatingSystemType.Linux:
return new UnixMemoryMapper();
case OperatingSystemType.Windows:
return new WindowsMemoryMapper();
default:
throw new NotImplementedException($"No support for {OperatingSystemName}");
}
}
/// <summary>
/// Initializes the native code page.
///
/// Safe to call if we already initialized (this function is idempotent).
/// <see cref="NativeCodePage"/>
/// </summary>
internal static void InitializeNativeCodePage()
{
// Do nothing if we already initialized.
if (NativeCodePage != IntPtr.Zero)
{
return;
}
// Allocate the page, write the native code into it, then set it
// to be executable.
IMemoryMapper mapper = CreateMemoryMapper();
int codeLength = NativeCode.Active.Code.Length;
NativeCodePage = mapper.MapWriteable(codeLength);
Marshal.Copy(NativeCode.Active.Code, 0, NativeCodePage, codeLength);
mapper.SetReadExec(NativeCodePage, codeLength);
}
}
}