Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / service_runtime / sel_main_chrome.c
index 89e7f30..b1da5d3 100644 (file)
@@ -4,7 +4,7 @@
  * found in the LICENSE file.
  */
 
-#include "native_client/src/trusted/service_runtime/sel_main_chrome.h"
+#include "native_client/src/public/chrome_main.h"
 
 #include "native_client/src/include/portability.h"
 #include "native_client/src/include/portability_io.h"
@@ -18,6 +18,7 @@
 #include <string.h>
 
 #include "native_client/src/include/nacl_macros.h"
+#include "native_client/src/public/nacl_app.h"
 #include "native_client/src/shared/platform/nacl_check.h"
 #include "native_client/src/shared/platform/nacl_exit.h"
 #include "native_client/src/shared/platform/nacl_log.h"
 #include "native_client/src/trusted/service_runtime/win/exception_patch/ntdll_patch.h"
 #include "native_client/src/trusted/validator/validation_metadata.h"
 
+static int g_initialized = 0;
+
+#if NACL_LINUX || NACL_OSX
+void NaClChromeMainSetUrandomFd(int urandom_fd) {
+  CHECK(!g_initialized);
+  NaClSecureRngModuleSetUrandomFd(urandom_fd);
+}
+#endif
+
+void NaClChromeMainInit(void) {
+  CHECK(!g_initialized);
+  NaClAllModulesInit();
+  g_initialized = 1;
+}
+
 struct NaClChromeMainArgs *NaClChromeMainArgsCreate(void) {
-  struct NaClChromeMainArgs *args = malloc(sizeof(*args));
+  struct NaClChromeMainArgs *args;
+
+  CHECK(g_initialized);
+  args = malloc(sizeof(*args));
   if (args == NULL)
     return NULL;
   args->imc_bootstrap_handle = NACL_INVALID_HANDLE;
   args->irt_fd = -1;
-  args->initial_ipc_desc = NULL;
   args->enable_exception_handling = 0;
   args->enable_debug_stub = 0;
   args->enable_dyncode_syscalls = 1;
@@ -64,20 +82,11 @@ struct NaClChromeMainArgs *NaClChromeMainArgsCreate(void) {
   args->attach_debug_exception_handler_func = NULL;
 #endif
 #if NACL_LINUX || NACL_OSX
-  args->urandom_fd = -1;
   args->number_of_cores = -1;  /* unknown */
 #endif
 #if NACL_LINUX
   args->prereserved_sandbox_size = 0;
 #endif
-
-  /*
-   * Initialize NaClLog so that Chromium can call
-   * NaClDescMakeCustomDesc() between calling
-   * NaClChromeMainArgsCreate() and NaClChromeMainStart().
-   */
-  NaClLogModuleInit();
-
   return args;
 }
 
@@ -124,12 +133,11 @@ static void NaClLoadIrt(struct NaClApp *nap, int irt_fd) {
   NaClDescUnref(nd);
 }
 
-void NaClChromeMainStart(struct NaClChromeMainArgs *args) {
+void NaClChromeMainStartApp(struct NaClApp *nap,
+                            struct NaClChromeMainArgs *args) {
   char *av[1];
   int ac = 1;
   const char **envp;
-  struct NaClApp state;
-  struct NaClApp *nap = &state;
   NaClErrorCode errcode = LOAD_INTERNAL;
   int ret_code = 1;
   struct NaClEnvCleanser env_cleanser;
@@ -145,29 +153,14 @@ void NaClChromeMainStart(struct NaClChromeMainArgs *args) {
   envp = (const char **) environ;
 #endif
 
-#if NACL_LINUX || NACL_OSX
-  /* This needs to happen before NaClAllModulesInit(). */
-  if (args->urandom_fd != -1)
-    NaClSecureRngModuleSetUrandomFd(args->urandom_fd);
-#endif
+  CHECK(g_initialized);
 
-  /*
-   * Clear state so that NaClBootstrapChannelErrorReporter will be
-   * able to know if the bootstrap channel is available or not.
-   */
-  memset(&state, 0, sizeof state);
-  NaClAllModulesInit();
   NaClBootstrapChannelErrorReporterInit();
-  NaClErrorLogHookInit(NaClBootstrapChannelErrorReporter, &state);
+  NaClErrorLogHookInit(NaClBootstrapChannelErrorReporter, nap);
 
   /* to be passed to NaClMain, eventually... */
   av[0] = "NaClMain";
 
-  if (NACL_FI_ERROR_COND("AppCtor", !NaClAppCtor(&state))) {
-    NaClLog(LOG_FATAL, "Error while constructing app state\n");
-    goto done;
-  }
-
   errcode = LOAD_OK;
 
   /* Allow or disallow dyncode API based on args. */
@@ -207,10 +200,6 @@ void NaClChromeMainStart(struct NaClChromeMainArgs *args) {
    * are 3 and 4.
    */
 
-  if (args->initial_ipc_desc != NULL) {
-    NaClSetDesc(nap, NACL_CHROME_INITIAL_IPC_DESC, args->initial_ipc_desc);
-  }
-
   /*
    * in order to report load error to the browser plugin through the
    * secure command channel, we do not immediate jump to cleanup code
@@ -254,7 +243,7 @@ void NaClChromeMainStart(struct NaClChromeMainArgs *args) {
     /* NaCl's signal handler is always enabled on Linux. */
 #elif NACL_OSX
     if (!NaClInterceptMachExceptions()) {
-      NaClLog(LOG_FATAL, "NaClChromeMainStart: "
+      NaClLog(LOG_FATAL, "NaClChromeMainStartApp: "
               "Failed to set up Mach exception handler\n");
     }
 #elif NACL_WINDOWS
@@ -269,7 +258,7 @@ void NaClChromeMainStart(struct NaClChromeMainArgs *args) {
 #endif
 
   /* Give debuggers a well known point at which xlate_base is known.  */
-  NaClGdbHook(&state);
+  NaClGdbHook(nap);
 
   NaClCreateServiceSocket(nap);
   /*
@@ -287,10 +276,8 @@ void NaClChromeMainStart(struct NaClChromeMainArgs *args) {
 
   NACL_FI_FATAL("BeforeSecureCommandChannel");
   /*
-   * NB: Spawns a thread that uses the command channel.  We do this
-   * after NaClAppLoadFile so that the NaClApp object is more fully
-   * populated.  Hereafter any changes to nap should be done while
-   * holding locks.
+   * Spawns a thread that uses the command channel.
+   * Hereafter any changes to nap should be done while holding locks.
    */
   NaClSecureCommandChannel(nap);