blob: c2407d7772019af1ed3cfed971b175e0035b1804 [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"
Jeff Hao848f70a2014-01-15 13:49:50 -080027#include "gc/heap-inl.h"
Vladimir Marko4ef52262015-08-26 18:12:56 +010028#include "globals.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "intern_table.h"
30#include "runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070031#include "thread.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070032#include "utf.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010033#include "utils.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070034
35namespace art {
36namespace mirror {
37
Andreas Gampe542451c2016-07-26 09:02:02 -070038inline uint32_t String::ClassSize(PointerSize pointer_size) {
Przemyslaw Szczepaniaka4fa2e72016-06-22 13:30:36 +010039 uint32_t vtable_entries = Object::kVTableLength + 57;
Narayan Kamath5d8fa8b2016-04-13 14:17:44 +010040 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041}
42
Jeff Hao848f70a2014-01-15 13:49:50 -080043// Sets string count in the allocation code path to ensure it is guarded by a CAS.
44class SetStringCountVisitor {
45 public:
46 explicit SetStringCountVisitor(int32_t count) : count_(count) {
47 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000048
Mathieu Chartier9d156d52016-10-06 17:44:26 -070049 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080051 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070052 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -080053 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -070054 DCHECK(!string->IsCompressed() || kUseStringCompression);
Jeff Hao848f70a2014-01-15 13:49:50 -080055 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070056
Jeff Hao848f70a2014-01-15 13:49:50 -080057 private:
58 const int32_t count_;
59};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070060
Jeff Hao848f70a2014-01-15 13:49:50 -080061// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
62class SetStringCountAndBytesVisitor {
63 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -070064 SetStringCountAndBytesVisitor(int32_t count, Handle<ByteArray> src_array, int32_t offset,
65 int32_t high_byte)
66 : count_(count), src_array_(src_array), offset_(offset), high_byte_(high_byte) {
Jeff Hao848f70a2014-01-15 13:49:50 -080067 }
68
Mathieu Chartier9d156d52016-10-06 17:44:26 -070069 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080071 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -070072 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -080073 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -070074 DCHECK(!string->IsCompressed() || kUseStringCompression);
75 int32_t length = String::GetLengthFromCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -070076 const uint8_t* const src = reinterpret_cast<uint8_t*>(src_array_->GetData()) + offset_;
jessicahandojo3aaa37b2016-07-29 14:46:37 -070077 if (string->IsCompressed()) {
78 uint8_t* valueCompressed = string->GetValueCompressed();
79 for (int i = 0; i < length; i++) {
80 valueCompressed[i] = (src[i] & 0xFF);
81 }
82 } else {
83 uint16_t* value = string->GetValue();
84 for (int i = 0; i < length; i++) {
85 value[i] = high_byte_ + (src[i] & 0xFF);
86 }
Jeff Hao848f70a2014-01-15 13:49:50 -080087 }
88 }
89
90 private:
91 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -070092 Handle<ByteArray> src_array_;
93 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -080094 const int32_t high_byte_;
95};
96
97// 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 -070098class SetStringCountAndValueVisitorFromCharArray {
Jeff Hao848f70a2014-01-15 13:49:50 -080099 public:
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700100 SetStringCountAndValueVisitorFromCharArray(int32_t count, Handle<CharArray> src_array,
101 int32_t offset) :
102 count_(count), src_array_(src_array), offset_(offset) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800103 }
104
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700105 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800107 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700108 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Jeff Hao848f70a2014-01-15 13:49:50 -0800109 string->SetCount(count_);
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700110 const uint16_t* const src = src_array_->GetData() + offset_;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700111 const int32_t length = String::GetLengthFromCount(count_);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100112 if (kUseStringCompression && String::IsCompressed(count_)) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700113 for (int i = 0; i < length; ++i) {
114 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
115 }
116 } else {
117 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
118 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800119 }
120
121 private:
122 const int32_t count_;
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700123 Handle<CharArray> src_array_;
124 const int32_t offset_;
125};
126
127// Sets string count and value in the allocation code path to ensure it is guarded by a CAS.
128class SetStringCountAndValueVisitorFromString {
129 public:
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100130 SetStringCountAndValueVisitorFromString(int32_t count,
131 Handle<String> src_string,
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700132 int32_t offset) :
133 count_(count), src_string_(src_string), offset_(offset) {
134 }
135
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700136 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700138 // Avoid AsString as object is not yet in live bitmap or allocation stack.
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700139 ObjPtr<String> string = ObjPtr<String>::DownCast(obj);
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700140 string->SetCount(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700141 const int32_t length = String::GetLengthFromCount(count_);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100142 bool compressible = kUseStringCompression && String::IsCompressed(count_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700143 if (src_string_->IsCompressed()) {
144 const uint8_t* const src = src_string_->GetValueCompressed() + offset_;
145 memcpy(string->GetValueCompressed(), src, length * sizeof(uint8_t));
146 } else {
147 const uint16_t* const src = src_string_->GetValue() + offset_;
148 if (compressible) {
149 for (int i = 0; i < length; ++i) {
150 string->GetValueCompressed()[i] = static_cast<uint8_t>(src[i]);
151 }
152 } else {
153 memcpy(string->GetValue(), src, length * sizeof(uint16_t));
154 }
155 }
Mathieu Chartier81aa0122015-04-28 10:01:28 -0700156 }
157
158 private:
159 const int32_t count_;
160 Handle<String> src_string_;
161 const int32_t offset_;
Jeff Hao848f70a2014-01-15 13:49:50 -0800162};
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700163
Mathieu Chartier9e868092016-10-31 14:58:04 -0700164inline ObjPtr<String> String::Intern() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700165 return Runtime::Current()->GetInternTable()->InternWeak(this);
166}
167
Jeff Hao848f70a2014-01-15 13:49:50 -0800168inline uint16_t String::CharAt(int32_t index) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700169 int32_t count = GetLength();
Jeff Hao848f70a2014-01-15 13:49:50 -0800170 if (UNLIKELY((index < 0) || (index >= count))) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100171 ThrowStringIndexOutOfBoundsException(index, count);
Jeff Hao848f70a2014-01-15 13:49:50 -0800172 return 0;
173 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700174 if (IsCompressed()) {
175 return GetValueCompressed()[index];
176 } else {
177 return GetValue()[index];
178 }
179}
180
181template <typename MemoryType>
182int32_t String::FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) {
183 const MemoryType* p = chars + start;
184 const MemoryType* end = chars + GetLength();
185 while (p < end) {
186 if (*p++ == ch) {
187 return (p - 1) - chars;
188 }
189 }
190 return -1;
Jeff Hao848f70a2014-01-15 13:49:50 -0800191}
192
193template<VerifyObjectFlags kVerifyFlags>
194inline size_t String::SizeOf() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700195 size_t size = sizeof(String);
196 if (IsCompressed()) {
197 size += (sizeof(uint8_t) * GetLength<kVerifyFlags>());
198 } else {
199 size += (sizeof(uint16_t) * GetLength<kVerifyFlags>());
200 }
Vladimir Marko4ef52262015-08-26 18:12:56 +0100201 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100202 // so make sure the zero-padding is actually copied around if GC compaction
203 // chooses to copy only SizeOf() bytes.
Vladimir Marko4ef52262015-08-26 18:12:56 +0100204 // http://b/23528461
205 return RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800206}
207
208template <bool kIsInstrumented, typename PreFenceVisitor>
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700209inline String* String::Alloc(Thread* self, int32_t utf16_length_with_flag,
210 gc::AllocatorType allocator_type,
Jeff Hao848f70a2014-01-15 13:49:50 -0800211 const PreFenceVisitor& pre_fence_visitor) {
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100212 constexpr size_t header_size = sizeof(String);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100213 const bool compressible = kUseStringCompression && String::IsCompressed(utf16_length_with_flag);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700214 const size_t block_size = (compressible) ? sizeof(uint8_t) : sizeof(uint16_t);
215 size_t length = String::GetLengthFromCount(utf16_length_with_flag);
216 static_assert(sizeof(length) <= sizeof(size_t),
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100217 "static_cast<size_t>(utf16_length) must not lose bits.");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700218 size_t data_size = block_size * length;
Jeff Hao848f70a2014-01-15 13:49:50 -0800219 size_t size = header_size + data_size;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100220 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
221 // so make sure the allocator clears the padding as well.
222 // http://b/23528461
223 size_t alloc_size = RoundUp(size, kObjectAlignment);
Jeff Hao848f70a2014-01-15 13:49:50 -0800224
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700225 Class* string_class = GetJavaLangString();
Jeff Hao848f70a2014-01-15 13:49:50 -0800226 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100227 // Do this by comparing with the maximum length that will _not_ cause an overflow.
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700228 const size_t overflow_length = (-header_size) / block_size; // Unsigned arithmetic.
229 const size_t max_alloc_length = overflow_length - 1u;
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100230 static_assert(IsAligned<sizeof(uint16_t)>(kObjectAlignment),
231 "kObjectAlignment must be at least as big as Java char alignment");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700232 const size_t max_length = RoundDown(max_alloc_length, kObjectAlignment / block_size);
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100233 if (UNLIKELY(length > max_length)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800234 self->ThrowOutOfMemoryError(
235 android::base::StringPrintf("%s of length %d would overflow",
236 Class::PrettyDescriptor(string_class).c_str(),
237 static_cast<int>(length)).c_str());
Jeff Hao848f70a2014-01-15 13:49:50 -0800238 return nullptr;
239 }
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100240
Jeff Hao848f70a2014-01-15 13:49:50 -0800241 gc::Heap* heap = Runtime::Current()->GetHeap();
242 return down_cast<String*>(
Vladimir Markoc2b35d22015-08-27 11:09:29 +0100243 heap->AllocObjectWithAllocator<kIsInstrumented, true>(self, string_class, alloc_size,
Jeff Haob7c8c1a2015-06-22 14:29:54 -0700244 allocator_type, pre_fence_visitor));
Jeff Hao848f70a2014-01-15 13:49:50 -0800245}
246
247template <bool kIsInstrumented>
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700248inline String* String::AllocEmptyString(Thread* self, gc::AllocatorType allocator_type) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100249 const int32_t length_with_flag = String::GetFlaggedCount(0, /* compressible */ true);
jessicahandojo7908c8e2016-09-09 19:05:34 -0700250 SetStringCountVisitor visitor(length_with_flag);
251 return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700252}
253
254template <bool kIsInstrumented>
Jeff Hao848f70a2014-01-15 13:49:50 -0800255inline String* String::AllocFromByteArray(Thread* self, int32_t byte_length,
256 Handle<ByteArray> array, int32_t offset,
257 int32_t high_byte, gc::AllocatorType allocator_type) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700258 const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100259 const bool compressible =
260 kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0);
261 const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700262 SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
263 String* string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800264 return string;
265}
266
267template <bool kIsInstrumented>
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700268inline String* String::AllocFromCharArray(Thread* self, int32_t count,
Jeff Hao848f70a2014-01-15 13:49:50 -0800269 Handle<CharArray> array, int32_t offset,
270 gc::AllocatorType allocator_type) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700271 // It is a caller error to have a count less than the actual array's size.
272 DCHECK_GE(array->GetLength(), count);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700273 const bool compressible = kUseStringCompression &&
274 String::AllASCII<uint16_t>(array->GetData() + offset, count);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100275 const int32_t length_with_flag = String::GetFlaggedCount(count, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700276 SetStringCountAndValueVisitorFromCharArray visitor(length_with_flag, array, offset);
277 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800278 return new_string;
279}
280
281template <bool kIsInstrumented>
282inline String* String::AllocFromString(Thread* self, int32_t string_length, Handle<String> string,
283 int32_t offset, gc::AllocatorType allocator_type) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700284 const bool compressible = kUseStringCompression &&
285 ((string->IsCompressed()) ? true : String::AllASCII<uint16_t>(string->GetValue() + offset,
286 string_length));
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100287 const int32_t length_with_flag = String::GetFlaggedCount(string_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700288 SetStringCountAndValueVisitorFromString visitor(length_with_flag, string, offset);
289 String* new_string = Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800290 return new_string;
291}
292
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700293inline int32_t String::GetHashCode() {
294 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
295 if (UNLIKELY(result == 0)) {
296 result = ComputeHashCode();
297 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700298 if (kIsDebugBuild) {
299 if (IsCompressed()) {
300 DCHECK(result != 0 || ComputeUtf16Hash(GetValueCompressed(), GetLength()) == 0)
301 << ToModifiedUtf8() << " " << result;
302 } else {
303 DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0)
304 << ToModifiedUtf8() << " " << result;
305 }
306 }
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700307 return result;
308}
309
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700310template<typename MemoryType>
Vladimir Markoe39f14f2017-02-10 15:44:25 +0000311inline bool String::AllASCII(const MemoryType* chars, const int length) {
Vladimir Marko16850ae2016-12-09 14:01:02 +0000312 static_assert(std::is_unsigned<MemoryType>::value, "Expecting unsigned MemoryType");
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700313 for (int i = 0; i < length; ++i) {
Vladimir Marko16850ae2016-12-09 14:01:02 +0000314 // Valid ASCII characters are in range 1..0x7f. Zero is not considered ASCII
315 // because it would complicate the detection of ASCII strings in Modified-UTF8.
316 if ((chars[i] - 1u) >= 0x7fu) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700317 return false;
318 }
319 }
320 return true;
321}
322
Vladimir Markoe39f14f2017-02-10 15:44:25 +0000323inline bool String::DexFileStringAllASCII(const char* chars, const int length) {
324 // For strings from the dex file we just need to check that
325 // the terminating character is at the right position.
326 DCHECK_EQ(AllASCII(reinterpret_cast<const uint8_t*>(chars), length), chars[length] == 0);
327 return chars[length] == 0;
328}
329
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700330} // namespace mirror
331} // namespace art
332
333#endif // ART_RUNTIME_MIRROR_STRING_INL_H_