1 | /* |
2 | * Copyright (c) 2002, 2019, 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 & reinterpret_cast<uintptr_t>(*q)); |
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 | Copy::fill_to_words(mr.start(), mr.word_size(), badHeapWord); |
88 | #endif |
89 | } |
90 | |
91 | // Check that top, top_for_allocations and the last |
92 | // word of the space are mangled. In a tight memory |
93 | // situation even this light weight mangling could |
94 | // cause paging by touching the end of the space. |
95 | void SpaceMangler::check_mangled_unused_area(HeapWord* limit) { |
96 | if (CheckZapUnusedHeapArea) { |
97 | // This method can be called while the spaces are |
98 | // being reshaped so skip the test if the end of the |
99 | // space is beyond the specified limit; |
100 | if (end() > limit) return; |
101 | |
102 | assert(top() == end() || |
103 | (is_mangled(top())), "Top not mangled" ); |
104 | assert((top_for_allocations() < top()) || |
105 | (top_for_allocations() >= end()) || |
106 | (is_mangled(top_for_allocations())), |
107 | "Older unused not mangled" ); |
108 | assert(top() == end() || |
109 | (is_mangled(end() - 1)), "End not properly mangled" ); |
110 | // Only does checking when DEBUG_MANGLING is defined. |
111 | check_mangled_unused_area_complete(); |
112 | } |
113 | } |
114 | |
115 | #undef DEBUG_MANGLING |
116 | // This should only be used while debugging the mangling |
117 | // because of the high cost of checking the completeness. |
118 | void SpaceMangler::check_mangled_unused_area_complete() { |
119 | if (CheckZapUnusedHeapArea) { |
120 | assert(ZapUnusedHeapArea, "Not mangling unused area" ); |
121 | #ifdef DEBUG_MANGLING |
122 | HeapWord* q = top(); |
123 | HeapWord* limit = end(); |
124 | |
125 | bool passed = true; |
126 | while (q < limit) { |
127 | if (!is_mangled(q)) { |
128 | passed = false; |
129 | break; |
130 | } |
131 | q++; |
132 | } |
133 | assert(passed, "Mangling is not complete" ); |
134 | #endif |
135 | } |
136 | } |
137 | #undef DEBUG_MANGLING |
138 | #endif // not PRODUCT |
139 | |