nacl_process_host.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_
  5. #define COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "build/build_config.h"
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include <vector>
  11. #include "base/files/file.h"
  12. #include "base/files/file_path.h"
  13. #include "base/memory/read_only_shared_memory_region.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/process/process.h"
  17. #include "components/nacl/common/nacl_types.h"
  18. #include "content/public/browser/browser_child_process_host_delegate.h"
  19. #include "content/public/browser/browser_child_process_host_iterator.h"
  20. #include "ipc/ipc_channel_handle.h"
  21. #include "net/socket/socket_descriptor.h"
  22. #include "ppapi/shared_impl/ppapi_permissions.h"
  23. #include "url/gurl.h"
  24. namespace content {
  25. class BrowserChildProcessHost;
  26. class BrowserPpapiHost;
  27. }
  28. namespace IPC {
  29. class ChannelProxy;
  30. }
  31. namespace nacl {
  32. // NaClFileToken is a single-use nonce that the NaCl loader process can use
  33. // to query the browser process for trusted information about a file. This
  34. // helps establish that the file is known by the browser to be immutable
  35. // and suitable for file-identity-based validation caching. lo == 0 && hi
  36. // == 0 indicates the token is invalid and no additional information is
  37. // available.
  38. struct NaClFileToken {
  39. uint64_t lo;
  40. uint64_t hi;
  41. };
  42. class NaClHostMessageFilter;
  43. void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size);
  44. // Represents the browser side of the browser <--> NaCl communication
  45. // channel. There will be one NaClProcessHost per NaCl process
  46. // The browser is responsible for starting the NaCl process
  47. // when requested by the renderer.
  48. // After that, most of the communication is directly between NaCl plugin
  49. // running in the renderer and NaCl processes.
  50. class NaClProcessHost : public content::BrowserChildProcessHostDelegate {
  51. public:
  52. // manifest_url: the URL of the manifest of the Native Client plugin being
  53. // executed.
  54. // nexe_file: A file that corresponds to the nexe module to be loaded.
  55. // nexe_token: A cache validation token for nexe_file.
  56. // prefetched_resource_files_info: An array of resource files prefetched.
  57. // permissions: PPAPI permissions, to control access to private APIs.
  58. // permission_bits: controls which interfaces the NaCl plugin can use.
  59. // off_the_record: was the process launched from an incognito renderer?
  60. // process_type: the type of NaCl process.
  61. // profile_directory: is the path of current profile directory.
  62. NaClProcessHost(
  63. const GURL& manifest_url,
  64. base::File nexe_file,
  65. const NaClFileToken& nexe_token,
  66. const std::vector<NaClResourcePrefetchResult>& prefetched_resource_files,
  67. ppapi::PpapiPermissions permissions,
  68. uint32_t permission_bits,
  69. bool off_the_record,
  70. NaClAppProcessType process_type,
  71. const base::FilePath& profile_directory);
  72. NaClProcessHost(const NaClProcessHost&) = delete;
  73. NaClProcessHost& operator=(const NaClProcessHost&) = delete;
  74. ~NaClProcessHost() override;
  75. void OnProcessCrashed(int exit_status) override;
  76. // Do any minimal work that must be done at browser startup.
  77. static void EarlyStartup();
  78. // Specifies throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
  79. static void SetPpapiKeepAliveThrottleForTesting(unsigned milliseconds);
  80. // Initialize the new NaCl process. Result is returned by sending ipc
  81. // message reply_msg.
  82. void Launch(NaClHostMessageFilter* nacl_host_message_filter,
  83. IPC::Message* reply_msg,
  84. const base::FilePath& manifest_path);
  85. void OnChannelConnected(int32_t peer_pid) override;
  86. #if BUILDFLAG(IS_WIN)
  87. void OnProcessLaunchedByBroker(base::Process process);
  88. void OnDebugExceptionHandlerLaunchedByBroker(bool success);
  89. #endif
  90. bool Send(IPC::Message* msg);
  91. content::BrowserChildProcessHost* process() { return process_.get(); }
  92. content::BrowserPpapiHost* browser_ppapi_host() { return ppapi_host_.get(); }
  93. private:
  94. void LaunchNaClGdb();
  95. // Mark the process as using a particular GDB debug stub port and notify
  96. // listeners (if the port is not kGdbDebugStubPortUnknown).
  97. void SetDebugStubPort(int port);
  98. #if BUILDFLAG(IS_POSIX)
  99. // Create bound TCP socket in the browser process so that the NaCl GDB debug
  100. // stub can use it to accept incoming connections even when the Chrome sandbox
  101. // is enabled.
  102. net::SocketDescriptor GetDebugStubSocketHandle();
  103. #endif
  104. #if BUILDFLAG(IS_WIN)
  105. // Called when the debug stub port has been selected.
  106. void OnDebugStubPortSelected(uint16_t debug_stub_port);
  107. #endif
  108. bool LaunchSelLdr();
  109. // BrowserChildProcessHostDelegate implementation:
  110. bool OnMessageReceived(const IPC::Message& msg) override;
  111. void OnProcessLaunched() override;
  112. void OnResourcesReady();
  113. // Sends the reply message to the renderer who is waiting for the plugin
  114. // to load. Returns true on success.
  115. void ReplyToRenderer(
  116. mojo::ScopedMessagePipeHandle ppapi_channel_handle,
  117. mojo::ScopedMessagePipeHandle trusted_channel_handle,
  118. mojo::ScopedMessagePipeHandle manifest_service_channel_handle,
  119. base::ReadOnlySharedMemoryRegion crash_info_shmem_region);
  120. // Sends the reply with error message to the renderer.
  121. void SendErrorToRenderer(const std::string& error_message);
  122. // Sends the reply message to the renderer. Either result or
  123. // error message must be empty.
  124. void SendMessageToRenderer(const NaClLaunchResult& result,
  125. const std::string& error_message);
  126. // Sends the message to the NaCl process to load the plugin. Returns true
  127. // on success.
  128. bool StartNaClExecution();
  129. void StartNaClFileResolved(
  130. NaClStartParams params,
  131. const base::FilePath& file_path,
  132. base::File nexe_file);
  133. // Starts browser PPAPI proxy. Returns true on success.
  134. bool StartPPAPIProxy(mojo::ScopedMessagePipeHandle channel_handle);
  135. // Does post-process-launching tasks for starting the NaCl process once
  136. // we have a connection.
  137. //
  138. // Returns false on failure.
  139. bool StartWithLaunchedProcess();
  140. // Message handlers for validation caching.
  141. void OnQueryKnownToValidate(const std::string& signature, bool* result);
  142. void OnSetKnownToValidate(const std::string& signature);
  143. void OnResolveFileToken(uint64_t file_token_lo, uint64_t file_token_hi);
  144. void FileResolved(uint64_t file_token_lo,
  145. uint64_t file_token_hi,
  146. const base::FilePath& file_path,
  147. base::File file);
  148. #if BUILDFLAG(IS_WIN)
  149. // Message handler for Windows hardware exception handling.
  150. void OnAttachDebugExceptionHandler(const std::string& info,
  151. IPC::Message* reply_msg);
  152. bool AttachDebugExceptionHandler(const std::string& info,
  153. IPC::Message* reply_msg);
  154. #endif
  155. // Called when the PPAPI IPC channels to the browser/renderer have been
  156. // created.
  157. void OnPpapiChannelsCreated(
  158. const IPC::ChannelHandle& ppapi_browser_channel_handle,
  159. const IPC::ChannelHandle& ppapi_renderer_channel_handle,
  160. const IPC::ChannelHandle& trusted_renderer_channel_handle,
  161. const IPC::ChannelHandle& manifest_service_channel_handle,
  162. base::ReadOnlySharedMemoryRegion crash_info_shmem_region);
  163. GURL manifest_url_;
  164. base::File nexe_file_;
  165. NaClFileToken nexe_token_;
  166. std::vector<NaClResourcePrefetchResult> prefetched_resource_files_;
  167. ppapi::PpapiPermissions permissions_;
  168. #if BUILDFLAG(IS_WIN)
  169. // This field becomes true when the broker successfully launched
  170. // the NaCl loader.
  171. bool process_launched_by_broker_;
  172. #endif
  173. // The NaClHostMessageFilter that requested this NaCl process. We use
  174. // this for sending the reply once the process has started.
  175. scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter_;
  176. // The reply message to send. We must always send this message when the
  177. // sub-process either succeeds or fails to unblock the renderer waiting for
  178. // the reply. NULL when there is no reply to send.
  179. raw_ptr<IPC::Message> reply_msg_;
  180. #if BUILDFLAG(IS_WIN)
  181. bool debug_exception_handler_requested_;
  182. std::unique_ptr<IPC::Message> attach_debug_exception_handler_reply_msg_;
  183. #endif
  184. // The file path to the manifest is passed to nacl-gdb when it is used to
  185. // debug the NaCl loader.
  186. base::FilePath manifest_path_;
  187. std::unique_ptr<content::BrowserChildProcessHost> process_;
  188. bool enable_debug_stub_;
  189. bool enable_crash_throttling_;
  190. bool off_the_record_;
  191. NaClAppProcessType process_type_;
  192. const base::FilePath profile_directory_;
  193. // Channel proxy to terminate the NaCl-Browser PPAPI channel.
  194. std::unique_ptr<IPC::ChannelProxy> ipc_proxy_channel_;
  195. // Browser host for plugin process.
  196. std::unique_ptr<content::BrowserPpapiHost> ppapi_host_;
  197. // Throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
  198. static unsigned keepalive_throttle_interval_milliseconds_;
  199. base::WeakPtrFactory<NaClProcessHost> weak_factory_{this};
  200. };
  201. } // namespace nacl
  202. #endif // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_