frame.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #define _PY_INTERPRETER
  2. #include "Python.h"
  3. #include "frameobject.h"
  4. #include "pycore_code.h" // stats
  5. #include "pycore_frame.h"
  6. #include "pycore_object.h" // _PyObject_GC_UNTRACK()
  7. #include "opcode.h"
  8. int
  9. _PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg)
  10. {
  11. Py_VISIT(frame->frame_obj);
  12. Py_VISIT(frame->f_locals);
  13. Py_VISIT(frame->f_funcobj);
  14. Py_VISIT(frame->f_code);
  15. /* locals */
  16. PyObject **locals = _PyFrame_GetLocalsArray(frame);
  17. int i = 0;
  18. /* locals and stack */
  19. for (; i <frame->stacktop; i++) {
  20. Py_VISIT(locals[i]);
  21. }
  22. return 0;
  23. }
  24. PyFrameObject *
  25. _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame)
  26. {
  27. assert(frame->frame_obj == NULL);
  28. PyObject *exc = PyErr_GetRaisedException();
  29. PyFrameObject *f = _PyFrame_New_NoTrack(frame->f_code);
  30. if (f == NULL) {
  31. Py_XDECREF(exc);
  32. return NULL;
  33. }
  34. PyErr_SetRaisedException(exc);
  35. if (frame->frame_obj) {
  36. // GH-97002: How did we get into this horrible situation? Most likely,
  37. // allocating f triggered a GC collection, which ran some code that
  38. // *also* created the same frame... while we were in the middle of
  39. // creating it! See test_sneaky_frame_object in test_frame.py for a
  40. // concrete example.
  41. //
  42. // Regardless, just throw f away and use that frame instead, since it's
  43. // already been exposed to user code. It's actually a bit tricky to do
  44. // this, since we aren't backed by a real _PyInterpreterFrame anymore.
  45. // Just pretend that we have an owned, cleared frame so frame_dealloc
  46. // doesn't make the situation worse:
  47. f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
  48. f->f_frame->owner = FRAME_CLEARED;
  49. f->f_frame->frame_obj = f;
  50. Py_DECREF(f);
  51. return frame->frame_obj;
  52. }
  53. assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
  54. assert(frame->owner != FRAME_CLEARED);
  55. f->f_frame = frame;
  56. frame->frame_obj = f;
  57. return f;
  58. }
  59. void
  60. _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest)
  61. {
  62. assert(src->stacktop >= src->f_code->co_nlocalsplus);
  63. Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src;
  64. memcpy(dest, src, size);
  65. // Don't leave a dangling pointer to the old frame when creating generators
  66. // and coroutines:
  67. dest->previous = NULL;
  68. }
  69. static void
  70. take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
  71. {
  72. assert(frame->owner != FRAME_OWNED_BY_CSTACK);
  73. assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
  74. assert(frame->owner != FRAME_CLEARED);
  75. Py_ssize_t size = ((char*)&frame->localsplus[frame->stacktop]) - (char *)frame;
  76. Py_INCREF(frame->f_code);
  77. memcpy((_PyInterpreterFrame *)f->_f_frame_data, frame, size);
  78. frame = (_PyInterpreterFrame *)f->_f_frame_data;
  79. f->f_frame = frame;
  80. frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
  81. if (_PyFrame_IsIncomplete(frame)) {
  82. // This may be a newly-created generator or coroutine frame. Since it's
  83. // dead anyways, just pretend that the first RESUME ran:
  84. PyCodeObject *code = frame->f_code;
  85. frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable;
  86. }
  87. assert(!_PyFrame_IsIncomplete(frame));
  88. assert(f->f_back == NULL);
  89. _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous);
  90. frame->previous = NULL;
  91. if (prev) {
  92. assert(prev->owner != FRAME_OWNED_BY_CSTACK);
  93. /* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
  94. PyFrameObject *back = _PyFrame_GetFrameObject(prev);
  95. if (back == NULL) {
  96. /* Memory error here. */
  97. assert(PyErr_ExceptionMatches(PyExc_MemoryError));
  98. /* Nothing we can do about it */
  99. PyErr_Clear();
  100. }
  101. else {
  102. f->f_back = (PyFrameObject *)Py_NewRef(back);
  103. }
  104. }
  105. if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) {
  106. _PyObject_GC_TRACK((PyObject *)f);
  107. }
  108. }
  109. void
  110. _PyFrame_ClearExceptCode(_PyInterpreterFrame *frame)
  111. {
  112. /* It is the responsibility of the owning generator/coroutine
  113. * to have cleared the enclosing generator, if any. */
  114. assert(frame->owner != FRAME_OWNED_BY_GENERATOR ||
  115. _PyFrame_GetGenerator(frame)->gi_frame_state == FRAME_CLEARED);
  116. // GH-99729: Clearing this frame can expose the stack (via finalizers). It's
  117. // crucial that this frame has been unlinked, and is no longer visible:
  118. assert(_PyThreadState_GET()->cframe->current_frame != frame);
  119. if (frame->frame_obj) {
  120. PyFrameObject *f = frame->frame_obj;
  121. frame->frame_obj = NULL;
  122. if (Py_REFCNT(f) > 1) {
  123. take_ownership(f, frame);
  124. Py_DECREF(f);
  125. return;
  126. }
  127. Py_DECREF(f);
  128. }
  129. assert(frame->stacktop >= 0);
  130. for (int i = 0; i < frame->stacktop; i++) {
  131. Py_XDECREF(frame->localsplus[i]);
  132. }
  133. Py_XDECREF(frame->frame_obj);
  134. Py_XDECREF(frame->f_locals);
  135. Py_DECREF(frame->f_funcobj);
  136. }
  137. /* Unstable API functions */
  138. PyObject *
  139. PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame)
  140. {
  141. PyObject *code = (PyObject *)frame->f_code;
  142. Py_INCREF(code);
  143. return code;
  144. }
  145. int
  146. PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame)
  147. {
  148. return _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
  149. }
  150. int
  151. PyUnstable_InterpreterFrame_GetLine(_PyInterpreterFrame *frame)
  152. {
  153. int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT);
  154. return PyCode_Addr2Line(frame->f_code, addr);
  155. }