hle_ipc.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <catch.hpp>
  5. #include "core/hle/ipc.h"
  6. #include "core/hle/kernel/client_port.h"
  7. #include "core/hle/kernel/client_session.h"
  8. #include "core/hle/kernel/event.h"
  9. #include "core/hle/kernel/handle_table.h"
  10. #include "core/hle/kernel/hle_ipc.h"
  11. #include "core/hle/kernel/process.h"
  12. #include "core/hle/kernel/server_session.h"
  13. namespace Kernel {
  14. static SharedPtr<Object> MakeObject() {
  15. return Event::Create(ResetType::OneShot);
  16. }
  17. TEST_CASE("HLERequestContext::PopoulateFromIncomingCommandBuffer", "[core][kernel]") {
  18. auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair());
  19. HLERequestContext context(std::move(session));
  20. auto process = Process::Create(CodeSet::Create("", 0));
  21. HandleTable handle_table;
  22. SECTION("works with empty cmdbuf") {
  23. const u32_le input[]{
  24. IPC::MakeHeader(0x1234, 0, 0),
  25. };
  26. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  27. REQUIRE(context.CommandBuffer()[0] == 0x12340000);
  28. }
  29. SECTION("translates regular params") {
  30. const u32_le input[]{
  31. IPC::MakeHeader(0, 3, 0), 0x12345678, 0x21122112, 0xAABBCCDD,
  32. };
  33. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  34. auto* output = context.CommandBuffer();
  35. REQUIRE(output[1] == 0x12345678);
  36. REQUIRE(output[2] == 0x21122112);
  37. REQUIRE(output[3] == 0xAABBCCDD);
  38. }
  39. SECTION("translates move handles") {
  40. auto a = MakeObject();
  41. Handle a_handle = handle_table.Create(a).Unwrap();
  42. const u32_le input[]{
  43. IPC::MakeHeader(0, 0, 2), IPC::MoveHandleDesc(1), a_handle,
  44. };
  45. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  46. auto* output = context.CommandBuffer();
  47. REQUIRE(context.GetIncomingHandle(output[2]) == a);
  48. REQUIRE(handle_table.GetGeneric(a_handle) == nullptr);
  49. }
  50. SECTION("translates copy handles") {
  51. auto a = MakeObject();
  52. Handle a_handle = handle_table.Create(a).Unwrap();
  53. const u32_le input[]{
  54. IPC::MakeHeader(0, 0, 2), IPC::CopyHandleDesc(1), a_handle,
  55. };
  56. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  57. auto* output = context.CommandBuffer();
  58. REQUIRE(context.GetIncomingHandle(output[2]) == a);
  59. REQUIRE(handle_table.GetGeneric(a_handle) == a);
  60. }
  61. SECTION("translates multi-handle descriptors") {
  62. auto a = MakeObject();
  63. auto b = MakeObject();
  64. auto c = MakeObject();
  65. const u32_le input[]{
  66. IPC::MakeHeader(0, 0, 5), IPC::MoveHandleDesc(2),
  67. handle_table.Create(a).Unwrap(), handle_table.Create(b).Unwrap(),
  68. IPC::MoveHandleDesc(1), handle_table.Create(c).Unwrap(),
  69. };
  70. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  71. auto* output = context.CommandBuffer();
  72. REQUIRE(context.GetIncomingHandle(output[2]) == a);
  73. REQUIRE(context.GetIncomingHandle(output[3]) == b);
  74. REQUIRE(context.GetIncomingHandle(output[5]) == c);
  75. }
  76. SECTION("translates CallingPid descriptors") {
  77. const u32_le input[]{
  78. IPC::MakeHeader(0, 0, 2), IPC::CallingPidDesc(), 0x98989898,
  79. };
  80. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  81. REQUIRE(context.CommandBuffer()[2] == process->process_id);
  82. }
  83. SECTION("translates mixed params") {
  84. auto a = MakeObject();
  85. const u32_le input[]{
  86. IPC::MakeHeader(0, 2, 4),
  87. 0x12345678,
  88. 0xABCDEF00,
  89. IPC::MoveHandleDesc(1),
  90. handle_table.Create(a).Unwrap(),
  91. IPC::CallingPidDesc(),
  92. 0,
  93. };
  94. context.PopulateFromIncomingCommandBuffer(input, *process, handle_table);
  95. auto* output = context.CommandBuffer();
  96. REQUIRE(output[1] == 0x12345678);
  97. REQUIRE(output[2] == 0xABCDEF00);
  98. REQUIRE(context.GetIncomingHandle(output[4]) == a);
  99. REQUIRE(output[6] == process->process_id);
  100. }
  101. }
  102. TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") {
  103. auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair());
  104. HLERequestContext context(std::move(session));
  105. auto process = Process::Create(CodeSet::Create("", 0));
  106. HandleTable handle_table;
  107. auto* input = context.CommandBuffer();
  108. u32_le output[IPC::COMMAND_BUFFER_LENGTH];
  109. SECTION("works with empty cmdbuf") {
  110. input[0] = IPC::MakeHeader(0x1234, 0, 0);
  111. context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
  112. REQUIRE(output[0] == 0x12340000);
  113. }
  114. SECTION("translates regular params") {
  115. input[0] = IPC::MakeHeader(0, 3, 0);
  116. input[1] = 0x12345678;
  117. input[2] = 0x21122112;
  118. input[3] = 0xAABBCCDD;
  119. context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
  120. REQUIRE(output[1] == 0x12345678);
  121. REQUIRE(output[2] == 0x21122112);
  122. REQUIRE(output[3] == 0xAABBCCDD);
  123. }
  124. SECTION("translates move/copy handles") {
  125. auto a = MakeObject();
  126. auto b = MakeObject();
  127. input[0] = IPC::MakeHeader(0, 0, 4);
  128. input[1] = IPC::MoveHandleDesc(1);
  129. input[2] = context.AddOutgoingHandle(a);
  130. input[3] = IPC::CopyHandleDesc(1);
  131. input[4] = context.AddOutgoingHandle(b);
  132. context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
  133. REQUIRE(handle_table.GetGeneric(output[2]) == a);
  134. REQUIRE(handle_table.GetGeneric(output[4]) == b);
  135. }
  136. SECTION("translates multi-handle descriptors") {
  137. auto a = MakeObject();
  138. auto b = MakeObject();
  139. auto c = MakeObject();
  140. input[0] = IPC::MakeHeader(0, 0, 5);
  141. input[1] = IPC::MoveHandleDesc(2);
  142. input[2] = context.AddOutgoingHandle(a);
  143. input[3] = context.AddOutgoingHandle(b);
  144. input[4] = IPC::CopyHandleDesc(1);
  145. input[5] = context.AddOutgoingHandle(c);
  146. context.WriteToOutgoingCommandBuffer(output, *process, handle_table);
  147. REQUIRE(handle_table.GetGeneric(output[2]) == a);
  148. REQUIRE(handle_table.GetGeneric(output[3]) == b);
  149. REQUIRE(handle_table.GetGeneric(output[5]) == c);
  150. }
  151. }
  152. } // namespace Kernel