Testlibadimalloc.java revision 9521:eb0e2e67755b
1/*
2 * Copyright (c) 2015, 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/*
26 * @test Testlibadimalloc.java
27 * @bug 8141445
28 * @summary make sure the Solaris Sparc M7 libadimalloc.so library generates SIGSEGV's on buffer overflow
29 * @requires (os.family == "solaris" & os.arch == "sparcv9")
30 * @library /testlibrary
31 * @build jdk.test.lib.*
32 * @compile SEGVOverflow.java
33 * @run driver Testlibadimalloc
34 */
35
36import java.io.*;
37import java.nio.file.*;
38import java.util.*;
39import jdk.test.lib.ProcessTools;
40
41public class Testlibadimalloc {
42
43    // Expected return value when java program cores
44    static final int EXPECTED_RET_VAL = 6;
45
46    public static void main(String[] args) throws Throwable {
47
48        // See if the libadimalloc.so library exists
49        Path path = Paths.get("/usr/lib/64/libadimalloc.so");
50
51        // If the libadimalloc.so file does not exist, pass the test
52        if (!(Files.isRegularFile(path) || Files.isSymbolicLink(path))) {
53            System.out.println("Test skipped; libadimalloc.so does not exist");
54            return;
55        }
56
57        // Get the JDK, library and class path properties
58        String libpath = System.getProperty("java.library.path");
59
60        // Create a new java process for the SEGVOverflow Java/JNI test
61        ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(
62            "-Djava.library.path=" + libpath + ":.", "SEGVOverflow");
63
64        // Add the LD_PRELOAD_64 value to the environment
65        Map<String, String> env = builder.environment();
66        env.put("LD_PRELOAD_64", "libadimalloc.so");
67
68        // Start the process, get the pid and then wait for the test to finish
69        Process process = builder.start();
70        long pid = process.getPid();
71        int retval = process.waitFor();
72
73        // make sure the SEGVOverflow test crashed
74        boolean found = false;
75        if (retval == EXPECTED_RET_VAL) {
76            String filename = "hs_err_pid" + pid + ".log";
77            Path filepath = Paths.get(filename);
78            // check to see if hs_err_file exists
79            if (Files.isRegularFile(filepath)) {
80                // see if the crash was due to a SEGV_ACCPERR signal
81                File hs_err_file = new File(filename);
82                Scanner scanner = new Scanner(hs_err_file);
83                while (!found && scanner.hasNextLine()) {
84                    String nextline = scanner.nextLine();
85                    if (nextline.contains("SEGV_ACCPERR")) {
86                         found = true;
87                    }
88                }
89            } else {
90                System.out.println("Test failed; hs_err_file does not exist: "
91                                   + filepath);
92            }
93        } else {
94            System.out.println("Test failed; java test program did not " +
95                               "return expected error: expected = " +
96                               EXPECTED_RET_VAL + ", retval = " + retval);
97        }
98        // If SEGV_ACCPERR was not found in the hs_err file fail the test
99        if (!found) {
100            System.out.println("FAIL: SEGV_ACCPERR not found");
101            throw new RuntimeException("FAIL: SEGV_ACCPERR not found");
102        }
103    }
104}
105