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