1 : /*
2 : * Copyright (c) 2011 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 : /*
15 : * If we have a soft limit for RLIMIT_AS lower than the hard limit,
16 : * increase it to include enough for the untrusted address space.
17 : */
18 4 : void NaClAddrSpaceBeforeAlloc(size_t untrusted_size) {
19 : struct rlimit lim;
20 :
21 : UNREFERENCED_PARAMETER(untrusted_size);
22 :
23 4 : if (getrlimit(RLIMIT_AS, &lim) < 0) {
24 0 : NaClLog(LOG_WARNING, "NaClAddrSpaceBeforeAlloc: getrlimit failed: %s",
25 : strerror(errno));
26 0 : return;
27 : }
28 :
29 : /*
30 : * We don't just increase it by untrusted_size, because we need a
31 : * contiguous piece of that size, and that may stretch the overall
32 : * address space we need to occupy well beyond the sum of the old
33 : * limit and untrusted_size.
34 : */
35 4 : if (lim.rlim_cur < lim.rlim_max) {
36 0 : NaClLog(LOG_INFO, "NaClAddrSpaceBeforeAlloc: "
37 : "raising RLIMIT_AS from %#lx to hard limit %#lx\n",
38 : (unsigned long) lim.rlim_cur,
39 : (unsigned long) lim.rlim_max);
40 0 : lim.rlim_cur = lim.rlim_max;
41 0 : if (setrlimit(RLIMIT_AS, &lim) < 0) {
42 0 : NaClLog(LOG_WARNING, "NaClAddrSpaceBeforeAlloc: setrlimit failed: %s",
43 : strerror(errno));
44 : }
45 : }
46 : }
|