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  * @library /test/lib
 28  * @modules jdk.incubator.foreign/jdk.internal.foreign
 29  * @build ThrowingUpcall TestUpcallException
 30  *
 31  * @run testng/othervm/native
 32  *   --enable-native-access=ALL-UNNAMED
 33  *   TestUpcallException
 34  */
 35 
 36 import jdk.incubator.foreign.CLinker;
 37 import jdk.incubator.foreign.FunctionDescriptor;
 38 import jdk.incubator.foreign.ResourceScope;
 39 import jdk.test.lib.Utils;
 40 import org.testng.annotations.Test;
 41 
 42 import java.io.BufferedReader;
 43 import java.io.IOException;
 44 import java.io.InputStream;
 45 import java.io.InputStreamReader;
 46 import java.nio.file.Paths;
 47 import java.util.List;
 48 
 49 import static org.testng.Assert.assertFalse;
 50 import static org.testng.Assert.assertNotEquals;
 51 import static org.testng.Assert.assertTrue;
 52 
 53 public class TestUpcallException {
 54 
 55     @Test
 56     public void testExceptionInterpreted() throws InterruptedException, IOException {
 57         boolean useSpec = false;
 58         run(useSpec);
 59     }
 60 
 61     @Test
 62     public void testExceptionSpecialized() throws IOException, InterruptedException {
 63         boolean useSpec = true;
 64         run(useSpec);
 65     }
 66 
 67     private void run(boolean useSpec) throws IOException, InterruptedException {
 68         Process process = new ProcessBuilder()
 69             .command(
 70                 Paths.get(Utils.TEST_JDK)
 71                      .resolve("bin")
 72                      .resolve("java")
 73                      .toAbsolutePath()
 74                      .toString(),
 75                 "--add-modules", "jdk.incubator.foreign",
 76                 "--enable-native-access=ALL-UNNAMED",
 77                 "-Djava.library.path=" + System.getProperty("java.library.path"),
 78                 "-Djdk.internal.foreign.ProgrammableUpcallHandler.USE_SPEC=" + useSpec,
 79                 "-cp", Utils.TEST_CLASS_PATH,
 80                 "ThrowingUpcall")
 81             .start();
 82 
 83         int result = process.waitFor();
 84         assertNotEquals(result, 0);
 85 
 86         List<String> outLines = linesFromStream(process.getInputStream());
 87         outLines.forEach(System.out::println);
 88         List<String> errLines = linesFromStream(process.getErrorStream());
 89         errLines.forEach(System.err::println);
 90 
 91         // Exception message would be found in stack trace
 92         String shouldInclude = "Testing upcall exceptions";
 93         assertTrue(linesContain(errLines, shouldInclude), "Did not find '" + shouldInclude + "' in stderr");
 94     }
 95 
 96     private boolean linesContain(List<String> errLines, String shouldInclude) {
 97         return errLines.stream().anyMatch(line -> line.contains(shouldInclude));
 98     }
 99 
100     private static List<String> linesFromStream(InputStream stream) throws IOException {
101         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
102             return reader.lines().toList();
103         }
104     }
105 }