• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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_RENDERER_PPB_NACL_PRIVATE_H_
6 #define COMPONENTS_NACL_RENDERER_PPB_NACL_PRIVATE_H_
7 
8 #include <memory>
9 
10 #include "base/process/process_handle.h"
11 #include "ipc/ipc_sync_channel.h"
12 #include "ppapi/c/pp_bool.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/pp_instance.h"
15 #include "ppapi/c/pp_macros.h"
16 #include "ppapi/c/pp_stdint.h"
17 #include "ppapi/c/pp_var.h"
18 #include "ppapi/c/private/pp_file_handle.h"
19 #include "ppapi/c/private/ppb_instance_private.h"
20 #include "ppapi/c/private/ppp_pexe_stream_handler.h"
21 
22 // This file defines an interface that is used by the code in
23 // components/nacl/renderer/plugin/.  It is defined as a PPAPI PPB_*
24 // interface for historical reasons: The code in plugin/ used to live
25 // outside the Chromium repo.
26 //
27 // In the future, we may integrate the code in nacl/renderer/plugin/ more
28 // closely with nacl/renderer/.  This could include having plugin/ call the
29 // functions below directly, rather than providing these functions via the
30 // PPB_NaCl_Private interface struct.
31 
32 /**
33  * @addtogroup Enums
34  * @{
35  */
36 /** NaCl-specific errors that should be reported to the user.
37  *  These error codes are reported via UMA so, if you edit them:
38  *   1) make sure you understand UMA first.
39  *   2) update src/tools/metrics/histograms/histograms.xml
40  *  Values are explicitly specified to make sure they don't shift around when
41  *  edited, and also to make reading about:histograms easier.
42  */
43 typedef enum {
44   PP_NACL_ERROR_LOAD_SUCCESS = 0,
45   PP_NACL_ERROR_LOAD_ABORTED = 1,
46   PP_NACL_ERROR_UNKNOWN = 2,
47   PP_NACL_ERROR_MANIFEST_RESOLVE_URL = 3,
48   PP_NACL_ERROR_MANIFEST_LOAD_URL = 4,
49   PP_NACL_ERROR_MANIFEST_STAT = 5,
50   PP_NACL_ERROR_MANIFEST_TOO_LARGE = 6,
51   PP_NACL_ERROR_MANIFEST_OPEN = 7,
52   PP_NACL_ERROR_MANIFEST_MEMORY_ALLOC = 8,
53   PP_NACL_ERROR_MANIFEST_READ = 9,
54   PP_NACL_ERROR_MANIFEST_PARSING = 10,
55   PP_NACL_ERROR_MANIFEST_SCHEMA_VALIDATE = 11,
56   PP_NACL_ERROR_MANIFEST_GET_NEXE_URL = 12,
57   PP_NACL_ERROR_NEXE_LOAD_URL = 13,
58   PP_NACL_ERROR_NEXE_ORIGIN_PROTOCOL = 14,
59   PP_NACL_ERROR_NEXE_FH_DUP = 15,
60   PP_NACL_ERROR_NEXE_STAT = 16,
61   PP_NACL_ERROR_ELF_CHECK_IO = 17,
62   PP_NACL_ERROR_ELF_CHECK_FAIL = 18,
63   PP_NACL_ERROR_SEL_LDR_INIT = 19,
64   PP_NACL_ERROR_SEL_LDR_CREATE_LAUNCHER = 20,
65   PP_NACL_ERROR_SEL_LDR_FD = 21,
66   PP_NACL_ERROR_SEL_LDR_LAUNCH = 22,
67   /* Deprecated, safe to reuse the value because it's never logged in UMA.
68    */
69   PP_NACL_ERROR_SEL_LDR_SEND_NEXE = 24,
70   PP_NACL_ERROR_SEL_LDR_HANDLE_PASSING = 25,
71   PP_NACL_ERROR_SEL_LDR_START_MODULE = 26,
72   PP_NACL_ERROR_SEL_LDR_START_STATUS = 27,
73   PP_NACL_ERROR_SRPC_CONNECTION_FAIL = 28,
74   PP_NACL_ERROR_START_PROXY_CHECK_PPP = 29,
75   PP_NACL_ERROR_START_PROXY_ALLOC = 30,
76   PP_NACL_ERROR_START_PROXY_MODULE = 31,
77   PP_NACL_ERROR_START_PROXY_INSTANCE = 32,
78   PP_NACL_ERROR_SEL_LDR_COMMUNICATION_CMD_CHANNEL = 33,
79   PP_NACL_ERROR_SEL_LDR_COMMUNICATION_REV_SETUP = 34,
80   PP_NACL_ERROR_SEL_LDR_COMMUNICATION_WRAPPER = 35,
81   PP_NACL_ERROR_SEL_LDR_COMMUNICATION_REV_SERVICE = 36,
82   PP_NACL_ERROR_START_PROXY_CRASH = 37,
83   PP_NACL_ERROR_MANIFEST_PROGRAM_MISSING_ARCH = 38,
84   PP_NACL_ERROR_PNACL_CACHE_OPEN_INPROGRESS = 39,
85   PP_NACL_ERROR_PNACL_CACHE_OPEN_NOACCESS = 40,
86   PP_NACL_ERROR_PNACL_CACHE_OPEN_NOQUOTA = 41,
87   PP_NACL_ERROR_PNACL_CACHE_OPEN_NOSPACE = 42,
88   PP_NACL_ERROR_PNACL_CACHE_OPEN_OTHER = 43,
89   PP_NACL_ERROR_PNACL_CACHE_DIRECTORY_CREATE = 44,
90   PP_NACL_ERROR_PNACL_CACHE_FILEOPEN_NOACCESS = 45,
91   PP_NACL_ERROR_PNACL_CACHE_FILEOPEN_NOQUOTA = 46,
92   PP_NACL_ERROR_PNACL_CACHE_FILEOPEN_NOSPACE = 47,
93   PP_NACL_ERROR_PNACL_CACHE_FILEOPEN_NOTAFILE = 48,
94   PP_NACL_ERROR_PNACL_CACHE_FILEOPEN_OTHER = 49,
95   PP_NACL_ERROR_PNACL_CACHE_FETCH_NOACCESS = 50,
96   PP_NACL_ERROR_PNACL_CACHE_FETCH_NOTFOUND = 51,
97   PP_NACL_ERROR_PNACL_CACHE_FETCH_OTHER = 52,
98   PP_NACL_ERROR_PNACL_CACHE_FINALIZE_COPY_NOQUOTA = 53,
99   PP_NACL_ERROR_PNACL_CACHE_FINALIZE_COPY_NOSPACE = 54,
100   PP_NACL_ERROR_PNACL_CACHE_FINALIZE_COPY_OTHER = 55,
101   PP_NACL_ERROR_PNACL_CACHE_FINALIZE_RENAME_NOACCESS = 56,
102   PP_NACL_ERROR_PNACL_CACHE_FINALIZE_RENAME_OTHER = 57,
103   PP_NACL_ERROR_PNACL_RESOURCE_FETCH = 58,
104   PP_NACL_ERROR_PNACL_PEXE_FETCH_ABORTED = 59,
105   PP_NACL_ERROR_PNACL_PEXE_FETCH_NOACCESS = 60,
106   PP_NACL_ERROR_PNACL_PEXE_FETCH_OTHER = 61,
107   PP_NACL_ERROR_PNACL_THREAD_CREATE = 62,
108   PP_NACL_ERROR_PNACL_LLC_SETUP = 63,
109   PP_NACL_ERROR_PNACL_LD_SETUP = 64,
110   PP_NACL_ERROR_PNACL_LLC_INTERNAL = 65,
111   PP_NACL_ERROR_PNACL_LD_INTERNAL = 66,
112   PP_NACL_ERROR_PNACL_CREATE_TEMP = 67,
113   /* This entry is no longer used, but should not be removed, because UMA
114    */
115   PP_NACL_ERROR_PNACL_NOT_ENABLED = 68,
116   PP_NACL_ERROR_MANIFEST_NOACCESS_URL = 69,
117   PP_NACL_ERROR_NEXE_NOACCESS_URL = 70,
118   PP_NACL_ERROR_PNACL_CRASH_THROTTLED = 71,
119   /* If you add a code, read the enum comment above on how to update
120    */
121   PP_NACL_ERROR_MAX
122 } PP_NaClError;
123 
124 /** Event types that NaCl may use when reporting load progress or errors. */
125 typedef enum {
126   PP_NACL_EVENT_LOADSTART,
127   PP_NACL_EVENT_PROGRESS,
128   PP_NACL_EVENT_ERROR,
129   PP_NACL_EVENT_ABORT,
130   PP_NACL_EVENT_LOAD,
131   PP_NACL_EVENT_LOADEND,
132   PP_NACL_EVENT_CRASH
133 } PP_NaClEventType;
134 
135 typedef enum {
136   PP_SCHEME_CHROME_EXTENSION,
137   PP_SCHEME_DATA,
138   PP_SCHEME_OTHER
139 } PP_UrlSchemeType;
140 
141 typedef enum {
142   /* The trusted plugin begins in this ready state. */
143   PP_NACL_READY_STATE_UNSENT = 0,
144   /* The manifest file has been requested, but not yet received. */
145   PP_NACL_READY_STATE_OPENED = 1,
146   /* The manifest file has been received and the nexe successfully requested. */
147   PP_NACL_READY_STATE_LOADING = 3,
148   /* The nexe has been loaded and the proxy started, so it is ready for
149    */
150   PP_NACL_READY_STATE_DONE = 4
151 } PP_NaClReadyState;
152 
153 /** Types of untrusted NaCl processes. Mirrors NaClAppProcessType from
154  *  components/nacl/common/nacl_types.h.
155  */
156 typedef enum {
157   PP_UNKNOWN_NACL_PROCESS_TYPE,
158   PP_NATIVE_NACL_PROCESS_TYPE,
159   PP_PNACL_PROCESS_TYPE,
160   PP_PNACL_TRANSLATOR_PROCESS_TYPE,
161   PP_NUM_NACL_PROCESS_TYPES
162 } PP_NaClAppProcessType;
163 /**
164  * @}
165  */
166 
167 /**
168  * @addtogroup Structs
169  * @{
170  */
171 struct PP_PNaClOptions {
172   PP_Bool translate;
173   PP_Bool is_debug;
174   PP_Bool use_subzero;
175   int32_t opt_level;
176 };
177 /**
178  * @}
179  */
180 
181 /**
182  * @addtogroup Typedefs
183  * @{
184  */
185 /* Callback invoked upon completion of PPP_ManifestService::OpenResource(). */
186 typedef void (*PP_OpenResourceCompletionCallback)(void* user_data,
187                                                   PP_FileHandle file_handle);
188 /**
189  * @}
190  */
191 
192 /**
193  * @addtogroup Structs
194  * @{
195  */
196 struct PP_NaClFileInfo {
197   PP_FileHandle handle;
198   /* See NaClFileToken comment in nacl_process_host.h */
199   uint64_t token_lo;
200   uint64_t token_hi;
201 };
202 /**
203  * @}
204  */
205 
206 namespace nacl {
207 
208 // This is a set of interfaces used by the code in
209 // components/nacl/renderer/plugin/, implemented by
210 // components/nacl/renderer/.
211 //
212 // There is not really a good name for this set of interfaces because the
213 // grouping exists only for historical reasons.  It used to be a PPAPI
214 // PPB_* interface (PPB_NaCl_Private) because the code in plugin/ used to
215 // live outside the Chromium repo and used to be built as a separate
216 // DSO/DLL.  Since that's no longer the case, there is now no strong
217 // distinction between renderer/ and renderer/plugin/.
218 class PPBNaClPrivate {
219  public:
220   /* Launches NaCl's sel_ldr process.  Returns PP_EXTERNAL_PLUGIN_OK on success.
221    * Returns PP_EXTERNAL_PLUGIN_FAILED on failure.
222    * The |nexe_file_info| is the file handle for the main nexe file, which
223    * should be initially loaded.
224    * LaunchSelLdr takes the ownership of the file handle.
225    * |translator_channel| is filled out when launching PNaCl translator
226    * processes.
227    */
228   static void LaunchSelLdr(
229       PP_Instance instance,
230       PP_Bool main_service_runtime,
231       const char* alleged_url,
232       const struct PP_NaClFileInfo* nexe_file_info,
233       PP_NaClAppProcessType process_type,
234       std::unique_ptr<IPC::SyncChannel>* translator_channel,
235       struct PP_CompletionCallback callback);
236   /* Returns a read-only (but executable) file descriptor / file info for
237    * a url for pnacl translator tools. Returns an invalid handle on failure.
238    */
239   static void GetReadExecPnaclFd(const char* url,
240                                  struct PP_NaClFileInfo* out_file_info);
241   /* This creates a temporary file that will be deleted by the time
242    * the last handle is closed (or earlier on POSIX systems), and
243    * returns a posix handle to that temporary file.
244    */
245   static PP_FileHandle CreateTemporaryFile(PP_Instance instance);
246   /* Return the number of processors in the system as reported by the OS */
247   static int32_t GetNumberOfProcessors();
248   /* Report to the browser that translation of the pexe for |instance|
249    * has finished, or aborted with an error. If |success| is true, the
250    * browser may then store the translation in the cache. The renderer
251    * must first have called StreamPexe for the same instance. (The browser is
252    * not guaranteed to store the nexe even if |success| is true; if there is
253    * an error on the browser side, or the file is too big for the cache, or
254    * the browser is in incognito mode, no notification will be delivered to
255    * the plugin.)
256    */
257   static void ReportTranslationFinished(PP_Instance instance,
258                                         PP_Bool success,
259                                         int32_t opt_level,
260                                         PP_Bool use_subzero,
261                                         int64_t nexe_size,
262                                         int64_t pexe_size,
263                                         int64_t compile_time_us);
264   /* Dispatch a progress event on the DOM element where the given instance is
265    * embedded.
266    */
267   static void DispatchEvent(PP_Instance instance,
268                             PP_NaClEventType event_type,
269                             const char* resource_url,
270                             PP_Bool length_is_computable,
271                             uint64_t loaded_bytes,
272                             uint64_t total_bytes);
273   /* Report an error that occured while attempting to load a nexe. */
274   static void ReportLoadError(PP_Instance instance,
275                               PP_NaClError error,
276                               const char* error_message);
277   /* Performs internal setup when an instance is created. */
278   static void InstanceCreated(PP_Instance instance);
279   /* Performs internal cleanup when an instance is destroyed. */
280   static void InstanceDestroyed(PP_Instance instance);
281   /* Terminates the NaCl loader process associated with the instance */
282   static void TerminateNaClLoader(PP_Instance instance);
283   /* Returns the kind of SFI sandbox implemented by NaCl on this
284    * platform.
285    */
286   static const char* GetSandboxArch(void);
287   /* Initializes internal state for a NaCl plugin. */
288   static void InitializePlugin(PP_Instance instance,
289                                uint32_t argc,
290                                const char* argn[],
291                                const char* argv[]);
292   /* Requests the NaCl manifest specified in the plugin arguments. */
293   static void RequestNaClManifest(PP_Instance instance,
294                                   struct PP_CompletionCallback callback);
295   static struct PP_Var GetManifestBaseURL(PP_Instance instance);
296   /* Processes the NaCl manifest once it's been retrieved.
297    * TODO(teravest): Move the rest of the supporting logic out of the trusted
298    * plugin.
299    */
300   static void ProcessNaClManifest(PP_Instance instance,
301                                   const char* program_url);
302   static PP_Bool GetManifestProgramURL(PP_Instance instance,
303                                        struct PP_Var* full_url,
304                                        struct PP_PNaClOptions* pnacl_options);
305   /* Returns the filenames for the llc and ld tools. */
306   static PP_Bool GetPnaclResourceInfo(PP_Instance instance,
307                                       struct PP_Var* llc_tool_name,
308                                       struct PP_Var* ld_tool_name,
309                                       struct PP_Var* subzero_tool_name);
310   /* PP_Var string of attributes describing the CPU features supported
311    * by the current architecture. The string is a comma-delimited list
312    * of attributes supported by LLVM in its -mattr= option:
313    *   http://llvm.org/docs/CommandGuide/llc.html#cmdoption-mattr */
314   static struct PP_Var GetCpuFeatureAttrs(void);
315   /* Downloads the .nexe file at the given URL to a file, and sets |file_info|
316    * to information for a handle to a file containing its contents.
317    * If metadata for identity-based validation caching is available
318    * then it sets token information in |file_info| (otherwise left untouched).
319    */
320   static void DownloadNexe(PP_Instance instance,
321                            const char* url,
322                            struct PP_NaClFileInfo* file_info,
323                            struct PP_CompletionCallback callback);
324   /* Logs time taken by an operation to UMA histograms.
325    * This function is safe to call on any thread.
326    */
327   static void LogTranslateTime(const char* histogram_name, int64_t time_us);
328   /* Logs amount of pexe bytes compiled when download is complete. */
329   static void LogBytesCompiledVsDownloaded(PP_Bool use_subzero,
330                                            int64_t pexe_bytes_compiled,
331                                            int64_t pexe_bytes_downloaded);
332   /* Sets the start time for PNaCl downloading and translation to the current
333    * time.
334    */
335   static void SetPNaClStartTime(PP_Instance instance);
336   /* Downloads and streams a pexe file for PNaCl translation.
337    * Fetches the content at |pexe_url| for the given instance and opt_level.
338    * If a translated cached nexe is already available,
339    * the |stream_handler|'s |DidCacheHit| is called. Otherwise, |DidCacheMiss|
340    * is called and |DidStreamData| is called repeatedly with blocks of data
341    * as they are received. |DidFinishStream| is called after all
342    * data has been received and dispatched to |DidStreamData|.
343    */
344   static void StreamPexe(PP_Instance instance,
345                          const char* pexe_url,
346                          int32_t opt_level,
347                          PP_Bool use_subzero,
348                          const struct PPP_PexeStreamHandler_1_0* stream_handler,
349                          void* stream_handler_user_data);
350 };
351 
352 }  // namespace nacl
353 
354 #endif  // COMPONENTS_NACL_RENDERER_PPB_NACL_PRIVATE_H_
355