TamperedKProcess.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.HLE.HOS.Kernel.Process;
  4. using System.Runtime.CompilerServices;
  5. namespace Ryujinx.HLE.HOS.Tamper
  6. {
  7. class TamperedKProcess : ITamperedProcess
  8. {
  9. private KProcess _process;
  10. public ProcessState State => _process.State;
  11. public bool TamperedCodeMemory { get; set; } = false;
  12. public TamperedKProcess(KProcess process)
  13. {
  14. _process = process;
  15. }
  16. private void AssertMemoryRegion<T>(ulong va, bool isWrite) where T : unmanaged
  17. {
  18. ulong size = (ulong)Unsafe.SizeOf<T>();
  19. // TODO (Caian): This double check is workaround because CpuMemory.IsRangeMapped reports
  20. // some addresses as mapped even though they are not, i. e. 4 bytes from 0xffffffffffffff70.
  21. if (!_process.CpuMemory.IsMapped(va) || !_process.CpuMemory.IsRangeMapped(va, size))
  22. {
  23. throw new TamperExecutionException($"Unmapped memory access of {size} bytes at 0x{va:X16}");
  24. }
  25. if (!isWrite)
  26. {
  27. return;
  28. }
  29. // TODO (Caian): The JIT does not support invalidating a code region so writing to code memory may not work
  30. // as intended, so taint the operation to issue a warning later.
  31. if (isWrite && (va >= _process.MemoryManager.CodeRegionStart) && (va + size <= _process.MemoryManager.CodeRegionEnd))
  32. {
  33. TamperedCodeMemory = true;
  34. }
  35. }
  36. public T ReadMemory<T>(ulong va) where T : unmanaged
  37. {
  38. AssertMemoryRegion<T>(va, false);
  39. return _process.CpuMemory.Read<T>(va);
  40. }
  41. public void WriteMemory<T>(ulong va, T value) where T : unmanaged
  42. {
  43. AssertMemoryRegion<T>(va, true);
  44. _process.CpuMemory.Write(va, value);
  45. }
  46. public void PauseProcess()
  47. {
  48. Logger.Warning?.Print(LogClass.TamperMachine, "Process pausing is not supported!");
  49. }
  50. public void ResumeProcess()
  51. {
  52. Logger.Warning?.Print(LogClass.TamperMachine, "Process resuming is not supported!");
  53. }
  54. }
  55. }