1 /*
2 * Copyright (C) 2019 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 ATRACE_TAG ATRACE_TAG_DALVIK
18
19 #include "palette/palette.h"
20
21 #include <errno.h>
22 #include <sys/resource.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25
26 #include <filesystem>
27 #include <mutex>
28
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/macros.h>
32 #include <cutils/ashmem.h>
33 #include <cutils/trace.h>
34 #include <processgroup/processgroup.h>
35 #include <processgroup/sched_policy.h>
36 #include <selinux/selinux.h>
37 #include <tombstoned/tombstoned.h>
38 #include <utils/Thread.h>
39
40 #include "palette_system.h"
41
42 // Conversion map for "nice" values.
43 //
44 // We use Android thread priority constants to be consistent with the rest
45 // of the system. In some cases adjacent entries may overlap.
46 //
47 static const int kNiceValues[art::palette::kNumManagedThreadPriorities] = {
48 ANDROID_PRIORITY_LOWEST, // 1 (MIN_PRIORITY)
49 ANDROID_PRIORITY_BACKGROUND + 6,
50 ANDROID_PRIORITY_BACKGROUND + 3,
51 ANDROID_PRIORITY_BACKGROUND,
52 ANDROID_PRIORITY_NORMAL, // 5 (NORM_PRIORITY)
53 ANDROID_PRIORITY_NORMAL - 2,
54 ANDROID_PRIORITY_NORMAL - 4,
55 ANDROID_PRIORITY_URGENT_DISPLAY + 3,
56 ANDROID_PRIORITY_URGENT_DISPLAY + 2,
57 ANDROID_PRIORITY_URGENT_DISPLAY // 10 (MAX_PRIORITY)
58 };
59
PaletteSchedSetPriority(int32_t tid,int32_t managed_priority)60 palette_status_t PaletteSchedSetPriority(int32_t tid, int32_t managed_priority) {
61 if (managed_priority < art::palette::kMinManagedThreadPriority ||
62 managed_priority > art::palette::kMaxManagedThreadPriority) {
63 return PALETTE_STATUS_INVALID_ARGUMENT;
64 }
65 int new_nice = kNiceValues[managed_priority - art::palette::kMinManagedThreadPriority];
66 int curr_nice = getpriority(PRIO_PROCESS, tid);
67
68 if (curr_nice == new_nice) {
69 return PALETTE_STATUS_OK;
70 }
71
72 if (new_nice >= ANDROID_PRIORITY_BACKGROUND) {
73 SetTaskProfiles(tid, {"SCHED_SP_SYSTEM"}, true);
74 } else if (curr_nice >= ANDROID_PRIORITY_BACKGROUND) {
75 SetTaskProfiles(tid, {"SCHED_SP_FOREGROUND"}, true);
76 }
77
78 if (setpriority(PRIO_PROCESS, tid, new_nice) != 0) {
79 return PALETTE_STATUS_CHECK_ERRNO;
80 }
81 return PALETTE_STATUS_OK;
82 }
83
PaletteSchedGetPriority(int32_t tid,int32_t * managed_priority)84 palette_status_t PaletteSchedGetPriority(int32_t tid, /*out*/ int32_t* managed_priority) {
85 errno = 0;
86 int native_priority = getpriority(PRIO_PROCESS, tid);
87 if (native_priority == -1 && errno != 0) {
88 *managed_priority = art::palette::kNormalManagedThreadPriority;
89 return PALETTE_STATUS_CHECK_ERRNO;
90 }
91
92 for (int p = art::palette::kMinManagedThreadPriority;
93 p <= art::palette::kMaxManagedThreadPriority; p = p + 1) {
94 int index = p - art::palette::kMinManagedThreadPriority;
95 if (native_priority >= kNiceValues[index]) {
96 *managed_priority = p;
97 return PALETTE_STATUS_OK;
98 }
99 }
100 *managed_priority = art::palette::kMaxManagedThreadPriority;
101 return PALETTE_STATUS_OK;
102 }
103
PaletteWriteCrashThreadStacks(const char * stacks,size_t stacks_len)104 palette_status_t PaletteWriteCrashThreadStacks(/*in*/ const char* stacks, size_t stacks_len) {
105 android::base::unique_fd tombstone_fd;
106 android::base::unique_fd output_fd;
107
108 if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) {
109 // Failure here could be due to file descriptor resource exhaustion
110 // so write the stack trace message to the log in case it helps
111 // debug that.
112 LOG(INFO) << std::string_view(stacks, stacks_len);
113 // tombstoned_connect() logs failure reason.
114 return PALETTE_STATUS_FAILED_CHECK_LOG;
115 }
116
117 palette_status_t status = PALETTE_STATUS_OK;
118 if (!android::base::WriteFully(output_fd, stacks, stacks_len)) {
119 PLOG(ERROR) << "Failed to write tombstoned output";
120 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
121 status = PALETTE_STATUS_FAILED_CHECK_LOG;
122 }
123
124 if (TEMP_FAILURE_RETRY(fdatasync(output_fd)) == -1 && errno != EINVAL) {
125 // Ignore EINVAL so we don't report failure if we just tried to flush a pipe
126 // or socket.
127 if (status == PALETTE_STATUS_OK) {
128 PLOG(ERROR) << "Failed to fsync tombstoned output";
129 status = PALETTE_STATUS_FAILED_CHECK_LOG;
130 }
131 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
132 TEMP_FAILURE_RETRY(fdatasync(output_fd));
133 }
134
135 if (close(output_fd.release()) == -1 && errno != EINTR) {
136 if (status == PALETTE_STATUS_OK) {
137 PLOG(ERROR) << "Failed to close tombstoned output";
138 status = PALETTE_STATUS_FAILED_CHECK_LOG;
139 }
140 }
141
142 if (!tombstoned_notify_completion(tombstone_fd)) {
143 // tombstoned_notify_completion() logs failure.
144 status = PALETTE_STATUS_FAILED_CHECK_LOG;
145 }
146
147 return status;
148 }
149
PaletteTraceEnabled(bool * enabled)150 palette_status_t PaletteTraceEnabled(/*out*/ bool* enabled) {
151 *enabled = (ATRACE_ENABLED() != 0) ? true : false;
152 return PALETTE_STATUS_OK;
153 }
154
PaletteTraceBegin(const char * name)155 palette_status_t PaletteTraceBegin(const char* name) {
156 ATRACE_BEGIN(name);
157 return PALETTE_STATUS_OK;
158 }
159
PaletteTraceEnd()160 palette_status_t PaletteTraceEnd() {
161 ATRACE_END();
162 return PALETTE_STATUS_OK;
163 }
164
PaletteTraceIntegerValue(const char * name,int32_t value)165 palette_status_t PaletteTraceIntegerValue(const char* name, int32_t value) {
166 ATRACE_INT(name, value);
167 return PALETTE_STATUS_OK;
168 }
169
170 // Flag whether to use legacy ashmem or current (b/139855428)
171 static std::atomic_bool g_assume_legacy_ashmemd(false);
172
PaletteAshmemCreateRegion(const char * name,size_t size,int * fd)173 palette_status_t PaletteAshmemCreateRegion(const char* name, size_t size, int* fd) {
174 if (g_assume_legacy_ashmemd.load(std::memory_order_acquire) == false) {
175 // Current platform behaviour which open ashmem fd in process (b/139855428)
176 *fd = ashmem_create_region(name, size);
177 if (*fd >= 0) {
178 return PALETTE_STATUS_OK;
179 }
180 }
181
182 // Try legacy behavior just required for ART build bots which may be running tests on older
183 // platform builds.
184
185 // We implement our own ashmem creation, as the libcutils implementation does
186 // a binder call, and our only use of ashmem in ART is for zygote, which
187 // cannot communicate to binder.
188 *fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC));
189 if (*fd == -1) {
190 return PALETTE_STATUS_CHECK_ERRNO;
191 }
192
193 if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_SIZE, size)) < 0) {
194 goto error;
195 }
196
197 if (name != nullptr) {
198 char buf[ASHMEM_NAME_LEN] = {0};
199 strlcpy(buf, name, sizeof(buf));
200 if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_NAME, buf)) < 0) {
201 goto error;
202 }
203 }
204
205 g_assume_legacy_ashmemd.store(true, std::memory_order_release);
206 return PALETTE_STATUS_OK;
207
208 error:
209 // Save errno before closing.
210 int save_errno = errno;
211 close(*fd);
212 errno = save_errno;
213 return PALETTE_STATUS_CHECK_ERRNO;
214 }
215
PaletteAshmemSetProtRegion(int fd,int prot)216 palette_status_t PaletteAshmemSetProtRegion(int fd, int prot) {
217 if (!g_assume_legacy_ashmemd.load(std::memory_order_acquire)) {
218 if (ashmem_set_prot_region(fd, prot) < 0) {
219 return PALETTE_STATUS_CHECK_ERRNO;
220 }
221 } else if (TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)) < 0) {
222 // Legacy behavior just required for ART build bots which may be running tests on older
223 // platform builds.
224 return PALETTE_STATUS_CHECK_ERRNO;
225 }
226 return PALETTE_STATUS_OK;
227 }
228
PaletteCreateOdrefreshStagingDirectory(const char ** staging_dir)229 palette_status_t PaletteCreateOdrefreshStagingDirectory(const char** staging_dir) {
230 static constexpr const char* kStagingDirectory = "/data/misc/apexdata/com.android.art/staging";
231
232 std::error_code ec;
233 if (std::filesystem::exists(kStagingDirectory, ec)) {
234 if (!std::filesystem::remove_all(kStagingDirectory, ec)) {
235 LOG(ERROR) << ec.message()
236 << "Could not remove existing staging directory: " << kStagingDirectory;
237 DCHECK_EQ(ec.value(), errno);
238 return PALETTE_STATUS_CHECK_ERRNO;
239 }
240 }
241
242 if (mkdir(kStagingDirectory, S_IRWXU) != 0) {
243 PLOG(ERROR) << "Could not set permissions on staging directory: " << kStagingDirectory;
244 return PALETTE_STATUS_CHECK_ERRNO;
245 }
246
247 if (setfilecon(kStagingDirectory, "u:object_r:apex_art_staging_data_file:s0") != 0) {
248 PLOG(ERROR) << "Could not set label on staging directory: " << kStagingDirectory;
249 return PALETTE_STATUS_CHECK_ERRNO;
250 }
251
252 *staging_dir = kStagingDirectory;
253 return PALETTE_STATUS_OK;
254 }
255