TamperMachine.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.HLE.HOS.Kernel;
  4. using Ryujinx.HLE.HOS.Kernel.Process;
  5. using Ryujinx.HLE.HOS.Services.Hid;
  6. using Ryujinx.HLE.HOS.Tamper;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. namespace Ryujinx.HLE.HOS
  12. {
  13. public class TamperMachine
  14. {
  15. private Thread _tamperThread = null;
  16. private ConcurrentQueue<ITamperProgram> _programs = new ConcurrentQueue<ITamperProgram>();
  17. private long _pressedKeys = 0;
  18. private void Activate()
  19. {
  20. if (_tamperThread == null || !_tamperThread.IsAlive)
  21. {
  22. _tamperThread = new Thread(this.TamperRunner);
  23. _tamperThread.Name = "HLE.TamperMachine";
  24. _tamperThread.Start();
  25. }
  26. }
  27. internal void InstallAtmosphereCheat(IEnumerable<string> rawInstructions, ProcessTamperInfo info, ulong exeAddress)
  28. {
  29. if (!CanInstallOnPid(info.Process.Pid))
  30. {
  31. return;
  32. }
  33. ITamperedProcess tamperedProcess = new TamperedKProcess(info.Process);
  34. AtmosphereCompiler compiler = new AtmosphereCompiler();
  35. ITamperProgram program = compiler.Compile(rawInstructions, exeAddress, info.HeapAddress, tamperedProcess);
  36. if (program != null)
  37. {
  38. _programs.Enqueue(program);
  39. }
  40. Activate();
  41. }
  42. private bool CanInstallOnPid(long pid)
  43. {
  44. // Do not allow tampering of kernel processes.
  45. if (pid < KernelConstants.InitialProcessId)
  46. {
  47. Logger.Warning?.Print(LogClass.TamperMachine, $"Refusing to tamper kernel process {pid}");
  48. return false;
  49. }
  50. return true;
  51. }
  52. private bool IsProcessValid(ITamperedProcess process)
  53. {
  54. return process.State != ProcessState.Crashed && process.State != ProcessState.Exiting && process.State != ProcessState.Exited;
  55. }
  56. private void TamperRunner()
  57. {
  58. Logger.Info?.Print(LogClass.TamperMachine, "TamperMachine thread running");
  59. int sleepCounter = 0;
  60. while (true)
  61. {
  62. // Sleep to not consume too much CPU.
  63. if (sleepCounter == 0)
  64. {
  65. sleepCounter = _programs.Count;
  66. Thread.Sleep(1);
  67. }
  68. else
  69. {
  70. sleepCounter--;
  71. }
  72. if (!AdvanceTamperingsQueue())
  73. {
  74. // No more work to be done.
  75. Logger.Info?.Print(LogClass.TamperMachine, "TamperMachine thread exiting");
  76. return;
  77. }
  78. }
  79. }
  80. private bool AdvanceTamperingsQueue()
  81. {
  82. if (!_programs.TryDequeue(out ITamperProgram program))
  83. {
  84. // No more programs in the queue.
  85. return false;
  86. }
  87. // Check if the process is still suitable for running the tamper program.
  88. if (!IsProcessValid(program.Process))
  89. {
  90. // Exit without re-enqueuing the program because the process is no longer valid.
  91. return true;
  92. }
  93. // Re-enqueue the tampering program because the process is still valid.
  94. _programs.Enqueue(program);
  95. Logger.Debug?.Print(LogClass.TamperMachine, "Running tampering program");
  96. try
  97. {
  98. ControllerKeys pressedKeys = (ControllerKeys)Thread.VolatileRead(ref _pressedKeys);
  99. program.Execute(pressedKeys);
  100. }
  101. catch (CodeRegionTamperedException ex)
  102. {
  103. Logger.Debug?.Print(LogClass.TamperMachine, $"Prevented tampering program from modifing code memory");
  104. if (!String.IsNullOrEmpty(ex.Message))
  105. {
  106. Logger.Debug?.Print(LogClass.TamperMachine, ex.Message);
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. Logger.Debug?.Print(LogClass.TamperMachine, $"The tampering program crashed, this can happen while the game is starting");
  112. if (!String.IsNullOrEmpty(ex.Message))
  113. {
  114. Logger.Debug?.Print(LogClass.TamperMachine, ex.Message);
  115. }
  116. }
  117. return true;
  118. }
  119. public void UpdateInput(List<GamepadInput> gamepadInputs)
  120. {
  121. // Look for the input of the player one or the handheld.
  122. foreach (GamepadInput input in gamepadInputs)
  123. {
  124. if (input.PlayerId == PlayerIndex.Player1 || input.PlayerId == PlayerIndex.Handheld)
  125. {
  126. Thread.VolatileWrite(ref _pressedKeys, (long)input.Buttons);
  127. return;
  128. }
  129. }
  130. // Clear the input because player one is not conected.
  131. Thread.VolatileWrite(ref _pressedKeys, 0);
  132. }
  133. }
  134. }