Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3147ec0d4457e91c9532ff68f983d3d356caed74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2019 Syntevo and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Syntevo - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tests.manual;

import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import java.io.*;
import java.util.*;

public class Bug547109_JvmCrashNativeLibraryLoad {
	private static final String ARG_NO_UI = "-noUI";
	private static final int EXIT_CODE_GOOD = 0;

	private static void createEmptyFolder(File folder) {
		if (!folder.exists()) {
			folder.mkdir();
		}
		else {
			File[] files = folder.listFiles();
			for (int i = 0; i < files.length; i++) {
				files[i].delete();
			}
		}
	}

	private static void test(Shell shell) {
		// Create a folder to hold the native libraries
		// Delete any libraries if already present
		File libsFolder = new File("nativeLibs");
		createEmptyFolder(libsFolder);

		boolean isMacOS = Platform.PLATFORM.equalsIgnoreCase("cocoa");

		// Run processes
		ProcessBuilder processBuilder = new ProcessBuilder(
				System.getProperty("java.home") + "/bin/java",
				"-Dswt.library.path=" + libsFolder.getAbsolutePath(),
				"-classpath", System.getProperty("java.class.path"),
				isMacOS ? "-XstartOnFirstThread" : "",
				Bug547109_JvmCrashNativeLibraryLoad.class.getName(),
				ARG_NO_UI
		);
		processBuilder.inheritIO();

		int numErrors = 0;
		try {
			Process[] processes = new Process[15];

			// Run many processes at once
			for (int i = 0; i < processes.length; i++) {
				processes[i] = processBuilder.start();
			}

			// Test for errors
			for (int i = 0; i < processes.length; i++) {
				int exitCode = processes[i].waitFor();
				if (EXIT_CODE_GOOD != exitCode) numErrors++;
			}
		} catch (Throwable ex) {
			ex.printStackTrace();
		}

		// Report
		String message = String.format("%d crashes/errors detected", numErrors);
		MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION);
		dialog.setMessage(message);
		dialog.open();
	}

	private static boolean isSwtJar() {
		// Code from Library.loadLibrary
		String libName = "swt-" + Platform.PLATFORM + "-" + Library.getVersionString();
		// Code from Library.mapLibraryName
		libName = System.mapLibraryName (libName);
		String ext = ".dylib"; //$NON-NLS-1$
		if (libName.endsWith(ext)) {
			libName = libName.substring(0, libName.length() - ext.length()) + ".jnilib"; //$NON-NLS-1$
		}

		// Is it available?
		try (InputStream inputStream = Display.class.getResourceAsStream("/" + libName))
		{
			return (inputStream != null);
		} catch (Throwable e) {
			return false;
		}
	}

	public static void main(String[] args) throws Exception {
		final Display display = new Display();

		if (Arrays.asList(args).contains(ARG_NO_UI)) {
			System.exit(EXIT_CODE_GOOD);
		}

		final Shell shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setSize(300, 200);

		FillLayout layout = new FillLayout();
		layout.marginWidth = 30;
		layout.marginHeight = 30;
		shell.setLayout(layout);

		final Button button = new Button(shell, SWT.PUSH | SWT.WRAP);

		if (isSwtJar()) {
			button.setText("Click me && wait for msgbox\n\nIf you have crashes/errors, bug exists. Check console to see detected problems.");
			button.addListener(SWT.Selection, event -> {
				test(shell);
			});
		} else {
			// Only swt.jar has the libraries
			button.setText("Native library resource not found. Add it to classpath or run with swt.jar");
		}

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();
	}
}

Back to the top