Mercurial > hg > graal-jvmci-8
annotate src/share/vm/runtime/jniHandles.hpp @ 1716:be3f9c242c9d
6948538: CMS: BOT walkers can fall into object allocation and initialization cracks
Summary: GC workers now recognize an intermediate transient state of blocks which are allocated but have not yet completed initialization. blk_start() calls do not attempt to determine the size of a block in the transient state, rather waiting for the block to become initialized so that it is safe to query its size. Audited and ensured the order of initialization of object fields (klass, free bit and size) to respect block state transition protocol. Also included some new assertion checking code enabled in debug mode.
Reviewed-by: chrisphi, johnc, poonam
author | ysr |
---|---|
date | Mon, 16 Aug 2010 15:58:42 -0700 |
parents | c18cbe5936b8 |
children | f95d63e2154a |
rev | line source |
---|---|
0 | 1 /* |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1536
diff
changeset
|
2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1536
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1536
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1536
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 class JNIHandleBlock; | |
26 | |
27 | |
28 // Interface for creating and resolving local/global JNI handles | |
29 | |
30 class JNIHandles : AllStatic { | |
31 friend class VMStructs; | |
32 private: | |
33 static JNIHandleBlock* _global_handles; // First global handle block | |
34 static JNIHandleBlock* _weak_global_handles; // First weak global handle block | |
35 static oop _deleted_handle; // Sentinel marking deleted handles | |
36 | |
37 public: | |
38 // Resolve handle into oop | |
39 inline static oop resolve(jobject handle); | |
40 // Resolve externally provided handle into oop with some guards | |
41 inline static oop resolve_external_guard(jobject handle); | |
42 // Resolve handle into oop, result guaranteed not to be null | |
43 inline static oop resolve_non_null(jobject handle); | |
44 | |
45 // Local handles | |
46 static jobject make_local(oop obj); | |
47 static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known | |
48 static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known | |
49 inline static void destroy_local(jobject handle); | |
50 | |
51 // Global handles | |
52 static jobject make_global(Handle obj); | |
53 static void destroy_global(jobject handle); | |
54 | |
55 // Weak global handles | |
56 static jobject make_weak_global(Handle obj); | |
57 static void destroy_weak_global(jobject handle); | |
58 | |
59 // jmethodID handling (as Weak global handles). | |
60 // Because the useful life-span of a jmethodID cannot be determined, once created they are | |
61 // never reclaimed. The methods to which they refer, however, can be GC'ed away if the class | |
62 // is unloaded or if the method is made obsolete or deleted -- in these cases, the jmethodID | |
63 // refers to NULL (as is the case for any weak reference). | |
64 static jmethodID make_jmethod_id(methodHandle mh); | |
65 static void destroy_jmethod_id(jmethodID mid); | |
1535
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
66 // Use resolve_jmethod_id() in situations where the caller is expected |
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
67 // to provide a valid jmethodID; the only sanity checks are in asserts; |
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
68 // result guaranteed not to be NULL. |
0 | 69 inline static methodOop resolve_jmethod_id(jmethodID mid); |
1535
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
70 // Use checked_resolve_jmethod_id() in situations where the caller |
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
71 // should provide a valid jmethodID, but might not. NULL is returned |
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
72 // when the jmethodID does not refer to a valid method. |
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
73 inline static methodOop checked_resolve_jmethod_id(jmethodID mid); |
0 | 74 static void change_method_associated_with_jmethod_id(jmethodID jmid, methodHandle mh); |
75 | |
76 // Sentinel marking deleted handles in block. Note that we cannot store NULL as | |
77 // the sentinel, since clearing weak global JNI refs are done by storing NULL in | |
78 // the handle. The handle may not be reused before destroy_weak_global is called. | |
79 static oop deleted_handle() { return _deleted_handle; } | |
80 | |
81 // Initialization | |
82 static void initialize(); | |
83 | |
84 // Debugging | |
85 static void print_on(outputStream* st); | |
86 static void print() { print_on(tty); } | |
87 static void verify(); | |
88 static bool is_local_handle(Thread* thread, jobject handle); | |
89 static bool is_frame_handle(JavaThread* thr, jobject obj); | |
90 static bool is_global_handle(jobject handle); | |
91 static bool is_weak_global_handle(jobject handle); | |
92 static long global_handle_memory_usage(); | |
93 static long weak_global_handle_memory_usage(); | |
94 | |
95 // Garbage collection support(global handles only, local handles are traversed from thread) | |
96 // Traversal of regular global handles | |
97 static void oops_do(OopClosure* f); | |
98 // Traversal of weak global handles. Unreachable oops are cleared. | |
99 static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); | |
100 }; | |
101 | |
102 | |
103 | |
104 // JNI handle blocks holding local/global JNI handles | |
105 | |
106 class JNIHandleBlock : public CHeapObj { | |
107 friend class VMStructs; | |
1010 | 108 friend class CppInterpreter; |
109 | |
0 | 110 private: |
111 enum SomeConstants { | |
112 block_size_in_oops = 32 // Number of handles per handle block | |
113 }; | |
114 | |
115 oop _handles[block_size_in_oops]; // The handles | |
116 int _top; // Index of next unused handle | |
117 JNIHandleBlock* _next; // Link to next block | |
118 | |
119 // The following instance variables are only used by the first block in a chain. | |
120 // Having two types of blocks complicates the code and the space overhead in negligble. | |
121 JNIHandleBlock* _last; // Last block in use | |
122 JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call | |
123 oop* _free_list; // Handle free list | |
124 int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list | |
125 | |
126 #ifndef PRODUCT | |
127 JNIHandleBlock* _block_list_link; // Link for list below | |
128 static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only) | |
129 #endif | |
130 | |
131 static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks | |
132 static int _blocks_allocated; // For debugging/printing | |
133 | |
134 // Fill block with bad_handle values | |
135 void zap(); | |
136 | |
1010 | 137 protected: |
0 | 138 // No more handles in the both the current and following blocks |
139 void clear() { _top = 0; } | |
140 | |
1010 | 141 private: |
0 | 142 // Free list computation |
143 void rebuild_free_list(); | |
144 | |
145 public: | |
146 // Handle allocation | |
147 jobject allocate_handle(oop obj); | |
148 | |
149 // Block allocation and block free list management | |
150 static JNIHandleBlock* allocate_block(Thread* thread = NULL); | |
151 static void release_block(JNIHandleBlock* block, Thread* thread = NULL); | |
152 | |
153 // JNI PushLocalFrame/PopLocalFrame support | |
154 JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; } | |
155 void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; } | |
156 | |
157 // Stub generator support | |
158 static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); } | |
159 | |
160 // Garbage collection support | |
161 // Traversal of regular handles | |
162 void oops_do(OopClosure* f); | |
163 // Traversal of weak handles. Unreachable oops are cleared. | |
164 void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); | |
165 | |
166 // Debugging | |
167 bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle | |
168 bool contains(jobject handle) const; // Does this block contain handle | |
169 int length() const; // Length of chain starting with this block | |
170 long memory_usage() const; | |
171 #ifndef PRODUCT | |
172 static bool any_contains(jobject handle); // Does any block currently in use contain handle | |
173 static void print_statistics(); | |
174 #endif | |
175 }; | |
176 | |
177 | |
178 inline oop JNIHandles::resolve(jobject handle) { | |
179 oop result = (handle == NULL ? (oop)NULL : *(oop*)handle); | |
180 assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle"); | |
181 assert(result != badJNIHandle, "Pointing to zapped jni handle area"); | |
182 return result; | |
183 }; | |
184 | |
185 | |
186 inline oop JNIHandles::resolve_external_guard(jobject handle) { | |
187 if (handle == NULL) return NULL; | |
188 oop result = *(oop*)handle; | |
189 if (result == NULL || result == badJNIHandle) return NULL; | |
190 return result; | |
191 }; | |
192 | |
193 | |
194 inline oop JNIHandles::resolve_non_null(jobject handle) { | |
195 assert(handle != NULL, "JNI handle should not be null"); | |
196 oop result = *(oop*)handle; | |
197 assert(result != NULL, "Invalid value read from jni handle"); | |
198 assert(result != badJNIHandle, "Pointing to zapped jni handle area"); | |
199 // Don't let that private _deleted_handle object escape into the wild. | |
200 assert(result != deleted_handle(), "Used a deleted global handle."); | |
201 return result; | |
202 }; | |
203 | |
204 inline methodOop JNIHandles::resolve_jmethod_id(jmethodID mid) { | |
205 return (methodOop) resolve_non_null((jobject)mid); | |
206 }; | |
207 | |
208 inline methodOop JNIHandles::checked_resolve_jmethod_id(jmethodID mid) { | |
1535
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
209 oop o = resolve_external_guard((jobject) mid); |
d3562366cbfd
6949515: 3/3 VM crash when calling GetMethodDeclaringClass
dcubed
parents:
917
diff
changeset
|
210 if (o == NULL || !o->is_method()) { |
917
1760a1cbed36
6862945: 4/3 conversion of jmethodID to methodOop in JVMTI is too expensive
dcubed
parents:
0
diff
changeset
|
211 return (methodOop) NULL; |
1760a1cbed36
6862945: 4/3 conversion of jmethodID to methodOop in JVMTI is too expensive
dcubed
parents:
0
diff
changeset
|
212 } |
1760a1cbed36
6862945: 4/3 conversion of jmethodID to methodOop in JVMTI is too expensive
dcubed
parents:
0
diff
changeset
|
213 |
1760a1cbed36
6862945: 4/3 conversion of jmethodID to methodOop in JVMTI is too expensive
dcubed
parents:
0
diff
changeset
|
214 return (methodOop) o; |
0 | 215 }; |
216 | |
217 | |
218 inline void JNIHandles::destroy_local(jobject handle) { | |
219 if (handle != NULL) { | |
220 *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it | |
221 } | |
222 } |