| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + import gc | ||
| 1 | 2 | import re | |
| 2 | 3 | import sys | |
| 3 | 4 | import textwrap | |
@@ -261,5 +262,69 @@ def gen(): | |||
| 261 | 262 | """) | |
| 262 | 263 | assert_python_ok("-c", code) | |
| 263 | 264 | ||
| 265 | + @support.cpython_only | ||
| 266 | + def test_sneaky_frame_object(self): | ||
| 267 | + | ||
| 268 | + def trace(frame, event, arg): | ||
| 269 | + """ | ||
| 270 | + Don't actually do anything, just force a frame object to be created. | ||
| 271 | + """ | ||
| 272 | + | ||
| 273 | + def callback(phase, info): | ||
| 274 | + """ | ||
| 275 | + Yo dawg, I heard you like frames, so I'm allocating a frame while | ||
| 276 | + you're allocating a frame, so you can have a frame while you have a | ||
| 277 | + frame! | ||
| 278 | + """ | ||
| 279 | + nonlocal sneaky_frame_object | ||
| 280 | + sneaky_frame_object = sys._getframe().f_back | ||
| 281 | + # We're done here: | ||
| 282 | + gc.callbacks.remove(callback) | ||
| 283 | + | ||
| 284 | + def f(): | ||
| 285 | + while True: | ||
| 286 | + yield | ||
| 287 | + | ||
| 288 | + old_threshold = gc.get_threshold() | ||
| 289 | + old_callbacks = gc.callbacks[:] | ||
| 290 | + old_enabled = gc.isenabled() | ||
| 291 | + old_trace = sys.gettrace() | ||
| 292 | + try: | ||
| 293 | + # Stop the GC for a second while we set things up: | ||
| 294 | + gc.disable() | ||
| 295 | + # Create a paused generator: | ||
| 296 | + g = f() | ||
| 297 | + next(g) | ||
| 298 | + # Move all objects to the oldest generation, and tell the GC to run | ||
| 299 | + # on the *very next* allocation: | ||
| 300 | + gc.collect() | ||
| 301 | + gc.set_threshold(1, 0, 0) | ||
| 302 | + # Okay, so here's the nightmare scenario: | ||
| 303 | + # - We're tracing the resumption of a generator, which creates a new | ||
| 304 | + # frame object. | ||
| 305 | + # - The allocation of this frame object triggers a collection | ||
| 306 | + # *before* the frame object is actually created. | ||
| 307 | + # - During the collection, we request the exact same frame object. | ||
| 308 | + # This test does it with a GC callback, but in real code it would | ||
| 309 | + # likely be a trace function, weakref callback, or finalizer. | ||
| 310 | + # - The collection finishes, and the original frame object is | ||
| 311 | + # created. We now have two frame objects fighting over ownership | ||
| 312 | + # of the same interpreter frame! | ||
| 313 | + sys.settrace(trace) | ||
| 314 | + gc.callbacks.append(callback) | ||
| 315 | + sneaky_frame_object = None | ||
| 316 | + gc.enable() | ||
| 317 | + next(g) | ||
| 318 | + # g.gi_frame should be the the frame object from the callback (the | ||
| 319 | + # one that was *requested* second, but *created* first): | ||
| 320 | + self.assertIs(g.gi_frame, sneaky_frame_object) | ||
| 321 | + finally: | ||
| 322 | + gc.set_threshold(*old_threshold) | ||
| 323 | + gc.callbacks[:] = old_callbacks | ||
| 324 | + sys.settrace(old_trace) | ||
| 325 | + if old_enabled: | ||
| 326 | + gc.enable() | ||
| 327 | + | ||
| 328 | + | ||
| 264 | 329 | if __name__ == "__main__": | |
| 265 | 330 | unittest.main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Fix an issue where several frame objects could be backed by the same | ||
| 2 | + interpreter frame, possibly leading to corrupted memory and hard crashes of | ||
| 3 | + the interpreter. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,14 +35,31 @@ _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame) | |||
| 35 | 35 | Py_XDECREF(error_type); | |
| 36 | 36 | Py_XDECREF(error_value); | |
| 37 | 37 | Py_XDECREF(error_traceback); | |
| 38 | + return NULL; | ||
| 38 | 39 | } | |
| 39 | - else { | ||
| 40 | - assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); | ||
| 41 | - assert(frame->owner != FRAME_CLEARED); | ||
| 42 | - f->f_frame = frame; | ||
| 43 | - frame->frame_obj = f; | ||
| 44 | - PyErr_Restore(error_type, error_value, error_traceback); | ||
| 40 | + PyErr_Restore(error_type, error_value, error_traceback); | ||
| 41 | + if (frame->frame_obj) { | ||
| 42 | + // GH-97002: How did we get into this horrible situation? Most likely, | ||
| 43 | + // allocating f triggered a GC collection, which ran some code that | ||
| 44 | + // *also* created the same frame... while we were in the middle of | ||
| 45 | + // creating it! See test_sneaky_frame_object in test_frame.py for a | ||
| 46 | + // concrete example. | ||
| 47 | + // | ||
| 48 | + // Regardless, just throw f away and use that frame instead, since it's | ||
| 49 | + // already been exposed to user code. It's actually a bit tricky to do | ||
| 50 | + // this, since we aren't backed by a real _PyInterpreterFrame anymore. | ||
| 51 | + // Just pretend that we have an owned, cleared frame so frame_dealloc | ||
| 52 | + // doesn't make the situation worse: | ||
| 53 | + f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data; | ||
| 54 | + f->f_frame->owner = FRAME_CLEARED; | ||
| 55 | + f->f_frame->frame_obj = f; | ||
| 56 | + Py_DECREF(f); | ||
| 57 | + return frame->frame_obj; | ||
| 45 | 58 | } | |
| 59 | + assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); | ||
| 60 | + assert(frame->owner != FRAME_CLEARED); | ||
| 61 | + f->f_frame = frame; | ||
| 62 | + frame->frame_obj = f; | ||
| 46 | 63 | return f; | |
| 47 | 64 | } | |
| 48 | 65 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments