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