TamperMachine.cs 5.6 KB

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