blob: 7402c12280f0b89249397d9440998753acae5ec5 [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 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_UTILS_H_
18#define ART_RUNTIME_UTILS_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
Elliott Hughes92b3b562011-09-08 16:32:26 -070020#include <pthread.h>
Alex Light15324762015-11-19 11:03:10 -080021#include <stdlib.h>
Elliott Hughese222ee02012-12-13 14:41:43 -080022
Alex Light15324762015-11-19 11:03:10 -080023#include <random>
Elliott Hughes34023802011-08-30 12:06:17 -070024#include <string>
Elliott Hughes34023802011-08-30 12:06:17 -070025
Andreas Gampe57943812017-12-06 21:39:13 -080026#include <android-base/logging.h>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set.h"
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000029#include "base/casts.h"
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +000030#include "base/stringpiece.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080031#include "globals.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070032#include "primitive.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010033
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070034namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035
Alex Light53cb16b2014-06-12 11:26:29 -070036template <typename T>
37bool ParseUint(const char *in, T* out) {
38 char* end;
39 unsigned long long int result = strtoull(in, &end, 0); // NOLINT(runtime/int)
40 if (in == end || *end != '\0') {
41 return false;
42 }
43 if (std::numeric_limits<T>::max() < result) {
44 return false;
45 }
46 *out = static_cast<T>(result);
47 return true;
48}
49
50template <typename T>
51bool ParseInt(const char* in, T* out) {
52 char* end;
53 long long int result = strtoll(in, &end, 0); // NOLINT(runtime/int)
54 if (in == end || *end != '\0') {
55 return false;
56 }
57 if (result < std::numeric_limits<T>::min() || std::numeric_limits<T>::max() < result) {
58 return false;
59 }
60 *out = static_cast<T>(result);
61 return true;
62}
63
Ian Rogersef7d42f2014-01-06 12:55:46 -080064static inline uint32_t PointerToLowMemUInt32(const void* p) {
65 uintptr_t intp = reinterpret_cast<uintptr_t>(p);
66 DCHECK_LE(intp, 0xFFFFFFFFU);
67 return intp & 0xFFFFFFFFU;
68}
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069
Ian Rogers576ca0c2014-06-06 15:58:22 -070070std::string PrintableChar(uint16_t ch);
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070071
Elliott Hughes82914b62012-04-09 15:56:29 -070072// Returns an ASCII string corresponding to the given UTF-8 string.
73// Java escapes are used for non-ASCII characters.
Ian Rogers68b56852014-08-29 20:19:11 -070074std::string PrintableString(const char* utf8);
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070075
Elliott Hughes54e7df12011-09-16 11:47:04 -070076// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
77// one of which is probably more useful to you.
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070078// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
79// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
80// "java.lang.String[]", and so forth.
Vladimir Markob8a55f82017-09-21 16:21:43 +010081void AppendPrettyDescriptor(const char* descriptor, std::string* result);
Ian Rogers1ff3c982014-08-12 02:30:58 -070082std::string PrettyDescriptor(const char* descriptor);
Ian Rogers68d8b422014-07-17 11:09:10 -070083std::string PrettyDescriptor(Primitive::Type type);
Elliott Hughes11e45072011-08-16 17:40:46 -070084
Andreas Gampec0d82292014-09-23 10:38:30 -070085// Returns a human-readable version of the Java part of the access flags, e.g., "private static "
86// (note the trailing whitespace).
87std::string PrettyJavaAccessFlags(uint32_t access_flags);
88
Elliott Hughesc967f782012-04-16 10:23:15 -070089// Returns a human-readable size string such as "1MB".
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080090std::string PrettySize(int64_t size_in_bytes);
Ian Rogers3bb17a62012-01-27 23:56:44 -080091
Elliott Hughes79082e32011-08-25 12:07:32 -070092// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
93// of the JNI spec.
94std::string MangleForJni(const std::string& s);
95
Alex Light888a59e2017-01-25 11:41:41 -080096std::string GetJniShortName(const std::string& class_name, const std::string& method_name);
97
Brian Carlstromf91c8c32011-09-21 17:30:34 -070098// Turn "java.lang.String" into "Ljava/lang/String;".
99std::string DotToDescriptor(const char* class_name);
100
Ian Rogers1ff3c982014-08-12 02:30:58 -0700101// Turn "Ljava/lang/String;" into "java.lang.String" using the conventions of
102// java.lang.Class.getName().
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800103std::string DescriptorToDot(const char* descriptor);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104
Ian Rogers1ff3c982014-08-12 02:30:58 -0700105// Turn "Ljava/lang/String;" into "java/lang/String" using the opposite conventions of
106// java.lang.Class.getName().
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800107std::string DescriptorToName(const char* descriptor);
108
Elliott Hughes906e6852011-10-28 14:52:10 -0700109// Tests for whether 's' is a valid class name in the three common forms:
110bool IsValidBinaryClassName(const char* s); // "java.lang.String"
111bool IsValidJniClassName(const char* s); // "java/lang/String"
112bool IsValidDescriptor(const char* s); // "Ljava/lang/String;"
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700113
jeffhao10037c82012-01-23 15:06:23 -0800114// Returns whether the given string is a valid field or method name,
115// additionally allowing names that begin with '<' and end with '>'.
116bool IsValidMemberName(const char* s);
117
Elliott Hughes48436bb2012-02-07 15:23:28 -0800118// Splits a string using the given separator character into a vector of
Elliott Hughes34023802011-08-30 12:06:17 -0700119// strings. Empty strings will be omitted.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700120void Split(const std::string& s, char separator, std::vector<std::string>* result);
Elliott Hughes48436bb2012-02-07 15:23:28 -0800121
Elliott Hughes42ee1422011-09-06 12:33:32 -0700122// Returns the calling thread's tid. (The C libraries don't expose this.)
123pid_t GetTid();
124
Elliott Hughes289be852012-06-12 13:57:20 -0700125// Returns the given thread's name.
126std::string GetThreadName(pid_t tid);
127
Elliott Hughesdcc24742011-09-07 14:02:44 -0700128// Sets the name of the current thread. The name may be truncated to an
129// implementation-defined limit.
Elliott Hughes22869a92012-03-27 14:08:24 -0700130void SetThreadName(const char* thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700131
David Sehr891a50e2017-10-27 17:01:07 -0700132// Reads data from "/proc/self/task/${tid}/stat".
133void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100134
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800135class VoidFunctor {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700136 public:
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800137 template <typename A>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100138 inline void operator() (A a ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800139 }
140
141 template <typename A, typename B>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100142 inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800143 }
144
145 template <typename A, typename B, typename C>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100146 inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED, C c ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800147 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700148};
149
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700150inline bool TestBitmap(size_t idx, const uint8_t* bitmap) {
151 return ((bitmap[idx / kBitsPerByte] >> (idx % kBitsPerByte)) & 0x01) != 0;
152}
153
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154static inline constexpr bool ValidPointerSize(size_t pointer_size) {
155 return pointer_size == 4 || pointer_size == 8;
156}
Mathieu Chartier50030ef2015-05-08 14:19:26 -0700157
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100158static inline const void* EntryPointToCodePointer(const void* entry_point) {
159 uintptr_t code = reinterpret_cast<uintptr_t>(entry_point);
160 // TODO: Make this Thumb2 specific. It is benign on other architectures as code is always at
161 // least 2 byte aligned.
162 code &= ~0x1;
163 return reinterpret_cast<const void*>(code);
164}
165
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000166using UsageFn = void (*)(const char*, ...);
167
168template <typename T>
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000169static void ParseIntOption(const StringPiece& option,
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000170 const std::string& option_name,
171 T* out,
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000172 UsageFn usage,
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000173 bool is_long_option = true) {
174 std::string option_prefix = option_name + (is_long_option ? "=" : "");
Calin Juravle2e2db782016-02-23 12:00:03 +0000175 DCHECK(option.starts_with(option_prefix)) << option << " " << option_prefix;
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000176 const char* value_string = option.substr(option_prefix.size()).data();
Nicolas Geoffrayfca90a12015-10-30 12:05:41 +0000177 int64_t parsed_integer_value = 0;
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000178 if (!ParseInt(value_string, &parsed_integer_value)) {
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000179 usage("Failed to parse %s '%s' as an integer", option_name.c_str(), value_string);
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000180 }
181 *out = dchecked_integral_cast<T>(parsed_integer_value);
182}
183
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000184template <typename T>
185static void ParseUintOption(const StringPiece& option,
186 const std::string& option_name,
187 T* out,
188 UsageFn usage,
189 bool is_long_option = true) {
190 ParseIntOption(option, option_name, out, usage, is_long_option);
191 if (*out < 0) {
192 usage("%s passed a negative value %d", option_name.c_str(), *out);
193 *out = 0;
194 }
195}
196
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000197void ParseDouble(const std::string& option,
198 char after_char,
199 double min,
200 double max,
201 double* parsed_value,
202 UsageFn Usage);
203
Alex Light15324762015-11-19 11:03:10 -0800204#if defined(__BIONIC__)
205struct Arc4RandomGenerator {
206 typedef uint32_t result_type;
207 static constexpr uint32_t min() { return std::numeric_limits<uint32_t>::min(); }
208 static constexpr uint32_t max() { return std::numeric_limits<uint32_t>::max(); }
209 uint32_t operator() () { return arc4random(); }
210};
211using RNG = Arc4RandomGenerator;
212#else
213using RNG = std::random_device;
214#endif
215
216template <typename T>
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700217static T GetRandomNumber(T min, T max) {
Alex Light15324762015-11-19 11:03:10 -0800218 CHECK_LT(min, max);
219 std::uniform_int_distribution<T> dist(min, max);
220 RNG rng;
221 return dist(rng);
222}
223
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800224// Sleep forever and never come back.
225NO_RETURN void SleepForever();
226
Roland Levillain32430262016-02-01 15:23:20 +0000227inline void FlushInstructionCache(char* begin, char* end) {
Roland Levillain32430262016-02-01 15:23:20 +0000228 __builtin___clear_cache(begin, end);
Roland Levillain32430262016-02-01 15:23:20 +0000229}
230
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +0000231inline void FlushDataCache(char* begin, char* end) {
232 // Same as FlushInstructionCache for lack of other builtin. __builtin___clear_cache
233 // flushes both caches.
234 __builtin___clear_cache(begin, end);
235}
236
Andreas Gampebda1d602016-08-29 17:43:45 -0700237template <typename T>
238constexpr PointerSize ConvertToPointerSize(T any) {
239 if (any == 4 || any == 8) {
240 return static_cast<PointerSize>(any);
241 } else {
242 LOG(FATAL);
243 UNREACHABLE();
244 }
245}
246
Orion Hodsonc069a302017-01-18 09:23:12 +0000247// Returns a type cast pointer if object pointed to is within the provided bounds.
248// Otherwise returns nullptr.
249template <typename T>
250inline static T BoundsCheckedCast(const void* pointer,
251 const void* lower,
252 const void* upper) {
253 const uint8_t* bound_begin = static_cast<const uint8_t*>(lower);
254 const uint8_t* bound_end = static_cast<const uint8_t*>(upper);
255 DCHECK(bound_begin <= bound_end);
256
257 T result = reinterpret_cast<T>(pointer);
258 const uint8_t* begin = static_cast<const uint8_t*>(pointer);
259 const uint8_t* end = begin + sizeof(*result);
260 if (begin < bound_begin || end > bound_end || begin > end) {
261 return nullptr;
262 }
263 return result;
264}
265
266template <typename T, size_t size>
267constexpr size_t ArrayCount(const T (&)[size]) {
268 return size;
269}
270
buzbee31afbec2017-03-14 15:30:19 -0700271// Return -1 if <, 0 if ==, 1 if >.
272template <typename T>
273inline static int32_t Compare(T lhs, T rhs) {
274 return (lhs < rhs) ? -1 : ((lhs == rhs) ? 0 : 1);
275}
276
277// Return -1 if < 0, 0 if == 0, 1 if > 0.
278template <typename T>
279inline static int32_t Signum(T opnd) {
280 return (opnd < 0) ? -1 : ((opnd == 0) ? 0 : 1);
281}
282
Mathieu Chartier3425d022017-10-03 16:22:05 -0700283template <typename Func, typename... Args>
284static inline void CheckedCall(const Func& function, const char* what, Args... args) {
285 int rc = function(args...);
286 if (UNLIKELY(rc != 0)) {
287 errno = rc;
288 PLOG(FATAL) << "Checked call failed for " << what;
289 }
290}
291
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800292// Hash bytes using a relatively fast hash.
293static inline size_t HashBytes(const uint8_t* data, size_t len) {
294 size_t hash = 0x811c9dc5;
295 for (uint32_t i = 0; i < len; ++i) {
296 hash = (hash * 16777619) ^ data[i];
297 }
298 hash += hash << 13;
299 hash ^= hash >> 7;
300 hash += hash << 3;
301 hash ^= hash >> 17;
302 hash += hash << 5;
303 return hash;
304}
305
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700306} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700307
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700308#endif // ART_RUNTIME_UTILS_H_