Repository navigation
Description
What happens
The full suite against nanopynix-tests-nix_2_34-nogc aborts:
corrupted size vs. prev_size
Fatal Python error: Aborted
File "tests/nanopynix/bindings/test_expr.py", line 73 in test_value_keeps_eval_state_alive
It aborts a second time in tests/nanopynix/test_examples.py::test_example_runs,
and both times the thread is inside the constructor of EvalState.
The cause, from the ASAN job of #76
nanopynix-tests-nix_2_34-asan is the same build with the sanitizer, and it
names the write:
ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 8 at 0x7ae40324ff88
#0 nix::EvalState::addPrimOp(nix::PrimOp&&) ../eval.cc:550
nixidae/nixidae#30 nix::EvalState::createBaseEnv(nix::EvalSettings const&) ../primops.cc:5427
nixidae/nixidae#31 nix::EvalState::EvalState(...) ../eval.cc:349
0x7ae40324ff88 is located 0 bytes after 1032-byte region
allocated by thread T0 here:
nixidae/nixidae#30 nix::EvalMemory::allocBytes(unsigned long) ../include/nix/expr/eval-inline.hh:22
nixidae/nixidae#31 nix::EvalMemory::allocEnv(unsigned long) ../include/nix/expr/eval-inline.hh:88
nixidae/nixidae#32 nix::EvalState::EvalState(...) ../eval.cc:311
The region is the base Env. eval.cc:228 fixes its size:
static constexpr size_t BASE_ENV_SIZE = 128;1032 bytes is sizeof(Env) plus 128 slots of 8 bytes. addPrimOp and
addConstant then fill it with no bound check at all:
staticBaseEnv->vars.emplace_back(envName, baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v; // eval.cc:481 and eval.cc:550Nix 2.34 publishes 119 names under builtins, and the base env holds those
plus the top-level constants. The count therefore sits at the ceiling, and one
name past it writes into the next heap block.
Why no build has ever reported this
EvalMemory::allocBytes is the collector under a normal build, and
GC_MALLOC rounds a request up to a size class. The write past slot 127 lands
in the slack of the block, so nothing observes it. -Dgc=disabled makes
allocBytes a plain calloc, which gives the exact size, so the same write
lands on the header of the next block and glibc aborts.
The collector hides the defect. It does not prevent it. The shipped
build makes the same out-of-bounds write, and it is undefined behaviour there
too.
An evaluator with no settings at all still fits, which is why a minimal script
runs and the suite does not.
What this blocks
Acceptance criterion 2 of #76 asks for the full suite against the non-GC
build. That suite cannot pass while this is open.
Options
- Report it upstream. The bound check is one line, and the size wants to
come from the count of registered names rather than from a constant. - Carry a patch on
nix-exprin the-nogcand-asanscopes only, which
raisesBASE_ENV_SIZEor adds the bound check. This unblocks Support a non-GC build for RPC workers, so ASAN runs in a supported configuration #76 and ci: build the bindings with AddressSanitizer, beside the TSan matrix #64
without touching the build that ships.
Option 2 unblocks the work. Option 1 is the correction.
Activity
Lillecarl commentedon Aug 3, 2026
The count, exactly
builtins.addConstantandaddPrimOpeach take one base env slot and push onebuiltinsattribute,and
builtinsis slot 0 throughaddConstantas well, sobaseEnvDisplequals the attribute count.
_register_test_primopsintests/conftest.pycallsnanopynix.register_primop24 times.BASE_ENV_SIZE = 128. The base env overflows by15 slots, which is 120 bytes.
nanopynix.register_primopreachesnix::RegisterPrimOp(
nanopynix-bindings/src/nix_expr.cpp:1354), which appends to a globalregistry.
createBaseEnvthen walks that registry with no bound:So stock Nix fits with 9 slots to spare, and this API is what exceeds the
limit. Any consumer that registers 10 or more primops does the same.
This is not limited to the non-GC build
GC_MALLOCrounds the 1032-byte request up to a size class, so the 120 extrabytes land in the slack of the block. The GC build makes the same
out-of-bounds write on every run of this suite and does not report it. The
non-GC build only removes the rounding that hides it.
Whether the slack absorbs all 15 slots depends on the size class, on the
enabled experimental features, and on the number of registered primops. That
is the profile of an intermittent, configuration-dependent corruption, so this
is a candidate cause for the multi-EvalState corruption that has no
explanation yet.
BASE_ENV_SIZE = 128is identical in 2.31, 2.34 and 2.35, and it is astatic constexprineval.cc. It is not a meson option, not a setting, andnot an environment variable, so no configuration avoids this.
Lillecarl commentedon Aug 3, 2026
Upstream issue filed against the fork, for the real fix: Lillecarl/nix#279
It carries the analysis that this issue does not: why the size is a constant
(the allocation is in the member-initialiser list, which runs before
createBaseEnvcan count), why anEnvcannot grow (variable-length struct,and the parser compiles each reference to a fixed displacement), and why the
size is not a performance parameter. It proposes sizing from
RegisterPrimOp::primOps()plusEvalSettings::extraPrimOps, and a boundcheck at both writes.
It also states the acceptance criterion that matters: a regression test under
the collector proves nothing, because
GC_MALLOCrounds the request up andhides the write. The test needs
-Dgc=disabledor AddressSanitizer.This repository carries the stopgap in
nix/patches/nix-base-env-size.patch,on every scope rather than only the ones with no collector.
fix(nix): give the base environment room, and check the bound
Lillecarl commentedon Aug 5, 2026
Done. The patch landed in 2395efab, on
develop, and it takes option 2 forevery version rather than for the builds with no collector alone. The commit
gives the reason: the collector build makes the same out-of-bounds write, and
Boehm hides it in the slack of the size class.
That commit closed with "The full suite on the non-GC build is not yet
complete". It is complete now. Run
31016955422
ran
test-nogc-*against all four versions:test-nogc-nix_2_31test-nogc-nix_2_34test-nogc-nix_2_35test-nogc-gitnanopynix-tests-nix_2_34-nogcis the build that aborted with "corrupted sizevs. prev_size" in the report, and it runs the whole suite now.
This also satisfies acceptance criterion 2 of #76.
Option 1, the report upstream, stays undone. The patch header names the two
containers and the missing bound check, so the material for it is written.
fix(nix): give the base environment room, and check the bound