1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2020 SAP SE. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "memory/classLoaderMetaspace.hpp"
29 #include "memory/metaspace.hpp"
30 #include "memory/metaspace/virtualSpaceList.hpp"
31 #include "runtime/mutex.hpp"
32 #include "runtime/mutexLocker.hpp"
33 #include "runtime/os.hpp"
34 #include "unittest.hpp"
35 
36 using namespace metaspace;
37 
38 // Test the cheerful multitude of metaspace-contains-functions.
39 class MetaspaceIsMetaspaceObjTest {
40   Mutex* _lock;
41   ClassLoaderMetaspace* _ms;
42 
43 public:
44 
MetaspaceIsMetaspaceObjTest()45   MetaspaceIsMetaspaceObjTest() : _lock(NULL), _ms(NULL) {}
~MetaspaceIsMetaspaceObjTest()46   ~MetaspaceIsMetaspaceObjTest() {
47     delete _ms;
48     delete _lock;
49   }
50 
do_test(Metaspace::MetadataType mdType)51   void do_test(Metaspace::MetadataType mdType) {
52     _lock = new Mutex(Monitor::native, "gtest-IsMetaspaceObjTest-lock", false, Monitor::_safepoint_check_never);
53     {
54       MutexLocker ml(_lock, Mutex::_no_safepoint_check_flag);
55       _ms = new ClassLoaderMetaspace(_lock, Metaspace::StandardMetaspaceType);
56     }
57 
58     const MetaspaceObj* p = (MetaspaceObj*) _ms->allocate(42, mdType);
59 
60     // Test MetaspaceObj::is_metaspace_object
61     ASSERT_TRUE(MetaspaceObj::is_valid(p));
62 
63     // A misaligned object shall not be recognized
64     const MetaspaceObj* p_misaligned = (MetaspaceObj*)((address)p) + 1;
65     ASSERT_FALSE(MetaspaceObj::is_valid(p_misaligned));
66 
67     // Test VirtualSpaceList::contains
68     const VirtualSpaceList* const vslist =
69         (mdType == Metaspace::ClassType && Metaspace::using_class_space()) ?
70          VirtualSpaceList::vslist_class() : VirtualSpaceList::vslist_nonclass();
71 
72     ASSERT_TRUE(vslist->contains((MetaWord*)p));
73 
74     // A misaligned pointer shall still be recognized by list::contains
75     ASSERT_TRUE(vslist->contains((MetaWord*)((address)p) + 1));
76 
77     // Now for some bogus values
78     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)NULL));
79 
80     // Should exercise various paths in MetaspaceObj::is_valid()
81     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)1024));
82     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)8192));
83 
84     MetaspaceObj* p_stack = (MetaspaceObj*) &_lock;
85     ASSERT_FALSE(MetaspaceObj::is_valid(p_stack));
86 
87     MetaspaceObj* p_heap = (MetaspaceObj*) os::malloc(41, mtInternal);
88     ASSERT_FALSE(MetaspaceObj::is_valid(p_heap));
89     os::free(p_heap);
90 
91     // Test Metaspace::contains_xxx
92     ASSERT_TRUE(Metaspace::contains(p));
93     ASSERT_TRUE(Metaspace::contains_non_shared(p));
94 
95     delete _ms;
96     _ms = NULL;
97     delete _lock;
98     _lock = NULL;
99   }
100 
101 };
102 
TEST_VM(metaspace,is_metaspace_obj_non_class)103 TEST_VM(metaspace, is_metaspace_obj_non_class) {
104   MetaspaceIsMetaspaceObjTest test;
105   test.do_test(Metaspace::NonClassType);
106 }
107 
TEST_VM(metaspace,is_metaspace_obj_class)108 TEST_VM(metaspace, is_metaspace_obj_class) {
109   MetaspaceIsMetaspaceObjTest test;
110   test.do_test(Metaspace::ClassType);
111 }
112 
113