Path: blob/master/test/hotspot/gtest/metaspace/test_is_metaspace_obj.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/allocation.inline.hpp"27#include "memory/classLoaderMetaspace.hpp"28#include "memory/metaspace.hpp"29#include "memory/metaspace/virtualSpaceList.hpp"30#include "runtime/mutex.hpp"31#include "runtime/mutexLocker.hpp"32#include "runtime/os.hpp"33#include "unittest.hpp"3435using namespace metaspace;3637// Test the cheerful multitude of metaspace-contains-functions.38class MetaspaceIsMetaspaceObjTest {39Mutex* _lock;40ClassLoaderMetaspace* _ms;4142public:4344MetaspaceIsMetaspaceObjTest() : _lock(NULL), _ms(NULL) {}45~MetaspaceIsMetaspaceObjTest() {46delete _ms;47delete _lock;48}4950void do_test(Metaspace::MetadataType mdType) {51_lock = new Mutex(Monitor::native, "gtest-IsMetaspaceObjTest-lock", false, Monitor::_safepoint_check_never);52{53MutexLocker ml(_lock, Mutex::_no_safepoint_check_flag);54_ms = new ClassLoaderMetaspace(_lock, Metaspace::StandardMetaspaceType);55}5657const MetaspaceObj* p = (MetaspaceObj*) _ms->allocate(42, mdType);5859// Test MetaspaceObj::is_metaspace_object60ASSERT_TRUE(MetaspaceObj::is_valid(p));6162// A misaligned object shall not be recognized63const MetaspaceObj* p_misaligned = (MetaspaceObj*)((address)p) + 1;64ASSERT_FALSE(MetaspaceObj::is_valid(p_misaligned));6566// Test VirtualSpaceList::contains67const VirtualSpaceList* const vslist =68(mdType == Metaspace::ClassType && Metaspace::using_class_space()) ?69VirtualSpaceList::vslist_class() : VirtualSpaceList::vslist_nonclass();7071ASSERT_TRUE(vslist->contains((MetaWord*)p));7273// A misaligned pointer shall still be recognized by list::contains74ASSERT_TRUE(vslist->contains((MetaWord*)((address)p) + 1));7576// Now for some bogus values77ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)NULL));7879// Should exercise various paths in MetaspaceObj::is_valid()80ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)1024));81ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)8192));8283MetaspaceObj* p_stack = (MetaspaceObj*) &_lock;84ASSERT_FALSE(MetaspaceObj::is_valid(p_stack));8586MetaspaceObj* p_heap = (MetaspaceObj*) os::malloc(41, mtInternal);87ASSERT_FALSE(MetaspaceObj::is_valid(p_heap));88os::free(p_heap);8990// Test Metaspace::contains_xxx91ASSERT_TRUE(Metaspace::contains(p));92ASSERT_TRUE(Metaspace::contains_non_shared(p));9394delete _ms;95_ms = NULL;96delete _lock;97_lock = NULL;98}99100};101102TEST_VM(metaspace, is_metaspace_obj_non_class) {103MetaspaceIsMetaspaceObjTest test;104test.do_test(Metaspace::NonClassType);105}106107TEST_VM(metaspace, is_metaspace_obj_class) {108MetaspaceIsMetaspaceObjTest test;109test.do_test(Metaspace::ClassType);110}111112113114