dsimms@7032: /* dsimms@7032: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. dsimms@7032: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. dsimms@7032: * dsimms@7032: * This code is free software; you can redistribute it and/or modify it dsimms@7032: * under the terms of the GNU General Public License version 2 only, as dsimms@7032: * published by the Free Software Foundation. dsimms@7032: * dsimms@7032: * This code is distributed in the hope that it will be useful, but WITHOUT dsimms@7032: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or dsimms@7032: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License dsimms@7032: * version 2 for more details (a copy is included in the LICENSE file that dsimms@7032: * accompanied this code). dsimms@7032: * dsimms@7032: * You should have received a copy of the GNU General Public License version dsimms@7032: * 2 along with this work; if not, write to the Free Software Foundation, dsimms@7032: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. dsimms@7032: * dsimms@7032: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA dsimms@7032: * or visit www.oracle.com if you need additional information or have any dsimms@7032: * questions. dsimms@7032: * dsimms@7032: */ dsimms@7032: #include "precompiled.hpp" dsimms@7032: #include "memory/allocation.hpp" dsimms@7032: #include "memory/allocation.inline.hpp" dsimms@7032: #include "memory/guardedMemory.hpp" dsimms@7032: #include "runtime/os.hpp" dsimms@7032: dsimms@7032: void* GuardedMemory::wrap_copy(const void* ptr, const size_t len, const void* tag) { dsimms@7032: size_t total_sz = GuardedMemory::get_total_size(len); dsimms@7032: void* outerp = os::malloc(total_sz, mtInternal); dsimms@7032: if (outerp != NULL) { dsimms@7032: GuardedMemory guarded(outerp, len, tag); dsimms@7032: void* innerp = guarded.get_user_ptr(); dsimms@7032: memcpy(innerp, ptr, len); dsimms@7032: return innerp; dsimms@7032: } dsimms@7032: return NULL; // OOM dsimms@7032: } dsimms@7032: dsimms@7032: bool GuardedMemory::free_copy(void* p) { dsimms@7032: if (p == NULL) { dsimms@7032: return true; dsimms@7032: } dsimms@7032: GuardedMemory guarded((u_char*)p); dsimms@7032: bool verify_ok = guarded.verify_guards(); dsimms@7032: dsimms@7032: /* always attempt to free, pass problem on to any nested memchecker */ dsimms@7032: os::free(guarded.release_for_freeing()); dsimms@7032: dsimms@7032: return verify_ok; dsimms@7032: } dsimms@7032: dsimms@7032: void GuardedMemory::print_on(outputStream* st) const { dsimms@7032: if (_base_addr == NULL) { dsimms@7032: st->print_cr("GuardedMemory(" PTR_FORMAT ") not associated to any memory", p2i(this)); dsimms@7032: return; dsimms@7032: } dsimms@7032: st->print_cr("GuardedMemory(" PTR_FORMAT ") base_addr=" PTR_FORMAT dsimms@7032: " tag=" PTR_FORMAT " user_size=" SIZE_FORMAT " user_data=" PTR_FORMAT, dsimms@7032: p2i(this), p2i(_base_addr), p2i(get_tag()), get_user_size(), p2i(get_user_ptr())); dsimms@7032: dsimms@7032: Guard* guard = get_head_guard(); dsimms@7032: st->print_cr(" Header guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN")); dsimms@7032: guard = get_tail_guard(); dsimms@7032: st->print_cr(" Trailer guard @" PTR_FORMAT " is %s", p2i(guard), (guard->verify() ? "OK" : "BROKEN")); dsimms@7032: dsimms@7032: u_char udata = *get_user_ptr(); dsimms@7032: switch (udata) { dsimms@7032: case uninitBlockPad: dsimms@7032: st->print_cr(" User data appears unused"); dsimms@7032: break; dsimms@7032: case freeBlockPad: dsimms@7032: st->print_cr(" User data appears to have been freed"); dsimms@7032: break; dsimms@7032: default: dsimms@7032: st->print_cr(" User data appears to be in use"); dsimms@7032: break; dsimms@7032: } dsimms@7032: } dsimms@7032: dsimms@7032: // test code... dsimms@7032: dsimms@7032: #ifndef PRODUCT dsimms@7032: dsimms@7032: static void guarded_memory_test_check(void* p, size_t sz, void* tag) { dsimms@7032: assert(p != NULL, "NULL pointer given to check"); dsimms@7032: u_char* c = (u_char*) p; dsimms@7032: GuardedMemory guarded(c); dsimms@7032: assert(guarded.get_tag() == tag, "Tag is not the same as supplied"); dsimms@7032: assert(guarded.get_user_ptr() == c, "User pointer is not the same as supplied"); dsimms@7032: assert(guarded.get_user_size() == sz, "User size is not the same as supplied"); dsimms@7032: assert(guarded.verify_guards(), "Guard broken"); dsimms@7032: } dsimms@7032: dsimms@7032: void GuardedMemory::test_guarded_memory() { dsimms@7032: // Test the basic characteristics... dsimms@7032: size_t total_sz = GuardedMemory::get_total_size(1); dsimms@7032: assert(total_sz > 1 && total_sz >= (sizeof(GuardHeader) + 1 + sizeof(Guard)), "Unexpected size"); dsimms@7032: u_char* basep = (u_char*) os::malloc(total_sz, mtInternal); dsimms@7032: dsimms@7032: GuardedMemory guarded(basep, 1, (void*)0xf000f000); dsimms@7032: dsimms@7032: assert(*basep == badResourceValue, "Expected guard in the form of badResourceValue"); dsimms@7032: u_char* userp = guarded.get_user_ptr(); dsimms@7032: assert(*userp == uninitBlockPad, "Expected uninitialized data in the form of uninitBlockPad"); dsimms@7032: guarded_memory_test_check(userp, 1, (void*)0xf000f000); dsimms@7032: dsimms@7032: void* freep = guarded.release_for_freeing(); dsimms@7032: assert((u_char*)freep == basep, "Expected the same pointer guard was "); dsimms@7032: assert(*userp == freeBlockPad, "Expected user data to be free block padded"); dsimms@7032: assert(!guarded.verify_guards(), "Expected failed"); dsimms@7032: os::free(freep); dsimms@7032: dsimms@7032: // Test a number of odd sizes... dsimms@7032: size_t sz = 0; dsimms@7032: do { dsimms@7032: void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal); dsimms@7032: void* up = guarded.wrap_with_guards(p, sz, (void*)1); dsimms@7032: memset(up, 0, sz); dsimms@7032: guarded_memory_test_check(up, sz, (void*)1); dsimms@7032: os::free(guarded.release_for_freeing()); dsimms@7032: sz = (sz << 4) + 1; dsimms@7032: } while (sz < (256 * 1024)); dsimms@7032: dsimms@7032: // Test buffer overrun into head... dsimms@7032: basep = (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal); dsimms@7032: guarded.wrap_with_guards(basep, 1); dsimms@7032: *basep = 0; dsimms@7032: assert(!guarded.verify_guards(), "Expected failure"); dsimms@7032: os::free(basep); dsimms@7032: dsimms@7032: // Test buffer overrun into tail with a number of odd sizes... dsimms@7032: sz = 1; dsimms@7032: do { dsimms@7032: void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal); dsimms@7032: void* up = guarded.wrap_with_guards(p, sz, (void*)1); dsimms@7032: memset(up, 0, sz + 1); // Buffer-overwrite (within guard) dsimms@7032: assert(!guarded.verify_guards(), "Guard was not broken as expected"); dsimms@7032: os::free(guarded.release_for_freeing()); dsimms@7032: sz = (sz << 4) + 1; dsimms@7032: } while (sz < (256 * 1024)); dsimms@7032: dsimms@7032: // Test wrap_copy/wrap_free... dsimms@7032: assert(GuardedMemory::free_copy(NULL), "Expected free NULL to be OK"); dsimms@7032: dsimms@7032: const char* str = "Check my bounds out"; dsimms@7032: size_t str_sz = strlen(str) + 1; dsimms@7032: char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz); dsimms@7032: guarded_memory_test_check(str_copy, str_sz, NULL); dsimms@7032: assert(strcmp(str, str_copy) == 0, "Not identical copy"); dsimms@7032: assert(GuardedMemory::free_copy(str_copy), "Free copy failed to verify"); dsimms@7032: dsimms@7032: void* no_data = NULL; dsimms@7032: void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0); dsimms@7032: assert(GuardedMemory::free_copy(no_data_copy), "Expected valid guards even for no data copy"); dsimms@7032: } dsimms@7032: dsimms@7032: #endif // !PRODUCT dsimms@7032: