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