Path: blob/master/test/hotspot/gtest/metaspace/test_allocationGuard.cpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "memory/metaspace/metaspaceArena.hpp"27#include "memory/metaspace/metaspaceSettings.hpp"28#include "memory/metaspace/testHelpers.hpp"29#include "utilities/debug.hpp"30#include "utilities/ostream.hpp"3132//#define LOG_PLEASE33#include "metaspaceGtestCommon.hpp"34#include "metaspaceGtestContexts.hpp"3536#ifdef ASSERT3738using metaspace::MetaspaceArena;39using metaspace::MetaspaceTestArena;40using metaspace::Settings;4142// Test that overwriting memory triggers an assert if allocation guards are enabled.43// Note: We use TEST_VM_ASSERT_MSG. However, an assert is only triggered if allocation44// guards are enabled; if guards are disabled for the gtests, this test would fail.45// So for that case, we trigger a fake assert.46TEST_VM_ASSERT_MSG(metaspace, test_overwriter, ".*failed: Corrupt block") {4748if (Settings::use_allocation_guard()) {49MetaspaceGtestContext context;50MetaspaceTestArena* arena = context.create_arena(Metaspace::StandardMetaspaceType);51// We allocate two blocks. We then write over the end of the first block, which52// should corrupt the eyecatcher at the start of the second block.53// Note: there is of course no guarantee that blocks allocated sequentially are neighbors;54// but in this case (clean standard-sized test arena and very small allocations) it can55// be safely assumed).56MetaWord* p1 = arena->allocate(8);57MetaWord* p2 = arena->allocate(2);58p1[8] = (MetaWord)0x9345; // Overwriter59// Now we delete the arena (as happens during class unloading); this will check all60// block canaries and should trigger an assert (see MetaspaceArena::verify_allocation_guards()).61tty->print_cr("Death test, please ignore the following \"Corrupt block\" printout.");62delete arena;63} else {64assert(false, "Corrupt block fake message to satisfy tests");65}6667}6869#endif // ASSERT707172