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 <debugstore/debugstore_cxx_bridge.rs.h>
35 #include <processgroup/processgroup.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
60 // Unless explicitly mentioned otherwise, the following methods have been
61 // introduced in version 1 API, corresponding to SDK level 31.
62
PaletteSchedSetPriority(int32_t tid,int32_t managed_priority)63 palette_status_t PaletteSchedSetPriority(int32_t tid, int32_t managed_priority) {
64 if (managed_priority < art::palette::kMinManagedThreadPriority ||
65 managed_priority > art::palette::kMaxManagedThreadPriority) {
66 return PALETTE_STATUS_INVALID_ARGUMENT;
67 }
68 int new_nice = kNiceValues[managed_priority - art::palette::kMinManagedThreadPriority];
69 int curr_nice = getpriority(PRIO_PROCESS, tid);
70
71 if (curr_nice == new_nice) {
72 return PALETTE_STATUS_OK;
73 }
74
75 if (setpriority(PRIO_PROCESS, tid, new_nice) != 0) {
76 return PALETTE_STATUS_CHECK_ERRNO;
77 }
78 return PALETTE_STATUS_OK;
79 }
80
PaletteSchedGetPriority(int32_t tid,int32_t * managed_priority)81 palette_status_t PaletteSchedGetPriority(int32_t tid, /*out*/ int32_t* managed_priority) {
82 errno = 0;
83 int native_priority = getpriority(PRIO_PROCESS, tid);
84 if (native_priority == -1 && errno != 0) {
85 *managed_priority = art::palette::kNormalManagedThreadPriority;
86 return PALETTE_STATUS_CHECK_ERRNO;
87 }
88
89 for (int p = art::palette::kMinManagedThreadPriority;
90 p <= art::palette::kMaxManagedThreadPriority; p = p + 1) {
91 int index = p - art::palette::kMinManagedThreadPriority;
92 if (native_priority >= kNiceValues[index]) {
93 *managed_priority = p;
94 return PALETTE_STATUS_OK;
95 }
96 }
97 *managed_priority = art::palette::kMaxManagedThreadPriority;
98 return PALETTE_STATUS_OK;
99 }
100
PaletteWriteCrashThreadStacks(const char * stacks,size_t stacks_len)101 palette_status_t PaletteWriteCrashThreadStacks(/*in*/ const char* stacks, size_t stacks_len) {
102 android::base::unique_fd tombstone_fd;
103 android::base::unique_fd output_fd;
104
105 if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) {
106 // Failure here could be due to file descriptor resource exhaustion
107 // so write the stack trace message to the log in case it helps
108 // debug that.
109 LOG(INFO) << std::string_view(stacks, stacks_len);
110 // tombstoned_connect() logs failure reason.
111 return PALETTE_STATUS_FAILED_CHECK_LOG;
112 }
113
114 palette_status_t status = PALETTE_STATUS_OK;
115 if (!android::base::WriteFully(output_fd, stacks, stacks_len)) {
116 PLOG(ERROR) << "Failed to write tombstoned output";
117 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
118 status = PALETTE_STATUS_FAILED_CHECK_LOG;
119 }
120
121 if (TEMP_FAILURE_RETRY(fdatasync(output_fd)) == -1 && errno != EINVAL) {
122 // Ignore EINVAL so we don't report failure if we just tried to flush a pipe
123 // or socket.
124 if (status == PALETTE_STATUS_OK) {
125 PLOG(ERROR) << "Failed to fsync tombstoned output";
126 status = PALETTE_STATUS_FAILED_CHECK_LOG;
127 }
128 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
129 TEMP_FAILURE_RETRY(fdatasync(output_fd));
130 }
131
132 if (close(output_fd.release()) == -1 && errno != EINTR) {
133 if (status == PALETTE_STATUS_OK) {
134 PLOG(ERROR) << "Failed to close tombstoned output";
135 status = PALETTE_STATUS_FAILED_CHECK_LOG;
136 }
137 }
138
139 if (!tombstoned_notify_completion(tombstone_fd)) {
140 // tombstoned_notify_completion() logs failure.
141 status = PALETTE_STATUS_FAILED_CHECK_LOG;
142 }
143
144 return status;
145 }
146
PaletteTraceEnabled(bool * enabled)147 palette_status_t PaletteTraceEnabled(/*out*/ bool* enabled) {
148 *enabled = (ATRACE_ENABLED() != 0) ? true : false;
149 return PALETTE_STATUS_OK;
150 }
151
PaletteTraceBegin(const char * name)152 palette_status_t PaletteTraceBegin(const char* name) {
153 ATRACE_BEGIN(name);
154 return PALETTE_STATUS_OK;
155 }
156
PaletteTraceEnd()157 palette_status_t PaletteTraceEnd() {
158 ATRACE_END();
159 return PALETTE_STATUS_OK;
160 }
161
PaletteTraceIntegerValue(const char * name,int32_t value)162 palette_status_t PaletteTraceIntegerValue(const char* name, int32_t value) {
163 ATRACE_INT(name, value);
164 return PALETTE_STATUS_OK;
165 }
166
167 // Flag whether to use legacy ashmem or current (b/139855428)
168 static std::atomic_bool g_assume_legacy_ashmemd(false);
169
PaletteAshmemCreateRegion(const char * name,size_t size,int * fd)170 palette_status_t PaletteAshmemCreateRegion(const char* name, size_t size, int* fd) {
171 if (g_assume_legacy_ashmemd.load(std::memory_order_acquire) == false) {
172 // Current platform behaviour which open ashmem fd in process (b/139855428)
173 *fd = ashmem_create_region(name, size);
174 if (*fd >= 0) {
175 return PALETTE_STATUS_OK;
176 }
177 }
178
179 // Try legacy behavior just required for ART build bots which may be running tests on older
180 // platform builds.
181
182 // We implement our own ashmem creation, as the libcutils implementation does
183 // a binder call, and our only use of ashmem in ART is for zygote, which
184 // cannot communicate to binder.
185 *fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC));
186 if (*fd == -1) {
187 return PALETTE_STATUS_CHECK_ERRNO;
188 }
189
190 if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_SIZE, size)) < 0) {
191 goto error;
192 }
193
194 if (name != nullptr) {
195 char buf[ASHMEM_NAME_LEN] = {0};
196 strlcpy(buf, name, sizeof(buf));
197 if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_NAME, buf)) < 0) {
198 goto error;
199 }
200 }
201
202 g_assume_legacy_ashmemd.store(true, std::memory_order_release);
203 return PALETTE_STATUS_OK;
204
205 error:
206 // Save errno before closing.
207 int save_errno = errno;
208 close(*fd);
209 errno = save_errno;
210 return PALETTE_STATUS_CHECK_ERRNO;
211 }
212
PaletteAshmemSetProtRegion(int fd,int prot)213 palette_status_t PaletteAshmemSetProtRegion(int fd, int prot) {
214 if (!g_assume_legacy_ashmemd.load(std::memory_order_acquire)) {
215 if (ashmem_set_prot_region(fd, prot) < 0) {
216 return PALETTE_STATUS_CHECK_ERRNO;
217 }
218 } else if (TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)) < 0) {
219 // Legacy behavior just required for ART build bots which may be running tests on older
220 // platform builds.
221 return PALETTE_STATUS_CHECK_ERRNO;
222 }
223 return PALETTE_STATUS_OK;
224 }
225
PaletteCreateOdrefreshStagingDirectory(const char ** staging_dir)226 palette_status_t PaletteCreateOdrefreshStagingDirectory(const char** staging_dir) {
227 static constexpr const char* kStagingDirectory = "/data/misc/apexdata/com.android.art/staging";
228
229 std::error_code ec;
230 if (std::filesystem::exists(kStagingDirectory, ec)) {
231 if (!std::filesystem::remove_all(kStagingDirectory, ec)) {
232 LOG(ERROR) << ec.message()
233 << "Could not remove existing staging directory: " << kStagingDirectory;
234 DCHECK_EQ(ec.value(), errno);
235 return PALETTE_STATUS_CHECK_ERRNO;
236 }
237 }
238
239 if (mkdir(kStagingDirectory, S_IRWXU) != 0) {
240 PLOG(ERROR) << "Could not set permissions on staging directory: " << kStagingDirectory;
241 return PALETTE_STATUS_CHECK_ERRNO;
242 }
243
244 if (setfilecon(kStagingDirectory, "u:object_r:apex_art_staging_data_file:s0") != 0) {
245 PLOG(ERROR) << "Could not set label on staging directory: " << kStagingDirectory;
246 return PALETTE_STATUS_CHECK_ERRNO;
247 }
248
249 *staging_dir = kStagingDirectory;
250 return PALETTE_STATUS_OK;
251 }
252
253 // Introduced in version 3 API, corresponding to SDK level 34.
PaletteSetTaskProfiles(int32_t tid,const char * const profiles[],size_t profiles_len)254 palette_status_t PaletteSetTaskProfiles(int32_t tid, const char* const profiles[],
255 size_t profiles_len) {
256 std::vector<std::string> p;
257 p.reserve(profiles_len);
258 for (int i = 0; i < profiles_len; ++i) {
259 p.push_back(profiles[i]);
260 }
261 return SetTaskProfiles(tid, p, false) ? PALETTE_STATUS_OK : PALETTE_STATUS_FAILED_CHECK_LOG;
262 }
263
264 // Introduced in version 4 API, corresponding to SDK level 36.
PaletteDebugStoreGetString(char * result,size_t max_size)265 palette_status_t PaletteDebugStoreGetString(char* result, size_t max_size) {
266 if (result == nullptr || max_size == 0) {
267 return PALETTE_STATUS_INVALID_ARGUMENT;
268 }
269 std::string store_string = static_cast<std::string>(android::debugstore::debug_store_to_string());
270 strncpy(result, store_string.c_str(), max_size - 1);
271 result[max_size - 1] = '\0';
272 return PALETTE_STATUS_OK;
273 }