1 /*
   2  * Copyright (c) 2016, 2018 Oracle and/or its affiliates. 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "memory/allocation.inline.hpp"
  26 #include "memory/metaspace.hpp"
  27 #include "memory/metaspace/virtualSpaceList.hpp"
  28 #include "runtime/mutex.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "runtime/os.hpp"
  31 #include "unittest.hpp"
  32 
  33 using namespace metaspace;
  34 
  35 #if 0
  36 
  37 // Test the cheerful multitude of metaspace-contains-functions.
  38 class MetaspaceIsMetaspaceObjTest : public ::testing::Test {
  39   Mutex* _lock;
  40   ClassLoaderMetaspace* _ms;
  41 
  42 public:
  43 
  44   MetaspaceIsMetaspaceObjTest() : _lock(NULL), _ms(NULL) {}
  45 
  46   virtual void SetUp() {
  47   }
  48 
  49   virtual void TearDown() {
  50     delete _ms;
  51     delete _lock;
  52   }
  53 
  54   void do_test(Metaspace::MetadataType mdType) {
  55     _lock = new Mutex(Monitor::native, "gtest-IsMetaspaceObjTest-lock", false, Monitor::_safepoint_check_never);
  56     {
  57       MutexLocker ml(_lock, Mutex::_no_safepoint_check_flag);
  58       _ms = new ClassLoaderMetaspace(_lock, Metaspace::StandardMetaspaceType);
  59     }
  60 
  61     const MetaspaceObj* p = (MetaspaceObj*) _ms->allocate(42, mdType);
  62 
  63     // Test MetaspaceObj::is_metaspace_object
  64     ASSERT_TRUE(MetaspaceObj::is_valid(p));
  65 
  66     // A misaligned object shall not be recognized
  67     const MetaspaceObj* p_misaligned = (MetaspaceObj*)((address)p) + 1;
  68     ASSERT_FALSE(MetaspaceObj::is_valid(p_misaligned));
  69 
  70     // Test VirtualSpaceList::contains and find_enclosing_space
  71     VirtualSpaceList* list = Metaspace::space_list();
  72     if (mdType == Metaspace::ClassType && Metaspace::using_class_space()) {
  73       list = Metaspace::class_space_list();
  74     }
  75     ASSERT_TRUE(list->contains(p));
  76     VirtualSpaceNode* const n = list->find_enclosing_space(p);
  77     ASSERT_TRUE(n != NULL);
  78     ASSERT_TRUE(n->contains(p));
  79 
  80     // A misaligned pointer shall be recognized by list::contains
  81     ASSERT_TRUE(list->contains((address)p) + 1);
  82 
  83     // Now for some bogus values
  84     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)NULL));
  85 
  86     // Should exercise various paths in MetaspaceObj::is_valid()
  87     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)1024));
  88     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)8192));
  89 
  90     MetaspaceObj* p_stack = (MetaspaceObj*) &_lock;
  91     ASSERT_FALSE(MetaspaceObj::is_valid(p_stack));
  92 
  93     MetaspaceObj* p_heap = (MetaspaceObj*) os::malloc(41, mtInternal);
  94     ASSERT_FALSE(MetaspaceObj::is_valid(p_heap));
  95     os::free(p_heap);
  96 
  97     // Test Metaspace::contains_xxx
  98     ASSERT_TRUE(Metaspace::contains(p));
  99     ASSERT_TRUE(Metaspace::contains_non_shared(p));
 100 
 101     delete _ms;
 102     _ms = NULL;
 103     delete _lock;
 104     _lock = NULL;
 105   }
 106 
 107 };
 108 
 109 TEST_VM_F(MetaspaceIsMetaspaceObjTest, non_class_space) {
 110   do_test(Metaspace::NonClassType);
 111 }
 112 
 113 TEST_VM_F(MetaspaceIsMetaspaceObjTest, class_space) {
 114   do_test(Metaspace::ClassType);
 115 }
 116 
 117 #endif