Skip to content

Commit 7af7f9b

Browse files
committed
Implement Ralf J's suggestions.
1 parent 4f28c66 commit 7af7f9b

4 files changed

Lines changed: 47 additions & 228 deletions

File tree

‎.github/actions/setup-hardened-python/action.yml‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ inputs:
2626
venv:
2727
description: Directory for the venv. Defaults to a new directory under RUNNER_TEMP.
2828
default: ""
29+
stack-reserve:
30+
description: >-
31+
Windows only. Raise the base interpreter's main-thread stack reserve to at
32+
least this many bytes. Empty (default) leaves the interpreter unchanged.
33+
default: ""
2934

3035
outputs:
3136
python-path:
@@ -123,3 +128,41 @@ runs:
123128
PYTHON: ${{ steps.venv.outputs.python-path }}
124129
run: |
125130
"${PYTHON}" -m pip install -U pip
131+
132+
- name: Raise the interpreter's stack reserve
133+
if: runner.os == 'Windows' && inputs.stack-reserve != ''
134+
shell: bash --noprofile --norc -xeuo pipefail {0}
135+
env:
136+
PYTHON: ${{ steps.venv.outputs.python-path }}
137+
STACK_RESERVE: ${{ inputs.stack-reserve }}
138+
run: |
139+
# sys.stdout.write avoids a trailing CR from Windows Python's CRLF.
140+
base="$("${PYTHON}" -c 'import sys; sys.stdout.write(sys._base_executable)')"
141+
# Windows cannot overwrite a running executable. Prepare a copy and
142+
# replace the original after the patching interpreter exits.
143+
uv run --no-project --isolated --with pefile==2024.8.26 --python "${base}" python - "${base}" "${STACK_RESERVE}" <<'EOF'
144+
import sys
145+
146+
import pefile
147+
148+
path, reserve = sys.argv[1], int(sys.argv[2])
149+
if reserve <= 0:
150+
raise ValueError("stack-reserve must be a positive number of bytes")
151+
with pefile.PE(path) as pe:
152+
pe.OPTIONAL_HEADER.SizeOfStackReserve = max(pe.OPTIONAL_HEADER.SizeOfStackReserve, reserve)
153+
pe.write(path + ".stack-reserve")
154+
EOF
155+
mv -f -- "${base}.stack-reserve" "${base}"
156+
# Verify the running interpreter's stack, rather than just its header.
157+
"${base}" - "${STACK_RESERVE}" <<'EOF'
158+
import ctypes
159+
import sys
160+
161+
lo, hi = ctypes.c_size_t(), ctypes.c_size_t()
162+
get_limits = ctypes.windll.kernel32.GetCurrentThreadStackLimits
163+
get_limits.argtypes = [ctypes.POINTER(ctypes.c_size_t), ctypes.POINTER(ctypes.c_size_t)]
164+
get_limits.restype = None
165+
get_limits(ctypes.byref(lo), ctypes.byref(hi))
166+
print("main-thread stack reserve:", hi.value - lo.value)
167+
sys.exit(hi.value - lo.value < int(sys.argv[1]))
168+
EOF

‎.github/workflows/coverage.yml‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,15 @@ jobs:
242242
submodules: true
243243

244244
- name: Set up Python
245-
uses: ./.github/actions/setup-hardened-python
245+
uses: $/.github/actions/setup-hardened-python
246246
with:
247247
python-version: ${{ env.PY_VER }}
248248
venv: .venv
249+
stack-reserve: 8388608
249250

250251
- name: Set up MSVC
251252
uses: step-security/msvc-dev-cmd@22c98154b708dbd743e6f27a933cf6ceba3305c4 # v1.13.1
252253

253-
- name: Raise the interpreter's stack reserve to 8 MiB
254-
# Importing the coverage-built cuda.bindings.driver overflows the
255-
# 2,000,000-byte main-thread stack of python-build-standalone (#3330).
256-
run: |
257-
base_py="$(.venv/Scripts/python -c 'import sys; print(sys._base_executable)')"
258-
editbin -nologo -stack:8388608 "$base_py"
259-
dumpbin -nologo -headers "$base_py" | grep -i "size of stack reserve"
260-
261254
- name: Set up mini CTK
262255
uses: ./.github/actions/fetch_ctk
263256
continue-on-error: false
@@ -381,18 +374,11 @@ jobs:
381374
nvidia-smi
382375
383376
- name: Set up Python
384-
uses: ./.github/actions/setup-hardened-python
377+
uses: $/.github/actions/setup-hardened-python
385378
with:
386379
python-version: ${{ env.PY_VER }}
387380
venv: .venv
388-
389-
- name: Raise the interpreter's stack reserve to 8 MiB
390-
# This runner has no MSVC tools. Patch a copy, then replace the base
391-
# interpreter after Python exits so subprocesses also get 8 MiB stacks.
392-
run: |
393-
base_py="$(cygpath -u "$(.venv/Scripts/python -c 'import sys; sys.stdout.write(sys._base_executable)')")"
394-
.venv/Scripts/python ci/tools/prepare_windows_python_stack.py "$base_py" "$base_py.stack-reserve"
395-
mv -f -- "$base_py.stack-reserve" "$base_py"
381+
stack-reserve: 8388608
396382

397383
- name: Set up mini CTK
398384
uses: ./.github/actions/fetch_ctk

‎ci/tools/prepare_windows_python_stack.py‎

Lines changed: 0 additions & 70 deletions
This file was deleted.

‎ci/tools/tests/test_prepare_windows_python_stack.py‎

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)