blob: 3fe18c7933470d296a752b1f57f7195c47e5977c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughes42ee1422011-09-06 12:33:32 -070017#include "utils.h"
18
Christopher Ferris943af7d2014-01-16 12:41:46 -080019#include <inttypes.h>
Elliott Hughes92b3b562011-09-08 16:32:26 -070020#include <pthread.h>
Mathieu Chartier120aa282017-08-05 16:03:03 -070021#include <sys/mman.h> // For madvise
Brian Carlstroma9f19782011-10-13 00:14:47 -070022#include <sys/stat.h>
Elliott Hughes42ee1422011-09-06 12:33:32 -070023#include <sys/syscall.h>
24#include <sys/types.h>
Brian Carlstrom4cf5e572014-02-25 11:47:48 -080025#include <sys/wait.h>
Elliott Hughes42ee1422011-09-06 12:33:32 -070026#include <unistd.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027
Ian Rogers700a4022014-05-19 16:49:03 -070028#include <memory>
Elliott Hughes42ee1422011-09-06 12:33:32 -070029
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080031#include "android-base/strings.h"
32
Brian Carlstrom6449c622014-02-10 23:48:36 -080033#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080034#include "base/unix_file/fd_file.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Andreas Gampe5073fed2015-08-10 11:40:25 -070036#include "dex_instruction.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010037#include "oat_quick_method_header.h"
buzbeec143c552011-08-20 17:38:58 -070038#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Ian Rogersa6724902013-09-23 09:23:37 -070040#include "utf-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070041
Elliott Hughes4ae722a2012-03-13 11:08:51 -070042#if defined(__APPLE__)
David Sehrfa442002016-08-22 18:42:08 -070043#include <crt_externs.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070044#include <sys/syscall.h>
45#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
Elliott Hughes4ae722a2012-03-13 11:08:51 -070046#endif
47
Elliott Hughes058a6de2012-05-24 19:13:02 -070048#if defined(__linux__)
Elliott Hughese1aee692012-01-17 16:40:10 -080049#include <linux/unistd.h>
Elliott Hughese1aee692012-01-17 16:40:10 -080050#endif
51
Elliott Hughes11e45072011-08-16 17:40:46 -070052namespace art {
53
Andreas Gampe46ee31b2016-12-14 10:11:49 -080054using android::base::StringAppendF;
55using android::base::StringPrintf;
56
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080057pid_t GetTid() {
Brian Carlstromf3a26412012-08-24 11:06:02 -070058#if defined(__APPLE__)
59 uint64_t owner;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070060 CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__); // Requires Mac OS 10.6
Brian Carlstromf3a26412012-08-24 11:06:02 -070061 return owner;
Elliott Hughes323aa862014-08-20 15:00:04 -070062#elif defined(__BIONIC__)
63 return gettid();
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080064#else
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080065 return syscall(__NR_gettid);
66#endif
67}
68
Elliott Hughes289be852012-06-12 13:57:20 -070069std::string GetThreadName(pid_t tid) {
70 std::string result;
71 if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070072 result.resize(result.size() - 1); // Lose the trailing '\n'.
Elliott Hughes289be852012-06-12 13:57:20 -070073 } else {
74 result = "<unknown>";
75 }
76 return result;
77}
78
Elliott Hughesd92bec42011-09-02 17:04:36 -070079bool ReadFileToString(const std::string& file_name, std::string* result) {
Andreas Gampedf878922015-08-13 16:44:54 -070080 File file(file_name, O_RDONLY, false);
81 if (!file.IsOpened()) {
Elliott Hughesd92bec42011-09-02 17:04:36 -070082 return false;
83 }
buzbeec143c552011-08-20 17:38:58 -070084
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070085 std::vector<char> buf(8 * KB);
buzbeec143c552011-08-20 17:38:58 -070086 while (true) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -080087 int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[0], buf.size()));
Elliott Hughesd92bec42011-09-02 17:04:36 -070088 if (n == -1) {
89 return false;
buzbeec143c552011-08-20 17:38:58 -070090 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070091 if (n == 0) {
92 return true;
93 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070094 result->append(&buf[0], n);
buzbeec143c552011-08-20 17:38:58 -070095 }
buzbeec143c552011-08-20 17:38:58 -070096}
97
Andreas Gampea6dfdae2015-02-24 15:50:19 -080098bool PrintFileToLog(const std::string& file_name, LogSeverity level) {
Andreas Gampedf878922015-08-13 16:44:54 -070099 File file(file_name, O_RDONLY, false);
100 if (!file.IsOpened()) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800101 return false;
102 }
103
104 constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings.
105 char buf[kBufSize + 1]; // +1 for terminator.
106 size_t filled_to = 0;
107 while (true) {
108 DCHECK_LT(filled_to, kBufSize);
109 int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to));
110 if (n <= 0) {
111 // Print the rest of the buffer, if it exists.
112 if (filled_to > 0) {
113 buf[filled_to] = 0;
114 LOG(level) << buf;
115 }
116 return n == 0;
117 }
118 // Scan for '\n'.
119 size_t i = filled_to;
120 bool found_newline = false;
121 for (; i < filled_to + n; ++i) {
122 if (buf[i] == '\n') {
123 // Found a line break, that's something to print now.
124 buf[i] = 0;
125 LOG(level) << buf;
126 // Copy the rest to the front.
127 if (i + 1 < filled_to + n) {
128 memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1);
129 filled_to = filled_to + n - i - 1;
130 } else {
131 filled_to = 0;
132 }
133 found_newline = true;
134 break;
135 }
136 }
137 if (found_newline) {
138 continue;
139 } else {
140 filled_to += n;
141 // Check if we must flush now.
142 if (filled_to == kBufSize) {
143 buf[kBufSize] = 0;
144 LOG(level) << buf;
145 filled_to = 0;
146 }
147 }
148 }
149}
150
Ian Rogers1ff3c982014-08-12 02:30:58 -0700151std::string PrettyDescriptor(const char* descriptor) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700152 // Count the number of '['s to get the dimensionality.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700153 const char* c = descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700154 size_t dim = 0;
155 while (*c == '[') {
156 dim++;
157 c++;
158 }
159
160 // Reference or primitive?
161 if (*c == 'L') {
162 // "[[La/b/C;" -> "a.b.C[][]".
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700163 c++; // Skip the 'L'.
Elliott Hughes11e45072011-08-16 17:40:46 -0700164 } else {
165 // "[[B" -> "byte[][]".
166 // To make life easier, we make primitives look like unqualified
167 // reference types.
168 switch (*c) {
169 case 'B': c = "byte;"; break;
170 case 'C': c = "char;"; break;
171 case 'D': c = "double;"; break;
172 case 'F': c = "float;"; break;
173 case 'I': c = "int;"; break;
174 case 'J': c = "long;"; break;
175 case 'S': c = "short;"; break;
176 case 'Z': c = "boolean;"; break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700177 case 'V': c = "void;"; break; // Used when decoding return types.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700178 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700179 }
180 }
181
182 // At this point, 'c' is a string of the form "fully/qualified/Type;"
183 // or "primitive;". Rewrite the type with '.' instead of '/':
184 std::string result;
185 const char* p = c;
186 while (*p != ';') {
187 char ch = *p++;
188 if (ch == '/') {
189 ch = '.';
190 }
191 result.push_back(ch);
192 }
193 // ...and replace the semicolon with 'dim' "[]" pairs:
Ian Rogers1ff3c982014-08-12 02:30:58 -0700194 for (size_t i = 0; i < dim; ++i) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700195 result += "[]";
196 }
197 return result;
198}
199
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700200std::string PrettyArguments(const char* signature) {
201 std::string result;
202 result += '(';
203 CHECK_EQ(*signature, '(');
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700204 ++signature; // Skip the '('.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700205 while (*signature != ')') {
206 size_t argument_length = 0;
207 while (signature[argument_length] == '[') {
208 ++argument_length;
209 }
210 if (signature[argument_length] == 'L') {
211 argument_length = (strchr(signature, ';') - signature + 1);
212 } else {
213 ++argument_length;
214 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 {
216 std::string argument_descriptor(signature, argument_length);
217 result += PrettyDescriptor(argument_descriptor.c_str());
218 }
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700219 if (signature[argument_length] != ')') {
220 result += ", ";
221 }
222 signature += argument_length;
223 }
224 CHECK_EQ(*signature, ')');
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700225 ++signature; // Skip the ')'.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700226 result += ')';
227 return result;
228}
229
230std::string PrettyReturnType(const char* signature) {
231 const char* return_type = strchr(signature, ')');
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700232 CHECK(return_type != nullptr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700233 ++return_type; // Skip ')'.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700234 return PrettyDescriptor(return_type);
235}
236
Andreas Gampec0d82292014-09-23 10:38:30 -0700237std::string PrettyJavaAccessFlags(uint32_t access_flags) {
238 std::string result;
239 if ((access_flags & kAccPublic) != 0) {
240 result += "public ";
241 }
242 if ((access_flags & kAccProtected) != 0) {
243 result += "protected ";
244 }
245 if ((access_flags & kAccPrivate) != 0) {
246 result += "private ";
247 }
248 if ((access_flags & kAccFinal) != 0) {
249 result += "final ";
250 }
251 if ((access_flags & kAccStatic) != 0) {
252 result += "static ";
253 }
David Brazdilca3c8c32016-09-06 14:04:48 +0100254 if ((access_flags & kAccAbstract) != 0) {
255 result += "abstract ";
256 }
257 if ((access_flags & kAccInterface) != 0) {
258 result += "interface ";
259 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700260 if ((access_flags & kAccTransient) != 0) {
261 result += "transient ";
262 }
263 if ((access_flags & kAccVolatile) != 0) {
264 result += "volatile ";
265 }
266 if ((access_flags & kAccSynchronized) != 0) {
267 result += "synchronized ";
268 }
269 return result;
270}
271
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800272std::string PrettySize(int64_t byte_count) {
Elliott Hughesc967f782012-04-16 10:23:15 -0700273 // The byte thresholds at which we display amounts. A byte count is displayed
274 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
Ian Rogersef7d42f2014-01-06 12:55:46 -0800275 static const int64_t kUnitThresholds[] = {
Elliott Hughesc967f782012-04-16 10:23:15 -0700276 0, // B up to...
277 3*1024, // KB up to...
278 2*1024*1024, // MB up to...
279 1024*1024*1024 // GB from here.
280 };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800281 static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
Elliott Hughesc967f782012-04-16 10:23:15 -0700282 static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800283 const char* negative_str = "";
284 if (byte_count < 0) {
285 negative_str = "-";
286 byte_count = -byte_count;
287 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700288 int i = arraysize(kUnitThresholds);
289 while (--i > 0) {
290 if (byte_count >= kUnitThresholds[i]) {
291 break;
292 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800293 }
Brian Carlstrom474cc792014-03-07 14:18:15 -0800294 return StringPrintf("%s%" PRId64 "%s",
295 negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800296}
297
Andreas Gampe9186ced2016-12-12 14:28:21 -0800298static inline constexpr bool NeedsEscaping(uint16_t ch) {
299 return (ch < ' ' || ch > '~');
300}
301
Ian Rogers576ca0c2014-06-06 15:58:22 -0700302std::string PrintableChar(uint16_t ch) {
303 std::string result;
304 result += '\'';
305 if (NeedsEscaping(ch)) {
306 StringAppendF(&result, "\\u%04x", ch);
307 } else {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700308 result += static_cast<std::string::value_type>(ch);
Ian Rogers576ca0c2014-06-06 15:58:22 -0700309 }
310 result += '\'';
311 return result;
312}
313
Ian Rogers68b56852014-08-29 20:19:11 -0700314std::string PrintableString(const char* utf) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700315 std::string result;
316 result += '"';
Ian Rogers68b56852014-08-29 20:19:11 -0700317 const char* p = utf;
Elliott Hughes82914b62012-04-09 15:56:29 -0700318 size_t char_count = CountModifiedUtf8Chars(p);
319 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000320 uint32_t ch = GetUtf16FromUtf8(&p);
Elliott Hughes82914b62012-04-09 15:56:29 -0700321 if (ch == '\\') {
322 result += "\\\\";
323 } else if (ch == '\n') {
324 result += "\\n";
325 } else if (ch == '\r') {
326 result += "\\r";
327 } else if (ch == '\t') {
328 result += "\\t";
Elliott Hughes82914b62012-04-09 15:56:29 -0700329 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000330 const uint16_t leading = GetLeadingUtf16Char(ch);
331
332 if (NeedsEscaping(leading)) {
333 StringAppendF(&result, "\\u%04x", leading);
334 } else {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700335 result += static_cast<std::string::value_type>(leading);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000336 }
337
338 const uint32_t trailing = GetTrailingUtf16Char(ch);
339 if (trailing != 0) {
340 // All high surrogates will need escaping.
341 StringAppendF(&result, "\\u%04x", trailing);
342 }
Elliott Hughes82914b62012-04-09 15:56:29 -0700343 }
344 }
345 result += '"';
346 return result;
347}
348
Alex Light888a59e2017-01-25 11:41:41 -0800349std::string GetJniShortName(const std::string& class_descriptor, const std::string& method) {
350 // Remove the leading 'L' and trailing ';'...
351 std::string class_name(class_descriptor);
352 CHECK_EQ(class_name[0], 'L') << class_name;
353 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
354 class_name.erase(0, 1);
355 class_name.erase(class_name.size() - 1, 1);
356
357 std::string short_name;
358 short_name += "Java_";
359 short_name += MangleForJni(class_name);
360 short_name += "_";
361 short_name += MangleForJni(method);
362 return short_name;
363}
364
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800365// See http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615 for the full rules.
Elliott Hughes79082e32011-08-25 12:07:32 -0700366std::string MangleForJni(const std::string& s) {
367 std::string result;
368 size_t char_count = CountModifiedUtf8Chars(s.c_str());
369 const char* cp = &s[0];
370 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000371 uint32_t ch = GetUtf16FromUtf8(&cp);
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800372 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
373 result.push_back(ch);
374 } else if (ch == '.' || ch == '/') {
375 result += "_";
376 } else if (ch == '_') {
377 result += "_1";
378 } else if (ch == ';') {
379 result += "_2";
380 } else if (ch == '[') {
381 result += "_3";
Elliott Hughes79082e32011-08-25 12:07:32 -0700382 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000383 const uint16_t leading = GetLeadingUtf16Char(ch);
384 const uint32_t trailing = GetTrailingUtf16Char(ch);
385
386 StringAppendF(&result, "_0%04x", leading);
387 if (trailing != 0) {
388 StringAppendF(&result, "_0%04x", trailing);
389 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700390 }
391 }
392 return result;
393}
394
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700395std::string DotToDescriptor(const char* class_name) {
396 std::string descriptor(class_name);
397 std::replace(descriptor.begin(), descriptor.end(), '.', '/');
398 if (descriptor.length() > 0 && descriptor[0] != '[') {
399 descriptor = "L" + descriptor + ";";
400 }
401 return descriptor;
402}
403
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800404std::string DescriptorToDot(const char* descriptor) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800405 size_t length = strlen(descriptor);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700406 if (length > 1) {
407 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
408 // Descriptors have the leading 'L' and trailing ';' stripped.
409 std::string result(descriptor + 1, length - 2);
410 std::replace(result.begin(), result.end(), '/', '.');
411 return result;
412 } else {
413 // For arrays the 'L' and ';' remain intact.
414 std::string result(descriptor);
415 std::replace(result.begin(), result.end(), '/', '.');
416 return result;
417 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800418 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700419 // Do nothing for non-class/array descriptors.
Elliott Hughes2435a572012-02-17 16:07:41 -0800420 return descriptor;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800421}
422
423std::string DescriptorToName(const char* descriptor) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800424 size_t length = strlen(descriptor);
Elliott Hughes2435a572012-02-17 16:07:41 -0800425 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
426 std::string result(descriptor + 1, length - 2);
427 return result;
428 }
429 return descriptor;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700430}
431
jeffhao10037c82012-01-23 15:06:23 -0800432// Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700433uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700434 0x00000000, // 00..1f low control characters; nothing valid
435 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
436 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
437 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z'
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700438};
439
jeffhao10037c82012-01-23 15:06:23 -0800440// Helper for IsValidPartOfMemberNameUtf8(); do not call directly.
441bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700442 /*
443 * It's a multibyte encoded character. Decode it and analyze. We
444 * accept anything that isn't (a) an improperly encoded low value,
445 * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
446 * control character, or (e) a high space, layout, or special
447 * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
448 * U+fff0..U+ffff). This is all specified in the dex format
449 * document.
450 */
451
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000452 const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000453 const uint16_t leading = GetLeadingUtf16Char(pair);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000454
Narayan Kamath8508e372015-05-06 14:55:43 +0100455 // We have a surrogate pair resulting from a valid 4 byte UTF sequence.
456 // No further checks are necessary because 4 byte sequences span code
457 // points [U+10000, U+1FFFFF], which are valid codepoints in a dex
458 // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of
459 // the surrogate halves are valid and well formed in this instance.
460 if (GetTrailingUtf16Char(pair) != 0) {
461 return true;
462 }
463
464
465 // We've encountered a one, two or three byte UTF-8 sequence. The
466 // three byte UTF-8 sequence could be one half of a surrogate pair.
467 switch (leading >> 8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000468 case 0x00:
469 // It's only valid if it's above the ISO-8859-1 high space (0xa0).
470 return (leading > 0x00a0);
471 case 0xd8:
472 case 0xd9:
473 case 0xda:
474 case 0xdb:
Narayan Kamath8508e372015-05-06 14:55:43 +0100475 {
476 // We found a three byte sequence encoding one half of a surrogate.
477 // Look for the other half.
478 const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr);
479 const uint16_t trailing = GetLeadingUtf16Char(pair2);
480
481 return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff);
482 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000483 case 0xdc:
484 case 0xdd:
485 case 0xde:
486 case 0xdf:
487 // It's a trailing surrogate, which is not valid at this point.
488 return false;
489 case 0x20:
490 case 0xff:
491 // It's in the range that has spaces, controls, and specials.
492 switch (leading & 0xfff8) {
Narayan Kamath8508e372015-05-06 14:55:43 +0100493 case 0x2000:
494 case 0x2008:
495 case 0x2028:
496 case 0xfff0:
497 case 0xfff8:
498 return false;
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000499 }
Narayan Kamath8508e372015-05-06 14:55:43 +0100500 return true;
501 default:
502 return true;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700503 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000504
Narayan Kamath8508e372015-05-06 14:55:43 +0100505 UNREACHABLE();
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700506}
507
508/* Return whether the pointed-at modified-UTF-8 encoded character is
509 * valid as part of a member name, updating the pointer to point past
510 * the consumed character. This will consume two encoded UTF-16 code
511 * points if the character is encoded as a surrogate pair. Also, if
512 * this function returns false, then the given pointer may only have
513 * been partially advanced.
514 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700515static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700516 uint8_t c = (uint8_t) **pUtf8Ptr;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700517 if (LIKELY(c <= 0x7f)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700518 // It's low-ascii, so check the table.
519 uint32_t wordIdx = c >> 5;
520 uint32_t bitIdx = c & 0x1f;
521 (*pUtf8Ptr)++;
522 return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
523 }
524
525 // It's a multibyte encoded character. Call a non-inline function
526 // for the heavy lifting.
jeffhao10037c82012-01-23 15:06:23 -0800527 return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr);
528}
529
530bool IsValidMemberName(const char* s) {
531 bool angle_name = false;
532
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700533 switch (*s) {
jeffhao10037c82012-01-23 15:06:23 -0800534 case '\0':
535 // The empty string is not a valid name.
536 return false;
537 case '<':
538 angle_name = true;
539 s++;
540 break;
541 }
542
543 while (true) {
544 switch (*s) {
545 case '\0':
546 return !angle_name;
547 case '>':
548 return angle_name && s[1] == '\0';
549 }
550
551 if (!IsValidPartOfMemberNameUtf8(&s)) {
552 return false;
553 }
554 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700555}
556
Elliott Hughes906e6852011-10-28 14:52:10 -0700557enum ClassNameType { kName, kDescriptor };
Ian Rogers7b078e82014-09-10 14:44:24 -0700558template<ClassNameType kType, char kSeparator>
559static bool IsValidClassName(const char* s) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700560 int arrayCount = 0;
561 while (*s == '[') {
562 arrayCount++;
563 s++;
564 }
565
566 if (arrayCount > 255) {
567 // Arrays may have no more than 255 dimensions.
568 return false;
569 }
570
Ian Rogers7b078e82014-09-10 14:44:24 -0700571 ClassNameType type = kType;
572 if (type != kDescriptor && arrayCount != 0) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700573 /*
574 * If we're looking at an array of some sort, then it doesn't
575 * matter if what is being asked for is a class name; the
576 * format looks the same as a type descriptor in that case, so
577 * treat it as such.
578 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700579 type = kDescriptor;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700580 }
581
Elliott Hughes906e6852011-10-28 14:52:10 -0700582 if (type == kDescriptor) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700583 /*
584 * We are looking for a descriptor. Either validate it as a
585 * single-character primitive type, or continue on to check the
586 * embedded class name (bracketed by "L" and ";").
587 */
588 switch (*(s++)) {
589 case 'B':
590 case 'C':
591 case 'D':
592 case 'F':
593 case 'I':
594 case 'J':
595 case 'S':
596 case 'Z':
597 // These are all single-character descriptors for primitive types.
598 return (*s == '\0');
599 case 'V':
600 // Non-array void is valid, but you can't have an array of void.
601 return (arrayCount == 0) && (*s == '\0');
602 case 'L':
603 // Class name: Break out and continue below.
604 break;
605 default:
606 // Oddball descriptor character.
607 return false;
608 }
609 }
610
611 /*
612 * We just consumed the 'L' that introduces a class name as part
613 * of a type descriptor, or we are looking for an unadorned class
614 * name.
615 */
616
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700617 bool sepOrFirst = true; // first character or just encountered a separator.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700618 for (;;) {
619 uint8_t c = (uint8_t) *s;
620 switch (c) {
621 case '\0':
622 /*
623 * Premature end for a type descriptor, but valid for
624 * a class name as long as we haven't encountered an
625 * empty component (including the degenerate case of
626 * the empty string "").
627 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700628 return (type == kName) && !sepOrFirst;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700629 case ';':
630 /*
631 * Invalid character for a class name, but the
632 * legitimate end of a type descriptor. In the latter
633 * case, make sure that this is the end of the string
634 * and that it doesn't end with an empty component
635 * (including the degenerate case of "L;").
636 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700637 return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700638 case '/':
639 case '.':
Ian Rogers7b078e82014-09-10 14:44:24 -0700640 if (c != kSeparator) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700641 // The wrong separator character.
642 return false;
643 }
644 if (sepOrFirst) {
645 // Separator at start or two separators in a row.
646 return false;
647 }
648 sepOrFirst = true;
649 s++;
650 break;
651 default:
jeffhao10037c82012-01-23 15:06:23 -0800652 if (!IsValidPartOfMemberNameUtf8(&s)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700653 return false;
654 }
655 sepOrFirst = false;
656 break;
657 }
658 }
659}
660
Elliott Hughes906e6852011-10-28 14:52:10 -0700661bool IsValidBinaryClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700662 return IsValidClassName<kName, '.'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700663}
664
665bool IsValidJniClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700666 return IsValidClassName<kName, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700667}
668
669bool IsValidDescriptor(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700670 return IsValidClassName<kDescriptor, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700671}
672
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700673void Split(const std::string& s, char separator, std::vector<std::string>* result) {
Elliott Hughes34023802011-08-30 12:06:17 -0700674 const char* p = s.data();
675 const char* end = p + s.size();
676 while (p != end) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800677 if (*p == separator) {
Elliott Hughes34023802011-08-30 12:06:17 -0700678 ++p;
679 } else {
680 const char* start = p;
Elliott Hughes48436bb2012-02-07 15:23:28 -0800681 while (++p != end && *p != separator) {
682 // Skip to the next occurrence of the separator.
Elliott Hughes34023802011-08-30 12:06:17 -0700683 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700684 result->push_back(std::string(start, p - start));
Elliott Hughes34023802011-08-30 12:06:17 -0700685 }
686 }
687}
688
Elliott Hughes22869a92012-03-27 14:08:24 -0700689void SetThreadName(const char* thread_name) {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700690 int hasAt = 0;
691 int hasDot = 0;
Elliott Hughes22869a92012-03-27 14:08:24 -0700692 const char* s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700693 while (*s) {
694 if (*s == '.') {
695 hasDot = 1;
696 } else if (*s == '@') {
697 hasAt = 1;
698 }
699 s++;
700 }
Elliott Hughes22869a92012-03-27 14:08:24 -0700701 int len = s - thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700702 if (len < 15 || hasAt || !hasDot) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700703 s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700704 } else {
Elliott Hughes22869a92012-03-27 14:08:24 -0700705 s = thread_name + len - 15;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700706 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800707#if defined(__linux__)
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700708 // pthread_setname_np fails rather than truncating long strings.
Elliott Hughes0a18df82015-01-09 15:16:16 -0800709 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700710 strncpy(buf, s, sizeof(buf)-1);
711 buf[sizeof(buf)-1] = '\0';
712 errno = pthread_setname_np(pthread_self(), buf);
713 if (errno != 0) {
714 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
715 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800716#else // __APPLE__
Elliott Hughes22869a92012-03-27 14:08:24 -0700717 pthread_setname_np(thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700718#endif
719}
720
Brian Carlstrom29212012013-09-12 22:18:30 -0700721void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
722 *utime = *stime = *task_cpu = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700723 std::string stats;
Elliott Hughes8a31b502012-04-30 19:36:11 -0700724 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700725 return;
726 }
727 // Skip the command, which may contain spaces.
728 stats = stats.substr(stats.find(')') + 2);
729 // Extract the three fields we care about.
730 std::vector<std::string> fields;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700731 Split(stats, ' ', &fields);
Brian Carlstrom29212012013-09-12 22:18:30 -0700732 *state = fields[0][0];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700733 *utime = strtoull(fields[11].c_str(), nullptr, 10);
734 *stime = strtoull(fields[12].c_str(), nullptr, 10);
735 *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700736}
737
Calin Juravle36eb3132017-01-13 16:32:38 -0800738static const char* GetAndroidDirSafe(const char* env_var,
739 const char* default_dir,
740 std::string* error_msg) {
741 const char* android_dir = getenv(env_var);
742 if (android_dir == nullptr) {
743 if (OS::DirectoryExists(default_dir)) {
744 android_dir = default_dir;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700745 } else {
Calin Juravle36eb3132017-01-13 16:32:38 -0800746 *error_msg = StringPrintf("%s not set and %s does not exist", env_var, default_dir);
747 return nullptr;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700748 }
749 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800750 if (!OS::DirectoryExists(android_dir)) {
751 *error_msg = StringPrintf("Failed to find %s directory %s", env_var, android_dir);
752 return nullptr;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700753 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800754 return android_dir;
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800755}
Brian Carlstroma9f19782011-10-13 00:14:47 -0700756
Calin Juravle36eb3132017-01-13 16:32:38 -0800757const char* GetAndroidDir(const char* env_var, const char* default_dir) {
Alex Lighta59dd802014-07-02 16:28:08 -0700758 std::string error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800759 const char* dir = GetAndroidDirSafe(env_var, default_dir, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700760 if (dir != nullptr) {
761 return dir;
762 } else {
763 LOG(FATAL) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800764 return nullptr;
Alex Lighta59dd802014-07-02 16:28:08 -0700765 }
766}
767
Calin Juravle36eb3132017-01-13 16:32:38 -0800768const char* GetAndroidRoot() {
769 return GetAndroidDir("ANDROID_ROOT", "/system");
770}
771
772const char* GetAndroidRootSafe(std::string* error_msg) {
773 return GetAndroidDirSafe("ANDROID_ROOT", "/system", error_msg);
774}
775
776const char* GetAndroidData() {
777 return GetAndroidDir("ANDROID_DATA", "/data");
778}
779
Alex Lighta59dd802014-07-02 16:28:08 -0700780const char* GetAndroidDataSafe(std::string* error_msg) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800781 return GetAndroidDirSafe("ANDROID_DATA", "/data", error_msg);
782}
783
784std::string GetDefaultBootImageLocation(std::string* error_msg) {
785 const char* android_root = GetAndroidRootSafe(error_msg);
786 if (android_root == nullptr) {
787 return "";
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800788 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800789 return StringPrintf("%s/framework/boot.art", android_root);
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800790}
791
Alex Lighta59dd802014-07-02 16:28:08 -0700792void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string* dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700793 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700794 CHECK(subdir != nullptr);
795 std::string error_msg;
796 const char* android_data = GetAndroidDataSafe(&error_msg);
797 if (android_data == nullptr) {
798 *have_android_data = false;
799 *dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700800 *is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700801 return;
802 } else {
803 *have_android_data = true;
804 }
805 const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
806 *dalvik_cache = dalvik_cache_root + subdir;
807 *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str());
Andreas Gampe3c13a792014-09-18 20:56:04 -0700808 *is_global_cache = strcmp(android_data, "/data") == 0;
809 if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700810 // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
811 *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) &&
812 (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST));
813 }
814}
815
Richard Uhler55b58b62016-08-12 09:05:13 -0700816std::string GetDalvikCache(const char* subdir) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100817 CHECK(subdir != nullptr);
Brian Carlstrom41ccffd2014-05-06 10:37:30 -0700818 const char* android_data = GetAndroidData();
819 const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100820 const std::string dalvik_cache = dalvik_cache_root + subdir;
Andreas Gampe40da2862015-02-27 12:49:04 -0800821 if (!OS::DirectoryExists(dalvik_cache.c_str())) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700822 // TODO: Check callers. Traditional behavior is to not abort.
823 return "";
Brian Carlstroma9f19782011-10-13 00:14:47 -0700824 }
Brian Carlstrom7675e162013-06-10 16:18:04 -0700825 return dalvik_cache;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700826}
827
Alex Lighta59dd802014-07-02 16:28:08 -0700828bool GetDalvikCacheFilename(const char* location, const char* cache_location,
829 std::string* filename, std::string* error_msg) {
Ian Rogerse6060102013-05-16 12:01:04 -0700830 if (location[0] != '/') {
Alex Lighta59dd802014-07-02 16:28:08 -0700831 *error_msg = StringPrintf("Expected path in location to be absolute: %s", location);
832 return false;
Ian Rogerse6060102013-05-16 12:01:04 -0700833 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700834 std::string cache_file(&location[1]); // skip leading slash
Andreas Gampe9186ced2016-12-12 14:28:21 -0800835 if (!android::base::EndsWith(location, ".dex") &&
836 !android::base::EndsWith(location, ".art") &&
837 !android::base::EndsWith(location, ".oat")) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700838 cache_file += "/";
839 cache_file += DexFile::kClassesDex;
840 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700841 std::replace(cache_file.begin(), cache_file.end(), '/', '@');
Alex Lighta59dd802014-07-02 16:28:08 -0700842 *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str());
843 return true;
844}
845
Calin Juravle367b9d82017-05-15 18:18:39 -0700846std::string GetVdexFilename(const std::string& oat_location) {
847 return ReplaceFileExtension(oat_location, "vdex");
848}
849
Brian Carlstrom2afe4942014-05-19 10:25:33 -0700850static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) {
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700851 // in = /foo/bar/baz
852 // out = /foo/bar/<isa>/baz
853 size_t pos = filename->rfind('/');
854 CHECK_NE(pos, std::string::npos) << *filename << " " << isa;
855 filename->insert(pos, "/", 1);
856 filename->insert(pos + 1, GetInstructionSetString(isa));
857}
858
859std::string GetSystemImageFilename(const char* location, const InstructionSet isa) {
860 // location = /system/framework/boot.art
861 // filename = /system/framework/<isa>/boot.art
862 std::string filename(location);
Brian Carlstrom2afe4942014-05-19 10:25:33 -0700863 InsertIsaDirectory(isa, &filename);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700864 return filename;
865}
866
Calin Juravle5e2b9712015-12-18 14:10:00 +0200867bool FileExists(const std::string& filename) {
868 struct stat buffer;
869 return stat(filename.c_str(), &buffer) == 0;
870}
871
Calin Juravleb9c1b9b2016-03-17 17:07:52 +0000872bool FileExistsAndNotEmpty(const std::string& filename) {
873 struct stat buffer;
874 if (stat(filename.c_str(), &buffer) != 0) {
875 return false;
876 }
877 return buffer.st_size > 0;
878}
879
David Brazdil7b49e6c2016-09-01 11:06:18 +0100880std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension) {
881 const size_t last_ext = filename.find_last_of('.');
882 if (last_ext == std::string::npos) {
883 return filename + "." + new_extension;
884 } else {
885 return filename.substr(0, last_ext + 1) + new_extension;
886 }
887}
888
Mathieu Chartier76433272014-09-26 14:32:37 -0700889std::string PrettyDescriptor(Primitive::Type type) {
890 return PrettyDescriptor(Primitive::Descriptor(type));
891}
892
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000893static void ParseStringAfterChar(const std::string& s,
894 char c,
895 std::string* parsed_value,
896 UsageFn Usage) {
897 std::string::size_type colon = s.find(c);
898 if (colon == std::string::npos) {
899 Usage("Missing char %c in option %s\n", c, s.c_str());
900 }
901 // Add one to remove the char we were trimming until.
902 *parsed_value = s.substr(colon + 1);
903}
904
905void ParseDouble(const std::string& option,
906 char after_char,
907 double min,
908 double max,
909 double* parsed_value,
910 UsageFn Usage) {
911 std::string substring;
912 ParseStringAfterChar(option, after_char, &substring, Usage);
913 bool sane_val = true;
914 double value;
915 if ((false)) {
916 // TODO: this doesn't seem to work on the emulator. b/15114595
917 std::stringstream iss(substring);
918 iss >> value;
919 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
920 sane_val = iss.eof() && (value >= min) && (value <= max);
921 } else {
922 char* end = nullptr;
923 value = strtod(substring.c_str(), &end);
924 sane_val = *end == '\0' && value >= min && value <= max;
925 }
926 if (!sane_val) {
927 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
928 }
929 *parsed_value = value;
930}
931
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000932int64_t GetFileSizeBytes(const std::string& filename) {
933 struct stat stat_buf;
934 int rc = stat(filename.c_str(), &stat_buf);
935 return rc == 0 ? stat_buf.st_size : -1;
936}
937
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800938void SleepForever() {
939 while (true) {
940 usleep(1000000);
941 }
942}
943
Mathieu Chartier120aa282017-08-05 16:03:03 -0700944int MadviseLargestPageAlignedRegion(const uint8_t* begin, const uint8_t* end, int advice) {
945 DCHECK_LE(begin, end);
946 begin = AlignUp(begin, kPageSize);
947 end = AlignDown(end, kPageSize);
948 if (begin < end) {
949 int result = madvise(const_cast<uint8_t*>(begin), end - begin, advice);
950 if (result != 0) {
951 PLOG(WARNING) << "madvise failed " << result;
952 }
953 return result;
954 }
955 return 0;
956}
957
Elliott Hughes42ee1422011-09-06 12:33:32 -0700958} // namespace art