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