Fri, 15 Apr 2011 09:36:28 -0400
7032407: Crash in LinkResolver::runtime_resolve_virtual_method()
Summary: Make CDS reorder vtables so that dump time vtables match run time order, so when redefine classes reinitializes them, they aren't in the wrong order.
Reviewed-by: dcubed, acorn
duke@435 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #ifndef SHARE_VM_MEMORY_RESOURCEAREA_HPP |
stefank@2314 | 26 | #define SHARE_VM_MEMORY_RESOURCEAREA_HPP |
stefank@2314 | 27 | |
stefank@2314 | 28 | #include "memory/allocation.hpp" |
stefank@2314 | 29 | #ifdef TARGET_OS_FAMILY_linux |
stefank@2314 | 30 | # include "thread_linux.inline.hpp" |
stefank@2314 | 31 | #endif |
stefank@2314 | 32 | #ifdef TARGET_OS_FAMILY_solaris |
stefank@2314 | 33 | # include "thread_solaris.inline.hpp" |
stefank@2314 | 34 | #endif |
stefank@2314 | 35 | #ifdef TARGET_OS_FAMILY_windows |
stefank@2314 | 36 | # include "thread_windows.inline.hpp" |
stefank@2314 | 37 | #endif |
stefank@2314 | 38 | |
duke@435 | 39 | // The resource area holds temporary data structures in the VM. |
duke@435 | 40 | // The actual allocation areas are thread local. Typical usage: |
duke@435 | 41 | // |
duke@435 | 42 | // ... |
duke@435 | 43 | // { |
duke@435 | 44 | // ResourceMark rm; |
duke@435 | 45 | // int foo[] = NEW_RESOURCE_ARRAY(int, 64); |
duke@435 | 46 | // ... |
duke@435 | 47 | // } |
duke@435 | 48 | // ... |
duke@435 | 49 | |
duke@435 | 50 | //------------------------------ResourceArea----------------------------------- |
duke@435 | 51 | // A ResourceArea is an Arena that supports safe usage of ResourceMark. |
duke@435 | 52 | class ResourceArea: public Arena { |
duke@435 | 53 | friend class ResourceMark; |
duke@435 | 54 | friend class DeoptResourceMark; |
duke@435 | 55 | debug_only(int _nesting;) // current # of nested ResourceMarks |
duke@435 | 56 | debug_only(static int _warned;) // to suppress multiple warnings |
duke@435 | 57 | |
duke@435 | 58 | public: |
duke@435 | 59 | ResourceArea() { |
duke@435 | 60 | debug_only(_nesting = 0;) |
duke@435 | 61 | } |
duke@435 | 62 | |
duke@435 | 63 | ResourceArea(size_t init_size) : Arena(init_size) { |
duke@435 | 64 | debug_only(_nesting = 0;); |
duke@435 | 65 | } |
duke@435 | 66 | |
duke@435 | 67 | char* allocate_bytes(size_t size) { |
duke@435 | 68 | #ifdef ASSERT |
duke@435 | 69 | if (_nesting < 1 && !_warned++) |
duke@435 | 70 | fatal("memory leak: allocating without ResourceMark"); |
duke@435 | 71 | if (UseMallocOnly) { |
duke@435 | 72 | // use malloc, but save pointer in res. area for later freeing |
duke@435 | 73 | char** save = (char**)internal_malloc_4(sizeof(char*)); |
duke@435 | 74 | return (*save = (char*)os::malloc(size)); |
duke@435 | 75 | } |
duke@435 | 76 | #endif |
duke@435 | 77 | return (char*)Amalloc(size); |
duke@435 | 78 | } |
duke@435 | 79 | |
duke@435 | 80 | debug_only(int nesting() const { return _nesting; }); |
duke@435 | 81 | }; |
duke@435 | 82 | |
duke@435 | 83 | |
duke@435 | 84 | //------------------------------ResourceMark----------------------------------- |
duke@435 | 85 | // A resource mark releases all resources allocated after it was constructed |
duke@435 | 86 | // when the destructor is called. Typically used as a local variable. |
duke@435 | 87 | class ResourceMark: public StackObj { |
duke@435 | 88 | protected: |
duke@435 | 89 | ResourceArea *_area; // Resource area to stack allocate |
duke@435 | 90 | Chunk *_chunk; // saved arena chunk |
duke@435 | 91 | char *_hwm, *_max; |
duke@435 | 92 | NOT_PRODUCT(size_t _size_in_bytes;) |
duke@435 | 93 | |
duke@435 | 94 | void initialize(Thread *thread) { |
duke@435 | 95 | _area = thread->resource_area(); |
duke@435 | 96 | _chunk = _area->_chunk; |
duke@435 | 97 | _hwm = _area->_hwm; |
duke@435 | 98 | _max= _area->_max; |
duke@435 | 99 | NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) |
duke@435 | 100 | debug_only(_area->_nesting++;) |
duke@435 | 101 | assert( _area->_nesting > 0, "must stack allocate RMs" ); |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | public: |
duke@435 | 105 | |
duke@435 | 106 | #ifndef ASSERT |
duke@435 | 107 | ResourceMark(Thread *thread) { |
duke@435 | 108 | assert(thread == Thread::current(), "not the current thread"); |
duke@435 | 109 | initialize(thread); |
duke@435 | 110 | } |
duke@435 | 111 | #else |
duke@435 | 112 | ResourceMark(Thread *thread); |
duke@435 | 113 | #endif // ASSERT |
duke@435 | 114 | |
duke@435 | 115 | ResourceMark() { initialize(Thread::current()); } |
duke@435 | 116 | |
duke@435 | 117 | ResourceMark( ResourceArea *r ) : |
duke@435 | 118 | _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { |
duke@435 | 119 | NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) |
duke@435 | 120 | debug_only(_area->_nesting++;) |
duke@435 | 121 | assert( _area->_nesting > 0, "must stack allocate RMs" ); |
duke@435 | 122 | } |
duke@435 | 123 | |
duke@435 | 124 | void reset_to_mark() { |
duke@435 | 125 | if (UseMallocOnly) free_malloced_objects(); |
duke@435 | 126 | |
duke@435 | 127 | if( _chunk->next() ) // Delete later chunks |
duke@435 | 128 | _chunk->next_chop(); |
duke@435 | 129 | _area->_chunk = _chunk; // Roll back arena to saved chunk |
duke@435 | 130 | _area->_hwm = _hwm; |
duke@435 | 131 | _area->_max = _max; |
duke@435 | 132 | |
duke@435 | 133 | // clear out this chunk (to detect allocation bugs) |
duke@435 | 134 | if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); |
duke@435 | 135 | _area->set_size_in_bytes(size_in_bytes()); |
duke@435 | 136 | } |
duke@435 | 137 | |
duke@435 | 138 | ~ResourceMark() { |
duke@435 | 139 | assert( _area->_nesting > 0, "must stack allocate RMs" ); |
duke@435 | 140 | debug_only(_area->_nesting--;) |
duke@435 | 141 | reset_to_mark(); |
duke@435 | 142 | } |
duke@435 | 143 | |
duke@435 | 144 | |
duke@435 | 145 | private: |
duke@435 | 146 | void free_malloced_objects() PRODUCT_RETURN; |
duke@435 | 147 | size_t size_in_bytes() NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0; |
duke@435 | 148 | }; |
duke@435 | 149 | |
duke@435 | 150 | //------------------------------DeoptResourceMark----------------------------------- |
duke@435 | 151 | // A deopt resource mark releases all resources allocated after it was constructed |
duke@435 | 152 | // when the destructor is called. Typically used as a local variable. It differs |
duke@435 | 153 | // from a typical resource more in that it is C-Heap allocated so that deoptimization |
duke@435 | 154 | // can use data structures that are arena based but are not amenable to vanilla |
duke@435 | 155 | // ResourceMarks because deoptimization can not use a stack allocated mark. During |
duke@435 | 156 | // deoptimization we go thru the following steps: |
duke@435 | 157 | // |
duke@435 | 158 | // 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info |
duke@435 | 159 | // 1: create the vframeArray (contains pointers to Resource allocated structures) |
duke@435 | 160 | // This allocates the DeoptResourceMark. |
duke@435 | 161 | // 2: return to assembly stub and remove stub frame and deoptee frame and create |
duke@435 | 162 | // the new skeletal frames. |
duke@435 | 163 | // 3: push new stub frame and call unpack_frames |
duke@435 | 164 | // 4: retrieve information from the vframeArray to populate the skeletal frames |
duke@435 | 165 | // 5: release the DeoptResourceMark |
duke@435 | 166 | // 6: return to stub and eventually to interpreter |
duke@435 | 167 | // |
duke@435 | 168 | // With old style eager deoptimization the vframeArray was created by the vmThread there |
duke@435 | 169 | // was no way for the vframeArray to contain resource allocated objects and so |
duke@435 | 170 | // a complex set of data structures to simulate an array of vframes in CHeap memory |
duke@435 | 171 | // was used. With new style lazy deoptimization the vframeArray is created in the |
duke@435 | 172 | // the thread that will use it and we can use a much simpler scheme for the vframeArray |
duke@435 | 173 | // leveraging existing data structures if we simply create a way to manage this one |
duke@435 | 174 | // special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj |
duke@435 | 175 | // then existing ResourceMarks would work fine since no one use new to allocate them |
duke@435 | 176 | // and they would be stack allocated. This leaves open the possibilty of accidental |
duke@435 | 177 | // misuse so we simple duplicate the ResourceMark functionality here. |
duke@435 | 178 | |
duke@435 | 179 | class DeoptResourceMark: public CHeapObj { |
duke@435 | 180 | protected: |
duke@435 | 181 | ResourceArea *_area; // Resource area to stack allocate |
duke@435 | 182 | Chunk *_chunk; // saved arena chunk |
duke@435 | 183 | char *_hwm, *_max; |
duke@435 | 184 | NOT_PRODUCT(size_t _size_in_bytes;) |
duke@435 | 185 | |
duke@435 | 186 | void initialize(Thread *thread) { |
duke@435 | 187 | _area = thread->resource_area(); |
duke@435 | 188 | _chunk = _area->_chunk; |
duke@435 | 189 | _hwm = _area->_hwm; |
duke@435 | 190 | _max= _area->_max; |
duke@435 | 191 | NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) |
duke@435 | 192 | debug_only(_area->_nesting++;) |
duke@435 | 193 | assert( _area->_nesting > 0, "must stack allocate RMs" ); |
duke@435 | 194 | } |
duke@435 | 195 | |
duke@435 | 196 | public: |
duke@435 | 197 | |
duke@435 | 198 | #ifndef ASSERT |
duke@435 | 199 | DeoptResourceMark(Thread *thread) { |
duke@435 | 200 | assert(thread == Thread::current(), "not the current thread"); |
duke@435 | 201 | initialize(thread); |
duke@435 | 202 | } |
duke@435 | 203 | #else |
duke@435 | 204 | DeoptResourceMark(Thread *thread); |
duke@435 | 205 | #endif // ASSERT |
duke@435 | 206 | |
duke@435 | 207 | DeoptResourceMark() { initialize(Thread::current()); } |
duke@435 | 208 | |
duke@435 | 209 | DeoptResourceMark( ResourceArea *r ) : |
duke@435 | 210 | _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { |
duke@435 | 211 | NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) |
duke@435 | 212 | debug_only(_area->_nesting++;) |
duke@435 | 213 | assert( _area->_nesting > 0, "must stack allocate RMs" ); |
duke@435 | 214 | } |
duke@435 | 215 | |
duke@435 | 216 | void reset_to_mark() { |
duke@435 | 217 | if (UseMallocOnly) free_malloced_objects(); |
duke@435 | 218 | |
duke@435 | 219 | if( _chunk->next() ) // Delete later chunks |
duke@435 | 220 | _chunk->next_chop(); |
duke@435 | 221 | _area->_chunk = _chunk; // Roll back arena to saved chunk |
duke@435 | 222 | _area->_hwm = _hwm; |
duke@435 | 223 | _area->_max = _max; |
duke@435 | 224 | |
duke@435 | 225 | // clear out this chunk (to detect allocation bugs) |
duke@435 | 226 | if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); |
duke@435 | 227 | _area->set_size_in_bytes(size_in_bytes()); |
duke@435 | 228 | } |
duke@435 | 229 | |
duke@435 | 230 | ~DeoptResourceMark() { |
duke@435 | 231 | assert( _area->_nesting > 0, "must stack allocate RMs" ); |
duke@435 | 232 | debug_only(_area->_nesting--;) |
duke@435 | 233 | reset_to_mark(); |
duke@435 | 234 | } |
duke@435 | 235 | |
duke@435 | 236 | |
duke@435 | 237 | private: |
duke@435 | 238 | void free_malloced_objects() PRODUCT_RETURN; |
duke@435 | 239 | size_t size_in_bytes() NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0; |
duke@435 | 240 | }; |
stefank@2314 | 241 | |
stefank@2314 | 242 | #endif // SHARE_VM_MEMORY_RESOURCEAREA_HPP |