Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1359271

Browse filesBrowse files
authored
Fix: Buggy pointer magic in copying type context (#27)
* Fix: Buggy pointer magic in copying type context Added better debugging for type context * Refactor: Rename vars
1 parent 3143a10 commit 1359271
Copy full SHA for 1359271

File tree

1 file changed

+70
-17
lines changed
Filter options

1 file changed

+70
-17
lines changed

‎Python/tier2.c

Copy file name to clipboardExpand all lines: Python/tier2.c
+70-17Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ _PyTier2TypeContext_Copy(const _PyTier2TypeContext *type_context)
113113
_Py_TYPENODE_t *parent = (_Py_TYPENODE_t *)_Py_TYPENODE_CLEAR_TAG(node);
114114

115115
// Check if part of locals
116-
if (parent - type_context->type_locals < nlocals) {
117-
type_locals[i] = node - (uintptr_t)orig_type_locals + (uintptr_t)type_locals;
116+
int offset_locals = (int)(parent - type_context->type_locals);
117+
if (0 <= offset_locals && offset_locals < nlocals) {
118+
type_locals[i] = _Py_TYPENODE_MAKE_REF((_Py_TYPENODE_t)(&type_locals[offset_locals]));
118119
}
119120
// Is part of stack
120121
else {
121-
type_locals[i] = node - (uintptr_t)orig_type_stack + (uintptr_t)type_stack;
122+
#if TYPEPROP_DEBUG
123+
int offset_stack = (int)(parent - type_context->type_stack);
124+
assert(0 <= offset_stack && offset_stack < nstack);
125+
#endif
126+
type_locals[i] = _Py_TYPENODE_MAKE_REF((_Py_TYPENODE_t)(&type_stack[offset_stack]));
122127
}
123128
break;
124129
}
@@ -139,12 +144,17 @@ _PyTier2TypeContext_Copy(const _PyTier2TypeContext *type_context)
139144
_Py_TYPENODE_t *parent = (_Py_TYPENODE_t *)_Py_TYPENODE_CLEAR_TAG(node);
140145

141146
// Check if part of locals
142-
if (parent - type_context->type_locals < nlocals) {
143-
type_stack[i] = node - (uintptr_t)orig_type_locals + (uintptr_t)type_locals;
147+
int plocals = (int)(parent - type_context->type_locals);
148+
if (0 <= plocals && plocals < nlocals) {
149+
type_stack[i] = _Py_TYPENODE_MAKE_REF((_Py_TYPENODE_t)(&type_locals[plocals]));
144150
}
145151
// Is part of stack
146152
else {
147-
type_stack[i] = node - (uintptr_t)orig_type_stack + (uintptr_t)type_stack;
153+
#if TYPEPROP_DEBUG
154+
int offset_stack = (int)(parent - type_context->type_stack);
155+
assert(0 <= offset_stack && offset_stack < nstack);
156+
#endif
157+
type_stack[i] = _Py_TYPENODE_MAKE_REF((_Py_TYPENODE_t)(&type_stack[offset_stack]));
148158
}
149159
break;
150160
}
@@ -394,23 +404,62 @@ print_typestack(const _PyTier2TypeContext *type_context)
394404

395405
int nstack_use = (int)(type_stackptr - type_stack);
396406
int nstack = type_context->type_stack_len;
407+
int nlocals = type_context->type_locals_len;
408+
409+
int plocals = 0;
410+
int pstack = 0;
411+
bool is_local = false;
412+
397413
fprintf(stderr, " Stack: %p: [", type_stack);
398414
for (int i = 0; i < nstack; i++) {
399-
PyTypeObject *type = typenode_get_type(type_stack[i]);
400-
_Py_TYPENODE_t tag = _Py_TYPENODE_GET_TAG(type_stack[i]);
401-
fprintf(stderr, "%s%s%s",
415+
_Py_TYPENODE_t node = type_stack[i];
416+
PyTypeObject *type = typenode_get_type(node);
417+
_Py_TYPENODE_t tag = _Py_TYPENODE_GET_TAG(node);
418+
419+
if (tag == TYPE_REF) {
420+
_Py_TYPENODE_t *parent = (_Py_TYPENODE_t *)(_Py_TYPENODE_CLEAR_TAG(node));
421+
plocals = (int)(parent - type_context->type_locals);
422+
pstack = (int)(parent - type_context->type_stack);
423+
is_local = (0 <= plocals) && (plocals < nlocals);
424+
if (!is_local) {
425+
assert((0 <= pstack) && (pstack < nstack));
426+
}
427+
}
428+
429+
fprintf(stderr, "%s%s",
402430
i == nstack_use ? "." : " ",
403-
type == NULL ? "?" : type->tp_name,
404-
tag == TYPE_REF ? "*" : "");
431+
type == NULL ? "?" : type->tp_name);
432+
if (tag == TYPE_REF) {
433+
fprintf(stderr, "%s%d]",
434+
is_local ? "->locals[" : "->stack[",
435+
is_local ? plocals : pstack);
436+
}
405437
}
406438
fprintf(stderr, "]\n");
439+
407440
fprintf(stderr, " Locals %p: [", type_locals);
408-
for (int i = 0; i < type_context->type_locals_len; i++) {
409-
PyTypeObject *type = typenode_get_type(type_locals[i]);
410-
_Py_TYPENODE_t tag = _Py_TYPENODE_GET_TAG(type_locals[i]);
411-
fprintf(stderr, "%s%s ",
412-
type == NULL ? "?" : type->tp_name,
413-
tag == TYPE_REF ? "*" : "");
441+
for (int i = 0; i < nlocals; i++) {
442+
_Py_TYPENODE_t node = type_locals[i];
443+
PyTypeObject *type = typenode_get_type(node);
444+
_Py_TYPENODE_t tag = _Py_TYPENODE_GET_TAG(node);
445+
446+
if (tag == TYPE_REF) {
447+
_Py_TYPENODE_t *parent = (_Py_TYPENODE_t *)(_Py_TYPENODE_CLEAR_TAG(node));
448+
plocals = (int)(parent - type_context->type_locals);
449+
pstack = (int)(parent - type_context->type_stack);
450+
is_local = (0 <= plocals) && (plocals < nlocals);
451+
if (!is_local) {
452+
assert((0 <= pstack) && (pstack < nstack));
453+
}
454+
}
455+
456+
fprintf(stderr, " %s",
457+
type == NULL ? "?" : type->tp_name);
458+
if (tag == TYPE_REF) {
459+
fprintf(stderr, "%s%d]",
460+
is_local ? "->locals[" : "->stack[",
461+
is_local ? plocals : pstack);
462+
}
414463
}
415464
fprintf(stderr, "]\n");
416465
}
@@ -1669,7 +1718,11 @@ _PyCode_Tier2Initialize(_PyInterpreterFrame *frame, _Py_CODEUNIT *next_instr)
16691718
int deopt = _PyOpcode_Deopt[_Py_OPCODE(*curr_instr)];
16701719
if (IS_FORBIDDEN_OPCODE(deopt)) {
16711720
#if BB_DEBUG
1721+
#ifdef Py_DEBUG
1722+
fprintf(stderr, "FORBIDDEN OPCODE %s\n", _PyOpcode_OpName[_Py_OPCODE(*curr_instr)]);
1723+
#else
16721724
fprintf(stderr, "FORBIDDEN OPCODE %d\n", _Py_OPCODE(*curr_instr));
1725+
#endif
16731726
#endif
16741727
return NULL;
16751728
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.