Don't preload rarely seen large images
[chromium-blink-merge.git] / components / nacl / loader / nonsfi / nonsfi_main.cc
blob6781514a7a3ec2b1e592558f5ed10f1e4384cdfd
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.
5 #include "components/nacl/loader/nonsfi/nonsfi_main.h"
7 #include "base/logging.h"
8 #include "base/threading/platform_thread.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "native_client/src/include/elf_auxv.h"
12 #if defined(OS_NACL_NONSFI)
13 #include "native_client/src/public/nonsfi/elf_loader.h"
14 #include "ppapi/nacl_irt/irt_ppapi.h"
15 #else
16 #include "base/memory/scoped_ptr.h"
17 #include "components/nacl/loader/nonsfi/elf_loader.h"
18 #include "components/nacl/loader/nonsfi/irt_interfaces.h"
19 #include "native_client/src/include/nacl_macros.h"
20 #include "native_client/src/public/nacl_desc.h"
21 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h"
22 #endif
24 namespace nacl {
25 namespace nonsfi {
26 namespace {
28 typedef void (*EntryPointType)(uintptr_t*);
30 class PluginMainDelegate : public base::PlatformThread::Delegate {
31 public:
32 explicit PluginMainDelegate(EntryPointType entry_point)
33 : entry_point_(entry_point) {
36 ~PluginMainDelegate() override {}
38 void ThreadMain() override {
39 base::PlatformThread::SetName("NaClMainThread");
41 // This will only happen once per process, so we give the permission to
42 // create Singletons.
43 base::ThreadRestrictions::SetSingletonAllowed(true);
44 uintptr_t info[] = {
45 0, // Do not use fini.
46 0, // envc.
47 0, // argc.
48 0, // Null terminate for argv.
49 0, // Null terminate for envv.
50 AT_SYSINFO,
51 #if defined(OS_NACL_NONSFI)
52 reinterpret_cast<uintptr_t>(&chrome_irt_query),
53 #else
54 reinterpret_cast<uintptr_t>(&NaClIrtInterface),
55 #endif
56 AT_NULL,
57 0, // Null terminate for auxv.
59 entry_point_(info);
62 private:
63 EntryPointType entry_point_;
66 // Default stack size of the plugin main thread. We heuristically chose 16M.
67 const size_t kStackSize = (16 << 20);
69 #if !defined(OS_NACL_NONSFI)
70 struct NaClDescUnrefer {
71 void operator()(struct NaClDesc* desc) const {
72 NaClDescUnref(desc);
75 #endif
77 } // namespace
79 void MainStart(int nexe_file) {
80 #if defined(OS_NACL_NONSFI)
81 EntryPointType entry_point =
82 reinterpret_cast<EntryPointType>(NaClLoadElfFile(nexe_file));
83 #else
84 ::scoped_ptr<struct NaClDesc, NaClDescUnrefer> desc(
85 NaClDescIoMakeFromHandle(nexe_file, NACL_ABI_O_RDONLY));
86 ElfImage image;
87 if (image.Read(desc.get()) != LOAD_OK) {
88 LOG(ERROR) << "LoadModuleRpc: Failed to read binary.";
89 return;
92 if (image.Load(desc.get()) != LOAD_OK) {
93 LOG(ERROR) << "LoadModuleRpc: Failed to load the image";
94 return;
97 EntryPointType entry_point =
98 reinterpret_cast<EntryPointType>(image.entry_point());
99 #endif
100 if (!base::PlatformThread::CreateNonJoinable(
101 kStackSize, new PluginMainDelegate(entry_point))) {
102 LOG(ERROR) << "LoadModuleRpc: Failed to create plugin main thread.";
103 return;
107 } // namespace nonsfi
108 } // namespace nacl