Skip to content

[mypyc] Run mypyc runtime tests under ASan/UBSan in CI #22466

Open

Description

@Jahongir-Qurbonov

Feature

Add an optional CI job (nightly and/or workflow_dispatch) that runs the mypyc runtime
tests with AddressSanitizer and UndefinedBehaviorSanitizer enabled, and possibly a cheaper
one with PYTHONMALLOC=debug.

Pitch

lib-rt (and librt) contain a lot of hand-written C that deals with reference counting,
borrowed references and raw buffers. Several recent fixes were memory-safety bugs in this
code (use-after-free on error paths, out-of-bounds buffer accesses, borrowed references held
across calls that can run arbitrary code). Such bugs often don't crash the normal test suite,
since freed memory usually still holds valid-looking data. A sanitizer job would catch
regressions of this kind close to when they are introduced.

Local prototype

I tried this locally on master (0952029), CPython 3.13 (regular, non-ASan build),
gcc 15, Linux x86_64, pytest -n 12 mypyc/test/test_run.py mypyc/test/test_external.py:

Configuration Result Time
Normal all pass ~2.5 min
PYTHONMALLOC=debug 912 passed, 1 xfailed 3.5 min
ASan + UBSan 910 passed, 2 failed (both false positives, see below) 12.6 min

ASan + UBSan setup (the sanitizer runtimes must be preloaded since the interpreter itself
isn't instrumented):

export CFLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer"
export LDFLAGS="-fsanitize=address,undefined"
export LD_PRELOAD="$(gcc -print-file-name=libasan.so) $(gcc -print-file-name=libubsan.so)"
# CPython itself leaks at exit; some vec tests intentionally request huge allocations
export ASAN_OPTIONS=detect_leaks=0:allocator_may_return_null=1
pytest -n 12 mypyc/test/test_run.py mypyc/test/test_external.py

No real memory errors were found on current master. The two failures:

  1. testLibrtVecsMiscInterpreted_librt: the test intentionally requests a huge allocation to
    check the MemoryError path. With allocator_may_return_null=1 this works, but ASan
    still prints WARNING: AddressSanitizer failed to allocate 0x2000000000000020 bytes to
    stderr, which breaks the expected output comparison. Could be handled by skipping the
    test under ASan or ignoring stderr there.
  2. test_c_unit_test: UBSan reports CPy.h:310:22: runtime error: left shift of negative value -15. This is the tagged-int encoding (value << 1) and is well-defined on
    GCC/Clang in practice, so the job would use -fno-sanitize=shift-base.

Proposal

  • A separate workflow (or a job in an existing one) running the configuration above with
    -fno-sanitize=shift-base, on a schedule and/or workflow_dispatch, since it is ~5x
    slower than the normal run and probably shouldn't run on every PR.
  • Optionally, a PYTHONMALLOC=debug run, which is much cheaper and could run more often.

Would maintainers be interested in this? If so, I can open a PR with the workflow and the
fixes for the two false positives.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions