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 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 currently used only in non-SFI mode. It is the 223 * file handle for the main nexe file, which should be initially loaded. 224 * LaunchSelLdr takes the ownership of the file handle. 225 * The |uses_nonsfi_mode| flag indicates whether or not nonsfi-mode should 226 * be used with the binary pointed by the url. 227 * |translator_channel| is filled out when launching PNaCl translator 228 * processes. 229 */ 230 static void LaunchSelLdr( 231 PP_Instance instance, 232 PP_Bool main_service_runtime, 233 const char* alleged_url, 234 const struct PP_NaClFileInfo* nexe_file_info, 235 PP_Bool uses_nonsfi_mode, 236 PP_NaClAppProcessType process_type, 237 std::unique_ptr<IPC::SyncChannel>* translator_channel, 238 struct PP_CompletionCallback callback); 239 /* Returns a read-only (but executable) file descriptor / file info for 240 * a url for pnacl translator tools. Returns an invalid handle on failure. 241 */ 242 static void GetReadExecPnaclFd(const char* url, 243 struct PP_NaClFileInfo* out_file_info); 244 /* This creates a temporary file that will be deleted by the time 245 * the last handle is closed (or earlier on POSIX systems), and 246 * returns a posix handle to that temporary file. 247 */ 248 static PP_FileHandle CreateTemporaryFile(PP_Instance instance); 249 /* Return the number of processors in the system as reported by the OS */ 250 static int32_t GetNumberOfProcessors(); 251 /* Report to the browser that translation of the pexe for |instance| 252 * has finished, or aborted with an error. If |success| is true, the 253 * browser may then store the translation in the cache. The renderer 254 * must first have called StreamPexe for the same instance. (The browser is 255 * not guaranteed to store the nexe even if |success| is true; if there is 256 * an error on the browser side, or the file is too big for the cache, or 257 * the browser is in incognito mode, no notification will be delivered to 258 * the plugin.) 259 */ 260 static void ReportTranslationFinished(PP_Instance instance, 261 PP_Bool success, 262 int32_t opt_level, 263 PP_Bool use_subzero, 264 int64_t nexe_size, 265 int64_t pexe_size, 266 int64_t compile_time_us); 267 /* Dispatch a progress event on the DOM element where the given instance is 268 * embedded. 269 */ 270 static void DispatchEvent(PP_Instance instance, 271 PP_NaClEventType event_type, 272 const char* resource_url, 273 PP_Bool length_is_computable, 274 uint64_t loaded_bytes, 275 uint64_t total_bytes); 276 /* Report an error that occured while attempting to load a nexe. */ 277 static void ReportLoadError(PP_Instance instance, 278 PP_NaClError error, 279 const char* error_message); 280 /* Performs internal setup when an instance is created. */ 281 static void InstanceCreated(PP_Instance instance); 282 /* Performs internal cleanup when an instance is destroyed. */ 283 static void InstanceDestroyed(PP_Instance instance); 284 /* Terminates the NaCl loader process associated with the instance */ 285 static void TerminateNaClLoader(PP_Instance instance); 286 /* Returns the kind of SFI sandbox implemented by NaCl on this 287 * platform. 288 */ 289 static const char* GetSandboxArch(void); 290 /* Initializes internal state for a NaCl plugin. */ 291 static void InitializePlugin(PP_Instance instance, 292 uint32_t argc, 293 const char* argn[], 294 const char* argv[]); 295 /* Requests the NaCl manifest specified in the plugin arguments. */ 296 static void RequestNaClManifest(PP_Instance instance, 297 struct PP_CompletionCallback callback); 298 static struct PP_Var GetManifestBaseURL(PP_Instance instance); 299 /* Processes the NaCl manifest once it's been retrieved. 300 * TODO(teravest): Move the rest of the supporting logic out of the trusted 301 * plugin. 302 */ 303 static void ProcessNaClManifest(PP_Instance instance, 304 const char* program_url); 305 static PP_Bool GetManifestProgramURL(PP_Instance instance, 306 struct PP_Var* full_url, 307 struct PP_PNaClOptions* pnacl_options, 308 PP_Bool* uses_nonsfi_mode); 309 /* Returns the filenames for the llc and ld tools. */ 310 static PP_Bool GetPnaclResourceInfo(PP_Instance instance, 311 struct PP_Var* llc_tool_name, 312 struct PP_Var* ld_tool_name, 313 struct PP_Var* subzero_tool_name); 314 /* PP_Var string of attributes describing the CPU features supported 315 * by the current architecture. The string is a comma-delimited list 316 * of attributes supported by LLVM in its -mattr= option: 317 * http://llvm.org/docs/CommandGuide/llc.html#cmdoption-mattr */ 318 static struct PP_Var GetCpuFeatureAttrs(void); 319 /* Downloads the .nexe file at the given URL to a file, and sets |file_info| 320 * to information for a handle to a file containing its contents. 321 * If metadata for identity-based validation caching is available 322 * then it sets token information in |file_info| (otherwise left untouched). 323 */ 324 static void DownloadNexe(PP_Instance instance, 325 const char* url, 326 struct PP_NaClFileInfo* file_info, 327 struct PP_CompletionCallback callback); 328 /* Logs time taken by an operation to UMA histograms. 329 * This function is safe to call on any thread. 330 */ 331 static void LogTranslateTime(const char* histogram_name, int64_t time_us); 332 /* Logs amount of pexe bytes compiled when download is complete. */ 333 static void LogBytesCompiledVsDownloaded(PP_Bool use_subzero, 334 int64_t pexe_bytes_compiled, 335 int64_t pexe_bytes_downloaded); 336 /* Sets the start time for PNaCl downloading and translation to the current 337 * time. 338 */ 339 static void SetPNaClStartTime(PP_Instance instance); 340 /* Downloads and streams a pexe file for PNaCl translation. 341 * Fetches the content at |pexe_url| for the given instance and opt_level. 342 * If a translated cached nexe is already available, 343 * the |stream_handler|'s |DidCacheHit| is called. Otherwise, |DidCacheMiss| 344 * is called and |DidStreamData| is called repeatedly with blocks of data 345 * as they are received. |DidFinishStream| is called after all 346 * data has been received and dispatched to |DidStreamData|. 347 */ 348 static void StreamPexe(PP_Instance instance, 349 const char* pexe_url, 350 int32_t opt_level, 351 PP_Bool use_subzero, 352 const struct PPP_PexeStreamHandler_1_0* stream_handler, 353 void* stream_handler_user_data); 354 }; 355 356 } // namespace nacl 357 358 #endif /* COMPONENTS_NACL_RENDERER_PPB_NACL_PRIVATE_H_ */ 359