Repository navigation
Conversation
`set_memory_limit` caps the whole state, but a single block can still be as large as that cap, so the cost of one library call (`string.rep`, `utf8.len` on a large string, growing a table) is bounded only by the total limit. `set_allocation_limit` makes the allocator refuse new or growing blocks larger than the limit, which raises the usual memory error. Shrinking always succeeds. When unset, the allocator does one extra comparison.
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.
set_memory_limitcaps the memory of the whole Lua state, but a single block can be as large as that cap. For a host running untrusted scripts, this means the cost of one library call (utf8.len,string.packsize,string.lowerand so on over the largest string the script can build) is bounded only by the total limit: with a 6 MiB limit, oneutf8.lencall takes over 10 ms. A smaller per-block limit bounds how large a string, table or buffer can be, and so how much work a single call can do on it.API
It mirrors
set_memory_limit:0removes the limit, and it returnsError::MemoryControlNotAvailablewhere mlua's allocator is not used (module mode, LuaJIT's own allocator).Error::MemoryErroron the Rust side, catchable bypcallin Lua).ignore_limit(used forlua_pushcfunctionon Lua 5.1/LuaJIT/Luau) and setslimit_reachedon Luau, the same way the memory limit does.unlikely_memory_erroris now false while either limit is set (MemoryState::has_limit), so operations that skip protection when no limit is set stay protected.usize::MAX, so the allocator does one extra comparison.Tests
memory::tests::test_allocation_limit(unit test calling the allocator directly): no limit by default, below/at/one past the limit, realloc growing past the limit fails and keeps the block, shrinking from above the limit succeeds, used memory is unchanged by refused requests,0removes the limit.tests/memory.rs::test_allocation_limit: strings at half the limit and over it, the largest string that fits grows byte for byte with the limit (checked at +64 so LuaJIT's 4-byte string rounding doesn't matter), a growing table fails,pcall(string.rep, ...)over the limit returnsfalseand the state keeps working, removing the limit restores everything.tests/memory.rs::test_allocation_limit_with_memory_limit: each limit applies on its own, and removing the memory limit keeps the allocation limit (and keepscreate_stringprotected rather than aborting).Ran
tests/memory.rson lua55, lua54, lua53, lua52, lua51, luajit, luau and luau-jit; the full suite (withasync,serde,macros,anyhow,userdata-wrappers,send) on lua54 and luau, also with--cfg=force_memory_limit; clippy and nightly rustfmt.