View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.util;
21  
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.File;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  
29  import org.apache.bcel.Const;
30  import org.apache.bcel.classfile.JavaClass;
31  import org.apache.bcel.generic.ClassGen;
32  import org.apache.bcel.generic.ConstantPoolGen;
33  import org.apache.bcel.generic.ILOAD;
34  import org.apache.bcel.generic.InstructionFactory;
35  import org.apache.bcel.generic.InstructionList;
36  import org.apache.bcel.generic.MethodGen;
37  import org.apache.bcel.generic.Type;
38  import org.junit.jupiter.api.Test;
39  
40  class CodeHTMLNarrowingTest {
41  
42      /**
43       * A wide local-variable index is a u2 in the byte code; disassembling a class whose index is >= 0x8000 must render the
44       * real slot number, not a sign-extended negative. The reference disassembler {@code Utility.codeToString} reads it via
45       * {@code readUnsignedShort}.
46       */
47      @Test
48      void testWideLocalVariableIndexIsUnsigned() throws Exception {
49          final int index = 40000; // > 0x7FFF so a signed read wraps negative
50          final ClassGen cg = new ClassGen("Wide", "java.lang.Object", "Wide.java", Const.ACC_PUBLIC, null);
51          final ConstantPoolGen cp = cg.getConstantPool();
52          final InstructionList il = new InstructionList();
53          il.append(new ILOAD(index)); // emitted as WIDE + iload + u2 index
54          il.append(InstructionFactory.createReturn(Type.INT));
55          final MethodGen mg = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, Type.NO_ARGS, null, "m", "Wide", il, cp);
56          mg.setMaxStack(1);
57          mg.setMaxLocals(index + 1);
58          cg.addMethod(mg.getMethod());
59          final JavaClass jc = cg.getJavaClass();
60  
61          final File outputDir = new File("target/test-output/html-narrowing");
62          if (!outputDir.mkdirs()) {
63              assertTrue(outputDir.isDirectory());
64          }
65          new Class2HTML(jc, outputDir.getAbsolutePath() + File.separator);
66  
67          final String code = new String(Files.readAllBytes(new File(outputDir, "Wide_code.html").toPath()), StandardCharsets.UTF_8);
68          assertTrue(code.contains("%" + index), "expected the wide index rendered as " + index);
69          assertFalse(code.contains("%" + (short) index), "wide index was sign-extended to " + (short) index);
70      }
71  }