1573 }
1574
1575 return true;
1576 } // end rewrite_cp_refs()
1577
1578 // Rewrite constant pool references in the methods.
1579 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(
1580 instanceKlassHandle scratch_class, TRAPS) {
1581
1582 Array<Method*>* methods = scratch_class->methods();
1583
1584 if (methods == NULL || methods->length() == 0) {
1585 // no methods so nothing to do
1586 return true;
1587 }
1588
1589 // rewrite constant pool references in the methods:
1590 for (int i = methods->length() - 1; i >= 0; i--) {
1591 methodHandle method(THREAD, methods->at(i));
1592 methodHandle new_method;
1593 rewrite_cp_refs_in_method(method, &new_method, CHECK_false);
1594 if (!new_method.is_null()) {
1595 // the method has been replaced so save the new method version
1596 methods->at_put(i, new_method());
1597 }
1598 }
1599
1600 return true;
1601 }
1602
1603
1604 // Rewrite constant pool references in the specific method. This code
1605 // was adapted from Rewriter::rewrite_method().
1606 void VM_RedefineClasses::rewrite_cp_refs_in_method(methodHandle method,
1607 methodHandle *new_method_p, TRAPS) {
1608
1609 *new_method_p = methodHandle(); // default is no new method
1610
1611 // We cache a pointer to the bytecodes here in code_base. If GC
1612 // moves the Method*, then the bytecodes will also move which
1613 // will likely cause a crash. We create a No_Safepoint_Verifier
1614 // object to detect whether we pass a possible safepoint in this
1615 // code block.
1616 No_Safepoint_Verifier nsv;
1617
1618 // Bytecodes and their length
1657 RC_TRACE_WITH_THREAD(0x00080000, THREAD,
1658 ("%s->ldc_w@" INTPTR_FORMAT " old=%d, new=%d",
1659 Bytecodes::name(c), bcp, cp_index, new_index));
1660 // the new value needs ldc_w instead of ldc
1661 u_char inst_buffer[4]; // max instruction size is 4 bytes
1662 bcp = (address)inst_buffer;
1663 // construct new instruction sequence
1664 *bcp = Bytecodes::_ldc_w;
1665 bcp++;
1666 // Rewriter::rewrite_method() does not rewrite ldc -> ldc_w.
1667 // See comment below for difference between put_Java_u2()
1668 // and put_native_u2().
1669 Bytes::put_Java_u2(bcp, new_index);
1670
1671 Relocator rc(method, NULL /* no RelocatorListener needed */);
1672 methodHandle m;
1673 {
1674 Pause_No_Safepoint_Verifier pnsv(&nsv);
1675
1676 // ldc is 2 bytes and ldc_w is 3 bytes
1677 m = rc.insert_space_at(bci, 3, inst_buffer, THREAD);
1678 if (m.is_null() || HAS_PENDING_EXCEPTION) {
1679 guarantee(false, "insert_space_at() failed");
1680 }
1681 }
1682
1683 // return the new method so that the caller can update
1684 // the containing class
1685 *new_method_p = method = m;
1686 // switch our bytecode processing loop from the old method
1687 // to the new method
1688 code_base = method->code_base();
1689 code_length = method->code_size();
1690 bcp = code_base + bci;
1691 c = (Bytecodes::Code)(*bcp);
1692 bc_length = Bytecodes::length_for(c);
1693 assert(bc_length != 0, "sanity check");
1694 } // end we need ldc_w instead of ldc
1695 } // end if there is a mapped index
1696 } break;
1697
1698 // these bytecodes have a two-byte constant pool index
1699 case Bytecodes::_anewarray : // fall through
1700 case Bytecodes::_checkcast : // fall through
1701 case Bytecodes::_getfield : // fall through
|
1573 }
1574
1575 return true;
1576 } // end rewrite_cp_refs()
1577
1578 // Rewrite constant pool references in the methods.
1579 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(
1580 instanceKlassHandle scratch_class, TRAPS) {
1581
1582 Array<Method*>* methods = scratch_class->methods();
1583
1584 if (methods == NULL || methods->length() == 0) {
1585 // no methods so nothing to do
1586 return true;
1587 }
1588
1589 // rewrite constant pool references in the methods:
1590 for (int i = methods->length() - 1; i >= 0; i--) {
1591 methodHandle method(THREAD, methods->at(i));
1592 methodHandle new_method;
1593 rewrite_cp_refs_in_method(method, &new_method, THREAD);
1594 if (!new_method.is_null()) {
1595 // the method has been replaced so save the new method version
1596 // even in the case of an exception. original method is on the
1597 // deallocation list.
1598 methods->at_put(i, new_method());
1599 }
1600 if (HAS_PENDING_EXCEPTION) {
1601 CLEAR_PENDING_EXCEPTION;
1602 return false;
1603 }
1604 }
1605
1606 return true;
1607 }
1608
1609
1610 // Rewrite constant pool references in the specific method. This code
1611 // was adapted from Rewriter::rewrite_method().
1612 void VM_RedefineClasses::rewrite_cp_refs_in_method(methodHandle method,
1613 methodHandle *new_method_p, TRAPS) {
1614
1615 *new_method_p = methodHandle(); // default is no new method
1616
1617 // We cache a pointer to the bytecodes here in code_base. If GC
1618 // moves the Method*, then the bytecodes will also move which
1619 // will likely cause a crash. We create a No_Safepoint_Verifier
1620 // object to detect whether we pass a possible safepoint in this
1621 // code block.
1622 No_Safepoint_Verifier nsv;
1623
1624 // Bytecodes and their length
1663 RC_TRACE_WITH_THREAD(0x00080000, THREAD,
1664 ("%s->ldc_w@" INTPTR_FORMAT " old=%d, new=%d",
1665 Bytecodes::name(c), bcp, cp_index, new_index));
1666 // the new value needs ldc_w instead of ldc
1667 u_char inst_buffer[4]; // max instruction size is 4 bytes
1668 bcp = (address)inst_buffer;
1669 // construct new instruction sequence
1670 *bcp = Bytecodes::_ldc_w;
1671 bcp++;
1672 // Rewriter::rewrite_method() does not rewrite ldc -> ldc_w.
1673 // See comment below for difference between put_Java_u2()
1674 // and put_native_u2().
1675 Bytes::put_Java_u2(bcp, new_index);
1676
1677 Relocator rc(method, NULL /* no RelocatorListener needed */);
1678 methodHandle m;
1679 {
1680 Pause_No_Safepoint_Verifier pnsv(&nsv);
1681
1682 // ldc is 2 bytes and ldc_w is 3 bytes
1683 m = rc.insert_space_at(bci, 3, inst_buffer, CHECK);
1684 }
1685
1686 // return the new method so that the caller can update
1687 // the containing class
1688 *new_method_p = method = m;
1689 // switch our bytecode processing loop from the old method
1690 // to the new method
1691 code_base = method->code_base();
1692 code_length = method->code_size();
1693 bcp = code_base + bci;
1694 c = (Bytecodes::Code)(*bcp);
1695 bc_length = Bytecodes::length_for(c);
1696 assert(bc_length != 0, "sanity check");
1697 } // end we need ldc_w instead of ldc
1698 } // end if there is a mapped index
1699 } break;
1700
1701 // these bytecodes have a two-byte constant pool index
1702 case Bytecodes::_anewarray : // fall through
1703 case Bytecodes::_checkcast : // fall through
1704 case Bytecodes::_getfield : // fall through
|