blob: 8c2a49c5f62cde391f230cf746bd16359c276543 [file] [log] [blame]
Ian Rogersb0fa5dc2014-04-28 16:47:08 -07001/*
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 */
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070016#ifndef ART_RUNTIME_MIRROR_STRING_INL_H_
17#define ART_RUNTIME_MIRROR_STRING_INL_H_
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "string.h"
20
21#include "android-base/stringprintf.h"
22
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "array.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010024#include "base/bit_utils.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "class.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010026#include "common_throws.h"
David Sehr0225f8e2018-01-31 08:52:24 +000027#include "dex/utf.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080028#include "gc/heap-inl.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010029#include "globals.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070030#include "runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070031#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010032#include "utils.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070033
34namespace art {
35namespace mirror {
36
Andreas Gampe542451c2016-07-26 09:02:02 -070037inline uint32_t String::ClassSize(PointerSize pointer_size) {
Alan Leung8f514ee2017-12-08 14:08:25 -080038#ifdef USE_D8_DESUGAR
39 // Two lambdas in CharSequence:
40 // lambda$chars$0$CharSequence
41 // lambda$codePoints$1$CharSequence
42 // which were virtual functions in standalone desugar, becomes
43 // direct functions with D8 desugaring.
44 uint32_t vtable_entries = Object::kVTableLength + 54;
45#else
Vladimir Marko92907f32017-02-20 14:08:30 +000046 uint32_t vtable_entries = Object::kVTableLength + 56;
Alan Leung8f514ee2017-12-08 14:08:25 -080047#endif
Narayan Kamath5d8fa8b2016-04-13 14:17:44 +010048 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070049}
50
Jeff Hao848f70a2014-01-15 13:49:50 -080051// Sets string count in the allocation code path to ensure it is guarded by a CAS.
52class SetStringCountVisitor {
53 public:
54 explicit SetStringCountVisitor(int32_t count) : count_(count) {
55 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000056
Mathieu Chartier9d156d52016-10-06 17:44:26 -070057 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080059 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070060 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -080061 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -070062 DCHECK(!string->IsCompressed() || kUseStringCompression);
Jeff Hao848f70a2014-01-15 13:49:50 -080063 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070064
Jeff Hao848f70a2014-01-15 13:49:50 -080065 private:
66 const int32_t count_;
67};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070068
Jeff Hao848f70a2014-01-15 13:49:50 -080069// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
70class SetStringCountAndBytesVisitor {
71 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070072 SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
73 int32_t high_byte)
74 : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
Jeff Hao848f70a2014-01-15 13:49:50 -080075 }
76
Mathieu Chartier9d156d52016-10-06 17:44:26 -070077 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080079 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070080 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -080081 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -070082 DCHECK(!string->IsCompressed() || kUseStringCompression);
83 int32_t length = String::GetLengthFromCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -070084 const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
jessicahandojo3aaa37b2016-07-29 14:46:37 -070085 if (string->IsCompressed()) {
86 uint8_t* valueCompressed = string->GetValueCompressed();
87 for (int i = 0; i < length; i++) {
88 valueCompressed[i] = (src[i] & 0xFF);
89 }
90 } else {
91 uint16_t* value = string->GetValue();
92 for (int i = 0; i < length; i++) {
93 value[i] = high_byte_ + (src[i] & 0xFF);
94 }
Jeff Hao848f70a2014-01-15 13:49:50 -080095 }
96 }
97
98 private:
99 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700100 Handle<ByteArray> src_array_;
101 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -0800102 const int32_t high_byte_;
103};
104
105// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700106class SetStringCountAndValueVisitorFromCharArray {
Jeff Hao848f70a2014-01-15 13:49:50 -0800107 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700108 SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
109 int32_t offset) :
110 count_(count), src_array_(src_array), offset_(offset) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800111 }
112
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700113 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800115 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700116 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -0800117 string->SetCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700118 const uint16_t* const src = src_array_->GetData() + offset_;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700119 const int32_t length = String::GetLengthFromCount(count_);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100120 if (kUseStringCompression && String::IsCompressed(count_)) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700121 for (int i = 0; i < length; ++i) {
122 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
123 }
124 } else {
125 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
126 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800127 }
128
129 private:
130 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700131 Handle<CharArray> src_array_;
132 const int32_t offset_;
133};
134
135// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
136class SetStringCountAndValueVisitorFromString {
137 public:
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100138 SetStringCountAndValueVisitorFromString(int32_t count,
139 Handle<String> src_string,
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700140 int32_t offset) :
141 count_(count), src_string_(src_string), offset_(offset) {
142 }
143
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700144 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700146 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700147 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700148 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700149 const int32_t length = String::GetLengthFromCount(count_);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100150 bool compressible = kUseStringCompression && String::IsCompressed(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700151 if (src_string_->IsCompressed()) {
152 const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
153 memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
154 } else {
155 const uint16_t* const src = src_string_->GetValue() + offset_;
156 if (compressible) {
157 for (int i = 0; i < length; ++i) {
158 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
159 }
160 } else {
161 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
162 }
163 }
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700164 }
165
166 private:
167 const int32_t count_;
168 Handle<String> src_string_;
169 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -0800170};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700171
Jeff Hao848f70a2014-01-15 13:49:50 -0800172inline uint16_t String::CharAt(int32_t index) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700173 int32_t count = GetLength();
Jeff Hao848f70a2014-01-15 13:49:50 -0800174 if (UNLIKELY((index < 0) || (index >= count))) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 ThrowStringIndexOutOfBoundsException(index, count);
Jeff Hao848f70a2014-01-15 13:49:50 -0800176 return 0;
177 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700178 if (IsCompressed()) {
179 return GetValueCompressed()[index];
180 } else {
181 return GetValue()[index];
182 }
183}
184
185template <typename MemoryType>
186int32_t String::FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) {
187 const MemoryType* p = chars + start;
188 const MemoryType* end = chars + GetLength();
189 while (p < end) {
190 if (*p++ == ch) {
191 return (p - 1) - chars;
192 }
193 }
194 return -1;
Jeff Hao848f70a2014-01-15 13:49:50 -0800195}
196
197template<VerifyObjectFlags kVerifyFlags>
198inline size_t String::SizeOf() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700199 size_t size = sizeof(String);
200 if (IsCompressed()) {
201 size += (sizeof(uint8_t) * GetLength<kVerifyFlags>());
202 } else {
203 size += (sizeof(uint16_t) * GetLength<kVerifyFlags>());
204 }
Vladimir Marko4ef52262015-08-26 18:12:56 +0100205 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100206 // so make sure the zero-padding is actually copied around if GC compaction
207 // chooses to copy only SizeOf() bytes.
Vladimir Marko4ef52262015-08-26 18:12:56 +0100208 // http://b/23528461
209 return RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800210}
211
212template <bool kIsInstrumented, typename PreFenceVisitor>
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700213inline String* String::Alloc(Thread* self, int32_t utf16_length_with_flag,
214 gc::AllocatorType allocator_type,
Jeff Hao848f70a2014-01-15 13:49:50 -0800215 const PreFenceVisitor& pre_fence_visitor) {
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100216 constexpr size_t header_size = sizeof(String);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100217 const bool compressible = kUseStringCompression && String::IsCompressed(utf16_length_with_flag);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700218 const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
219 size_t length = String::GetLengthFromCount(utf16_length_with_flag);
220 static_assert(sizeof(length) <= sizeof(size_t),
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100221 "static_cast<size_t>(utf16_length) must not lose bits.");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700222 size_t data_size = block_size * length;
Jeff Hao848f70a2014-01-15 13:49:50 -0800223 size_t size = header_size + data_size;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100224 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
225 // so make sure the allocator clears the padding as well.
226 // http://b/23528461
227 size_t alloc_size = RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800228
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700229 Class* string_class = GetJavaLangString();
Jeff Hao848f70a2014-01-15 13:49:50 -0800230 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100231 // Do this by comparing with the maximum length that will _not_ cause an overflow.
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700232 const size_t overflow_length = (-header_size) / block_size; // Unsigned arithmetic.
233 const size_t max_alloc_length = overflow_length - 1u;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100234 static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
235 "kObjectAlignment must be at least as big as Java char alignment");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700236 const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100237 if (UNLIKELY(length > max_length)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800238 self->ThrowOutOfMemoryError(
239 android::base::StringPrintf("%s of length %d would overflow",
240 Class::PrettyDescriptor(string_class).c_str(),
241 static_cast<int>(length)).c_str());
Jeff Hao848f70a2014-01-15 13:49:50 -0800242 return nullptr;
243 }
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100244
Jeff Hao848f70a2014-01-15 13:49:50 -0800245 gc::Heap* heap = Runtime::Current()->GetHeap();
246 return down_cast<String*>(
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100247 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
Jeff Haob7c8c1a2015-06-22 14:29:54 -0700248 allocator_type, pre_fence_visitor));
Jeff Hao848f70a2014-01-15 13:49:50 -0800249}
250
251template <bool kIsInstrumented>
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700252inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100253 const int32_t length_with_flag = String::GetFlaggedCount(0, /* compressible */ true);
jessicahandojo7908c8e2016-09-09 19:05:34 -0700254 SetStringCountVisitor visitor(length_with_flag);
255 return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700256}
257
258template <bool kIsInstrumented>
Jeff Hao848f70a2014-01-15 13:49:50 -0800259inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
260 Handle<ByteArray> array, int32_t offset,
261 int32_t high_byte, gc::AllocatorType allocator_type) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700262 const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
Vladimir Markoa44c4452017-07-13 18:49:35 +0100263 high_byte &= 0xff; // Extract the relevant bits before determining `compressible`.
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100264 const bool compressible =
265 kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0);
266 const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700267 SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
268 String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800269 return string;
270}
271
272template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700273inline String* String::AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800274 Handle<CharArray> array, int32_t offset,
275 gc::AllocatorType allocator_type) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700276 // It is a caller error to have a count less than the actual array's size.
277 DCHECK_GE(array->GetLength(), count);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700278 const bool compressible = kUseStringCompression &&
279 String::AllASCII<uint16_t>(array->GetData() + offset, count);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100280 const int32_t length_with_flag = String::GetFlaggedCount(count, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700281 SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
282 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800283 return new_string;
284}
285
286template <bool kIsInstrumented>
287inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
288 int32_t offset, gc::AllocatorType allocator_type) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700289 const bool compressible = kUseStringCompression &&
290 ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
291 string_length));
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100292 const int32_t length_with_flag = String::GetFlaggedCount(string_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700293 SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
294 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800295 return new_string;
296}
297
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700298inline int32_t String::GetHashCode() {
299 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
300 if (UNLIKELY(result == 0)) {
301 result = ComputeHashCode();
302 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700303 if (kIsDebugBuild) {
304 if (IsCompressed()) {
305 DCHECK(result != 0 || ComputeUtf16Hash(GetValueCompressed(), GetLength()) == 0)
306 << ToModifiedUtf8() << " " << result;
307 } else {
308 DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
309 << ToModifiedUtf8() << " " << result;
310 }
311 }
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700312 return result;
313}
314
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700315template<typename MemoryType>
Vladimir Markoe39f14f2017-02-10 15:44:25 +0000316inline bool String::AllASCII(const MemoryType* chars, const int length) {
Vladimir Marko16850ae2016-12-09 14:01:02 +0000317 static_assert(std::is_unsigned<MemoryType>::value, "Expecting unsigned MemoryType");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700318 for (int i = 0; i < length; ++i) {
Vladimir Marko92907f32017-02-20 14:08:30 +0000319 if (!IsASCII(chars[i])) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700320 return false;
321 }
322 }
323 return true;
324}
325
Vladimir Markoe39f14f2017-02-10 15:44:25 +0000326inline bool String::DexFileStringAllASCII(const char* chars, const int length) {
327 // For strings from the dex file we just need to check that
328 // the terminating character is at the right position.
329 DCHECK_EQ(AllASCII(reinterpret_cast<const uint8_t*>(chars), length), chars[length] == 0);
330 return chars[length] == 0;
331}
332
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700333} // namespace mirror
334} // namespace art
335
336#endif // ART_RUNTIME_MIRROR_STRING_INL_H_