Thu, 11 Dec 2008 12:05:08 -0800
6578152: fill_region_with_object has usability and safety issues
Reviewed-by: apetrusenko, ysr
1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_typeArrayKlass.cpp.incl"
28 bool typeArrayKlass::compute_is_subtype_of(klassOop k) {
29 if (!k->klass_part()->oop_is_typeArray()) {
30 return arrayKlass::compute_is_subtype_of(k);
31 }
33 typeArrayKlass* tak = typeArrayKlass::cast(k);
34 if (dimension() != tak->dimension()) return false;
36 return element_type() == tak->element_type();
37 }
39 klassOop typeArrayKlass::create_klass(BasicType type, int scale,
40 const char* name_str, TRAPS) {
41 typeArrayKlass o;
43 symbolHandle sym(symbolOop(NULL));
44 // bootstrapping: don't create sym if symbolKlass not created yet
45 if (Universe::symbolKlassObj() != NULL && name_str != NULL) {
46 sym = oopFactory::new_symbol_handle(name_str, CHECK_NULL);
47 }
48 KlassHandle klassklass (THREAD, Universe::typeArrayKlassKlassObj());
50 arrayKlassHandle k = base_create_array_klass(o.vtbl_value(), header_size(), klassklass, CHECK_NULL);
51 typeArrayKlass* ak = typeArrayKlass::cast(k());
52 ak->set_name(sym());
53 ak->set_layout_helper(array_layout_helper(type));
54 assert(scale == (1 << ak->log2_element_size()), "scale must check out");
55 assert(ak->oop_is_javaArray(), "sanity");
56 assert(ak->oop_is_typeArray(), "sanity");
57 ak->set_max_length(arrayOopDesc::max_array_length(type));
58 assert(k()->size() > header_size(), "bad size");
60 // Call complete_create_array_klass after all instance variables have been initialized.
61 KlassHandle super (THREAD, k->super());
62 complete_create_array_klass(k, super, CHECK_NULL);
64 return k();
65 }
67 typeArrayOop typeArrayKlass::allocate(int length, TRAPS) {
68 assert(log2_element_size() >= 0, "bad scale");
69 if (length >= 0) {
70 if (length <= max_length()) {
71 size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
72 KlassHandle h_k(THREAD, as_klassOop());
73 typeArrayOop t;
74 CollectedHeap* ch = Universe::heap();
75 if (size < ch->large_typearray_limit()) {
76 t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL);
77 } else {
78 t = (typeArrayOop)CollectedHeap::large_typearray_allocate(h_k, (int)size, length, CHECK_NULL);
79 }
80 assert(t->is_parsable(), "Don't publish unless parsable");
81 return t;
82 } else {
83 THROW_OOP_0(Universe::out_of_memory_error_array_size());
84 }
85 } else {
86 THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
87 }
88 }
90 typeArrayOop typeArrayKlass::allocate_permanent(int length, TRAPS) {
91 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
92 int size = typeArrayOopDesc::object_size(layout_helper(), length);
93 KlassHandle h_k(THREAD, as_klassOop());
94 typeArrayOop t = (typeArrayOop)
95 CollectedHeap::permanent_array_allocate(h_k, size, length, CHECK_NULL);
96 assert(t->is_parsable(), "Can't publish until parsable");
97 return t;
98 }
100 oop typeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) {
101 // For typeArrays this is only called for the last dimension
102 assert(rank == 1, "just checking");
103 int length = *last_size;
104 return allocate(length, THREAD);
105 }
108 void typeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
109 assert(s->is_typeArray(), "must be type array");
111 // Check destination
112 if (!d->is_typeArray() || element_type() != typeArrayKlass::cast(d->klass())->element_type()) {
113 THROW(vmSymbols::java_lang_ArrayStoreException());
114 }
116 // Check is all offsets and lengths are non negative
117 if (src_pos < 0 || dst_pos < 0 || length < 0) {
118 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
119 }
120 // Check if the ranges are valid
121 if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
122 || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) {
123 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
124 }
126 // This is an attempt to make the copy_array fast.
127 // NB: memmove takes care of overlapping memory segments.
128 // Potential problem: memmove is not guaranteed to be word atomic
129 // Revisit in Merlin
130 int l2es = log2_element_size();
131 int ihs = array_header_in_bytes() / wordSize;
132 char* src = (char*) ((oop*)s + ihs) + (src_pos << l2es);
133 char* dst = (char*) ((oop*)d + ihs) + (dst_pos << l2es);
134 memmove(dst, src, length << l2es);
135 }
138 // create a klass of array holding typeArrays
139 klassOop typeArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) {
140 typeArrayKlassHandle h_this(THREAD, as_klassOop());
141 return array_klass_impl(h_this, or_null, n, THREAD);
142 }
144 klassOop typeArrayKlass::array_klass_impl(typeArrayKlassHandle h_this, bool or_null, int n, TRAPS) {
145 int dimension = h_this->dimension();
146 assert(dimension <= n, "check order of chain");
147 if (dimension == n)
148 return h_this();
150 objArrayKlassHandle h_ak(THREAD, h_this->higher_dimension());
151 if (h_ak.is_null()) {
152 if (or_null) return NULL;
154 ResourceMark rm;
155 JavaThread *jt = (JavaThread *)THREAD;
156 {
157 MutexLocker mc(Compile_lock, THREAD); // for vtables
158 // Atomic create higher dimension and link into list
159 MutexLocker mu(MultiArray_lock, THREAD);
161 h_ak = objArrayKlassHandle(THREAD, h_this->higher_dimension());
162 if (h_ak.is_null()) {
163 klassOop oak = objArrayKlassKlass::cast(
164 Universe::objArrayKlassKlassObj())->allocate_objArray_klass(
165 dimension + 1, h_this, CHECK_NULL);
166 h_ak = objArrayKlassHandle(THREAD, oak);
167 h_ak->set_lower_dimension(h_this());
168 h_this->set_higher_dimension(h_ak());
169 assert(h_ak->oop_is_objArray(), "incorrect initialization of objArrayKlass");
170 }
171 }
172 } else {
173 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
174 }
175 if (or_null) {
176 return h_ak->array_klass_or_null(n);
177 }
178 return h_ak->array_klass(n, CHECK_NULL);
179 }
181 klassOop typeArrayKlass::array_klass_impl(bool or_null, TRAPS) {
182 return array_klass_impl(or_null, dimension() + 1, THREAD);
183 }
185 int typeArrayKlass::oop_size(oop obj) const {
186 assert(obj->is_typeArray(),"must be a type array");
187 typeArrayOop t = typeArrayOop(obj);
188 return t->object_size();
189 }
191 void typeArrayKlass::oop_follow_contents(oop obj) {
192 assert(obj->is_typeArray(),"must be a type array");
193 // Performance tweak: We skip iterating over the klass pointer since we
194 // know that Universe::typeArrayKlass never moves.
195 }
197 #ifndef SERIALGC
198 void typeArrayKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) {
199 assert(obj->is_typeArray(),"must be a type array");
200 // Performance tweak: We skip iterating over the klass pointer since we
201 // know that Universe::typeArrayKlass never moves.
202 }
203 #endif // SERIALGC
205 int typeArrayKlass::oop_adjust_pointers(oop obj) {
206 assert(obj->is_typeArray(),"must be a type array");
207 typeArrayOop t = typeArrayOop(obj);
208 // Performance tweak: We skip iterating over the klass pointer since we
209 // know that Universe::typeArrayKlass never moves.
210 return t->object_size();
211 }
213 int typeArrayKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
214 assert(obj->is_typeArray(),"must be a type array");
215 typeArrayOop t = typeArrayOop(obj);
216 // Performance tweak: We skip iterating over the klass pointer since we
217 // know that Universe::typeArrayKlass never moves.
218 return t->object_size();
219 }
221 int typeArrayKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
222 assert(obj->is_typeArray(),"must be a type array");
223 typeArrayOop t = typeArrayOop(obj);
224 // Performance tweak: We skip iterating over the klass pointer since we
225 // know that Universe::typeArrayKlass never moves.
226 return t->object_size();
227 }
229 #ifndef SERIALGC
230 void typeArrayKlass::oop_copy_contents(PSPromotionManager* pm, oop obj) {
231 assert(obj->is_typeArray(),"must be a type array");
232 }
234 void typeArrayKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
235 assert(obj->is_typeArray(),"must be a type array");
236 }
238 int
239 typeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
240 assert(obj->is_typeArray(),"must be a type array");
241 return typeArrayOop(obj)->object_size();
242 }
244 int
245 typeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
246 HeapWord* beg_addr, HeapWord* end_addr) {
247 assert(obj->is_typeArray(),"must be a type array");
248 return typeArrayOop(obj)->object_size();
249 }
250 #endif // SERIALGC
252 void typeArrayKlass::initialize(TRAPS) {
253 // Nothing to do. Having this function is handy since objArrayKlasses can be
254 // initialized by calling initialize on their bottom_klass, see objArrayKlass::initialize
255 }
257 const char* typeArrayKlass::external_name(BasicType type) {
258 switch (type) {
259 case T_BOOLEAN: return "[Z";
260 case T_CHAR: return "[C";
261 case T_FLOAT: return "[F";
262 case T_DOUBLE: return "[D";
263 case T_BYTE: return "[B";
264 case T_SHORT: return "[S";
265 case T_INT: return "[I";
266 case T_LONG: return "[J";
267 default: ShouldNotReachHere();
268 }
269 return NULL;
270 }
272 #ifndef PRODUCT
273 // Printing
275 static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) {
276 for (int index = 0; index < print_len; index++) {
277 st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true");
278 }
279 }
282 static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) {
283 for (int index = 0; index < print_len; index++) {
284 jchar c = ta->char_at(index);
285 st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
286 }
287 }
290 static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) {
291 for (int index = 0; index < print_len; index++) {
292 st->print_cr(" - %3d: %g", index, ta->float_at(index));
293 }
294 }
297 static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) {
298 for (int index = 0; index < print_len; index++) {
299 st->print_cr(" - %3d: %g", index, ta->double_at(index));
300 }
301 }
304 static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) {
305 for (int index = 0; index < print_len; index++) {
306 jbyte c = ta->byte_at(index);
307 st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
308 }
309 }
312 static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) {
313 for (int index = 0; index < print_len; index++) {
314 int v = ta->ushort_at(index);
315 st->print_cr(" - %3d: 0x%x\t %d", index, v, v);
316 }
317 }
320 static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) {
321 for (int index = 0; index < print_len; index++) {
322 jint v = ta->int_at(index);
323 st->print_cr(" - %3d: 0x%x %d", index, v, v);
324 }
325 }
328 static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) {
329 for (int index = 0; index < print_len; index++) {
330 jlong v = ta->long_at(index);
331 st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v));
332 }
333 }
336 void typeArrayKlass::oop_print_on(oop obj, outputStream* st) {
337 arrayKlass::oop_print_on(obj, st);
338 typeArrayOop ta = typeArrayOop(obj);
339 int print_len = MIN2((intx) ta->length(), MaxElementPrintSize);
340 switch (element_type()) {
341 case T_BOOLEAN: print_boolean_array(ta, print_len, st); break;
342 case T_CHAR: print_char_array(ta, print_len, st); break;
343 case T_FLOAT: print_float_array(ta, print_len, st); break;
344 case T_DOUBLE: print_double_array(ta, print_len, st); break;
345 case T_BYTE: print_byte_array(ta, print_len, st); break;
346 case T_SHORT: print_short_array(ta, print_len, st); break;
347 case T_INT: print_int_array(ta, print_len, st); break;
348 case T_LONG: print_long_array(ta, print_len, st); break;
349 default: ShouldNotReachHere();
350 }
351 int remaining = ta->length() - print_len;
352 if (remaining > 0) {
353 tty->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
354 }
355 }
357 #endif // PRODUCT
359 const char* typeArrayKlass::internal_name() const {
360 return Klass::external_name();
361 }