From cf7b49ccf0b45a5790f89fe02f2a3ed04b733064 Mon Sep 17 00:00:00 2001 From: Naveed Khan Date: Mon, 13 Jul 2026 01:43:59 +0530 Subject: [PATCH] set length in INVOKEDYNAMIC constructor The constructor never set the length, so a generated invokedynamic reported getLength() 3 while dump() writes 5 bytes, shifting the offsets of every following instruction in InstructionList.setPositions(). Match INVOKEINTERFACE, MULTIANEWARRAY and this class's own initFromFile. --- .../apache/bcel/generic/INVOKEDYNAMIC.java | 1 + .../bcel/generic/INVOKEDYNAMICTest.java | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java diff --git a/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java b/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java index beba53241c..7f7e9992be 100644 --- a/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java +++ b/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java @@ -51,6 +51,7 @@ public class INVOKEDYNAMIC extends InvokeInstruction { */ public INVOKEDYNAMIC(final int index) { super(Const.INVOKEDYNAMIC, index); + super.setLength(5); } /** diff --git a/src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java b/src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java new file mode 100644 index 0000000000..3881f21c47 --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.bcel.generic; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link INVOKEDYNAMIC}. + * + * @see The invokedynamic instruction + */ +class INVOKEDYNAMICTest { + + /** + * {@code invokedynamic} occupies 5 bytes (opcode, u2 index, two zero bytes), which is what {@code dump()} writes and + * {@code initFromFile()} sets, so the constructed instruction must also report a length of 5. + */ + @Test + void testConstructorSetsLength() { + assertEquals(5, new INVOKEDYNAMIC(1).getLength()); + } + + /** + * {@link InstructionList#setPositions()} adds up {@code getLength()} to place each instruction, so an understated + * invokedynamic length shifts the offset of every following instruction. + */ + @Test + void testInstructionListPositionsAccountForFullLength() { + final InstructionList il = new InstructionList(); + il.append(new NOP()); + il.append(new INVOKEDYNAMIC(1)); + final InstructionHandle after = il.append(new NOP()); + il.setPositions(); + // NOP (1) + invokedynamic (5) places the following NOP at offset 6 + assertEquals(6, after.getPosition()); + } +}