77 default: |
77 default: |
78 st->print_cr(" User data appears to be in use"); |
78 st->print_cr(" User data appears to be in use"); |
79 break; |
79 break; |
80 } |
80 } |
81 } |
81 } |
82 |
|
83 // test code... |
|
84 |
|
85 #ifndef PRODUCT |
|
86 |
|
87 static void guarded_memory_test_check(void* p, size_t sz, void* tag) { |
|
88 assert(p != NULL, "NULL pointer given to check"); |
|
89 u_char* c = (u_char*) p; |
|
90 GuardedMemory guarded(c); |
|
91 assert(guarded.get_tag() == tag, "Tag is not the same as supplied"); |
|
92 assert(guarded.get_user_ptr() == c, "User pointer is not the same as supplied"); |
|
93 assert(guarded.get_user_size() == sz, "User size is not the same as supplied"); |
|
94 assert(guarded.verify_guards(), "Guard broken"); |
|
95 } |
|
96 |
|
97 void GuardedMemory::test_guarded_memory() { |
|
98 // Test the basic characteristics... |
|
99 size_t total_sz = GuardedMemory::get_total_size(1); |
|
100 assert(total_sz > 1 && total_sz >= (sizeof(GuardHeader) + 1 + sizeof(Guard)), "Unexpected size"); |
|
101 u_char* basep = (u_char*) os::malloc(total_sz, mtInternal); |
|
102 |
|
103 GuardedMemory guarded(basep, 1, (void*)0xf000f000); |
|
104 |
|
105 assert(*basep == badResourceValue, "Expected guard in the form of badResourceValue"); |
|
106 u_char* userp = guarded.get_user_ptr(); |
|
107 assert(*userp == uninitBlockPad, "Expected uninitialized data in the form of uninitBlockPad"); |
|
108 guarded_memory_test_check(userp, 1, (void*)0xf000f000); |
|
109 |
|
110 void* freep = guarded.release_for_freeing(); |
|
111 assert((u_char*)freep == basep, "Expected the same pointer guard was "); |
|
112 assert(*userp == freeBlockPad, "Expected user data to be free block padded"); |
|
113 assert(!guarded.verify_guards(), "Expected failed"); |
|
114 os::free(freep); |
|
115 |
|
116 // Test a number of odd sizes... |
|
117 size_t sz = 0; |
|
118 do { |
|
119 void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal); |
|
120 void* up = guarded.wrap_with_guards(p, sz, (void*)1); |
|
121 memset(up, 0, sz); |
|
122 guarded_memory_test_check(up, sz, (void*)1); |
|
123 os::free(guarded.release_for_freeing()); |
|
124 sz = (sz << 4) + 1; |
|
125 } while (sz < (256 * 1024)); |
|
126 |
|
127 // Test buffer overrun into head... |
|
128 basep = (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal); |
|
129 guarded.wrap_with_guards(basep, 1); |
|
130 *basep = 0; |
|
131 assert(!guarded.verify_guards(), "Expected failure"); |
|
132 os::free(basep); |
|
133 |
|
134 // Test buffer overrun into tail with a number of odd sizes... |
|
135 sz = 1; |
|
136 do { |
|
137 void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal); |
|
138 void* up = guarded.wrap_with_guards(p, sz, (void*)1); |
|
139 memset(up, 0, sz + 1); // Buffer-overwrite (within guard) |
|
140 assert(!guarded.verify_guards(), "Guard was not broken as expected"); |
|
141 os::free(guarded.release_for_freeing()); |
|
142 sz = (sz << 4) + 1; |
|
143 } while (sz < (256 * 1024)); |
|
144 |
|
145 // Test wrap_copy/wrap_free... |
|
146 assert(GuardedMemory::free_copy(NULL), "Expected free NULL to be OK"); |
|
147 |
|
148 const char* str = "Check my bounds out"; |
|
149 size_t str_sz = strlen(str) + 1; |
|
150 char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz); |
|
151 guarded_memory_test_check(str_copy, str_sz, NULL); |
|
152 assert(strcmp(str, str_copy) == 0, "Not identical copy"); |
|
153 assert(GuardedMemory::free_copy(str_copy), "Free copy failed to verify"); |
|
154 |
|
155 void* no_data = NULL; |
|
156 void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0); |
|
157 assert(GuardedMemory::free_copy(no_data_copy), "Expected valid guards even for no data copy"); |
|
158 } |
|
159 |
|
160 void GuardedMemory_test() { |
|
161 GuardedMemory::test_guarded_memory(); |
|
162 } |
|
163 |
|
164 #endif // !PRODUCT |
|
165 |
|