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 | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "gc_root-inl.h" |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 23 | #include "runtime-inl.h" |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 24 | #include "verify_object-inl.h" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace mirror { |
| 28 | class Object; |
| 29 | } // namespace mirror |
| 30 | |
| 31 | // Verifies that the indirect table lookup is valid. |
| 32 | // Returns "false" if something looks bad. |
| 33 | inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const { |
| 34 | if (UNLIKELY(iref == nullptr)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 35 | LOG(WARNING) << "Attempt to look up nullptr " << kind_; |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 36 | return false; |
| 37 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 38 | if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) { |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 39 | LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref; |
| 40 | AbortIfNoCheckJNI(); |
| 41 | return false; |
| 42 | } |
| 43 | const int topIndex = segment_state_.parts.topIndex; |
| 44 | int idx = ExtractIndex(iref); |
| 45 | if (UNLIKELY(idx >= topIndex)) { |
| 46 | LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " |
| 47 | << iref << " (index " << idx << " in a table of size " << topIndex << ")"; |
| 48 | AbortIfNoCheckJNI(); |
| 49 | return false; |
| 50 | } |
Mathieu Chartier | 4838d66 | 2014-09-25 15:27:43 -0700 | [diff] [blame] | 51 | if (UNLIKELY(table_[idx].GetReference()->IsNull())) { |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 52 | LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref; |
| 53 | AbortIfNoCheckJNI(); |
| 54 | return false; |
| 55 | } |
| 56 | if (UNLIKELY(!CheckEntry("use", iref, idx))) { |
| 57 | return false; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | // Make sure that the entry at "idx" is correctly paired with "iref". |
| 63 | inline bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const { |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 64 | IndirectRef checkRef = ToIndirectRef(idx); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 65 | if (UNLIKELY(checkRef != iref)) { |
| 66 | LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what |
| 67 | << " stale " << kind_ << " " << iref |
| 68 | << " (should be " << checkRef << ")"; |
| 69 | AbortIfNoCheckJNI(); |
| 70 | return false; |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 75 | template<ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 76 | inline mirror::Object* IndirectReferenceTable::Get(IndirectRef iref) const { |
| 77 | if (!GetChecked(iref)) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 78 | return nullptr; |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 79 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 80 | uint32_t idx = ExtractIndex(iref); |
Hiroshi Yamauchi | 86891cd | 2014-10-06 14:24:36 -0700 | [diff] [blame] | 81 | mirror::Object* obj = table_[idx].GetReference()->Read<kReadBarrierOption>(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 82 | VerifyObject(obj); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 83 | return obj; |
| 84 | } |
| 85 | |
Jeff Hao | 39b6c24 | 2015-05-19 20:30:23 -0700 | [diff] [blame] | 86 | inline void IndirectReferenceTable::Update(IndirectRef iref, mirror::Object* obj) { |
| 87 | if (!GetChecked(iref)) { |
| 88 | LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref; |
| 89 | return; |
| 90 | } |
| 91 | uint32_t idx = ExtractIndex(iref); |
| 92 | table_[idx].SetReference(obj); |
| 93 | } |
| 94 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 95 | } // namespace art |
| 96 | |
| 97 | #endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_ |