Skip to content

Add CI to run pytest under AddressSanitizer - #323

Open
take-cheeze wants to merge 5 commits into
onnx:mainfrom
onnxsim:claude/pytest-address-sanitizer-ci-ff9549
Open

Add CI to run pytest under AddressSanitizer#323
take-cheeze wants to merge 5 commits into
onnx:mainfrom
onnxsim:claude/pytest-address-sanitizer-ci-ff9549

Conversation

@take-cheeze

Copy link
Copy Markdown
Member

Build the native extension (and the bundled onnx/protobuf) with -fsanitize=address and run the existing pytest suite with the ASan runtime preloaded, so memory errors (heap-buffer-overflow, use-after-free, ...) in onnxoptimizer's C++ passes are caught in CI.

CPython is not built with ASan, so libasan is LD_PRELOADed and detect_leaks=0 suppresses leaks from the uninstrumented interpreter and third-party libraries. onnxruntime is omitted from the test dependencies because its uninstrumented C++ throws exceptions that trip ASan's __cxa_throw interceptor; it is only used for optional numerical comparison, so every optimize() call is still exercised.

Claude-Session: https://claude.ai/code/session_01GewbTty3fkFT4G9u6J1JJn

@take-cheeze
take-cheeze force-pushed the claude/pytest-address-sanitizer-ci-ff9549 branch from f53230b to cb07962 Compare July 27, 2026 07:53
Comment thread .github/workflows/pytest-asan.yml Outdated
claude and others added 3 commits July 29, 2026 21:13
Build the native extension (and the bundled onnx/protobuf) with
-fsanitize=address and run the existing pytest suite with the ASan
runtime preloaded, so memory errors (heap-buffer-overflow,
use-after-free, ...) in onnxoptimizer's C++ passes are caught in CI.

CPython is not built with ASan, so libasan is LD_PRELOADed and
detect_leaks=0 suppresses leaks from the uninstrumented interpreter
and third-party libraries. onnxruntime is omitted from the test
dependencies because its uninstrumented C++ throws exceptions that
trip ASan's __cxa_throw interceptor; it is only used for optional
numerical comparison, so every optimize() call is still exercised.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GewbTty3fkFT4G9u6J1JJn
Signed-off-by: Takeshi Watanabe <take-cheeze@users.noreply.github.com>
Signed-off-by: take-cheeze <takechi101010@gmail.com>
The job aborted with "AddressSanitizer: CHECK failed ...
real___cxa_throw != 0" on the first checker.check_model call. The cause
is not onnxruntime (already omitted) but the uninstrumented `onnx`
Python wheel: its C++ extension throws C++ exceptions from
checker.check_model / shape inference, which the tests exercise
constantly. With only libasan preloaded, ASan's __cxa_throw interceptor
cannot resolve the real symbol at init and aborts the process on the
first raised exception.

Preload libstdc++ alongside libasan (libasan still first, as ASan
requires) so the interceptor finds the real __cxa_throw and exceptions
unwind normally; only genuine memory errors now fail CI. Update the
stale comment that attributed the abort solely to onnxruntime.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GewbTty3fkFT4G9u6J1JJn
Signed-off-by: Takeshi Watanabe <take-cheeze@users.noreply.github.com>
Signed-off-by: take-cheeze <takechi101010@gmail.com>
Signed-off-by: Takeshi Watanabe <take-cheeze@users.noreply.github.com>
Signed-off-by: take-cheeze <takechi101010@gmail.com>
@take-cheeze
take-cheeze force-pushed the claude/pytest-address-sanitizer-ci-ff9549 branch from 73e0dc5 to a959cf7 Compare July 29, 2026 12:13
The Pytest (ASan) job failed at collection with:

    ModuleNotFoundError: No module named 'onnxoptimizer.onnx_opt_cpp2py_export'

`pip install .` puts the compiled extension only in site-packages, but the
test step runs `pytest onnxoptimizer/test/` from the repo root. Because the
test modules live inside the onnxoptimizer package, importing them puts the
repo root on sys.path, so `import onnxoptimizer` resolves to the in-tree
source package -- which has the .py sources but no compiled
onnx_opt_cpp2py_export extension -- and collection fails before any test runs.

Build the extension in place as well (reusing the cmake build the pip install
just produced) so the compiled module sits next to the source package pytest
imports. This is orthogonal to the ASan flags; the extension itself builds and
imports correctly under ASan once it is present in the source tree.

Signed-off-by: take-cheeze <takechi101010@gmail.com>

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GewbTty3fkFT4G9u6J1JJn
@take-cheeze
take-cheeze marked this pull request as ready for review July 29, 2026 13:29
@take-cheeze
take-cheeze requested review from a team as code owners July 29, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants