Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / nacl / loader / nonsfi / nonsfi_main.cc
1 // Copyright 2014 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 #include "components/nacl/loader/nonsfi/nonsfi_main.h"
6
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/threading/platform_thread.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "components/nacl/loader/nonsfi/elf_loader.h"
12 #include "components/nacl/loader/nonsfi/irt_interfaces.h"
13 #include "native_client/src/include/elf_auxv.h"
14 #include "native_client/src/include/nacl_macros.h"
15 #include "native_client/src/trusted/desc/nacl_desc_base.h"
16 #include "native_client/src/trusted/desc/nacl_desc_io.h"
17 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h"
18
19 namespace nacl {
20 namespace nonsfi {
21 namespace {
22
23 typedef void (*EntryPointType)(uintptr_t*);
24
25 class PluginMainDelegate : public base::PlatformThread::Delegate {
26  public:
27   explicit PluginMainDelegate(EntryPointType entry_point)
28       : entry_point_(entry_point) {
29   }
30
31   ~PluginMainDelegate() override {}
32
33   void ThreadMain() override {
34     base::PlatformThread::SetName("NaClMainThread");
35
36     // This will only happen once per process, so we give the permission to
37     // create Singletons.
38     base::ThreadRestrictions::SetSingletonAllowed(true);
39     uintptr_t info[] = {
40       0,  // Do not use fini.
41       0,  // envc.
42       0,  // argc.
43       0,  // Null terminate for argv.
44       0,  // Null terminate for envv.
45       AT_SYSINFO,
46       reinterpret_cast<uintptr_t>(&NaClIrtInterface),
47       AT_NULL,
48       0,  // Null terminate for auxv.
49     };
50     entry_point_(info);
51   }
52
53  private:
54   EntryPointType entry_point_;
55 };
56
57 // Default stack size of the plugin main thread. We heuristically chose 16M.
58 const size_t kStackSize = (16 << 20);
59
60 struct NaClDescUnrefer {
61   void operator()(struct NaClDesc* desc) const {
62     NaClDescUnref(desc);
63   }
64 };
65
66 }  // namespace
67
68 void MainStart(int nexe_file) {
69   ::scoped_ptr<struct NaClDesc, NaClDescUnrefer> desc(
70        NaClDescIoDescFromDescAllocCtor(nexe_file, NACL_ABI_O_RDONLY));
71   ElfImage image;
72   if (image.Read(desc.get()) != LOAD_OK) {
73     LOG(ERROR) << "LoadModuleRpc: Failed to read binary.";
74     return;
75   }
76
77   if (image.Load(desc.get()) != LOAD_OK) {
78     LOG(ERROR) << "LoadModuleRpc: Failed to load the image";
79     return;
80   }
81
82   EntryPointType entry_point =
83       reinterpret_cast<EntryPointType>(image.entry_point());
84   if (!base::PlatformThread::CreateNonJoinable(
85           kStackSize, new PluginMainDelegate(entry_point))) {
86     LOG(ERROR) << "LoadModuleRpc: Failed to create plugin main thread.";
87     return;
88   }
89 }
90
91 }  // namespace nonsfi
92 }  // namespace nacl