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 (UseCompactObjectHeaders) {
 86     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 12);
 87     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE),    12);
 88     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT),   12);
 89     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR),    12);
 90     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT),     12);
 91     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT),   12);
 92     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG),    16);
 93     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE),  16);
 94     if (UseCompressedOops) {
 95       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 12);
 96       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY),  12);
 97     } else {
 98       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 16);
 99       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY),  16);
100     }
101   } else if (UseCompressedClassPointers) {
102     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 16);
103     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE),    16);
104     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT),   16);
105     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR),    16);
106     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT),     16);
107     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT),   16);
108     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG),    16);
109     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE),  16);
110     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT),  16);
111     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY),   16);
112   } else {
113     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 20);
114     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE),    20);
115     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT),   20);
116     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR),    20);
117     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT),     20);
118     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT),   20);
119     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG),    24);
120     EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE),  24);
121     if (UseCompressedOops) {
122       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 20);
123       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY),  20);
124     } else {
125       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT), 24);
126       EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY),  24);
127     }
128   }
129 #else
130   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BOOLEAN), 12);
131   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_BYTE),    12);
132   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_SHORT),   12);
133   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_CHAR),    12);
134   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_INT),     12);
135   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_FLOAT),   12);
136   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_LONG),    16);
137   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_DOUBLE),  16);
138   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_OBJECT),  12);
139   EXPECT_EQ(arrayOopDesc::base_offset_in_bytes(T_ARRAY),   12);
140 #endif
141 }