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 
  36 // Test the cheerful multitude of metaspace-contains-functions.
  37 class MetaspaceIsMetaspaceObjTest : public ::testing::Test {
  38   Mutex* _lock;
  39   ClassLoaderMetaspace* _ms;
  40 
  41 public:
  42 
  43   MetaspaceIsMetaspaceObjTest() : _lock(NULL), _ms(NULL) {}
  44 
  45   virtual void SetUp() {
  46   }
  47 
  48   virtual void TearDown() {
  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 and find_enclosing_space
  70     VirtualSpaceList* list = Metaspace::space_list();
  71     if (mdType == Metaspace::ClassType && Metaspace::using_class_space()) {
  72       list = Metaspace::class_space_list();
  73     }
  74     ASSERT_TRUE(list->contains(p));
  75     VirtualSpaceNode* const n = list->find_enclosing_space(p);
  76     ASSERT_TRUE(n != NULL);
  77     ASSERT_TRUE(n->contains(p));
  78 
  79     // A misaligned pointer shall be recognized by list::contains
  80     ASSERT_TRUE(list->contains((address)p) + 1);
  81 
  82     // Now for some bogus values
  83     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)NULL));
  84 
  85     // Should exercise various paths in MetaspaceObj::is_valid()
  86     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)1024));
  87     ASSERT_FALSE(MetaspaceObj::is_valid((MetaspaceObj*)8192));
  88 
  89     MetaspaceObj* p_stack = (MetaspaceObj*) &_lock;
  90     ASSERT_FALSE(MetaspaceObj::is_valid(p_stack));
  91 
  92     MetaspaceObj* p_heap = (MetaspaceObj*) os::malloc(41, mtInternal);
  93     ASSERT_FALSE(MetaspaceObj::is_valid(p_heap));
  94     os::free(p_heap);
  95 
  96     // Test Metaspace::contains_xxx
  97     ASSERT_TRUE(Metaspace::contains(p));
  98     ASSERT_TRUE(Metaspace::contains_non_shared(p));
  99 
 100     delete _ms;
 101     _ms = NULL;
 102     delete _lock;
 103     _lock = NULL;
 104   }
 105 
 106 };
 107 
 108 TEST_VM_F(MetaspaceIsMetaspaceObjTest, non_class_space) {
 109   do_test(Metaspace::NonClassType);
 110 }
 111 
 112 TEST_VM_F(MetaspaceIsMetaspaceObjTest, class_space) {
 113   do_test(Metaspace::ClassType);
 114 }
 115