KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > tree > analysis > SimpleVerifierUnitTest


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.tree.analysis;
31
32 import org.objectweb.asm.Label;
33 import org.objectweb.asm.MethodVisitor;
34 import org.objectweb.asm.Opcodes;
35 import org.objectweb.asm.Type;
36 import org.objectweb.asm.tree.MethodNode;
37
38 import junit.framework.TestCase;
39
40 /**
41  * SimpleVerifier unit tests.
42  *
43  * @author Eric Bruneton
44  */

45 public class SimpleVerifierUnitTest extends TestCase implements Opcodes {
46
47     private Analyzer a;
48
49     private MethodNode mn;
50
51     protected void setUp() {
52         Type c = Type.getType("LC;");
53         Type d = Type.getType("Ljava/lang/Number;");
54         a = new Analyzer(new SimpleVerifier(c, d, false));
55         mn = new MethodNode(ACC_PUBLIC, "m", "()V", null, null);
56     }
57
58     private void assertValid() throws AnalyzerException {
59         mn.visitInsn(RETURN);
60         mn.visitMaxs(10, 10);
61         a.analyze("C", mn);
62         Frame[] frames = a.getFrames();
63         for (int i = 0; i < frames.length; ++i) {
64             if (frames[i] != null) {
65                 frames[i].toString();
66             }
67         }
68         a.getHandlers(0);
69     }
70
71     private void assertInvalid() {
72         mn.visitInsn(RETURN);
73         mn.visitMaxs(10, 10);
74         try {
75             a.analyze("C", mn);
76             fail();
77         } catch (AnalyzerException e) {
78         } catch (RuntimeException JavaDoc e) {
79         }
80     }
81
82     public void testInvalidOpcode() {
83         mn.visitInsn(-1);
84         assertInvalid();
85     }
86
87     public void testInvalidPop() {
88         mn.visitInsn(LCONST_0);
89         mn.visitInsn(POP);
90         assertInvalid();
91     }
92
93     public void testInvalidPop2() {
94         mn.visitInsn(LCONST_0);
95         mn.visitInsn(ICONST_0);
96         mn.visitInsn(POP2);
97         assertInvalid();
98     }
99
100     public void testInvalidDup() {
101         mn.visitInsn(LCONST_0);
102         mn.visitInsn(DUP);
103         assertInvalid();
104     }
105
106     public void testInvalidDupx1() {
107         mn.visitInsn(LCONST_0);
108         mn.visitInsn(ICONST_0);
109         mn.visitInsn(DUP_X1);
110         assertInvalid();
111     }
112
113     public void testInvalidDupx2() {
114         mn.visitInsn(LCONST_0);
115         mn.visitInsn(ICONST_0);
116         mn.visitInsn(ICONST_0);
117         mn.visitInsn(DUP_X2);
118         assertInvalid();
119     }
120
121     public void testInvalidDup2() {
122         mn.visitInsn(LCONST_0);
123         mn.visitInsn(ICONST_0);
124         mn.visitInsn(DUP2);
125         assertInvalid();
126     }
127
128     public void testInvalidDup2x1() {
129         mn.visitInsn(LCONST_0);
130         mn.visitInsn(ICONST_0);
131         mn.visitInsn(ICONST_0);
132         mn.visitInsn(DUP2_X1);
133         assertInvalid();
134     }
135
136     public void testInvalidDup2x2() {
137         mn.visitInsn(LCONST_0);
138         mn.visitInsn(ICONST_0);
139         mn.visitInsn(ICONST_0);
140         mn.visitInsn(ICONST_0);
141         mn.visitInsn(DUP2_X2);
142         assertInvalid();
143     }
144
145     public void testInvalidSwap() {
146         mn.visitInsn(LCONST_0);
147         mn.visitInsn(ICONST_0);
148         mn.visitInsn(SWAP);
149         assertInvalid();
150     }
151
152     public void testInvalidGetLocal() {
153         mn.visitVarInsn(ALOAD, 10);
154         assertInvalid();
155     }
156
157     public void testInvalidSetLocal() {
158         mn.visitInsn(ACONST_NULL);
159         mn.visitVarInsn(ASTORE, 10);
160         assertInvalid();
161     }
162
163     public void testInvalidEmptyStack() {
164         mn.visitInsn(POP);
165         assertInvalid();
166     }
167
168     public void testInvalidFullStack() {
169         mn.visitInsn(ICONST_0);
170         mn.visitInsn(ICONST_0);
171         mn.visitInsn(ICONST_0);
172         mn.visitInsn(ICONST_0);
173         mn.visitInsn(ICONST_0);
174         mn.visitInsn(ICONST_0);
175         mn.visitInsn(ICONST_0);
176         mn.visitInsn(ICONST_0);
177         mn.visitInsn(ICONST_0);
178         mn.visitInsn(ICONST_0);
179         mn.visitInsn(ICONST_0);
180         assertInvalid();
181     }
182
183     public void testInconsistentStackHeights() {
184         Label l0 = new Label();
185         mn.visitInsn(ICONST_0);
186         mn.visitJumpInsn(IFEQ, l0);
187         mn.visitInsn(ICONST_0);
188         mn.visitLabel(l0);
189         assertInvalid();
190     }
191
192     public void testInvalidNewArray() {
193         mn.visitInsn(ICONST_1);
194         mn.visitIntInsn(NEWARRAY, -1);
195         assertInvalid();
196     }
197
198     public void testInvalidAload() {
199         mn.visitInsn(ICONST_0);
200         mn.visitVarInsn(ISTORE, 1);
201         mn.visitVarInsn(ALOAD, 1);
202         assertInvalid();
203     }
204
205     public void testInvalidAstore() {
206         mn.visitInsn(ICONST_0);
207         mn.visitVarInsn(ASTORE, 1);
208         assertInvalid();
209     }
210
211     public void testInvalidIstore() {
212         mn.visitInsn(ACONST_NULL);
213         mn.visitVarInsn(ISTORE, 1);
214         assertInvalid();
215     }
216
217     public void testInvalidCheckcast() {
218         mn.visitInsn(ICONST_0);
219         mn.visitTypeInsn(CHECKCAST, "java/lang/String");
220         assertInvalid();
221     }
222
223     public void testInvalidArraylength() {
224         mn.visitInsn(ICONST_0);
225         mn.visitInsn(ARRAYLENGTH);
226         assertInvalid();
227     }
228
229     public void testInvalidAthrow() {
230         mn.visitInsn(ICONST_0);
231         mn.visitInsn(ATHROW);
232         assertInvalid();
233     }
234
235     public void testInvalidIneg() {
236         mn.visitInsn(FCONST_0);
237         mn.visitInsn(INEG);
238         assertInvalid();
239     }
240
241     public void testInvalidIadd() {
242         mn.visitInsn(FCONST_0);
243         mn.visitInsn(ICONST_0);
244         mn.visitInsn(IADD);
245         assertInvalid();
246     }
247
248     public void testInvalidIsub() {
249         mn.visitInsn(ICONST_0);
250         mn.visitInsn(FCONST_0);
251         mn.visitInsn(ISUB);
252         assertInvalid();
253     }
254
255     public void testInvalidIastore() {
256         mn.visitInsn(ICONST_1);
257         mn.visitIntInsn(NEWARRAY, T_INT);
258         mn.visitInsn(FCONST_0);
259         mn.visitInsn(ICONST_0);
260         mn.visitInsn(IASTORE);
261         assertInvalid();
262     }
263
264     public void testInvalidFastore() {
265         mn.visitInsn(ICONST_1);
266         mn.visitIntInsn(NEWARRAY, T_FLOAT);
267         mn.visitInsn(ICONST_0);
268         mn.visitInsn(ICONST_0);
269         mn.visitInsn(FASTORE);
270         assertInvalid();
271     }
272
273     public void testInvalidLastore() {
274         mn.visitInsn(ICONST_1);
275         mn.visitInsn(ICONST_0);
276         mn.visitInsn(LCONST_0);
277         mn.visitInsn(LASTORE);
278         assertInvalid();
279     }
280
281     public void testInvalidMultianewarray() {
282         mn.visitInsn(FCONST_1);
283         mn.visitInsn(ICONST_2);
284         mn.visitMultiANewArrayInsn("[[I", 2);
285         assertInvalid();
286     }
287
288     public void testInvalidInvokevirtual() {
289         mn.visitInsn(ACONST_NULL);
290         mn.visitTypeInsn(CHECKCAST, "java/lang/Object");
291         mn.visitMethodInsn(INVOKEVIRTUAL, "java/util/List", "size", "()I");
292         assertInvalid();
293     }
294
295     public void testInvalidInvokeinterface() {
296         mn.visitInsn(ACONST_NULL);
297         mn.visitTypeInsn(CHECKCAST, "java/util/List");
298         mn.visitInsn(FCONST_0);
299         mn.visitMethodInsn(INVOKEINTERFACE,
300                 "java/util/List",
301                 "get",
302                 "(I)Ljava/lang/Object;");
303         assertInvalid();
304     }
305
306     public void testInvalidRet() {
307         mn.visitVarInsn(RET, 1);
308         assertInvalid();
309     }
310
311     public void testInvalidFalloff() {
312         mn.visitMaxs(10, 10);
313         try {
314             a.analyze("C", mn);
315             fail();
316         } catch (AnalyzerException e) {
317         } catch (RuntimeException JavaDoc e) {
318         }
319     }
320
321     public void testInvalidSubroutineFalloff() {
322         Label l0 = new Label();
323         Label l1 = new Label();
324         mn.visitJumpInsn(GOTO, l0);
325         mn.visitLabel(l1);
326         mn.visitVarInsn(ASTORE, 1);
327         mn.visitVarInsn(RET, 1);
328         mn.visitLabel(l0);
329         mn.visitJumpInsn(JSR, l1);
330         mn.visitMaxs(10, 10);
331         try {
332             a.analyze("C", mn);
333             fail();
334         } catch (AnalyzerException e) {
335         } catch (RuntimeException JavaDoc e) {
336         }
337     }
338
339     public void testNestedSubroutines() throws AnalyzerException {
340         Label l0 = new Label();
341         Label l1 = new Label();
342         mn.visitJumpInsn(JSR, l0);
343         mn.visitInsn(RETURN);
344         mn.visitLabel(l0);
345         mn.visitVarInsn(ASTORE, 1);
346         mn.visitJumpInsn(JSR, l1);
347         mn.visitJumpInsn(JSR, l1);
348         mn.visitVarInsn(RET, 1);
349         mn.visitLabel(l1);
350         mn.visitVarInsn(ASTORE, 2);
351         mn.visitVarInsn(RET, 2);
352         assertValid();
353     }
354
355     public void testSubroutineLocalsAccess() throws AnalyzerException {
356         MethodVisitor mv = mn;
357         mv.visitCode();
358         Label l0 = new Label();
359         Label l1 = new Label();
360         Label l2 = new Label();
361         Label l3 = new Label();
362         mv.visitTryCatchBlock(l0, l0, l1, null);
363         mv.visitTryCatchBlock(l0, l2, l2, "java/lang/RuntimeException");
364         mv.visitLabel(l0);
365         mv.visitJumpInsn(JSR, l3);
366         mv.visitInsn(RETURN);
367         mv.visitLabel(l1);
368         mv.visitVarInsn(ASTORE, 1);
369         mv.visitJumpInsn(JSR, l3);
370         mv.visitVarInsn(ALOAD, 1);
371         mv.visitInsn(ATHROW);
372         mv.visitLabel(l3);
373         mv.visitVarInsn(ASTORE, 2);
374         mv.visitInsn(ACONST_NULL);
375         mv.visitVarInsn(ASTORE, 3);
376         mv.visitVarInsn(RET, 2);
377         mv.visitLabel(l2);
378         mv.visitVarInsn(ASTORE, 4);
379         mv.visitVarInsn(ALOAD, 4);
380         mv.visitInsn(ATHROW);
381         assertValid();
382     }
383
384     public void _testOverlappingSubroutines() {
385         // TODO currently Analyzer can not detect this situation. The problem
386
// is that other overlapping subroutine situations are valid, such as
387
// when a nested subroutine implicitely returns to its parent
388
// subroutine, without a RET.
389
Label l0 = new Label();
390         Label l1 = new Label();
391         Label l2 = new Label();
392         mn.visitJumpInsn(JSR, l0);
393         mn.visitJumpInsn(JSR, l1);
394         mn.visitInsn(RETURN);
395         mn.visitLabel(l0);
396         mn.visitVarInsn(ASTORE, 1);
397         mn.visitJumpInsn(GOTO, l2);
398         mn.visitLabel(l1);
399         mn.visitVarInsn(ASTORE, 1);
400         mn.visitLabel(l2);
401         mn.visitVarInsn(RET, 1);
402         assertInvalid();
403     }
404
405     public void testMerge() throws AnalyzerException {
406         Label l0 = new Label();
407         mn.visitVarInsn(ALOAD, 0);
408         mn.visitVarInsn(ASTORE, 1);
409         mn.visitInsn(ACONST_NULL);
410         mn.visitTypeInsn(CHECKCAST, "java/lang/Number");
411         mn.visitVarInsn(ASTORE, 2);
412         mn.visitVarInsn(ALOAD, 0);
413         mn.visitVarInsn(ASTORE, 3);
414         mn.visitLabel(l0);
415         mn.visitInsn(ACONST_NULL);
416         mn.visitTypeInsn(CHECKCAST, "java/lang/Number");
417         mn.visitVarInsn(ASTORE, 1);
418         mn.visitVarInsn(ALOAD, 0);
419         mn.visitVarInsn(ASTORE, 2);
420         mn.visitInsn(ACONST_NULL);
421         mn.visitTypeInsn(CHECKCAST, "java/lang/Integer");
422         mn.visitVarInsn(ASTORE, 3);
423         mn.visitJumpInsn(GOTO, l0);
424         assertValid();
425     }
426
427     public void testClassNotFound() {
428         Label l0 = new Label();
429         mn.visitVarInsn(ALOAD, 0);
430         mn.visitVarInsn(ASTORE, 1);
431         mn.visitLabel(l0);
432         mn.visitInsn(ACONST_NULL);
433         mn.visitTypeInsn(CHECKCAST, "D");
434         mn.visitVarInsn(ASTORE, 1);
435         mn.visitJumpInsn(GOTO, l0);
436         mn.visitMaxs(10, 10);
437         try {
438             a.analyze("C", mn);
439             fail();
440         } catch (Exception JavaDoc e) {
441         }
442     }
443 }
444
Popular Tags