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/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"
23 typedef void (*EntryPointType)(uintptr_t*);
25 class PluginMainDelegate : public base::PlatformThread::Delegate {
27 explicit PluginMainDelegate(EntryPointType entry_point)
28 : entry_point_(entry_point) {
31 ~PluginMainDelegate() override {}
33 void ThreadMain() override {
34 base::PlatformThread::SetName("NaClMainThread");
36 // This will only happen once per process, so we give the permission to
38 base::ThreadRestrictions::SetSingletonAllowed(true);
40 0, // Do not use fini.
43 0, // Null terminate for argv.
44 0, // Null terminate for envv.
46 reinterpret_cast<uintptr_t>(&NaClIrtInterface),
48 0, // Null terminate for auxv.
54 EntryPointType entry_point_;
57 // Default stack size of the plugin main thread. We heuristically chose 16M.
58 const size_t kStackSize = (16 << 20);
60 struct NaClDescUnrefer {
61 void operator()(struct NaClDesc* desc) const {
68 void MainStart(int nexe_file) {
69 ::scoped_ptr<struct NaClDesc, NaClDescUnrefer> desc(
70 NaClDescIoDescFromDescAllocCtor(nexe_file, NACL_ABI_O_RDONLY));
72 if (image.Read(desc.get()) != LOAD_OK) {
73 LOG(ERROR) << "LoadModuleRpc: Failed to read binary.";
77 if (image.Load(desc.get()) != LOAD_OK) {
78 LOG(ERROR) << "LoadModuleRpc: Failed to load the image";
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.";