comparison src/share/vm/oops/arrayOop.hpp @ 4075:aa4c21b00f7f

7110152: assert(size_in_words <= (julong)max_jint) failed: no overflow Summary: Reduce what arrayOopDesc::max_array_length() returns to avoid int overflow Reviewed-by: kvn, dholmes, tonyp
author brutisso
date Tue, 15 Nov 2011 20:17:33 +0100
parents 6fd81579526f
children 3c648b9ad052
comparison
equal deleted inserted replaced
4074:ab5107bee78c 4075:aa4c21b00f7f
108 // 32 bit platforms when we convert it to a byte size. 108 // 32 bit platforms when we convert it to a byte size.
109 static int32_t max_array_length(BasicType type) { 109 static int32_t max_array_length(BasicType type) {
110 assert(type >= 0 && type < T_CONFLICT, "wrong type"); 110 assert(type >= 0 && type < T_CONFLICT, "wrong type");
111 assert(type2aelembytes(type) != 0, "wrong type"); 111 assert(type2aelembytes(type) != 0, "wrong type");
112 112
113 const size_t max_element_words_per_size_t = align_size_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment); 113 const size_t max_element_words_per_size_t =
114 const size_t max_elements_per_size_t = HeapWordSize * max_element_words_per_size_t / type2aelembytes(type); 114 align_size_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment);
115 const size_t max_elements_per_size_t =
116 HeapWordSize * max_element_words_per_size_t / type2aelembytes(type);
115 if ((size_t)max_jint < max_elements_per_size_t) { 117 if ((size_t)max_jint < max_elements_per_size_t) {
116 return max_jint; 118 // It should be ok to return max_jint here, but parts of the code
119 // (CollectedHeap, Klass::oop_oop_iterate(), and more) uses an int for
120 // passing around the size (in words) of an object. So, we need to avoid
121 // overflowing an int when we add the header. See CRs 4718400 and 7110613.
122 return align_size_down(max_jint - header_size(type), MinObjAlignment);
117 } 123 }
118 return (int32_t)max_elements_per_size_t; 124 return (int32_t)max_elements_per_size_t;
119 } 125 }
120 126
121 // for unit testing 127 // for unit testing