1
1
/*
2
- * Copyright (c) 2014, 2023 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2014, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
26
26
#define SHARE_MEMORY_GUARDEDMEMORY_HPP
27
27
28
28
#include " memory/allocation.hpp"
29
+ #include " runtime/safefetch.hpp"
29
30
#include " utilities/globalDefinitions.hpp"
30
31
31
32
/* *
43
44
* |base_addr | 0xABABABABABABABAB | Head guard |
44
45
* |+16 | <size_t:user_size> | User data size |
45
46
* |+sizeof(uintptr_t) | <tag> | Tag word |
47
+ * |+sizeof(uintptr_t) | <tag2> | Tag word |
46
48
* |+sizeof(void*) | 0xF1 <user_data> ( | User data |
47
49
* |+user_size | 0xABABABABABABABAB | Tail guard |
48
50
* -------------------------------------------------------------
49
51
*
50
52
* Where:
51
53
* - guard padding uses "badResourceValue" (0xAB)
52
- * - tag word is general purpose
54
+ * - tag word and tag2 word are general purpose
53
55
* - user data
54
56
* -- initially padded with "uninitBlockPad" (0xF1),
55
57
* -- to "freeBlockPad" (0xBA), when freed
@@ -114,7 +116,11 @@ class GuardedMemory : StackObj { // Wrapper on stack
114
116
u_char* c = (u_char*) _guard;
115
117
u_char* end = c + GUARD_SIZE;
116
118
while (c < end) {
117
- if (*c != badResourceValue) {
119
+ // We may not be able to dereference directly so use
120
+ // SafeFetch. It doesn't matter if the value read happens
121
+ // to be 0xFF as that is not what we expect anyway.
122
+ u_char val = (u_char) SafeFetch32 ((int *)c, 0xFF );
123
+ if (val != badResourceValue) {
118
124
return false ;
119
125
}
120
126
c++;
@@ -137,13 +143,18 @@ class GuardedMemory : StackObj { // Wrapper on stack
137
143
size_t _user_size;
138
144
};
139
145
void * _tag;
146
+ void * _tag2;
140
147
public:
141
148
void set_user_size (const size_t usz) { _user_size = usz; }
142
149
size_t get_user_size () const { return _user_size; }
143
150
144
151
void set_tag (const void * tag) { _tag = (void *) tag; }
145
152
void * get_tag () const { return _tag; }
146
153
154
+ void set_tag2 (const void * tag2) { _tag2 = (void *) tag2; }
155
+ void * get_tag2 () const { return _tag2; }
156
+
157
+
147
158
}; // GuardedMemory::GuardHeader
148
159
149
160
// Guarded Memory...
@@ -162,9 +173,11 @@ class GuardedMemory : StackObj { // Wrapper on stack
162
173
* @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
163
174
* @param user_size the size of the user data to be wrapped.
164
175
* @param tag optional general purpose tag.
176
+ * @param tag2 optional second general purpose tag.
165
177
*/
166
- GuardedMemory (void * base_ptr, const size_t user_size, const void * tag = nullptr ) {
167
- wrap_with_guards (base_ptr, user_size, tag);
178
+ GuardedMemory (void * base_ptr, const size_t user_size,
179
+ const void * tag = nullptr , const void * tag2 = nullptr ) {
180
+ wrap_with_guards (base_ptr, user_size, tag, tag2);
168
181
}
169
182
170
183
/* *
@@ -189,16 +202,19 @@ class GuardedMemory : StackObj { // Wrapper on stack
189
202
* @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
190
203
* @param user_size the size of the user data to be wrapped.
191
204
* @param tag optional general purpose tag.
205
+ * @param tag2 optional second general purpose tag.
192
206
*
193
207
* @return user data pointer (inner pointer to supplied "base_ptr").
194
208
*/
195
- void * wrap_with_guards (void * base_ptr, size_t user_size, const void * tag = nullptr ) {
209
+ void * wrap_with_guards (void * base_ptr, size_t user_size,
210
+ const void * tag = nullptr , const void * tag2 = nullptr ) {
196
211
assert (base_ptr != nullptr , " Attempt to wrap null with memory guard" );
197
212
_base_addr = (u_char*)base_ptr;
198
213
get_head_guard ()->build ();
199
214
get_head_guard ()->set_user_size (user_size);
200
215
get_tail_guard ()->build ();
201
216
set_tag (tag);
217
+ set_tag2 (tag2);
202
218
set_user_bytes (uninitBlockPad);
203
219
assert (verify_guards (), " Expected valid memory guards" );
204
220
return get_user_ptr ();
@@ -230,6 +246,20 @@ class GuardedMemory : StackObj { // Wrapper on stack
230
246
*/
231
247
void * get_tag () const { return get_head_guard ()->get_tag (); }
232
248
249
+ /* *
250
+ * Set the second general purpose tag.
251
+ *
252
+ * @param tag general purpose tag.
253
+ */
254
+ void set_tag2 (const void * tag) { get_head_guard ()->set_tag2 (tag); }
255
+
256
+ /* *
257
+ * Return the second general purpose tag.
258
+ *
259
+ * @return the second general purpose tag, defaults to null.
260
+ */
261
+ void * get_tag2 () const { return get_head_guard ()->get_tag2 (); }
262
+
233
263
/* *
234
264
* Return the size of the user data.
235
265
*
@@ -302,10 +332,12 @@ class GuardedMemory : StackObj { // Wrapper on stack
302
332
* @param ptr the memory to be copied
303
333
* @param len the length of the copy
304
334
* @param tag optional general purpose tag (see GuardedMemory::get_tag())
335
+ * @param tag2 optional general purpose tag (see GuardedMemory::get_tag2())
305
336
*
306
337
* @return guarded wrapped memory pointer to the user area, or null if OOM.
307
338
*/
308
- static void * wrap_copy (const void * p, const size_t len, const void * tag = nullptr );
339
+ static void * wrap_copy (const void * p, const size_t len,
340
+ const void * tag = nullptr , const void * tag2 = nullptr );
309
341
310
342
/* *
311
343
* Free wrapped copy.
0 commit comments