11/*
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.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2626#define SHARE_MEMORY_GUARDEDMEMORY_HPP
2727
2828#include " memory/allocation.hpp"
29+ #include " runtime/os.hpp"
2930#include " utilities/globalDefinitions.hpp"
3031
3132/* *
4344 * |base_addr | 0xABABABABABABABAB | Head guard |
4445 * |+16 | <size_t:user_size> | User data size |
4546 * |+sizeof(uintptr_t) | <tag> | Tag word |
47+ * |+sizeof(uintptr_t) | <tag2> | Tag word |
4648 * |+sizeof(void*) | 0xF1 <user_data> ( | User data |
4749 * |+user_size | 0xABABABABABABABAB | Tail guard |
4850 * -------------------------------------------------------------
4951 *
5052 * Where:
5153 * - guard padding uses "badResourceValue" (0xAB)
52- * - tag word is general purpose
54+ * - tag word and tag2 word are general purpose
5355 * - user data
5456 * -- initially padded with "uninitBlockPad" (0xF1),
5557 * -- to "freeBlockPad" (0xBA), when freed
@@ -111,6 +113,10 @@ class GuardedMemory : StackObj { // Wrapper on stack
111113 }
112114
113115 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+ }
114120 u_char* c = (u_char*) _guard;
115121 u_char* end = c + GUARD_SIZE;
116122 while (c < end) {
@@ -137,13 +143,16 @@ class GuardedMemory : StackObj { // Wrapper on stack
137143 size_t _user_size;
138144 };
139145 void * _tag;
146+ void * _tag2;
140147 public:
141148 void set_user_size (const size_t usz) { _user_size = usz; }
142149 size_t get_user_size () const { return _user_size; }
143150
144151 void set_tag (const void * tag) { _tag = (void *) tag; }
145152 void * get_tag () const { return _tag; }
146153
154+ void set_tag2 (const void * tag2) { _tag2 = (void *) tag2; }
155+ void * get_tag2 () const { return _tag2; }
147156 }; // GuardedMemory::GuardHeader
148157
149158 // Guarded Memory...
@@ -162,9 +171,11 @@ class GuardedMemory : StackObj { // Wrapper on stack
162171 * @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
163172 * @param user_size the size of the user data to be wrapped.
164173 * @param tag optional general purpose tag.
174+ * @param tag2 optional second general purpose tag.
165175 */
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);
168179 }
169180
170181 /* *
@@ -189,16 +200,19 @@ class GuardedMemory : StackObj { // Wrapper on stack
189200 * @param base_ptr allocation wishing to be wrapped, must be at least "GuardedMemory::get_total_size()" bytes.
190201 * @param user_size the size of the user data to be wrapped.
191202 * @param tag optional general purpose tag.
203+ * @param tag2 optional second general purpose tag.
192204 *
193205 * @return user data pointer (inner pointer to supplied "base_ptr").
194206 */
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 ) {
196209 assert (base_ptr != nullptr , " Attempt to wrap null with memory guard" );
197210 _base_addr = (u_char*)base_ptr;
198211 get_head_guard ()->build ();
199212 get_head_guard ()->set_user_size (user_size);
200213 get_tail_guard ()->build ();
201214 set_tag (tag);
215+ set_tag2 (tag2);
202216 set_user_bytes (uninitBlockPad);
203217 assert (verify_guards (), " Expected valid memory guards" );
204218 return get_user_ptr ();
@@ -230,6 +244,20 @@ class GuardedMemory : StackObj { // Wrapper on stack
230244 */
231245 void * get_tag () const { return get_head_guard ()->get_tag (); }
232246
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+
233261 /* *
234262 * Return the size of the user data.
235263 *
@@ -302,10 +330,12 @@ class GuardedMemory : StackObj { // Wrapper on stack
302330 * @param ptr the memory to be copied
303331 * @param len the length of the copy
304332 * @param tag optional general purpose tag (see GuardedMemory::get_tag())
333+ * @param tag2 optional general purpose tag (see GuardedMemory::get_tag2())
305334 *
306335 * @return guarded wrapped memory pointer to the user area, or null if OOM.
307336 */
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 );
309339
310340 /* *
311341 * Free wrapped copy.
0 commit comments