Skip to content

Commit

Permalink
Merge pull request #32 from EpicPlayerA10/fix/wide-instruction-size
Browse files Browse the repository at this point in the history
Fix computed size of WideInstruction
  • Loading branch information
Col-E authored Nov 24, 2024
2 parents 50392e2 + 7dffd6f commit 7e892b6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ public Instruction getBacking() {

@Override
public int computeSize() {
switch (backing.getOpcode()) {
case Opcodes.IINC:
return 6;
case Opcodes.LLOAD:
case Opcodes.DLOAD:
case Opcodes.LSTORE:
case Opcodes.DSTORE:
return 4;
default:
return 3;
if (backing.getOpcode() == Opcodes.IINC) {
// IINC is unique in that it becomes 6 bytes:
// https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-6.html#jvms-6.5.wide
//
// opcode
// iinc
// indexbyte1
// indexbyte2
// constbyte1
// constbyte2
return 6;
}
// opcode
// input opcode
// indexbyte1
// indexbyte2
return 4;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package software.coley.cafedude;

import software.coley.cafedude.classfile.ClassFile;
import software.coley.cafedude.io.ClassFileReader;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.coley.cafedude.io.ClassFileWriter;

import java.io.File;
import java.io.IOException;
Expand All @@ -23,7 +25,8 @@ public void testNoErrors(File sample) {
try {
byte[] code = Files.readAllBytes(sample.toPath());
assertDoesNotThrow(() -> {
new ClassFileReader().read(code);
ClassFile classFile = new ClassFileReader().read(code);
new ClassFileWriter().write(classFile);
}, "Library crashes when reading: " + sample.getName());
} catch (IOException error) {
fail("Failed to read class, IO error", error);
Expand Down
20 changes: 6 additions & 14 deletions core/src/test/java/software/coley/cafedude/SizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,18 @@ void testIincInstruction() {

@Test
void testWideInstruction() {
int[] four_byte_ops = {
LLOAD, DLOAD, LSTORE, DSTORE
};
int[] three_byte_ops = {
RET, ILOAD, FLOAD, ALOAD, ISTORE, FSTORE, ASTORE
};

// IINC is 6 bytes
WideInstruction instruction = new WideInstruction(new IincInstruction(0, 0));
assertEquals(6, instruction.computeSize());

int[] four_byte_ops = {
LLOAD, DLOAD, LSTORE, DSTORE, RET, ILOAD, FLOAD, ALOAD, ISTORE, FSTORE, ASTORE
};

// Wide type variable insns are 4
for (int op : four_byte_ops) {
instruction = new WideInstruction(new IntOperandInstruction(op, 0));
assertEquals(4, instruction.computeSize());
}
// Normal variable insns are 3
for (int op : three_byte_ops) {
instruction = new WideInstruction(new IntOperandInstruction(op, 0));
assertEquals(3, instruction.computeSize());
WideInstruction instruction1 = new WideInstruction(new IntOperandInstruction(op, 0));
assertEquals(4, instruction1.computeSize());
}
}

Expand Down
Binary file not shown.

0 comments on commit 7e892b6

Please sign in to comment.