1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 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 
  28 #include "memory/allocation.inline.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 
  39 
  40 // Test the cheerful multitude of metaspace-contains-functions.
  41 class MetaspaceIsMetaspaceObjTest {
  42   Mutex* _lock;
  43   ClassLoaderMetaspace* _ms;
  44 
  45 public:
  46 
  47   MetaspaceIsMetaspaceObjTest() : _lock(NULL), _ms(NULL) {}
  48   ~MetaspaceIsMetaspaceObjTest() {
  49     delete _ms;
  50     delete _lock;
  51   }
  52 
  53   void do_test(Metaspace::MetadataType mdType) {
  54     _lock = new Mutex(Monitor::native, "gtest-IsMetaspaceObjTest-lock", false, Monitor::_safepoint_check_never);
  55     {
  56       MutexLocker ml(_lock, Mutex::_no_safepoint_check_flag);
  57       _ms = new ClassLoaderMetaspace(_lock, Metaspace::StandardMetaspaceType);
  58     }
  59 
  60     const MetaspaceObj* p = (MetaspaceObj*) _ms->allocate(42, mdType);
  61 
  62     // Test MetaspaceObj::is_metaspace_object
  63     ASSERT_TRUE(MetaspaceObj::is_valid(p));
  64 
  65     // A misaligned object shall not be recognized
  66     const MetaspaceObj* p_misaligned = (MetaspaceObj*)((address)p) + 1;
  67     ASSERT_FALSE(MetaspaceObj::is_valid(p_misaligned));
  68 
  69     // Test VirtualSpaceList::contains
  70     const VirtualSpaceList* const vslist =
  71         (mdType == Metaspace::ClassType && Metaspace::using_class_space()) ?
  72          VirtualSpaceList::vslist_class() : VirtualSpaceList::vslist_nonclass();
  73 
  74     ASSERT_TRUE(vslist->contains((MetaWord*)p));
  75 
  76     // A misaligned pointer shall still be recognized by list::contains
  77     ASSERT_TRUE(vslist->contains((MetaWord*)((address)p) + 1));
  78 
  79     // Now for some bogus values
  80     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)NULL));
  81 
  82     // Should exercise various paths in MetaspaceObj::is_valid()
  83     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)1024));
  84     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)8192));
  85 
  86     MetaspaceObj* p_stack = (MetaspaceObj*) &_lock;
  87     ASSERT_FALSE(MetaspaceObj::is_valid(p_stack));
  88 
  89     MetaspaceObj* p_heap = (MetaspaceObj*) os::malloc(41, mtInternal);
  90     ASSERT_FALSE(MetaspaceObj::is_valid(p_heap));
  91     os::free(p_heap);
  92 
  93     // Test Metaspace::contains_xxx
  94     ASSERT_TRUE(Metaspace::contains(p));
  95     ASSERT_TRUE(Metaspace::contains_non_shared(p));
  96 
  97     delete _ms;
  98     _ms = NULL;
  99     delete _lock;
 100     _lock = NULL;
 101   }
 102 
 103 };
 104 
 105 TEST_VM(metaspace, is_metaspace_obj_non_class) {
 106   MetaspaceIsMetaspaceObjTest test;
 107   test.do_test(Metaspace::NonClassType);
 108 }
 109 
 110 TEST_VM(metaspace, is_metaspace_obj_class) {
 111   MetaspaceIsMetaspaceObjTest test;
 112   test.do_test(Metaspace::ClassType);
 113 }
 114