Path: blob/master/test/hotspot/gtest/memory/test_guardedMemory.cpp
41144 views
/*1* Copyright (c) 2016, 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*/2223#include "precompiled.hpp"24#include "memory/allocation.hpp"25#include "memory/allocation.inline.hpp"26#include "memory/guardedMemory.hpp"27#include "runtime/os.hpp"28#include "unittest.hpp"2930#define GEN_PURPOSE_TAG ((void *) ((uintptr_t)0xf000f000))3132static void guarded_memory_test_check(void* p, size_t sz, void* tag) {33ASSERT_TRUE(p != NULL) << "NULL pointer given to check";34u_char* c = (u_char*) p;35GuardedMemory guarded(c);36EXPECT_EQ(guarded.get_tag(), tag) << "Tag is not the same as supplied";37EXPECT_EQ(guarded.get_user_ptr(), c) << "User pointer is not the same as supplied";38EXPECT_EQ(guarded.get_user_size(), sz) << "User size is not the same as supplied";39EXPECT_TRUE(guarded.verify_guards()) << "Guard broken";40}4142class GuardedMemoryTest {43public:44static size_t get_guard_header_size() {45return sizeof (GuardedMemory::GuardHeader);46}47static size_t get_guard_size() {48return sizeof (GuardedMemory::Guard);49}50};5152// Test GuardedMemory size53TEST(GuardedMemory, size) {54size_t total_sz = GuardedMemory::get_total_size(1);55ASSERT_GT(total_sz, (size_t) 1) << "Unexpected size";56ASSERT_GE(total_sz, GuardedMemoryTest::get_guard_header_size() + 157+ GuardedMemoryTest::get_guard_size()) << "Unexpected size";58}5960// Test the basic characteristics61TEST(GuardedMemory, basic) {62u_char* basep =63(u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);64GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);6566EXPECT_EQ(badResourceValue, *basep)67<< "Expected guard in the form of badResourceValue";6869u_char* userp = guarded.get_user_ptr();70EXPECT_EQ(uninitBlockPad, *userp)71<< "Expected uninitialized data in the form of uninitBlockPad";72guarded_memory_test_check(userp, 1, GEN_PURPOSE_TAG);7374void* freep = guarded.release_for_freeing();75EXPECT_EQ((u_char*) freep, basep) << "Expected the same pointer guard was ";76EXPECT_EQ(freeBlockPad, *userp) << "Expected user data to be free block padded";77EXPECT_FALSE(guarded.verify_guards());78os::free(freep);79}8081// Test a number of odd sizes82TEST(GuardedMemory, odd_sizes) {83u_char* basep =84(u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);85GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);8687size_t sz = 0;88do {89void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);90void* up = guarded.wrap_with_guards(p, sz, (void*) 1);91memset(up, 0, sz);92guarded_memory_test_check(up, sz, (void*) 1);93if (HasFatalFailure()) {94return;95}9697os::free(guarded.release_for_freeing());98sz = (sz << 4) + 1;99} while (sz < (256 * 1024));100}101102// Test buffer overrun into head...103TEST(GuardedMemory, buffer_overrun_head) {104u_char* basep =105(u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);106GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);107108guarded.wrap_with_guards(basep, 1);109*basep = 0;110EXPECT_FALSE(guarded.verify_guards());111os::free(basep);112}113114// Test buffer overrun into tail with a number of odd sizes115TEST(GuardedMemory, buffer_overrun_tail) {116u_char* basep =117(u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal);118GuardedMemory guarded(basep, 1, GEN_PURPOSE_TAG);119120size_t sz = 1;121do {122void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal);123void* up = guarded.wrap_with_guards(p, sz, (void*) 1);124memset(up, 0, sz + 1); // Buffer-overwrite (within guard)125EXPECT_FALSE(guarded.verify_guards()) << "Guard was not broken as expected";126os::free(guarded.release_for_freeing());127sz = (sz << 4) + 1;128} while (sz < (256 * 1024));129}130131// Test wrap_copy/wrap_free132TEST(GuardedMemory, wrap) {133EXPECT_TRUE(GuardedMemory::free_copy(NULL)) << "Expected free NULL to be OK";134135const char* str = "Check my bounds out";136size_t str_sz = strlen(str) + 1;137char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz);138guarded_memory_test_check(str_copy, str_sz, NULL);139if (HasFatalFailure()) {140return;141}142EXPECT_EQ(0, strcmp(str, str_copy)) << "Not identical copy";143EXPECT_TRUE(GuardedMemory::free_copy(str_copy)) << "Free copy failed to verify";144145void* no_data = NULL;146void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0);147EXPECT_TRUE(GuardedMemory::free_copy(no_data_copy))148<< "Expected valid guards even for no data copy";149}150151152