1 // Copyright 2013 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 
5 #ifndef COMPONENTS_NACL_COMMON_NACL_TYPES_H_
6 #define COMPONENTS_NACL_COMMON_NACL_TYPES_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include "base/memory/read_only_shared_memory_region.h"
15 #include "base/memory/writable_shared_memory_region.h"
16 #include "base/process/process_handle.h"
17 #include "build/build_config.h"
18 #include "ipc/ipc_channel.h"
19 #include "ipc/ipc_platform_file.h"
20 
21 namespace nacl {
22 
23 // We allocate a page of shared memory for sharing crash information from
24 // trusted code in the NaCl process to the renderer.
25 static const int kNaClCrashInfoShmemSize = 4096;
26 static const int kNaClCrashInfoMaxLogSize = 1024;
27 
28 // Types of untrusted NaCl processes.
29 enum NaClAppProcessType {
30   kUnknownNaClProcessType,
31   // Runs user-provided *native* code. Enabled for Chrome Web Store apps.
32   kNativeNaClProcessType,
33   // Runs user-provided code that is translated from *bitcode* by an
34   // in-browser PNaCl translator.
35   kPNaClProcessType,
36   // Runs pnacl-llc/linker *native* code. These nexes are browser-provided
37   // (not user-provided).
38   kPNaClTranslatorProcessType,
39   kNumNaClProcessTypes
40 };
41 
42 // Represents a request to prefetch a file that's listed in the "files" section
43 // of a NaCl manifest file.
44 struct NaClResourcePrefetchRequest {
45   NaClResourcePrefetchRequest();
46   NaClResourcePrefetchRequest(const std::string& file_key,
47                               const std::string& resource_url);
48   ~NaClResourcePrefetchRequest();
49 
50   std::string file_key;  // a key for open_resource.
51   std::string resource_url;
52 };
53 
54 // Represents a single prefetched file that's listed in the "files" section of
55 // a NaCl manifest file.
56 struct NaClResourcePrefetchResult {
57   NaClResourcePrefetchResult();
58   NaClResourcePrefetchResult(const IPC::PlatformFileForTransit& file,
59                              const base::FilePath& file_path,
60                              const std::string& file_key);
61   ~NaClResourcePrefetchResult();
62 
63   IPC::PlatformFileForTransit file;
64   base::FilePath file_path_metadata;  // a key for validation caching
65   std::string file_key;  // a key for open_resource
66 };
67 
68 // Parameters sent to the NaCl process when we start it.
69 struct NaClStartParams {
70   NaClStartParams();
71   NaClStartParams(NaClStartParams&& other);
72   ~NaClStartParams();
73 
74   IPC::PlatformFileForTransit nexe_file;
75   // Used only as a key for validation caching.
76   base::FilePath nexe_file_path_metadata;
77 
78   IPC::PlatformFileForTransit irt_handle;
79 #if defined(OS_POSIX)
80   IPC::PlatformFileForTransit debug_stub_server_bound_socket;
81 #endif
82 
83 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_NACL_NONSFI)
84   // These are for Non-SFI mode IPC channels.
85   // For security hardening, unlike in SFI mode, we cannot create socket pairs
86   // in a NaCl loader process. Thus, the browser process creates the
87   // ChannelHandle instances, and passes them to the NaCl loader process.
88   // SFI mode uses NaClProcessHostMsg_PpapiChannelsCreated instead.
89   IPC::ChannelHandle ppapi_browser_channel_handle;
90   IPC::ChannelHandle ppapi_renderer_channel_handle;
91   IPC::ChannelHandle trusted_service_channel_handle;
92   IPC::ChannelHandle manifest_service_channel_handle;
93 #endif
94 
95   bool validation_cache_enabled;
96   std::string validation_cache_key;
97   // Chrome version string. Sending the version string over IPC avoids linkage
98   // issues in cases where NaCl is not compiled into the main Chromium
99   // executable or DLL.
100   std::string version;
101 
102   bool enable_debug_stub;
103 
104   NaClAppProcessType process_type;
105 
106   // For NaCl <-> renderer crash information reporting.
107   base::WritableSharedMemoryRegion crash_info_shmem_region;
108 
109   // NOTE: Any new fields added here must also be added to the IPC
110   // serialization in nacl_messages.h and (for POD fields) the constructor
111   // in nacl_types.cc.
112 
113  private:
114   DISALLOW_COPY_AND_ASSIGN(NaClStartParams);
115 };
116 
117 // Parameters sent to the browser process to have it launch a NaCl process.
118 //
119 // If you change this, you will also need to update the IPC serialization in
120 // nacl_host_messages.h.
121 struct NaClLaunchParams {
122   NaClLaunchParams();
123   NaClLaunchParams(const std::string& manifest_url,
124                    const IPC::PlatformFileForTransit& nexe_file,
125                    uint64_t nexe_token_lo,
126                    uint64_t nexe_token_hi,
127                    const std::vector<NaClResourcePrefetchRequest>&
128                        resource_prefetch_request_list,
129                    int render_view_id,
130                    uint32_t permission_bits,
131                    bool uses_nonsfi_mode,
132                    NaClAppProcessType process_type);
133   NaClLaunchParams(const NaClLaunchParams& other);
134   ~NaClLaunchParams();
135 
136   std::string manifest_url;
137   // On Windows, the HANDLE passed here is valid in the renderer's context.
138   // It's the responsibility of the browser to duplicate this handle properly
139   // for passing it to the plugin.
140   IPC::PlatformFileForTransit nexe_file;
141   uint64_t nexe_token_lo;
142   uint64_t nexe_token_hi;
143   std::vector<NaClResourcePrefetchRequest> resource_prefetch_request_list;
144 
145   int render_view_id;
146   uint32_t permission_bits;
147   bool uses_nonsfi_mode;
148 
149   NaClAppProcessType process_type;
150 };
151 
152 struct NaClLaunchResult {
153   NaClLaunchResult();
154   NaClLaunchResult(
155       const IPC::ChannelHandle& ppapi_ipc_channel_handle,
156       const IPC::ChannelHandle& trusted_ipc_channel_handle,
157       const IPC::ChannelHandle& manifest_service_ipc_channel_handle,
158       base::ProcessId plugin_pid,
159       int plugin_child_id,
160       base::ReadOnlySharedMemoryRegion crash_info_shmem_region);
161   ~NaClLaunchResult();
162 
163   // For plugin <-> renderer PPAPI communication.
164   IPC::ChannelHandle ppapi_ipc_channel_handle;
165 
166   // For plugin loader <-> renderer control communication (loading and
167   // starting nexe).
168   IPC::ChannelHandle trusted_ipc_channel_handle;
169 
170   // For plugin <-> renderer ManifestService communication.
171   IPC::ChannelHandle manifest_service_ipc_channel_handle;
172 
173   base::ProcessId plugin_pid;
174   int plugin_child_id;
175 
176   // For NaCl <-> renderer crash information reporting.
177   base::ReadOnlySharedMemoryRegion crash_info_shmem_region;
178 
179  private:
180   DISALLOW_COPY_AND_ASSIGN(NaClLaunchResult);
181 };
182 
183 }  // namespace nacl
184 
185 #endif  // COMPONENTS_NACL_COMMON_NACL_TYPES_H_
186