Skip to content

Address sanitizer #32781

Open
Open

Description

@da-woods

Warning

This is not a good first issue to contribute. Great if you are interested to contribute to scikit-learn 🙏. Please have a look at our contributing doc and in particular the section Issues for new contributors.

Describe the workflow you want to enable

This was discussed a little on issue #32491 and I wanted to give a bit of context about how to do it (unfortunately I don't think I know your CI workflow well enough to do it myself).

Essentially, address sanitizer is a compiler option which looks for out of bounds reads and writes so should catch errors like reading beyond the end of an array. It slows things down so you definitely don't want to distribute wheels with it enabled, but it may help with testing. It only works if you actually exercise the code so you'll want to build and run the tests. It's probably easiest to do on Linux and with just one Python version - there's likely little benefit to doing it comprehensively.

Undefined behaviour sanitizer is related and can be enabled at the same time. However it probably has less value, and there's some minor bits of undefined behaviour in Cython which you'll have to ignore.

Describe your proposed solution

Rough outline:

  • pick a compiler - clang is probably the best but gcc or msvc will work if you have strong preferences.
  • Compile and link with -fsanitize=address as a command-line option.
  • Run the tests with ASAN_OPTIONS=detect_leaks=0 set as an environmental variable - Cython definitely doesn't clean everything up on exit so you'll get lots of fairly useless false positives.
  • You may need LD_PRELOAD=$(gcc --print-file-name=libasan.so) as an environmental variable too.
  • Dependency: I don't think it's strictly necessary to build all your dependencies with address sanitizer, however:
    • if you're linking to C++ libraries that use std::string and std::vector then it's a good idea to build them with it too (otherwise you have to manually disable some checks).
    • I imagine you're doing a lot of your memory allocation in Numpy? In which case it'd probably be a good idea to build Numpy with it too. I'm not the right person to advise on that.

Hopefully that makes some sense - feel ping me if it doesn't work, or close if it isn't something you want to do.

Additional context

Numpy doc about compiler sanitizers

Numpy CI workflow

Activity

  1. Aymuos22 commented on Nov 24, 2025

    @Aymuos22
  2. removed
    Needs Triage
    on Nov 26, 2025
  3. lesteve commented on Nov 26, 2025

    @lesteve
    Member

    Super useful @da-woods, thanks! I edited your comment and added the numpy doc about compiler sanitizers and the link to numpy CI workflow.

    I guess before thinking about having a CI Setup, a first step would be to try to compile things locally and get a feeling of how many false positives we get? Then we can think about whether this would be useful to catch subtle bugs in general and wether we think it's worth adding a CI for it?

  4. lesteve commented on Nov 26, 2025

    @lesteve
  5. Aymuos22 commented on Nov 26, 2025

    @Aymuos22
  6. da-woods commented on Nov 26, 2025

    @da-woods
    Author

    I guess before thinking about having a CI Setup, a first step would be to try to compile things locally and get a feeling of how many false positives we get?

    Yes - absolutely.

    (For most of these sanitizers "false positives" hardly even happen though - they're almost always correct. For the undefined behaviour and thread sanitizers they're sometimes correct but you don't care because it's technically undefined but you're confident that your compiler does the right thing. But for address sanitizer it's usually just correct).

    added the numpy doc about compiler sanitizers and the link to numpy CI workflow.

    Yes - very useful links. I suspect that'd be the way to get a sanitized build of Numpy at least.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions