Wed, 02 Nov 2011 08:04:23 +0100
7106751: G1: gc/gctests/nativeGC03 crashes VM with SIGSEGV
Summary: _cset_rs_update_cl[] was indexed with values beyond what it is set up to handle.
Reviewed-by: ysr, jmasa, johnc
ysr@777 | 1 | /* |
tonyp@2453 | 2 | * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. |
ysr@777 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ysr@777 | 4 | * |
ysr@777 | 5 | * This code is free software; you can redistribute it and/or modify it |
ysr@777 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ysr@777 | 7 | * published by the Free Software Foundation. |
ysr@777 | 8 | * |
ysr@777 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ysr@777 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ysr@777 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ysr@777 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ysr@777 | 13 | * accompanied this code). |
ysr@777 | 14 | * |
ysr@777 | 15 | * You should have received a copy of the GNU General Public License version |
ysr@777 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ysr@777 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ysr@777 | 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. |
ysr@777 | 22 | * |
ysr@777 | 23 | */ |
ysr@777 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
tonyp@2963 | 26 | #include "gc_implementation/g1/heapRegion.hpp" |
tonyp@2963 | 27 | #include "gc_implementation/g1/heapRegionSeq.inline.hpp" |
tonyp@2963 | 28 | #include "gc_implementation/g1/heapRegionSets.hpp" |
stefank@2314 | 29 | #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" |
stefank@2314 | 30 | #include "memory/allocation.hpp" |
ysr@777 | 31 | |
tonyp@2963 | 32 | // Private |
ysr@777 | 33 | |
tonyp@2963 | 34 | size_t HeapRegionSeq::find_contiguous_from(size_t from, size_t num) { |
tonyp@2963 | 35 | size_t len = length(); |
tonyp@2963 | 36 | assert(num > 1, "use this only for sequences of length 2 or greater"); |
tonyp@2963 | 37 | assert(from <= len, |
tonyp@2963 | 38 | err_msg("from: "SIZE_FORMAT" should be valid and <= than "SIZE_FORMAT, |
tonyp@2963 | 39 | from, len)); |
ysr@777 | 40 | |
tonyp@2963 | 41 | size_t curr = from; |
tonyp@2963 | 42 | size_t first = G1_NULL_HRS_INDEX; |
tonyp@2472 | 43 | size_t num_so_far = 0; |
tonyp@2963 | 44 | while (curr < len && num_so_far < num) { |
tonyp@2963 | 45 | if (at(curr)->is_empty()) { |
tonyp@2963 | 46 | if (first == G1_NULL_HRS_INDEX) { |
tonyp@2472 | 47 | first = curr; |
tonyp@2472 | 48 | num_so_far = 1; |
tonyp@2472 | 49 | } else { |
tonyp@2472 | 50 | num_so_far += 1; |
tonyp@2472 | 51 | } |
tonyp@2472 | 52 | } else { |
tonyp@2963 | 53 | first = G1_NULL_HRS_INDEX; |
tonyp@2472 | 54 | num_so_far = 0; |
tonyp@2472 | 55 | } |
tonyp@2472 | 56 | curr += 1; |
tonyp@2472 | 57 | } |
tonyp@2472 | 58 | assert(num_so_far <= num, "post-condition"); |
tonyp@2472 | 59 | if (num_so_far == num) { |
tonyp@2643 | 60 | // we found enough space for the humongous object |
tonyp@2963 | 61 | assert(from <= first && first < len, "post-condition"); |
tonyp@2963 | 62 | assert(first < curr && (curr - first) == num, "post-condition"); |
tonyp@2963 | 63 | for (size_t i = first; i < first + num; ++i) { |
tonyp@2963 | 64 | assert(at(i)->is_empty(), "post-condition"); |
tonyp@2472 | 65 | } |
tonyp@2472 | 66 | return first; |
tonyp@2472 | 67 | } else { |
tonyp@2472 | 68 | // we failed to find enough space for the humongous object |
tonyp@2963 | 69 | return G1_NULL_HRS_INDEX; |
tonyp@2472 | 70 | } |
tonyp@2472 | 71 | } |
tonyp@2472 | 72 | |
tonyp@2963 | 73 | // Public |
tonyp@2472 | 74 | |
tonyp@2963 | 75 | void HeapRegionSeq::initialize(HeapWord* bottom, HeapWord* end, |
tonyp@2963 | 76 | size_t max_length) { |
tonyp@2963 | 77 | assert((size_t) bottom % HeapRegion::GrainBytes == 0, |
tonyp@2963 | 78 | "bottom should be heap region aligned"); |
tonyp@2963 | 79 | assert((size_t) end % HeapRegion::GrainBytes == 0, |
tonyp@2963 | 80 | "end should be heap region aligned"); |
tonyp@2963 | 81 | |
tonyp@2963 | 82 | _length = 0; |
tonyp@2963 | 83 | _heap_bottom = bottom; |
tonyp@2963 | 84 | _heap_end = end; |
tonyp@2963 | 85 | _region_shift = HeapRegion::LogOfHRGrainBytes; |
tonyp@2963 | 86 | _next_search_index = 0; |
tonyp@2963 | 87 | _allocated_length = 0; |
tonyp@2963 | 88 | _max_length = max_length; |
tonyp@2963 | 89 | |
tonyp@2963 | 90 | _regions = NEW_C_HEAP_ARRAY(HeapRegion*, max_length); |
tonyp@2963 | 91 | memset(_regions, 0, max_length * sizeof(HeapRegion*)); |
tonyp@2963 | 92 | _regions_biased = _regions - ((size_t) bottom >> _region_shift); |
tonyp@2963 | 93 | |
tonyp@2963 | 94 | assert(&_regions[0] == &_regions_biased[addr_to_index_biased(bottom)], |
tonyp@2963 | 95 | "bottom should be included in the region with index 0"); |
tonyp@2963 | 96 | } |
tonyp@2963 | 97 | |
tonyp@2963 | 98 | MemRegion HeapRegionSeq::expand_by(HeapWord* old_end, |
tonyp@2963 | 99 | HeapWord* new_end, |
tonyp@2963 | 100 | FreeRegionList* list) { |
tonyp@2963 | 101 | assert(old_end < new_end, "don't call it otherwise"); |
tonyp@2963 | 102 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
tonyp@2963 | 103 | |
tonyp@2963 | 104 | HeapWord* next_bottom = old_end; |
tonyp@2963 | 105 | assert(_heap_bottom <= next_bottom, "invariant"); |
tonyp@2963 | 106 | while (next_bottom < new_end) { |
tonyp@2963 | 107 | assert(next_bottom < _heap_end, "invariant"); |
tonyp@2963 | 108 | size_t index = length(); |
tonyp@2963 | 109 | |
tonyp@2963 | 110 | assert(index < _max_length, "otherwise we cannot expand further"); |
tonyp@2963 | 111 | if (index == 0) { |
tonyp@2963 | 112 | // We have not allocated any regions so far |
tonyp@2963 | 113 | assert(next_bottom == _heap_bottom, "invariant"); |
tonyp@2963 | 114 | } else { |
tonyp@2963 | 115 | // next_bottom should match the end of the last/previous region |
tonyp@2963 | 116 | assert(next_bottom == at(index - 1)->end(), "invariant"); |
tonyp@2963 | 117 | } |
tonyp@2963 | 118 | |
tonyp@2963 | 119 | if (index == _allocated_length) { |
tonyp@2963 | 120 | // We have to allocate a new HeapRegion. |
tonyp@2963 | 121 | HeapRegion* new_hr = g1h->new_heap_region(index, next_bottom); |
tonyp@2963 | 122 | if (new_hr == NULL) { |
tonyp@2963 | 123 | // allocation failed, we bail out and return what we have done so far |
tonyp@2963 | 124 | return MemRegion(old_end, next_bottom); |
tonyp@2963 | 125 | } |
tonyp@2963 | 126 | assert(_regions[index] == NULL, "invariant"); |
tonyp@2963 | 127 | _regions[index] = new_hr; |
tonyp@2963 | 128 | increment_length(&_allocated_length); |
tonyp@2963 | 129 | } |
tonyp@2963 | 130 | // Have to increment the length first, otherwise we will get an |
tonyp@2963 | 131 | // assert failure at(index) below. |
tonyp@2963 | 132 | increment_length(&_length); |
tonyp@2963 | 133 | HeapRegion* hr = at(index); |
tonyp@2963 | 134 | list->add_as_tail(hr); |
tonyp@2963 | 135 | |
tonyp@2963 | 136 | next_bottom = hr->end(); |
tonyp@2472 | 137 | } |
tonyp@2963 | 138 | assert(next_bottom == new_end, "post-condition"); |
tonyp@2963 | 139 | return MemRegion(old_end, next_bottom); |
tonyp@2963 | 140 | } |
tonyp@2963 | 141 | |
tonyp@2963 | 142 | size_t HeapRegionSeq::free_suffix() { |
tonyp@2963 | 143 | size_t res = 0; |
tonyp@2963 | 144 | size_t index = length(); |
tonyp@2963 | 145 | while (index > 0) { |
tonyp@2963 | 146 | index -= 1; |
tonyp@2963 | 147 | if (!at(index)->is_empty()) { |
tonyp@2963 | 148 | break; |
tonyp@2963 | 149 | } |
tonyp@2963 | 150 | res += 1; |
tonyp@2472 | 151 | } |
ysr@777 | 152 | return res; |
ysr@777 | 153 | } |
ysr@777 | 154 | |
tonyp@2963 | 155 | size_t HeapRegionSeq::find_contiguous(size_t num) { |
tonyp@2963 | 156 | assert(num > 1, "use this only for sequences of length 2 or greater"); |
tonyp@2963 | 157 | assert(_next_search_index <= length(), |
tonyp@2963 | 158 | err_msg("_next_search_indeex: "SIZE_FORMAT" " |
tonyp@2963 | 159 | "should be valid and <= than "SIZE_FORMAT, |
tonyp@2963 | 160 | _next_search_index, length())); |
tonyp@2963 | 161 | |
tonyp@2963 | 162 | size_t start = _next_search_index; |
tonyp@2963 | 163 | size_t res = find_contiguous_from(start, num); |
tonyp@2963 | 164 | if (res == G1_NULL_HRS_INDEX && start > 0) { |
tonyp@2963 | 165 | // Try starting from the beginning. If _next_search_index was 0, |
tonyp@2963 | 166 | // no point in doing this again. |
tonyp@2963 | 167 | res = find_contiguous_from(0, num); |
tonyp@2963 | 168 | } |
tonyp@2963 | 169 | if (res != G1_NULL_HRS_INDEX) { |
tonyp@2963 | 170 | assert(res < length(), |
tonyp@2963 | 171 | err_msg("res: "SIZE_FORMAT" should be valid", res)); |
tonyp@2963 | 172 | _next_search_index = res + num; |
tonyp@2963 | 173 | assert(_next_search_index <= length(), |
tonyp@2963 | 174 | err_msg("_next_search_indeex: "SIZE_FORMAT" " |
tonyp@2963 | 175 | "should be valid and <= than "SIZE_FORMAT, |
tonyp@2963 | 176 | _next_search_index, length())); |
tonyp@2963 | 177 | } |
tonyp@2963 | 178 | return res; |
ysr@777 | 179 | } |
ysr@777 | 180 | |
tonyp@2963 | 181 | void HeapRegionSeq::iterate(HeapRegionClosure* blk) const { |
tonyp@2963 | 182 | iterate_from((HeapRegion*) NULL, blk); |
tonyp@2963 | 183 | } |
ysr@777 | 184 | |
tonyp@2963 | 185 | void HeapRegionSeq::iterate_from(HeapRegion* hr, HeapRegionClosure* blk) const { |
tonyp@2963 | 186 | size_t hr_index = 0; |
tonyp@2963 | 187 | if (hr != NULL) { |
tonyp@2963 | 188 | hr_index = (size_t) hr->hrs_index(); |
tonyp@2963 | 189 | } |
ysr@777 | 190 | |
tonyp@2963 | 191 | size_t len = length(); |
tonyp@2963 | 192 | for (size_t i = hr_index; i < len; i += 1) { |
tonyp@2963 | 193 | bool res = blk->doHeapRegion(at(i)); |
ysr@777 | 194 | if (res) { |
ysr@777 | 195 | blk->incomplete(); |
ysr@777 | 196 | return; |
ysr@777 | 197 | } |
ysr@777 | 198 | } |
tonyp@2963 | 199 | for (size_t i = 0; i < hr_index; i += 1) { |
tonyp@2963 | 200 | bool res = blk->doHeapRegion(at(i)); |
ysr@777 | 201 | if (res) { |
ysr@777 | 202 | blk->incomplete(); |
ysr@777 | 203 | return; |
ysr@777 | 204 | } |
ysr@777 | 205 | } |
ysr@777 | 206 | } |
ysr@777 | 207 | |
ysr@777 | 208 | MemRegion HeapRegionSeq::shrink_by(size_t shrink_bytes, |
tonyp@2963 | 209 | size_t* num_regions_deleted) { |
tonyp@2472 | 210 | // Reset this in case it's currently pointing into the regions that |
tonyp@2472 | 211 | // we just removed. |
tonyp@2963 | 212 | _next_search_index = 0; |
tonyp@2472 | 213 | |
ysr@777 | 214 | assert(shrink_bytes % os::vm_page_size() == 0, "unaligned"); |
ysr@777 | 215 | assert(shrink_bytes % HeapRegion::GrainBytes == 0, "unaligned"); |
tonyp@2963 | 216 | assert(length() > 0, "the region sequence should not be empty"); |
tonyp@2963 | 217 | assert(length() <= _allocated_length, "invariant"); |
tonyp@2963 | 218 | assert(_allocated_length > 0, "we should have at least one region committed"); |
ysr@777 | 219 | |
tonyp@2963 | 220 | // around the loop, i will be the next region to be removed |
tonyp@2963 | 221 | size_t i = length() - 1; |
tonyp@2963 | 222 | assert(i > 0, "we should never remove all regions"); |
tonyp@2963 | 223 | // [last_start, end) is the MemRegion that covers the regions we will remove. |
tonyp@2963 | 224 | HeapWord* end = at(i)->end(); |
ysr@777 | 225 | HeapWord* last_start = end; |
tonyp@2963 | 226 | *num_regions_deleted = 0; |
tonyp@2963 | 227 | while (shrink_bytes > 0) { |
tonyp@2963 | 228 | HeapRegion* cur = at(i); |
tonyp@2963 | 229 | // We should leave the humongous regions where they are. |
tonyp@2963 | 230 | if (cur->isHumongous()) break; |
tonyp@2963 | 231 | // We should stop shrinking if we come across a non-empty region. |
ysr@777 | 232 | if (!cur->is_empty()) break; |
tonyp@2963 | 233 | |
tonyp@2963 | 234 | i -= 1; |
tonyp@2963 | 235 | *num_regions_deleted += 1; |
ysr@777 | 236 | shrink_bytes -= cur->capacity(); |
ysr@777 | 237 | last_start = cur->bottom(); |
tonyp@2963 | 238 | decrement_length(&_length); |
tonyp@2963 | 239 | // We will reclaim the HeapRegion. _allocated_length should be |
tonyp@2963 | 240 | // covering this index. So, even though we removed the region from |
tonyp@2963 | 241 | // the active set by decreasing _length, we still have it |
tonyp@2963 | 242 | // available in the future if we need to re-use it. |
tonyp@2963 | 243 | assert(i > 0, "we should never remove all regions"); |
tonyp@2963 | 244 | assert(length() > 0, "we should never remove all regions"); |
ysr@777 | 245 | } |
ysr@777 | 246 | return MemRegion(last_start, end); |
ysr@777 | 247 | } |
ysr@777 | 248 | |
tonyp@2963 | 249 | #ifndef PRODUCT |
tonyp@2963 | 250 | void HeapRegionSeq::verify_optional() { |
tonyp@2963 | 251 | guarantee(_length <= _allocated_length, |
tonyp@2963 | 252 | err_msg("invariant: _length: "SIZE_FORMAT" " |
tonyp@2963 | 253 | "_allocated_length: "SIZE_FORMAT, |
tonyp@2963 | 254 | _length, _allocated_length)); |
tonyp@2963 | 255 | guarantee(_allocated_length <= _max_length, |
tonyp@2963 | 256 | err_msg("invariant: _allocated_length: "SIZE_FORMAT" " |
tonyp@2963 | 257 | "_max_length: "SIZE_FORMAT, |
tonyp@2963 | 258 | _allocated_length, _max_length)); |
tonyp@2963 | 259 | guarantee(_next_search_index <= _length, |
tonyp@2963 | 260 | err_msg("invariant: _next_search_index: "SIZE_FORMAT" " |
tonyp@2963 | 261 | "_length: "SIZE_FORMAT, |
tonyp@2963 | 262 | _next_search_index, _length)); |
tonyp@2963 | 263 | |
tonyp@2963 | 264 | HeapWord* prev_end = _heap_bottom; |
tonyp@2963 | 265 | for (size_t i = 0; i < _allocated_length; i += 1) { |
tonyp@2963 | 266 | HeapRegion* hr = _regions[i]; |
tonyp@2963 | 267 | guarantee(hr != NULL, err_msg("invariant: i: "SIZE_FORMAT, i)); |
tonyp@2963 | 268 | guarantee(hr->bottom() == prev_end, |
tonyp@2963 | 269 | err_msg("invariant i: "SIZE_FORMAT" "HR_FORMAT" " |
tonyp@2963 | 270 | "prev_end: "PTR_FORMAT, |
tonyp@2963 | 271 | i, HR_FORMAT_PARAMS(hr), prev_end)); |
tonyp@2963 | 272 | guarantee(hr->hrs_index() == i, |
tonyp@2963 | 273 | err_msg("invariant: i: "SIZE_FORMAT" hrs_index(): "SIZE_FORMAT, |
tonyp@2963 | 274 | i, hr->hrs_index())); |
tonyp@2963 | 275 | if (i < _length) { |
tonyp@2963 | 276 | // Asserts will fire if i is >= _length |
tonyp@2963 | 277 | HeapWord* addr = hr->bottom(); |
tonyp@2963 | 278 | guarantee(addr_to_region(addr) == hr, "sanity"); |
tonyp@2963 | 279 | guarantee(addr_to_region_unsafe(addr) == hr, "sanity"); |
tonyp@2963 | 280 | } else { |
tonyp@2963 | 281 | guarantee(hr->is_empty(), "sanity"); |
tonyp@2963 | 282 | guarantee(!hr->isHumongous(), "sanity"); |
tonyp@2963 | 283 | // using assert instead of guarantee here since containing_set() |
tonyp@2963 | 284 | // is only available in non-product builds. |
tonyp@2963 | 285 | assert(hr->containing_set() == NULL, "sanity"); |
tonyp@2963 | 286 | } |
tonyp@2963 | 287 | if (hr->startsHumongous()) { |
tonyp@2963 | 288 | prev_end = hr->orig_end(); |
tonyp@2963 | 289 | } else { |
tonyp@2963 | 290 | prev_end = hr->end(); |
tonyp@2963 | 291 | } |
ysr@777 | 292 | } |
tonyp@2963 | 293 | for (size_t i = _allocated_length; i < _max_length; i += 1) { |
tonyp@2963 | 294 | guarantee(_regions[i] == NULL, err_msg("invariant i: "SIZE_FORMAT, i)); |
tonyp@2963 | 295 | } |
ysr@777 | 296 | } |
tonyp@2963 | 297 | #endif // PRODUCT |