1 /*
   2  *  Copyright (c) 2019, 2020, 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  * @requires vm.bits != "32"
  27  * @run testng TestMemoryAlignment
  28  */
  29 
  30 import jdk.incubator.foreign.MemoryLayouts;
  31 import jdk.incubator.foreign.MemoryLayout;
  32 
  33 import jdk.incubator.foreign.GroupLayout;
  34 import jdk.incubator.foreign.MemoryLayout.PathElement;
  35 import jdk.incubator.foreign.MemoryAddress;
  36 import jdk.incubator.foreign.MemorySegment;
  37 import jdk.incubator.foreign.SequenceLayout;
  38 import jdk.incubator.foreign.ValueLayout;
  39 import java.lang.invoke.VarHandle;
  40 import java.util.stream.LongStream;
  41 
  42 import org.testng.annotations.*;
  43 import static org.testng.Assert.*;
  44 
  45 public class TestMemoryAlignment {
  46 
  47     @Test(dataProvider = "alignments")
  48     public void testAlignedAccess(long align) {
  49         ValueLayout layout = MemoryLayouts.BITS_32_BE;
  50         assertEquals(layout.bitAlignment(), 32);
  51         ValueLayout aligned = layout.withBitAlignment(align);
  52         assertEquals(aligned.bitAlignment(), align); //unreasonable alignment here, to make sure access throws
  53         VarHandle vh = aligned.varHandle(int.class);
  54         try (MemorySegment segment = MemorySegment.allocateNative(aligned)) {
  55             MemoryAddress addr = segment.baseAddress();
  56             vh.set(addr, -42);
  57             int val = (int)vh.get(addr);
  58             assertEquals(val, -42);
  59         }
  60     }
  61 
  62     @Test(dataProvider = "alignments")
  63     public void testUnalignedAccess(long align) {
  64         ValueLayout layout = MemoryLayouts.BITS_32_BE;
  65         assertEquals(layout.bitAlignment(), 32);
  66         ValueLayout aligned = layout.withBitAlignment(align);
  67         MemoryLayout alignedGroup = MemoryLayout.ofStruct(MemoryLayouts.PAD_8, aligned);
  68         assertEquals(alignedGroup.bitAlignment(), align);
  69         VarHandle vh = aligned.varHandle(int.class);
  70         try (MemorySegment segment = MemorySegment.allocateNative(alignedGroup)) {
  71             MemoryAddress addr = segment.baseAddress();
  72             vh.set(addr.addOffset(1L), -42);
  73             assertEquals(align, 8); //this is the only case where access is aligned
  74         } catch (IllegalStateException ex) {
  75             assertNotEquals(align, 8); //if align != 8, access is always unaligned
  76         }
  77     }
  78 
  79     @Test(dataProvider = "alignments")
  80     public void testUnalignedPath(long align) {
  81         MemoryLayout layout = MemoryLayouts.BITS_32_BE;
  82         MemoryLayout aligned = layout.withBitAlignment(align).withName("value");
  83         GroupLayout alignedGroup = MemoryLayout.ofStruct(MemoryLayouts.PAD_8, aligned);
  84         try {
  85             alignedGroup.varHandle(int.class, PathElement.groupElement("value"));
  86             assertEquals(align, 8); //this is the only case where path is aligned
  87         } catch (UnsupportedOperationException ex) {
  88             assertNotEquals(align, 8); //if align != 8, path is always unaligned
  89         }
  90     }
  91 
  92     @Test(dataProvider = "alignments")
  93     public void testUnalignedSequence(long align) {
  94         SequenceLayout layout = MemoryLayout.ofSequence(5, MemoryLayouts.BITS_32_BE.withBitAlignment(align));
  95         try {
  96             VarHandle vh = layout.varHandle(int.class, PathElement.sequenceElement());
  97             try (MemorySegment segment = MemorySegment.allocateNative(layout)) {
  98                 MemoryAddress addr = segment.baseAddress();
  99                 for (long i = 0 ; i < 5 ; i++) {
 100                     vh.set(addr, i, -42);
 101                 }
 102             }
 103         } catch (UnsupportedOperationException ex) {
 104             assertTrue(align > 32); //if align > 32, access is always unaligned (for some elements)
 105         }
 106     }
 107 
 108     @Test
 109     public void testPackedAccess() {
 110         ValueLayout vChar = MemoryLayouts.BITS_8_BE;
 111         ValueLayout vShort = MemoryLayouts.BITS_16_BE;
 112         ValueLayout vInt = MemoryLayouts.BITS_32_BE;
 113         //mimic pragma pack(1)
 114         GroupLayout g = MemoryLayout.ofStruct(vChar.withBitAlignment(8).withName("a"),
 115                                vShort.withBitAlignment(8).withName("b"),
 116                                vInt.withBitAlignment(8).withName("c"));
 117         assertEquals(g.bitAlignment(), 8);
 118         VarHandle vh_c = g.varHandle(byte.class, PathElement.groupElement("a"));
 119         VarHandle vh_s = g.varHandle(short.class, PathElement.groupElement("b"));
 120         VarHandle vh_i = g.varHandle(int.class, PathElement.groupElement("c"));
 121         try (MemorySegment segment = MemorySegment.allocateNative(g)) {
 122             MemoryAddress addr = segment.baseAddress();
 123             vh_c.set(addr, Byte.MIN_VALUE);
 124             assertEquals(vh_c.get(addr), Byte.MIN_VALUE);
 125             vh_s.set(addr, Short.MIN_VALUE);
 126             assertEquals(vh_s.get(addr), Short.MIN_VALUE);
 127             vh_i.set(addr, Integer.MIN_VALUE);
 128             assertEquals(vh_i.get(addr), Integer.MIN_VALUE);
 129         }
 130     }
 131 
 132     @DataProvider(name = "alignments")
 133     public Object[][] createAlignments() {
 134         return LongStream.range(3, 32)
 135                 .mapToObj(v -> new Object[] { 1L << v })
 136                 .toArray(Object[][]::new);
 137     }
 138 }