blob: bcc7ebfd458a54216a3c4016ca47e6f1c2380188 [file] [log] [blame]
Marco Nelissenc57fe212016-05-31 09:45:43 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MediaUtils"
18#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <cutils/properties.h>
22#include <sys/resource.h>
23#include <unistd.h>
24
25#include "MediaUtils.h"
26
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070027extern "C" size_t __cfi_shadow_size();
Kostya Kortchinskye08896a2018-10-17 12:37:10 -070028extern "C" void __scudo_set_rss_limit(size_t, int) __attribute__((weak));
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070029
Marco Nelissenc57fe212016-05-31 09:45:43 -070030namespace android {
31
32void limitProcessMemory(
33 const char *property,
34 size_t numberOfBytes,
35 size_t percentageOfTotalMem) {
36
Jeff Vander Stoepb1005322016-10-01 20:26:51 -070037 if (running_with_asan()) {
Evgenii Stepanov7bd26ab2018-07-27 11:58:12 -070038 ALOGW("Running with (HW)ASan, skip enforcing memory limitations.");
Jeff Vander Stoepb1005322016-10-01 20:26:51 -070039 return;
40 }
41
Marco Nelissenc57fe212016-05-31 09:45:43 -070042 long pageSize = sysconf(_SC_PAGESIZE);
43 long numPages = sysconf(_SC_PHYS_PAGES);
44 size_t maxMem = SIZE_MAX;
45
46 if (pageSize > 0 && numPages > 0) {
47 if (size_t(numPages) < SIZE_MAX / size_t(pageSize)) {
48 maxMem = size_t(numPages) * size_t(pageSize);
49 }
50 ALOGV("physMem: %zu", maxMem);
51 if (percentageOfTotalMem > 100) {
52 ALOGW("requested %zu%% of total memory, using 100%%", percentageOfTotalMem);
53 percentageOfTotalMem = 100;
54 }
55 maxMem = maxMem / 100 * percentageOfTotalMem;
56 if (numberOfBytes < maxMem) {
57 maxMem = numberOfBytes;
58 }
59 ALOGV("requested limit: %zu", maxMem);
60 } else {
61 ALOGW("couldn't determine total RAM");
62 }
63
64 int64_t propVal = property_get_int64(property, maxMem);
65 if (propVal > 0 && uint64_t(propVal) <= SIZE_MAX) {
66 maxMem = propVal;
67 }
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070068
Kostya Kortchinskye08896a2018-10-17 12:37:10 -070069 // If 64-bit Scudo is in use, enforce the hard RSS limit (in MB).
70 if (maxMem != SIZE_MAX && sizeof(void *) == 8 &&
71 &__scudo_set_rss_limit != 0) {
72 __scudo_set_rss_limit(maxMem >> 20, 1);
73 ALOGV("Scudo hard RSS limit set to %zu MB", maxMem >> 20);
74 return;
75 }
76
Evgenii Stepanovf1b05232017-08-02 16:36:56 -070077 // Increase by the size of the CFI shadow mapping. Most of the shadow is not
78 // backed with physical pages, and it is possible for the result to be
79 // higher than total physical memory. This is fine for RLIMIT_AS.
80 size_t cfi_size = __cfi_shadow_size();
81 if (cfi_size) {
82 ALOGV("cfi shadow size: %zu", cfi_size);
83 if (maxMem <= SIZE_MAX - cfi_size) {
84 maxMem += cfi_size;
85 } else {
86 maxMem = SIZE_MAX;
87 }
88 }
Marco Nelissenc57fe212016-05-31 09:45:43 -070089 ALOGV("actual limit: %zu", maxMem);
90
91 struct rlimit limit;
92 getrlimit(RLIMIT_AS, &limit);
93 ALOGV("original limits: %lld/%lld", (long long)limit.rlim_cur, (long long)limit.rlim_max);
94 limit.rlim_cur = maxMem;
95 setrlimit(RLIMIT_AS, &limit);
96 limit.rlim_cur = -1;
97 limit.rlim_max = -1;
98 getrlimit(RLIMIT_AS, &limit);
99 ALOGV("new limits: %lld/%lld", (long long)limit.rlim_cur, (long long)limit.rlim_max);
100
101}
102
103} // namespace android