blob: 790f4d0c17905a1b7a32c69998fae891153df022 [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
22#include "verify_object-inl.h"
23
24namespace art {
25namespace mirror {
26class Object;
27} // namespace mirror
28
29// Verifies that the indirect table lookup is valid.
30// Returns "false" if something looks bad.
31inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
32 if (UNLIKELY(iref == nullptr)) {
33 LOG(WARNING) << "Attempt to look up NULL " << kind_;
34 return false;
35 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070037 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
38 AbortIfNoCheckJNI();
39 return false;
40 }
41 const int topIndex = segment_state_.parts.topIndex;
42 int idx = ExtractIndex(iref);
43 if (UNLIKELY(idx >= topIndex)) {
44 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
45 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
46 AbortIfNoCheckJNI();
47 return false;
48 }
49 if (UNLIKELY(table_[idx] == nullptr)) {
50 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
51 AbortIfNoCheckJNI();
52 return false;
53 }
54 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
55 return false;
56 }
57 return true;
58}
59
60// Make sure that the entry at "idx" is correctly paired with "iref".
61inline bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
62 const mirror::Object* obj = table_[idx];
63 IndirectRef checkRef = ToIndirectRef(obj, idx);
64 if (UNLIKELY(checkRef != iref)) {
65 LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
66 << " stale " << kind_ << " " << iref
67 << " (should be " << checkRef << ")";
68 AbortIfNoCheckJNI();
69 return false;
70 }
71 return true;
72}
73
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070074template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartierc56057e2014-05-04 13:18:58 -070075inline mirror::Object* IndirectReferenceTable::Get(IndirectRef iref) const {
76 if (!GetChecked(iref)) {
77 return kInvalidIndirectRefObject;
78 }
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070079 mirror::Object** root = &table_[ExtractIndex(iref)];
80 mirror::Object* obj = *root;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070081 if (LIKELY(obj != kClearedJniWeakGlobal)) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070082 // The read barrier or VerifyObject won't handle kClearedJniWeakGlobal.
83 obj = ReadBarrier::BarrierForWeakRoot<mirror::Object, kReadBarrierOption>(root);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070084 VerifyObject(obj);
85 }
86 return obj;
87}
88
89} // namespace art
90
91#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_