1 /*
2 * Copyright (c) 2018, 2025, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHASSERTS_HPP
27 #define SHARE_GC_SHENANDOAH_SHENANDOAHASSERTS_HPP
28
29 #include "memory/iterator.hpp"
30 #include "oops/compressedKlass.hpp"
31 #include "runtime/mutex.hpp"
32 #include "utilities/formatBuffer.hpp"
33
34 typedef FormatBuffer<8192> ShenandoahMessageBuffer;
35
36 class ShenandoahAsserts {
37 public:
38 enum SafeLevel {
39 _safe_unknown,
40 _safe_oop,
41 _safe_oop_fwd,
42 _safe_all
43 };
44
45 static void print_obj(ShenandoahMessageBuffer &msg, oop obj);
46
47 static void print_non_obj(ShenandoahMessageBuffer &msg, void *loc);
48
49 static void print_obj_safe(ShenandoahMessageBuffer &msg, void *loc);
50
51 static void print_failure(SafeLevel level, oop obj, void *interior_loc, oop loc,
52 const char *phase, const char *label,
53 const char *file, int line);
54
55 static void print_rp_failure(const char *label, BoolObjectClosure* actual,
56 const char *file, int line);
57
58 static void assert_in_heap_bounds(void* interior_loc, oop obj, const char* file, int line);
59 static void assert_in_heap_bounds_or_null(void* interior_loc, oop obj, const char* file, int line);
60 static void assert_in_correct_region(void* interior_loc, oop obj, const char* file, int line);
61
62 static void assert_correct(void* interior_loc, oop obj, const char* file, int line);
63 static void assert_forwarded(void* interior_loc, oop obj, const char* file, int line);
64 static void assert_not_forwarded(void* interior_loc, oop obj, const char* file, int line);
65 static void assert_marked(void* interior_loc, oop obj, const char* file, int line);
66 static void assert_marked_weak(void* interior_loc, oop obj, const char* file, int line);
67 static void assert_marked_strong(void* interior_loc, oop obj, const char* file, int line);
68 static void assert_in_cset(void* interior_loc, oop obj, const char* file, int line);
69 static void assert_not_in_cset(void* interior_loc, oop obj, const char* file, int line);
70 static void assert_not_in_cset_loc(void* interior_loc, const char* file, int line);
71
72 static void assert_locked_or_shenandoah_safepoint(Mutex* lock, const char* file, int line);
73
74 static void assert_heaplocked(const char* file, int line);
75 static void assert_not_heaplocked(const char* file, int line);
76 static void assert_heaplocked_or_safepoint(const char* file, int line);
77 static void assert_control_or_vm_thread_at_safepoint(bool at_safepoint, const char* file, int line);
78 static void assert_generational(const char* file, int line);
79 static void assert_generations_reconciled(const char* file, int line);
80
81 // Given a possibly invalid oop, extract narrowKlass (if UCCP) and Klass*
82 // from it safely.
83 // Note: For -UCCP, returned nk is always 0.
84 static bool extract_klass_safely(oop obj, narrowKlass& nk, const Klass*& k);
85
86 #ifdef ASSERT
87 #define shenandoah_assert_in_heap_bounds(interior_loc, obj) \
88 ShenandoahAsserts::assert_in_heap_bounds(interior_loc, obj, __FILE__, __LINE__)
89 #define shenandoah_assert_in_heap_bounds_or_null(interior_loc, obj) \
90 ShenandoahAsserts::assert_in_heap_bounds_or_null(interior_loc, obj, __FILE__, __LINE__)
91 #define shenandoah_assert_in_correct_region(interior_loc, obj) \
92 ShenandoahAsserts::assert_in_correct_region(interior_loc, obj, __FILE__, __LINE__)
93
94 #define shenandoah_assert_correct_if(interior_loc, obj, condition) \
95 if (condition) ShenandoahAsserts::assert_correct(interior_loc, obj, __FILE__, __LINE__)
96 #define shenandoah_assert_correct_except(interior_loc, obj, exception) \
97 if (!(exception)) ShenandoahAsserts::assert_correct(interior_loc, obj, __FILE__, __LINE__)
98 #define shenandoah_assert_correct(interior_loc, obj) \
99 ShenandoahAsserts::assert_correct(interior_loc, obj, __FILE__, __LINE__)
100
101 #define shenandoah_assert_forwarded_if(interior_loc, obj, condition) \
102 if (condition) ShenandoahAsserts::assert_forwarded(interior_loc, obj, __FILE__, __LINE__)
103 #define shenandoah_assert_forwarded_except(interior_loc, obj, exception) \
104 if (!(exception)) ShenandoahAsserts::assert_forwarded(interior_loc, obj, __FILE__, __LINE__)
105 #define shenandoah_assert_forwarded(interior_loc, obj) \
106 ShenandoahAsserts::assert_forwarded(interior_loc, obj, __FILE__, __LINE__)
107
108 #define shenandoah_assert_not_forwarded_if(interior_loc, obj, condition) \
109 if (condition) ShenandoahAsserts::assert_not_forwarded(interior_loc, obj, __FILE__, __LINE__)
110 #define shenandoah_assert_not_forwarded_except(interior_loc, obj, exception) \
111 if (!(exception)) ShenandoahAsserts::assert_not_forwarded(interior_loc, obj, __FILE__, __LINE__)
112 #define shenandoah_assert_not_forwarded(interior_loc, obj) \
113 ShenandoahAsserts::assert_not_forwarded(interior_loc, obj, __FILE__, __LINE__)
114
115 #define shenandoah_assert_marked_if(interior_loc, obj, condition) \
116 if (condition) ShenandoahAsserts::assert_marked(interior_loc, obj, __FILE__, __LINE__)
117 #define shenandoah_assert_marked_except(interior_loc, obj, exception) \
118 if (!(exception)) ShenandoahAsserts::assert_marked(interior_loc, obj, __FILE__, __LINE__)
119 #define shenandoah_assert_marked(interior_loc, obj) \
120 ShenandoahAsserts::assert_marked(interior_loc, obj, __FILE__, __LINE__)
121
122 #define shenandoah_assert_marked_weak_if(interior_loc, obj, condition) \
123 if (condition) ShenandoahAsserts::assert_marked_weak(interior_loc, obj, __FILE__, __LINE__)
124 #define shenandoah_assert_marked_weak_except(interior_loc, obj, exception) \
125 if (!(exception)) ShenandoahAsserts::assert_marked_weak(interior_loc, obj, __FILE__, __LINE__)
126 #define shenandoah_assert_marked_weak(interior_loc, obj) \
127 ShenandoahAsserts::assert_marked_weak(interior_loc, obj, __FILE__, __LINE__)
128
129 #define shenandoah_assert_marked_strong_if(interior_loc, obj, condition) \
130 if (condition) ShenandoahAsserts::assert_marked_strong(interior_loc, obj, __FILE__, __LINE__)
131 #define shenandoah_assert_marked_strong_except(interior_loc, obj, exception) \
132 if (!(exception)) ShenandoahAsserts::assert_marked_strong(interior_loc, obj, __FILE__, __LINE__)
133 #define shenandoah_assert_marked_strong(interior_loc, obj) \
134 ShenandoahAsserts::assert_marked_strong(interior_loc, obj, __FILE__, __LINE__)
135
136 #define shenandoah_assert_in_cset_if(interior_loc, obj, condition) \
137 if (condition) ShenandoahAsserts::assert_in_cset(interior_loc, obj, __FILE__, __LINE__)
138 #define shenandoah_assert_in_cset_except(interior_loc, obj, exception) \
139 if (!(exception)) ShenandoahAsserts::assert_in_cset(interior_loc, obj, __FILE__, __LINE__)
140 #define shenandoah_assert_in_cset(interior_loc, obj) \
141 ShenandoahAsserts::assert_in_cset(interior_loc, obj, __FILE__, __LINE__)
142
143 #define shenandoah_assert_not_in_cset_if(interior_loc, obj, condition) \
144 if (condition) ShenandoahAsserts::assert_not_in_cset(interior_loc, obj, __FILE__, __LINE__)
145 #define shenandoah_assert_not_in_cset_except(interior_loc, obj, exception) \
146 if (!(exception)) ShenandoahAsserts::assert_not_in_cset(interior_loc, obj, __FILE__, __LINE__)
147 #define shenandoah_assert_not_in_cset(interior_loc, obj) \
148 ShenandoahAsserts::assert_not_in_cset(interior_loc, obj, __FILE__, __LINE__)
149
150 #define shenandoah_assert_not_in_cset_loc_if(interior_loc, condition) \
151 if (condition) ShenandoahAsserts::assert_not_in_cset_loc(interior_loc, __FILE__, __LINE__)
152 #define shenandoah_assert_not_in_cset_loc_except(interior_loc, exception) \
153 if (!(exception)) ShenandoahAsserts::assert_not_in_cset_loc(interior_loc, __FILE__, __LINE__)
154 #define shenandoah_assert_not_in_cset_loc(interior_loc) \
155 ShenandoahAsserts::assert_not_in_cset_loc(interior_loc, __FILE__, __LINE__)
156
157 #define shenandoah_assert_rp_isalive_installed() \
158 ShenandoahAsserts::assert_rp_isalive_installed(__FILE__, __LINE__)
159 #define shenandoah_assert_rp_isalive_not_installed() \
160 ShenandoahAsserts::assert_rp_isalive_not_installed(__FILE__, __LINE__)
161
162 #define shenandoah_assert_safepoint() \
163 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Should be at Shenandoah Safepoints")
164
165 #define shenandoah_assert_locked_or_safepoint(lock) \
166 ShenandoahAsserts::assert_locked_or_shenandoah_safepoint(lock, __FILE__, __LINE__)
167
168 #define shenandoah_assert_heaplocked() \
169 ShenandoahAsserts::assert_heaplocked(__FILE__, __LINE__)
170
171 #define shenandoah_assert_not_heaplocked() \
172 ShenandoahAsserts::assert_not_heaplocked(__FILE__, __LINE__)
173
174 #define shenandoah_assert_heaplocked_or_safepoint() \
175 ShenandoahAsserts::assert_heaplocked_or_safepoint(__FILE__, __LINE__)
176
177 #define shenandoah_assert_control_or_vm_thread() \
178 ShenandoahAsserts::assert_control_or_vm_thread(false /* at_safepoint */, __FILE__, __LINE__)
179
180 // A stronger version of the above that checks that we are at a safepoint if the vm thread
181 #define shenandoah_assert_control_or_vm_thread_at_safepoint() \
182 ShenandoahAsserts::assert_control_or_vm_thread_at_safepoint(true /* at_safepoint */, __FILE__, __LINE__)
183
184 #define shenandoah_assert_generational() \
185 ShenandoahAsserts::assert_generational(__FILE__, __LINE__)
186
187 // Some limited sanity checking of the _gc_generation and _active_generation fields of ShenandoahHeap
188 #define shenandoah_assert_generations_reconciled() \
189 ShenandoahAsserts::assert_generations_reconciled(__FILE__, __LINE__)
190
191 #else
192 #define shenandoah_assert_in_heap_bounds(interior_loc, obj)
193 #define shenandoah_assert_in_heap_bounds_or_null(interior_loc, obj)
194 #define shenandoah_assert_in_correct_region(interior_loc, obj)
195
196 #define shenandoah_assert_correct_if(interior_loc, obj, condition)
197 #define shenandoah_assert_correct_except(interior_loc, obj, exception)
198 #define shenandoah_assert_correct(interior_loc, obj)
199
200 #define shenandoah_assert_forwarded_if(interior_loc, obj, condition)
201 #define shenandoah_assert_forwarded_except(interior_loc, obj, exception)
202 #define shenandoah_assert_forwarded(interior_loc, obj)
203
204 #define shenandoah_assert_not_forwarded_if(interior_loc, obj, condition)
205 #define shenandoah_assert_not_forwarded_except(interior_loc, obj, exception)
206 #define shenandoah_assert_not_forwarded(interior_loc, obj)
207
208 #define shenandoah_assert_marked_if(interior_loc, obj, condition)
209 #define shenandoah_assert_marked_except(interior_loc, obj, exception)
210 #define shenandoah_assert_marked(interior_loc, obj)
211
212 #define shenandoah_assert_marked_weak_if(interior_loc, obj, condition)
213 #define shenandoah_assert_marked_weak_except(interior_loc, obj, exception)
214 #define shenandoah_assert_marked_weak(interior_loc, obj)
215
216 #define shenandoah_assert_marked_strong_if(interior_loc, obj, condition)
217 #define shenandoah_assert_marked_strong_except(interior_loc, obj, exception)
218 #define shenandoah_assert_marked_strong(interior_loc, obj)
219
220 #define shenandoah_assert_in_cset_if(interior_loc, obj, condition)
221 #define shenandoah_assert_in_cset_except(interior_loc, obj, exception)
222 #define shenandoah_assert_in_cset(interior_loc, obj)
223
224 #define shenandoah_assert_not_in_cset_if(interior_loc, obj, condition)
225 #define shenandoah_assert_not_in_cset_except(interior_loc, obj, exception)
226 #define shenandoah_assert_not_in_cset(interior_loc, obj)
227
228 #define shenandoah_assert_not_in_cset_loc_if(interior_loc, condition)
229 #define shenandoah_assert_not_in_cset_loc_except(interior_loc, exception)
230 #define shenandoah_assert_not_in_cset_loc(interior_loc)
231
232 #define shenandoah_assert_rp_isalive_installed()
233 #define shenandoah_assert_rp_isalive_not_installed()
234
235 #define shenandoah_assert_safepoint()
236 #define shenandoah_assert_locked_or_safepoint(lock)
237
238 #define shenandoah_assert_heaplocked()
239 #define shenandoah_assert_not_heaplocked()
240 #define shenandoah_assert_heaplocked_or_safepoint()
241 #define shenandoah_assert_control_or_vm_thread()
242 #define shenandoah_assert_control_or_vm_thread_at_safepoint()
243 #define shenandoah_assert_generational()
244 #define shenandoah_assert_generations_reconciled()
245
246 #endif
247
248 #define shenandoah_not_implemented \
249 { fatal("Deliberately not implemented."); }
250 #define shenandoah_not_implemented_return(v) \
251 { fatal("Deliberately not implemented."); return v; }
252
253 };
254
255 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHASSERTS_HPP