Repository navigation
contrib/awk: bound string and array memory per invocation - #864
Merged
Merged
Conversation
- 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.
# Conflicts: # SPEC.md
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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:The vendored goawk interpreter gets two new
interp.Configfields, andcontrib/awksets 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$0rebuilt 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× 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 limitwith 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
splitinto 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-globalgensubnow 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 mostMaxStringBytes.SPEC
Expanded the
contrib/awkbullet 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
contrib/awk/goawk/interp/limits_test.goruns 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*,$0rebuilds (NF =and$i =), multi-dimensional subscripts, auto-created, assigned,++,+=, local andgetlinearray elements, and everysplitpath. 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.contrib/awk/awk_memory_limits_test.goruns 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 followsMaxFileBytes, and has agensubunit test.go test ./contrib/awk/...passes.TestAWKConformance, 39 cases) passes.make lint-contrib: 0 issues.