Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/guardedMemory.cpp
32285 views
/*1* Copyright (c) 2014, 2018, 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}8182// test code...8384#ifndef PRODUCT8586#define GEN_PURPOSE_TAG ((void *) ((uintptr_t)0xf000f000))8788static void guarded_memory_test_check(void* p, size_t sz, void* tag) {89assert(p != NULL, "NULL pointer given to check");90u_char* c = (u_char*) p;91GuardedMemory guarded(c);92assert(guarded.get_tag() == tag, "Tag is not the same as supplied");93assert(guarded.get_user_ptr() == c, "User pointer is not the same as supplied");94assert(guarded.get_user_size() == sz, "User size is not the same as supplied");95assert(guarded.verify_guards(), "Guard broken");96}9798void GuardedMemory::test_guarded_memory() {99// Test the basic characteristics...100size_t total_sz = GuardedMemory::get_total_size(1);101assert(total_sz > 1 && total_sz >= (sizeof(GuardHeader) + 1 + sizeof(Guard)), "Unexpected size");102u_char* basep = (u_char*) os::malloc(total_sz, mtInternal);103104GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);105106assert(*basep == badResourceValue, "Expected guard in the form of badResourceValue");107u_char* userp = guarded.get_user_ptr();108assert(*userp == uninitBlockPad, "Expected uninitialized data in the form of uninitBlockPad");109guarded_memory_test_check(userp, 1, GEN_PURPOSE_TAG);110111void* freep = guarded.release_for_freeing();112assert((u_char*)freep == basep, "Expected the same pointer guard was ");113assert(*userp == freeBlockPad, "Expected user data to be free block padded");114assert(!guarded.verify_guards(), "Expected failed");115os::free(freep);116117// Test a number of odd sizes...118size_t sz = 0;119do {120void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);121void* up = guarded.wrap_with_guards(p, sz, (void*)1);122memset(up, 0, sz);123guarded_memory_test_check(up, sz, (void*)1);124os::free(guarded.release_for_freeing());125sz = (sz << 4) + 1;126} while (sz < (256 * 1024));127128// Test buffer overrun into head...129basep = (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);130guarded.wrap_with_guards(basep, 1);131*basep = 0;132assert(!guarded.verify_guards(), "Expected failure");133os::free(basep);134135// Test buffer overrun into tail with a number of odd sizes...136sz = 1;137do {138void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);139void* up = guarded.wrap_with_guards(p, sz, (void*)1);140memset(up, 0, sz + 1); // Buffer-overwrite (within guard)141assert(!guarded.verify_guards(), "Guard was not broken as expected");142os::free(guarded.release_for_freeing());143sz = (sz << 4) + 1;144} while (sz < (256 * 1024));145146// Test wrap_copy/wrap_free...147assert(GuardedMemory::free_copy(NULL), "Expected free NULL to be OK");148149const char* str = "Check my bounds out";150size_t str_sz = strlen(str) + 1;151char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz);152guarded_memory_test_check(str_copy, str_sz, NULL);153assert(strcmp(str, str_copy) == 0, "Not identical copy");154assert(GuardedMemory::free_copy(str_copy), "Free copy failed to verify");155156void* no_data = NULL;157void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0);158assert(GuardedMemory::free_copy(no_data_copy), "Expected valid guards even for no data copy");159}160161#endif // !PRODUCT162163164165