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 TestSegments
  27  */
  28 
  29 import jdk.incubator.foreign.MemoryLayout;
  30 import jdk.incubator.foreign.MemoryLayouts;
  31 import jdk.incubator.foreign.MemorySegment;
  32 
  33 import java.awt.font.LayoutPath;
  34 import java.lang.invoke.VarHandle;
  35 import java.lang.reflect.Method;
  36 import java.lang.reflect.Modifier;
  37 import java.nio.ByteOrder;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 import java.util.concurrent.atomic.AtomicBoolean;
  41 import java.util.function.LongFunction;
  42 import java.util.stream.Stream;
  43 
  44 import jdk.incubator.foreign.SequenceLayout;
  45 import org.testng.annotations.*;
  46 
  47 import static org.testng.Assert.*;
  48 
  49 public class TestSegments {
  50 
  51     @Test(dataProvider = "badSizeAndAlignments", expectedExceptions = IllegalArgumentException.class)
  52     public void testBadAllocateAlign(long size, long align) {
  53         MemorySegment.allocateNative(size, align);
  54     }
  55 
  56     @Test(dataProvider = "badLayouts", expectedExceptions = UnsupportedOperationException.class)
  57     public void testBadAllocateLayout(MemoryLayout layout) {
  58         MemorySegment.allocateNative(layout);
  59     }
  60 
  61     @Test(expectedExceptions = OutOfMemoryError.class)
  62     public void testAllocateTooBig() {
  63         MemorySegment.allocateNative(Long.MAX_VALUE);
  64     }
  65 
  66     @Test(dataProvider = "segmentOperations")
  67     public void testOpOutsideConfinement(SegmentMember member) throws Throwable {
  68         try (MemorySegment segment = MemorySegment.allocateNative(4)) {
  69             AtomicBoolean failed = new AtomicBoolean(false);
  70             Thread t = new Thread(() -> {
  71                 try {
  72                     Object o = member.method.invoke(segment, member.params);
  73                     if (member.method.getName().equals("acquire")) {
  74                         ((MemorySegment)o).close();
  75                     }
  76                 } catch (ReflectiveOperationException ex) {
  77                     throw new IllegalStateException(ex);
  78                 }
  79             });
  80             t.setUncaughtExceptionHandler((thread, ex) -> failed.set(true));
  81             t.start();
  82             t.join();
  83             assertEquals(failed.get(), member.isConfined());
  84         }
  85     }
  86 
  87     @Test
  88     public void testNativeSegmentIsZeroed() {
  89         VarHandle byteHandle = MemoryLayout.ofSequence(MemoryLayouts.JAVA_BYTE)
  90                 .varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
  91         try (MemorySegment segment = MemorySegment.allocateNative(1000)) {
  92             for (long i = 0 ; i < segment.byteSize() ; i++) {
  93                 assertEquals(0, (byte)byteHandle.get(segment.baseAddress(), i));
  94             }
  95         }
  96     }
  97 
  98     @DataProvider(name = "badSizeAndAlignments")
  99     public Object[][] sizesAndAlignments() {
 100         return new Object[][] {
 101                 { -1, 8 },
 102                 { 1, 15 },
 103                 { 1, -15 }
 104         };
 105     }
 106 
 107     @DataProvider(name = "badLayouts")
 108     public Object[][] layouts() {
 109         SizedLayoutFactory[] layoutFactories = SizedLayoutFactory.values();
 110         Object[][] values = new Object[layoutFactories.length * 2][2];
 111         for (int i = 0; i < layoutFactories.length ; i++) {
 112             values[i * 2] = new Object[] { MemoryLayout.ofStruct(layoutFactories[i].make(7), MemoryLayout.ofPaddingBits(9)) }; // good size, bad align
 113             values[(i * 2) + 1] = new Object[] { layoutFactories[i].make(15).withBitAlignment(16) }; // bad size, good align
 114         }
 115         return values;
 116     }
 117 
 118     enum SizedLayoutFactory {
 119         VALUE_BE(size -> MemoryLayout.ofValueBits(size, ByteOrder.BIG_ENDIAN)),
 120         VALUE_LE(size -> MemoryLayout.ofValueBits(size, ByteOrder.LITTLE_ENDIAN)),
 121         PADDING(MemoryLayout::ofPaddingBits);
 122 
 123         private final LongFunction<MemoryLayout> factory;
 124 
 125         SizedLayoutFactory(LongFunction<MemoryLayout> factory) {
 126             this.factory = factory;
 127         }
 128 
 129         MemoryLayout make(long size) {
 130             return factory.apply(size);
 131         }
 132     }
 133 
 134     @DataProvider(name = "segmentOperations")
 135     static Object[][] segmentMembers() {
 136         List<SegmentMember> members = new ArrayList<>();
 137         for (Method m : MemorySegment.class.getDeclaredMethods()) {
 138             //skip statics and method declared in j.l.Object
 139             if (m.getDeclaringClass().equals(Object.class) ||
 140                     (m.getModifiers() & Modifier.STATIC) != 0) continue;
 141             Object[] args = Stream.of(m.getParameterTypes())
 142                     .map(TestSegments::defaultValue)
 143                     .toArray();
 144             members.add(new SegmentMember(m, args));
 145         }
 146         return members.stream().map(ms -> new Object[] { ms }).toArray(Object[][]::new);
 147     }
 148 
 149     static class SegmentMember {
 150         final Method method;
 151         final Object[] params;
 152 
 153         public SegmentMember(Method method, Object[] params) {
 154             this.method = method;
 155             this.params = params;
 156         }
 157 
 158         boolean isConfined() {
 159             return method.getName().startsWith("as") ||
 160                     method.getName().startsWith("to") ||
 161                     method.getName().equals("close") ||
 162                     method.getName().equals("slice");
 163         }
 164 
 165         @Override
 166         public String toString() {
 167             return method.getName();
 168         }
 169     }
 170 
 171     static Object defaultValue(Class<?> c) {
 172         if (c.isPrimitive()) {
 173             if (c == char.class) {
 174                 return (char)0;
 175             } else if (c == boolean.class) {
 176                 return false;
 177             } else if (c == byte.class) {
 178                 return (byte)0;
 179             } else if (c == short.class) {
 180                 return (short)0;
 181             } else if (c == int.class) {
 182                 return 0;
 183             } else if (c == long.class) {
 184                 return 0L;
 185             } else if (c == float.class) {
 186                 return 0f;
 187             } else if (c == double.class) {
 188                 return 0d;
 189             } else {
 190                 throw new IllegalStateException();
 191             }
 192         } else {
 193             return null;
 194         }
 195     }
 196 }