-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExecutableMemory.Unix.cs
51 lines (42 loc) · 1.5 KB
/
ExecutableMemory.Unix.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
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using static Posix;
public static unsafe partial class ExecutableMemory
{
static void* AllocateUnix(ReadOnlySpan<byte> code)
{
var length = (nuint)(uint)sizeof(Allocation) + (uint)code.Length - 1;
var alloc = (Allocation*)mmap(null, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (alloc is null)
ThrowExceptionForErrno(Marshal.GetLastPInvokeError());
// Store allocation size and advance pointer to code
alloc->Length = length;
code.CopyTo(new Span<byte>(alloc->Buffer, code.Length));
// TODO: Don't mark Allocation.Length as executable
if (mprotect(alloc, length, PROT_READ | PROT_EXEC) != 0)
ThrowExceptionForErrno(Marshal.GetLastPInvokeError());
__builtin___clear_cache(alloc->Buffer, alloc->Buffer + code.Length);
return alloc->Buffer;
}
static void FreeUnix(void* address)
{
var alloc = (Allocation*)((byte*)address - sizeof(nuint));
if (munmap(alloc, alloc->Length) != 0)
{
ThrowExceptionForErrno(Marshal.GetLastPInvokeError());
}
}
static void ThrowExceptionForErrno(int errno)
{
if (errno != 0)
{
throw new Win32Exception(errno);
}
}
struct Allocation
{
public nuint Length;
public fixed byte Buffer[1];
}
}