blob: e571a0ebc180e6cd7318763543d27d0e0d9b918f [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
Ian Rogersc0542af2014-09-03 16:16:56 -070022#include "runtime-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070023#include "verify_object-inl.h"
24
25namespace art {
26namespace mirror {
27class Object;
28} // namespace mirror
29
30// Verifies that the indirect table lookup is valid.
31// Returns "false" if something looks bad.
32inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
33 if (UNLIKELY(iref == nullptr)) {
34 LOG(WARNING) << "Attempt to look up NULL " << kind_;
35 return false;
36 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070037 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070038 LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
39 AbortIfNoCheckJNI();
40 return false;
41 }
42 const int topIndex = segment_state_.parts.topIndex;
43 int idx = ExtractIndex(iref);
44 if (UNLIKELY(idx >= topIndex)) {
45 LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
46 << iref << " (index " << idx << " in a table of size " << topIndex << ")";
47 AbortIfNoCheckJNI();
48 return false;
49 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070050 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070051 LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
52 AbortIfNoCheckJNI();
53 return false;
54 }
55 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
56 return false;
57 }
58 return true;
59}
60
61// Make sure that the entry at "idx" is correctly paired with "iref".
62inline bool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070063 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070064 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)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070077 return nullptr;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070078 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070079 uint32_t idx = ExtractIndex(iref);
Hiroshi Yamauchi86891cd2014-10-06 14:24:36 -070080 mirror::Object* obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
Ian Rogersc0542af2014-09-03 16:16:56 -070081 VerifyObject(obj);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070082 return obj;
83}
84
85} // namespace art
86
87#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_