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

Commit ed9d2b9

Browse files
authored
fix: address some cases where jumps were not being updated (#83)
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.
1 parent 8dcaf94 commit ed9d2b9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/googleclouddebugger/bytecode_manipulator.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ static PythonOpcodeType GetOpcodeType(uint8_t opcode) {
132132
#if PY_VERSION_HEX < 0x03080000
133133
// Removed in Python 3.8.
134134
case CONTINUE_LOOP:
135+
#endif
136+
#if PY_VERSION_HEX >= 0x03090000
137+
case JUMP_IF_NOT_EXC_MATCH:
135138
#endif
136139
return BRANCH_ABSOLUTE_OPCODE;
137140

@@ -144,10 +147,18 @@ static PythonOpcodeType GetOpcodeType(uint8_t opcode) {
144147
static int GetBranchTarget(int offset, PythonInstruction instruction) {
145148
switch (GetOpcodeType(instruction.opcode)) {
146149
case BRANCH_DELTA_OPCODE:
150+
#if PY_VERSION_HEX < 0x030A0000
147151
return offset + instruction.size + instruction.argument;
152+
#else
153+
return offset + instruction.size + instruction.argument * 2;
154+
#endif
148155

149156
case BRANCH_ABSOLUTE_OPCODE:
157+
#if PY_VERSION_HEX < 0x030A0000
150158
return instruction.argument;
159+
#else
160+
return instruction.argument * 2;
161+
#endif
151162

152163
default:
153164
DCHECK(false) << "Not a branch instruction";
@@ -428,13 +439,21 @@ static bool InsertAndUpdateBranchInstructions(
428439
// argument of 0 even when it is not required. This needs to be taken
429440
// into account when calculating the target of a branch instruction.
430441
int inst_size = std::max(instruction.size, it->original_size);
442+
#if PY_VERSION_HEX < 0x030A0000
431443
int32_t target = it->current_offset + inst_size + arg;
444+
#else
445+
int32_t target = it->current_offset + inst_size + arg * 2;
446+
#endif
432447
need_to_update = it->current_offset < insertion.current_offset &&
433448
insertion.current_offset < target;
434449
} else if (opcode_type == BRANCH_ABSOLUTE_OPCODE) {
435450
// For absolute branches, the argument needs to be updated if the
436451
// insertion before the target.
452+
#if PY_VERSION_HEX < 0x030A0000
437453
need_to_update = insertion.current_offset < arg;
454+
#else
455+
need_to_update = insertion.current_offset < arg * 2;
456+
#endif
438457
}
439458

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

0 commit comments

Comments
 (0)