1 /*
  2  * Copyright (c) 2019, 2025, 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 "classfile/vmClasses.hpp"
 25 #include "memory/resourceArea.hpp"
 26 #include "memory/universe.hpp"
 27 #include "oops/instanceKlass.hpp"
 28 #include "oops/oop.inline.hpp"
 29 #include "runtime/atomicAccess.hpp"
 30 #include "runtime/interfaceSupport.inline.hpp"
 31 #include "runtime/orderAccess.hpp"
 32 #include "runtime/os.hpp"
 33 #include "runtime/semaphore.inline.hpp"
 34 #include "runtime/synchronizer.hpp"
 35 #include "threadHelper.inline.hpp"
 36 #include "unittest.hpp"
 37 #include "utilities/globalDefinitions.hpp"
 38 #include "utilities/ostream.hpp"
 39 
 40 // The test doesn't work for PRODUCT because it needs WizardMode
 41 #ifndef PRODUCT
 42 
 43 template<typename Printable>
 44 static void assert_test_pattern(Printable object, const char* pattern) {
 45   stringStream st;
 46   object->print_on(&st);
 47   ASSERT_THAT(st.base(), testing::HasSubstr(pattern));
 48 }
 49 
 50 template<typename Printable>
 51 static void assert_mark_word_print_pattern(Printable object, const char* pattern) {
 52   assert_test_pattern(object, pattern);
 53 }
 54 
 55 class LockerThread : public JavaTestThread {
 56   oop _obj;
 57   public:
 58   LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}
 59   virtual ~LockerThread() {}
 60 
 61   void main_run() {
 62     JavaThread* THREAD = JavaThread::current();
 63     HandleMark hm(THREAD);
 64     Handle h_obj(THREAD, _obj);
 65     ResourceMark rm(THREAD);
 66 
 67     // Wait gets the lock inflated.
 68     // The object will stay locked for the context of 'ol' so the lock will
 69     // still be inflated after the notify_all() call. Deflation can't happen
 70     // while an ObjectMonitor is "busy" and being locked is the most "busy"
 71     // state we have...
 72     ObjectLocker ol(h_obj, THREAD);
 73     ol.notify_all(THREAD);
 74     assert_test_pattern(h_obj, "monitor");
 75   }
 76 };
 77 
 78 
 79 TEST_VM(markWord, printing) {
 80   JavaThread* THREAD = JavaThread::current();
 81   ThreadInVMfromNative invm(THREAD);
 82   ResourceMark rm(THREAD);
 83 
 84   oop obj = vmClasses::Byte_klass()->allocate_instance(THREAD);
 85 
 86   FlagSetting fs(WizardMode, true);
 87 
 88   HandleMark hm(THREAD);
 89   Handle h_obj(THREAD, obj);
 90 
 91   // Thread tries to lock it.
 92   {
 93     ObjectLocker ol(h_obj, THREAD);
 94     assert_mark_word_print_pattern(h_obj, "locked");
 95   }
 96   assert_mark_word_print_pattern(h_obj, "is_unlocked no_hash");
 97 
 98   // Hash the object then print it.
 99   intx hash = h_obj->identity_hash();
100   assert_mark_word_print_pattern(h_obj, "is_unlocked hash=0x");
101 
102   // Wait gets the lock inflated.
103   {
104     ObjectLocker ol(h_obj, THREAD);
105 
106     Semaphore done(0);
107     LockerThread* st;
108     st = new LockerThread(&done, h_obj());
109     st->doit();
110 
111     ol.wait_uninterruptibly(THREAD);
112     assert_test_pattern(h_obj, "monitor");
113     done.wait_with_safepoint_check(THREAD);  // wait till the thread is done.
114   }
115 }
116 
117 static void assert_unlocked_state(markWord mark) {
118   EXPECT_FALSE(mark.has_displaced_mark_helper());
119   EXPECT_FALSE(mark.is_fast_locked());
120   EXPECT_FALSE(mark.has_monitor());
121   EXPECT_FALSE(mark.is_locked());
122   EXPECT_TRUE(mark.is_unlocked());
123 }
124 
125 static void assert_copy_set_hash(markWord mark) {
126   const intptr_t hash = 4711;
127   EXPECT_TRUE(mark.has_no_hash());
128   markWord copy = mark.copy_set_hash(hash);
129   EXPECT_EQ(hash, copy.hash());
130   EXPECT_FALSE(copy.has_no_hash());
131 }
132 
133 static void assert_type(markWord mark) {
134   EXPECT_FALSE(mark.is_flat_array());
135   EXPECT_FALSE(mark.is_inline_type());
136 }
137 
138 TEST_VM(markWord, prototype) {
139   markWord mark = markWord::prototype();
140   assert_unlocked_state(mark);
141   EXPECT_TRUE(mark.is_neutral());
142 
143   assert_type(mark);
144 
145   EXPECT_TRUE(mark.has_no_hash());
146   EXPECT_FALSE(mark.is_marked());
147 
148   assert_copy_set_hash(mark);
149   assert_type(mark);
150 }
151 
152 static void assert_inline_type(markWord mark) {
153   EXPECT_FALSE(mark.is_flat_array());
154   EXPECT_TRUE(mark.is_inline_type());
155   EXPECT_FALSE(mark.is_null_free_array());
156 }
157 
158 TEST_VM(markWord, inline_type_prototype) {
159   markWord mark = markWord::inline_type_prototype();
160   assert_unlocked_state(mark);
161   // Don't call mark.is_neutral() on value class instances
162   assert_test_pattern(&mark, " inline_type");
163 
164   assert_inline_type(mark);
165 
166   EXPECT_TRUE(mark.has_no_hash());
167   EXPECT_FALSE(mark.is_marked());
168 }
169 
170 #if _LP64
171 
172 static void assert_flat_array_type(markWord mark) {
173   EXPECT_TRUE(mark.is_flat_array());
174   EXPECT_FALSE(mark.is_inline_type());
175 }
176 
177 TEST_VM(markWord, null_free_flat_array_prototype) {
178   markWord mark = markWord::flat_array_prototype(true /* null_free */);
179   assert_unlocked_state(mark);
180   EXPECT_TRUE(mark.is_neutral());
181 
182   assert_flat_array_type(mark);
183   EXPECT_TRUE(mark.is_null_free_array());
184 
185   EXPECT_TRUE(mark.has_no_hash());
186   EXPECT_FALSE(mark.is_marked());
187 
188   assert_copy_set_hash(mark);
189   assert_flat_array_type(mark);
190   EXPECT_TRUE(mark.is_null_free_array());
191 
192   assert_test_pattern(&mark, " flat_null_free_array");
193 }
194 
195 TEST_VM(markWord, nullable_flat_array_prototype) {
196   markWord mark = markWord::flat_array_prototype(false /* null_free */);
197   assert_unlocked_state(mark);
198   EXPECT_TRUE(mark.is_neutral());
199 
200   assert_flat_array_type(mark);
201   EXPECT_FALSE(mark.is_null_free_array());
202 
203   EXPECT_TRUE(mark.has_no_hash());
204   EXPECT_FALSE(mark.is_marked());
205 
206   assert_copy_set_hash(mark);
207   assert_flat_array_type(mark);
208   EXPECT_FALSE(mark.is_null_free_array());
209 
210   assert_test_pattern(&mark, " flat_array");
211 }
212 
213 static void assert_null_free_array_type(markWord mark) {
214   EXPECT_FALSE(mark.is_flat_array());
215   EXPECT_FALSE(mark.is_inline_type());
216   EXPECT_TRUE(mark.is_null_free_array());
217 }
218 
219 TEST_VM(markWord, null_free_array_prototype) {
220   markWord mark = markWord::null_free_array_prototype();
221   assert_unlocked_state(mark);
222   EXPECT_TRUE(mark.is_neutral());
223 
224   assert_null_free_array_type(mark);
225 
226   EXPECT_TRUE(mark.has_no_hash());
227   EXPECT_FALSE(mark.is_marked());
228 
229   assert_copy_set_hash(mark);
230   assert_null_free_array_type(mark);
231 
232   assert_test_pattern(&mark, " null_free_array");
233 }
234 #endif // _LP64
235 
236 #endif // PRODUCT