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