Mercurial > hg > graal-compiler
comparison src/share/vm/memory/guardedMemory.cpp @ 20318:fa62fb12cdca
6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
Summary: Wrapped memory with standard bounds checking "GuardedMemory".
Reviewed-by: zgu, fparain, dcubed
author | dsimms |
---|---|
date | Thu, 14 Aug 2014 15:16:07 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
20317:ee019285a52c | 20318:fa62fb12cdca |
---|---|
1 /* | |
2 * Copyright (c) 2014, 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 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 #endif // !PRODUCT | |
161 |