Path: blob/master/src/hotspot/share/memory/guardedMemory.cpp
40949 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "memory/allocation.hpp"25#include "memory/allocation.inline.hpp"26#include "memory/guardedMemory.hpp"27#include "runtime/os.hpp"2829void* GuardedMemory::wrap_copy(const void* ptr, const size_t len, const void* tag) {30size_t total_sz = GuardedMemory::get_total_size(len);31void* outerp = os::malloc(total_sz, mtInternal);32if (outerp != NULL) {33GuardedMemory guarded(outerp, len, tag);34void* innerp = guarded.get_user_ptr();35memcpy(innerp, ptr, len);36return innerp;37}38return NULL; // OOM39}4041bool GuardedMemory::free_copy(void* p) {42if (p == NULL) {43return true;44}45GuardedMemory guarded((u_char*)p);46bool verify_ok = guarded.verify_guards();4748/* always attempt to free, pass problem on to any nested memchecker */49os::free(guarded.release_for_freeing());5051return verify_ok;52}5354void GuardedMemory::print_on(outputStream* st) const {55if (_base_addr == NULL) {56st->print_cr("GuardedMemory(" PTR_FORMAT ") not associated to any memory", p2i(this));57return;58}59st->print_cr("GuardedMemory(" PTR_FORMAT ") base_addr=" PTR_FORMAT60" tag=" PTR_FORMAT " user_size=" SIZE_FORMAT " user_data=" PTR_FORMAT,61p2i(this), p2i(_base_addr), p2i(get_tag()), get_user_size(), p2i(get_user_ptr()));6263Guard* guard = get_head_guard();64st->print_cr(" Header guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN"));65guard = get_tail_guard();66st->print_cr(" Trailer guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN"));6768u_char udata = *get_user_ptr();69switch (udata) {70case uninitBlockPad:71st->print_cr(" User data appears unused");72break;73case freeBlockPad:74st->print_cr(" User data appears to have been freed");75break;76default:77st->print_cr(" User data appears to be in use");78break;79}80}818283