Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/guardedMemory.cpp
32285 views
1
/*
2
* Copyright (c) 2014, 2018, 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
#include "precompiled.hpp"
25
#include "memory/allocation.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "memory/guardedMemory.hpp"
28
#include "runtime/os.hpp"
29
30
void* GuardedMemory::wrap_copy(const void* ptr, const size_t len, const void* tag) {
31
size_t total_sz = GuardedMemory::get_total_size(len);
32
void* outerp = os::malloc(total_sz, mtInternal);
33
if (outerp != NULL) {
34
GuardedMemory guarded(outerp, len, tag);
35
void* innerp = guarded.get_user_ptr();
36
memcpy(innerp, ptr, len);
37
return innerp;
38
}
39
return NULL; // OOM
40
}
41
42
bool GuardedMemory::free_copy(void* p) {
43
if (p == NULL) {
44
return true;
45
}
46
GuardedMemory guarded((u_char*)p);
47
bool verify_ok = guarded.verify_guards();
48
49
/* always attempt to free, pass problem on to any nested memchecker */
50
os::free(guarded.release_for_freeing());
51
52
return verify_ok;
53
}
54
55
void GuardedMemory::print_on(outputStream* st) const {
56
if (_base_addr == NULL) {
57
st->print_cr("GuardedMemory(" PTR_FORMAT ") not associated to any memory", p2i(this));
58
return;
59
}
60
st->print_cr("GuardedMemory(" PTR_FORMAT ") base_addr=" PTR_FORMAT
61
" tag=" PTR_FORMAT " user_size=" SIZE_FORMAT " user_data=" PTR_FORMAT,
62
p2i(this), p2i(_base_addr), p2i(get_tag()), get_user_size(), p2i(get_user_ptr()));
63
64
Guard* guard = get_head_guard();
65
st->print_cr(" Header guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN"));
66
guard = get_tail_guard();
67
st->print_cr(" Trailer guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN"));
68
69
u_char udata = *get_user_ptr();
70
switch (udata) {
71
case uninitBlockPad:
72
st->print_cr(" User data appears unused");
73
break;
74
case freeBlockPad:
75
st->print_cr(" User data appears to have been freed");
76
break;
77
default:
78
st->print_cr(" User data appears to be in use");
79
break;
80
}
81
}
82
83
// test code...
84
85
#ifndef PRODUCT
86
87
#define GEN_PURPOSE_TAG ((void *) ((uintptr_t)0xf000f000))
88
89
static void guarded_memory_test_check(void* p, size_t sz, void* tag) {
90
assert(p != NULL, "NULL pointer given to check");
91
u_char* c = (u_char*) p;
92
GuardedMemory guarded(c);
93
assert(guarded.get_tag() == tag, "Tag is not the same as supplied");
94
assert(guarded.get_user_ptr() == c, "User pointer is not the same as supplied");
95
assert(guarded.get_user_size() == sz, "User size is not the same as supplied");
96
assert(guarded.verify_guards(), "Guard broken");
97
}
98
99
void GuardedMemory::test_guarded_memory() {
100
// Test the basic characteristics...
101
size_t total_sz = GuardedMemory::get_total_size(1);
102
assert(total_sz > 1 && total_sz >= (sizeof(GuardHeader) + 1 + sizeof(Guard)), "Unexpected size");
103
u_char* basep = (u_char*) os::malloc(total_sz, mtInternal);
104
105
GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);
106
107
assert(*basep == badResourceValue, "Expected guard in the form of badResourceValue");
108
u_char* userp = guarded.get_user_ptr();
109
assert(*userp == uninitBlockPad, "Expected uninitialized data in the form of uninitBlockPad");
110
guarded_memory_test_check(userp, 1, GEN_PURPOSE_TAG);
111
112
void* freep = guarded.release_for_freeing();
113
assert((u_char*)freep == basep, "Expected the same pointer guard was ");
114
assert(*userp == freeBlockPad, "Expected user data to be free block padded");
115
assert(!guarded.verify_guards(), "Expected failed");
116
os::free(freep);
117
118
// Test a number of odd sizes...
119
size_t sz = 0;
120
do {
121
void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
122
void* up = guarded.wrap_with_guards(p, sz, (void*)1);
123
memset(up, 0, sz);
124
guarded_memory_test_check(up, sz, (void*)1);
125
os::free(guarded.release_for_freeing());
126
sz = (sz << 4) + 1;
127
} while (sz < (256 * 1024));
128
129
// Test buffer overrun into head...
130
basep = (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);
131
guarded.wrap_with_guards(basep, 1);
132
*basep = 0;
133
assert(!guarded.verify_guards(), "Expected failure");
134
os::free(basep);
135
136
// Test buffer overrun into tail with a number of odd sizes...
137
sz = 1;
138
do {
139
void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);
140
void* up = guarded.wrap_with_guards(p, sz, (void*)1);
141
memset(up, 0, sz + 1); // Buffer-overwrite (within guard)
142
assert(!guarded.verify_guards(), "Guard was not broken as expected");
143
os::free(guarded.release_for_freeing());
144
sz = (sz << 4) + 1;
145
} while (sz < (256 * 1024));
146
147
// Test wrap_copy/wrap_free...
148
assert(GuardedMemory::free_copy(NULL), "Expected free NULL to be OK");
149
150
const char* str = "Check my bounds out";
151
size_t str_sz = strlen(str) + 1;
152
char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz);
153
guarded_memory_test_check(str_copy, str_sz, NULL);
154
assert(strcmp(str, str_copy) == 0, "Not identical copy");
155
assert(GuardedMemory::free_copy(str_copy), "Free copy failed to verify");
156
157
void* no_data = NULL;
158
void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0);
159
assert(GuardedMemory::free_copy(no_data_copy), "Expected valid guards even for no data copy");
160
}
161
162
#endif // !PRODUCT
163
164
165