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. |
| 36 | inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const { |
| 37 | if (UNLIKELY(iref == nullptr)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 38 | LOG(WARNING) << "Attempt to look up nullptr " << kind_; |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 39 | return false; |
| 40 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 41 | if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 42 | AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): invalid %s %p", |
| 43 | GetIndirectRefKindString(kind_), |
| 44 | iref)); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 45 | return false; |
| 46 | } |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 47 | const uint32_t top_index = segment_state_.top_index; |
| 48 | uint32_t idx = ExtractIndex(iref); |
| 49 | if (UNLIKELY(idx >= top_index)) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 50 | std::string msg = android::base::StringPrintf( |
Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 51 | "JNI ERROR (app bug): accessed stale %s %p (index %d in a table of size %d)", |
| 52 | GetIndirectRefKindString(kind_), |
| 53 | iref, |
| 54 | idx, |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 55 | top_index); |
Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 56 | AbortIfNoCheckJNI(msg); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 59 | if (UNLIKELY(table_[idx].GetReference()->IsNull())) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 60 | AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): accessed deleted %s %p", |
| 61 | GetIndirectRefKindString(kind_), |
| 62 | iref)); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | if (UNLIKELY(!CheckEntry("use", iref, idx))) { |
| 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | // Make sure that the entry at "idx" is correctly paired with "iref". |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 72 | inline bool IndirectReferenceTable::CheckEntry(const char* what, |
| 73 | IndirectRef iref, |
| 74 | uint32_t idx) const { |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 75 | IndirectRef checkRef = ToIndirectRef(idx); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 76 | if (UNLIKELY(checkRef != iref)) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 77 | std::string msg = android::base::StringPrintf( |
Andreas Gampe | f1e8630 | 2016-10-03 11:42:31 -0700 | [diff] [blame] | 78 | "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)", |
| 79 | what, |
| 80 | GetIndirectRefKindString(kind_), |
| 81 | iref, |
| 82 | checkRef); |
| 83 | AbortIfNoCheckJNI(msg); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 89 | template<ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 90 | inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const { |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 91 | if (!GetChecked(iref)) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 92 | return nullptr; |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 93 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 94 | uint32_t idx = ExtractIndex(iref); |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 95 | ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>(); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 96 | VerifyObject(obj); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 97 | return obj; |
| 98 | } |
| 99 | |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 100 | inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) { |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 101 | if (!GetChecked(iref)) { |
| 102 | LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref; |
| 103 | return; |
| 104 | } |
| 105 | uint32_t idx = ExtractIndex(iref); |
| 106 | table_[idx].SetReference(obj); |
| 107 | } |
| 108 | |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 109 | inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) { |
| 110 | ++serial_; |
| 111 | if (serial_ == kIRTPrevCount) { |
| 112 | serial_ = 0; |
| 113 | } |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 114 | references_[serial_] = GcRoot<mirror::Object>(obj.Ptr()); |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) { |
| 118 | DCHECK_LT(serial_, kIRTPrevCount); |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 119 | references_[serial_] = GcRoot<mirror::Object>(obj.Ptr()); |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 122 | } // namespace art |
| 123 | |
| 124 | #endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_ |