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/os.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
@@ -111,6 +113,10 @@ class GuardedMemory : StackObj { // Wrapper on stack
111
113
}
112
114
113
115
bool verify () const {
116
+ // We may not be able to dereference directly.
117
+ if (!os::is_readable_range ((const void *) _guard, (const void *) (_guard + GUARD_SIZE))) {
118
+ return false ;
119
+ }
114
120
u_char* c = (u_char*) _guard;
115
121
u_char* end = c + GUARD_SIZE;
116
122
while (c < end) {
@@ -137,13 +143,16 @@ 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; }
147
156
}; // GuardedMemory::GuardHeader
148
157
149
158
// Guarded Memory...
@@ -162,9 +171,11 @@ class GuardedMemory : StackObj { // Wrapper on stack
162
171
* @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
163
172
* @param user_size the size of the user data to be wrapped.
164
173
* @param tag optional general purpose tag.
174
+ * @param tag2 optional second general purpose tag.
165
175
*/
166
- GuardedMemory (void * base_ptr, const size_t user_size, const void * tag = nullptr ) {
167
- wrap_with_guards (base_ptr, user_size, tag);
176
+ GuardedMemory (void * base_ptr, const size_t user_size,
177
+ const void * tag = nullptr , const void * tag2 = nullptr ) {
178
+ wrap_with_guards (base_ptr, user_size, tag, tag2);
168
179
}
169
180
170
181
/* *
@@ -189,16 +200,19 @@ class GuardedMemory : StackObj { // Wrapper on stack
189
200
* @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
190
201
* @param user_size the size of the user data to be wrapped.
191
202
* @param tag optional general purpose tag.
203
+ * @param tag2 optional second general purpose tag.
192
204
*
193
205
* @return user data pointer (inner pointer to supplied "base_ptr").
194
206
*/
195
- void * wrap_with_guards (void * base_ptr, size_t user_size, const void * tag = nullptr ) {
207
+ void * wrap_with_guards (void * base_ptr, size_t user_size,
208
+ const void * tag = nullptr , const void * tag2 = nullptr ) {
196
209
assert (base_ptr != nullptr , " Attempt to wrap null with memory guard" );
197
210
_base_addr = (u_char*)base_ptr;
198
211
get_head_guard ()->build ();
199
212
get_head_guard ()->set_user_size (user_size);
200
213
get_tail_guard ()->build ();
201
214
set_tag (tag);
215
+ set_tag2 (tag2);
202
216
set_user_bytes (uninitBlockPad);
203
217
assert (verify_guards (), " Expected valid memory guards" );
204
218
return get_user_ptr ();
@@ -230,6 +244,20 @@ class GuardedMemory : StackObj { // Wrapper on stack
230
244
*/
231
245
void * get_tag () const { return get_head_guard ()->get_tag (); }
232
246
247
+ /* *
248
+ * Set the second general purpose tag.
249
+ *
250
+ * @param tag general purpose tag.
251
+ */
252
+ void set_tag2 (const void * tag) { get_head_guard ()->set_tag2 (tag); }
253
+
254
+ /* *
255
+ * Return the second general purpose tag.
256
+ *
257
+ * @return the second general purpose tag, defaults to null.
258
+ */
259
+ void * get_tag2 () const { return get_head_guard ()->get_tag2 (); }
260
+
233
261
/* *
234
262
* Return the size of the user data.
235
263
*
@@ -302,10 +330,12 @@ class GuardedMemory : StackObj { // Wrapper on stack
302
330
* @param ptr the memory to be copied
303
331
* @param len the length of the copy
304
332
* @param tag optional general purpose tag (see GuardedMemory::get_tag())
333
+ * @param tag2 optional general purpose tag (see GuardedMemory::get_tag2())
305
334
*
306
335
* @return guarded wrapped memory pointer to the user area, or null if OOM.
307
336
*/
308
- static void * wrap_copy (const void * p, const size_t len, const void * tag = nullptr );
337
+ static void * wrap_copy (const void * p, const size_t len,
338
+ const void * tag = nullptr , const void * tag2 = nullptr );
309
339
310
340
/* *
311
341
* Free wrapped copy.
0 commit comments