1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel;
19
20 import org.apache.bcel.generic.GOTO;
21 import org.apache.bcel.generic.ILOAD;
22 import org.apache.bcel.generic.InstructionHandle;
23 import org.apache.bcel.generic.InstructionList;
24 import org.apache.bcel.generic.NOP;
25 import org.junit.jupiter.api.Test;
26
27
28
29
30
31 public class HandleTestCase {
32
33 static Throwable exception;
34 static final int MAXI = 100;
35 static final int MAXJ = 1000;
36
37
38
39
40 static void branchHandles() {
41 for (int i = 0; i < MAXI; i++) {
42 final InstructionList list = new InstructionList();
43 final InstructionHandle start = list.append(new NOP());
44 try {
45 for (int j = 0; j < MAXJ; j++) {
46 list.append(new GOTO(start));
47 }
48 final InstructionHandle[] instructionHandles = list.getInstructionHandles();
49 for (int j = 0; j < instructionHandles.length; j++) {
50 final InstructionHandle handle = instructionHandles[j];
51 if (j > 0) {
52 checkLinkage(handle, j);
53 if (start != ((GOTO) handle.getInstruction()).getTarget()) {
54 final AssertionError error = new AssertionError("unexpected instruction at index " + j);
55 exception = error;
56 throw error;
57 }
58 }
59 }
60 if (exception != null) {
61 return;
62 }
63 } catch (final NullPointerException e) {
64 System.out.println("NPE at i=" + i);
65 exception = e;
66 throw e;
67 }
68 list.dispose();
69 }
70 }
71
72
73
74
75 static void checkLinkage(final InstructionHandle ih, final int index) {
76 final InstructionHandle prev = ih.getPrev();
77 final InstructionHandle next = ih.getNext();
78 if (prev != null && prev.getNext() != ih || next != null && next.getPrev() != ih) {
79 final AssertionError error = new AssertionError("corrupt instruction list at index " + index);
80 exception = error;
81 throw error;
82 }
83 }
84
85
86
87
88 static void handles() {
89 for (int i = 0; i < MAXI; i++) {
90 final InstructionList list = new InstructionList();
91 try {
92 for (int j = 0; j < MAXJ; j++) {
93 list.append(new ILOAD(j));
94 }
95 final InstructionHandle[] instructionHandles = list.getInstructionHandles();
96 for (int j = 0; j < instructionHandles.length; j++) {
97 final InstructionHandle handle = instructionHandles[j];
98 checkLinkage(handle, j);
99 if (j != ((ILOAD) handle.getInstruction()).getIndex()) {
100 final AssertionError error = new AssertionError("unexpected instruction at index " + j);
101 exception = error;
102 throw error;
103 }
104 }
105 if (exception != null) {
106 return;
107 }
108 } catch (final NullPointerException e) {
109 System.out.println("NPE at i=" + i);
110 exception = e;
111 throw e;
112 }
113 list.dispose();
114 }
115 }
116
117
118
119
120 private void perform(final Runnable r) throws Throwable {
121 exception = null;
122 final Thread t1 = new Thread(r);
123 final Thread t2 = new Thread(r);
124 t1.start();
125 t2.start();
126 t1.join();
127 t2.join();
128 if (exception != null) {
129 throw exception;
130 }
131 }
132
133
134
135
136 @Test
137 public void testBranchHandle() throws Throwable {
138 perform(HandleTestCase::branchHandles);
139 }
140
141
142
143
144 @Test
145 public void testInstructionHandle() throws Throwable {
146 perform(HandleTestCase::handles);
147 }
148 }