46
46
import jdk .vm .ci .runtime .JVMCIRuntime ;
47
47
48
48
import java .io .File ;
49
- import java .io .IOException ;
50
49
import java .lang .reflect .Method ;
51
50
import java .nio .file .Files ;
52
51
import java .nio .file .Path ;
53
52
import java .util .List ;
53
+ import java .util .concurrent .TimeUnit ;
54
54
import java .util .concurrent .atomic .AtomicInteger ;
55
55
56
56
public class TestUncaughtErrorInCompileMethod extends JVMCIServiceLocator {
57
57
58
- /**
59
- * Name of file whose existence implies that a JVMCICompiler has been created.
60
- */
61
- static String tmpFileName = "ErrorCompilerCreated." + System .nanoTime ();
58
+ static volatile boolean compilerCreationErrorOccurred ;
62
59
63
60
/**
64
61
* @param args if args.length != 0, then executing in subprocess
@@ -68,19 +65,11 @@ public static void main(String[] args) throws Exception {
68
65
testSubprocess (false );
69
66
testSubprocess (true );
70
67
} else {
71
- File watch = new File (tmpFileName );
72
68
int total = 0 ;
73
- long start = System .currentTimeMillis ();
74
-
75
- // Use a 10 sec timeout to prevent endless loop if
76
- // JVMCI compiler creation fails
77
- while (System .currentTimeMillis () - start < 10_000 ) {
69
+ while (!compilerCreationErrorOccurred ) {
70
+ // Do some random work to trigger compilation
78
71
total += getTime ();
79
- if (watch .exists ()) {
80
- System .err .println ("saw " + watch + " - exiting loop" );
81
- watch .delete ();
82
- break ;
83
- }
72
+ total += String .valueOf (total ).hashCode ();
84
73
}
85
74
System .out .println (total );
86
75
}
@@ -101,7 +90,16 @@ static void testSubprocess(boolean fatalError) throws Exception {
101
90
"-XX:+PrintWarnings" ,
102
91
"-Xbootclasspath/a:." ,
103
92
TestUncaughtErrorInCompileMethod .class .getName (), "true" );
104
- OutputAnalyzer output = new OutputAnalyzer (pb .start ());
93
+ Process p = pb .start ();
94
+ OutputAnalyzer output = new OutputAnalyzer (p );
95
+
96
+ if (!waitForProcess (p )) {
97
+ // The subprocess might not enter JVMCI compilation.
98
+ // Print the subprocess output and pass the test in this case.
99
+ System .out .println (output .getOutput ());
100
+ return ;
101
+ }
102
+
105
103
if (fatalError ) {
106
104
output .shouldContain ("testing JVMCI fatal exception handling" );
107
105
output .shouldNotHaveExitValue (0 );
@@ -137,6 +135,31 @@ static void testSubprocess(boolean fatalError) throws Exception {
137
135
}
138
136
}
139
137
138
+ /**
139
+ * @return true if {@code p} exited on its own, false if it had to be destroyed
140
+ */
141
+ private static boolean waitForProcess (Process p ) {
142
+ while (true ) {
143
+ try {
144
+ boolean exited = p .waitFor (10 , TimeUnit .SECONDS );
145
+ if (!exited ) {
146
+ System .out .println ("destroying process: " + p );
147
+ p .destroy ();
148
+ Thread .sleep (1000 );
149
+ while (p .isAlive ()) {
150
+ System .out .println ("forcibly destroying process: " + p );
151
+ Thread .sleep (1000 );
152
+ p .destroyForcibly ();
153
+ }
154
+ return false ;
155
+ }
156
+ return true ;
157
+ } catch (InterruptedException e ) {
158
+ e .printStackTrace (System .out );
159
+ }
160
+ }
161
+ }
162
+
140
163
public TestUncaughtErrorInCompileMethod () {
141
164
}
142
165
@@ -161,18 +184,10 @@ public JVMCICompiler createCompiler(JVMCIRuntime runtime) {
161
184
int attempt = counter .incrementAndGet ();
162
185
CompilerCreationError e = new CompilerCreationError (attempt );
163
186
e .printStackTrace ();
164
- if (attempt == 10 ) {
165
- // Delay the creation of the file that causes the
166
- // loop in main to exit so that compilation failures
187
+ if (attempt >= 10 ) {
188
+ // Delay notifying the loop in main so that compilation failures
167
189
// have time to be reported by -XX:+PrintCompilation.
168
- File watch = new File (tmpFileName );
169
- try {
170
- System .err .println ("creating " + watch );
171
- watch .createNewFile ();
172
- System .err .println ("created " + watch );
173
- } catch (IOException ex ) {
174
- ex .printStackTrace ();
175
- }
190
+ compilerCreationErrorOccurred = true ;
176
191
}
177
192
throw e ;
178
193
}
0 commit comments