-
Notifications
You must be signed in to change notification settings - Fork 0
fix: the 2026-08-19 autoresearch run — two Critical soundness bugs, a false mathematical claim, and an uncatchable process abort #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AregGevorgyan
wants to merge
21
commits into
main
Choose a base branch
from
integrate/autoresearch-2026-08-19
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d3aed71
fix(pslq): close the four value-preserving conversions that switched …
AregGevorgyan 481826a
fix(novelty): read OEIS's real notation, page terms= searches, carry …
AregGevorgyan 8de5af9
fix: five trust-boundary and false-red bugs in the research/claim-gra…
AregGevorgyan e55a65c
Merge branch 'fix/research-graph' into integrate/autoresearch-2026-08-19
AregGevorgyan 251333b
fix(holonomic): report singular indices on a guessed recurrence, make…
AregGevorgyan a7a7af0
Merge branch 'fix/pslq-trust' into integrate/autoresearch-2026-08-19
AregGevorgyan 431dcbe
Merge branch 'fix/holonomic-guess' into integrate/autoresearch-2026-0…
AregGevorgyan cfc1994
fix: give the Zeilberger boundary verdict the domain of n it is a the…
AregGevorgyan fec9ebc
Merge branch 'fix/zeilberger-boundary' into integrate/autoresearch-20…
AregGevorgyan 90f2498
fix(ball): tan and pow_f returned "enclosures" that exclude values in…
AregGevorgyan 886d789
Merge branch 'fix/ball-soundness' into integrate/autoresearch-2026-08-19
AregGevorgyan 8e68ee8
fix: parenthesise a negative or fractional power base in all three pr…
AregGevorgyan 84c1219
Merge branch 'fix/printer-negbase' into integrate/autoresearch-2026-0…
AregGevorgyan 80a70b2
fix(sos): half-Newton-polytope reduction; retract the false N=2 Motzk…
AregGevorgyan 666429a
Merge branch 'fix/sos-motzkin' into integrate/autoresearch-2026-08-19
AregGevorgyan 852529c
fix(validated): enclose bounded integrands at a domain boundary; rest…
AregGevorgyan c6634e8
Merge branch 'fix/validated-integral' into integrate/autoresearch-202…
AregGevorgyan d23a2d3
fix(m9): accept Q(params) input, verify specialisation refusals, and …
AregGevorgyan d6175f3
Merge branch 'fix/parametric-groebner' into integrate/autoresearch-20…
AregGevorgyan 28b2fc2
fix(budget): an out-of-memory exact solve refused, not an uncatchable…
AregGevorgyan f059ec7
Merge branch 'fix/resource-budgets' into integrate/autoresearch-2026-…
AregGevorgyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,322 @@ | ||
| //! Memory accounting for the exact-arithmetic paths, and the address-space | ||
| //! guard that turns an imminent out-of-memory `abort()` into a refusal. | ||
| //! | ||
| //! # The failure this exists to remove | ||
| //! | ||
| //! `rug`/GMP's default reaction to a failed allocation is to print | ||
| //! `GNU MP: Cannot allocate memory (size=N)` and call `abort()`. The Rust | ||
| //! allocator's is `memory allocation of N bytes failed` followed by the same | ||
| //! `abort()`. Neither is catchable: an exact-rational solve that outgrows the | ||
| //! machine takes the whole interpreter with it, and an unattended research | ||
| //! loop loses every result it was holding, not just the offending call. | ||
| //! | ||
| //! # What is enforceable, and what is not | ||
| //! | ||
| //! GMP's contract for a replacement allocation function is that it *must not | ||
| //! return `NULL`* — the library has no failure path to take. Nor may a | ||
| //! replacement unwind: a Rust `panic!` crossing a C frame is undefined | ||
| //! behaviour (and, since Rust 1.81, aborts anyway). So a custom allocator | ||
| //! cannot itself convert an out-of-memory condition into an error. | ||
| //! | ||
| //! What it *can* do soundly is **count**. The functions installed by | ||
| //! [`install`] delegate to whatever GMP was already using and maintain a | ||
| //! process-wide live-byte total. Nothing in them allocates, unwinds, or | ||
| //! returns `NULL`, so they are safe to run inside GMP frames. The refusal | ||
| //! then happens at Alkahest's own cooperative checkpoints | ||
| //! ([`crate::budget::check_all`]), which are ordinary Rust code returning an | ||
| //! ordinary `Err` — a *pre-flight* refusal, before the allocation that would | ||
| //! have died is attempted. | ||
| //! | ||
| //! Two ceilings feed those checkpoints: | ||
| //! | ||
| //! * **`Budget::max_bytes`** — an explicit, caller-supplied ceiling on how | ||
| //! much GMP memory one guarded block may hold live. See | ||
| //! [`crate::budget::enter_with_memory`]. | ||
| //! * **The address-space guard** — active with no budget at all. When the | ||
| //! process runs under a finite `RLIMIT_AS` (`ulimit -v`, a container limit, | ||
| //! a batch scheduler), [`headroom_exhausted`] reports when the process has | ||
| //! climbed to within [`reserve_bytes`] of that limit, and the checkpoint | ||
| //! refuses. This is what makes the *default-arguments* case survivable: | ||
| //! the operator already said how much the process may have, so Alkahest | ||
| //! stops inside that number instead of dying at it. With no limit set | ||
| //! (`ulimit -v unlimited`), the guard is inert and behaviour is unchanged. | ||
| //! | ||
| //! # What remains | ||
| //! | ||
| //! The guard is checkpoint-granular. A single allocation large enough to jump | ||
| //! the whole reserve in one step, between two consecutive checkpoints, still | ||
| //! aborts — nothing short of a fallible allocator can fix that, and GMP does | ||
| //! not have one. `reserve_bytes` is sized to make that unlikely rather than | ||
| //! impossible. Address-space *usage* is only observable on Linux | ||
| //! (`/proc/self/statm`); on other platforms the guard degrades to the | ||
| //! `Budget::max_bytes` ceiling alone. | ||
|
|
||
| use std::ffi::c_void; | ||
| use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; | ||
| use std::sync::{Once, OnceLock}; | ||
|
|
||
| use gmp_mpfr_sys::gmp; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // GMP allocation accounting | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Live bytes currently held by GMP allocations, process-wide. | ||
| static LIVE_BYTES: AtomicU64 = AtomicU64::new(0); | ||
|
|
||
| /// The allocation functions GMP was using before [`install`] wrapped them, | ||
| /// stored as raw addresses because a function pointer is not a `const`- | ||
| /// initialisable atomic. Written once, before `INSTALLED` is set. | ||
| static ORIG_ALLOC: AtomicUsize = AtomicUsize::new(0); | ||
| static ORIG_REALLOC: AtomicUsize = AtomicUsize::new(0); | ||
| static ORIG_FREE: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| static INSTALLED: AtomicBool = AtomicBool::new(false); | ||
| static INSTALL_ONCE: Once = Once::new(); | ||
|
|
||
| type AllocFn = extern "C" fn(usize) -> *mut c_void; | ||
| type ReallocFn = extern "C" fn(*mut c_void, usize, usize) -> *mut c_void; | ||
| type FreeFn = unsafe extern "C" fn(*mut c_void, usize); | ||
|
|
||
| fn add_live(n: usize) { | ||
| LIVE_BYTES.fetch_add(n as u64, Ordering::Relaxed); | ||
| } | ||
|
|
||
| fn sub_live(n: usize) { | ||
| // Saturating, not wrapping: blocks allocated *before* `install` ran are | ||
| // freed through our wrapper without ever having been counted, so the | ||
| // total can legitimately try to go negative. Under-counting only ever | ||
| // makes the ceiling fire late, never spuriously. | ||
| let _ = LIVE_BYTES.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |v| { | ||
| Some(v.saturating_sub(n as u64)) | ||
| }); | ||
| } | ||
|
|
||
| extern "C" fn wrap_alloc(size: usize) -> *mut c_void { | ||
| let orig: AllocFn = unsafe { std::mem::transmute(ORIG_ALLOC.load(Ordering::Acquire)) }; | ||
| let p = orig(size); | ||
| add_live(size); | ||
| p | ||
| } | ||
|
|
||
| extern "C" fn wrap_realloc(ptr: *mut c_void, old: usize, new: usize) -> *mut c_void { | ||
| let orig: ReallocFn = unsafe { std::mem::transmute(ORIG_REALLOC.load(Ordering::Acquire)) }; | ||
| let p = orig(ptr, old, new); | ||
| sub_live(old); | ||
| add_live(new); | ||
| p | ||
| } | ||
|
|
||
| unsafe extern "C" fn wrap_free(ptr: *mut c_void, size: usize) { | ||
| let orig: FreeFn = std::mem::transmute(ORIG_FREE.load(Ordering::Acquire)); | ||
| orig(ptr, size); | ||
| sub_live(size); | ||
| } | ||
|
|
||
| /// Install the counting wrappers around GMP's current allocation functions. | ||
| /// | ||
| /// Idempotent and thread-safe (guarded by a [`Once`]); returns `true` if | ||
| /// accounting is active. Call it as early as the embedding allows — the | ||
| /// Python extension does it from its module initialiser — but it is *not* | ||
| /// required to run before GMP's first allocation: the wrappers delegate to | ||
| /// the functions that were installed at the time, so a block allocated | ||
| /// before installation is still freed by the allocator that produced it, and | ||
| /// the only consequence of installing late is that the live total starts from | ||
| /// a baseline it never saw allocated (handled by [`sub_live`]'s saturation). | ||
| /// | ||
| /// GMP's own documentation warns that changing the allocation functions while | ||
| /// other threads are inside GMP is unsafe; the [`Once`] makes the swap happen | ||
| /// exactly once, and the intended call site is process start-up. | ||
| pub fn install() -> bool { | ||
| INSTALL_ONCE.call_once(|| { | ||
| let mut alloc: gmp::allocate_function = None; | ||
| let mut realloc: gmp::reallocate_function = None; | ||
| let mut free: gmp::free_function = None; | ||
| // SAFETY: three out-pointers to live locals, which is exactly what | ||
| // `mp_get_memory_functions` expects. | ||
| unsafe { gmp::get_memory_functions(&mut alloc, &mut realloc, &mut free) }; | ||
| let (Some(alloc), Some(realloc), Some(free)) = (alloc, realloc, free) else { | ||
| // GMP always reports concrete functions (the defaults if nothing | ||
| // was installed); if it somehow does not, leave it alone. | ||
| return; | ||
| }; | ||
| ORIG_ALLOC.store(alloc as usize, Ordering::Release); | ||
| ORIG_REALLOC.store(realloc as usize, Ordering::Release); | ||
| ORIG_FREE.store(free as usize, Ordering::Release); | ||
| // SAFETY: the wrappers delegate to the functions just captured, never | ||
| // return NULL where the original did not, never unwind, and never | ||
| // allocate — the three things GMP requires of a replacement. | ||
| unsafe { | ||
| gmp::set_memory_functions(Some(wrap_alloc), Some(wrap_realloc), Some(wrap_free)); | ||
| } | ||
| INSTALLED.store(true, Ordering::Release); | ||
| }); | ||
| INSTALLED.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| /// `true` if [`install`] has swapped in the counting wrappers. | ||
| pub fn is_installed() -> bool { | ||
| INSTALLED.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| /// Bytes currently held live by GMP allocations, process-wide. | ||
| /// | ||
| /// Zero when [`install`] has not run. Process-wide rather than per-thread | ||
| /// because GMP's allocation hooks are global: a block allocated on one thread | ||
| /// may be freed on another, so a thread-local total could not stay balanced. | ||
| pub fn gmp_live_bytes() -> u64 { | ||
| LIVE_BYTES.load(Ordering::Relaxed) | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Address-space guard | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// The soft `RLIMIT_AS` of this process in bytes, or `None` when it is | ||
| /// unlimited (or cannot be read). | ||
| /// | ||
| /// Read once and cached: a process that raises its own limit mid-run is not a | ||
| /// case worth a syscall on every checkpoint, and caching can only make the | ||
| /// guard *more* conservative. | ||
| pub fn address_space_limit() -> Option<u64> { | ||
| static LIMIT: OnceLock<Option<u64>> = OnceLock::new(); | ||
| *LIMIT.get_or_init(read_address_space_limit) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn read_address_space_limit() -> Option<u64> { | ||
| let mut rl = libc::rlimit { | ||
| rlim_cur: 0, | ||
| rlim_max: 0, | ||
| }; | ||
| // SAFETY: `getrlimit` writes a `struct rlimit` through the out-pointer. | ||
| if unsafe { libc::getrlimit(libc::RLIMIT_AS, &mut rl) } != 0 { | ||
| return None; | ||
| } | ||
| if rl.rlim_cur == libc::RLIM_INFINITY { | ||
| None | ||
| } else { | ||
| // The cast is a no-op where `rlim_t` is already `u64` (Linux, macOS) | ||
| // and load-bearing where it is not, so it stays. | ||
| #[allow(clippy::unnecessary_cast)] | ||
| Some(rl.rlim_cur as u64) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| fn read_address_space_limit() -> Option<u64> { | ||
| None | ||
| } | ||
|
|
||
| /// Virtual address space currently mapped by this process, in bytes. | ||
| /// | ||
| /// Linux only (`/proc/self/statm`); `None` elsewhere, which disables the | ||
| /// address-space guard rather than guessing. | ||
| #[cfg(target_os = "linux")] | ||
| pub fn address_space_used() -> Option<u64> { | ||
| use std::io::Read; | ||
| let mut buf = [0u8; 64]; | ||
| let mut f = std::fs::File::open("/proc/self/statm").ok()?; | ||
| let n = f.read(&mut buf).ok()?; | ||
| let text = std::str::from_utf8(buf.get(..n)?).ok()?; | ||
| let pages: u64 = text.split_whitespace().next()?.parse().ok()?; | ||
| Some(pages.saturating_mul(page_size())) | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "linux"))] | ||
| pub fn address_space_used() -> Option<u64> { | ||
| None | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| fn page_size() -> u64 { | ||
| static PAGE: OnceLock<u64> = OnceLock::new(); | ||
| *PAGE.get_or_init(|| { | ||
| // SAFETY: `sysconf` takes an int and returns a long; no pointers. | ||
| let n = unsafe { libc::sysconf(libc::_SC_PAGESIZE) }; | ||
| if n > 0 { | ||
| n as u64 | ||
| } else { | ||
| 4096 | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// Headroom the address-space guard keeps in reserve below `RLIMIT_AS`. | ||
| /// | ||
| /// Sized to be crossed by *many* checkpoint intervals, not by one: the | ||
| /// allocations between two consecutive checkpoints in an exact-rational | ||
| /// elimination are limb arrays measured in kilobytes, so 32 MiB is hundreds of | ||
| /// them. | ||
| /// | ||
| /// It is deliberately **not** a large fraction of the limit. `RLIMIT_AS` caps | ||
| /// virtual address space, and importing the extension already maps ~600 MB of | ||
| /// it (arena reservations and thread stacks, ~46 MB of which is resident), so | ||
| /// a reserve of "an eighth of the limit" would refuse every call under a | ||
| /// 900 MB `ulimit -v` — including the ones that fit comfortably today. A flat | ||
| /// floor with a gentle fraction for large limits keeps the guard out of the | ||
| /// way until the process is genuinely at the edge. | ||
| pub fn reserve_bytes(limit: u64) -> u64 { | ||
| const FLOOR: u64 = 32 * 1024 * 1024; | ||
| const CEILING: u64 = 256 * 1024 * 1024; | ||
| (limit / 64).clamp(FLOOR, CEILING) | ||
| } | ||
|
|
||
| /// `Some((used, limit))` when the process has climbed to within | ||
| /// [`reserve_bytes`] of its address-space limit, else `None`. | ||
| /// | ||
| /// `None` — the guard is inert — when no finite `RLIMIT_AS` is set, or when | ||
| /// address-space usage is not observable on this platform. | ||
| pub fn headroom_exhausted() -> Option<(u64, u64)> { | ||
| let limit = address_space_limit()?; | ||
| let used = address_space_used()?; | ||
| if used.saturating_add(reserve_bytes(limit)) >= limit { | ||
| Some((used, limit)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn gmp_accounting_tracks_a_big_rational() { | ||
| assert!(install(), "GMP accounting must install"); | ||
| let before = gmp_live_bytes(); | ||
| let big = { | ||
| let mut z = rug::Integer::from(1); | ||
| z <<= 8_000_000; // ~1 MB of limbs | ||
| z | ||
| }; | ||
| let during = gmp_live_bytes(); | ||
| assert!( | ||
| during > before + 500_000, | ||
| "expected the shift to be counted: {before} -> {during}" | ||
| ); | ||
| drop(big); | ||
| let after = gmp_live_bytes(); | ||
| assert!( | ||
| after < during, | ||
| "freeing must decrement the live total: {during} -> {after}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reserve_is_clamped_to_the_floor_and_ceiling() { | ||
| assert_eq!(reserve_bytes(1024), 32 * 1024 * 1024); | ||
| assert_eq!(reserve_bytes(900_000_000), 32 * 1024 * 1024); | ||
| assert_eq!(reserve_bytes(64 * 1024 * 1024 * 1024), 256 * 1024 * 1024); | ||
| } | ||
|
|
||
| #[test] | ||
| fn headroom_guard_is_inert_without_a_limit() { | ||
| // The test binary itself runs with no `ulimit -v` in CI, so the guard | ||
| // must not fire. (When a limit *is* set the subprocess regression | ||
| // test in tests/ exercises the firing path.) | ||
| if address_space_limit().is_none() { | ||
| assert_eq!(headroom_exhausted(), None); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Remove the intra-doc link to the private
sub_live.cargo docfails in CI: the public documentation ofinstalllinks to the private itemsub_live(rustdoc::private-intra-doc-links). Use plain code formatting instead of a link.🔧 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: docs / Build docs
[error] 125-125: cargo doc failed: public documentation for
installlinks to private itemsub_live(rustdoc::private-intra-doc-links).🤖 Prompt for AI Agents
Source: Pipeline failures