Skip to content

[AUTOMATED] Let the per-function watchdog stop heritage's guard placement - #1044

Merged
mahaloz merged 1 commit into
mainfrom
fix/fix1034-hugefn
Oct 12, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/fix1034-hugefn

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

--max-fn-seconds cannot stop a function that hands the addresses of many stack locals to many calls: it runs far past the budget and its memory keeps growing until it finishes or the system kills it. This 45 KB function asks for 1 second and gets 13:

// big.c -- clang -O0 -fno-pic -no-pie -nostdlib -Wl,-e,big -o big big.c
struct S { int a; long b; char c[24]; };
__attribute__((noinline)) int cb(void *p) { return *(int *)p; }
__attribute__((noinline)) int reg(const char *n, void *p, int i, int (*f)(void *)) { return f(p) + i + *n; }
#define L(i) { struct S s; s.a = (i); s.b = r; r += reg("n", &s, (i), cb); }
#define L10(i) L(i##0) L(i##1) L(i##2) L(i##3) L(i##4) L(i##5) L(i##6) L(i##7) L(i##8) L(i##9)
#define L100(i) L10(i##0) L10(i##1) L10(i##2) L10(i##3) L10(i##4) L10(i##5) L10(i##6) L10(i##7) L10(i##8) L10(i##9)
int big(void) { int r = 0; L100(1) L100(2) L100(3) L100(4) L100(5) L100(6) L100(7) return r; }
$ /usr/bin/time -f '%e s, %M KB max RSS' kuna decompile-all big --functions big --json --max-fn-seconds 1
  "error": "per-function decompile budget exceeded (1 s)"
13.13 s, 2672244 KB max RSS

The same shape at 290 KB (5,000 locals) ignores a 10 s budget and reaches 8 GB in 36 s, which matches the growth in #1034.

  • Heritage guards every stack range at every call, as upstream does, so this function gets one INDIRECT per (local, call) pair, about a million here. That placement loop had no deadline probe; it now checks the deadline before each range.
  • A pass whose deadline passed during placement returns before renaming, and a pass panic after the deadline reports the budget error, not the panic text.
  • A function that finishes inside its budget is unchanged: the probes only act once the deadline has passed.

After: 1.37 s, 389 MB for the example; 10.8 s and 775 MB for the 290 KB function with a 10 s budget.

Tests: watchdog_stops_heritage_guard_placement runs the example (checked in as tests/hang-repro/escaped_locals) with a 1 s budget and fails without the fix (15 s against a 10 s bound). Speed (decompile-all of gzip, kmod, grep and ls, 11 interleaved runs): +1.7% on the min-sum with flat medians. That is within noise: the new probe runs 26,518 times over all of ls, which is well under a millisecond.

Part of #1034

🤖 Generated with Claude Code

https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz

…ment

Heritage guards every stack range at every call, so a function that hands
the addresses of thousands of stack locals to thousands of calls places
ranges x calls INDIRECT ops in one pass. That placement loop had no deadline
probe: --max-fn-seconds could not cut it off, and a 290 KB function of this
shape ran past a 10 s budget to 8 GB.

Probe the deadline before each range placement visits, return before
renaming once it has passed, and report a pass panic after the deadline as
the budget error. A drive that finishes before its deadline is unchanged.

Part of #1034

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 12, 2026
@mahaloz
mahaloz force-pushed the fix/fix1034-hugefn branch from b4c48f2 to 416ffc0 Compare October 12, 2026 16:52
@mahaloz
mahaloz merged commit 546e620 into main Oct 12, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/fix1034-hugefn branch October 12, 2026 17:06
mahaloz added a commit that referenced this pull request Oct 12, 2026
…oup default) (#1045)

A function can use up a container's memory long before `--max-fn-seconds` fires. When it does, the kernel kills the whole run, not just that function. With this change kuna stops the function and records it as that function's error. A serial run does this by default under a cgroup memory cap; any run does it with an explicit `--max-fn-memory MiB`. With `big` from #1044 (one function that passes 700 stack structs to 700 calls) under a 1 GB cap:

```
$ systemd-run --user --scope -p MemoryMax=1G -p MemorySwapMax=0 -- kuna decompile-all big --json --max-fn-seconds 0
Killed                                    # main: exit 137, no output
cb   "error": null                        # this PR, same command
reg  "error": null
big  "error": "per-function memory limit exceeded (512 MiB)"
```

`kuna decompile big big` under the same cap now exits 1 with that error instead of being killed. This is the shape of the command in #1034.

- **Trip rule.** A function stops only when the process decompiling it is over the limit and the function itself has added at least 64 MiB since it began. Memory the batch already held when the function started can stop it only if the function adds 64 MiB on top of that. A function that starts within a tenth of the limit first returns free heap pages (`malloc_trim`) and measures again. A stopped function returns its pages before the next one starts.
- **Default.** A serial run and `kuna decompile` under a finite cgroup v2 `memory.max` (the smallest one on the way to the root) take the cap less max(512 MiB, 10%). A 6 GiB cap gives 5,529 MiB. A `--jobs N` pool gets no default: a fixed share per worker stopped functions the container could have held. A container-level check for pools is a possible follow-up. Outside a capped cgroup there is no default either. An explicit value, `0` included, always wins, and `--jobs` workers and `decomp_dbg` get the parent's value as a flag.
- **Mechanism.** It uses the watchdog's existing cooperative probes. Memory is read when a function starts and then at most every 50 ms from `/proc/self/status`. On platforms without that file a non-zero value is refused and there is no default.
- **Surfaces.** `decompile-all`, `decompile-project`, `decompile-graph` and `kuna decompile`.

No new knob for huge functions: `--max-size N` already leaves functions over N bytes out of a run, and `--option maxinstruction N` truncates their flow. `docs/cli.md` points to both.

On a long in-process batch the limit is still a whole-process limit. In stripped `bash -O2`, the process holds 158 to 175 MiB of live data after releasing free pages. At `--max-fn-memory 250`, 4 of 2,540 functions stop, and each of them added 64 to 85 MiB, taking the process to 260 to 273 MiB. At 200 MiB, 7 functions stop. 300 functions of `/usr/bin/gh` at 150 MiB, below the process's post-load size, all decompile. In a 6 GiB scope with the defaults, `gh` under `--jobs 8` has no memory stops. Its 30 largest functions run serially give 17 decompiled and 13 watchdog trips, and the run is not killed.

Tests:
- `memory_limit_stops_one_function_and_the_batch_continues`: serial, `--jobs 2` and `kuna decompile`.
- `ordinary_functions_never_trip_the_memory_limit`: fauxware is byte-identical at 1 MiB, below the post-load size, and at 64 GiB; `6G` is refused.
- `the_memory_limit_defaults_to_a_share_of_the_cgroup_cap`: a faked 1 GiB cap gives 512 MiB serially, `kuna decompile` included. `--jobs 2` gets no default, and an explicit value wins.
- Unit tests cover the trip rule and the cgroup walk, including a namespaced container's root cap.

The first three tests fail before this change, which rejects the flag and has no default. dash, bzip2, cmp and crond decompile byte-identically to main with the limit off, and dash and cmp also with the default on (a 32 GiB cgroup). Speed (decompile-all of gzip, kmod, grep and ls, 11 interleaved runs under a 32 GiB cgroup, so with the default on): +0.33% on the min-sum.

Fixes #1034

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant