1 /*
   2  * Copyright (c) 2002, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/space.inline.hpp"
  27 #include "gc/shared/spaceDecorator.hpp"
  28 #include "logging/log.hpp"
  29 #include "utilities/copy.hpp"
  30 
  31 // Catch-all file for utility classes
  32 
  33 #ifndef PRODUCT
  34 
  35 // Returns true is the location q matches the mangling
  36 // pattern.
  37 bool SpaceMangler::is_mangled(HeapWord* q) {
  38   // This test loses precision but is good enough
  39   return badHeapWord == (max_juint & (uintptr_t) q->value());
  40 }
  41 
  42 
  43 void SpaceMangler::set_top_for_allocations(HeapWord* v)  {
  44   if (v < end()) {
  45     assert(!CheckZapUnusedHeapArea || is_mangled(v),
  46       "The high water mark is not mangled");
  47   }
  48   _top_for_allocations = v;
  49 }
  50 
  51 // Mangle only the unused space that has not previously
  52 // been mangled and that has not been allocated since being
  53 // mangled.
  54 void SpaceMangler::mangle_unused_area() {
  55   assert(ZapUnusedHeapArea, "Mangling should not be in use");
  56   // Mangle between top and the high water mark.  Safeguard
  57   // against the space changing since top_for_allocations was
  58   // set.
  59   HeapWord* mangled_end = MIN2(top_for_allocations(), end());
  60   if (top() < mangled_end) {
  61     MemRegion mangle_mr(top(), mangled_end);
  62     SpaceMangler::mangle_region(mangle_mr);
  63     // Light weight check of mangling.
  64     check_mangled_unused_area(end());
  65   }
  66   // Complete check of unused area which is functional when
  67   // DEBUG_MANGLING is defined.
  68   check_mangled_unused_area_complete();
  69 }
  70 
  71 // A complete mangle is expected in the
  72 // exceptional case where top_for_allocations is not
  73 // properly tracking the high water mark for mangling.
  74 // This can be the case when to-space is being used for
  75 // scratch space during a mark-sweep-compact.  See
  76 // contribute_scratch() and PSMarkSweep::allocate_stacks().
  77 void SpaceMangler::mangle_unused_area_complete() {
  78   assert(ZapUnusedHeapArea, "Mangling should not be in use");
  79   MemRegion mangle_mr(top(), end());
  80   SpaceMangler::mangle_region(mangle_mr);
  81 }
  82 
  83 // Simply mangle the MemRegion mr.
  84 void SpaceMangler::mangle_region(MemRegion mr) {
  85   assert(ZapUnusedHeapArea, "Mangling should not be in use");
  86 #ifdef ASSERT
  87   log_develop(gc, init)("Mangling [" PTR_FORMAT " to " PTR_FORMAT ")", p2i(mr.start()), p2i(mr.end()));
  88   Copy::fill_to_words(mr.start(), mr.word_size(), badHeapWord);
  89   log_develop(gc, init)("Mangling done.");
  90 #endif
  91 }
  92 
  93 // Check that top, top_for_allocations and the last
  94 // word of the space are mangled.  In a tight memory
  95 // situation even this light weight mangling could
  96 // cause paging by touching the end of the space.
  97 void  SpaceMangler::check_mangled_unused_area(HeapWord* limit) {
  98   if (CheckZapUnusedHeapArea) {
  99     // This method can be called while the spaces are
 100     // being reshaped so skip the test if the end of the
 101     // space is beyond the specified limit;
 102     if (end() > limit) return;
 103 
 104     assert(top() == end() ||
 105            (is_mangled(top())), "Top not mangled");
 106     assert((top_for_allocations() < top()) ||
 107            (top_for_allocations() >= end()) ||
 108            (is_mangled(top_for_allocations())),
 109            "Older unused not mangled");
 110     assert(top() == end() ||
 111            (is_mangled(end() - 1)), "End not properly mangled");
 112     // Only does checking when DEBUG_MANGLING is defined.
 113     check_mangled_unused_area_complete();
 114   }
 115 }
 116 
 117 #undef DEBUG_MANGLING
 118 // This should only be used while debugging the mangling
 119 // because of the high cost of checking the completeness.
 120 void  SpaceMangler::check_mangled_unused_area_complete() {
 121   if (CheckZapUnusedHeapArea) {
 122     assert(ZapUnusedHeapArea, "Not mangling unused area");
 123 #ifdef DEBUG_MANGLING
 124     HeapWord* q = top();
 125     HeapWord* limit = end();
 126 
 127     bool passed = true;
 128     while (q < limit) {
 129       if (!is_mangled(q)) {
 130         passed = false;
 131         break;
 132       }
 133       q++;
 134     }
 135     assert(passed, "Mangling is not complete");
 136 #endif
 137   }
 138 }
 139 #undef DEBUG_MANGLING
 140 #endif // not PRODUCT