Skip to main content

il_instruction_size

Function il_instruction_size 

pub fn il_instruction_size(il_bytes: &[u8], offset: usize) -> usize
Expand description

Calculates the size of an instruction at the given offset in IL bytecode.

This function uses the INSTRUCTIONS and INSTRUCTIONS_FE tables to determine the correct instruction size based on the opcode and operand type. It handles:

  • Single-byte opcodes (0x00-0xE0)
  • Two-byte opcodes with 0xFE prefix
  • Variable-length switch instructions

§Arguments

  • il_bytes - The IL bytecode buffer
  • offset - The offset of the instruction to measure

§Returns

The total size of the instruction in bytes (opcode + operand).

§Examples

use dotscope::assembly::il_instruction_size;

// nop (0x00) - 1 byte, no operand
assert_eq!(il_instruction_size(&[0x00], 0), 1);

// ldc.i4 (0x20) - 1 byte opcode + 4 byte operand = 5 bytes
assert_eq!(il_instruction_size(&[0x20, 0x01, 0x00, 0x00, 0x00], 0), 5);

// ldarg (0xFE 0x09) - 2 byte opcode + 2 byte operand = 4 bytes
assert_eq!(il_instruction_size(&[0xFE, 0x09, 0x00, 0x00], 0), 4);

// switch with 2 targets - 1 + 4 + (2 * 4) = 13 bytes
let switch_bytes = [0x45, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00];
assert_eq!(il_instruction_size(&switch_bytes, 0), 13);