Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ART_RUNTIME_TRANSACTION_H_ |
| 18 | #define ART_RUNTIME_TRANSACTION_H_ |
| 19 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 20 | #include "base/scoped_arena_containers.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 21 | #include "base/macros.h" |
| 22 | #include "base/mutex.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 23 | #include "base/safe_map.h" |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 24 | #include "base/value_object.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file_types.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 26 | #include "dex/primitive.h" |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 27 | #include "gc_root.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 28 | #include "offsets.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 29 | |
| 30 | #include <list> |
| 31 | #include <map> |
| 32 | |
| 33 | namespace art { |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 34 | namespace gc { |
| 35 | class Heap; |
| 36 | } // namespace gc |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 37 | namespace mirror { |
| 38 | class Array; |
Chang Xing | cade5c3 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 39 | class Class; |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 40 | class DexCache; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 41 | class Object; |
| 42 | class String; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 43 | } // namespace mirror |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 44 | class InternTable; |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 45 | template<class MirrorType> class ObjPtr; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 46 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 47 | class Transaction final { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 48 | public: |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 49 | static constexpr const char* kAbortExceptionDescriptor = "Ldalvik/system/TransactionAbortError;"; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 50 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 51 | Transaction(bool strict, mirror::Class* root, ArenaStack* arena_stack, ArenaPool* arena_pool); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 52 | ~Transaction(); |
| 53 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 54 | ArenaStack* GetArenaStack() { |
| 55 | return allocator_.GetArenaStack(); |
| 56 | } |
| 57 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 58 | void Abort(const std::string& abort_message) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 59 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | b81e1cd | 2015-04-28 12:31:41 +0200 | [diff] [blame] | 60 | void ThrowAbortError(Thread* self, const std::string* abort_message) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 61 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 62 | bool IsAborted() const { |
| 63 | return aborted_; |
| 64 | } |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 65 | |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 66 | // If the transaction is rollbacking. Transactions will set this flag when they start rollbacking, |
| 67 | // because the nested transaction should be disabled when rollbacking to restore the memory. |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 68 | bool IsRollingBack() const { |
| 69 | return rolling_back_; |
| 70 | } |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 71 | |
Chang Xing | 5a906fc | 2017-07-26 15:01:16 -0700 | [diff] [blame] | 72 | // If the transaction is in strict mode, then all access of static fields will be constrained, |
| 73 | // one class's clinit will not be allowed to read or modify another class's static fields, unless |
| 74 | // the transaction is aborted. |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 75 | bool IsStrict() const { |
Vladimir Marko | 4d7b689 | 2020-01-16 17:06:35 +0000 | [diff] [blame] | 76 | return strict_; |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 77 | } |
Chang Xing | 5a906fc | 2017-07-26 15:01:16 -0700 | [diff] [blame] | 78 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 79 | // Record object field changes. |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 80 | void RecordWriteFieldBoolean(mirror::Object* obj, |
| 81 | MemberOffset field_offset, |
| 82 | uint8_t value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 83 | bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 84 | void RecordWriteFieldByte(mirror::Object* obj, |
| 85 | MemberOffset field_offset, |
| 86 | int8_t value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 87 | bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 88 | void RecordWriteFieldChar(mirror::Object* obj, |
| 89 | MemberOffset field_offset, |
| 90 | uint16_t value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 91 | bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 92 | void RecordWriteFieldShort(mirror::Object* obj, |
| 93 | MemberOffset field_offset, |
| 94 | int16_t value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 95 | bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 96 | void RecordWriteField32(mirror::Object* obj, |
| 97 | MemberOffset field_offset, |
| 98 | uint32_t value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 99 | bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 100 | void RecordWriteField64(mirror::Object* obj, |
| 101 | MemberOffset field_offset, |
| 102 | uint64_t value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 103 | bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 104 | void RecordWriteFieldReference(mirror::Object* obj, |
| 105 | MemberOffset field_offset, |
| 106 | mirror::Object* value, |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 107 | bool is_volatile); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 108 | |
| 109 | // Record array change. |
| 110 | void RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 111 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 112 | |
| 113 | // Record intern string table changes. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 114 | void RecordStrongStringInsertion(ObjPtr<mirror::String> s) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 115 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 116 | void RecordWeakStringInsertion(ObjPtr<mirror::String> s) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 117 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 118 | void RecordStrongStringRemoval(ObjPtr<mirror::String> s) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 119 | REQUIRES(Locks::intern_table_lock_); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 120 | void RecordWeakStringRemoval(ObjPtr<mirror::String> s) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 121 | REQUIRES(Locks::intern_table_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 122 | |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 123 | // Record resolve string. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 124 | void RecordResolveString(ObjPtr<mirror::DexCache> dex_cache, dex::StringIndex string_idx) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 125 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 126 | |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 127 | // Record resolve method type. |
| 128 | void RecordResolveMethodType(ObjPtr<mirror::DexCache> dex_cache, dex::ProtoIndex proto_idx) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 129 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 130 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 131 | // Abort transaction by undoing all recorded changes. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 132 | void Rollback() |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 133 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 134 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 135 | void VisitRoots(RootVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 136 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 137 | |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 138 | bool ReadConstraint(ObjPtr<mirror::Object> obj) const |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 139 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 140 | |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 141 | bool WriteConstraint(ObjPtr<mirror::Object> obj) const |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 142 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 143 | |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 144 | bool WriteValueConstraint(ObjPtr<mirror::Object> value) const |
Vladimir Marko | 149cdda | 2019-11-12 15:02:51 +0000 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 146 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 147 | private: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 148 | class ObjectLog : public ValueObject { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 149 | public: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 150 | void LogBooleanValue(MemberOffset offset, uint8_t value, bool is_volatile); |
| 151 | void LogByteValue(MemberOffset offset, int8_t value, bool is_volatile); |
| 152 | void LogCharValue(MemberOffset offset, uint16_t value, bool is_volatile); |
| 153 | void LogShortValue(MemberOffset offset, int16_t value, bool is_volatile); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 154 | void Log32BitsValue(MemberOffset offset, uint32_t value, bool is_volatile); |
| 155 | void Log64BitsValue(MemberOffset offset, uint64_t value, bool is_volatile); |
| 156 | void LogReferenceValue(MemberOffset offset, mirror::Object* obj, bool is_volatile); |
| 157 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 158 | void Undo(mirror::Object* obj) const REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 159 | void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 160 | |
| 161 | size_t Size() const { |
| 162 | return field_values_.size(); |
| 163 | } |
| 164 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 165 | explicit ObjectLog(ScopedArenaAllocator* allocator) |
| 166 | : field_values_(std::less<uint32_t>(), allocator->Adapter(kArenaAllocTransaction)) {} |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 167 | ObjectLog(ObjectLog&& log) = default; |
| 168 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 169 | private: |
| 170 | enum FieldValueKind { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 171 | kBoolean, |
| 172 | kByte, |
| 173 | kChar, |
| 174 | kShort, |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 175 | k32Bits, |
| 176 | k64Bits, |
| 177 | kReference |
| 178 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 179 | struct FieldValue : public ValueObject { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 180 | // TODO use JValue instead ? |
| 181 | uint64_t value; |
| 182 | FieldValueKind kind; |
| 183 | bool is_volatile; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 184 | |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 185 | FieldValue() : value(0), kind(FieldValueKind::kBoolean), is_volatile(false) {} |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 186 | FieldValue(FieldValue&& log) = default; |
| 187 | |
| 188 | private: |
| 189 | DISALLOW_COPY_AND_ASSIGN(FieldValue); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 192 | void LogValue(FieldValueKind kind, MemberOffset offset, uint64_t value, bool is_volatile); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 193 | void UndoFieldWrite(mirror::Object* obj, |
| 194 | MemberOffset field_offset, |
| 195 | const FieldValue& field_value) const REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 196 | |
| 197 | // Maps field's offset to its value. |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 198 | ScopedArenaSafeMap<uint32_t, FieldValue> field_values_; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 199 | |
| 200 | DISALLOW_COPY_AND_ASSIGN(ObjectLog); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 201 | }; |
| 202 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 203 | class ArrayLog : public ValueObject { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 204 | public: |
| 205 | void LogValue(size_t index, uint64_t value); |
| 206 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 207 | void Undo(mirror::Array* obj) const REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 208 | |
| 209 | size_t Size() const { |
| 210 | return array_values_.size(); |
| 211 | } |
| 212 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 213 | explicit ArrayLog(ScopedArenaAllocator* allocator) |
| 214 | : array_values_(std::less<size_t>(), allocator->Adapter(kArenaAllocTransaction)) {} |
| 215 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 216 | ArrayLog(ArrayLog&& log) = default; |
| 217 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 218 | private: |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 219 | void UndoArrayWrite(mirror::Array* array, |
| 220 | Primitive::Type array_type, |
| 221 | size_t index, |
| 222 | uint64_t value) const REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 223 | |
| 224 | // Maps index to value. |
| 225 | // TODO use JValue instead ? |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 226 | ScopedArenaSafeMap<size_t, uint64_t> array_values_; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 227 | |
| 228 | DISALLOW_COPY_AND_ASSIGN(ArrayLog); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 229 | }; |
| 230 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 231 | class InternStringLog : public ValueObject { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 232 | public: |
| 233 | enum StringKind { |
| 234 | kStrongString, |
| 235 | kWeakString |
| 236 | }; |
| 237 | enum StringOp { |
| 238 | kInsert, |
| 239 | kRemove |
| 240 | }; |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 241 | InternStringLog(ObjPtr<mirror::String> s, StringKind kind, StringOp op); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 242 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 243 | void Undo(InternTable* intern_table) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 244 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 245 | REQUIRES(Locks::intern_table_lock_); |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 246 | void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 247 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 248 | InternStringLog() = default; |
| 249 | InternStringLog(InternStringLog&& log) = default; |
| 250 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 251 | private: |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 252 | mutable GcRoot<mirror::String> str_; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 253 | const StringKind string_kind_; |
| 254 | const StringOp string_op_; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 255 | |
| 256 | DISALLOW_COPY_AND_ASSIGN(InternStringLog); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 257 | }; |
| 258 | |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 259 | class ResolveStringLog : public ValueObject { |
| 260 | public: |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 261 | ResolveStringLog(ObjPtr<mirror::DexCache> dex_cache, dex::StringIndex string_idx); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 262 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 263 | void Undo() const REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 264 | |
| 265 | void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
| 266 | |
| 267 | private: |
| 268 | GcRoot<mirror::DexCache> dex_cache_; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 269 | const dex::StringIndex string_idx_; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 270 | |
| 271 | DISALLOW_COPY_AND_ASSIGN(ResolveStringLog); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 274 | class ResolveMethodTypeLog : public ValueObject { |
| 275 | public: |
| 276 | ResolveMethodTypeLog(ObjPtr<mirror::DexCache> dex_cache, dex::ProtoIndex proto_idx); |
| 277 | |
| 278 | void Undo() const REQUIRES_SHARED(Locks::mutator_lock_); |
| 279 | |
| 280 | void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
| 281 | |
| 282 | private: |
| 283 | GcRoot<mirror::DexCache> dex_cache_; |
| 284 | const dex::ProtoIndex proto_idx_; |
| 285 | |
| 286 | DISALLOW_COPY_AND_ASSIGN(ResolveMethodTypeLog); |
| 287 | }; |
| 288 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 289 | void LogInternedString(InternStringLog&& log) |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 290 | REQUIRES(Locks::intern_table_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 291 | |
| 292 | void UndoObjectModifications() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 293 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 294 | void UndoArrayModifications() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 295 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 296 | void UndoInternStringTableModifications() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 297 | REQUIRES(Locks::intern_table_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 298 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 299 | void UndoResolveStringModifications() |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 300 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 301 | void UndoResolveMethodTypeModifications() |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 302 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 303 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 304 | void VisitObjectLogs(RootVisitor* visitor, ArenaStack* arena_stack) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 305 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 306 | void VisitArrayLogs(RootVisitor* visitor, ArenaStack* arena_stack) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 307 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 308 | void VisitInternStringLogs(RootVisitor* visitor) |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 309 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 310 | void VisitResolveStringLogs(RootVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 311 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 312 | void VisitResolveMethodTypeLogs(RootVisitor* visitor) |
Vladimir Marko | e9a4a60 | 2021-06-22 16:33:34 +0100 | [diff] [blame] | 313 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 314 | |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 315 | const std::string& GetAbortMessage() const; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 316 | |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 317 | ObjectLog& GetOrCreateObjectLog(mirror::Object* obj); |
| 318 | |
| 319 | // The top-level transaction creates an `ArenaStack` which is then |
| 320 | // passed down to nested transactions. |
| 321 | std::optional<ArenaStack> arena_stack_; |
| 322 | // The allocator uses the `ArenaStack` from the top-level transaction. |
| 323 | ScopedArenaAllocator allocator_; |
| 324 | |
| 325 | ScopedArenaSafeMap<mirror::Object*, ObjectLog> object_logs_; |
Roland Levillain | 4f62327 | 2021-07-07 16:32:26 +0100 | [diff] [blame] | 326 | ScopedArenaSafeMap<mirror::Array*, ArrayLog> array_logs_; |
Vladimir Marko | b6e6792 | 2021-07-06 15:53:07 +0100 | [diff] [blame] | 327 | ScopedArenaForwardList<InternStringLog> intern_string_logs_; |
| 328 | ScopedArenaForwardList<ResolveStringLog> resolve_string_logs_; |
| 329 | ScopedArenaForwardList<ResolveMethodTypeLog> resolve_method_type_logs_; |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 330 | bool aborted_; |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 331 | bool rolling_back_; // Single thread, no race. |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 332 | gc::Heap* const heap_; |
Vladimir Marko | 4d7b689 | 2020-01-16 17:06:35 +0000 | [diff] [blame] | 333 | const bool strict_; |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 334 | std::string abort_message_; |
| 335 | mirror::Class* root_; |
| 336 | const char* assert_no_new_records_reason_; |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 337 | |
| 338 | friend class ScopedAssertNoNewTransactionRecords; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 339 | |
| 340 | DISALLOW_COPY_AND_ASSIGN(Transaction); |
| 341 | }; |
| 342 | |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 343 | class ScopedAssertNoNewTransactionRecords { |
| 344 | public: |
| 345 | explicit ScopedAssertNoNewTransactionRecords(const char* reason) |
| 346 | : transaction_(kIsDebugBuild ? InstallAssertion(reason) : nullptr) {} |
| 347 | |
| 348 | ~ScopedAssertNoNewTransactionRecords() { |
| 349 | if (kIsDebugBuild && transaction_ != nullptr) { |
| 350 | RemoveAssertion(transaction_); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | static Transaction* InstallAssertion(const char* reason); |
| 356 | static void RemoveAssertion(Transaction* transaction); |
| 357 | |
| 358 | Transaction* transaction_; |
| 359 | }; |
| 360 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 361 | } // namespace art |
| 362 | |
| 363 | #endif // ART_RUNTIME_TRANSACTION_H_ |