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