Skip to content

Commit 6adcb62

Browse files
markshannonGlyphack
authored andcommitted
pythonGH-111485: Increment next_instr consistently at the start of the instruction. (pythonGH-111486)
1 parent 8f4cc54 commit 6adcb62

11 files changed

+1079
-521
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_generated_cases.py

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ def test_inst_no_args(self):
138138
"""
139139
output = """
140140
TARGET(OP) {
141+
frame->instr_ptr = next_instr;
142+
next_instr += 1;
143+
INSTRUCTION_STATS(OP);
141144
spam();
142145
DISPATCH();
143146
}
@@ -152,6 +155,9 @@ def test_inst_one_pop(self):
152155
"""
153156
output = """
154157
TARGET(OP) {
158+
frame->instr_ptr = next_instr;
159+
next_instr += 1;
160+
INSTRUCTION_STATS(OP);
155161
PyObject *value;
156162
value = stack_pointer[-1];
157163
spam();
@@ -169,6 +175,9 @@ def test_inst_one_push(self):
169175
"""
170176
output = """
171177
TARGET(OP) {
178+
frame->instr_ptr = next_instr;
179+
next_instr += 1;
180+
INSTRUCTION_STATS(OP);
172181
PyObject *res;
173182
spam();
174183
STACK_GROW(1);
@@ -186,6 +195,9 @@ def test_inst_one_push_one_pop(self):
186195
"""
187196
output = """
188197
TARGET(OP) {
198+
frame->instr_ptr = next_instr;
199+
next_instr += 1;
200+
INSTRUCTION_STATS(OP);
189201
PyObject *value;
190202
PyObject *res;
191203
value = stack_pointer[-1];
@@ -204,6 +216,9 @@ def test_binary_op(self):
204216
"""
205217
output = """
206218
TARGET(OP) {
219+
frame->instr_ptr = next_instr;
220+
next_instr += 1;
221+
INSTRUCTION_STATS(OP);
207222
PyObject *right;
208223
PyObject *left;
209224
PyObject *res;
@@ -225,6 +240,9 @@ def test_overlap(self):
225240
"""
226241
output = """
227242
TARGET(OP) {
243+
frame->instr_ptr = next_instr;
244+
next_instr += 1;
245+
INSTRUCTION_STATS(OP);
228246
PyObject *right;
229247
PyObject *left;
230248
PyObject *result;
@@ -249,6 +267,9 @@ def test_predictions_and_eval_breaker(self):
249267
"""
250268
output = """
251269
TARGET(OP1) {
270+
frame->instr_ptr = next_instr;
271+
next_instr += 1;
272+
INSTRUCTION_STATS(OP1);
252273
PREDICTED(OP1);
253274
static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
254275
PyObject *arg;
@@ -259,6 +280,9 @@ def test_predictions_and_eval_breaker(self):
259280
}
260281
261282
TARGET(OP3) {
283+
frame->instr_ptr = next_instr;
284+
next_instr += 1;
285+
INSTRUCTION_STATS(OP3);
262286
PyObject *arg;
263287
PyObject *res;
264288
arg = stack_pointer[-1];
@@ -278,6 +302,9 @@ def test_error_if_plain(self):
278302
"""
279303
output = """
280304
TARGET(OP) {
305+
frame->instr_ptr = next_instr;
306+
next_instr += 1;
307+
INSTRUCTION_STATS(OP);
281308
if (cond) goto label;
282309
DISPATCH();
283310
}
@@ -292,6 +319,9 @@ def test_error_if_plain_with_comment(self):
292319
"""
293320
output = """
294321
TARGET(OP) {
322+
frame->instr_ptr = next_instr;
323+
next_instr += 1;
324+
INSTRUCTION_STATS(OP);
295325
if (cond) goto label;
296326
DISPATCH();
297327
}
@@ -306,6 +336,9 @@ def test_error_if_pop(self):
306336
"""
307337
output = """
308338
TARGET(OP) {
339+
frame->instr_ptr = next_instr;
340+
next_instr += 1;
341+
INSTRUCTION_STATS(OP);
309342
PyObject *right;
310343
PyObject *left;
311344
PyObject *res;
@@ -326,12 +359,14 @@ def test_cache_effect(self):
326359
"""
327360
output = """
328361
TARGET(OP) {
362+
_Py_CODEUNIT *this_instr = frame->instr_ptr = next_instr;
363+
next_instr += 4;
364+
INSTRUCTION_STATS(OP);
329365
PyObject *value;
330366
value = stack_pointer[-1];
331-
uint16_t counter = read_u16(&next_instr[0].cache);
332-
uint32_t extra = read_u32(&next_instr[1].cache);
367+
uint16_t counter = read_u16(&this_instr[1].cache);
368+
uint32_t extra = read_u32(&this_instr[2].cache);
333369
STACK_SHRINK(1);
334-
next_instr += 3;
335370
DISPATCH();
336371
}
337372
"""
@@ -345,6 +380,9 @@ def test_suppress_dispatch(self):
345380
"""
346381
output = """
347382
TARGET(OP) {
383+
frame->instr_ptr = next_instr;
384+
next_instr += 1;
385+
INSTRUCTION_STATS(OP);
348386
goto somewhere;
349387
}
350388
"""
@@ -366,18 +404,24 @@ def test_macro_instruction(self):
366404
"""
367405
output = """
368406
TARGET(OP1) {
407+
_Py_CODEUNIT *this_instr = frame->instr_ptr = next_instr;
408+
next_instr += 2;
409+
INSTRUCTION_STATS(OP1);
369410
PyObject *right;
370411
PyObject *left;
371412
right = stack_pointer[-1];
372413
left = stack_pointer[-2];
373-
uint16_t counter = read_u16(&next_instr[0].cache);
414+
uint16_t counter = read_u16(&this_instr[1].cache);
374415
op1(left, right);
375-
next_instr += 1;
376416
DISPATCH();
377417
}
378418
379419
TARGET(OP) {
420+
frame->instr_ptr = next_instr;
421+
next_instr += 6;
422+
INSTRUCTION_STATS(OP);
380423
PREDICTED(OP);
424+
_Py_CODEUNIT *this_instr = next_instr - 6;
381425
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
382426
PyObject *right;
383427
PyObject *left;
@@ -387,22 +431,24 @@ def test_macro_instruction(self):
387431
right = stack_pointer[-1];
388432
left = stack_pointer[-2];
389433
{
390-
uint16_t counter = read_u16(&next_instr[0].cache);
434+
uint16_t counter = read_u16(&this_instr[1].cache);
391435
op1(left, right);
392436
}
393437
// OP2
394438
arg2 = stack_pointer[-3];
395439
{
396-
uint32_t extra = read_u32(&next_instr[3].cache);
440+
uint32_t extra = read_u32(&this_instr[4].cache);
397441
res = op2(arg2, left, right);
398442
}
399443
STACK_SHRINK(2);
400444
stack_pointer[-1] = res;
401-
next_instr += 5;
402445
DISPATCH();
403446
}
404447
405448
TARGET(OP3) {
449+
frame->instr_ptr = next_instr;
450+
next_instr += 6;
451+
INSTRUCTION_STATS(OP3);
406452
PyObject *right;
407453
PyObject *left;
408454
PyObject *arg2;
@@ -413,7 +459,6 @@ def test_macro_instruction(self):
413459
res = op3(arg2, left, right);
414460
STACK_SHRINK(2);
415461
stack_pointer[-1] = res;
416-
next_instr += 5;
417462
DISPATCH();
418463
}
419464
"""
@@ -427,6 +472,9 @@ def test_array_input(self):
427472
"""
428473
output = """
429474
TARGET(OP) {
475+
frame->instr_ptr = next_instr;
476+
next_instr += 1;
477+
INSTRUCTION_STATS(OP);
430478
PyObject *above;
431479
PyObject **values;
432480
PyObject *below;
@@ -449,6 +497,9 @@ def test_array_output(self):
449497
"""
450498
output = """
451499
TARGET(OP) {
500+
frame->instr_ptr = next_instr;
501+
next_instr += 1;
502+
INSTRUCTION_STATS(OP);
452503
PyObject *below;
453504
PyObject **values;
454505
PyObject *above;
@@ -470,6 +521,9 @@ def test_array_input_output(self):
470521
"""
471522
output = """
472523
TARGET(OP) {
524+
frame->instr_ptr = next_instr;
525+
next_instr += 1;
526+
INSTRUCTION_STATS(OP);
473527
PyObject **values;
474528
PyObject *above;
475529
values = stack_pointer - oparg;
@@ -489,6 +543,9 @@ def test_array_error_if(self):
489543
"""
490544
output = """
491545
TARGET(OP) {
546+
frame->instr_ptr = next_instr;
547+
next_instr += 1;
548+
INSTRUCTION_STATS(OP);
492549
PyObject **values;
493550
PyObject *extra;
494551
values = stack_pointer - oparg;
@@ -509,6 +566,9 @@ def test_cond_effect(self):
509566
"""
510567
output = """
511568
TARGET(OP) {
569+
frame->instr_ptr = next_instr;
570+
next_instr += 1;
571+
INSTRUCTION_STATS(OP);
512572
PyObject *cc;
513573
PyObject *input = NULL;
514574
PyObject *aa;
@@ -541,6 +601,9 @@ def test_macro_cond_effect(self):
541601
"""
542602
output = """
543603
TARGET(M) {
604+
frame->instr_ptr = next_instr;
605+
next_instr += 1;
606+
INSTRUCTION_STATS(M);
544607
PyObject *right;
545608
PyObject *middle;
546609
PyObject *left;
@@ -580,6 +643,9 @@ def test_macro_push_push(self):
580643
"""
581644
output = """
582645
TARGET(M) {
646+
frame->instr_ptr = next_instr;
647+
next_instr += 1;
648+
INSTRUCTION_STATS(M);
583649
PyObject *val1;
584650
PyObject *val2;
585651
// A
@@ -609,6 +675,9 @@ def test_override_inst(self):
609675
"""
610676
output = """
611677
TARGET(OP) {
678+
frame->instr_ptr = next_instr;
679+
next_instr += 1;
680+
INSTRUCTION_STATS(OP);
612681
ham();
613682
DISPATCH();
614683
}
@@ -627,6 +696,9 @@ def test_override_op(self):
627696
"""
628697
output = """
629698
TARGET(M) {
699+
frame->instr_ptr = next_instr;
700+
next_instr += 1;
701+
INSTRUCTION_STATS(M);
630702
ham();
631703
DISPATCH();
632704
}

Lib/test/test_monitoring.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,33 +1378,35 @@ def func():
13781378
x = 4
13791379
else:
13801380
x = 6
1381+
7
13811382

13821383
self.check_events(func, recorders = JUMP_AND_BRANCH_RECORDERS, expected = [
13831384
('branch', 'func', 2, 2),
1384-
('branch', 'func', 3, 4),
1385+
('branch', 'func', 3, 6),
13851386
('jump', 'func', 6, 2),
13861387
('branch', 'func', 2, 2),
1387-
('branch', 'func', 3, 3),
1388+
('branch', 'func', 3, 4),
13881389
('jump', 'func', 4, 2),
1389-
('branch', 'func', 2, 2)])
1390+
('branch', 'func', 2, 7)])
13901391

13911392
self.check_events(func, recorders = JUMP_BRANCH_AND_LINE_RECORDERS, expected = [
13921393
('line', 'get_events', 10),
13931394
('line', 'func', 1),
13941395
('line', 'func', 2),
13951396
('branch', 'func', 2, 2),
13961397
('line', 'func', 3),
1397-
('branch', 'func', 3, 4),
1398+
('branch', 'func', 3, 6),
13981399
('line', 'func', 6),
13991400
('jump', 'func', 6, 2),
14001401
('line', 'func', 2),
14011402
('branch', 'func', 2, 2),
14021403
('line', 'func', 3),
1403-
('branch', 'func', 3, 3),
1404+
('branch', 'func', 3, 4),
14041405
('line', 'func', 4),
14051406
('jump', 'func', 4, 2),
14061407
('line', 'func', 2),
1407-
('branch', 'func', 2, 2),
1408+
('branch', 'func', 2, 7),
1409+
('line', 'func', 7),
14081410
('line', 'get_events', 11)])
14091411

14101412
def test_except_star(self):
@@ -1434,7 +1436,7 @@ def func():
14341436
('line', 'meth', 1),
14351437
('jump', 'func', 5, 5),
14361438
('jump', 'func', 5, '[offset=114]'),
1437-
('branch', 'func', '[offset=120]', '[offset=122]'),
1439+
('branch', 'func', '[offset=120]', '[offset=124]'),
14381440
('line', 'get_events', 11)])
14391441

14401442
self.check_events(func, recorders = FLOW_AND_LINE_RECORDERS, expected = [
@@ -1450,7 +1452,7 @@ def func():
14501452
('return', None),
14511453
('jump', 'func', 5, 5),
14521454
('jump', 'func', 5, '[offset=114]'),
1453-
('branch', 'func', '[offset=120]', '[offset=122]'),
1455+
('branch', 'func', '[offset=120]', '[offset=124]'),
14541456
('return', None),
14551457
('line', 'get_events', 11)])
14561458

0 commit comments

Comments
 (0)