8149741: Don't refer to stub entry points by index in external_word relocations
authorvlivanov
Fri, 19 Feb 2016 20:41:36 +0300
changeset 36301 cb578d8c6cba
parent 36300 5b47f168b948
child 36302 23a79c43ba92
8149741: Don't refer to stub entry points by index in external_word relocations Reviewed-by: kvn
hotspot/src/cpu/x86/vm/templateInterpreterGenerator_x86.cpp
hotspot/src/os_cpu/windows_x86/vm/assembler_windows_x86.cpp
hotspot/src/share/vm/code/relocInfo.cpp
hotspot/src/share/vm/code/relocInfo.hpp
hotspot/src/share/vm/prims/jvmtiCodeBlobEvents.cpp
hotspot/src/share/vm/runtime/stubCodeGenerator.cpp
hotspot/src/share/vm/runtime/stubCodeGenerator.hpp
--- a/hotspot/src/cpu/x86/vm/templateInterpreterGenerator_x86.cpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/cpu/x86/vm/templateInterpreterGenerator_x86.cpp	Fri Feb 19 20:41:36 2016 +0300
@@ -161,13 +161,7 @@
                                      create_klass_exception),
                rarg, rarg2);
   } else {
-    // kind of lame ExternalAddress can't take NULL because
-    // external_word_Relocation will assert.
-    if (message != NULL) {
-      __ lea(rarg2, ExternalAddress((address)message));
-    } else {
-      __ movptr(rarg2, NULL_WORD);
-    }
+    __ lea(rarg2, ExternalAddress((address)message));
     __ call_VM(rax,
                CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),
                rarg, rarg2);
--- a/hotspot/src/os_cpu/windows_x86/vm/assembler_windows_x86.cpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/os_cpu/windows_x86/vm/assembler_windows_x86.cpp	Fri Feb 19 20:41:36 2016 +0300
@@ -51,11 +51,8 @@
 // movl reg, [reg + thread_ptr_offset]     Load thread
 //
 void MacroAssembler::get_thread(Register thread) {
-  // can't use ExternalAddress because it can't take NULL
-  AddressLiteral null(0, relocInfo::none);
-
   prefix(FS_segment);
-  movptr(thread, null);
+  movptr(thread, ExternalAddress(NULL));
   assert(os::win32::get_thread_ptr_offset() != 0,
          "Thread Pointer Offset has not been initialized");
   movl(thread, Address(thread, os::win32::get_thread_ptr_offset()));
--- a/hotspot/src/share/vm/code/relocInfo.cpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/share/vm/code/relocInfo.cpp	Fri Feb 19 20:41:36 2016 +0300
@@ -457,49 +457,6 @@
   return itr._rh;
 }
 
-int32_t Relocation::runtime_address_to_index(address runtime_address) {
-  assert(!is_reloc_index((intptr_t)runtime_address), "must not look like an index");
-
-  if (runtime_address == NULL)  return 0;
-
-  StubCodeDesc* p = StubCodeDesc::desc_for(runtime_address);
-  if (p != NULL && p->begin() == runtime_address) {
-    assert(is_reloc_index(p->index()), "there must not be too many stubs");
-    return (int32_t)p->index();
-  } else {
-    // Known "miscellaneous" non-stub pointers:
-    // os::get_polling_page(), SafepointSynchronize::address_of_state()
-    if (PrintRelocations) {
-      tty->print_cr("random unregistered address in relocInfo: " INTPTR_FORMAT, p2i(runtime_address));
-    }
-#ifndef _LP64
-    return (int32_t) (intptr_t)runtime_address;
-#else
-    // didn't fit return non-index
-    return -1;
-#endif /* _LP64 */
-  }
-}
-
-
-address Relocation::index_to_runtime_address(int32_t index) {
-  if (index == 0)  return NULL;
-
-  if (is_reloc_index(index)) {
-    StubCodeDesc* p = StubCodeDesc::desc_for_index(index);
-    assert(p != NULL, "there must be a stub for this index");
-    return p->begin();
-  } else {
-#ifndef _LP64
-    // this only works on 32bit machines
-    return (address) ((intptr_t) index);
-#else
-    fatal("Relocation::index_to_runtime_address, int32_t not pointer sized");
-    return NULL;
-#endif /* _LP64 */
-  }
-}
-
 address Relocation::old_addr_for(address newa,
                                  const CodeBuffer* src, CodeBuffer* dest) {
   int sect = dest->section_index_of(newa);
@@ -623,20 +580,13 @@
 
 void external_word_Relocation::pack_data_to(CodeSection* dest) {
   short* p = (short*) dest->locs_end();
-  int32_t index = runtime_address_to_index(_target);
 #ifndef _LP64
-  p = pack_1_int_to(p, index);
+  p = pack_1_int_to(p, (int32_t) (intptr_t)_target);
 #else
-  if (is_reloc_index(index)) {
-    p = pack_2_ints_to(p, index, 0);
-  } else {
-    jlong t = (jlong) _target;
-    int32_t lo = low(t);
-    int32_t hi = high(t);
-    p = pack_2_ints_to(p, lo, hi);
-    DEBUG_ONLY(jlong t1 = jlong_from(hi, lo));
-    assert(!is_reloc_index(t1) && (address) t1 == _target, "not symmetric");
-  }
+  jlong t = (jlong) _target;
+  int32_t lo = low(t);
+  int32_t hi = high(t);
+  p = pack_2_ints_to(p, lo, hi);
 #endif /* _LP64 */
   dest->set_locs_end((relocInfo*) p);
 }
@@ -644,16 +594,12 @@
 
 void external_word_Relocation::unpack_data() {
 #ifndef _LP64
-  _target = index_to_runtime_address(unpack_1_int());
+  _target = (address) (intptr_t)unpack_1_int();
 #else
   int32_t lo, hi;
   unpack_2_ints(lo, hi);
   jlong t = jlong_from(hi, lo);;
-  if (is_reloc_index(t)) {
-    _target = index_to_runtime_address(t);
-  } else {
-    _target = (address) t;
-  }
+  _target = (address) t;
 #endif /* _LP64 */
 }
 
--- a/hotspot/src/share/vm/code/relocInfo.hpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/share/vm/code/relocInfo.hpp	Fri Feb 19 20:41:36 2016 +0300
@@ -707,10 +707,6 @@
     assert(datalen()==0 || type()==relocInfo::none, "no data here");
   }
 
-  static bool is_reloc_index(intptr_t index) {
-    return 0 < index && index < os::vm_page_size();
-  }
-
  protected:
   // Helper functions for pack_data_to() and unpack_data().
 
@@ -806,10 +802,6 @@
     return base + byte_offset;
   }
 
-  // these convert between indexes and addresses in the runtime system
-  static int32_t runtime_address_to_index(address runtime_address);
-  static address index_to_runtime_address(int32_t index);
-
   // helpers for mapping between old and new addresses after a move or resize
   address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
   address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
@@ -1253,7 +1245,8 @@
   // Some address looking values aren't safe to treat as relocations
   // and should just be treated as constants.
   static bool can_be_relocated(address target) {
-    return target != NULL && !is_reloc_index((intptr_t)target);
+    assert(target == NULL || (uintptr_t)target >= (uintptr_t)os::vm_page_size(), INTPTR_FORMAT, (intptr_t)target);
+    return target != NULL;
   }
 
  private:
--- a/hotspot/src/share/vm/prims/jvmtiCodeBlobEvents.cpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/share/vm/prims/jvmtiCodeBlobEvents.cpp	Fri Feb 19 20:41:36 2016 +0300
@@ -173,9 +173,7 @@
   _global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
 
   // iterate over the stub code descriptors and put them in the list first.
-  int index = 0;
-  StubCodeDesc* desc;
-  while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
+  for (StubCodeDesc* desc = StubCodeDesc::first(); desc != NULL; desc = StubCodeDesc::next(desc)) {
     _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
   }
 
--- a/hotspot/src/share/vm/runtime/stubCodeGenerator.cpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/share/vm/runtime/stubCodeGenerator.cpp	Fri Feb 19 20:41:36 2016 +0300
@@ -36,19 +36,13 @@
 // Implementation of StubCodeDesc
 
 StubCodeDesc* StubCodeDesc::_list = NULL;
-int           StubCodeDesc::_count = 0;
 bool          StubCodeDesc::_frozen = false;
 
 StubCodeDesc* StubCodeDesc::desc_for(address pc) {
   StubCodeDesc* p = _list;
-  while (p != NULL && !p->contains(pc)) p = p->_next;
-  // p == NULL || p->contains(pc)
-  return p;
-}
-
-StubCodeDesc* StubCodeDesc::desc_for_index(int index) {
-  StubCodeDesc* p = _list;
-  while (p != NULL && p->index() != index) p = p->_next;
+  while (p != NULL && !p->contains(pc)) {
+    p = p->_next;
+  }
   return p;
 }
 
@@ -73,43 +67,17 @@
 // Implementation of StubCodeGenerator
 
 StubCodeGenerator::StubCodeGenerator(CodeBuffer* code, bool print_code) {
-  _masm = new MacroAssembler(code);
-  _first_stub = _last_stub = NULL;
-  _print_code = print_code;
-}
-
-extern "C" {
-  static int compare_cdesc(const void* void_a, const void* void_b) {
-    int ai = (*((StubCodeDesc**) void_a))->index();
-    int bi = (*((StubCodeDesc**) void_b))->index();
-    return ai - bi;
-  }
+  _masm = new MacroAssembler(code );
+  _print_code = PrintStubCode || print_code;
 }
 
 StubCodeGenerator::~StubCodeGenerator() {
-  if (PrintStubCode || _print_code) {
+  if (_print_code) {
     CodeBuffer* cbuf = _masm->code();
     CodeBlob*   blob = CodeCache::find_blob_unsafe(cbuf->insts()->start());
     if (blob != NULL) {
       blob->set_strings(cbuf->strings());
     }
-    bool saw_first = false;
-    StubCodeDesc* toprint[1000];
-    int toprint_len = 0;
-    for (StubCodeDesc* cdesc = _last_stub; cdesc != NULL; cdesc = cdesc->_next) {
-      toprint[toprint_len++] = cdesc;
-      if (cdesc == _first_stub) { saw_first = true; break; }
-    }
-    assert(toprint_len == 0 || saw_first, "must get both first & last");
-    // Print in reverse order:
-    qsort(toprint, toprint_len, sizeof(toprint[0]), compare_cdesc);
-    for (int i = 0; i < toprint_len; i++) {
-      StubCodeDesc* cdesc = toprint[i];
-      cdesc->print();
-      tty->cr();
-      Disassembler::decode(cdesc->begin(), cdesc->end());
-      tty->cr();
-    }
   }
 }
 
@@ -118,9 +86,12 @@
 }
 
 void StubCodeGenerator::stub_epilog(StubCodeDesc* cdesc) {
-  // default implementation - record the cdesc
-  if (_first_stub == NULL)  _first_stub = cdesc;
-  _last_stub = cdesc;
+  if (_print_code) {
+    cdesc->print();
+    tty->cr();
+    Disassembler::decode(cdesc->begin(), cdesc->end());
+    tty->cr();
+  }
 }
 
 
--- a/hotspot/src/share/vm/runtime/stubCodeGenerator.hpp	Fri Feb 19 20:40:20 2016 +0300
+++ b/hotspot/src/share/vm/runtime/stubCodeGenerator.hpp	Fri Feb 19 20:41:36 2016 +0300
@@ -39,13 +39,11 @@
 class StubCodeDesc: public CHeapObj<mtCode> {
  private:
   static StubCodeDesc* _list;                  // the list of all descriptors
-  static int           _count;                 // length of list
   static bool          _frozen;                // determines whether _list modifications are allowed
 
   StubCodeDesc*        _next;                  // the next element in the linked list
   const char*          _group;                 // the group to which the stub code belongs
   const char*          _name;                  // the name assigned to the stub code
-  int                  _index;                 // serial number assigned to the stub
   address              _begin;                 // points to the first byte of the stub code    (included)
   address              _end;                   // points to the first byte after the stub code (excluded)
 
@@ -64,8 +62,10 @@
   friend class StubCodeGenerator;
 
  public:
+  static StubCodeDesc* first() { return _list; }
+  static StubCodeDesc* next(StubCodeDesc* desc)  { return desc->_next; }
+
   static StubCodeDesc* desc_for(address pc);     // returns the code descriptor for the code containing pc or NULL
-  static StubCodeDesc* desc_for_index(int);      // returns the code descriptor for the index or NULL
   static const char*   name_for(address pc);     // returns the name of the code containing pc or NULL
 
   StubCodeDesc(const char* group, const char* name, address begin, address end = NULL) {
@@ -74,7 +74,6 @@
     _next           = _list;
     _group          = group;
     _name           = name;
-    _index          = ++_count; // (never zero)
     _begin          = begin;
     _end            = end;
     _list           = this;
@@ -84,7 +83,6 @@
 
   const char* group() const                      { return _group; }
   const char* name() const                       { return _name; }
-  int         index() const                      { return _index; }
   address     begin() const                      { return _begin; }
   address     end() const                        { return _end; }
   int         size_in_bytes() const              { return _end - _begin; }
@@ -97,13 +95,12 @@
 // Provides utility functions.
 
 class StubCodeGenerator: public StackObj {
+ private:
+  bool _print_code;
+
  protected:
   MacroAssembler*  _masm;
 
-  StubCodeDesc* _first_stub;
-  StubCodeDesc* _last_stub;
-  bool _print_code;
-
  public:
   StubCodeGenerator(CodeBuffer* code, bool print_code = false);
   ~StubCodeGenerator();