Skip to content

builtin: check concat's remaining memory budget before allocation - #1011

Open
waw666waw666 wants to merge 1 commit into
expr-lang:masterfrom
waw666waw666:fix/concat-memory-budget
Open

waw666waw666 wants to merge 1 commit into
expr-lang:masterfrom
waw666waw666:fix/concat-memory-budget

Conversation

@waw666waw666

Copy link
Copy Markdown
Contributor

builtin: check concat's remaining memory budget before allocation

Follow-up to #1003 and #1006.

The Safe callback for concat ignores its remaining-budget argument. It allocates and fills the output slice, then returns the memory charge for the VM to check. An over-budget call already returns a budget error, but only after the output allocation: with a 1M-element environment array and a budget of 8, concat(items) allocated 96,407,192 bytes before the VM rejected the result. reverse had the same flaw and was fixed in #1006; concat was left behind, even though its total output size is known upfront from the argument lengths.

Validate the arguments and sum their lengths first, then check the total against the remaining budget before allocating, mirroring the strict boundary (size >= remaining) that reverse uses.

Argument validation order, successful results and the returned charge are unchanged: every call rejected by the new check would also have been rejected by the VM's memGrow check once the charge was returned (size >= remaining is exactly memory + size >= MemoryBudget), with the same "memory budget exceeded" error. The only difference is that the allocation no longer happens first.

Testing (mirroring the reverse tests from #1006):

  • TestConcatRemainingBudget (builtin): boundary cases for the remaining budget (below / exact / above, slices, arrays, empty, mixed types), plus invalid-argument cases asserting they still return the original errors rather than the budget error.
  • TestMemoryBudget_concat_before_allocation (root): a 1M-element env array with MemoryBudget: 8 now fails after allocating 1,232 bytes instead of 96,407,192.
  • TestMemoryBudget_concat_remaining (root): end-to-end budgets for concat(items) and concat(items, items).

Verified with:

go test ./...
go test -race . ./builtin/
go vet ./builtin/ ./vm/ .
GOARCH=386 go build . ./builtin/

All pass; no existing tests were modified.

Follow-up to expr-lang#1003 and expr-lang#1006.

The Safe callback for concat ignores its remaining-budget argument.
It allocates and fills the output slice, then returns the memory
charge for the VM to check. An over-budget call already returns a
budget error, but only after the output allocation: with a
1M-element environment array and a budget of 8, concat allocated
96,407,192 bytes before the VM rejected the result.

Validate the arguments and sum their lengths first, then check the
total against the remaining budget before allocating, mirroring the
strict boundary (size >= remaining) that reverse uses. Argument
validation order, successful results and the returned charge are
unchanged: every call rejected by the new check would also have
been rejected by the VM's memGrow check once the charge was
returned, with the same "memory budget exceeded" error.

This branch has not been deployed

No deployments
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