Skip to content

Commit

Permalink
Branch instructions were marked as invalid in code attributes if swit…
Browse files Browse the repository at this point in the history
…ch instructions were present in between the branch instruction and the target instruction
  • Loading branch information
ingokegel committed Feb 25, 2018
1 parent 4c0c944 commit cfd5660
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
Expand Up @@ -41,25 +41,10 @@ class ByteCodeDocument(styles: StyleContext, private val attribute: CodeAttribut
}

private fun verifyOffsets(instructions: ArrayList<Instruction>) {
instructions.forEachIndexed { i, instruction ->
if (instruction is AbstractBranchInstruction) {
var branchOffset = instruction.branchOffset
var targetDistance = 0
if (branchOffset > 0) {
branchOffset -= instruction.size
while (branchOffset > 0 && i + targetDistance + 1 < instructions.size) {
++targetDistance
branchOffset -= instructions[i + targetDistance].size
}
} else {
while (branchOffset < 0 && i + targetDistance > 0) {
--targetDistance
branchOffset += instructions[i + targetDistance].size
}
}
if (branchOffset != 0) {
invalidBranches.add(instruction)
}
instructions.filterIsInstance<AbstractBranchInstruction>().forEach { instruction ->
val targetOffset = instruction.branchOffset + instruction.offset
if (instructions.none { it.offset == targetOffset }) {
invalidBranches.add(instruction)
}
}
}
Expand Down
Expand Up @@ -27,6 +27,14 @@ open class Instruction(val opcode: Opcode) {
open val size: Int
get() = 1

/**
* Get the padded size in bytes of this instruction. This will be the same as
* `size` except for instances of [PaddedInstruction].
* @param offset the offset at which this instruction is found.
* @return the padded size in bytes
*/
open fun getPaddedSize(offset: Int): Int = size

/**
* Read this instruction from the given ByteCodeInput.
* Expects ByteCodeInput to be in JVM class file format and just
Expand Down
Expand Up @@ -22,7 +22,7 @@ open class PaddedInstruction(opcode: Opcode) : Instruction(opcode) {
* @param offset the offset at which this instruction is found.
* @return the padded size in bytes
*/
fun getPaddedSize(offset: Int): Int = size + paddingBytes(offset + 1)
override fun getPaddedSize(offset: Int): Int = size + paddingBytes(offset + 1)

override fun read(input: ByteCodeInput) {
super.read(input)
Expand Down

0 comments on commit cfd5660

Please sign in to comment.