blob: 5cc1de209dc79f1b5f75d071e940475771cf858e [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 Chartiere401d142015-04-22 13:56:20 -070022#include "gc_root-inl.h"
Ian Rogersc0542af2014-09-03 16:16:56 -070023#include "runtime-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070024#include "verify_object-inl.h"
25
26namespace art {
27namespace mirror {
28class Object;
29} // namespace mirror
30
31// Verifies that the indirect table lookup is valid.
32// Returns "false" if something looks bad.
33inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
34 if (UNLIKELY(iref == nullptr)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070035 LOG(WARNING) << "Attempt to look up nullptr " << kind_;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070036 return false;
37 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070039 AbortIfNoCheckJNI(StringPrintf("JNI ERROR (app bug): invalid %s %p",
40 GetIndirectRefKindString(kind_),
41 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070042 return false;
43 }
44 const int topIndex = segment_state_.parts.topIndex;
45 int idx = ExtractIndex(iref);
46 if (UNLIKELY(idx >= topIndex)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070047 std::string msg = StringPrintf(
48 "JNI ERROR (app bug): accessed stale %s %p (index %d in a table of size %d)",
49 GetIndirectRefKindString(kind_),
50 iref,
51 idx,
52 topIndex);
53 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070054 return false;
55 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070056 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Andreas Gampef1e86302016-10-03 11:42:31 -070057 AbortIfNoCheckJNI(StringPrintf("JNI ERROR (app bug): accessed deleted %s %p",
58 GetIndirectRefKindString(kind_),
59 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070060 return false;
61 }
62 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
63 return false;
64 }
65 return true;
66}
67
68// Make sure that the entry at "idx" is correctly paired with "iref".
69inline bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070070 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070071 if (UNLIKELY(checkRef != iref)) {
Andreas Gampef1e86302016-10-03 11:42:31 -070072 std::string msg = StringPrintf(
73 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
74 what,
75 GetIndirectRefKindString(kind_),
76 iref,
77 checkRef);
78 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070079 return false;
80 }
81 return true;
82}
83
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070084template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartierc56057e2014-05-04 13:18:58 -070085inline mirror::Object* IndirectReferenceTable::Get(IndirectRef iref) const {
86 if (!GetChecked(iref)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070087 return nullptr;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070088 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070089 uint32_t idx = ExtractIndex(iref);
Hiroshi Yamauchi86891cd2014-10-06 14:24:36 -070090 mirror::Object* obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
Ian Rogersc0542af2014-09-03 16:16:56 -070091 VerifyObject(obj);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070092 return obj;
93}
94
Jeff Hao39b6c242015-05-19 20:30:23 -070095inline void IndirectReferenceTable::Update(IndirectRef iref, mirror::Object* obj) {
96 if (!GetChecked(iref)) {
97 LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref;
98 return;
99 }
100 uint32_t idx = ExtractIndex(iref);
101 table_[idx].SetReference(obj);
102}
103
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700104} // namespace art
105
106#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_