1 /*
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 */
16
17 #include "string-alloc-inl.h"
18
19 #include "arch/memcmp16.h"
20 #include "array-alloc-inl.h"
21 #include "base/array_ref.h"
22 #include "base/stl_util.h"
23 #include "class-inl.h"
24 #include "dex/descriptors_names.h"
25 #include "dex/utf-inl.h"
26 #include "gc/accounting/card_table-inl.h"
27 #include "handle_scope-inl.h"
28 #include "intern_table.h"
29 #include "object-inl.h"
30 #include "runtime.h"
31 #include "string-inl.h"
32 #include "thread.h"
33
34 namespace art {
35 namespace mirror {
36
FastIndexOf(int32_t ch,int32_t start)37 int32_t String::FastIndexOf(int32_t ch, int32_t start) {
38 int32_t count = GetLength();
39 if (start < 0) {
40 start = 0;
41 } else if (start > count) {
42 start = count;
43 }
44 if (IsCompressed()) {
45 return FastIndexOf<uint8_t>(GetValueCompressed(), ch, start);
46 } else {
47 return FastIndexOf<uint16_t>(GetValue(), ch, start);
48 }
49 }
50
ComputeAndSetHashCode()51 int32_t String::ComputeAndSetHashCode() {
52 int32_t new_hash_code = ComputeHashCode();
53 SetHashCode(new_hash_code);
54 return new_hash_code;
55 }
56
AllASCIIExcept(const uint16_t * chars,int32_t length,uint16_t non_ascii)57 inline bool String::AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii) {
58 DCHECK(!IsASCII(non_ascii));
59 for (int32_t i = 0; i < length; ++i) {
60 if (!IsASCII(chars[i]) && chars[i] != non_ascii) {
61 return false;
62 }
63 }
64 return true;
65 }
66
DoReplace(Thread * self,Handle<String> src,uint16_t old_c,uint16_t new_c)67 ObjPtr<String> String::DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) {
68 int32_t length = src->GetLength();
69 DCHECK(src->IsCompressed()
70 ? ContainsElement(ArrayRef<uint8_t>(src->value_compressed_, length), old_c)
71 : ContainsElement(ArrayRef<uint16_t>(src->value_, length), old_c));
72 bool compressible =
73 kUseStringCompression &&
74 IsASCII(new_c) &&
75 (src->IsCompressed() || (!IsASCII(old_c) && AllASCIIExcept(src->value_, length, old_c)));
76 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
77 const int32_t length_with_flag = String::GetFlaggedCount(length, compressible);
78
79 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
80 SetStringCountVisitor set_string_count_visitor(length_with_flag);
81 set_string_count_visitor(obj, usable_size);
82 ObjPtr<String> new_string = obj->AsString();
83 if (compressible) {
84 auto replace = [old_c, new_c](uint16_t c) {
85 return dchecked_integral_cast<uint8_t>((old_c != c) ? c : new_c);
86 };
87 uint8_t* out = new_string->value_compressed_;
88 if (LIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
89 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
90 } else {
91 std::transform(src->value_, src->value_ + length, out, replace);
92 }
93 DCHECK(kUseStringCompression && AllASCII(out, length));
94 } else {
95 auto replace = [old_c, new_c](uint16_t c) {
96 return (old_c != c) ? c : new_c;
97 };
98 uint16_t* out = new_string->value_;
99 if (UNLIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
100 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
101 } else {
102 std::transform(src->value_, src->value_ + length, out, replace);
103 }
104 DCHECK_IMPLIES(kUseStringCompression, !AllASCII(out, length));
105 }
106 };
107 return Alloc(self, length_with_flag, allocator_type, visitor);
108 }
109
DoConcat(Thread * self,Handle<String> h_this,Handle<String> h_arg)110 ObjPtr<String> String::DoConcat(Thread* self, Handle<String> h_this, Handle<String> h_arg) {
111 int32_t length_this = h_this->GetLength();
112 int32_t length_arg = h_arg->GetLength();
113 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
114 const bool compressible =
115 kUseStringCompression && (h_this->IsCompressed() && h_arg->IsCompressed());
116 const int32_t length_with_flag = String::GetFlaggedCount(length_this + length_arg, compressible);
117
118 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
119 SetStringCountVisitor set_string_count_visitor(length_with_flag);
120 set_string_count_visitor(obj, usable_size);
121 ObjPtr<String> new_string = obj->AsString();
122 if (compressible) {
123 uint8_t* new_value = new_string->GetValueCompressed();
124 memcpy(new_value, h_this->GetValueCompressed(), length_this * sizeof(uint8_t));
125 memcpy(new_value + length_this, h_arg->GetValueCompressed(), length_arg * sizeof(uint8_t));
126 } else {
127 uint16_t* new_value = new_string->GetValue();
128 if (h_this->IsCompressed()) {
129 const uint8_t* value_this = h_this->GetValueCompressed();
130 for (int i = 0; i < length_this; ++i) {
131 new_value[i] = value_this[i];
132 }
133 } else {
134 memcpy(new_value, h_this->GetValue(), length_this * sizeof(uint16_t));
135 }
136 if (h_arg->IsCompressed()) {
137 const uint8_t* value_arg = h_arg->GetValueCompressed();
138 for (int i = 0; i < length_arg; ++i) {
139 new_value[i + length_this] = value_arg[i];
140 }
141 } else {
142 memcpy(new_value + length_this, h_arg->GetValue(), length_arg * sizeof(uint16_t));
143 }
144 }
145 };
146 return Alloc(self, length_with_flag, allocator_type, visitor);
147 }
148
149 template<typename T>
RepeatCharacters(ObjPtr<String> new_string,Handle<String> h_this,int32_t count)150 static void RepeatCharacters(ObjPtr<String> new_string, Handle<String> h_this, int32_t count)
151 REQUIRES_SHARED(Locks::mutator_lock_) {
152 T *new_value, *h_this_value;
153 if constexpr (std::is_same_v<T, uint8_t>) {
154 new_value = new_string->GetValueCompressed();
155 h_this_value = h_this->GetValueCompressed();
156 } else {
157 new_value = new_string->GetValue();
158 h_this_value = h_this->GetValue();
159 }
160 int32_t length_this = h_this->GetLength();
161 if (length_this == 1) {
162 // compiler is smart enough to use memset for uint8_t
163 std::fill(new_value, new_value + count, h_this_value[0]);
164 } else {
165 memcpy(new_value, h_this_value, length_this * sizeof(T));
166 int32_t copied = length_this;
167 int32_t limit = length_this * count;
168 for (; copied < limit - copied; copied <<= 1) {
169 memcpy(new_value + copied, new_value, copied * sizeof(T));
170 }
171 memcpy(new_value + copied, new_value, (limit - copied) * sizeof(T));
172 }
173 }
174
DoRepeat(Thread * self,Handle<String> h_this,int32_t count)175 ObjPtr<String> String::DoRepeat(Thread* self, Handle<String> h_this, int32_t count) {
176 int32_t length_this = h_this->GetLength();
177 DCHECK_GT(count, 1);
178 DCHECK_LE(length_this, std::numeric_limits<int32_t>::max() / count);
179 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
180 const bool compressible = kUseStringCompression && (h_this->IsCompressed());
181 const int32_t length_with_flag = String::GetFlaggedCount(length_this * count, compressible);
182
183 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
184 SetStringCountVisitor set_string_count_visitor(length_with_flag);
185 set_string_count_visitor(obj, usable_size);
186 ObjPtr<String> new_string = obj->AsString();
187
188 if (compressible) {
189 RepeatCharacters<uint8_t>(new_string, h_this, count);
190 } else {
191 RepeatCharacters<uint16_t>(new_string, h_this, count);
192 }
193 };
194 return Alloc(self, length_with_flag, allocator_type, visitor);
195 }
196
AllocFromUtf16(Thread * self,int32_t utf16_length,const uint16_t * utf16_data_in)197 ObjPtr<String> String::AllocFromUtf16(Thread* self,
198 int32_t utf16_length,
199 const uint16_t* utf16_data_in) {
200 CHECK_IMPLIES(utf16_data_in == nullptr, utf16_length == 0);
201 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
202 const bool compressible = kUseStringCompression &&
203 String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
204 int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
205
206 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
207 SetStringCountVisitor set_string_count_visitor(length_with_flag);
208 set_string_count_visitor(obj, usable_size);
209 ObjPtr<String> new_string = obj->AsString();
210 if (compressible) {
211 uint8_t* value = new_string->GetValueCompressed();
212 for (int i = 0; i < utf16_length; ++i) {
213 value[i] = static_cast<uint8_t>(utf16_data_in[i]);
214 }
215 } else {
216 memcpy(new_string->GetValue(), utf16_data_in, utf16_length * sizeof(uint16_t));
217 }
218 };
219 return Alloc(self, length_with_flag, allocator_type, visitor);
220 }
221
AllocFromModifiedUtf8(Thread * self,const char * utf)222 ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
223 DCHECK(utf != nullptr);
224 size_t byte_count = strlen(utf);
225 size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
226 return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
227 }
228
AllocFromModifiedUtf8(Thread * self,int32_t utf16_length,const char * utf8_data_in)229 ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self,
230 int32_t utf16_length,
231 const char* utf8_data_in) {
232 return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
233 }
234
AllocFromModifiedUtf8(Thread * self,int32_t utf16_length,const char * utf8_data_in,int32_t utf8_length)235 ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self,
236 int32_t utf16_length,
237 const char* utf8_data_in,
238 int32_t utf8_length) {
239 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
240 const bool compressible = kUseStringCompression && (utf16_length == utf8_length);
241 const int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
242
243 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
244 SetStringCountVisitor set_string_count_visitor(length_with_flag);
245 set_string_count_visitor(obj, usable_size);
246 ObjPtr<String> new_string = obj->AsString();
247 if (compressible) {
248 memcpy(new_string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
249 } else {
250 uint16_t* utf16_data_out = new_string->GetValue();
251 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
252 }
253 };
254 return Alloc(self, length_with_flag, allocator_type, visitor);
255 }
256
Equals(ObjPtr<String> that)257 bool String::Equals(ObjPtr<String> that) {
258 if (this == that) {
259 // Quick reference equality test
260 return true;
261 } else if (that == nullptr) {
262 // Null isn't an instanceof anything
263 return false;
264 } else if (this->GetCount() != that->GetCount()) {
265 // Quick length and compression inequality test
266 return false;
267 } else {
268 // Note: don't short circuit on hash code as we're presumably here as the
269 // hash code was already equal
270 if (this->IsCompressed()) {
271 return memcmp(this->GetValueCompressed(), that->GetValueCompressed(), this->GetLength()) == 0;
272 } else {
273 return memcmp(this->GetValue(), that->GetValue(), sizeof(uint16_t) * this->GetLength()) == 0;
274 }
275 }
276 }
277
Equals(const char * modified_utf8)278 bool String::Equals(const char* modified_utf8) {
279 const int32_t length = GetLength();
280 if (IsCompressed()) {
281 return strlen(modified_utf8) == dchecked_integral_cast<uint32_t>(length) &&
282 memcmp(modified_utf8, GetValueCompressed(), length) == 0;
283 }
284 const uint16_t* value = GetValue();
285 int32_t i = 0;
286 while (i < length) {
287 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
288 if (ch == '\0') {
289 return false;
290 }
291
292 if (GetLeadingUtf16Char(ch) != value[i++]) {
293 return false;
294 }
295
296 const uint16_t trailing = GetTrailingUtf16Char(ch);
297 if (trailing != 0) {
298 if (i == length) {
299 return false;
300 }
301
302 if (value[i++] != trailing) {
303 return false;
304 }
305 }
306 }
307 return *modified_utf8 == '\0';
308 }
309
310 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
ToModifiedUtf8()311 std::string String::ToModifiedUtf8() {
312 if (IsCompressed()) {
313 return std::string(reinterpret_cast<const char*>(GetValueCompressed()), GetLength());
314 } else {
315 size_t byte_count = GetUtfLength();
316 std::string result(byte_count, static_cast<char>(0));
317 ConvertUtf16ToModifiedUtf8(&result[0], byte_count, GetValue(), GetLength());
318 return result;
319 }
320 }
321
CompareTo(ObjPtr<String> rhs)322 int32_t String::CompareTo(ObjPtr<String> rhs) {
323 // Quick test for comparison of a string with itself.
324 ObjPtr<String> lhs = this;
325 if (lhs == rhs) {
326 return 0;
327 }
328 int32_t lhs_count = lhs->GetLength();
329 int32_t rhs_count = rhs->GetLength();
330 int32_t count_diff = lhs_count - rhs_count;
331 int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count;
332 if (lhs->IsCompressed() && rhs->IsCompressed()) {
333 const uint8_t* lhs_chars = lhs->GetValueCompressed();
334 const uint8_t* rhs_chars = rhs->GetValueCompressed();
335 for (int32_t i = 0; i < min_count; ++i) {
336 int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]);
337 if (char_diff != 0) {
338 return char_diff;
339 }
340 }
341 } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
342 const uint8_t* compressed_chars =
343 lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed();
344 const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue();
345 for (int32_t i = 0; i < min_count; ++i) {
346 int32_t char_diff =
347 static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]);
348 if (char_diff != 0) {
349 return lhs->IsCompressed() ? char_diff : -char_diff;
350 }
351 }
352 } else {
353 const uint16_t* lhs_chars = lhs->GetValue();
354 const uint16_t* rhs_chars = rhs->GetValue();
355 // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch
356 // where memcmp() only guarantees that the returned value has the same sign.
357 int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count);
358 if (char_diff != 0) {
359 return char_diff;
360 }
361 }
362 return count_diff;
363 }
364
ToCharArray(Handle<String> h_this,Thread * self)365 ObjPtr<CharArray> String::ToCharArray(Handle<String> h_this, Thread* self) {
366 ObjPtr<CharArray> result = CharArray::Alloc(self, h_this->GetLength());
367 if (result != nullptr) {
368 if (h_this->IsCompressed()) {
369 int32_t length = h_this->GetLength();
370 const uint8_t* src = h_this->GetValueCompressed();
371 uint16_t* dest = result->GetData();
372 for (int i = 0; i < length; ++i) {
373 dest[i] = src[i];
374 }
375 } else {
376 memcpy(result->GetData(), h_this->GetValue(), h_this->GetLength() * sizeof(uint16_t));
377 }
378 } else {
379 self->AssertPendingOOMException();
380 }
381 return result;
382 }
383
GetChars(int32_t start,int32_t end,Handle<CharArray> array,int32_t index)384 void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
385 uint16_t* data = array->GetData() + index;
386 DCHECK_LE(start, end);
387 int32_t length = end - start;
388 if (IsCompressed()) {
389 const uint8_t* value = GetValueCompressed() + start;
390 for (int i = 0; i < length; ++i) {
391 data[i] = value[i];
392 }
393 } else {
394 uint16_t* value = GetValue() + start;
395 memcpy(data, value, length * sizeof(uint16_t));
396 }
397 }
398
IsValueNull()399 bool String::IsValueNull() {
400 return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
401 }
402
PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor)403 std::string String::PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor) {
404 if (java_descriptor == nullptr) {
405 return "null";
406 }
407 return java_descriptor->PrettyStringDescriptor();
408 }
409
PrettyStringDescriptor()410 std::string String::PrettyStringDescriptor() {
411 return PrettyDescriptor(ToModifiedUtf8().c_str());
412 }
413
Intern()414 ObjPtr<String> String::Intern() {
415 return Runtime::Current()->GetInternTable()->InternWeak(this);
416 }
417
418 } // namespace mirror
419 } // namespace art
420