1/*
2 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include "native_client/src/shared/srpc/nacl_srpc.h"
8#include "ppapi/native_client/src/untrusted/nacl_ppapi_util/ppapi_srpc_main.h"
9
10/*
11 * Here be dragons.  Beware.
12 *
13 * We need to provide a main so that when this code is compiled with
14 * irt=1, we can invoke NaClSrpcModuleInit.  This is not needed when
15 * we compile with irt=0 because the PpapiPluginMain uses SRPC and
16 * will invoke NaClSrpcModuleInit.  However, with irt=1, there are two
17 * copies of the SRPC (and platform, and ...) libraries: the
18 * PpapiPluginMain code uses a copy of SRPC in the IRT, and the user
19 * code -- that's us -- has another copy.  The two copies have
20 * separate allocators, globals, etc because of the IRT separation, so
21 * that the NaClSrpcModuleInit that is invoked in the IRT will not
22 * initialize the data structures used by the copy in the user code.
23 *
24 * The weak reference to the function __nacl_register_thread_creator
25 * is an IRT implementation-dependent hack.  Here's how it works.  In
26 * irt_stub, there is a definition for __nacl_register_thread_creator,
27 * so if this code is compiled and linked with irt=1, the weak symbol
28 * will be overridden with a real function, and the tests inside of
29 * main will succeed.  If this code is compiled and linked with irt=0,
30 * however, we will not link against irt_stub (-lppapi which is a
31 * linker script which causes libppapi_stub.a to get included, which
32 * includes the code from the src/untrusted/irt_stub directory), and
33 * so the __nacl_register_thread_creator weak symbol will have the
34 * value zero.  Voila, we call NaClSrpcModuleInit in the main function
35 * below if the scons command invocation had irt=1 set, and we will
36 * leave it to PpapiPluginMain to invoke NaClSrpcModuleInit when the
37 * scons invocation had irt=0 set.
38 *
39 * Yes, this could be conditionally linked in via scons magic by
40 * testing the irt bit, avoiding the weak attribute magic.
41 */
42
43struct nacl_irt_ppapihook;
44
45void __nacl_register_thread_creator(const struct nacl_irt_ppapihook *)
46    __attribute__((weak));
47
48int main(void) {
49  int rv;
50
51  if (__nacl_register_thread_creator) {
52    if (!NaClSrpcModuleInit()) {
53      return 1;
54    }
55  }
56
57  rv = PpapiPluginMain();
58
59  if (__nacl_register_thread_creator) {
60    NaClSrpcModuleFini();
61  }
62
63  return rv;
64}
65