Interface.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Ryujinx.Tests.Unicorn.Native.Const;
  2. using System;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Tests.Unicorn.Native
  7. {
  8. public static class Interface
  9. {
  10. public static bool IsUnicornAvailable { get; private set; } = true;
  11. private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
  12. {
  13. if (libraryName == "unicorn")
  14. {
  15. string loadPath = $"{Path.GetDirectoryName(assembly.Location)}/";
  16. loadPath += OperatingSystem.IsWindows() ? $"{libraryName}.dll" : $"lib{libraryName}.so";
  17. if (!NativeLibrary.TryLoad(loadPath, out IntPtr libraryPtr))
  18. {
  19. IsUnicornAvailable = false;
  20. Console.Error.WriteLine($"ERROR: Could not find unicorn at: {loadPath}");
  21. }
  22. return libraryPtr;
  23. }
  24. // Otherwise, fallback to default import resolver.
  25. return IntPtr.Zero;
  26. }
  27. static Interface()
  28. {
  29. NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), ImportResolver);
  30. }
  31. public static void Checked(Error error)
  32. {
  33. if (error != Error.OK)
  34. {
  35. throw new UnicornException(error);
  36. }
  37. }
  38. public static void MarshalArrayOf<T>(IntPtr input, int length, out T[] output)
  39. {
  40. int size = Marshal.SizeOf(typeof(T));
  41. output = new T[length];
  42. for (int i = 0; i < length; i++)
  43. {
  44. IntPtr item = new IntPtr(input.ToInt64() + i * size);
  45. output[i] = Marshal.PtrToStructure<T>(item);
  46. }
  47. }
  48. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  49. public static extern uint uc_version(out uint major, out uint minor);
  50. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  51. public static extern Error uc_open(Arch arch, Mode mode, out IntPtr uc);
  52. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  53. public static extern Error uc_close(IntPtr uc);
  54. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  55. public static extern IntPtr uc_strerror(Error err);
  56. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  57. public static extern Error uc_reg_write(IntPtr uc, int regid, byte[] value);
  58. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  59. public static extern Error uc_reg_read(IntPtr uc, int regid, byte[] value);
  60. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  61. public static extern Error uc_mem_write(IntPtr uc, ulong address, byte[] bytes, ulong size);
  62. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  63. public static extern Error uc_mem_read(IntPtr uc, ulong address, byte[] bytes, ulong size);
  64. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  65. public static extern Error uc_emu_start(IntPtr uc, ulong begin, ulong until, ulong timeout, ulong count);
  66. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  67. public static extern Error uc_mem_map(IntPtr uc, ulong address, ulong size, uint perms);
  68. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  69. public static extern Error uc_mem_unmap(IntPtr uc, ulong address, ulong size);
  70. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  71. public static extern Error uc_mem_protect(IntPtr uc, ulong address, ulong size, uint perms);
  72. [DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)]
  73. public static extern Error uc_mem_regions(IntPtr uc, out IntPtr regions, out uint count);
  74. }
  75. }