1 /*
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 
27 extern "C" size_t __cfi_shadow_size();
28 
29 namespace android {
30 
limitProcessMemory(const char * property,size_t numberOfBytes,size_t percentageOfTotalMem)31 void limitProcessMemory(
32     const char *property,
33     size_t numberOfBytes,
34     size_t percentageOfTotalMem) {
35 
36     if (running_with_asan()) {
37         ALOGW("Running with ASan, skip enforcing memory limitations.");
38         return;
39     }
40 
41     long pageSize = sysconf(_SC_PAGESIZE);
42     long numPages = sysconf(_SC_PHYS_PAGES);
43     size_t maxMem = SIZE_MAX;
44 
45     if (pageSize > 0 && numPages > 0) {
46         if (size_t(numPages) < SIZE_MAX / size_t(pageSize)) {
47             maxMem = size_t(numPages) * size_t(pageSize);
48         }
49         ALOGV("physMem: %zu", maxMem);
50         if (percentageOfTotalMem > 100) {
51             ALOGW("requested %zu%% of total memory, using 100%%", percentageOfTotalMem);
52             percentageOfTotalMem = 100;
53         }
54         maxMem = maxMem / 100 * percentageOfTotalMem;
55         if (numberOfBytes < maxMem) {
56             maxMem = numberOfBytes;
57         }
58         ALOGV("requested limit: %zu", maxMem);
59     } else {
60         ALOGW("couldn't determine total RAM");
61     }
62 
63     int64_t propVal = property_get_int64(property, maxMem);
64     if (propVal > 0 && uint64_t(propVal) <= SIZE_MAX) {
65         maxMem = propVal;
66     }
67 
68     // Increase by the size of the CFI shadow mapping. Most of the shadow is not
69     // backed with physical pages, and it is possible for the result to be
70     // higher than total physical memory. This is fine for RLIMIT_AS.
71     size_t cfi_size = __cfi_shadow_size();
72     if (cfi_size) {
73       ALOGV("cfi shadow size: %zu", cfi_size);
74       if (maxMem <= SIZE_MAX - cfi_size) {
75         maxMem += cfi_size;
76       } else {
77         maxMem = SIZE_MAX;
78       }
79     }
80     ALOGV("actual limit: %zu", maxMem);
81 
82     struct rlimit limit;
83     getrlimit(RLIMIT_AS, &limit);
84     ALOGV("original limits: %lld/%lld", (long long)limit.rlim_cur, (long long)limit.rlim_max);
85     limit.rlim_cur = maxMem;
86     setrlimit(RLIMIT_AS, &limit);
87     limit.rlim_cur = -1;
88     limit.rlim_max = -1;
89     getrlimit(RLIMIT_AS, &limit);
90     ALOGV("new limits: %lld/%lld", (long long)limit.rlim_cur, (long long)limit.rlim_max);
91 
92 }
93 
94 } // namespace android
95