Repository navigation
fix(hookvm): charge a ReplaceAll result before building it - #527
Merged
Merged
Conversation
strings.ReplaceAll, bytes.ReplaceAll and regexp.ReplaceAllString built their result and only then charged its length against the memory budget. A hook that replaces each byte of a 1 MiB string with another 1 MiB string makes the runtime ask for 1 TiB, and the server exits with "fatal error: runtime: out of memory" before the limit is checked. The length of the result is now computed from the match count and charged first. For a regexp template, each match is bounded by the template with every $ reference expanded to the whole match.
Contributor
|
Review notes Looks good to merge.
Minor (non-blocking): for regexp replacements, every |
BaaRai
force-pushed
the
fix/hookvm-replace-budget
branch
from
November 26, 2026 22:24
d4082cf to
2d93be4
Compare
BaaRai
added a commit
to rheatkhs/ARTEX
that referenced
this pull request
Nov 26, 2026
BaaRai
force-pushed
the
fix/hookvm-replace-budget
branch
from
November 26, 2026 22:40
2d93be4 to
56378da
Compare
BaaRai
added a commit
to rheatkhs/ARTEX
that referenced
this pull request
Nov 26, 2026
BaaRai
force-pushed
the
fix/hookvm-replace-budget
branch
from
November 26, 2026 22:56
56378da to
f0f861c
Compare
BaaRai
added a commit
to rheatkhs/ARTEX
that referenced
this pull request
Nov 26, 2026
BaaRai
force-pushed
the
fix/hookvm-replace-budget
branch
from
November 26, 2026 23:13
f0f861c to
fb80900
Compare
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.
A tamper hook can take the whole server down from inside its memory budget. With this hook in a dry run (
POST /api/traffic/hooks/dry-run, default 4 MiB budget):the process exits with
fatal error: runtime: out of memory(the runtime asks for a 1 TiB span). The hook should instead fail open withmemory_limit, as the tamper hook docs describe.strings.ReplaceAll,bytes.ReplaceAllandregexp.ReplaceAllStringintraffic/hookvm/stdlib.gobuild their result first and calle.alloc(len(out))after, so the budget only sees the result once it already exists. Both inputs fit the budget (2 MiB together); the result is 1 TiB. The same hook on live traffic kills the server in the proxy path.The fix charges the result before building it. For
ReplaceAllthe length is exact:len(s) + count*(len(new)-len(old)), withstrings.Count/bytes.Count, which count an emptyoldthe same wayReplaceAlldoes. ForReplaceAllStringI count the matches and their total length with a first pass that builds nothing larger thans, then charge an upper bound: each match becomes at most the template with every$expanded to the whole match.The new subtest in
TestResourceBoundsMemoryStepTimeoutPanicAndRegexpreplaces 8 KiB of matches with 8 KiB each under a 1 MiB budget and checksTotalAlloc. Before the fix it fails on all three calls (64 MiB, 64 MiB and 386 MiB allocated, even though the run does end withErrMemoryLimit); after it passes. I also ran the 1 MiB × 1 MiB hook throughtraffic.DryRunHookwith the default budget: before,fatal error: runtime: out of memory; after, a trace line withoutcome=error reason=memory_limit.go test ./traffic/hookvmand theTamperHooktests in./trafficpass.Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.5.