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