From e9c2ec8c2a42ef128b7d3d113a26c18ef6e02895 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Mon, 12 Aug 2024 10:07:47 +0800 Subject: [PATCH 1/5] Make thread state(tstate) pointer safe --- .../2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst | 1 + Python/pystate.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst new file mode 100644 index 00000000000000..2dba7c1af93d35 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst @@ -0,0 +1 @@ +Make thread state(tstate) pointer safe diff --git a/Python/pystate.c b/Python/pystate.c index bba88b76088e71..2105b6fda96710 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3002,11 +3002,13 @@ _PyThreadState_CheckConsistency(PyThreadState *tstate) // // tstate can be a dangling pointer (point to freed memory): only tstate value // is used, the pointer is not deferenced. -// -// tstate must be non-NULL. int _PyThreadState_MustExit(PyThreadState *tstate) { + // tstate must be non-NULL. + if (tstate == NULL) { + return 1; + } /* bpo-39877: Access _PyRuntime directly rather than using tstate->interp->runtime to support calls from Python daemon threads. After Py_Finalize() has been called, tstate can be a dangling pointer: From b3fd552758a0b74a1e5191dbdc1335ebd13fc42b Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Mon, 12 Aug 2024 10:12:18 +0800 Subject: [PATCH 2/5] update --- Python/pystate.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index 2105b6fda96710..fbab46d45611a4 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3016,8 +3016,6 @@ _PyThreadState_MustExit(PyThreadState *tstate) unsigned long finalizing_id = _PyRuntimeState_GetFinalizingID(&_PyRuntime); PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime); if (finalizing == NULL) { - // XXX This isn't completely safe from daemon thraeds, - // since tstate might be a dangling pointer. finalizing = _PyInterpreterState_GetFinalizing(tstate->interp); finalizing_id = _PyInterpreterState_GetFinalizingID(tstate->interp); } From fe193937f03cc4c1075df210f37611769fa48a03 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Tue, 13 Aug 2024 19:46:09 +0800 Subject: [PATCH 3/5] update --- Python/pystate.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index fbab46d45611a4..db6b6418d307e2 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3002,13 +3002,12 @@ _PyThreadState_CheckConsistency(PyThreadState *tstate) // // tstate can be a dangling pointer (point to freed memory): only tstate value // is used, the pointer is not deferenced. +// +// tstate must be non-NULL. int _PyThreadState_MustExit(PyThreadState *tstate) { - // tstate must be non-NULL. - if (tstate == NULL) { - return 1; - } + assert(tstate != NULL); /* bpo-39877: Access _PyRuntime directly rather than using tstate->interp->runtime to support calls from Python daemon threads. After Py_Finalize() has been called, tstate can be a dangling pointer: @@ -3016,6 +3015,8 @@ _PyThreadState_MustExit(PyThreadState *tstate) unsigned long finalizing_id = _PyRuntimeState_GetFinalizingID(&_PyRuntime); PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime); if (finalizing == NULL) { + // XXX This isn't completely safe from daemon threads, + // since tstate might be a dangling pointer. finalizing = _PyInterpreterState_GetFinalizing(tstate->interp); finalizing_id = _PyInterpreterState_GetFinalizingID(tstate->interp); } From 0c5cc2de7a6af29203b636256eb3ee0241757bba Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Tue, 13 Aug 2024 20:11:32 +0800 Subject: [PATCH 4/5] lint --- Python/pystate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index db6b6418d307e2..95638bbe701566 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3002,7 +3002,7 @@ _PyThreadState_CheckConsistency(PyThreadState *tstate) // // tstate can be a dangling pointer (point to freed memory): only tstate value // is used, the pointer is not deferenced. -// +// // tstate must be non-NULL. int _PyThreadState_MustExit(PyThreadState *tstate) From e5ed2d769ab48937708c0ccc7ff6725c050ceda6 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Wed, 14 Aug 2024 19:16:03 +0800 Subject: [PATCH 5/5] del --- .../2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst deleted file mode 100644 index 2dba7c1af93d35..00000000000000 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-08-12-10-02-40.gh-issue-122928.e6_yVr.rst +++ /dev/null @@ -1 +0,0 @@ -Make thread state(tstate) pointer safe