blob: e05f8f307ce178cb78f7d3125ef885ac6d2d9ab9 [file] [log] [blame]
Mathieu Chartierc56057e2014-05-04 13:18:58 -07001/*
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 Chartier8778c522016-10-04 19:06:30 -070022#include "base/dumpable.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "gc_root-inl.h"
Mathieu Chartier8778c522016-10-04 19:06:30 -070024#include "obj_ptr-inl.h"
Ian Rogersc0542af2014-09-03 16:16:56 -070025#include "runtime-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070026#include "verify_object-inl.h"
27
28namespace art {
29namespace mirror {
30class Object;
31} // namespace mirror
32
33// Verifies that the indirect table lookup is valid.
34// Returns "false" if something looks bad.
35inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
36 if (UNLIKELY(iref == nullptr)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 LOG(WARNING) << "Attempt to look up nullptr " << kind_;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070038 return false;
39 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070041 AbortIfNoCheckJNI(StringPrintf("JNI ERROR (app bug): invalid %s %p",
42 GetIndirectRefKindString(kind_),
43 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070044 return false;
45 }
46 const int topIndex = segment_state_.parts.topIndex;
47 int idx = ExtractIndex(iref);
48 if (UNLIKELY(idx >= topIndex)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070049 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,
54 topIndex);
55 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070056 return false;
57 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070058 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Andreas Gampef1e86302016-10-03 11:42:31 -070059 AbortIfNoCheckJNI(StringPrintf("JNI ERROR (app bug): accessed deleted %s %p",
60 GetIndirectRefKindString(kind_),
61 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070062 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".
71inline bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070072 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070073 if (UNLIKELY(checkRef != iref)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070074 std::string msg = StringPrintf(
75 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
76 what,
77 GetIndirectRefKindString(kind_),
78 iref,
79 checkRef);
80 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 return false;
82 }
83 return true;
84}
85
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070086template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier8778c522016-10-04 19:06:30 -070087inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070088 if (!GetChecked(iref)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070089 return nullptr;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070090 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070091 uint32_t idx = ExtractIndex(iref);
Mathieu Chartier8778c522016-10-04 19:06:30 -070092 ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
93 VerifyObject(obj.Ptr());
Mathieu Chartierc56057e2014-05-04 13:18:58 -070094 return obj;
95}
96
Mathieu Chartier8778c522016-10-04 19:06:30 -070097inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
Jeff Hao39b6c242015-05-19 20:30:23 -070098 if (!GetChecked(iref)) {
99 LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref;
100 return;
101 }
102 uint32_t idx = ExtractIndex(iref);
103 table_[idx].SetReference(obj);
104}
105
Mathieu Chartier8778c522016-10-04 19:06:30 -0700106inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
107 ++serial_;
108 if (serial_ == kIRTPrevCount) {
109 serial_ = 0;
110 }
111 references_[serial_] = GcRoot<mirror::Object>(obj);
112}
113
114inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
115 DCHECK_LT(serial_, kIRTPrevCount);
116 references_[serial_] = GcRoot<mirror::Object>(obj);
117}
118
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700119} // namespace art
120
121#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_