jdk

Форк
0
/
TestInheritFD.java 
385 строк · 16.2 Кб
1
/*
2
 * Copyright (c) 2018, 2023, 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
import static java.lang.Character.isDigit;
25
import static java.lang.Long.parseLong;
26
import static java.lang.System.getProperty;
27
import static java.nio.file.Files.readAllBytes;
28
import static java.util.Arrays.stream;
29
import static java.util.stream.Collectors.joining;
30
import static java.util.stream.Collectors.toList;
31
import static jdk.test.lib.process.ProcessTools.createLimitedTestJavaProcessBuilder;
32
import static jdk.test.lib.Platform.isWindows;
33
import jdk.test.lib.Utils;
34
import jdk.test.lib.Platform;
35
import jtreg.SkippedException;
36

37
import java.io.BufferedReader;
38
import java.io.File;
39
import java.io.FileNotFoundException;
40
import java.io.FileOutputStream;
41
import java.io.IOException;
42
import java.io.InputStreamReader;
43
import java.util.Collection;
44
import java.util.concurrent.TimeoutException;
45
import java.util.concurrent.CompletionException;
46
import java.util.concurrent.TimeUnit;
47
import java.util.Optional;
48
import java.util.stream.Stream;
49

50
/*
51
 * @test TestInheritFD
52
 * @bug 8176717 8176809 8222500
53
 * @summary a new process should not inherit open file descriptors
54
 * @comment On Aix lsof requires root privileges.
55
 * @requires os.family != "aix"
56
 * @library /test/lib
57
 * @modules java.base/jdk.internal.misc
58
 *          java.management
59
 * @run driver TestInheritFD
60
 */
61

62
/**
63
 * Test that HotSpot does not leak logging file descriptors.
64
 *
65
 * This test is performed in three steps. The first VM starts a second VM with
66
 * gc logging enabled. The second VM starts a third VM and redirects the third
67
 * VMs output to the first VM. The second VM then exits and hopefully closes
68
 * its log file.
69
 *
70
 * The third VM waits for the second to exit and close its log file.
71
 * On Windows, the third VM tries to rename the log file of the second VM.
72
 * If it succeeds in doing so it means that the third VM did not inherit
73
 * the open log file (windows cannot rename opened files easily).
74
 * On unix like systems, the third VM uses "lsof" for verification.
75
 *
76
 * The third VM communicates success by printing "RETAINS FD". The first VM
77
 * waits for the third VM to exit and checks that the string was printed by
78
 * the third VM.
79
 */
80

81
public class TestInheritFD {
82

83
    public static final String LEAKS_FD = "VM RESULT => LEAKS FD";
84
    public static final String RETAINS_FD = "VM RESULT => RETAINS FD";
85
    public static final String EXIT = "VM RESULT => VM EXIT";
86
    public static final String LOG_SUFFIX = ".strangelogsuffixthatcanbecheckedfor";
87
    public static final String USER_DIR = System.getProperty("user.dir");
88
    public static final String LSOF_PID_PREFIX = " VM lsof pid=";
89
    public static final String SECOND_VM_PID_PREFIX = "Second VM pid=";
90
    public static final String THIRD_VM_PID_PREFIX = "Third VM pid=";
91
    public static final String THIRD_VM_WAITING_PREFIX = "Third VM waiting for second VM pid=";
92

93
    public static float timeoutFactor = Float.parseFloat(System.getProperty("test.timeout.factor", "1.0"));
94
    public static long subProcessTimeout = (long)(15L * timeoutFactor);
95

96
    // Extract a pid from the specified String at the specified start offset.
97
    private static long extractPidFromStringOffset(String str, int start) {
98
        int end;
99
        for (end = start; end < str.length(); end++) {
100
            if (!isDigit(str.charAt(end))) {
101
                break;
102
            }
103
        }
104
        if (start == end) {  // no digits at all
105
            return -1;
106
        }
107
        return parseLong(str.substring(start, end));
108
    }
109

110
    // Wait for the sub-process pids identified in commFile to finish executing.
111
    // Returns true if RETAINS_FD was found in the commFile and false otherwise.
112
    enum Result {
113
        FOUND_LEAKS_FD,
114
        FOUND_RETAINS_FD,
115
        FOUND_NONE // Unexpected.
116
    };
117
    private static Result waitForSubPids(File commFile) throws Exception {
118
        String out = "";
119
        int sleepCnt = 0;
120
        long secondVMPID = -1;
121
        long secondVMlsofPID = -1;
122
        long thirdVMPID = -1;
123
        long thirdVMlsofPID = -1;
124
        // Only have to gather info until the doneWithPattern shows up in the output:
125
        String doneWithPattern;
126
        if (isWindows()) {
127
            doneWithPattern = THIRD_VM_PID_PREFIX;
128
        } else {
129
            doneWithPattern = "Third" + LSOF_PID_PREFIX;
130
        }
131
        do {
132
            out = new String(readAllBytes(commFile.toPath()));
133
            if (secondVMPID == -1) {
134
                int ind = out.indexOf(SECOND_VM_PID_PREFIX);
135
                if (ind != -1) {
136
                    int startPid = ind + SECOND_VM_PID_PREFIX.length();
137
                    secondVMPID = extractPidFromStringOffset(out, startPid);
138
                    System.out.println("secondVMPID=" + secondVMPID);
139
                }
140
            }
141
            if (!isWindows() && secondVMlsofPID == -1) {
142
                String prefix = "Second" + LSOF_PID_PREFIX;
143
                int ind = out.indexOf(prefix);
144
                if (ind != -1) {
145
                    int startPid = ind + prefix.length();
146
                    secondVMlsofPID = extractPidFromStringOffset(out, startPid);
147
                    System.out.println("secondVMlsofPID=" + secondVMlsofPID);
148
                }
149
            }
150
            if (thirdVMPID == -1) {
151
                int ind = out.indexOf(THIRD_VM_PID_PREFIX);
152
                if (ind != -1) {
153
                    int startPid = ind + THIRD_VM_PID_PREFIX.length();
154
                    thirdVMPID = extractPidFromStringOffset(out, startPid);
155
                    System.out.println("thirdVMPID=" + thirdVMPID);
156
                }
157
            }
158
            if (!isWindows() && thirdVMlsofPID == -1) {
159
                String prefix = "Third" + LSOF_PID_PREFIX;
160
                int ind = out.indexOf(prefix);
161
                if (ind != -1) {
162
                    int startPid = ind + prefix.length();
163
                    thirdVMlsofPID = extractPidFromStringOffset(out, startPid);
164
                    System.out.println("thirdVMlsofPID=" + thirdVMlsofPID);
165
                }
166
            }
167
            Thread.sleep(100);
168
            sleepCnt++;
169
        } while (!out.contains(doneWithPattern) && !out.contains(EXIT));
170

171
        System.out.println("Called Thread.sleep(100) " + sleepCnt + " times.");
172

173
        long subPids[] = new long[4];       // At most 4 pids to check.
174
        String subNames[] = new String[4];  // At most 4 names for those pids.
175
        int ind = 0;
176
        if (!isWindows() && secondVMlsofPID != -1) {
177
            // The second VM's lsof cmd should be the first non-windows sub-process to finish:
178
            subPids[ind] = secondVMlsofPID;
179
            subNames[ind] = "second VM lsof";
180
            ind++;
181
        }
182
        // The second VM should the second non-windows or first windows sub-process to finish:
183
        subPids[ind] = secondVMPID;
184
        subNames[ind] = "second VM";
185
        ind++;
186
        if (!isWindows() && thirdVMlsofPID != -1) {
187
            // The third VM's lsof cmd should be the third non-windows sub-process to finish:
188
            subPids[ind] = thirdVMlsofPID;
189
            subNames[ind] = "third VM lsof";
190
            ind++;
191
        }
192
        // The third VM should the last sub-process to finish:
193
        subPids[ind] = thirdVMPID;
194
        subNames[ind] = "third VM";
195
        ind++;
196
        if (isWindows()) {
197
            // No lsof pids on windows so we use fewer array slots.
198
            // Make sure they are marked as not used.
199
            for (; ind < subPids.length; ind++) {
200
                subPids[ind] = -1;
201
            }
202
        }
203

204
        try {
205
            for (ind = 0; ind < subPids.length; ind++) {
206
                if (subPids[ind] == -1) {
207
                    continue;
208
                }
209
                System.out.print("subs[" + ind + "]={pid=" + subPids[ind] + ", name=" + subNames[ind] + "}");
210
                ProcessHandle.of(subPids[ind]).ifPresent(handle -> handle.onExit().orTimeout(subProcessTimeout, TimeUnit.SECONDS).join());
211
                System.out.println(" finished.");
212
            }
213
        } catch (Exception e) {
214
            // Terminate the "subs" line from above:
215
            System.out.println(" Exception was thrown while trying to join() subPids: " + e.toString());
216
            throw e;
217
        } finally {
218
            // Reread to get everything in the commFile:
219
            out = new String(readAllBytes(commFile.toPath()));
220
            System.out.println("<BEGIN commFile contents>");
221
            System.out.println(out);
222
            System.out.println("<END commFile contents>");
223
        }
224
        if (out.contains(RETAINS_FD)) {
225
            return Result.FOUND_RETAINS_FD;
226
        } else if (out.contains(LEAKS_FD)) {
227
            return Result.FOUND_LEAKS_FD;
228
        } else {
229
            return Result.FOUND_NONE;
230
        }
231
    }
232

233
    // first VM
234
    public static void main(String[] args) throws Exception {
235
        System.out.println("subProcessTimeout=" + subProcessTimeout + " seconds.");
236
        System.out.println("First VM starts.");
237
        String logPath = Utils.createTempFile("logging", LOG_SUFFIX).toFile().getName();
238
        File commFile = Utils.createTempFile("communication", ".txt").toFile();
239

240
        if (!isWindows() && !lsofCommand().isPresent()) {
241
            throw new SkippedException("Could not find lsof like command");
242
        }
243

244
        ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
245
            "-Xlog:gc:\"" + logPath + "\"",
246
            "-Dtest.jdk=" + getProperty("test.jdk"),
247
            VMStartedWithLogging.class.getName(),
248
            logPath);
249

250
        pb.redirectOutput(commFile); // use temp file to communicate between processes
251
        pb.start();
252

253
        Result result = waitForSubPids(commFile);
254
        if (result == Result.FOUND_RETAINS_FD) {
255
            System.out.println("Log file was not inherited by third VM.");
256
        } else if (result == Result.FOUND_LEAKS_FD) {
257
            throw new RuntimeException("Log file was leaked to the third VM.");
258
        } else {
259
            throw new RuntimeException("Found neither message, test failed to run correctly");
260
        }
261
        System.out.println("First VM ends.");
262
    }
263

264
    static class VMStartedWithLogging {
265
        // second VM
266
        public static void main(String[] args) throws IOException, InterruptedException {
267
            System.out.println(SECOND_VM_PID_PREFIX + ProcessHandle.current().pid());
268
            ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
269
                "-Dtest.jdk=" + getProperty("test.jdk"),
270
                VMShouldNotInheritFileDescriptors.class.getName(),
271
                args[0],
272
                "" + ProcessHandle.current().pid());
273
            pb.inheritIO(); // in future, redirect information from third VM to first VM
274
            pb.start();
275

276
            if (!isWindows()) {
277
                System.out.println("(Second VM) Open file descriptors:\n" + outputContainingFilenames("Second").stream().collect(joining("\n")));
278
            }
279
            if (false) {  // Enable to simulate a timeout in the second VM.
280
                Thread.sleep(300 * 1000);
281
            }
282
            System.out.println("Second VM ends.");
283
        }
284
    }
285

286
    static class VMShouldNotInheritFileDescriptors {
287
        // third VM
288
        public static void main(String[] args) throws InterruptedException {
289
            System.out.println(THIRD_VM_PID_PREFIX + ProcessHandle.current().pid());
290
            try {
291
                File logFile = new File(args[0]);
292
                long parentPid = parseLong(args[1]);
293
                fakeLeakyJVM(false); // for debugging of test case
294

295
                System.out.println(THIRD_VM_WAITING_PREFIX + parentPid);
296
                ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().orTimeout(subProcessTimeout, TimeUnit.SECONDS).join());
297

298
                if (isWindows()) {
299
                    windows(logFile);
300
                } else {
301
                    Collection<String> output = outputContainingFilenames("Third");
302
                    System.out.println("(Third VM) Open file descriptors:\n" + output.stream().collect(joining("\n")));
303
                    System.out.println(findOpenLogFile(output) ? LEAKS_FD : RETAINS_FD);
304
                }
305
                if (false) {  // Enable to simulate a timeout in the third VM.
306
                    Thread.sleep(300 * 1000);
307
                }
308
            } catch (CompletionException e) {
309
                if (e.getCause() instanceof TimeoutException) {
310
                    System.out.println("(Third VM) Timed out waiting for second VM: " + e.toString());
311
                } else {
312
                    System.out.println("(Third VM) Exception was thrown: " + e.toString());
313
                }
314
                throw e;
315
            } catch (Exception e) {
316
                System.out.println("(Third VM) Exception was thrown: " + e.toString());
317
                throw e;
318
            } finally {
319
                System.out.println(EXIT);
320
                System.out.println("Third VM ends.");
321
            }
322
        }
323
    }
324

325
    // for debugging of test case
326
    @SuppressWarnings("resource")
327
    static void fakeLeakyJVM(boolean fake) {
328
        if (fake) {
329
            try {
330
                new FileOutputStream("fakeLeakyJVM" + LOG_SUFFIX, false);
331
            } catch (FileNotFoundException e) {
332
            }
333
        }
334
    }
335

336
    static Stream<String> runLsof(String whichVM, String... args){
337
        try {
338
            Process lsof = new ProcessBuilder(args).start();
339
            System.out.println(whichVM + LSOF_PID_PREFIX + lsof.pid());
340
            return new BufferedReader(new InputStreamReader(lsof.getInputStream())).lines();
341
        } catch (IOException e) {
342
            throw new RuntimeException(e);
343
        }
344
    }
345

346
    static Optional<String[]> lsofCommandCache = stream(new String[][]{
347
            {"/usr/bin/lsof", "-p"},
348
            {"/usr/sbin/lsof", "-p"},
349
            {"/bin/lsof", "-p"},
350
            {"/sbin/lsof", "-p"},
351
            {"/usr/local/bin/lsof", "-p"}})
352
        .filter(args -> new File(args[0]).exists())
353
        .findFirst();
354

355
    static Optional<String[]> lsofCommand() {
356
        return lsofCommandCache;
357
    }
358

359
    static Collection<String> outputContainingFilenames(String whichVM) {
360
        long pid = ProcessHandle.current().pid();
361
        String[] command = lsofCommand().orElseThrow(() -> new RuntimeException("lsof like command not found"));
362
        // Only search the directory in which the VM is running (user.dir property).
363
        System.out.println("using command: " + command[0] + " -a +d " + USER_DIR + " " + command[1] + " " + pid);
364
        return runLsof(whichVM, command[0], "-a", "+d", USER_DIR, command[1], "" + pid).collect(toList());
365
    }
366

367
    static boolean findOpenLogFile(Collection<String> fileNames) {
368
        String pid = Long.toString(ProcessHandle.current().pid());
369
        String[] command = lsofCommand().orElseThrow(() ->
370
                new RuntimeException("lsof like command not found"));
371
        String lsof = command[0];
372
        boolean isBusybox = Platform.isBusybox(lsof);
373
        return fileNames.stream()
374
            // lsof from busybox does not support "-p" option
375
            .filter(fileName -> !isBusybox || fileName.contains(pid))
376
            .filter(fileName -> fileName.contains(LOG_SUFFIX))
377
            .findAny()
378
            .isPresent();
379
    }
380

381
    static void windows(File f) throws InterruptedException {
382
        System.out.println("trying to rename file to the same name: " + f);
383
        System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by printing "VM RESULT => RETAINS FD"
384
    }
385
}
386

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.