1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2020 SAP SE. 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 #include "precompiled.hpp"
27 #include "metaspaceGtestCommon.hpp"
28 #include "metaspaceGtestRangeHelpers.hpp"
29 #include "runtime/os.hpp"
30 
zap_range(MetaWord * p,size_t word_size)31 void zap_range(MetaWord* p, size_t word_size) {
32   for (MetaWord* pzap = p; pzap < p + word_size; pzap += os::vm_page_size() / BytesPerWord) {
33     *pzap = (MetaWord)NOT_LP64(0xFEFEFEFE) LP64_ONLY(0xFEFEFEFEEFEFEFEFULL);
34   }
35 }
36 
37 // Writes a unique pattern to p
mark_address(MetaWord * p,uintx pattern)38 void mark_address(MetaWord* p, uintx pattern) {
39   MetaWord x = (MetaWord)((uintx) p ^ pattern);
40   *p = x;
41 }
42 
43 // checks pattern at address
check_marked_address(const MetaWord * p,uintx pattern)44 void check_marked_address(const MetaWord* p, uintx pattern) {
45   MetaWord x = (MetaWord)((uintx) p ^ pattern);
46   EXPECT_EQ(*p, x);
47 }
48 
49 // "fill_range_with_pattern" fills a range of heap words with pointers to itself.
50 //
51 // The idea is to fill a memory range with a pattern which is both marked clearly to the caller
52 // and cannot be moved without becoming invalid.
53 //
54 // The filled range can be checked with check_range_for_pattern. One also can only check
55 // a sub range of the original range.
fill_range_with_pattern(MetaWord * p,size_t word_size,uintx pattern)56 void fill_range_with_pattern(MetaWord* p, size_t word_size, uintx pattern) {
57   assert(word_size > 0 && p != NULL, "sanity");
58   for (MetaWord* p2 = p; p2 < p + word_size; p2++) {
59     mark_address(p2, pattern);
60   }
61 }
62 
check_range_for_pattern(const MetaWord * p,size_t word_size,uintx pattern)63 void check_range_for_pattern(const MetaWord* p, size_t word_size, uintx pattern) {
64   assert(p != NULL, "sanity");
65   const MetaWord* p2 = p;
66   while (p2 < p + word_size) {
67     check_marked_address(p2, pattern);
68     p2++;
69   }
70 }
71 
72 // Similar to fill_range_with_pattern, but only marks start and end. This is optimized for cases
73 // where fill_range_with_pattern just is too slow.
74 // Use check_marked_range to check the range. In contrast to check_range_for_pattern, only the original
75 // range can be checked.
mark_range(MetaWord * p,size_t word_size,uintx pattern)76 void mark_range(MetaWord* p, size_t word_size, uintx pattern) {
77   assert(word_size > 0 && p != NULL, "sanity");
78   mark_address(p, pattern);
79   mark_address(p + word_size - 1, pattern);
80 }
81 
check_marked_range(const MetaWord * p,size_t word_size,uintx pattern)82 void check_marked_range(const MetaWord* p, size_t word_size, uintx pattern) {
83   assert(word_size > 0 && p != NULL, "sanity");
84   check_marked_address(p, pattern);
85   check_marked_address(p + word_size - 1, pattern);
86 }
87 
mark_range(MetaWord * p,size_t word_size)88 void mark_range(MetaWord* p, size_t word_size) {
89   assert(word_size > 0 && p != NULL, "sanity");
90   uintx pattern = (uintx)p2i(p);
91   mark_range(p, word_size, pattern);
92 }
93 
check_marked_range(const MetaWord * p,size_t word_size)94 void check_marked_range(const MetaWord* p, size_t word_size) {
95   uintx pattern = (uintx)p2i(p);
96   check_marked_range(p, word_size, pattern);
97 }
98 
99