1 /*
2 * Copyright (c) 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 "oops/flatArrayOop.hpp"
25 #include "unittest.hpp"
26 #include "utilities/globalDefinitions.hpp"
27
28 // INTENTIONALLY SMALL BACKING, SHOULD ONLY CONTAIN METADATA + A FEW ELEMENTS.
29 static unsigned char memory[1024];
30
31 // Do not perform operations on the array's memory without ensuring that the
32 // backing is large enough and you will not go out of bounds.
33 static flatArrayOop fake_flat_array(int length) {
34 flatArrayOop farr = flatArrayOop(cast_to_oop(memory));
35 // We can't ensure the backing for the length, but we can still do pointer
36 // arithmetic and e.g. ensure that the resulting pointers didn't overflow.
37 farr->set_length(length);
38 return farr;
39 }
40
41 // What FlatArrayKlass::array_layout_helper does, but w/o InlineKlass
42 static int make_lh(int payload_size_bytes, bool null_free) {
43 BasicType etype = T_FLAT_ELEMENT;
44 int esize = log2i_exact(round_up_power_of_2(payload_size_bytes));
45 int hsize = arrayOopDesc::base_offset_in_bytes(etype);
46 return Klass::array_layout_helper(Klass::_lh_array_tag_flat_value, null_free, hsize, etype, esize);
47 }
48
49 static void ensure_no_overflow(flatArrayOop farr, int lh) {
50 void* vaa_small = farr->value_at_addr(123, lh);
51 EXPECT_TRUE(vaa_small >= farr);
52 void* vaa_large = farr->value_at_addr(321999888, lh);
53 EXPECT_TRUE(vaa_large >= farr);
54 }
55
56 TEST_VM(flatArrayOopDesc, value_at_addr_intbox_nullable) {
57 flatArrayOop farr = fake_flat_array(500000000);
58 ensure_no_overflow(farr, make_lh(8, false));
59 }
60
61
62 TEST_VM(flatArrayOopDesc, value_at_addr_intbox_null_free) {
63 flatArrayOop farr = fake_flat_array(500000000);
64 ensure_no_overflow(farr, make_lh(4, true));
65 }