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 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 22 | #include "runtime-inl.h" |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 23 | #include "verify_object-inl.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace mirror { |
| 27 | class Object; |
| 28 | } // namespace mirror |
| 29 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 30 | inline void IrtIterator::SkipNullsAndTombstones() { |
| 31 | // We skip NULLs and tombstones. Clients don't want to see implementation details. |
| 32 | while (i_ < capacity_ && |
| 33 | (table_[i_].IsNull() || |
| 34 | Runtime::Current()->IsClearedJniWeakGlobal(table_[i_].Read<kWithoutReadBarrier>()))) { |
| 35 | ++i_; |
| 36 | } |
| 37 | } |
| 38 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 39 | // Verifies that the indirect table lookup is valid. |
| 40 | // Returns "false" if something looks bad. |
| 41 | inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const { |
| 42 | if (UNLIKELY(iref == nullptr)) { |
| 43 | LOG(WARNING) << "Attempt to look up NULL " << kind_; |
| 44 | return false; |
| 45 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) { |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 47 | LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref; |
| 48 | AbortIfNoCheckJNI(); |
| 49 | return false; |
| 50 | } |
| 51 | const int topIndex = segment_state_.parts.topIndex; |
| 52 | int idx = ExtractIndex(iref); |
| 53 | if (UNLIKELY(idx >= topIndex)) { |
| 54 | LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " |
| 55 | << iref << " (index " << idx << " in a table of size " << topIndex << ")"; |
| 56 | AbortIfNoCheckJNI(); |
| 57 | return false; |
| 58 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 59 | if (UNLIKELY(table_[idx].IsNull())) { |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 60 | LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref; |
| 61 | AbortIfNoCheckJNI(); |
| 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". |
| 71 | inline bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const { |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 72 | IndirectRef checkRef = ToIndirectRef(idx); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 73 | if (UNLIKELY(checkRef != iref)) { |
| 74 | LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what |
| 75 | << " stale " << kind_ << " " << iref |
| 76 | << " (should be " << checkRef << ")"; |
| 77 | AbortIfNoCheckJNI(); |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
Hiroshi Yamauchi | 196851b | 2014-05-29 12:16:04 -0700 | [diff] [blame] | 83 | template<ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 84 | inline mirror::Object* IndirectReferenceTable::Get(IndirectRef iref) const { |
| 85 | if (!GetChecked(iref)) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 86 | return nullptr; |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 87 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 88 | uint32_t idx = ExtractIndex(iref); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 89 | mirror::Object* obj = table_[idx].Read<kReadBarrierOption>(); |
| 90 | VerifyObject(obj); |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 91 | return obj; |
| 92 | } |
| 93 | |
| 94 | } // namespace art |
| 95 | |
| 96 | #endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_ |