Quellcode durchsuchen

Split Unix allocator into OS-specific allocators

ghorsington vor 4 Jahren
Ursprung
Commit
a949c8d228

+ 54 - 0
BepInEx.IL2CPP/Hook/Allocator/LinuxPageAllocator.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using MonoMod.Utils;
+
+namespace BepInEx.IL2CPP.Allocator
+{
+	internal class LinuxPageAllocator : UnixPageAllocator
+	{
+		protected override IMemoryMapper OpenMemoryMap()
+		{
+			throw new NotImplementedException();
+		}
+
+		protected class LinuxMemoryMapper : IMemoryMapper
+		{
+			public void Dispose()
+			{
+				throw new NotImplementedException();
+			}
+
+			public bool FindNextFree(ref IntPtr start, ref ulong size)
+			{
+				throw new NotImplementedException();
+			}
+		}
+		
+		static class Unix
+		{
+			public delegate IntPtr mmapDelegate(IntPtr addr, UIntPtr length, int prot, int flags, int fd, int offset);
+			[DynDllImport("mmap")]
+			public static mmapDelegate mmap;
+
+			public delegate int munmapDelegate(IntPtr addr, UIntPtr length);
+			[DynDllImport("munmap")]
+			public static munmapDelegate munmap;
+			
+			public delegate IntPtr fdopenDelegate(int fd, string mode);
+			[DynDllImport("libc")]
+			public static fdopenDelegate fdopen;
+			
+			static Unix()
+			{
+				typeof(Unix).ResolveDynDllImports(new Dictionary<string, List<DynDllMapping>>
+				{
+					["libc"] = new List<DynDllMapping>
+					{
+						"libc.so.6", // Ubuntu glibc
+						"libc",      // Linux glibc
+					}
+				});
+			}
+		}
+	}
+}

+ 25 - 0
BepInEx.IL2CPP/Hook/Allocator/MacOsPageAllocator.cs

@@ -0,0 +1,25 @@
+using System;
+
+namespace BepInEx.IL2CPP.Allocator
+{
+	internal class MacOsPageAllocator : UnixPageAllocator
+	{
+		protected override IMemoryMapper OpenMemoryMap()
+		{
+			throw new System.NotImplementedException();
+		}
+		
+		protected class MacOsMemoryMapper : IMemoryMapper
+		{
+			public void Dispose()
+			{
+				throw new NotImplementedException();
+			}
+
+			public bool FindNextFree(ref IntPtr start, ref ulong size)
+			{
+				throw new NotImplementedException();
+			}
+		}
+	}
+}

+ 14 - 9
BepInEx.IL2CPP/Hook/Allocator/PageAllocator.cs

@@ -2,7 +2,7 @@
 using System.Runtime.CompilerServices;
 using MonoMod.Utils;
 
-namespace BepInEx.IL2CPP
+namespace BepInEx.IL2CPP.Allocator
 {
 	/// <summary>
 	///     A general purpose page allocator for patching purposes.
@@ -64,13 +64,18 @@ namespace BepInEx.IL2CPP
 			return int.MinValue <= diff && diff <= int.MaxValue;
 		}
 
-		private static PageAllocator Init()
-		{
-			if (PlatformHelper.Is(Platform.Windows))
-				return new WindowsPageAllocator();
-			if (PlatformHelper.Is(Platform.Unix))
-				return new UnixPageAllocator();
-			throw new NotImplementedException();
-		}
+		private static PageAllocator Init() =>
+			PlatformHelper.Current switch
+			{
+				var v when v.Is(Platform.Windows) => new WindowsPageAllocator(),
+				var v when v.Is(Platform.Linux)   => new LinuxPageAllocator(),
+				var v when v.Is(Platform.MacOS)   => new MacOsPageAllocator(),
+				_                                 => throw new NotImplementedException()
+			};
+	}
+	
+	internal static class PlatformExt
+	{
+		public static bool Is(this Platform pl, Platform val) => (pl & val) == val;
 	}
 }

+ 11 - 2
BepInEx.IL2CPP/Hook/Allocator/UnixPageAllocator.cs

@@ -1,12 +1,16 @@
 using System;
+using System.Collections.Generic;
+using MonoMod.Utils;
 
-namespace BepInEx.IL2CPP
+namespace BepInEx.IL2CPP.Allocator
 {
 	/// <summary>
 	///     Based on https://github.com/kubo/funchook
 	/// </summary>
-	internal class UnixPageAllocator : PageAllocator
+	internal abstract class UnixPageAllocator : PageAllocator
 	{
+		protected abstract IMemoryMapper OpenMemoryMap();
+		
 		public override IntPtr Allocate(IntPtr hint)
 		{
 			throw new NotImplementedException();
@@ -16,5 +20,10 @@ namespace BepInEx.IL2CPP
 		{
 			throw new NotImplementedException();
 		}
+
+		protected interface IMemoryMapper: IDisposable
+		{
+			bool FindNextFree(ref IntPtr start, ref ulong size);
+		}
 	}
 }

+ 1 - 1
BepInEx.IL2CPP/Hook/Allocator/WindowsPageAllocator.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
 
-namespace BepInEx.IL2CPP
+namespace BepInEx.IL2CPP.Allocator
 {
 	/// <summary>
 	///     Based on https://github.com/kubo/funchook

+ 1 - 0
BepInEx.IL2CPP/Hook/DetourGenerator.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
+using BepInEx.IL2CPP.Allocator;
 using BepInEx.Logging;
 using Iced.Intel;
 using MonoMod.RuntimeDetour;

+ 1 - 0
BepInEx.IL2CPP/Hook/FastNativeDetour.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Reflection;
 using System.Runtime.InteropServices;
+using BepInEx.IL2CPP.Allocator;
 using BepInEx.Logging;
 using MonoMod.RuntimeDetour;
 using MonoMod.Utils;