View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test for https://issues.apache.org/jira/browse/BCEL-267 "Race conditions on static fields in BranchHandle and
29   * InstructionHandle".
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       * Asserts that branch handles can be added an instruction list, without corrupting the list.
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(); // this initializes caching of unused instruction handles
69          }
70      }
71  
72      /**
73       * Assert that opposite next/prev pairs always match.
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       * Asserts that instruction handles can be added an instruction list, without corrupting the list.
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(); // this initializes caching of unused instruction handles
114         }
115     }
116 
117     /**
118      * Concurrently run the given runnable in two threads.
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      * Assert that two independent instruction lists can be modified concurrently. Here: inserting branch instructions.
135      */
136     @Test
137     public void testBranchHandle() throws Throwable {
138         perform(HandleTestCase::branchHandles);
139     }
140 
141     /**
142      * Assert that two independent instruction lists can be modified concurrently. Here: inserting regular instructions.
143      */
144     @Test
145     public void testInstructionHandle() throws Throwable {
146         perform(HandleTestCase::handles);
147     }
148 }