Skip to content

contrib/awk: bound string and array memory per invocation - #864

Merged
ewhauser merged 4 commits into
mainfrom
claude/awk-memory-bound
Oct 19, 2026
Merged

ewhauser merged 4 commits into
mainfrom
claude/awk-memory-bound

Conversation

@ewhauser

@ewhauser ewhauser commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #862 (base branch claude/nice-volhard-653458). Follow-up to finding 7 of the contrib interpreter security review: awk had no heap cap.

User-visible behavior

Before this change, awk 'BEGIN{s="x"; while(1) s = s s}' reached 1 GB of heap in about 400 ms. It now stops right away:

$ awk 'BEGIN { s = "x"; while (1) s = s s }'
awk: string exceeds the 16777216-byte memory limit
$ echo $?
2

The vendored goawk interpreter gets two new interp.Config fields, and contrib/awk sets both from policy limits:

  • MaxStringBytes = max(MaxStdoutBytes, MaxFileBytes, 16 MiB). It is checked before allocation for concatenation (Concat/ConcatMulti), sprintf/printf, sub/gsub, gensub, multi-dimensional subscripts, and $0 rebuilt from fields ($n =, NF =). For formats it uses an upper bound from widths and precisions, including * arguments, because Go's fmt pads up to 1e6 per verb. For replacements it uses an upper bound from the literal bytes plus each & times the match length. Native function results are also checked after they return.
  • MaxArrayBytes = max(256 MiB, 4 × MaxStringBytes). This is an estimate of all array elements together: 96 bytes per element plus key and string-value bytes. It is charged on every assignment, ++, augmented assignment, getline arr[k], and auto-created reference (a[k]). delete, delete a, split() replacing an array, and function-local arrays going out of scope give their bytes back. split() counts its parts first and refuses before it builds a slice that can't fit.

Both errors come back as awk: … exceeds the N-byte memory limit with exit status 2, like gawk's fatal errors.

Peak RSS when each runaway case runs alone under the default limits: doubling concat 53 MB, gsub amplification 108 MB, distinct large array values 291 MB, 2.6M tiny array elements 329 MB, and split into 4M parts 28 MB (refused before splitting).

Not covered

Transient regex match lists are not capped separately: FS-regex field splitting of a single record, which is already bounded by goawk's 10 MB record limit, and gensub(..., "g", ...) over a target with millions of matches. Non-global gensub now asks the regexp only for the first N matches. Stack frames from deep recursion are not counted either: up to 1000 frames, each holding strings at most MaxStringBytes.

SPEC

Expanded the contrib/awk bullet in the contrib interpreter limits list (§7) with both caps, what credits bytes back, the error and exit status, and the residuals above. The goawk fork README lists the new local change.

Testing

  • New contrib/awk/goawk/interp/limits_test.go runs with small limits (4 KiB strings, 64 KiB arrays) and covers each bounded operation: concat, multi-concat, gsub/sub amplification, huge & replacements, sprintf widths, precisions and *, $0 rebuilds (NF = and $i =), multi-dimensional subscripts, auto-created, assigned, ++, +=, local and getline array elements, and every split path. One program in that file allocates and frees arrays well past the budget and must still succeed, which proves bytes are credited back. Another confirms that zero limits mean unlimited.
  • New contrib/awk/awk_memory_limits_test.go runs the same checks end to end with the real policy limits, but only on cases that fail before allocating much (peak RSS 142 MB for the whole set). Each case checks exit 2, the exact stderr, and empty stdout. It also checks that the string cap follows MaxFileBytes, and has a gensub unit test.
  • go test ./contrib/awk/... passes.
  • awk conformance against pinned gawk 5.3.2 (TestAWKConformance, 39 cases) passes.
  • make lint-contrib: 0 issues.

- sqlite3: attach the invocation context as the connection interrupt,
  stream rows to stdout (bounded buffering for -column/-table/-box),
  force in-memory temp storage behind a VFS that refuses host files,
  cap the SQLite heap at 128 MiB, and turn NOMEM panics into errors.
- awk: execute through goawk's ExecuteContext so loops honor ctx.
- yq: drop the process-wide evaluation lock, set yqlib security
  preferences once at init, and wrap each parsed operator so it checks
  ctx and sizes its result against an evaluation budget.
- SPEC: document the contrib interpreter cancellation and limit contract.
Cap strings built by concatenation, sprintf/printf, sub/gsub/gensub,
multi-dimensional subscripts, native results, and $0 rebuilds at
max(MaxStdoutBytes, MaxFileBytes, 16 MiB), and cap the estimated
bytes held by all arrays at max(256 MiB, 4x that). Runaway scripts
now stop with an awk: memory limit error and exit status 2.
The end-to-end cases allocated hundreds of MiB each under the real
policy limits and timed out on loaded CI runners. Cover each bounded
operation in goawk/interp with small limits instead, and keep only
end-to-end cases that fail before allocating much.
Base automatically changed from claude/nice-volhard-653458 to main October 19, 2026 07:01
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 07:03
@ewhauser
ewhauser merged commit c3599f4 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/awk-memory-bound branch October 19, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant