sharedmem_ipc_server.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SANDBOX_WIN_SRC_SHAREDMEM_IPC_SERVER_H_
  5. #define SANDBOX_WIN_SRC_SHAREDMEM_IPC_SERVER_H_
  6. #include <stdint.h>
  7. #include <list>
  8. #include <memory>
  9. #include "base/gtest_prod_util.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/win/scoped_handle.h"
  12. #include "sandbox/win/src/crosscall_params.h"
  13. #include "sandbox/win/src/crosscall_server.h"
  14. #include "sandbox/win/src/sharedmem_ipc_client.h"
  15. #include "sandbox/win/src/threadpool.h"
  16. // IPC transport implementation that uses shared memory.
  17. // This is the server side
  18. //
  19. // The server side has knowledge about the layout of the shared memory
  20. // and the state transitions. Both are explained in sharedmem_ipc_client.h
  21. //
  22. // As opposed to SharedMemIPClient, the Server object should be one for the
  23. // entire lifetime of the target process. The server is in charge of creating
  24. // the events (ping, pong) both for the client and for the target that are used
  25. // to signal the IPC and also in charge of setting the initial state of the
  26. // channels.
  27. //
  28. // When an IPC is ready, the server relies on being called by on the
  29. // ThreadPingEventReady callback. The IPC server then retrieves the buffer,
  30. // marshals it into a CrossCallParam object and calls the Dispatcher, who is in
  31. // charge of fulfilling the IPC request.
  32. namespace sandbox {
  33. // the shared memory implementation of the IPC server. There should be one
  34. // of these objects per target (IPC client) process
  35. class SharedMemIPCServer {
  36. public:
  37. // Creates the IPC server.
  38. // target_process: handle to the target process. It must be suspended. It is
  39. // unfortunate to receive a raw handle (and store it inside this object) as
  40. // that dilutes ownership of the process, but in practice a SharedMemIPCServer
  41. // is owned by TargetProcess, which calls this method, and owns the handle, so
  42. // everything is safe. If that changes, we should break this dependency and
  43. // duplicate the handle instead.
  44. // target_process_id: process id of the target process.
  45. // thread_pool: a thread pool object.
  46. // dispatcher: an object that can service IPC calls.
  47. SharedMemIPCServer(HANDLE target_process,
  48. DWORD target_process_id,
  49. ThreadPool* thread_pool,
  50. Dispatcher* dispatcher);
  51. SharedMemIPCServer(const SharedMemIPCServer&) = delete;
  52. SharedMemIPCServer& operator=(const SharedMemIPCServer&) = delete;
  53. ~SharedMemIPCServer();
  54. // Initializes the server structures, shared memory structures and
  55. // creates the kernels events used to signal the IPC.
  56. bool Init(void* shared_mem, uint32_t shared_size, uint32_t channel_size);
  57. private:
  58. // Allow tests to be marked DISABLED_. Note that FLAKY_ and FAILS_ prefixes
  59. // do not work with sandbox tests.
  60. FRIEND_TEST_ALL_PREFIXES(IPCTest, SharedMemServerTests);
  61. // When an event fires (IPC request). A thread from the ThreadPool
  62. // will call this function. The context parameter should be the same as
  63. // provided when ThreadPool::RegisterWait was called.
  64. static void __stdcall ThreadPingEventReady(void* context, unsigned char);
  65. // Makes the client and server events. This function is called once
  66. // per channel.
  67. bool MakeEvents(base::win::ScopedHandle* server_ping,
  68. base::win::ScopedHandle* server_pong,
  69. HANDLE* client_ping,
  70. HANDLE* client_pong);
  71. // A copy this structure is maintained per channel.
  72. // Note that a lot of the fields are just the same of what we have in the IPC
  73. // object itself. It is better to have the copies since we can dispatch in the
  74. // static method without worrying about converting back to a member function
  75. // call or about threading issues.
  76. struct ServerControl {
  77. ServerControl();
  78. ~ServerControl();
  79. // This channel server ping event.
  80. base::win::ScopedHandle ping_event;
  81. // This channel server pong event.
  82. base::win::ScopedHandle pong_event;
  83. // The size of this channel.
  84. uint32_t channel_size;
  85. // The pointer to the actual channel data.
  86. raw_ptr<char> channel_buffer;
  87. // The pointer to the base of the shared memory.
  88. raw_ptr<char> shared_base;
  89. // A pointer to this channel's client-side control structure this structure
  90. // lives in the shared memory.
  91. raw_ptr<ChannelControl> channel;
  92. // the IPC dispatcher associated with this channel.
  93. raw_ptr<Dispatcher> dispatcher;
  94. // The target process information associated with this channel.
  95. ClientInfo target_info;
  96. };
  97. // Looks for the appropriate handler for this IPC and invokes it.
  98. static bool InvokeCallback(const ServerControl* service_context,
  99. void* ipc_buffer,
  100. CrossCallReturn* call_result);
  101. // Points to the shared memory channel control which lives at
  102. // the start of the shared section.
  103. //
  104. // `client_control_` is not a raw_ptr<IPCControl>, because reinterpret_cast of
  105. // uninitialized memory to raw_ptr can cause ref-counting mismatch.
  106. RAW_PTR_EXCLUSION IPCControl* client_control_;
  107. // Keeps track of the server side objects that are used to answer an IPC.
  108. std::list<std::unique_ptr<ServerControl>> server_contexts_;
  109. // The thread pool provides the threads that call back into this object
  110. // when the IPC events fire.
  111. raw_ptr<ThreadPool> thread_pool_;
  112. // The IPC object is associated with a target process.
  113. HANDLE target_process_;
  114. // The target process id associated with the IPC object.
  115. DWORD target_process_id_;
  116. // The dispatcher handles 'ready' IPC calls.
  117. //
  118. // `call_dispatcher_` is not a raw_ptr<Dispatcher>, because reinterpret_cast
  119. // of uninitialized memory to raw_ptr can cause ref-counting mismatch.
  120. RAW_PTR_EXCLUSION Dispatcher* call_dispatcher_;
  121. };
  122. } // namespace sandbox
  123. #endif // SANDBOX_WIN_SRC_SHAREDMEM_IPC_SERVER_H_