Skip to content

Commit cd73b13

Browse files
fix(internal): closure bytecode wrapping for Python 3.11 [backport #7044 to 1.20] (#7065)
Backport of #7044 to 1.20 We fix the bytecode wrapping for closures to ensure that the frame objects don't end up referencing missing data, ultimately resulting in a segmentation fault in profiling tools. Fixes #6701. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. Co-authored-by: Emmett Butler <[email protected]>
1 parent cc9e822 commit cd73b13

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

ddtrace/internal/wrapping/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,30 @@ def wrap_bytecode(wrapper, wrapped):
115115
Instr("LOAD_CONST", wrapped, lineno=lineno),
116116
]
117117
if PY >= (3, 11):
118-
# THis is required to start a new frame
118+
# From insert_prefix_instructions
119119
instrs[0:0] = [
120120
Instr("RESUME", 0, lineno=lineno - 1),
121121
Instr("PUSH_NULL", lineno=lineno),
122122
]
123123

124+
if code.co_cellvars:
125+
from bytecode import CellVar
126+
127+
instrs[0:0] = [Instr("MAKE_CELL", CellVar(_), lineno=lineno) for _ in code.co_cellvars]
128+
129+
if code.co_freevars:
130+
instrs.insert(0, Instr("COPY_FREE_VARS", len(code.co_freevars), lineno=lineno))
131+
124132
# Build the tuple of all the positional arguments
125133
if nargs:
126-
instrs.extend([Instr("LOAD_FAST", argname, lineno=lineno) for argname in argnames])
134+
instrs.extend(
135+
[
136+
Instr("LOAD_DEREF", CellVar(argname), lineno=lineno)
137+
if PY >= (3, 11) and argname in code.co_cellvars
138+
else Instr("LOAD_FAST", argname, lineno=lineno)
139+
for argname in argnames
140+
]
141+
)
127142
instrs.append(Instr("BUILD_TUPLE", nargs, lineno=lineno))
128143
if varargs:
129144
instrs.extend(
@@ -198,6 +213,8 @@ def wrap(f, wrapper):
198213

199214
code = wrap_bytecode(wrapper, wrapped)
200215
code.freevars = f.__code__.co_freevars
216+
if PY >= (3, 11):
217+
code.cellvars = f.__code__.co_cellvars
201218
code.name = f.__code__.co_name
202219
code.filename = f.__code__.co_filename
203220
code.flags = f.__code__.co_flags
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fix an issue that could have caused some tracing integrations to create
5+
invalid references to objects in Python frames, ultimately causing profiling
6+
tools to potentially induce a segmentation fault.

tests/internal/test_wrapping.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,27 @@ def f():
202202
wrap(f, wrapper)
203203

204204
assert [frame.f_code.co_name for frame in f()[:4]] == ["f", "wrapper", "f", "test_wrap_stack"]
205+
206+
207+
def test_wrap_closure():
208+
channel = []
209+
210+
def wrapper(f, args, kwargs):
211+
channel.append((args, kwargs))
212+
retval = f(*args, **kwargs)
213+
channel.append(retval)
214+
return retval
215+
216+
def outer(answer=42):
217+
def f(a, b, c=None):
218+
return (a, b, c, answer)
219+
220+
return f
221+
222+
wrap(outer, wrapper)
223+
224+
closure = outer()
225+
wrap(closure, wrapper)
226+
227+
assert closure(1, 2, 3) == (1, 2, 3, 42)
228+
assert channel == [((42,), {}), closure, ((1, 2, 3), {}), (1, 2, 3, 42)]

0 commit comments

Comments
 (0)