| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [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_INDIRECT_REFERENCE_TABLE_INL_H_ |
| 18 | #define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_ |
| 19 | |
| 20 | #include "indirect_reference_table.h" |
| 21 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
| 23 | |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 24 | #include "base/dumpable.h" |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "gc_root-inl.h" |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 26 | #include "obj_ptr-inl.h" |
| Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 27 | #include "verify_object.h" |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace mirror { |
| 31 | class Object; |
| 32 | } // namespace mirror |
| 33 | |
| 34 | // Verifies that the indirect table lookup is valid. |
| 35 | // Returns "false" if something looks bad. |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 36 | inline bool IndirectReferenceTable::IsValidReference(IndirectRef iref, |
| 37 | /*out*/std::string* error_msg) const { |
| 38 | DCHECK(iref != nullptr); |
| 39 | DCHECK_EQ(GetIndirectRefKind(iref), kind_); |
| Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 40 | const uint32_t top_index = segment_state_.top_index; |
| 41 | uint32_t idx = ExtractIndex(iref); |
| 42 | if (UNLIKELY(idx >= top_index)) { |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 43 | *error_msg = android::base::StringPrintf("deleted reference at index %u in a table of size %u", |
| 44 | idx, |
| 45 | top_index); |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 46 | return false; |
| 47 | } |
| Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 48 | if (UNLIKELY(table_[idx].GetReference()->IsNull())) { |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 49 | *error_msg = android::base::StringPrintf("deleted reference at index %u", idx); |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 52 | uint32_t iref_serial = DecodeSerial(reinterpret_cast<uintptr_t>(iref)); |
| 53 | uint32_t entry_serial = table_[idx].GetSerial(); |
| 54 | if (UNLIKELY(iref_serial != entry_serial)) { |
| 55 | *error_msg = android::base::StringPrintf("stale reference with serial number %u v. current %u", |
| 56 | iref_serial, |
| 57 | entry_serial); |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | // Make sure that the entry at "idx" is correctly paired with "iref". |
| Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 64 | inline bool IndirectReferenceTable::CheckEntry(const char* what, |
| 65 | IndirectRef iref, |
| 66 | uint32_t idx) const { |
| Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 67 | IndirectRef checkRef = ToIndirectRef(idx); |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 68 | if (UNLIKELY(checkRef != iref)) { |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 69 | std::string msg = android::base::StringPrintf( |
| Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 70 | "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)", |
| 71 | what, |
| 72 | GetIndirectRefKindString(kind_), |
| 73 | iref, |
| 74 | checkRef); |
| 75 | AbortIfNoCheckJNI(msg); |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 81 | template<ReadBarrierOption kReadBarrierOption> |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 82 | inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const { |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 83 | DCHECK_EQ(GetIndirectRefKind(iref), kind_); |
| Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 84 | uint32_t idx = ExtractIndex(iref); |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 85 | DCHECK_LT(idx, segment_state_.top_index); |
| 86 | DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial()); |
| 87 | DCHECK(!table_[idx].GetReference()->IsNull()); |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 88 | ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>(); |
| Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 89 | VerifyObject(obj); |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 90 | return obj; |
| 91 | } |
| 92 | |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 93 | inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) { |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 94 | DCHECK_EQ(GetIndirectRefKind(iref), kind_); |
| Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 95 | uint32_t idx = ExtractIndex(iref); |
| Vladimir Marko | 17491ac | 2020-12-01 12:02:29 +0000 | [diff] [blame] | 96 | DCHECK_LT(idx, segment_state_.top_index); |
| 97 | DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial()); |
| 98 | DCHECK(!table_[idx].GetReference()->IsNull()); |
| Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 99 | table_[idx].SetReference(obj); |
| 100 | } |
| 101 | |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 102 | inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) { |
| 103 | ++serial_; |
| 104 | if (serial_ == kIRTPrevCount) { |
| 105 | serial_ = 0; |
| 106 | } |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 107 | references_[serial_] = GcRoot<mirror::Object>(obj); |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) { |
| 111 | DCHECK_LT(serial_, kIRTPrevCount); |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 112 | references_[serial_] = GcRoot<mirror::Object>(obj); |
| Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 115 | } // namespace art |
| 116 | |
| 117 | #endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_ |