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 | #include "transaction.h" |
| 18 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 21 | #include "base/mutex-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 23 | #include "gc/accounting/card_table-inl.h" |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 24 | #include "gc/heap.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 25 | #include "gc_root-inl.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 26 | #include "intern_table.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 27 | #include "mirror/class-inl.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 28 | #include "mirror/dex_cache-inl.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
| 30 | #include "mirror/object_array-inl.h" |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 31 | #include "obj_ptr-inl.h" |
| 32 | #include "runtime.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 33 | |
| 34 | #include <list> |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | // TODO: remove (only used for debugging purpose). |
| 39 | static constexpr bool kEnableTransactionStats = false; |
| 40 | |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 41 | Transaction::Transaction(bool strict, mirror::Class* root) |
| 42 | : log_lock_("transaction log lock", kTransactionLogLock), |
| 43 | aborted_(false), |
| 44 | rolling_back_(false), |
| 45 | heap_(strict ? nullptr : Runtime::Current()->GetHeap()), |
| 46 | root_(root) { |
| 47 | DCHECK_EQ(strict, IsStrict()); |
| 48 | DCHECK(Runtime::Current()->IsAotCompiler()); |
Chang Xing | cade5c3 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 51 | Transaction::~Transaction() { |
| 52 | if (kEnableTransactionStats) { |
| 53 | MutexLock mu(Thread::Current(), log_lock_); |
| 54 | size_t objects_count = object_logs_.size(); |
| 55 | size_t field_values_count = 0; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 56 | for (const auto& it : object_logs_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 57 | field_values_count += it.second.Size(); |
| 58 | } |
| 59 | size_t array_count = array_logs_.size(); |
| 60 | size_t array_values_count = 0; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 61 | for (const auto& it : array_logs_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 62 | array_values_count += it.second.Size(); |
| 63 | } |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 64 | size_t intern_string_count = intern_string_logs_.size(); |
| 65 | size_t resolve_string_count = resolve_string_logs_.size(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 66 | LOG(INFO) << "Transaction::~Transaction" |
| 67 | << ": objects_count=" << objects_count |
| 68 | << ", field_values_count=" << field_values_count |
| 69 | << ", array_count=" << array_count |
| 70 | << ", array_values_count=" << array_values_count |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 71 | << ", intern_string_count=" << intern_string_count |
| 72 | << ", resolve_string_count=" << resolve_string_count; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 76 | void Transaction::Abort(const std::string& abort_message) { |
| 77 | MutexLock mu(Thread::Current(), log_lock_); |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 78 | // We may abort more than once if the exception thrown at the time of the |
| 79 | // previous abort has been caught during execution of a class initializer. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 80 | // We just keep the message of the first abort because it will cause the |
| 81 | // transaction to be rolled back anyway. |
| 82 | if (!aborted_) { |
| 83 | aborted_ = true; |
| 84 | abort_message_ = abort_message; |
| 85 | } |
| 86 | } |
| 87 | |
Sebastien Hertz | b81e1cd | 2015-04-28 12:31:41 +0200 | [diff] [blame] | 88 | void Transaction::ThrowAbortError(Thread* self, const std::string* abort_message) { |
| 89 | const bool rethrow = (abort_message == nullptr); |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 90 | if (kIsDebugBuild && rethrow) { |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 91 | CHECK(IsAborted()) << "Rethrow " << Transaction::kAbortExceptionDescriptor |
| 92 | << " while transaction is not aborted"; |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 93 | } |
Sebastien Hertz | b81e1cd | 2015-04-28 12:31:41 +0200 | [diff] [blame] | 94 | if (rethrow) { |
| 95 | // Rethrow an exception with the earlier abort message stored in the transaction. |
| 96 | self->ThrowNewWrappedException(Transaction::kAbortExceptionSignature, |
| 97 | GetAbortMessage().c_str()); |
| 98 | } else { |
| 99 | // Throw an exception with the given abort message. |
| 100 | self->ThrowNewWrappedException(Transaction::kAbortExceptionSignature, |
| 101 | abort_message->c_str()); |
| 102 | } |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool Transaction::IsAborted() { |
| 106 | MutexLock mu(Thread::Current(), log_lock_); |
| 107 | return aborted_; |
| 108 | } |
| 109 | |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 110 | bool Transaction::IsRollingBack() { |
| 111 | return rolling_back_; |
| 112 | } |
| 113 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 114 | const std::string& Transaction::GetAbortMessage() { |
| 115 | MutexLock mu(Thread::Current(), log_lock_); |
| 116 | return abort_message_; |
| 117 | } |
| 118 | |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 119 | bool Transaction::WriteConstraint(Thread* self, ObjPtr<mirror::Object> obj, ArtField* field) { |
| 120 | MutexLock mu(self, log_lock_); |
| 121 | if (IsStrict()) { |
| 122 | return field->IsStatic() && // no constraint instance updating |
| 123 | obj != root_; // modifying other classes' static field, fail |
| 124 | } else { |
| 125 | // For boot image extension, prevent changes in boot image. |
| 126 | // For boot image there are no boot image spaces and this returns false. |
| 127 | return heap_->ObjectIsInBootImageSpace(obj); |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 128 | } |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 131 | bool Transaction::ReadConstraint(Thread* self, ObjPtr<mirror::Object> obj, ArtField* field) { |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 132 | DCHECK(field->IsStatic()); |
| 133 | DCHECK(obj->IsClass()); |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 134 | MutexLock mu(self, log_lock_); |
| 135 | if (IsStrict()) { |
| 136 | return obj != root_; // fail if not self-updating |
| 137 | } else { |
| 138 | // For boot image and boot image extension, allow reading any field. |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 139 | return false; |
| 140 | } |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 143 | void Transaction::RecordWriteFieldBoolean(mirror::Object* obj, |
| 144 | MemberOffset field_offset, |
| 145 | uint8_t value, |
| 146 | bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 147 | DCHECK(obj != nullptr); |
| 148 | MutexLock mu(Thread::Current(), log_lock_); |
| 149 | ObjectLog& object_log = object_logs_[obj]; |
| 150 | object_log.LogBooleanValue(field_offset, value, is_volatile); |
| 151 | } |
| 152 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 153 | void Transaction::RecordWriteFieldByte(mirror::Object* obj, |
| 154 | MemberOffset field_offset, |
| 155 | int8_t value, |
| 156 | bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 157 | DCHECK(obj != nullptr); |
| 158 | MutexLock mu(Thread::Current(), log_lock_); |
| 159 | ObjectLog& object_log = object_logs_[obj]; |
| 160 | object_log.LogByteValue(field_offset, value, is_volatile); |
| 161 | } |
| 162 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 163 | void Transaction::RecordWriteFieldChar(mirror::Object* obj, |
| 164 | MemberOffset field_offset, |
| 165 | uint16_t value, |
| 166 | bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 167 | DCHECK(obj != nullptr); |
| 168 | MutexLock mu(Thread::Current(), log_lock_); |
| 169 | ObjectLog& object_log = object_logs_[obj]; |
| 170 | object_log.LogCharValue(field_offset, value, is_volatile); |
| 171 | } |
| 172 | |
| 173 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 174 | void Transaction::RecordWriteFieldShort(mirror::Object* obj, |
| 175 | MemberOffset field_offset, |
| 176 | int16_t value, |
| 177 | bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 178 | DCHECK(obj != nullptr); |
| 179 | MutexLock mu(Thread::Current(), log_lock_); |
| 180 | ObjectLog& object_log = object_logs_[obj]; |
| 181 | object_log.LogShortValue(field_offset, value, is_volatile); |
| 182 | } |
| 183 | |
| 184 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 185 | void Transaction::RecordWriteField32(mirror::Object* obj, |
| 186 | MemberOffset field_offset, |
| 187 | uint32_t value, |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 188 | bool is_volatile) { |
| 189 | DCHECK(obj != nullptr); |
| 190 | MutexLock mu(Thread::Current(), log_lock_); |
| 191 | ObjectLog& object_log = object_logs_[obj]; |
| 192 | object_log.Log32BitsValue(field_offset, value, is_volatile); |
| 193 | } |
| 194 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 195 | void Transaction::RecordWriteField64(mirror::Object* obj, |
| 196 | MemberOffset field_offset, |
| 197 | uint64_t value, |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 198 | bool is_volatile) { |
| 199 | DCHECK(obj != nullptr); |
| 200 | MutexLock mu(Thread::Current(), log_lock_); |
| 201 | ObjectLog& object_log = object_logs_[obj]; |
| 202 | object_log.Log64BitsValue(field_offset, value, is_volatile); |
| 203 | } |
| 204 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 205 | void Transaction::RecordWriteFieldReference(mirror::Object* obj, |
| 206 | MemberOffset field_offset, |
| 207 | mirror::Object* value, |
| 208 | bool is_volatile) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 209 | DCHECK(obj != nullptr); |
| 210 | MutexLock mu(Thread::Current(), log_lock_); |
| 211 | ObjectLog& object_log = object_logs_[obj]; |
| 212 | object_log.LogReferenceValue(field_offset, value, is_volatile); |
| 213 | } |
| 214 | |
| 215 | void Transaction::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) { |
| 216 | DCHECK(array != nullptr); |
| 217 | DCHECK(array->IsArrayInstance()); |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 218 | DCHECK(!array->IsObjectArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 219 | MutexLock mu(Thread::Current(), log_lock_); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 220 | auto it = array_logs_.find(array); |
| 221 | if (it == array_logs_.end()) { |
| 222 | ArrayLog log; |
| 223 | it = array_logs_.emplace(array, std::move(log)).first; |
| 224 | } |
| 225 | it->second.LogValue(index, value); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 228 | void Transaction::RecordResolveString(ObjPtr<mirror::DexCache> dex_cache, |
| 229 | dex::StringIndex string_idx) { |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 230 | DCHECK(dex_cache != nullptr); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 231 | DCHECK_LT(string_idx.index_, dex_cache->GetDexFile()->NumStringIds()); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 232 | MutexLock mu(Thread::Current(), log_lock_); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 233 | resolve_string_logs_.emplace_back(dex_cache, string_idx); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 236 | void Transaction::RecordStrongStringInsertion(ObjPtr<mirror::String> s) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 237 | InternStringLog log(s, InternStringLog::kStrongString, InternStringLog::kInsert); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 238 | LogInternedString(std::move(log)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 241 | void Transaction::RecordWeakStringInsertion(ObjPtr<mirror::String> s) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 242 | InternStringLog log(s, InternStringLog::kWeakString, InternStringLog::kInsert); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 243 | LogInternedString(std::move(log)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 246 | void Transaction::RecordStrongStringRemoval(ObjPtr<mirror::String> s) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 247 | InternStringLog log(s, InternStringLog::kStrongString, InternStringLog::kRemove); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 248 | LogInternedString(std::move(log)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 251 | void Transaction::RecordWeakStringRemoval(ObjPtr<mirror::String> s) { |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 252 | InternStringLog log(s, InternStringLog::kWeakString, InternStringLog::kRemove); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 253 | LogInternedString(std::move(log)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 256 | void Transaction::LogInternedString(InternStringLog&& log) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 257 | Locks::intern_table_lock_->AssertExclusiveHeld(Thread::Current()); |
| 258 | MutexLock mu(Thread::Current(), log_lock_); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 259 | intern_string_logs_.push_front(std::move(log)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 262 | void Transaction::Rollback() { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 263 | Thread* self = Thread::Current(); |
| 264 | self->AssertNoPendingException(); |
| 265 | MutexLock mu1(self, *Locks::intern_table_lock_); |
| 266 | MutexLock mu2(self, log_lock_); |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 267 | rolling_back_ = true; |
| 268 | CHECK(!Runtime::Current()->IsActiveTransaction()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 269 | UndoObjectModifications(); |
| 270 | UndoArrayModifications(); |
| 271 | UndoInternStringTableModifications(); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 272 | UndoResolveStringModifications(); |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 273 | rolling_back_ = false; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void Transaction::UndoObjectModifications() { |
| 277 | // TODO we may not need to restore objects allocated during this transaction. Or we could directly |
| 278 | // remove them from the heap. |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 279 | for (const auto& it : object_logs_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 280 | it.second.Undo(it.first); |
| 281 | } |
| 282 | object_logs_.clear(); |
| 283 | } |
| 284 | |
| 285 | void Transaction::UndoArrayModifications() { |
| 286 | // TODO we may not need to restore array allocated during this transaction. Or we could directly |
| 287 | // remove them from the heap. |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 288 | for (const auto& it : array_logs_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 289 | it.second.Undo(it.first); |
| 290 | } |
| 291 | array_logs_.clear(); |
| 292 | } |
| 293 | |
| 294 | void Transaction::UndoInternStringTableModifications() { |
| 295 | InternTable* const intern_table = Runtime::Current()->GetInternTable(); |
| 296 | // We want to undo each operation from the most recent to the oldest. List has been filled so the |
| 297 | // most recent operation is at list begin so just have to iterate over it. |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 298 | for (const InternStringLog& string_log : intern_string_logs_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 299 | string_log.Undo(intern_table); |
| 300 | } |
| 301 | intern_string_logs_.clear(); |
| 302 | } |
| 303 | |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 304 | void Transaction::UndoResolveStringModifications() { |
| 305 | for (ResolveStringLog& string_log : resolve_string_logs_) { |
| 306 | string_log.Undo(); |
| 307 | } |
| 308 | resolve_string_logs_.clear(); |
| 309 | } |
| 310 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 311 | void Transaction::VisitRoots(RootVisitor* visitor) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 312 | MutexLock mu(Thread::Current(), log_lock_); |
Chang Xing | cade5c3 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 313 | visitor->VisitRoot(reinterpret_cast<mirror::Object**>(&root_), RootInfo(kRootUnknown)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 314 | VisitObjectLogs(visitor); |
| 315 | VisitArrayLogs(visitor); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 316 | VisitInternStringLogs(visitor); |
| 317 | VisitResolveStringLogs(visitor); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 320 | void Transaction::VisitObjectLogs(RootVisitor* visitor) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 321 | // List of moving roots. |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 322 | using ObjectPair = std::pair<mirror::Object*, mirror::Object*>; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 323 | std::list<ObjectPair> moving_roots; |
| 324 | |
| 325 | // Visit roots. |
Mathieu Chartier | 5f257b1 | 2017-02-03 14:47:36 -0800 | [diff] [blame] | 326 | for (auto& it : object_logs_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 327 | it.second.VisitRoots(visitor); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 328 | mirror::Object* old_root = it.first; |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 329 | mirror::Object* new_root = old_root; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 330 | visitor->VisitRoot(&new_root, RootInfo(kRootUnknown)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 331 | if (new_root != old_root) { |
| 332 | moving_roots.push_back(std::make_pair(old_root, new_root)); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Update object logs with moving roots. |
| 337 | for (const ObjectPair& pair : moving_roots) { |
| 338 | mirror::Object* old_root = pair.first; |
| 339 | mirror::Object* new_root = pair.second; |
| 340 | auto old_root_it = object_logs_.find(old_root); |
| 341 | CHECK(old_root_it != object_logs_.end()); |
| 342 | CHECK(object_logs_.find(new_root) == object_logs_.end()); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 343 | object_logs_.emplace(new_root, std::move(old_root_it->second)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 344 | object_logs_.erase(old_root_it); |
| 345 | } |
| 346 | } |
| 347 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 348 | void Transaction::VisitArrayLogs(RootVisitor* visitor) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 349 | // List of moving roots. |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 350 | using ArrayPair = std::pair<mirror::Array*, mirror::Array*>; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 351 | std::list<ArrayPair> moving_roots; |
| 352 | |
Mathieu Chartier | 5f257b1 | 2017-02-03 14:47:36 -0800 | [diff] [blame] | 353 | for (auto& it : array_logs_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 354 | mirror::Array* old_root = it.first; |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 355 | CHECK(!old_root->IsObjectArray()); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 356 | mirror::Array* new_root = old_root; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 357 | visitor->VisitRoot(reinterpret_cast<mirror::Object**>(&new_root), RootInfo(kRootUnknown)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 358 | if (new_root != old_root) { |
| 359 | moving_roots.push_back(std::make_pair(old_root, new_root)); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Update array logs with moving roots. |
| 364 | for (const ArrayPair& pair : moving_roots) { |
| 365 | mirror::Array* old_root = pair.first; |
| 366 | mirror::Array* new_root = pair.second; |
| 367 | auto old_root_it = array_logs_.find(old_root); |
| 368 | CHECK(old_root_it != array_logs_.end()); |
| 369 | CHECK(array_logs_.find(new_root) == array_logs_.end()); |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 370 | array_logs_.emplace(new_root, std::move(old_root_it->second)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 371 | array_logs_.erase(old_root_it); |
| 372 | } |
| 373 | } |
| 374 | |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 375 | void Transaction::VisitInternStringLogs(RootVisitor* visitor) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 376 | for (InternStringLog& log : intern_string_logs_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 377 | log.VisitRoots(visitor); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 381 | void Transaction::VisitResolveStringLogs(RootVisitor* visitor) { |
| 382 | for (ResolveStringLog& log : resolve_string_logs_) { |
| 383 | log.VisitRoots(visitor); |
| 384 | } |
| 385 | } |
| 386 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 387 | void Transaction::ObjectLog::LogBooleanValue(MemberOffset offset, uint8_t value, bool is_volatile) { |
| 388 | LogValue(ObjectLog::kBoolean, offset, value, is_volatile); |
| 389 | } |
| 390 | |
| 391 | void Transaction::ObjectLog::LogByteValue(MemberOffset offset, int8_t value, bool is_volatile) { |
| 392 | LogValue(ObjectLog::kByte, offset, value, is_volatile); |
| 393 | } |
| 394 | |
| 395 | void Transaction::ObjectLog::LogCharValue(MemberOffset offset, uint16_t value, bool is_volatile) { |
| 396 | LogValue(ObjectLog::kChar, offset, value, is_volatile); |
| 397 | } |
| 398 | |
| 399 | void Transaction::ObjectLog::LogShortValue(MemberOffset offset, int16_t value, bool is_volatile) { |
| 400 | LogValue(ObjectLog::kShort, offset, value, is_volatile); |
| 401 | } |
| 402 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 403 | void Transaction::ObjectLog::Log32BitsValue(MemberOffset offset, uint32_t value, bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 404 | LogValue(ObjectLog::k32Bits, offset, value, is_volatile); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void Transaction::ObjectLog::Log64BitsValue(MemberOffset offset, uint64_t value, bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 408 | LogValue(ObjectLog::k64Bits, offset, value, is_volatile); |
| 409 | } |
| 410 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 411 | void Transaction::ObjectLog::LogReferenceValue(MemberOffset offset, |
| 412 | mirror::Object* obj, |
| 413 | bool is_volatile) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 414 | LogValue(ObjectLog::kReference, offset, reinterpret_cast<uintptr_t>(obj), is_volatile); |
| 415 | } |
| 416 | |
| 417 | void Transaction::ObjectLog::LogValue(ObjectLog::FieldValueKind kind, |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 418 | MemberOffset offset, |
| 419 | uint64_t value, |
| 420 | bool is_volatile) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 421 | auto it = field_values_.find(offset.Uint32Value()); |
| 422 | if (it == field_values_.end()) { |
| 423 | ObjectLog::FieldValue field_value; |
| 424 | field_value.value = value; |
| 425 | field_value.is_volatile = is_volatile; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 426 | field_value.kind = kind; |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 427 | field_values_.emplace(offset.Uint32Value(), std::move(field_value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 431 | void Transaction::ObjectLog::Undo(mirror::Object* obj) const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 432 | for (auto& it : field_values_) { |
| 433 | // Garbage collector needs to access object's class and array's length. So we don't rollback |
| 434 | // these values. |
| 435 | MemberOffset field_offset(it.first); |
| 436 | if (field_offset.Uint32Value() == mirror::Class::ClassOffset().Uint32Value()) { |
| 437 | // Skip Object::class field. |
| 438 | continue; |
| 439 | } |
| 440 | if (obj->IsArrayInstance() && |
| 441 | field_offset.Uint32Value() == mirror::Array::LengthOffset().Uint32Value()) { |
| 442 | // Skip Array::length field. |
| 443 | continue; |
| 444 | } |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 445 | const FieldValue& field_value = it.second; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 446 | UndoFieldWrite(obj, field_offset, field_value); |
| 447 | } |
| 448 | } |
| 449 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 450 | void Transaction::ObjectLog::UndoFieldWrite(mirror::Object* obj, |
| 451 | MemberOffset field_offset, |
| 452 | const FieldValue& field_value) const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 453 | // TODO We may want to abort a transaction while still being in transaction mode. In this case, |
| 454 | // we'd need to disable the check. |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 455 | constexpr bool kCheckTransaction = false; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 456 | switch (field_value.kind) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 457 | case kBoolean: |
| 458 | if (UNLIKELY(field_value.is_volatile)) { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 459 | obj->SetFieldBooleanVolatile<false, kCheckTransaction>( |
| 460 | field_offset, |
Andreas Gampe | 7c5acbb | 2018-09-20 13:54:52 -0700 | [diff] [blame] | 461 | field_value.value); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 462 | } else { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 463 | obj->SetFieldBoolean<false, kCheckTransaction>( |
| 464 | field_offset, |
Andreas Gampe | 7c5acbb | 2018-09-20 13:54:52 -0700 | [diff] [blame] | 465 | field_value.value); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 466 | } |
| 467 | break; |
| 468 | case kByte: |
| 469 | if (UNLIKELY(field_value.is_volatile)) { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 470 | obj->SetFieldByteVolatile<false, kCheckTransaction>( |
| 471 | field_offset, |
| 472 | static_cast<int8_t>(field_value.value)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 473 | } else { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 474 | obj->SetFieldByte<false, kCheckTransaction>( |
| 475 | field_offset, |
| 476 | static_cast<int8_t>(field_value.value)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 477 | } |
| 478 | break; |
| 479 | case kChar: |
| 480 | if (UNLIKELY(field_value.is_volatile)) { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 481 | obj->SetFieldCharVolatile<false, kCheckTransaction>( |
| 482 | field_offset, |
| 483 | static_cast<uint16_t>(field_value.value)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 484 | } else { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 485 | obj->SetFieldChar<false, kCheckTransaction>( |
| 486 | field_offset, |
| 487 | static_cast<uint16_t>(field_value.value)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 488 | } |
| 489 | break; |
| 490 | case kShort: |
| 491 | if (UNLIKELY(field_value.is_volatile)) { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 492 | obj->SetFieldShortVolatile<false, kCheckTransaction>( |
| 493 | field_offset, |
| 494 | static_cast<int16_t>(field_value.value)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 495 | } else { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 496 | obj->SetFieldShort<false, kCheckTransaction>( |
| 497 | field_offset, |
| 498 | static_cast<int16_t>(field_value.value)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 499 | } |
| 500 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 501 | case k32Bits: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 502 | if (UNLIKELY(field_value.is_volatile)) { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 503 | obj->SetField32Volatile<false, kCheckTransaction>( |
| 504 | field_offset, |
| 505 | static_cast<uint32_t>(field_value.value)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 506 | } else { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 507 | obj->SetField32<false, kCheckTransaction>( |
| 508 | field_offset, |
| 509 | static_cast<uint32_t>(field_value.value)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 510 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 511 | break; |
| 512 | case k64Bits: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 513 | if (UNLIKELY(field_value.is_volatile)) { |
| 514 | obj->SetField64Volatile<false, kCheckTransaction>(field_offset, field_value.value); |
| 515 | } else { |
| 516 | obj->SetField64<false, kCheckTransaction>(field_offset, field_value.value); |
| 517 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 518 | break; |
| 519 | case kReference: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 520 | if (UNLIKELY(field_value.is_volatile)) { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 521 | obj->SetFieldObjectVolatile<false, kCheckTransaction>( |
| 522 | field_offset, |
| 523 | reinterpret_cast<mirror::Object*>(field_value.value)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 524 | } else { |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 525 | obj->SetFieldObject<false, kCheckTransaction>( |
| 526 | field_offset, |
| 527 | reinterpret_cast<mirror::Object*>(field_value.value)); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 528 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 529 | break; |
| 530 | default: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 531 | LOG(FATAL) << "Unknown value kind " << static_cast<int>(field_value.kind); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 532 | UNREACHABLE(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 536 | void Transaction::ObjectLog::VisitRoots(RootVisitor* visitor) { |
Mathieu Chartier | 5f257b1 | 2017-02-03 14:47:36 -0800 | [diff] [blame] | 537 | for (auto& it : field_values_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 538 | FieldValue& field_value = it.second; |
| 539 | if (field_value.kind == ObjectLog::kReference) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 540 | visitor->VisitRootIfNonNull(reinterpret_cast<mirror::Object**>(&field_value.value), |
| 541 | RootInfo(kRootUnknown)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 546 | void Transaction::InternStringLog::Undo(InternTable* intern_table) const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 547 | DCHECK(intern_table != nullptr); |
| 548 | switch (string_op_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 549 | case InternStringLog::kInsert: { |
| 550 | switch (string_kind_) { |
| 551 | case InternStringLog::kStrongString: |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 552 | intern_table->RemoveStrongFromTransaction(str_.Read()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 553 | break; |
| 554 | case InternStringLog::kWeakString: |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 555 | intern_table->RemoveWeakFromTransaction(str_.Read()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 556 | break; |
| 557 | default: |
| 558 | LOG(FATAL) << "Unknown interned string kind"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 559 | UNREACHABLE(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 560 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 561 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 562 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 563 | case InternStringLog::kRemove: { |
| 564 | switch (string_kind_) { |
| 565 | case InternStringLog::kStrongString: |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 566 | intern_table->InsertStrongFromTransaction(str_.Read()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 567 | break; |
| 568 | case InternStringLog::kWeakString: |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 569 | intern_table->InsertWeakFromTransaction(str_.Read()); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 570 | break; |
| 571 | default: |
| 572 | LOG(FATAL) << "Unknown interned string kind"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 573 | UNREACHABLE(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 574 | } |
| 575 | break; |
| 576 | } |
| 577 | default: |
| 578 | LOG(FATAL) << "Unknown interned string op"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 579 | UNREACHABLE(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 580 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 583 | void Transaction::InternStringLog::VisitRoots(RootVisitor* visitor) { |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 584 | str_.VisitRoot(visitor, RootInfo(kRootInternedString)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 587 | void Transaction::ResolveStringLog::Undo() const { |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 588 | dex_cache_.Read()->ClearString(string_idx_); |
| 589 | } |
| 590 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 591 | Transaction::ResolveStringLog::ResolveStringLog(ObjPtr<mirror::DexCache> dex_cache, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 592 | dex::StringIndex string_idx) |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 593 | : dex_cache_(dex_cache), |
| 594 | string_idx_(string_idx) { |
| 595 | DCHECK(dex_cache != nullptr); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 596 | DCHECK_LT(string_idx_.index_, dex_cache->GetDexFile()->NumStringIds()); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void Transaction::ResolveStringLog::VisitRoots(RootVisitor* visitor) { |
| 600 | dex_cache_.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 601 | } |
| 602 | |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 603 | Transaction::InternStringLog::InternStringLog(ObjPtr<mirror::String> s, |
| 604 | StringKind kind, |
| 605 | StringOp op) |
| 606 | : str_(s), |
| 607 | string_kind_(kind), |
| 608 | string_op_(op) { |
| 609 | DCHECK(s != nullptr); |
| 610 | } |
| 611 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 612 | void Transaction::ArrayLog::LogValue(size_t index, uint64_t value) { |
| 613 | auto it = array_values_.find(index); |
| 614 | if (it == array_values_.end()) { |
| 615 | array_values_.insert(std::make_pair(index, value)); |
| 616 | } |
| 617 | } |
| 618 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 619 | void Transaction::ArrayLog::Undo(mirror::Array* array) const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 620 | DCHECK(array != nullptr); |
| 621 | DCHECK(array->IsArrayInstance()); |
| 622 | Primitive::Type type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 623 | for (auto it : array_values_) { |
| 624 | UndoArrayWrite(array, type, it.first, it.second); |
| 625 | } |
| 626 | } |
| 627 | |
Mathieu Chartier | 38e954c | 2017-02-03 16:06:35 -0800 | [diff] [blame] | 628 | void Transaction::ArrayLog::UndoArrayWrite(mirror::Array* array, |
| 629 | Primitive::Type array_type, |
| 630 | size_t index, |
| 631 | uint64_t value) const { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 632 | // TODO We may want to abort a transaction while still being in transaction mode. In this case, |
| 633 | // we'd need to disable the check. |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 634 | constexpr bool kCheckTransaction = false; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 635 | switch (array_type) { |
| 636 | case Primitive::kPrimBoolean: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 637 | array->AsBooleanArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 638 | index, static_cast<uint8_t>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 639 | break; |
| 640 | case Primitive::kPrimByte: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 641 | array->AsByteArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 642 | index, static_cast<int8_t>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 643 | break; |
| 644 | case Primitive::kPrimChar: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 645 | array->AsCharArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 646 | index, static_cast<uint16_t>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 647 | break; |
| 648 | case Primitive::kPrimShort: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 649 | array->AsShortArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 650 | index, static_cast<int16_t>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 651 | break; |
| 652 | case Primitive::kPrimInt: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 653 | array->AsIntArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 654 | index, static_cast<int32_t>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 655 | break; |
| 656 | case Primitive::kPrimFloat: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 657 | array->AsFloatArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 658 | index, static_cast<float>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 659 | break; |
| 660 | case Primitive::kPrimLong: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 661 | array->AsLongArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 662 | index, static_cast<int64_t>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 663 | break; |
| 664 | case Primitive::kPrimDouble: |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 665 | array->AsDoubleArray()->SetWithoutChecks<false, kCheckTransaction>( |
| 666 | index, static_cast<double>(value)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 667 | break; |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 668 | case Primitive::kPrimNot: |
| 669 | LOG(FATAL) << "ObjectArray should be treated as Object"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 670 | UNREACHABLE(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 671 | default: |
| 672 | LOG(FATAL) << "Unsupported type " << array_type; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 673 | UNREACHABLE(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 677 | } // namespace art |