8000 ci: build the bindings with AddressSanitizer, beside the TSan matrix · Issue #64 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

ci: build the bindings with AddressSanitizer, beside the TSan matrix #64

Closed

Description

@Lillecarl

Issue #63 was a use-after-free in the bindings. It was found by hand, from a
RuntimeError: Invalid argument on one Nix version, and it took a gdb
breakpoint on __cxa_throw to name the cause. AddressSanitizer finds this
class of defect in one run, and it names the allocation site and the free site
without any of that work.

What to build

A nanopynix-asan-nix_2_XX package for each supported Nix version, beside the
TSan packages that nix/checks.nix already builds. The TSan matrix proves the
shape is affordable here, and this reuses it.

Add the package to the scheduled workflow, in the same matrix job as
test-tsan. Do not put it in the per-commit workflow until the run time is
known.

What it must catch

The defect of #63, exactly. Restore either half and the job must fail:

  • Give parse_flake_ref back a stack-local nix::fetchers::Settings, and
    remove the bind_to_evaluator call in lock_flake.
  • Run tests/nanopynix/bindings/test_flake.py::TestGitFetcherSettings under
    Nix 2.31.

ASan must report a stack-use-after-return or a stack-use-after-scope. Prove
this before you close the issue, because a sanitizer build that links but does
not instrument the interesting code is worse than no job: it reports success.

What to expect

  • detect_stack_use_after_return=1 is not the default. Set it, because the
    defect of bindings: parse_flake_ref returns a FlakeRef that points at a destroyed fetchers::Settings #63 is exactly that shape.
  • Nix allocates from the Boehm collector, and Boehm and ASan disagree about
    what a leak is. Turn the leak checker off (detect_leaks=0) rather than
    fight it. This job is for a memory error, not for a leak.
  • Python itself needs PYTHONMALLOC=malloc for ASan to see its allocations.
    Without it the reports point at the wrong place.
  • A whole-suite ASan run may be too slow for one job. If it is, run the
    binding tests and the flake tests only, and say in the job what it leaves
    out. A silent subset reads as full coverage.

Why now

Nix reaches nanopynix through raw pointers in more than one place, and the
version-to-version differences are exactly where the ownership changes. Nix
2.31 keeps a const fetchers::Settings * inside fetchers::Input; Nix 2.34
and Nix 2.35 removed it. EvalState holds a const fetchers::Settings & that
the caller must outlive. PyValue holds an EvalState. Each is a rule that
only a person reading the C++ enforces today.

Blocked by nothing. Related to #63.

Activity

  1. Lillecarl commented on Aug 3, 2026

    @Lillecarl
    ContributorAuthor

    Boehm GC turns off the one ASan option this issue asks for

    detect_stack_use_after_return=1 is not the default. Set it, because the
    defect of #63 is exactly that shape.

    bdwgc sets it to 0, from inside the library, and says why. os_dep.c:

          /* Do not to use fake stacks. */
          "detect_stack_use_after_return=0");

    That string is the return value of __asan_default_options(), the weak hook
    the ASan runtime calls at startup. So the collector Nix links opts out on
    purpose, and it is right to: detect_stack_use_after_return=1 moves locals
    into a heap-allocated "fake stack", and a conservative collector scanning the
    real stack then cannot see a pointer held only in a local. It collects a live
    object, and the crash that follows looks like one of ours.

    ASAN_OPTIONS overrides __asan_default_options, so following this issue's
    instruction literally does not fail loudly -- it silently re-enables the thing
    bdwgc disabled.

    What this does not block

    Use-after-scope is separate. It is compile-time instrumentation
    (-fsanitize-address-use-after-scope, on by default with ASan) and needs no
    fake stack, so it works with bdwgc's setting untouched.

    The issue asks for "a stack-use-after-return or a stack-use-after-scope",
    so the first thing to measure is which one #63 actually produces. If it is
    use-after-scope, there is no conflict and no option to set. If it is
    use-after-return, the job has to choose between seeing that class of defect
    and having a working collector, and that choice belongs in this issue.

    The rest of the ASan plan is unaffected: detect_leaks=0 and
    PYTHONMALLOC=malloc are both still right, and neither touches the collector.

    On combining sanitizers

    Measured with this repository's toolchain, not quoted:

    combination result
    thread builds
    address builds
    undefined builds
    thread,undefined builds
    address,undefined builds
    address,thread cc1plus: error: '-fsanitize=thread' is incompatible with '-fsanitize=address'

    So both proposed pairings are legal. Recommend UBSan on the ASan job only,
    not on both.
    Three reasons:

    1. A UBSan finding is not thread-dependent, so the TSan job would report the
      same defects the ASan job already reported, and every one would be triaged
      twice.
    2. The TSan matrix excludes 2.31 on purpose (default.nix -- two known,
      unfixed upstream races on that version alone). UBSan attached to TSan would
      inherit that exclusion, and 2.31 is the version bindings: parse_flake_ref returns a FlakeRef that points at a destroyed fetchers::Settings #63 reproduces on. Attached
      to ASan it runs everywhere.
    3. TSan is already the slowest job.

    UBSan needs one flag or it is not a gate

    By default it prints and continues. Measured:

    u.cc:2:21: runtime error: shift exponent 40 is too large for 32-bit type 'int'
    exit=0
    

    A CI job would go green with the report sitting in its log. It needs
    -fno-sanitize-recover=undefined at compile time, or
    UBSAN_OPTIONS=halt_on_error=1, to fail. The TSan job already learned this
    lesson in the other direction -- TSAN_OPTIONS=halt_on_error=1 is set in
    nanopynix/tests.nix for a different reason, and the same env-var discipline
    applies here.

    Expect to need -fno-sanitize=vptr as well: the check needs every class in a
    hierarchy instrumented, and CPython is not. That is a measurement to make, not
    a flag to set in advance.

  2. Lillecarl commented on Aug 3, 2026

    @Lillecarl
    ContributorAuthor

    The variant is UBSan, and not ASAN

    libexpr refuses ASAN together with the collector, in src/libexpr/meson.build:

    bdw_gc_required = get_option('gc').disable_if(
      'address' in get_option('b_sanitize'),
      error_message : 'Building with Boehm GC and ASAN is not supported',
    )

    The ASAN variant of this issue met that test only because the flag arrived
    through NIX_CFLAGS_COMPILE, which meson does not read.

    What that build reported was an artefact, and this is now proven. On 2.34,
    on 2.35 and on git it stopped in
    test_a_cancelled_evaluation_abandons_the_evaluator with

    <utility>:232:26: runtime error: execution reached an unreachable program point
    

    which is one of the three nixUnreachableWhenHardened sites in value.hh, and
    all three read the type tag of a Value. A conservative collector that cannot
    see through the allocator of ASAN frees an object that is still live, and a tag
    read of a freed Value is what the report showed.

    The UBSan build keeps NIX_UBSAN_ENABLED on, so those three sites are
    nix::unreachable() rather than std::unreachable(), and each one prints its
    file and line and stops the process. The suite passes on all four versions with
    that path armed. The site is therefore not reached at all without ASAN, which
    is a stronger result than an absent report.

    Issue #76 holds the supported route to ASAN.

    The run time, which this issue asked for

    Run 30782379867, one job for each version, full suite, local backend:

    version build suite result
    git 38.2 min 10.5 min 2040 passed, 8 skipped
    2.31 34.4 min 13.0 min 1966 passed, 82 skipped
    2.34 36.2 min 9.9 min 2040 passed, 8 skipped
    2.35 37.5 min 9.7 min 2040 passed, 8 skipped

    No sanitizer report on any version.

    The regular suite takes 8 to 13 minutes, so UBSan costs almost nothing at
    run time
    . It adds inline checks and keeps no shadow memory. ASAN doubled the
    suite; this does not.

    The build is the cost, and it is a one-time cost: cachix holds the closure, and
    a rebuild happens only on a change to nix/sanitizer.nix. The cap of 60
    minutes holds the worst cold case measured, which is 48.7 minutes.

    This answers the question that kept the job out of the per-commit workflow.
    The job runs there now.

    What is still open

    • A change to nix/sanitizer.nix rebuilds the instrumented closure, and the
      TSAN job has a 30-minute cap that a 25-minute cold build nearly fills. A
      reorder of the flags alone timed out two TSAN jobs in run 30782379867. The
      flag order now carries a comment that says why it is not free to change, and
      the cap is unchanged. Raising the cap weakens what it is for.
    • PyValue reads nix::Value::type() at 16 sites with no isValid() guard,
      and Value::isValid() appears nowhere in nanopynix-bindings. Nix guards
      every such read in its own C API (nix_api_value.cc), and the parameter doc
      of type() says an invalid value is undefined behaviour. No evidence says
      this is reachable.
      It was a hypothesis for the ASAN report above, and that
      report is now explained by something else. Recorded here because it is a real
      difference from the pattern that upstream follows, and not because a defect
      is known.
  3. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    The ASAN job finds the defect of #63

    Run 30865598447, job 91856641557, against ci-develop at 2a2a9996. That
    commit restores the defect of #63 on purpose, and it does not reach develop.

    ==5759==ERROR: AddressSanitizer: stack-use-after-return on address 0x7b887d0b8580
    READ of size 1 at 0x7b887d0b8580 thread T123
        #0 nix::fetchers::GitInputScheme::RepoInfo::warnDirty(nix::fetchers::Settings const&) const ../git.cc:408
        nixidae/nixidae#30 nix::fetchers::GitInputScheme::getAccessorFromWorkdir(...)
        nixidae/nixidae#31 nix::fetchers::GitInputScheme::getAccessor(...)
        nixidae/nixidae#32 nix::fetchers::Input::getAccessorUnchecked(...) ../fetchers.cc:347
        nixidae/nixidae#33 nix::fetchers::Input::getAccessor(...) ../fetchers.cc:296
        nixidae/nixidae#34 nix::fetchers::InputCache::getAccessor(...)
        nixidae/nixidae#35 getFlake ../flake.cc:341
        nixidae/nixidae#36 nix::flake::lockFlake(...)
        nixidae/nixidae#37 (nanopynix_bindings/_ext.cpython-314-x86_64-linux-gnu.so)
    
    Address 0x7b887d0b8580 is located in stack of thread T123 at offset 1408 in frame
        #0 (nanopynix_bindings/_ext.cpython-314-x86_64-linux-gnu.so+0x17293c)
      This frame has 10 object(s):
        [704, 864) 'ref' (line 125)
        [928, 2112) 'stack_settings' (line 123) <== Memory access at offset 1408 is inside this variable
    
    SUMMARY: AddressSanitizer: stack-use-after-return ../git.cc:408 in warnDirty
    

    ASan names the variable, and it names the line. stack_settings is the local
    fetchers::Settings of parse_flake_ref, at nanopynix-bindings/src/nix_flake.cpp:123
    of that commit. The chain is complete: the binding returns, the frame dies,
    lockFlake reaches the Input inside the FlakeRef, and warnDirty reads a
    setting out of the dead frame.

    The test that reached it is
    tests/nanopynix/test_config_flow.py::test_every_settings_scope_reaches_the_door_that_owns_it[local-inproc].

    What it took to get this proof

    The first attempt reported nothing, and its acceptance tests passed
    (run 30860160011). It restored one half of the defect only. bind_to_evaluator
    rebuilds the FlakeRef from attrs against es.fetchSettings, which is alive,
    so lockFlake never reached the dangling pointer. Both hunks have to go back
    to make the defect observable, and that is worth knowing: the fix has two
    independent halves, and either one alone closes the hole.

    What is still not proven

    The job is red for a second reason as well, and that reason is not a defect of
    this repository. See #89: ASan reports against its own runtime, on Nix's
    unannotated boost fiber stack. Until #89 is fixed, a red test-asan job does
    not mean a real finding, and somebody must read the report class before
    believing it. A green job is still meaningful, and a
    stack-use-after-return in this repository's own frame, as above, is still
    meaningful.

  4. 11 remaining items

  5. added
    source: nanopynix
    on Oct 19, 2026
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

    source: nanopynix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0