1 /*
   2  * Copyright (c) 1997, 2011, 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 
  25 #include "precompiled.hpp"
  26 #include "oops/arrayOop.hpp"
  27 
  28 #ifndef PRODUCT
  29 
  30 #include "utilities/globalDefinitions.hpp"
  31 // Unit tests
  32 
  33 bool arrayOopDesc::check_overflow(BasicType type) {
  34   int32_t length = max_array_length(type);
  35   size_t bytes_per_element = type2aelembytes(type);
  36   size_t bytes = (size_t)length * bytes_per_element + header_size_in_bytes();
  37   return (bytes - header_size_in_bytes()) / bytes_per_element == (size_t)length;
  38 }
  39 
  40 // The old implementation of max_array_length. For 64-bit platforms the old
  41 // and new implementations should return the same value.
  42 int32_t arrayOopDesc::old_max_array_length(BasicType type) {
  43    const int bytes_per_element = type2aelembytes(type);
  44     if (bytes_per_element < HeapWordSize) {
  45       return max_jint;
  46     }
  47     const int32_t max_words = align_size_down(max_jint, MinObjAlignment);
  48     const int32_t max_element_words = max_words - header_size(type);
  49     const int32_t words_per_element = bytes_per_element >> LogHeapWordSize;
  50     return max_element_words / words_per_element;
  51 }
  52 
  53 bool arrayOopDesc::test_max_array_length() {
  54   tty->print_cr("test_max_array_length");
  55   
  56   assert(check_overflow(T_BOOLEAN), "size_t overflow for boolean array");
  57   assert(check_overflow(T_CHAR), "size_t overflow for char array");
  58   assert(check_overflow(T_FLOAT), "size_t overflow for float array");
  59   assert(check_overflow(T_DOUBLE), "size_t overflow for double array");
  60   assert(check_overflow(T_BYTE), "size_t overflow for byte array");
  61   assert(check_overflow(T_SHORT), "size_t overflow for short array");
  62   assert(check_overflow(T_INT), "size_t overflow for int array");
  63   assert(check_overflow(T_LONG), "size_t overflow for long array");
  64   assert(check_overflow(T_OBJECT), "size_t overflow for object array");
  65   assert(check_overflow(T_ARRAY), "size_t overflow for array array");
  66   assert(check_overflow(T_NARROWOOP), "size_t overflow for narrowOop array");
  67 
  68   // T_VOID and T_ADDRESS are not supported by max_array_length()
  69 
  70 #ifdef _LP64
  71   assert(max_array_length(T_BOOLEAN) == old_max_array_length(T_BOOLEAN), "calculation changed for boolean");
  72   assert(max_array_length(T_CHAR) == old_max_array_length(T_CHAR), "calculation changed for char");
  73   assert(max_array_length(T_FLOAT) == old_max_array_length(T_FLOAT), "calculation changed for float");
  74   assert(max_array_length(T_DOUBLE) == old_max_array_length(T_DOUBLE), "calculation changed for double");
  75   assert(max_array_length(T_BYTE) == old_max_array_length(T_BYTE), "calculation changed for byte");
  76   assert(max_array_length(T_SHORT) == old_max_array_length(T_SHORT), "calculation changed for short");
  77   assert(max_array_length(T_INT) == old_max_array_length(T_INT), "calculation changed for int");
  78   assert(max_array_length(T_LONG) == old_max_array_length(T_LONG), "calculation changed for long");
  79   assert(max_array_length(T_OBJECT) == old_max_array_length(T_OBJECT), "calculation changed for object");
  80   assert(max_array_length(T_ARRAY) == old_max_array_length(T_ARRAY), "calculation changed for array");
  81   assert(max_array_length(T_NARROWOOP) == old_max_array_length(T_NARROWOOP), "calculation changed for narrowOop");
  82 #endif
  83 
  84   return true;
  85 }
  86 
  87 
  88 #endif //PRODUCT