Repository navigation
Description
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_refback a stack-localnix::fetchers::Settings, and
remove thebind_to_evaluatorcall inlock_flake. - Run
tests/nanopynix/bindings/test_flake.py::TestGitFetcherSettingsunder
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=1is 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=mallocfor 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
Lillecarl commentedon Aug 3, 2026
Boehm GC turns off the one ASan option this issue asks for
bdwgc sets it to 0, from inside the library, and says why.
os_dep.c:That string is the return value of
__asan_default_options(), the weak hookthe ASan runtime calls at startup. So the collector Nix links opts out on
purpose, and it is right to:
detect_stack_use_after_return=1moves localsinto 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_OPTIONSoverrides__asan_default_options, so following this issue'sinstruction 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 nofake 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=0andPYTHONMALLOC=mallocare both still right, and neither touches the collector.On combining sanitizers
Measured with this repository's toolchain, not quoted:
threadaddressundefinedthread,undefinedaddress,undefinedaddress,threadcc1plus: 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:
same defects the ASan job already reported, and every one would be triaged
twice.
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.
UBSan needs one flag or it is not a gate
By default it prints and continues. Measured:
A CI job would go green with the report sitting in its log. It needs
-fno-sanitize-recover=undefinedat compile time, orUBSAN_OPTIONS=halt_on_error=1, to fail. The TSan job already learned thislesson in the other direction --
TSAN_OPTIONS=halt_on_error=1is set innanopynix/tests.nixfor a different reason, and the same env-var disciplineapplies here.
Expect to need
-fno-sanitize=vptras well: the check needs every class in ahierarchy instrumented, and CPython is not. That is a measurement to make, not
a flag to set in advance.
ci(sanitizers): build and run an ASan+UBSan variant beside TSan
ci(sanitizers): give both workflows the same kinds of job, and stop U…
ci(sanitizers): let the ASAN job name its own findings
fix(sanitizers): ask meson for UBSan only, because libexpr refuses ASAN
feat(sanitizers): replace the ASAN variant with a UBSan one
fix(sanitizers): keep the flag order, so the TSAN closure stays cached
Lillecarl commentedon Aug 3, 2026
The variant is UBSan, and not ASAN
libexpr refuses ASAN together with the collector, in
src/libexpr/meson.build: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_evaluatorwithwhich is one of the three
nixUnreachableWhenHardenedsites invalue.hh, andall three read the type tag of a
Value. A conservative collector that cannotsee through the allocator of ASAN frees an object that is still live, and a tag
read of a freed
Valueis what the report showed.The UBSan build keeps
NIX_UBSAN_ENABLEDon, so those three sites arenix::unreachable()rather thanstd::unreachable(), and each one prints itsfile 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,
localbackend: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 60minutes 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
nix/sanitizer.nixrebuilds the instrumented closure, and theTSAN 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.
PyValuereadsnix::Value::type()at 16 sites with noisValid()guard,and
Value::isValid()appears nowhere innanopynix-bindings. Nix guardsevery such read in its own C API (
nix_api_value.cc), and the parameter docof
type()says an invalid value is undefined behaviour. No evidence saysthis 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.
feat(ci): give each step of a job its own timeout
feat(nix): build without the collector, so ASan runs the way Nix supp…
fix(nix): make the ASan build run, and stop pytest from hiding its re…
fix(nix): give the base environment room, and check the bound
ci(nogc): run the no-collector suite on every commit
test(asan): restore the #34 defect to prove the ASAN job catches it
Lillecarl commentedon Aug 4, 2026
The ASAN job finds the defect of #63
Run 30865598447, job 91856641557, against
ci-developat2a2a9996. Thatcommit restores the defect of #63 on purpose, and it does not reach
develop.ASan names the variable, and it names the line.
stack_settingsis the localfetchers::Settingsofparse_flake_ref, atnanopynix-bindings/src/nix_flake.cpp:123of that commit. The chain is complete: the binding returns, the frame dies,
lockFlakereaches theInputinside theFlakeRef, andwarnDirtyreads asetting 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_evaluatorrebuilds the
FlakeReffrom attrs againstes.fetchSettings, which is alive,so
lockFlakenever reached the dangling pointer. Both hunks have to go backto 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-asanjob doesnot 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-returnin this repository's own frame, as above, is stillmeaningful.
11 remaining items
feat(ci): give each step of a job its own timeout
feat(nix): build without the collector, so ASan runs the way Nix supp…
fix(nix): make the ASan build run, and stop pytest from hiding its re…
fix(nix): give the base environment room, and check the bound
test(nogc): keep an evaluator out of the pytest process without a col…
test(nogc): add a fork mode for the in-process tests, and record why …
test(nogc): fork every in-process test, and drop the second ASAN run
ci(nogc): run the no-collector suite on every commit
ci(asan): run the ASAN jobs on every commit, now that the number is in
perf(ci): give the ASAN job the tests that ASAN can report on
pynixd: the Unix socket server and wire run on anyio streams
nixkube: the NRI volume sweep deletes in a thread