indirect_reference_table.cc revision 726079d3e2e50854cd6ca4c393f4529a796dba58
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"
215a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers#include "thread.h"
22cdd1d2d3fee0711b8b11db99f2dfb80113520100Ian Rogers#include "utils.h"
236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include <cstdlib>
256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesnamespace art {
276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesstatic void AbortMaybe() {
29a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  // If -Xcheck:jni is on, it'll give a more detailed error before aborting.
30a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  if (!Runtime::Current()->GetJavaVM()->check_jni) {
31a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // Otherwise, we want to abort rather than hand back a bad reference.
32a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    LOG(FATAL) << "JNI ERROR (app bug): see above.";
33a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectReferenceTable::IndirectReferenceTable(size_t initialCount,
376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    size_t maxCount, IndirectRefKind desiredKind)
386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes{
396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK_GT(initialCount, 0U);
406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK_LE(initialCount, maxCount);
41408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  CHECK_NE(desiredKind, kSirtOrInvalid);
426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
43cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes  table_ = reinterpret_cast<const Object**>(malloc(initialCount * sizeof(const Object*)));
446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK(table_ != NULL);
456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#ifndef NDEBUG
46cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes  memset(table_, 0xd1, initialCount * sizeof(const Object*));
476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#endif
486c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot)));
506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  CHECK(slot_data_ != NULL);
516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
52dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  segment_state_.all = IRT_FIRST_SEGMENT;
536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  alloc_entries_ = initialCount;
546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  max_entries_ = maxCount;
556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  kind_ = desiredKind;
566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectReferenceTable::~IndirectReferenceTable() {
596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  free(table_);
606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  free(slot_data_);
616c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  table_ = NULL;
626c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  slot_data_ = NULL;
636c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  alloc_entries_ = max_entries_ = -1;
646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
656c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/*
676c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Make sure that the entry at "idx" is correctly paired with "iref".
686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */
696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const {
70cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes  const Object* obj = table_[idx];
716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectRef checkRef = ToIndirectRef(obj, idx);
726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (checkRef != iref) {
736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what
746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes               << " stale " << kind_ << " " << iref
756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes               << " (should be " << checkRef << ")";
766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return true;
806c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
82cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott HughesIndirectRef IndirectReferenceTable::Add(uint32_t cookie, const Object* obj) {
836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IRTSegmentState prevState;
846c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  prevState.all = cookie;
85dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  size_t topIndex = segment_state_.parts.topIndex;
866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(obj != NULL);
88cdd1d2d3fee0711b8b11db99f2dfb80113520100Ian Rogers  // TODO: stronger sanity check on the object (such as in heap)
89395520eaa47eca25b92e86188accf3095d60af49Brian Carlstrom  DCHECK(IsAligned(reinterpret_cast<intptr_t>(obj), 8)) << reinterpret_cast<const void*>(obj);
906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(table_ != NULL);
916c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK_LE(alloc_entries_, max_entries_);
92dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (topIndex == alloc_entries_) {
956c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    /* reached end of allocated space; did we hit buffer max? */
966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (topIndex == max_entries_) {
976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(ERROR) << "JNI ERROR (app bug): " << kind_ << " table overflow "
986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes                 << "(max=" << max_entries_ << ")";
996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      Dump();
1006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
1016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    size_t newSize = alloc_entries_ * 2;
1046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (newSize > max_entries_) {
1056c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      newSize = max_entries_;
1066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    DCHECK_GT(newSize, alloc_entries_);
1086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
109cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes    table_ = (const Object**) realloc(table_, newSize * sizeof(const Object*));
1106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    slot_data_ = (IndirectRefSlot*) realloc(slot_data_, newSize * sizeof(IndirectRefSlot));
1116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (table_ == NULL || slot_data_ == NULL) {
1126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(ERROR) << "JNI ERROR (app bug): unable to expand "
1136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes                 << kind_ << " table (from "
1146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes                 << alloc_entries_ << " to " << newSize
1156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes                 << ", max=" << max_entries_ << ")";
1166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      Dump();
1176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(FATAL); // TODO: operator<< for IndirectReferenceTable
1186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    // Clear the newly-allocated slot_data_ elements.
1216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot));
1226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    alloc_entries_ = newSize;
1246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  /*
1276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes   * We know there's enough room in the table.  Now we just need to find
1286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes   * the right spot.  If there's a hole, find it and fill it; otherwise,
1296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes   * add to the end of the list.
1306c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes   */
1316c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IndirectRef result;
132dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
1336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (numHoles > 0) {
1346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    DCHECK_GT(topIndex, 1U);
1356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    /* find the first hole; likely to be near the end of the list */
136cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes    const Object** pScan = &table_[topIndex - 1];
1376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    DCHECK(*pScan != NULL);
1386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    while (*--pScan != NULL) {
1396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      DCHECK_GE(pScan, table_ + prevState.parts.topIndex);
1406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
1416c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    UpdateSlotAdd(obj, pScan - table_);
1426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    result = ToIndirectRef(obj, pScan - table_);
1436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    *pScan = obj;
144dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    segment_state_.parts.numHoles--;
1456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  } else {
1466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    /* add to the end */
1476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    UpdateSlotAdd(obj, topIndex);
1486c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    result = ToIndirectRef(obj, topIndex);
1496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    table_[topIndex++] = obj;
150dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    segment_state_.parts.topIndex = topIndex;
1516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1525a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  if (false) {
1535a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    LOG(INFO) << "+++ added at " << ExtractIndex(result) << " top=" << segment_state_.parts.topIndex
1545a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers              << " holes=" << segment_state_.parts.numHoles;
1555a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  }
1566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(result != NULL);
1586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return result;
1596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
1606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
161726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughesvoid IndirectReferenceTable::AssertEmpty() {
162726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  if (begin() != end()) {
163726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    Dump();
164726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    LOG(FATAL) << "Internal Error: non-empty local reference table";
165726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  }
166726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes}
167726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes
1686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/*
1696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Verify that the indirect table lookup is valid.
1706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
1716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Returns "false" if something looks bad.
1726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */
1736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
1746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (iref == NULL) {
1756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(WARNING) << "Attempt to look up NULL " << kind_;
1766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
178408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  if (GetIndirectRefKind(iref) == kSirtOrInvalid) {
1796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref;
1806c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
1816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
184dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  int topIndex = segment_state_.parts.topIndex;
1856c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  int idx = ExtractIndex(iref);
1866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx >= topIndex) {
1876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    /* bad -- stale reference? */
1885a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " "
1895a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers               << iref << " (index " << idx << " in a table of size " << topIndex << ")";
1906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
1916c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
1946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (table_[idx] == NULL) {
1956c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref;
1966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    AbortMaybe();
1976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
1986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
1996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (!CheckEntry("use", iref, idx)) {
2016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return true;
2056c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
2066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
207cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughesstatic int LinearScan(IndirectRef iref, int bottomIndex, int topIndex, const Object** table) {
2086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  for (int i = bottomIndex; i < topIndex; ++i) {
209cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes    if (table[i] == reinterpret_cast<const Object*>(iref)) {
2106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return i;
2116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
2126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return -1;
2146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
2156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::Contains(IndirectRef iref) const {
217dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  return LinearScan(iref, 0, segment_state_.parts.topIndex, table_) != -1;
2186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
2196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/*
2216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Remove "obj" from "pRef".  We extract the table offset bits from "iref"
2226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * and zap the corresponding entry, leaving a hole if it's not at the top.
2236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
2246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * If the entry is not between the current top index and the bottom index
2256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * specified by the cookie, we don't remove anything.  This is the behavior
2266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * required by JNI's DeleteLocalRef function.
2276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
2286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Note this is NOT called when a local frame is popped.  This is only used
2296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * for explicit single removals.
2306c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *
2316c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Returns "false" if nothing was removed.
2326c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */
2336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) {
2346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  IRTSegmentState prevState;
2356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  prevState.all = cookie;
236dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  int topIndex = segment_state_.parts.topIndex;
2376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  int bottomIndex = prevState.parts.topIndex;
2386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK(table_ != NULL);
2406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  DCHECK_LE(alloc_entries_, max_entries_);
241dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers  DCHECK_GE(segment_state_.parts.numHoles, prevState.parts.numHoles);
2426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  int idx = ExtractIndex(iref);
2446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
245c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes  JavaVMExt* vm = Runtime::Current()->GetJavaVM();
2465a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  if (GetIndirectRefKind(iref) == kSirtOrInvalid &&
2475a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      Thread::Current()->SirtContains(reinterpret_cast<jobject>(iref))) {
2485a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    LOG(WARNING) << "Attempt to remove local SIRT entry from IRT, ignoring";
2495a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    return true;
2505a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers  }
251c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes  if (GetIndirectRefKind(iref) == kSirtOrInvalid || vm->work_around_app_jni_bugs) {
2526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    idx = LinearScan(iref, bottomIndex, topIndex, table_);
2536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (idx == -1) {
2546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(WARNING) << "trying to work around app JNI bugs, but didn't find " << iref << " in table!";
2556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
2566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
2576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx < bottomIndex) {
260726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    // Wrong segment.
261726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    LOG(WARNING) << "Attempt to remove index outside index area (" << idx
262726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes                 << " vs " << bottomIndex << "-" << topIndex << ")";
2636c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2656c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx >= topIndex) {
266726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    // Bad --- stale reference?
267726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes    LOG(WARNING) << "Attempt to remove invalid index " << idx
268726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes                 << " (bottom=" << bottomIndex << " top=" << topIndex << ")";
2696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    return false;
2706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
2716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  if (idx == topIndex-1) {
2736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    // Top-most entry.  Scan up and consume holes.
2746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
275c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes    if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
2766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
2776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
2786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
2796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    table_[idx] = NULL;
280dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    int numHoles = segment_state_.parts.numHoles - prevState.parts.numHoles;
2816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (numHoles != 0) {
2826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      while (--topIndex > bottomIndex && numHoles != 0) {
2835a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        if (false) {
2845a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers          LOG(INFO) << "+++ checking for hole at " << topIndex-1
2855a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers                    << " (cookie=" << cookie << ") val=" << table_[topIndex - 1];
2865a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        }
2876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes        if (table_[topIndex-1] != NULL) {
2886c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes          break;
2896c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes        }
2905a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        if (false) {
2915a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers          LOG(INFO) << "+++ ate hole at " << (topIndex - 1);
2925a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        }
2936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes        numHoles--;
2946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      }
295dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers      segment_state_.parts.numHoles = numHoles + prevState.parts.numHoles;
296dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers      segment_state_.parts.topIndex = topIndex;
2976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    } else {
298dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers      segment_state_.parts.topIndex = topIndex-1;
2995a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      if (false) {
3005a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers        LOG(INFO) << "+++ ate last entry " << topIndex - 1;
3015a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      }
3026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
3036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  } else {
3046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    /*
3056c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes     * Not the top-most entry.  This creates a hole.  We NULL out the
3066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes     * entry to prevent somebody from deleting it twice and screwing up
3076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes     * the hole count.
3086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes     */
3096c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    if (table_[idx] == NULL) {
3106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      LOG(INFO) << "--- WEIRD: removing null entry " << idx;
3116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
3126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
313c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes    if (!vm->work_around_app_jni_bugs && !CheckEntry("remove", iref, idx)) {
3146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes      return false;
3156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    }
3166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    table_[idx] = NULL;
318dc51b79e65abcdad094ccd5e5a2caf5153433d49Ian Rogers    segment_state_.parts.numHoles++;
3195a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    if (false) {
3205a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers      LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segment_state_.parts.numHoles;
3215a7a74a042e73a355f5cedffa0d2faf5340028faIan Rogers    }
3226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
3236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return true;
3256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
3266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
327410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughesvoid IndirectReferenceTable::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
328410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes  typedef IndirectReferenceTable::iterator It; // TODO: C++0x auto
329410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes  for (It it = begin(), end = this->end(); it != end; ++it) {
330410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes    visitor(**it, arg);
331410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes  }
332410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes}
333410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes
3346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesstd::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
3356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  switch (rhs) {
336408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers  case kSirtOrInvalid:
337408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers    os << "stack indirect reference table or invalid reference";
3386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    break;
3396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  case kLocal:
3406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    os << "local reference";
3416c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    break;
3426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  case kGlobal:
3436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    os << "global reference";
3446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    break;
3456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  case kWeakGlobal:
3466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    os << "weak global reference";
3476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    break;
3486c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  default:
3496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
3506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes    break;
3516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  }
3526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  return os;
3536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
3546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesvoid IndirectReferenceTable::Dump() const {
3566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  LOG(WARNING) << kind_ << " table dump:";
3577577075b147fd8fa37ca41e7a32d1124676776ceElliott Hughes  std::vector<const Object*> entries(table_, table_ + Capacity());
3586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes  ReferenceTable::Dump(entries);
3596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}
3606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes
3616c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes}  // namespace art
362