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