indirect_reference_table.cc revision 719d1a33f6569864f529e5a3fff59e7bca97aad0
16c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/*
26c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
36c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
46c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
56c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * you may not use this file except in compliance with the License.
66c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * You may obtain a copy of the License at
76c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
86c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
96c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Unless required by applicable law or agreed to in writing, software
116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * See the License for the specific language governing permissions and
146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * limitations under the License.
156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */
166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include "indirect_reference_table.h"
18a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include "jni_internal.h"
196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include "reference_table.h"
20a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include "runtime.h"
2100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
225a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers#include "thread.h"
23cdd1d2d3fee0711b8b11db99f2dfb80113520100Ian Rogers#include "utils.h"
246dda898d47b3e8931e4404330e81b7110108e34fMathieu Chartier#include "verify_object-inl.h"
256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include <cstdlib>
276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesnamespace art {
296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
30719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerstemplate<typename T>
31719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersclass MutatorLockedDumpable {
32719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers public:
33719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  explicit MutatorLockedDumpable(T& value)
34719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : value_(value) {
35719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  }
36719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
37719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  void Dump(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
38719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    value_.Dump(os);
39719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  }
40719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
41719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers private:
42719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  T& value_;
43719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
44719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
45719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers};
46719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
47719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerstemplate<typename T>
48719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstd::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
49719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// TODO: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) however annotalysis
50719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers//       currently fails for this.
51719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    NO_THREAD_SAFETY_ANALYSIS {
52719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  rhs.Dump(os);
53719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return os;
54719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}
55719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesstatic void AbortMaybe() {
57a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
58a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  if (!Runtime::Current()->GetJavaVM()->check_jni) {
59a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // Otherwise, we want to abort rather than hand back a bad reference.
60a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    LOG(FATAL) << "JNI ERROR (app bug): see above.";
61a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
626c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
636c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
65ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes                                               size_t maxCount, IndirectRefKind desiredKind) {
666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK_GT(initialCount, 0U);
676c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK_LE(initialCount, maxCount);
68408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  CHECK_NE(desiredKind, kSirtOrInvalid);
696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
70423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier  table_ = reinterpret_cast<mirror::Object**>(malloc(initialCount * sizeof(const mirror::Object*)));
716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK(table_ != NULL);
722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  memset(table_, 0xd1, initialCount * sizeof(const mirror::Object*));
736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK(slot_data_ != NULL);
766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
77dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  segment_state_.all = IRT_FIRST_SEGMENT;
786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  alloc_entries_ = initialCount;
796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  max_entries_ = maxCount;
806c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  kind_ = desiredKind;
816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectReferenceTable::~IndirectReferenceTable() {
846c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  free(table_);
856c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  free(slot_data_);
866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  table_ = NULL;
876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  slot_data_ = NULL;
886c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  alloc_entries_ = max_entries_ = -1;
896c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
9173e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// Make sure that the entry at "idx" is correctly paired with "iref".
926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  const mirror::Object* obj = table_[idx];
946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectRef checkRef = ToIndirectRef(obj, idx);
954f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  if (UNLIKELY(checkRef != iref)) {
966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes               << " stale " << kind_ << " " << iref
986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes               << " (should be " << checkRef << ")";
996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
1006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return true;
1036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
1046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
105423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu ChartierIndirectRef IndirectReferenceTable::Add(uint32_t cookie, mirror::Object* obj) {
1066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IRTSegmentState prevState;
1076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  prevState.all = cookie;
108dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  size_t topIndex = segment_state_.parts.topIndex;
1096c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
110cd2cfff09c916c9e72a7cfeb0686b441847dc62eMathieu Chartier  CHECK(obj != NULL);
1116dda898d47b3e8931e4404330e81b7110108e34fMathieu Chartier  VerifyObject(obj);
1126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(table_ != NULL);
1136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK_LE(alloc_entries_, max_entries_);
114dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
1156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (topIndex == alloc_entries_) {
11773e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    // reached end of allocated space; did we hit buffer max?
1186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (topIndex == max_entries_) {
11973e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes      LOG(FATAL) << "JNI ERROR (app bug): " << kind_ << " table overflow "
12073e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes                 << "(max=" << max_entries_ << ")\n"
12100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
1226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    size_t newSize = alloc_entries_ * 2;
1256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (newSize > max_entries_) {
1266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      newSize = max_entries_;
1276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    DCHECK_GT(newSize, alloc_entries_);
1296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
130423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier    table_ = reinterpret_cast<mirror::Object**>(realloc(table_, newSize * sizeof(mirror::Object*)));
13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    slot_data_ = reinterpret_cast<IndirectRefSlot*>(realloc(slot_data_,
13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                                            newSize * sizeof(IndirectRefSlot)));
1336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (table_ == NULL || slot_data_ == NULL) {
13473e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes      LOG(FATAL) << "JNI ERROR (app bug): unable to expand "
1356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes                 << kind_ << " table (from "
1366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes                 << alloc_entries_ << " to " << newSize
13773e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes                 << ", max=" << max_entries_ << ")\n"
13800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                 << MutatorLockedDumpable<IndirectReferenceTable>(*this);
1396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1416c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    // Clear the newly-allocated slot_data_ elements.
1426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
1436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    alloc_entries_ = newSize;
1456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
14773e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes  // We know there's enough room in the table.  Now we just need to find
14873e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes  // the right spot.  If there's a hole, find it and fill it; otherwise,
14973e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes  // add to the end of the list.
1506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectRef result;
151dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
1526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (numHoles > 0) {
1536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    DCHECK_GT(topIndex, 1U);
15473e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    // Find the first hole; likely to be near the end of the list.
155423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier    mirror::Object** pScan = &table_[topIndex - 1];
1566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    DCHECK(*pScan != NULL);
1576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    while (*--pScan != NULL) {
1586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
1596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    UpdateSlotAdd(obj, pScan - table_);
1616c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    result = ToIndirectRef(obj, pScan - table_);
1626c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    *pScan = obj;
163dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    segment_state_.parts.numHoles--;
1646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  } else {
16573e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    // Add to the end.
1666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    UpdateSlotAdd(obj, topIndex);
1676c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    result = ToIndirectRef(obj, topIndex);
1686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    table_[topIndex++] = obj;
169dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    segment_state_.parts.topIndex = topIndex;
1706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1715a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  if (false) {
1725a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
1735a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers              << " holes=" << segment_state_.parts.numHoles;
1745a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  }
1756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(result != NULL);
1776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return result;
1786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
1796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
180726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughesvoid IndirectReferenceTable::AssertEmpty() {
18100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (UNLIKELY(begin() != end())) {
18200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
18373e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    LOG(FATAL) << "Internal Error: non-empty local reference table\n"
18400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers               << MutatorLockedDumpable<IndirectReferenceTable>(*this);
185726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  }
186726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes}
187726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes
18873e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// Verifies that the indirect table lookup is valid.
18973e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// Returns "false" if something looks bad.
1906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
1914f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  if (UNLIKELY(iref == NULL)) {
1926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(WARNING) << "Attempt to look up NULL " << kind_;
1936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1954f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  if (UNLIKELY(GetIndirectRefKind(iref) == kSirtOrInvalid)) {
1966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
1976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
1986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
201dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  int topIndex = segment_state_.parts.topIndex;
2026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  int idx = ExtractIndex(iref);
2034f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  if (UNLIKELY(idx >= topIndex)) {
2045a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
2055a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers               << iref << " (index " << idx << " in a table of size " << topIndex << ")";
2066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
2076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2096c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2104f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  if (UNLIKELY(table_[idx] == NULL)) {
2116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
2126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
2136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2164f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  if (UNLIKELY(!CheckEntry("use", iref, idx))) {
2176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return true;
2216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
2226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
223423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartierstatic int Find(mirror::Object* direct_pointer, int bottomIndex, int topIndex,
224423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier                mirror::Object** table) {
2256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  for (int i = bottomIndex; i < topIndex; ++i) {
2262ced6a534157d5d963693346904389c19775d2daElliott Hughes    if (table[i] == direct_pointer) {
2276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return i;
2286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
2296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2306c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return -1;
2316c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
2326c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersbool IndirectReferenceTable::ContainsDirectPointer(mirror::Object* direct_pointer) const {
2342ced6a534157d5d963693346904389c19775d2daElliott Hughes  return Find(direct_pointer, 0, segment_state_.parts.topIndex, table_) != -1;
2356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
2366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
23773e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// Removes an object. We extract the table offset bits from "iref"
23873e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// and zap the corresponding entry, leaving a hole if it's not at the top.
23973e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// If the entry is not between the current top index and the bottom index
24073e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// specified by the cookie, we don't remove anything. This is the behavior
24173e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// required by JNI's DeleteLocalRef function.
24273e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// This method is not called when a local frame is popped; this is only used
24373e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// for explicit single removals.
24473e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes// Returns "false" if nothing was removed.
2456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
2466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IRTSegmentState prevState;
2476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  prevState.all = cookie;
248dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  int topIndex = segment_state_.parts.topIndex;
2496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  int bottomIndex = prevState.parts.topIndex;
2506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(table_ != NULL);
2526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK_LE(alloc_entries_, max_entries_);
253dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
2546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  int idx = ExtractIndex(iref);
2566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
257c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes  JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2585a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
2590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
2605a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
2615a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    return true;
2625a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  }
263467c9699c3de467f76f03494a9389ebba285cb6cIan Rogers  if (GetIndirectRefKind(iref) == kSirtOrInvalid && vm->work_around_app_jni_bugs) {
2642dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* direct_pointer = reinterpret_cast<mirror::Object*>(iref);
2652ced6a534157d5d963693346904389c19775d2daElliott Hughes    idx = Find(direct_pointer, bottomIndex, topIndex, table_);
2666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (idx == -1) {
2679dcd45c60c691524bd8ef7d6f65075d9ee3e5554Elliott Hughes      LOG(WARNING) << "Trying to work around app JNI bugs, but didn't find " << iref << " in table!";
2686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
2696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
2706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx < bottomIndex) {
273726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    // Wrong segment.
274726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    LOG(WARNING) << "Attempt to remove index outside index area (" << idx
275726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes                 << " vs " << bottomIndex << "-" << topIndex << ")";
2766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx >= topIndex) {
279726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    // Bad --- stale reference?
280726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    LOG(WARNING) << "Attempt to remove invalid index " << idx
281726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes                 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
2826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2846c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2856c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx == topIndex-1) {
2866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    // Top-most entry.  Scan up and consume holes.
2876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
288c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes    if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
2896c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
2906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
2916c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    table_[idx] = NULL;
293dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
2946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (numHoles != 0) {
2956c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      while (--topIndex > bottomIndex && numHoles != 0) {
2965a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        if (false) {
2975a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers          LOG(INFO) << "+++ checking for hole at " << topIndex-1
2985a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers                    << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
2995a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        }
3006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes        if (table_[topIndex-1] != NULL) {
3016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes          break;
3026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes        }
3035a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        if (false) {
3045a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers          LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
3055a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        }
3066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes        numHoles--;
3076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      }
308dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers      segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
309dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers      segment_state_.parts.topIndex = topIndex;
3106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    } else {
311dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers      segment_state_.parts.topIndex = topIndex-1;
3125a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      if (false) {
3135a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        LOG(INFO) << "+++ ate last entry " << topIndex - 1;
3145a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      }
3156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
3166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  } else {
31773e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    // Not the top-most entry.  This creates a hole.  We NULL out the
31873e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    // entry to prevent somebody from deleting it twice and screwing up
31973e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes    // the hole count.
3206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (table_[idx] == NULL) {
3216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(INFO) << "--- WEIRD: removing null entry " << idx;
3226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
3236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
324c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes    if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
3256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
3266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
3276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    table_[idx] = NULL;
329dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    segment_state_.parts.numHoles++;
3305a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    if (false) {
3315a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
3325a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    }
3336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
3346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return true;
3366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
3376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
33883c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartiervoid IndirectReferenceTable::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
33983c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier                                        RootType root_type) {
34002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier  for (auto ref : *this) {
341815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier    callback(ref, arg, tid, root_type);
342423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier    DCHECK(*ref != nullptr);
343410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes  }
344410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes}
345410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes
34673e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughesvoid IndirectReferenceTable::Dump(std::ostream& os) const {
34773e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes  os << kind_ << " table dump:\n";
348423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier  ReferenceTable::Table entries(table_, table_ + Capacity());
34963818dc8b06af4a1e65c41b453f1a42166c22728Ian Rogers  // Remove NULLs.
35063818dc8b06af4a1e65c41b453f1a42166c22728Ian Rogers  for (int i = entries.size() - 1; i >= 0; --i) {
35163818dc8b06af4a1e65c41b453f1a42166c22728Ian Rogers    if (entries[i] == NULL) {
35263818dc8b06af4a1e65c41b453f1a42166c22728Ian Rogers      entries.erase(entries.begin() + i);
35363818dc8b06af4a1e65c41b453f1a42166c22728Ian Rogers    }
35463818dc8b06af4a1e65c41b453f1a42166c22728Ian Rogers  }
35573e66f73f5093b64f2b023ebbb85916a13d5c937Elliott Hughes  ReferenceTable::Dump(os, entries);
3566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
3576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}  // namespace art
359