indirect_reference_table.cc revision 6c1a394b47c85c8d1723fc3b156a3b1b0b29a757
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" 186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include "reference_table.h" 196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#include <cstdlib> 216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesnamespace art { 236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes// TODO: implement this for art. (only needed for non-CheckJNI operation.) 256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesstatic void AbortMaybe() { 266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes // If CheckJNI is on, it'll give a more detailed error before aborting. 276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes // Otherwise, we want to abort rather than hand back a bad reference. 286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes// if (!gDvmJni.useCheckJni) { 296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes// LOG(FATAL) << "bye!"; 306c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes// } 316c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 326c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectReferenceTable::IndirectReferenceTable(size_t initialCount, 346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes size_t maxCount, IndirectRefKind desiredKind) 356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes{ 366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes CHECK_GT(initialCount, 0U); 376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes CHECK_LE(initialCount, maxCount); 386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes CHECK_NE(desiredKind, kInvalid); 396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes table_ = reinterpret_cast<Object**>(malloc(initialCount * sizeof(Object*))); 416c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes CHECK(table_ != NULL); 426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#ifndef NDEBUG 436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes memset(table_, 0xd1, initialCount * sizeof(Object*)); 446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes#endif 456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes slot_data_ = reinterpret_cast<IndirectRefSlot*>(calloc(initialCount, sizeof(IndirectRefSlot))); 476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes CHECK(slot_data_ != NULL); 486c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.all = IRT_FIRST_SEGMENT; 506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes alloc_entries_ = initialCount; 516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes max_entries_ = maxCount; 526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes kind_ = desiredKind; 536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectReferenceTable::~IndirectReferenceTable() { 566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes free(table_); 576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes free(slot_data_); 586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes table_ = NULL; 596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes slot_data_ = NULL; 606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes alloc_entries_ = max_entries_ = -1; 616c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 626c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 636c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/* 646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Make sure that the entry at "idx" is correctly paired with "iref". 656c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */ 666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::CheckEntry(const char* what, IndirectRef iref, int idx) const { 676c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes Object* obj = table_[idx]; 686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes IndirectRef checkRef = ToIndirectRef(obj, idx); 696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (checkRef != iref) { 706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(ERROR) << "JNI ERROR (app bug): attempt to " << what 716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes << " stale " << kind_ << " " << iref 726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes << " (should be " << checkRef << ")"; 736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes AbortMaybe(); 746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return true; 776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott HughesIndirectRef IndirectReferenceTable::Add(uint32_t cookie, Object* obj) { 806c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes IRTSegmentState prevState; 816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes prevState.all = cookie; 826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes size_t topIndex = segmentState.parts.topIndex; 836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 846c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK(obj != NULL); 856c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes //DCHECK(dvmIsHeapAddress(obj)); 866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK(table_ != NULL); 876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_LE(alloc_entries_, max_entries_); 886c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_GE(segmentState.parts.numHoles, prevState.parts.numHoles); 896c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (topIndex == alloc_entries_) { 916c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* reached end of allocated space; did we hit buffer max? */ 926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (topIndex == max_entries_) { 936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(ERROR) << "JNI ERROR (app bug): " << kind_ << " table overflow " 946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes << "(max=" << max_entries_ << ")"; 956c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes Dump(); 966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(FATAL); // TODO: operator<< for IndirectReferenceTable 976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes size_t newSize = alloc_entries_ * 2; 1006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (newSize > max_entries_) { 1016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes newSize = max_entries_; 1026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_GT(newSize, alloc_entries_); 1046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1056c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes table_ = (Object**) realloc(table_, newSize * sizeof(Object*)); 1066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes slot_data_ = (IndirectRefSlot*) realloc(slot_data_, newSize * sizeof(IndirectRefSlot)); 1076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (table_ == NULL || slot_data_ == NULL) { 1086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(ERROR) << "JNI ERROR (app bug): unable to expand " 1096c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes << kind_ << " table (from " 1106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes << alloc_entries_ << " to " << newSize 1116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes << ", max=" << max_entries_ << ")"; 1126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes Dump(); 1136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(FATAL); // TODO: operator<< for IndirectReferenceTable 1146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes // Clear the newly-allocated slot_data_ elements. 1176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes memset(slot_data_ + alloc_entries_, 0, (newSize - alloc_entries_) * sizeof(IndirectRefSlot)); 1186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes alloc_entries_ = newSize; 1206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* 1236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * We know there's enough room in the table. Now we just need to find 1246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * the right spot. If there's a hole, find it and fill it; otherwise, 1256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * add to the end of the list. 1266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */ 1276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes IndirectRef result; 1286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles; 1296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (numHoles > 0) { 1306c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_GT(topIndex, 1U); 1316c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* find the first hole; likely to be near the end of the list */ 1326c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes Object** pScan = &table_[topIndex - 1]; 1336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK(*pScan != NULL); 1346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes while (*--pScan != NULL) { 1356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_GE(pScan, table_ + prevState.parts.topIndex); 1366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes UpdateSlotAdd(obj, pScan - table_); 1386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes result = ToIndirectRef(obj, pScan - table_); 1396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes *pScan = obj; 1406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.parts.numHoles--; 1416c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } else { 1426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* add to the end */ 1436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes UpdateSlotAdd(obj, topIndex); 1446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes result = ToIndirectRef(obj, topIndex); 1456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes table_[topIndex++] = obj; 1466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.parts.topIndex = topIndex; 1476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1486c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK(result != NULL); 1506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return result; 1516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 1526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/* 1546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Verify that the indirect table lookup is valid. 1556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * 1566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Returns "false" if something looks bad. 1576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */ 1586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::GetChecked(IndirectRef iref) const { 1596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (iref == NULL) { 1606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(WARNING) << "Attempt to look up NULL " << kind_; 1616c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 1626c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1636c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (GetIndirectRefKind(iref) == kInvalid) { 1646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(ERROR) << "JNI ERROR (app bug): invalid " << kind_ << " " << iref; 1656c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes AbortMaybe(); 1666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 1676c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int topIndex = segmentState.parts.topIndex; 1706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int idx = ExtractIndex(iref); 1716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (idx >= topIndex) { 1726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* bad -- stale reference? */ 1736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(ERROR) << "JNI ERROR (app bug): accessed stale " << kind_ << " " << iref << " (index " << idx << " in a table of size " << topIndex << ")"; 1746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes AbortMaybe(); 1756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 1766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (table_[idx] == NULL) { 1796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(ERROR) << "JNI ERROR (app bug): accessed deleted " << kind_ << " " << iref; 1806c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes AbortMaybe(); 1816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 1826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1846c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (!CheckEntry("use", iref, idx)) { 1856c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 1866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1886c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return true; 1896c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 1906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 1916c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesstatic int LinearScan(IndirectRef iref, int bottomIndex, int topIndex, Object** table) { 1926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes for (int i = bottomIndex; i < topIndex; ++i) { 1936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (table[i] == reinterpret_cast<Object*>(iref)) { 1946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return i; 1956c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 1976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return -1; 1986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 1996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::Contains(IndirectRef iref) const { 2016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return LinearScan(iref, 0, segmentState.parts.topIndex, table_) != -1; 2026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 2036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes/* 2056c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Remove "obj" from "pRef". We extract the table offset bits from "iref" 2066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * and zap the corresponding entry, leaving a hole if it's not at the top. 2076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * 2086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * If the entry is not between the current top index and the bottom index 2096c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * specified by the cookie, we don't remove anything. This is the behavior 2106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * required by JNI's DeleteLocalRef function. 2116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * 2126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Note this is NOT called when a local frame is popped. This is only used 2136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * for explicit single removals. 2146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * 2156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Returns "false" if nothing was removed. 2166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */ 2176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesbool IndirectReferenceTable::Remove(uint32_t cookie, IndirectRef iref) { 2186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes IRTSegmentState prevState; 2196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes prevState.all = cookie; 2206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int topIndex = segmentState.parts.topIndex; 2216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int bottomIndex = prevState.parts.topIndex; 2226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK(table_ != NULL); 2246c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_LE(alloc_entries_, max_entries_); 2256c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes DCHECK_GE(segmentState.parts.numHoles, prevState.parts.numHoles); 2266c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2276c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int idx = ExtractIndex(iref); 2286c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes bool workAroundAppJniBugs = false; 2296c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2306c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (GetIndirectRefKind(iref) == kInvalid /*&& gDvmJni.workAroundAppJniBugs*/) { // TODO 2316c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes idx = LinearScan(iref, bottomIndex, topIndex, table_); 2326c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes workAroundAppJniBugs = true; 2336c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (idx == -1) { 2346c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(WARNING) << "trying to work around app JNI bugs, but didn't find " << iref << " in table!"; 2356c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 2366c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2376c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2386c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2396c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (idx < bottomIndex) { 2406c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* wrong segment */ 2416c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(INFO) << "Attempt to remove index outside index area (" << idx << " vs " << bottomIndex << "-" << topIndex << ")"; 2426c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 2436c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2446c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (idx >= topIndex) { 2456c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* bad -- stale reference? */ 2466c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(INFO) << "Attempt to remove invalid index " << idx << " (bottom=" << bottomIndex << " top=" << topIndex << ")"; 2476c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 2486c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2496c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2506c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (idx == topIndex-1) { 2516c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes // Top-most entry. Scan up and consume holes. 2526c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2536c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (workAroundAppJniBugs == false && !CheckEntry("remove", iref, idx)) { 2546c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 2556c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2566c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2576c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes table_[idx] = NULL; 2586c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes int numHoles = segmentState.parts.numHoles - prevState.parts.numHoles; 2596c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (numHoles != 0) { 2606c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes while (--topIndex > bottomIndex && numHoles != 0) { 2616c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes //LOG(INFO) << "+++ checking for hole at " << topIndex-1 << " (cookie=" << cookie << ") val=" << table_[topIndex-1]; 2626c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (table_[topIndex-1] != NULL) { 2636c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes break; 2646c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2656c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes //LOG(INFO) << "+++ ate hole at " << (topIndex-1); 2666c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes numHoles--; 2676c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2686c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.parts.numHoles = numHoles + prevState.parts.numHoles; 2696c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.parts.topIndex = topIndex; 2706c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } else { 2716c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.parts.topIndex = topIndex-1; 2726c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(INFO) << "+++ ate last entry " << topIndex-1; 2736c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2746c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } else { 2756c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes /* 2766c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * Not the top-most entry. This creates a hole. We NULL out the 2776c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * entry to prevent somebody from deleting it twice and screwing up 2786c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes * the hole count. 2796c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes */ 2806c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (table_[idx] == NULL) { 2816c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(INFO) << "--- WEIRD: removing null entry " << idx; 2826c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 2836c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2846c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes if (workAroundAppJniBugs == false && !CheckEntry("remove", iref, idx)) { 2856c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return false; 2866c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2876c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2886c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes table_[idx] = NULL; 2896c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes segmentState.parts.numHoles++; 2906c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes //LOG(INFO) << "+++ left hole at " << idx << ", holes=" << segmentState.parts.numHoles; 2916c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 2926c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2936c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return true; 2946c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 2956c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 2966c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesstd::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) { 2976c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes switch (rhs) { 2986c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes case kInvalid: 2996c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes os << "invalid reference"; 3006c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes break; 3016c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes case kLocal: 3026c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes os << "local reference"; 3036c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes break; 3046c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes case kGlobal: 3056c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes os << "global reference"; 3066c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes break; 3076c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes case kWeakGlobal: 3086c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes os << "weak global reference"; 3096c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes break; 3106c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes default: 3116c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes os << "IndirectRefKind[" << static_cast<int>(rhs) << "]"; 3126c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes break; 3136c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes } 3146c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes return os; 3156c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 3166c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 3176c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughesvoid IndirectReferenceTable::Dump() const { 3186c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes LOG(WARNING) << kind_ << " table dump:"; 3196c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes std::vector<Object*> entries(table_, table_ + Capacity()); 3206c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes ReferenceTable::Dump(entries); 3216c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} 3226c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes 3236c1a394b47c85c8d1723fc3b156a3b1b0b29a757Elliott Hughes} // namespace art 324