Repository navigation
builtin: check concat's remaining memory budget before allocation - #1011
Open
waw666waw666 wants to merge 1 commit into
Open
waw666waw666 wants to merge 1 commit into
waw666waw666 wants to merge 1 commit into
Conversation
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
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.
builtin: check concat's remaining memory budget before allocation
Follow-up to #1003 and #1006.
The Safe callback for
concatignores 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.reversehad the same flaw and was fixed in #1006;concatwas 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) thatreverseuses.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
memGrowcheck once the charge was returned (size >= remainingis exactlymemory + size >= MemoryBudget), with the same "memory budget exceeded" error. The only difference is that the allocation no longer happens first.Testing (mirroring the
reversetests 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 withMemoryBudget: 8now fails after allocating 1,232 bytes instead of 96,407,192.TestMemoryBudget_concat_remaining(root): end-to-end budgets forconcat(items)andconcat(items, items).Verified with:
All pass; no existing tests were modified.