1 /*
2  * Copyright (c) 2015, 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 /*
26  * @test
27  * @summary Run multiple processes with the same archive, ensure they share
28  *
29  * @requires vm.cds
30  * @library /test/lib
31  * @build sun.hotspot.WhiteBox
32  * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
33  * @compile test-classes/MultiProcClass.java
34  * @run driver MultiProcessSharing
35  */
36 
37 import java.io.File;
38 import jdk.test.lib.Asserts;
39 import jdk.test.lib.Platform;
40 import jdk.test.lib.process.OutputAnalyzer;
41 import sun.hotspot.WhiteBox;
42 
43 
44 public class MultiProcessSharing {
45     static String useWbJar;
46     static String sharedClass1Jar;
47     static boolean checkPmap = false;
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         String wbJar = JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox");
51         useWbJar = "-Xbootclasspath/a:" + wbJar;
52         sharedClass1Jar = JarBuilder.build("shared_class1", "MultiProcClass");
53 
54         // create an archive
55         OutputAnalyzer out = TestCommon.dump(sharedClass1Jar,
56             TestCommon.list("MultiProcClass"), useWbJar);
57         TestCommon.checkDump(out);
58 
59         // determine whether OK to use pmap for extra test verification
60         long myPid = ProcessHandle.current().pid();
61         checkPmap = (Platform.isLinux() && (MultiProcClass.runPmap(myPid, false) == 0));
62         System.out.println("MultiProcessSharing: checkPmap is " + checkPmap);
63 
64         // use an archive in several processes concurrently
65         int numProcesses = 3;
66         Thread[] threads = new Thread[numProcesses];
67         ProcessHandler[] processHandlers = new ProcessHandler[numProcesses];
68         for (int i = 0; i < numProcesses; i++) {
69             processHandlers[i] = new ProcessHandler(i);
70             threads[i] = new Thread(processHandlers[i]);
71         }
72 
73         for (Thread t : threads) {
74             t.start();
75         }
76 
77         for (Thread t : threads) {
78             try {
79                 t.join();
80             } catch (InterruptedException ie) {
81                 throw ie;
82             }
83         }
84 
85         // check results
86         for (ProcessHandler ph : processHandlers) {
87             TestCommon.checkExec(ph.out);
88             if (checkPmap && !TestCommon.isUnableToMap(ph.out)) {
89                 checkPmapOutput(ph.out.getOutput());
90             }
91         }
92     }
93 
94 
95     static class ProcessHandler implements Runnable {
96         int processNumber;
97         OutputAnalyzer out;
98 
ProcessHandler(int processNumber)99         ProcessHandler(int processNumber) {
100             this.processNumber = processNumber;
101         }
102 
103         @Override
run()104         public void run() {
105             try {
106                 out = TestCommon.exec(sharedClass1Jar,
107                    "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", useWbJar,
108                    "MultiProcClass", "" + processNumber, "" + checkPmap);
109             } catch (Exception e) {
110                 throw new RuntimeException("Error occurred when using archive, exec()" + e);
111             }
112         }
113     }
114 
115 
checkPmapOutput(String stdio)116     private static void checkPmapOutput(String stdio) {
117         System.out.println("Checking pmap output ...");
118         String[] lines = stdio.split("\n");
119 
120         boolean foundJsa = false;
121         boolean foundReadOnlyJsaSection = false;
122 
123         for (String line : lines) {
124             if (line.contains(TestCommon.getCurrentArchiveName()))
125                 System.out.println(line);
126                 foundJsa = true;
127                 if (line.contains("r--")) {
128                     foundReadOnlyJsaSection = true;
129                 }
130 
131                 // On certain ARM platforms system maps r/o memory mapped files
132                 // as r/x; see JDK-8145694 for details
133                 if ( (Platform.isARM() || Platform.isAArch64()) && line.contains("r-x") ) {
134                     foundReadOnlyJsaSection = true;
135                 }
136         }
137 
138         Asserts.assertTrue(foundJsa && foundReadOnlyJsaSection);
139     }
140 
141 }
142