Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
fix: address some cases where jumps were not being updated (#83)
Browse files Browse the repository at this point in the history
Affects Python 3.10 only; in some situations jump instructions were not updated with new targets due to the targets being interpretted as memory offsets instead of instruction offsets.
  • Loading branch information
mctavish authored Apr 4, 2023
1 parent 8dcaf94 commit ed9d2b9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/googleclouddebugger/bytecode_manipulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ static PythonOpcodeType GetOpcodeType(uint8_t opcode) {
#if PY_VERSION_HEX < 0x03080000
// Removed in Python 3.8.
case CONTINUE_LOOP:
#endif
#if PY_VERSION_HEX >= 0x03090000
case JUMP_IF_NOT_EXC_MATCH:
#endif
return BRANCH_ABSOLUTE_OPCODE;

Expand All @@ -144,10 +147,18 @@ static PythonOpcodeType GetOpcodeType(uint8_t opcode) {
static int GetBranchTarget(int offset, PythonInstruction instruction) {
switch (GetOpcodeType(instruction.opcode)) {
case BRANCH_DELTA_OPCODE:
#if PY_VERSION_HEX < 0x030A0000
return offset + instruction.size + instruction.argument;
#else
return offset + instruction.size + instruction.argument * 2;
#endif

case BRANCH_ABSOLUTE_OPCODE:
#if PY_VERSION_HEX < 0x030A0000
return instruction.argument;
#else
return instruction.argument * 2;
#endif

default:
DCHECK(false) << "Not a branch instruction";
Expand Down Expand Up @@ -428,13 +439,21 @@ static bool InsertAndUpdateBranchInstructions(
// argument of 0 even when it is not required. This needs to be taken
// into account when calculating the target of a branch instruction.
int inst_size = std::max(instruction.size, it->original_size);
#if PY_VERSION_HEX < 0x030A0000
int32_t target = it->current_offset + inst_size + arg;
#else
int32_t target = it->current_offset + inst_size + arg * 2;
#endif
need_to_update = it->current_offset < insertion.current_offset &&
insertion.current_offset < target;
} else if (opcode_type == BRANCH_ABSOLUTE_OPCODE) {
// For absolute branches, the argument needs to be updated if the
// insertion before the target.
#if PY_VERSION_HEX < 0x030A0000
need_to_update = insertion.current_offset < arg;
#else
need_to_update = insertion.current_offset < arg * 2;
#endif
}

// If we are inserting the original method call instructions, we want to
Expand Down

0 comments on commit ed9d2b9

Please sign in to comment.