1 : /*
2 : * Copyright (c) 2012 The Native Client 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 <errno.h>
8 : #include <sys/resource.h>
9 : #include <string.h>
10 :
11 : #include "native_client/src/shared/platform/nacl_log.h"
12 : #include "native_client/src/trusted/service_runtime/sel_addrspace.h"
13 :
14 : size_t g_prereserved_sandbox_size = 0;
15 :
16 : /*
17 : * Find sandbox memory prereserved by the nacl_helper in chrome. The
18 : * nacl_helper, if present, reserves the bottom 1G of the address space
19 : * for use by Native Client.
20 : */
21 0 : int NaClFindPrereservedSandboxMemory(void **p, size_t num_bytes) {
22 0 : NaClLog(2, "NaClFindPrereservedSandboxMemory(, %#.8"NACL_PRIxPTR")\n",
23 : num_bytes);
24 :
25 0 : *p = 0;
26 0 : return num_bytes == g_prereserved_sandbox_size;
27 : }
28 :
29 : /*
30 : * If we have a soft limit for RLIMIT_AS lower than the hard limit,
31 : * increase it to include enough for the untrusted address space.
32 : */
33 279 : void NaClAddrSpaceBeforeAlloc(size_t untrusted_size) {
34 279 : struct rlimit lim;
35 :
36 558 : UNREFERENCED_PARAMETER(untrusted_size);
37 :
38 279 : if (getrlimit(RLIMIT_AS, &lim) < 0) {
39 0 : NaClLog(LOG_WARNING, "NaClAddrSpaceBeforeAlloc: getrlimit failed: %s",
40 0 : strerror(errno));
41 0 : return;
42 : }
43 :
44 : /*
45 : * We don't just increase it by untrusted_size, because we need a
46 : * contiguous piece of that size, and that may stretch the overall
47 : * address space we need to occupy well beyond the sum of the old
48 : * limit and untrusted_size.
49 : */
50 279 : if (lim.rlim_cur < lim.rlim_max) {
51 0 : NaClLog(LOG_INFO, "NaClAddrSpaceBeforeAlloc: "
52 : "raising RLIMIT_AS from %#lx to hard limit %#lx\n",
53 : (unsigned long) lim.rlim_cur,
54 : (unsigned long) lim.rlim_max);
55 0 : lim.rlim_cur = lim.rlim_max;
56 0 : if (setrlimit(RLIMIT_AS, &lim) < 0) {
57 0 : NaClLog(LOG_WARNING, "NaClAddrSpaceBeforeAlloc: setrlimit failed: %s",
58 0 : strerror(errno));
59 0 : }
60 0 : }
61 279 : }
|