blob: 53fcf82caa1d09272f7ad7886fd67182c1d12a82 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -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 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_STRING_H_
18#define ART_RUNTIME_MIRROR_STRING_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Vladimir Marko5924a4a2018-05-29 17:40:41 +010020#include "base/bit_utils.h"
Vladimir Marko6834d342018-05-25 13:12:09 +010021#include "class.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "object.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080023#include "runtime_globals.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25namespace art {
26
Andreas Gampe391be3a2019-05-14 12:41:40 -070027namespace gc {
28enum AllocatorType : char;
29} // namespace gc
30
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031template<class T> class Handle;
Vladimir Marko179b7c62019-03-22 13:38:57 +000032template<class MirrorType> class ObjPtr;
Vladimir Marko552a1342017-10-31 10:56:47 +000033class StringBuilderAppend;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034struct StringOffsets;
Roland Levillain0d5a2812015-11-13 10:07:31 +000035class StubTest_ReadBarrierForRoot_Test;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
37namespace mirror {
38
jessicahandojo3aaa37b2016-07-29 14:46:37 -070039// String Compression
Vladimir Markobcf716f2017-02-23 10:43:09 +000040static constexpr bool kUseStringCompression = true;
Vladimir Markofdaf0f42016-10-13 19:29:53 +010041enum class StringCompressionFlag : uint32_t {
42 kCompressed = 0u,
43 kUncompressed = 1u
44};
jessicahandojo3aaa37b2016-07-29 14:46:37 -070045
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046// C++ mirror of java.lang.String
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010047class MANAGED String final : public Object {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 public:
Alex Light8f187c32021-04-20 14:29:00 -070049 MIRROR_CLASS("Ljava/lang/String;");
50
Mingyao Yang98d1cc82014-05-15 17:02:16 -070051 // Size of java.lang.String.class.
Andreas Gampe542451c2016-07-26 09:02:02 -070052 static uint32_t ClassSize(PointerSize pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070053
54 // Size of an instance of java.lang.String not including its value array.
55 static constexpr uint32_t InstanceSize() {
56 return sizeof(String);
57 }
58
David Srbecky56de89a2018-10-01 15:32:20 +010059 static constexpr MemberOffset CountOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 return OFFSET_OF_OBJECT_MEMBER(String, count_);
61 }
62
David Srbecky56de89a2018-10-01 15:32:20 +010063 static constexpr MemberOffset ValueOffset() {
Jeff Hao848f70a2014-01-15 13:49:50 -080064 return OFFSET_OF_OBJECT_MEMBER(String, value_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 }
66
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 uint16_t* GetValue() REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080068 return &value_[0];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
70
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 uint8_t* GetValueCompressed() REQUIRES_SHARED(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070072 return &value_compressed_[0];
73 }
74
Jeff Hao848f70a2014-01-15 13:49:50 -080075 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Vladimir Marko5924a4a2018-05-29 17:40:41 +010076 size_t SizeOf() REQUIRES_SHARED(Locks::mutator_lock_) {
77 size_t size = sizeof(String);
78 if (IsCompressed()) {
79 size += (sizeof(uint8_t) * GetLength<kVerifyFlags>());
80 } else {
81 size += (sizeof(uint16_t) * GetLength<kVerifyFlags>());
82 }
83 // String.equals() intrinsics assume zero-padding up to kObjectAlignment,
84 // so make sure the zero-padding is actually copied around if GC compaction
85 // chooses to copy only SizeOf() bytes.
86 // http://b/23528461
87 return RoundUp(size, kObjectAlignment);
88 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089
jessicahandojo3aaa37b2016-07-29 14:46:37 -070090 // Taking out the first/uppermost bit because it is not part of actual length value
Jeff Hao848f70a2014-01-15 13:49:50 -080091 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 int32_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070093 return GetLengthFromCount(GetCount<kVerifyFlags>());
94 }
95
96 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 int32_t GetCount() REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -080098 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(String, count_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 }
100
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 void SetCount(int32_t new_count) REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800102 // Count is invariant so use non-transactional mode. Also disable check as we may run inside
103 // a transaction.
Jeff Hao848f70a2014-01-15 13:49:50 -0800104 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count);
105 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 int32_t GetHashCode() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700109 // Computes, stores, and returns the hash code.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 int32_t ComputeHashCode() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 int32_t GetUtfLength() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 uint16_t CharAt(int32_t index) REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800115
Vladimir Marko92907f32017-02-20 14:08:30 +0000116 // Create a new string where all occurences of `old_c` are replaced with `new_c`.
117 // String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000118 static ObjPtr<String> DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c)
Vladimir Marko92907f32017-02-20 14:08:30 +0000119 REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800120
Mathieu Chartier9e868092016-10-31 14:58:04 -0700121 ObjPtr<String> Intern() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122
Vladimir Markoedc005e2021-07-16 10:22:28 +0100123 template <bool kIsInstrumented = true, typename PreFenceVisitor>
124 ALWAYS_INLINE static ObjPtr<String> Alloc(Thread* self,
125 int32_t utf16_length_with_flag,
126 gc::AllocatorType allocator_type,
127 const PreFenceVisitor& pre_fence_visitor)
128 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
129
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100130 template <bool kIsInstrumented = true>
Vladimir Marko179b7c62019-03-22 13:38:57 +0000131 ALWAYS_INLINE static ObjPtr<String> AllocFromByteArray(Thread* self,
132 int32_t byte_length,
133 Handle<ByteArray> array,
134 int32_t offset,
135 int32_t high_byte,
136 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800138
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100139 template <bool kIsInstrumented = true>
Vladimir Marko179b7c62019-03-22 13:38:57 +0000140 ALWAYS_INLINE static ObjPtr<String> AllocFromCharArray(Thread* self,
141 int32_t count,
142 Handle<CharArray> array,
143 int32_t offset,
144 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800146
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100147 template <bool kIsInstrumented = true>
Vladimir Marko179b7c62019-03-22 13:38:57 +0000148 ALWAYS_INLINE static ObjPtr<String> AllocFromString(Thread* self,
149 int32_t string_length,
150 Handle<String> string,
151 int32_t offset,
152 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700153 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800154
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100155 template <bool kIsInstrumented = true>
Vladimir Marko179b7c62019-03-22 13:38:57 +0000156 ALWAYS_INLINE static ObjPtr<String> AllocEmptyString(Thread* self,
157 gc::AllocatorType allocator_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700158 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700159
Vladimir Marko036b0702020-10-27 10:36:06 +0000160 static ObjPtr<String> DoConcat(Thread* self, Handle<String> h_this, Handle<String> h_arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700161 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800162
Vladimir Marko179b7c62019-03-22 13:38:57 +0000163 static ObjPtr<String> AllocFromUtf16(Thread* self,
164 int32_t utf16_length,
165 const uint16_t* utf16_data_in)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700166 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167
Vladimir Marko179b7c62019-03-22 13:38:57 +0000168 static ObjPtr<String> AllocFromModifiedUtf8(Thread* self, const char* utf)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170
Vladimir Marko179b7c62019-03-22 13:38:57 +0000171 static ObjPtr<String> AllocFromModifiedUtf8(Thread* self,
172 int32_t utf16_length,
173 const char* utf8_data_in,
174 int32_t utf8_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300176
Vladimir Marko179b7c62019-03-22 13:38:57 +0000177 static ObjPtr<String> AllocFromModifiedUtf8(Thread* self,
178 int32_t utf16_length,
179 const char* utf8_data_in)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 bool Equals(const char* modified_utf8) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183
Mathieu Chartier31e88222016-10-14 18:43:19 -0700184 bool Equals(ObjPtr<String> that) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700187 std::string ToModifiedUtf8() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 int32_t FastIndexOf(int32_t ch, int32_t start) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700191 template <typename MemoryType>
192 int32_t FastIndexOf(MemoryType* chars, int32_t ch, int32_t start)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700193 REQUIRES_SHARED(Locks::mutator_lock_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700194
Mathieu Chartier31e88222016-10-14 18:43:19 -0700195 int32_t CompareTo(ObjPtr<String> other) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196
Vladimir Marko3068d582019-05-28 16:39:29 +0100197 static ObjPtr<CharArray> ToCharArray(Handle<String> h_this, Thread* self)
198 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartiered8990a2015-07-23 14:11:16 -0700199 REQUIRES(!Roles::uninterruptible_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800200
Jeff Hao848f70a2014-01-15 13:49:50 -0800201 void GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfd04b6f2014-11-14 19:34:18 -0800203
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700204 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 bool IsCompressed() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100206 return kUseStringCompression && IsCompressed(GetCount());
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700207 }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsValueNull() REQUIRES_SHARED(Locks::mutator_lock_);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700210
211 template<typename MemoryType>
Vladimir Markoe39f14f2017-02-10 15:44:25 +0000212 static bool AllASCII(const MemoryType* chars, const int length);
213
214 static bool DexFileStringAllASCII(const char* chars, const int length);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700215
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100216 ALWAYS_INLINE static bool IsCompressed(int32_t count) {
217 return GetCompressionFlagFromCount(count) == StringCompressionFlag::kCompressed;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700218 }
219
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100220 ALWAYS_INLINE static StringCompressionFlag GetCompressionFlagFromCount(int32_t count) {
221 return kUseStringCompression
222 ? static_cast<StringCompressionFlag>(static_cast<uint32_t>(count) & 1u)
223 : StringCompressionFlag::kUncompressed;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700224 }
225
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100226 ALWAYS_INLINE static int32_t GetLengthFromCount(int32_t count) {
227 return kUseStringCompression ? static_cast<int32_t>(static_cast<uint32_t>(count) >> 1) : count;
228 }
229
230 ALWAYS_INLINE static int32_t GetFlaggedCount(int32_t length, bool compressible) {
231 return kUseStringCompression
232 ? static_cast<int32_t>((static_cast<uint32_t>(length) << 1) |
233 (static_cast<uint32_t>(compressible
234 ? StringCompressionFlag::kCompressed
235 : StringCompressionFlag::kUncompressed)))
236 : length;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700237 }
238
David Sehr709b0702016-10-13 09:12:37 -0700239 // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
240 // "[[I" would be "int[][]", "[Ljava/lang/String;" would be
241 // "java.lang.String[]", and so forth.
242 static std::string PrettyStringDescriptor(ObjPtr<mirror::String> descriptor)
243 REQUIRES_SHARED(Locks::mutator_lock_);
244 std::string PrettyStringDescriptor()
245 REQUIRES_SHARED(Locks::mutator_lock_);
246
Vladimir Marko92907f32017-02-20 14:08:30 +0000247 static constexpr bool IsASCII(uint16_t c) {
248 // Valid ASCII characters are in range 1..0x7f. Zero is not considered ASCII
249 // because it would complicate the detection of ASCII strings in Modified-UTF8.
250 return (c - 1u) < 0x7fu;
251 }
252
Vladimir Marko7dd48b92020-11-12 15:18:40 +0000253 private:
Vladimir Marko92907f32017-02-20 14:08:30 +0000254 static bool AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii);
255
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 void SetHashCode(int32_t new_hash_code) REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100257 // Hash code is invariant so use non-transactional mode. Also disable check as we may run inside
258 // a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700259 DCHECK_EQ(0, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)));
260 SetField32<false, false>(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 }
262
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Vladimir Marko595beb32017-02-06 14:11:54 +0000264
265 // If string compression is enabled, count_ holds the StringCompressionFlag in the
266 // least significant bit and the length in the remaining bits, length = count_ >> 1.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 int32_t count_;
268
269 uint32_t hash_code_;
270
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700271 // Compression of all-ASCII into 8-bit memory leads to usage one of these fields
272 union {
273 uint16_t value_[0];
274 uint8_t value_compressed_[0];
275 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276
Vladimir Marko552a1342017-10-31 10:56:47 +0000277 friend class art::StringBuilderAppend;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 friend struct art::StringOffsets; // for verifying offset information
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
281};
282
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283} // namespace mirror
284} // namespace art
285
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700286#endif // ART_RUNTIME_MIRROR_STRING_H_