1 /*
  2  * Copyright (c) 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 ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 27  * @run testng/othervm --enable-native-access=ALL-UNNAMED StdLibTest
 28  */
 29 
 30 import java.lang.invoke.MethodHandle;
 31 import java.lang.invoke.MethodHandles;
 32 import java.lang.invoke.MethodType;
 33 import java.time.Instant;
 34 import java.time.LocalDateTime;
 35 import java.time.ZoneOffset;
 36 import java.time.ZonedDateTime;
 37 import java.util.ArrayList;
 38 import java.util.Arrays;
 39 import java.util.Collections;
 40 import java.util.LinkedHashSet;
 41 import java.util.List;
 42 import java.util.Optional;
 43 import java.util.Set;
 44 import java.util.function.BiConsumer;
 45 import java.util.function.Function;
 46 import java.util.stream.Collectors;
 47 import java.util.stream.Stream;
 48 
 49 import jdk.incubator.foreign.*;
 50 
 51 import static jdk.incubator.foreign.MemoryAccess.*;
 52 
 53 import org.testng.annotations.*;
 54 
 55 import static jdk.incubator.foreign.CLinker.*;
 56 import static org.testng.Assert.*;
 57 
 58 @Test
 59 public class StdLibTest {
 60 
 61     final static CLinker abi = CLinker.getInstance();
 62 
 63     private StdLibHelper stdLibHelper = new StdLibHelper();
 64 
 65     @Test(dataProvider = "stringPairs")
 66     void test_strcat(String s1, String s2) throws Throwable {
 67         assertEquals(stdLibHelper.strcat(s1, s2), s1 + s2);
 68     }
 69 
 70     @Test(dataProvider = "stringPairs")
 71     void test_strcmp(String s1, String s2) throws Throwable {
 72         assertEquals(Math.signum(stdLibHelper.strcmp(s1, s2)), Math.signum(s1.compareTo(s2)));
 73     }
 74 
 75     @Test(dataProvider = "strings")
 76     void test_puts(String s) throws Throwable {
 77         assertTrue(stdLibHelper.puts(s) >= 0);
 78     }
 79 
 80     @Test(dataProvider = "strings")
 81     void test_strlen(String s) throws Throwable {
 82         assertEquals(stdLibHelper.strlen(s), s.length());
 83     }
 84 
 85     @Test(dataProvider = "instants")
 86     void test_time(Instant instant) throws Throwable {
 87         StdLibHelper.Tm tm = stdLibHelper.gmtime(instant.getEpochSecond());
 88         LocalDateTime localTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
 89         assertEquals(tm.sec(), localTime.getSecond());
 90         assertEquals(tm.min(), localTime.getMinute());
 91         assertEquals(tm.hour(), localTime.getHour());
 92         //day pf year in Java has 1-offset
 93         assertEquals(tm.yday(), localTime.getDayOfYear() - 1);
 94         assertEquals(tm.mday(), localTime.getDayOfMonth());
 95         //days of week starts from Sunday in C, but on Monday in Java, also account for 1-offset
 96         assertEquals((tm.wday() + 6) % 7, localTime.getDayOfWeek().getValue() - 1);
 97         //month in Java has 1-offset
 98         assertEquals(tm.mon(), localTime.getMonth().getValue() - 1);
 99         assertEquals(tm.isdst(), ZoneOffset.UTC.getRules()
100                 .isDaylightSavings(Instant.ofEpochMilli(instant.getEpochSecond() * 1000)));
101     }
102 
103     @Test(dataProvider = "ints")
104     void test_qsort(List<Integer> ints) throws Throwable {
105         if (ints.size() > 0) {
106             int[] input = ints.stream().mapToInt(i -> i).toArray();
107             int[] sorted = stdLibHelper.qsort(input);
108             Arrays.sort(input);
109             assertEquals(sorted, input);
110         }
111     }
112 
113     @Test
114     void test_rand() throws Throwable {
115         int val = stdLibHelper.rand();
116         for (int i = 0 ; i < 100 ; i++) {
117             int newVal = stdLibHelper.rand();
118             if (newVal != val) {
119                 return; //ok
120             }
121             val = newVal;
122         }
123         fail("All values are the same! " + val);
124     }
125 
126     @Test(dataProvider = "printfArgs")
127     void test_printf(List<PrintfArg> args) throws Throwable {
128         String formatArgs = args.stream()
129                 .map(a -> a.format)
130                 .collect(Collectors.joining(","));
131 
132         String formatString = "hello(" + formatArgs + ")\n";
133 
134         String expected = String.format(formatString, args.stream()
135                 .map(a -> a.javaValue).toArray());
136 
137         int found = stdLibHelper.printf(formatString, args);
138         assertEquals(found, expected.length());
139     }
140 
141     @Test(dataProvider = "printfArgs")
142     void test_vprintf(List<PrintfArg> args) throws Throwable {
143         String formatArgs = args.stream()
144                 .map(a -> a.format)
145                 .collect(Collectors.joining(","));
146 
147         String formatString = "hello(" + formatArgs + ")\n";
148 
149         String expected = String.format(formatString, args.stream()
150                 .map(a -> a.javaValue).toArray());
151 
152         int found = stdLibHelper.vprintf(formatString, args);
153         assertEquals(found, expected.length());
154     }
155 
156     static class StdLibHelper {
157 
158         static final SymbolLookup LOOKUP = CLinker.systemLookup();
159 
160         final static MethodHandle strcat = abi.downcallHandle(LOOKUP.lookup("strcat").get(),
161                 MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemoryAddress.class),
162                 FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER));
163 
164         final static MethodHandle strcmp = abi.downcallHandle(LOOKUP.lookup("strcmp").get(),
165                 MethodType.methodType(int.class, MemoryAddress.class, MemoryAddress.class),
166                 FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER));
167 
168         final static MethodHandle puts = abi.downcallHandle(LOOKUP.lookup("puts").get(),
169                 MethodType.methodType(int.class, MemoryAddress.class),
170                 FunctionDescriptor.of(C_INT, C_POINTER));
171 
172         final static MethodHandle strlen = abi.downcallHandle(LOOKUP.lookup("strlen").get(),
173                 MethodType.methodType(int.class, MemoryAddress.class),
174                 FunctionDescriptor.of(C_INT, C_POINTER));
175 
176         final static MethodHandle gmtime = abi.downcallHandle(LOOKUP.lookup("gmtime").get(),
177                 MethodType.methodType(MemoryAddress.class, MemoryAddress.class),
178                 FunctionDescriptor.of(C_POINTER, C_POINTER));
179 
180         final static MethodHandle qsort = abi.downcallHandle(LOOKUP.lookup("qsort").get(),
181                 MethodType.methodType(void.class, MemoryAddress.class, long.class, long.class, MemoryAddress.class),
182                 FunctionDescriptor.ofVoid(C_POINTER, C_LONG_LONG, C_LONG_LONG, C_POINTER));
183 
184         final static FunctionDescriptor qsortComparFunction = FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER);
185 
186         final static MethodHandle qsortCompar;
187 
188         final static MethodHandle rand = abi.downcallHandle(LOOKUP.lookup("rand").get(),
189                 MethodType.methodType(int.class),
190                 FunctionDescriptor.of(C_INT));
191 
192         final static MethodHandle vprintf = abi.downcallHandle(LOOKUP.lookup("vprintf").get(),
193                 MethodType.methodType(int.class, MemoryAddress.class, VaList.class),
194                 FunctionDescriptor.of(C_INT, C_POINTER, C_VA_LIST));
195 
196         final static MemoryAddress printfAddr = LOOKUP.lookup("printf").get();
197 
198         final static FunctionDescriptor printfBase = FunctionDescriptor.of(C_INT, C_POINTER);
199 
200         static {
201             try {
202                 //qsort upcall handle
203                 qsortCompar = MethodHandles.lookup().findStatic(StdLibTest.StdLibHelper.class, "qsortCompare",
204                         MethodType.methodType(int.class, MemorySegment.class, MemoryAddress.class, MemoryAddress.class));
205             } catch (ReflectiveOperationException ex) {
206                 throw new IllegalStateException(ex);
207             }
208         }
209 
210         String strcat(String s1, String s2) throws Throwable {
211             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
212                 MemorySegment buf = MemorySegment.allocateNative(s1.length() + s2.length() + 1, scope);
213                 MemorySegment other = toCString(s2, scope);
214                 char[] chars = s1.toCharArray();
215                 for (long i = 0 ; i < chars.length ; i++) {
216                     setByteAtOffset(buf, i, (byte)chars[(int)i]);
217                 }
218                 setByteAtOffset(buf, chars.length, (byte)'\0');
219                 return toJavaString(((MemoryAddress)strcat.invokeExact(buf.address(), other.address())));
220             }
221         }
222 
223         int strcmp(String s1, String s2) throws Throwable {
224             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
225                 MemorySegment ns1 = toCString(s1, scope);
226                 MemorySegment ns2 = toCString(s2, scope);
227                 return (int)strcmp.invokeExact(ns1.address(), ns2.address());
228             }
229         }
230 
231         int puts(String msg) throws Throwable {
232             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
233                 MemorySegment s = toCString(msg, scope);
234                 return (int)puts.invokeExact(s.address());
235             }
236         }
237 
238         int strlen(String msg) throws Throwable {
239             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
240                 MemorySegment s = toCString(msg, scope);
241                 return (int)strlen.invokeExact(s.address());
242             }
243         }
244 
245         Tm gmtime(long arg) throws Throwable {
246             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
247                 MemorySegment time = MemorySegment.allocateNative(8, scope);
248                 setLong(time, arg);
249                 return new Tm((MemoryAddress)gmtime.invokeExact(time.address()));
250             }
251         }
252 
253         static class Tm {
254 
255             //Tm pointer should never be freed directly, as it points to shared memory
256             private final MemorySegment base;
257 
258             static final long SIZE = 56;
259 
260             Tm(MemoryAddress addr) {
261                 this.base = addr.asSegment(SIZE, ResourceScope.globalScope());
262             }
263 
264             int sec() {
265                 return getIntAtOffset(base, 0);
266             }
267             int min() {
268                 return getIntAtOffset(base, 4);
269             }
270             int hour() {
271                 return getIntAtOffset(base, 8);
272             }
273             int mday() {
274                 return getIntAtOffset(base, 12);
275             }
276             int mon() {
277                 return getIntAtOffset(base, 16);
278             }
279             int year() {
280                 return getIntAtOffset(base, 20);
281             }
282             int wday() {
283                 return getIntAtOffset(base, 24);
284             }
285             int yday() {
286                 return getIntAtOffset(base, 28);
287             }
288             boolean isdst() {
289                 byte b = getByteAtOffset(base, 32);
290                 return b != 0;
291             }
292         }
293 
294         int[] qsort(int[] arr) throws Throwable {
295             //init native array
296             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
297                 SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
298                 MemorySegment nativeArr = allocator.allocateArray(C_INT, arr);
299 
300                 //call qsort
301                 MemoryAddress qsortUpcallStub = abi.upcallStub(qsortCompar.bindTo(nativeArr), qsortComparFunction, scope);
302 
303                 qsort.invokeExact(nativeArr.address(), (long)arr.length, C_INT.byteSize(), qsortUpcallStub);
304 
305                 //convert back to Java array
306                 return nativeArr.toIntArray();
307             }
308         }
309 
310         static int qsortCompare(MemorySegment base, MemoryAddress addr1, MemoryAddress addr2) {
311             return getIntAtOffset(base, addr1.segmentOffset(base)) -
312                    getIntAtOffset(base, addr2.segmentOffset(base));
313         }
314 
315         int rand() throws Throwable {
316             return (int)rand.invokeExact();
317         }
318 
319         int printf(String format, List<PrintfArg> args) throws Throwable {
320             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
321                 MemorySegment formatStr = toCString(format, scope);
322                 return (int)specializedPrintf(args).invokeExact(formatStr.address(),
323                         args.stream().map(a -> a.nativeValue(scope)).toArray());
324             }
325         }
326 
327         int vprintf(String format, List<PrintfArg> args) throws Throwable {
328             try (ResourceScope scope = ResourceScope.newConfinedScope()) {
329                 MemorySegment formatStr = toCString(format, scope);
330                 VaList vaList = VaList.make(b -> args.forEach(a -> a.accept(b, scope)), scope);
331                 return (int)vprintf.invokeExact(formatStr.address(), vaList);
332             }
333         }
334 
335         private MethodHandle specializedPrintf(List<PrintfArg> args) {
336             //method type
337             MethodType mt = MethodType.methodType(int.class, MemoryAddress.class);
338             FunctionDescriptor fd = printfBase;
339             for (PrintfArg arg : args) {
340                 mt = mt.appendParameterTypes(arg.carrier);
341                 fd = fd.withAppendedArgumentLayouts(arg.layout);
342             }
343             MethodHandle mh = abi.downcallHandle(printfAddr, mt, fd);
344             return mh.asSpreader(1, Object[].class, args.size());
345         }
346     }
347 
348     /*** data providers ***/
349 
350     @DataProvider
351     public static Object[][] ints() {
352         return perms(0, new Integer[] { 0, 1, 2, 3, 4 }).stream()
353                 .map(l -> new Object[] { l })
354                 .toArray(Object[][]::new);
355     }
356 
357     @DataProvider
358     public static Object[][] strings() {
359         return perms(0, new String[] { "a", "b", "c" }).stream()
360                 .map(l -> new Object[] { String.join("", l) })
361                 .toArray(Object[][]::new);
362     }
363 
364     @DataProvider
365     public static Object[][] stringPairs() {
366         Object[][] strings = strings();
367         Object[][] stringPairs = new Object[strings.length * strings.length][];
368         int pos = 0;
369         for (Object[] s1 : strings) {
370             for (Object[] s2 : strings) {
371                 stringPairs[pos++] = new Object[] { s1[0], s2[0] };
372             }
373         }
374         return stringPairs;
375     }
376 
377     @DataProvider
378     public static Object[][] instants() {
379         Instant start = ZonedDateTime.of(LocalDateTime.parse("2017-01-01T00:00:00"), ZoneOffset.UTC).toInstant();
380         Instant end = ZonedDateTime.of(LocalDateTime.parse("2017-12-31T00:00:00"), ZoneOffset.UTC).toInstant();
381         Object[][] instants = new Object[100][];
382         for (int i = 0 ; i < instants.length ; i++) {
383             Instant instant = start.plusSeconds((long)(Math.random() * (end.getEpochSecond() - start.getEpochSecond())));
384             instants[i] = new Object[] { instant };
385         }
386         return instants;
387     }
388 
389     @DataProvider
390     public static Object[][] printfArgs() {
391         ArrayList<List<PrintfArg>> res = new ArrayList<>();
392         List<List<PrintfArg>> perms = new ArrayList<>(perms(0, PrintfArg.values()));
393         for (int i = 0 ; i < 100 ; i++) {
394             Collections.shuffle(perms);
395             res.addAll(perms);
396         }
397         return res.stream()
398                 .map(l -> new Object[] { l })
399                 .toArray(Object[][]::new);
400     }
401 
402     enum PrintfArg implements BiConsumer<VaList.Builder, ResourceScope> {
403 
404         INTEGRAL(int.class, asVarArg(C_INT), "%d", scope -> 42, 42, VaList.Builder::vargFromInt),
405         STRING(MemoryAddress.class, asVarArg(C_POINTER), "%s", scope -> toCString("str", scope).address(), "str", VaList.Builder::vargFromAddress),
406         CHAR(byte.class, asVarArg(C_CHAR), "%c", scope -> (byte) 'h', 'h', (builder, layout, value) -> builder.vargFromInt(C_INT, (int)value)),
407         DOUBLE(double.class, asVarArg(C_DOUBLE), "%.4f", scope ->1.2345d, 1.2345d, VaList.Builder::vargFromDouble);
408 
409         final Class<?> carrier;
410         final ValueLayout layout;
411         final String format;
412         final Function<ResourceScope, ?> nativeValueFactory;
413         final Object javaValue;
414         @SuppressWarnings("rawtypes")
415         final VaListBuilderCall builderCall;
416 
417         <Z> PrintfArg(Class<?> carrier, ValueLayout layout, String format, Function<ResourceScope, Z> nativeValueFactory, Object javaValue, VaListBuilderCall<Z> builderCall) {
418             this.carrier = carrier;
419             this.layout = layout;
420             this.format = format;
421             this.nativeValueFactory = nativeValueFactory;
422             this.javaValue = javaValue;
423             this.builderCall = builderCall;
424         }
425 
426         @Override
427         @SuppressWarnings("unchecked")
428         public void accept(VaList.Builder builder, ResourceScope scope) {
429             builderCall.build(builder, layout, nativeValueFactory.apply(scope));
430         }
431 
432         interface VaListBuilderCall<V> {
433             void build(VaList.Builder builder, ValueLayout layout, V value);
434         }
435 
436         public Object nativeValue(ResourceScope scope) {
437             return nativeValueFactory.apply(scope);
438         }
439     }
440 
441     static <Z> Set<List<Z>> perms(int count, Z[] arr) {
442         if (count == arr.length) {
443             return Set.of(List.of());
444         } else {
445             return Arrays.stream(arr)
446                     .flatMap(num -> {
447                         Set<List<Z>> perms = perms(count + 1, arr);
448                         return Stream.concat(
449                                 //take n
450                                 perms.stream().map(l -> {
451                                     List<Z> li = new ArrayList<>(l);
452                                     li.add(num);
453                                     return li;
454                                 }),
455                                 //drop n
456                                 perms.stream());
457                     }).collect(Collectors.toCollection(LinkedHashSet::new));
458         }
459     }
460 }