1 /*
  2  *  Copyright (c) 2021, 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 ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 27  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestScopedOperations
 28  */
 29 
 30 import jdk.incubator.foreign.CLinker;
 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemoryLayout;
 33 import jdk.incubator.foreign.MemoryLayouts;
 34 import jdk.incubator.foreign.MemorySegment;
 35 import jdk.incubator.foreign.ResourceScope;
 36 import jdk.incubator.foreign.SegmentAllocator;
 37 import org.testng.annotations.DataProvider;
 38 import org.testng.annotations.Test;
 39 
 40 import java.io.File;
 41 import java.io.IOException;
 42 import java.nio.channels.FileChannel;
 43 import java.nio.file.Path;
 44 import java.util.ArrayList;
 45 import java.util.List;
 46 import java.util.concurrent.atomic.AtomicReference;
 47 import java.util.function.Consumer;
 48 import java.util.function.Function;
 49 
 50 import static org.testng.Assert.assertEquals;
 51 import static org.testng.Assert.assertNotNull;
 52 import static org.testng.Assert.assertTrue;
 53 import static org.testng.Assert.fail;
 54 
 55 public class TestScopedOperations {
 56 
 57     static Path tempPath;
 58 
 59     static {
 60         try {
 61             File file = File.createTempFile("scopedBuffer", "txt");
 62             file.deleteOnExit();
 63             tempPath = file.toPath();
 64         } catch (IOException ex) {
 65             throw new ExceptionInInitializerError(ex);
 66         }
 67     }
 68 
 69     @Test(dataProvider = "scopedOperations")
 70     public void testOpAfterClose(String name, ScopedOperation scopedOperation) {
 71         ResourceScope scope = ResourceScope.newConfinedScope();
 72         scope.close();
 73         try {
 74             scopedOperation.accept(scope);
 75             fail();
 76         } catch (IllegalStateException ex) {
 77             assertTrue(ex.getMessage().contains("closed"));
 78         }
 79     }
 80 
 81     @Test(dataProvider = "scopedOperations")
 82     public void testOpOutsideConfinement(String name, ScopedOperation scopedOperation) {
 83         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 84             AtomicReference<Throwable> failed = new AtomicReference<>();
 85             Thread t = new Thread(() -> {
 86                 try {
 87                     scopedOperation.accept(scope);
 88                 } catch (Throwable ex) {
 89                     failed.set(ex);
 90                 }
 91             });
 92             t.start();
 93             t.join();
 94             assertNotNull(failed.get());
 95             assertEquals(failed.get().getClass(), IllegalStateException.class);
 96             assertTrue(failed.get().getMessage().contains("outside"));
 97         } catch (InterruptedException ex) {
 98             throw new AssertionError(ex);
 99         }
100     }
101 
102     static List<ScopedOperation> scopedOperations = new ArrayList<>();
103 
104     static {
105         // scope operations
106         ScopedOperation.ofScope(scope -> scope.addCloseAction(() -> {
107         }), "ResourceScope::addOnClose");
108         ScopedOperation.ofScope(scope -> {
109             ResourceScope.Handle handle = scope.acquire();
110             scope.release(handle);
111         }, "ResourceScope::lock");
112         ScopedOperation.ofScope(scope -> MemorySegment.allocateNative(100, scope), "MemorySegment::allocateNative");
113         ScopedOperation.ofScope(scope -> {
114             try {
115                 MemorySegment.mapFile(tempPath, 0, 10, FileChannel.MapMode.READ_WRITE, scope);
116             } catch (IOException ex) {
117                 fail();
118             }
119         }, "MemorySegment::mapFromFile");
120         ScopedOperation.ofScope(scope -> CLinker.VaList.make(b -> {}, scope), "VaList::make");
121         ScopedOperation.ofScope(scope -> CLinker.VaList.ofAddress(MemoryAddress.ofLong(42), scope), "VaList::make");
122         ScopedOperation.ofScope(scope -> CLinker.toCString("Hello", scope), "CLinker::toCString");
123         ScopedOperation.ofScope(SegmentAllocator::arenaAllocator, "SegmentAllocator::arenaAllocator");
124         // segment operations
125         ScopedOperation.ofSegment(MemorySegment::toByteArray, "MemorySegment::toByteArray");
126         ScopedOperation.ofSegment(MemorySegment::toCharArray, "MemorySegment::toCharArray");
127         ScopedOperation.ofSegment(MemorySegment::toShortArray, "MemorySegment::toShortArray");
128         ScopedOperation.ofSegment(MemorySegment::toIntArray, "MemorySegment::toIntArray");
129         ScopedOperation.ofSegment(MemorySegment::toFloatArray, "MemorySegment::toFloatArray");
130         ScopedOperation.ofSegment(MemorySegment::toLongArray, "MemorySegment::toLongArray");
131         ScopedOperation.ofSegment(MemorySegment::toDoubleArray, "MemorySegment::toDoubleArray");
132         ScopedOperation.ofSegment(MemorySegment::address, "MemorySegment::address");
133         ScopedOperation.ofSegment(s -> MemoryLayout.sequenceLayout(s.byteSize(), MemoryLayouts.JAVA_BYTE), "MemorySegment::spliterator");
134         ScopedOperation.ofSegment(s -> s.copyFrom(s), "MemorySegment::copyFrom");
135         ScopedOperation.ofSegment(s -> s.mismatch(s), "MemorySegment::mismatch");
136         ScopedOperation.ofSegment(s -> s.fill((byte) 0), "MemorySegment::fill");
137         // address operations
138         ScopedOperation.ofAddress(a -> a.toRawLongValue(), "MemoryAddress::toRawLongValue");
139         ScopedOperation.ofAddress(a -> a.asSegment(100, ResourceScope.globalScope()), "MemoryAddress::asSegment");
140         // valist operations
141         ScopedOperation.ofVaList(CLinker.VaList::address, "VaList::address");
142         ScopedOperation.ofVaList(CLinker.VaList::copy, "VaList::copy");
143         ScopedOperation.ofVaList(list -> list.vargAsAddress(MemoryLayouts.ADDRESS), "VaList::vargAsAddress");
144         ScopedOperation.ofVaList(list -> list.vargAsInt(MemoryLayouts.JAVA_INT), "VaList::vargAsInt");
145         ScopedOperation.ofVaList(list -> list.vargAsLong(MemoryLayouts.JAVA_LONG), "VaList::vargAsLong");
146         ScopedOperation.ofVaList(list -> list.vargAsDouble(MemoryLayouts.JAVA_DOUBLE), "VaList::vargAsDouble");
147         ScopedOperation.ofVaList(CLinker.VaList::skip, "VaList::skip");
148         ScopedOperation.ofVaList(list -> list.vargAsSegment(MemoryLayout.structLayout(MemoryLayouts.JAVA_INT), ResourceScope.newImplicitScope()), "VaList::vargAsSegment/1");
149         // allocator operations
150         ScopedOperation.ofAllocator(a -> a.allocate(1), "NativeAllocator::allocate/size");
151         ScopedOperation.ofAllocator(a -> a.allocate(1, 1), "NativeAllocator::allocate/size/align");
152         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_BYTE), "NativeAllocator::allocate/layout");
153         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_BYTE, (byte) 0), "NativeAllocator::allocate/byte");
154         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_CHAR, (char) 0), "NativeAllocator::allocate/char");
155         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_SHORT, (short) 0), "NativeAllocator::allocate/short");
156         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_INT, 0), "NativeAllocator::allocate/int");
157         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_FLOAT, 0f), "NativeAllocator::allocate/float");
158         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_LONG, 0L), "NativeAllocator::allocate/long");
159         ScopedOperation.ofAllocator(a -> a.allocate(MemoryLayouts.JAVA_DOUBLE, 0d), "NativeAllocator::allocate/double");
160         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_BYTE, 1L), "NativeAllocator::allocateArray/size");
161         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_BYTE, new byte[]{0}), "NativeAllocator::allocateArray/byte");
162         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_CHAR, new char[]{0}), "NativeAllocator::allocateArray/char");
163         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_SHORT, new short[]{0}), "NativeAllocator::allocateArray/short");
164         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_INT, new int[]{0}), "NativeAllocator::allocateArray/int");
165         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_FLOAT, new float[]{0}), "NativeAllocator::allocateArray/float");
166         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_LONG, new long[]{0}), "NativeAllocator::allocateArray/long");
167         ScopedOperation.ofAllocator(a -> a.allocateArray(MemoryLayouts.JAVA_DOUBLE, new double[]{0}), "NativeAllocator::allocateArray/double");
168     };
169 
170     @DataProvider(name = "scopedOperations")
171     static Object[][] scopedOperations() {
172         return scopedOperations.stream().map(op -> new Object[] { op.name, op }).toArray(Object[][]::new);
173     }
174 
175     static class ScopedOperation implements Consumer<ResourceScope> {
176 
177         final Consumer<ResourceScope> scopeConsumer;
178         final String name;
179 
180         private ScopedOperation(Consumer<ResourceScope> scopeConsumer, String name) {
181             this.scopeConsumer = scopeConsumer;
182             this.name = name;
183         }
184 
185         @Override
186         public void accept(ResourceScope scope) {
187             scopeConsumer.accept(scope);
188         }
189 
190         static void ofScope(Consumer<ResourceScope> scopeConsumer, String name) {
191             scopedOperations.add(new ScopedOperation(scopeConsumer::accept, name));
192         }
193 
194         static void ofVaList(Consumer<CLinker.VaList> vaListConsumer, String name) {
195             scopedOperations.add(new ScopedOperation(scope -> {
196                 CLinker.VaList vaList = CLinker.VaList.make((builder) -> {}, scope);
197                 vaListConsumer.accept(vaList);
198             }, name));
199         }
200 
201         static void ofSegment(Consumer<MemorySegment> segmentConsumer, String name) {
202             for (SegmentFactory segmentFactory : SegmentFactory.values()) {
203                 scopedOperations.add(new ScopedOperation(scope -> {
204                     MemorySegment segment = segmentFactory.segmentFactory.apply(scope);
205                     segmentConsumer.accept(segment);
206                 }, segmentFactory.name() + "/" + name));
207             }
208         }
209 
210         static void ofAddress(Consumer<MemoryAddress> addressConsumer, String name) {
211             for (SegmentFactory segmentFactory : SegmentFactory.values()) {
212                 scopedOperations.add(new ScopedOperation(scope -> {
213                     MemoryAddress segment = segmentFactory.segmentFactory.apply(scope).address();
214                     addressConsumer.accept(segment);
215                 }, segmentFactory.name() + "/" + name));
216             }
217         }
218 
219         static void ofAllocator(Consumer<SegmentAllocator> allocatorConsumer, String name) {
220             for (AllocatorFactory allocatorFactory : AllocatorFactory.values()) {
221                 scopedOperations.add(new ScopedOperation(scope -> {
222                     SegmentAllocator allocator = allocatorFactory.allocatorFactory.apply(scope);
223                     allocatorConsumer.accept(allocator);
224                 }, allocatorFactory.name() + "/" + name));
225             }
226         }
227 
228         enum SegmentFactory {
229 
230             NATIVE(scope -> MemorySegment.allocateNative(10, scope)),
231             MAPPED(scope -> {
232                 try {
233                     return MemorySegment.mapFile(Path.of("foo.txt"), 0, 10, FileChannel.MapMode.READ_WRITE, scope);
234                 } catch (IOException ex) {
235                     throw new AssertionError(ex);
236                 }
237             }),
238             UNSAFE(scope -> MemoryAddress.NULL.asSegment(10, scope));
239 
240             static {
241                 try {
242                     File f = new File("foo.txt");
243                     f.createNewFile();
244                     f.deleteOnExit();
245                 } catch (IOException ex) {
246                     throw new ExceptionInInitializerError(ex);
247                 }
248             }
249 
250             final Function<ResourceScope, MemorySegment> segmentFactory;
251 
252             SegmentFactory(Function<ResourceScope, MemorySegment> segmentFactory) {
253                 this.segmentFactory = segmentFactory;
254             }
255         }
256 
257         enum AllocatorFactory {
258             ARENA_BOUNDED(scope -> SegmentAllocator.arenaAllocator(1000, scope)),
259             ARENA_UNBOUNDED(SegmentAllocator::arenaAllocator),
260             FROM_SEGMENT(scope -> {
261                 MemorySegment segment = MemorySegment.allocateNative(10, scope);
262                 return SegmentAllocator.ofSegment(segment);
263             }),
264             FROM_SCOPE(SegmentAllocator::ofScope);
265 
266             final Function<ResourceScope, SegmentAllocator> allocatorFactory;
267 
268             AllocatorFactory(Function<ResourceScope, SegmentAllocator> allocatorFactory) {
269                 this.allocatorFactory = allocatorFactory;
270             }
271         }
272     }
273 }