1 /*
  2  * Copyright (c) 2021, 2023, 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  * @test
 26  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestHeapAlignment
 27  */
 28 
 29 import java.lang.foreign.AddressLayout;
 30 import java.lang.foreign.MemoryLayout;
 31 import java.lang.foreign.MemorySegment;
 32 import java.lang.foreign.Arena;
 33 import java.lang.foreign.ValueLayout;
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import java.util.function.Function;
 37 import org.testng.annotations.DataProvider;
 38 import org.testng.annotations.Test;
 39 
 40 import static org.testng.Assert.fail;
 41 
 42 public class TestHeapAlignment {
 43 
 44     @Test(dataProvider = "layouts")
 45     public void testHeapAlignment(MemorySegment segment, int align, Object val, Object arr, ValueLayout layout, Function<Object, MemorySegment> segmentFactory) {
 46         assertAligned(align, layout, () -> layout.varHandle().get(segment, 0L));
 47         assertAligned(align, layout, () -> layout.varHandle().set(segment, 0L, val));
 48         MemoryLayout seq = MemoryLayout.sequenceLayout(10, layout);
 49         assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).get(segment, 0L, 0L));
 50         assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).set(segment, 0L, 0L, val));
 51         assertAligned(align, layout, () -> segment.spliterator(layout));
 52         if (arr != null) {
 53             assertAligned(align, layout, () -> MemorySegment.copy(arr, 0, segment, layout, 0, 1));
 54             assertAligned(align, layout, () -> MemorySegment.copy(segment, layout, 0, arr, 0, 1));
 55             assertAligned(align, layout, () -> {
 56                 MemorySegment other = segmentFactory.apply(arr);
 57                 MemorySegment.copy(other, layout, 0, segment, layout, 0, 1);
 58             });
 59             MemorySegment other = segmentFactory.apply(arr);
 60             assertAligned(align, layout, () -> {
 61                 MemorySegment.copy(segment, layout, 0, other, layout, 0, 1);
 62             });
 63             assertAligned(align, layout, () -> {
 64                 MemorySegment.copy(other, layout, 0, segment, layout, 0, 1);
 65             });
 66         }
 67     }
 68 
 69     static void assertAligned(int align, ValueLayout layout, Runnable runnable) {
 70         boolean shouldFail = layout.byteAlignment() > align && align != -1;
 71         try {
 72             runnable.run();
 73             if (shouldFail) {
 74                 fail("Should not get here!");
 75             }
 76         } catch (IllegalArgumentException ex) {
 77             if (!shouldFail) {
 78                 fail("Should not get here!");
 79             } else if (!ex.getMessage().contains("alignment") && !ex.getMessage().contains("Misaligned")) {
 80                 fail("Unexpected exception: " + ex);
 81             }
 82         }
 83     }
 84 
 85     enum SegmentAndAlignment {
 86         HEAP_BYTE(MemorySegment.ofArray(new byte[8]), 1),
 87         HEAP_SHORT(MemorySegment.ofArray(new short[4]), 2),
 88         HEAP_CHAR(MemorySegment.ofArray(new char[4]), 2),
 89         HEAP_INT(MemorySegment.ofArray(new int[2]), 4),
 90         HEAP_FLOAT(MemorySegment.ofArray(new float[2]), 4),
 91         HEAP_LONG(MemorySegment.ofArray(new long[1]), 8),
 92         HEAP_DOUBLE(MemorySegment.ofArray(new double[1]), 8),
 93         NATIVE(Arena.ofAuto().allocate(8, 1), -1);
 94 
 95         final MemorySegment segment;
 96         final int align;
 97 
 98         SegmentAndAlignment(MemorySegment segment, int align) {
 99             this.segment = segment;
100             this.align = align;
101         }
102     }
103 
104     @DataProvider
105     public static Object[][] layouts() {
106         List<Object[]> layouts = new ArrayList<>();
107         for (SegmentAndAlignment testCase : SegmentAndAlignment.values()) {
108             layouts.add(new Object[] { testCase.segment, testCase.align, (byte) 42, new byte[]{42}, ValueLayout.JAVA_BYTE, (Function<byte[], MemorySegment>)MemorySegment::ofArray });
109             layouts.add(new Object[] { testCase.segment, testCase.align, true, null, ValueLayout.JAVA_BOOLEAN, null });
110             layouts.add(new Object[] { testCase.segment, testCase.align, (char) 42, new char[]{42}, ValueLayout.JAVA_CHAR, (Function<char[], MemorySegment>)MemorySegment::ofArray });
111             layouts.add(new Object[] { testCase.segment, testCase.align, (short) 42, new short[]{42}, ValueLayout.JAVA_SHORT, (Function<short[], MemorySegment>)MemorySegment::ofArray });
112             layouts.add(new Object[] { testCase.segment, testCase.align, 42, new int[]{42}, ValueLayout.JAVA_INT, (Function<int[], MemorySegment>)MemorySegment::ofArray });
113             layouts.add(new Object[] { testCase.segment, testCase.align, 42f, new float[]{42}, ValueLayout.JAVA_FLOAT, (Function<float[], MemorySegment>)MemorySegment::ofArray });
114             layouts.add(new Object[] { testCase.segment, testCase.align, 42L, new long[]{42}, ValueLayout.JAVA_LONG, (Function<long[], MemorySegment>)MemorySegment::ofArray });
115             layouts.add(new Object[] { testCase.segment, testCase.align, 42d, new double[]{42}, ValueLayout.JAVA_DOUBLE, (Function<double[], MemorySegment>)MemorySegment::ofArray });
116             layouts.add(new Object[] { testCase.segment, testCase.align, MemorySegment.ofAddress(42), null, ValueLayout.ADDRESS, null });
117         }
118         return layouts.toArray(new Object[0][]);
119     }
120 }