1 /* 2 * Copyright (c) 1997, 2024, 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 "oops/arrayOop.hpp" 26 #include "oops/oop.inline.hpp" 27 #include "unittest.hpp" 28 #include "utilities/globalDefinitions.hpp" 29 30 static bool check_max_length_overflow(BasicType type) { 31 julong length = arrayOopDesc::max_array_length(type); 32 julong bytes_per_element = type2aelembytes(type); 33 julong bytes = length * bytes_per_element 34 + arrayOopDesc::base_offset_in_bytes(type); 35 return (julong) (size_t) bytes == bytes; 36 } 37 38 TEST_VM(arrayOopDesc, boolean) { 39 ASSERT_PRED1(check_max_length_overflow, T_BOOLEAN); 40 } 41 42 TEST_VM(arrayOopDesc, char) { 43 ASSERT_PRED1(check_max_length_overflow, T_CHAR); 44 } 45 46 TEST_VM(arrayOopDesc, float) { 47 ASSERT_PRED1(check_max_length_overflow, T_FLOAT); 48 } 49 50 TEST_VM(arrayOopDesc, double) { 51 ASSERT_PRED1(check_max_length_overflow, T_DOUBLE); 52 } 53 54 TEST_VM(arrayOopDesc, byte) { 55 ASSERT_PRED1(check_max_length_overflow, T_BYTE); 56 } 57 58 TEST_VM(arrayOopDesc, short) { 59 ASSERT_PRED1(check_max_length_overflow, T_SHORT); 60 } 61 62 TEST_VM(arrayOopDesc, int) { 63 ASSERT_PRED1(check_max_length_overflow, T_INT); 64 } 65 66 TEST_VM(arrayOopDesc, long) { 67 ASSERT_PRED1(check_max_length_overflow, T_LONG); 68 } 69 70 TEST_VM(arrayOopDesc, object) { 71 ASSERT_PRED1(check_max_length_overflow, T_OBJECT); 72 } 73 74 TEST_VM(arrayOopDesc, array) { 75 ASSERT_PRED1(check_max_length_overflow, T_ARRAY); 76 } 77 78 TEST_VM(arrayOopDesc, narrowOop) { 79 ASSERT_PRED1(check_max_length_overflow, T_NARROWOOP); 80 } 81 // T_VOID and T_ADDRESS are not supported by max_array_length() 82 83 TEST_VM(arrayOopDesc, base_offset) { 84 #ifdef _LP64 85 if (UseCompressedClassPointers) { 86 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 16); 87 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE), 16); 88 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT), 16); 89 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR), 16); 90 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT), 16); 91 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT), 16); 92 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG), 16); 93 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE), 16); 94 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 16); 95 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY), 16); 96 } else { 97 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 20); 98 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE), 20); 99 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT), 20); 100 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR), 20); 101 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT), 20); 102 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT), 20); 103 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG), 24); 104 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE), 24); 105 if (UseCompressedOops) { 106 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 20); 107 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY), 20); 108 } else { 109 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 24); 110 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY), 24); 111 } 112 } 113 #else 114 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 12); 115 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE), 12); 116 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT), 12); 117 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR), 12); 118 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT), 12); 119 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT), 12); 120 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG), 16); 121 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE), 16); 122 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 12); 123 EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY), 12); 124 #endif 125 }