1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel.verifier;
19
20 import java.awt.Color;
21 import java.awt.Dialog;
22 import java.awt.Frame;
23 import java.awt.SystemColor;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.WindowAdapter;
27 import java.awt.event.WindowEvent;
28
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JPanel;
32 import javax.swing.WindowConstants;
33
34 import org.apache.bcel.Repository;
35 import org.apache.bcel.classfile.JavaClass;
36 import org.apache.bcel.classfile.Utility;
37
38
39
40
41
42
43
44
45
46
47 public class VerifyDialog extends JDialog {
48
49
50 final class IvjEventHandler implements ActionListener {
51
52 @Override
53 public void actionPerformed(final ActionEvent e) {
54 if (e.getSource() == VerifyDialog.this.getPass1Button()) {
55 connEtoC1(e);
56 }
57 if (e.getSource() == VerifyDialog.this.getPass2Button()) {
58 connEtoC2(e);
59 }
60 if (e.getSource() == VerifyDialog.this.getPass3Button()) {
61 connEtoC3(e);
62 }
63 if (e.getSource() == VerifyDialog.this.getFlushButton()) {
64 connEtoC4(e);
65 }
66 }
67 }
68
69 private static final long serialVersionUID = -6374807677043142313L;
70
71
72
73
74
75 private static int classesToVerify;
76
77
78
79
80
81
82
83 public static void main(final String[] args) {
84 classesToVerify = args.length;
85 for (final String arg : args) {
86 try {
87 final VerifyDialog aVerifyDialog;
88 aVerifyDialog = new VerifyDialog(arg);
89 aVerifyDialog.setModal(true);
90 aVerifyDialog.addWindowListener(new WindowAdapter() {
91
92 @Override
93 public void windowClosing(final WindowEvent e) {
94 classesToVerify--;
95 if (classesToVerify == 0) {
96 System.exit(0);
97 }
98 }
99 });
100 aVerifyDialog.setVisible(true);
101 } catch (final Throwable exception) {
102 System.err.println("Exception occurred in main() of JDialog");
103 exception.printStackTrace(System.out);
104 }
105 }
106 }
107
108
109 private JPanel ivjJDialogContentPane;
110
111 private JPanel ivjPass1Panel;
112
113 private JPanel ivjPass2Panel;
114
115 private JPanel ivjPass3Panel;
116
117 private JButton ivjPass1Button;
118
119 private JButton ivjPass2Button;
120
121 private JButton ivjPass3Button;
122
123
124 private final IvjEventHandler ivjEventHandler = new IvjEventHandler();
125
126
127
128
129
130 private String className = "java.lang.Object";
131
132
133 private JButton ivjFlushButton;
134
135
136 public VerifyDialog() {
137 initialize();
138 }
139
140
141 public VerifyDialog(final Dialog owner) {
142 super(owner);
143 }
144
145
146 public VerifyDialog(final Dialog owner, final boolean modal) {
147 super(owner, modal);
148 }
149
150
151 public VerifyDialog(final Dialog owner, final String title) {
152 super(owner, title);
153 }
154
155
156 public VerifyDialog(final Dialog owner, final String title, final boolean modal) {
157 super(owner, title, modal);
158 }
159
160
161 public VerifyDialog(final Frame owner) {
162 super(owner);
163 }
164
165
166 public VerifyDialog(final Frame owner, final boolean modal) {
167 super(owner, modal);
168 }
169
170
171 public VerifyDialog(final Frame owner, final String title) {
172 super(owner, title);
173 }
174
175
176 public VerifyDialog(final Frame owner, final String title, final boolean modal) {
177 super(owner, title, modal);
178 }
179
180
181
182
183
184
185 public VerifyDialog(String fullyQualifiedClassName) {
186 final int dotclasspos = fullyQualifiedClassName.lastIndexOf(JavaClass.EXTENSION);
187 if (dotclasspos != -1) {
188 fullyQualifiedClassName = fullyQualifiedClassName.substring(0, dotclasspos);
189 }
190 fullyQualifiedClassName = Utility.pathToPackage(fullyQualifiedClassName);
191 this.className = fullyQualifiedClassName;
192 initialize();
193 }
194
195
196 private void connEtoC1(final ActionEvent arg1) {
197 try {
198
199
200 this.pass1Button_ActionPerformed(arg1);
201
202
203 } catch (final Throwable ivjExc) {
204
205
206 handleException(ivjExc);
207 }
208 }
209
210
211 private void connEtoC2(final ActionEvent arg1) {
212 try {
213
214
215 this.pass2Button_ActionPerformed(arg1);
216
217
218 } catch (final Throwable ivjExc) {
219
220
221 handleException(ivjExc);
222 }
223 }
224
225
226 private void connEtoC3(final ActionEvent arg1) {
227 try {
228
229
230 this.pass4Button_ActionPerformed(arg1);
231
232
233 } catch (final Throwable ivjExc) {
234
235
236 handleException(ivjExc);
237 }
238 }
239
240
241 private void connEtoC4(final ActionEvent arg1) {
242 try {
243
244
245 this.flushButton_ActionPerformed(arg1);
246
247
248 } catch (final Throwable ivjExc) {
249
250
251 handleException(ivjExc);
252 }
253 }
254
255
256 public void flushButton_ActionPerformed(final ActionEvent actionEvent) {
257 VerifierFactory.getVerifier(className).flush();
258 Repository.removeClass(className);
259 getPass1Panel().setBackground(Color.gray);
260 getPass1Panel().repaint();
261 getPass2Panel().setBackground(Color.gray);
262 getPass2Panel().repaint();
263 getPass3Panel().setBackground(Color.gray);
264 getPass3Panel().repaint();
265 }
266
267
268 private JButton getFlushButton() {
269 if (ivjFlushButton == null) {
270 try {
271 ivjFlushButton = new JButton();
272 ivjFlushButton.setName("FlushButton");
273 ivjFlushButton.setText("Flush: Forget old verification results");
274 ivjFlushButton.setBackground(SystemColor.controlHighlight);
275 ivjFlushButton.setBounds(60, 215, 300, 30);
276 ivjFlushButton.setForeground(Color.red);
277 ivjFlushButton.setActionCommand("FlushButton");
278
279
280 } catch (final Throwable ivjExc) {
281
282
283 handleException(ivjExc);
284 }
285 }
286 return ivjFlushButton;
287 }
288
289
290 private JPanel getJDialogContentPane() {
291 if (ivjJDialogContentPane == null) {
292 try {
293 ivjJDialogContentPane = new JPanel();
294 ivjJDialogContentPane.setName("JDialogContentPane");
295 ivjJDialogContentPane.setLayout(null);
296 getJDialogContentPane().add(getPass1Panel(), getPass1Panel().getName());
297 getJDialogContentPane().add(getPass3Panel(), getPass3Panel().getName());
298 getJDialogContentPane().add(getPass2Panel(), getPass2Panel().getName());
299 getJDialogContentPane().add(getPass1Button(), getPass1Button().getName());
300 getJDialogContentPane().add(getPass2Button(), getPass2Button().getName());
301 getJDialogContentPane().add(getPass3Button(), getPass3Button().getName());
302 getJDialogContentPane().add(getFlushButton(), getFlushButton().getName());
303
304
305 } catch (final Throwable ivjExc) {
306
307
308 handleException(ivjExc);
309 }
310 }
311 return ivjJDialogContentPane;
312 }
313
314
315 private JButton getPass1Button() {
316 if (ivjPass1Button == null) {
317 try {
318 ivjPass1Button = new JButton();
319 ivjPass1Button.setName("Pass1Button");
320 ivjPass1Button.setText("Pass1: Verify binary layout of .class file");
321 ivjPass1Button.setBackground(SystemColor.controlHighlight);
322 ivjPass1Button.setBounds(100, 40, 300, 30);
323 ivjPass1Button.setActionCommand("Button1");
324
325
326 } catch (final Throwable ivjExc) {
327
328
329 handleException(ivjExc);
330 }
331 }
332 return ivjPass1Button;
333 }
334
335
336 private JPanel getPass1Panel() {
337 if (ivjPass1Panel == null) {
338 try {
339 ivjPass1Panel = new JPanel();
340 ivjPass1Panel.setName("Pass1Panel");
341 ivjPass1Panel.setLayout(null);
342 ivjPass1Panel.setBackground(SystemColor.controlShadow);
343 ivjPass1Panel.setBounds(30, 30, 50, 50);
344
345
346 } catch (final Throwable ivjExc) {
347
348
349 handleException(ivjExc);
350 }
351 }
352 return ivjPass1Panel;
353 }
354
355
356 private JButton getPass2Button() {
357 if (ivjPass2Button == null) {
358 try {
359 ivjPass2Button = new JButton();
360 ivjPass2Button.setName("Pass2Button");
361 ivjPass2Button.setText("Pass 2: Verify static .class file constraints");
362 ivjPass2Button.setBackground(SystemColor.controlHighlight);
363 ivjPass2Button.setBounds(100, 100, 300, 30);
364 ivjPass2Button.setActionCommand("Button2");
365
366
367 } catch (final Throwable ivjExc) {
368
369
370 handleException(ivjExc);
371 }
372 }
373 return ivjPass2Button;
374 }
375
376
377 private JPanel getPass2Panel() {
378 if (ivjPass2Panel == null) {
379 try {
380 ivjPass2Panel = new JPanel();
381 ivjPass2Panel.setName("Pass2Panel");
382 ivjPass2Panel.setLayout(null);
383 ivjPass2Panel.setBackground(SystemColor.controlShadow);
384 ivjPass2Panel.setBounds(30, 90, 50, 50);
385
386
387 } catch (final Throwable ivjExc) {
388
389
390 handleException(ivjExc);
391 }
392 }
393 return ivjPass2Panel;
394 }
395
396
397 private JButton getPass3Button() {
398 if (ivjPass3Button == null) {
399 try {
400 ivjPass3Button = new JButton();
401 ivjPass3Button.setName("Pass3Button");
402 ivjPass3Button.setText("Passes 3a+3b: Verify code arrays");
403 ivjPass3Button.setBackground(SystemColor.controlHighlight);
404 ivjPass3Button.setBounds(100, 160, 300, 30);
405 ivjPass3Button.setActionCommand("Button2");
406
407
408 } catch (final Throwable ivjExc) {
409
410
411 handleException(ivjExc);
412 }
413 }
414 return ivjPass3Button;
415 }
416
417
418 private JPanel getPass3Panel() {
419 if (ivjPass3Panel == null) {
420 try {
421 ivjPass3Panel = new JPanel();
422 ivjPass3Panel.setName("Pass3Panel");
423 ivjPass3Panel.setLayout(null);
424 ivjPass3Panel.setBackground(SystemColor.controlShadow);
425 ivjPass3Panel.setBounds(30, 150, 50, 50);
426
427
428 } catch (final Throwable ivjExc) {
429
430
431 handleException(ivjExc);
432 }
433 }
434 return ivjPass3Panel;
435 }
436
437
438 private void handleException(final Throwable exception) {
439
440 System.out.println("--------- UNCAUGHT EXCEPTION ---------");
441 exception.printStackTrace(System.out);
442
443 if (exception instanceof ThreadDeath) {
444 throw (ThreadDeath) exception;
445 }
446 if (exception instanceof VirtualMachineError) {
447 throw (VirtualMachineError) exception;
448 }
449 }
450
451
452 private void initConnections() {
453
454
455 getPass1Button().addActionListener(ivjEventHandler);
456 getPass2Button().addActionListener(ivjEventHandler);
457 getPass3Button().addActionListener(ivjEventHandler);
458 getFlushButton().addActionListener(ivjEventHandler);
459 }
460
461
462 private void initialize() {
463 try {
464
465
466 setName("VerifyDialog");
467 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
468 setSize(430, 280);
469 setVisible(true);
470 setModal(true);
471 setResizable(false);
472 setContentPane(getJDialogContentPane());
473 initConnections();
474 } catch (final Throwable ivjExc) {
475 handleException(ivjExc);
476 }
477
478 setTitle("'" + className + "' verification - JustIce / BCEL");
479
480 }
481
482
483 public void pass1Button_ActionPerformed(final ActionEvent actionEvent) {
484 final Verifier v = VerifierFactory.getVerifier(className);
485 final VerificationResult vr = v.doPass1();
486 if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
487 getPass1Panel().setBackground(Color.green);
488 getPass1Panel().repaint();
489 }
490 if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
491 getPass1Panel().setBackground(Color.red);
492 getPass1Panel().repaint();
493 }
494 }
495
496
497 public void pass2Button_ActionPerformed(final ActionEvent actionEvent) {
498 pass1Button_ActionPerformed(actionEvent);
499 final Verifier v = VerifierFactory.getVerifier(className);
500 final VerificationResult vr = v.doPass2();
501 if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
502 getPass2Panel().setBackground(Color.green);
503 getPass2Panel().repaint();
504 }
505 if (vr.getStatus() == VerificationResult.VERIFIED_NOTYET) {
506 getPass2Panel().setBackground(Color.yellow);
507 getPass2Panel().repaint();
508 }
509 if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
510 getPass2Panel().setBackground(Color.red);
511 getPass2Panel().repaint();
512 }
513 }
514
515
516 public void pass4Button_ActionPerformed(final ActionEvent actionEvent) {
517 pass2Button_ActionPerformed(actionEvent);
518 Color color = Color.green;
519 final Verifier v = VerifierFactory.getVerifier(className);
520 VerificationResult vr = v.doPass2();
521 if (vr.getStatus() == VerificationResult.VERIFIED_OK) {
522 JavaClass jc = null;
523 try {
524 jc = Repository.lookupClass(className);
525 final int nr = jc.getMethods().length;
526 for (int i = 0; i < nr; i++) {
527 vr = v.doPass3b(i);
528 if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
529 color = Color.red;
530 break;
531 }
532 }
533 } catch (final ClassNotFoundException ex) {
534
535 ex.printStackTrace();
536 }
537 } else {
538 color = Color.yellow;
539 }
540 getPass3Panel().setBackground(color);
541 getPass3Panel().repaint();
542 }
543 }