1 | /* |
2 | * Copyright (c) 2014, 2016, 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 | |