Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,12 @@ static inline int
get_line_delta(_PyCoLineInstrumentationData *line_data, int index)
{
uint8_t *ptr = &line_data->data[index*line_data->bytes_per_entry+1];
uint32_t value = *ptr;
assert(line_data->bytes_per_entry >= 2);
if (line_data->bytes_per_entry > 2) {
uint32_t value = *ptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could assert here that bytes_per_entry is within range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't easily assert anything here, as we don't know the max line number without traversing the entire locations table.
I've added an assert to set_line_delta instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that bytes_per_entry is between 1 and 4.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bytes_per_entry is between 2 and 5 because it includes the original opcode)

for (int idx = 2; idx < line_data->bytes_per_entry; idx++) {
ptr++;
value = (value << 8) | *ptr;
if (line_data->bytes_per_entry > 3) {
ptr++;
value = (value << 8) | *ptr;
if (line_data->bytes_per_entry > 4) {
assert(line_data->bytes_per_entry == 5);
ptr++;
value = (value << 8) | *ptr;
}
}
int shift = (idx-1)*8;
value |= ((uint32_t)(*ptr)) << shift;
}
assert(value < INT_MAX);
/* NO_LINE is stored as zero. */
Expand All @@ -375,22 +367,14 @@ set_line_delta(_PyCoLineInstrumentationData *line_data, int index, int line_delt
assert(line_delta >= NO_LINE);
uint32_t adjusted = line_delta - NO_LINE;
uint8_t *ptr = &line_data->data[index*line_data->bytes_per_entry+1];
assert(adjusted < (1ULL << (line_data->bytes_per_entry*8)));
assert(adjusted < (1ULL << ((line_data->bytes_per_entry-1)*8)));
assert(line_data->bytes_per_entry >= 2);
if (line_data->bytes_per_entry > 2) {
if (line_data->bytes_per_entry > 3) {
if (line_data->bytes_per_entry > 4) {
assert(line_data->bytes_per_entry == 5);
*ptr = adjusted >> 24;
ptr++;
}
*ptr = (adjusted >> 16) & 255;
ptr++;
}
*ptr = (adjusted >> 8) & 255;
*ptr = adjusted & 0xff;
for (int idx = 2; idx < line_data->bytes_per_entry; idx++) {
ptr++;
adjusted >>= 8;
*ptr = adjusted & 0xff;
}
*ptr = adjusted & 255;
}

#ifdef INSTRUMENT_DEBUG
Expand Down Expand Up @@ -1541,9 +1525,10 @@ initialize_lines(PyCodeObject *code, int bytes_per_entry)
case END_FOR:
case END_SEND:
case RESUME:
case POP_ITER:
/* END_FOR cannot start a line, as it is skipped by FOR_ITER
* END_SEND cannot start a line, as it is skipped by SEND
* RESUME must not be instrumented with INSTRUMENT_LINE */
* RESUME and POP_ITER must not be instrumented with INSTRUMENT_LINE */
set_original_opcode(line_data, i, 0);
break;
default:
Expand Down Expand Up @@ -1610,7 +1595,10 @@ initialize_lines(PyCodeObject *code, int bytes_per_entry)
}
assert(target >= 0);
if (get_line_delta(line_data, target) != NO_LINE) {
set_original_opcode(line_data, target, _Py_GetBaseCodeUnit(code, target).op.code);
int opcode = _Py_GetBaseCodeUnit(code, target).op.code;
if (opcode != POP_ITER) {
set_original_opcode(line_data, target, opcode);
}
}
}
/* Scan exception table */
Expand Down Expand Up @@ -1733,7 +1721,7 @@ update_instrumentation_data(PyCodeObject *code, PyInterpreterState *interp)
else {
bytes_per_entry = 5;
}
code->_co_monitoring->lines = PyMem_Malloc(1 + code_len *bytes_per_entry);
code->_co_monitoring->lines = PyMem_Malloc(1 + code_len * bytes_per_entry);
if (code->_co_monitoring->lines == NULL) {
PyErr_NoMemory();
return -1;
Expand Down
Loading