Repository navigation
fix(runtime): copy funs stored in ETS and persistent_term with their captured values - #30
Merged
Merged
Conversation
…captured values The ETS copy, which persistent_term also uses, copied lists, tuples, maps, binaries, floats, bignums and references but kept any other boxed term as it was. A fun stored in a table or persistent term therefore pointed into the heap of the process that stored it, and once that process exited or collected garbage, calling the fun read freed memory: a wrong result, badfun or a crash. Count and copy closures like tuples: the header, code pointer and arity are copied as they are, and the captured values are queued and copied. The same copier reads stored terms back with ets:tab2list/1 and persistent_term:get/1, so a fun read that way is now a copy in the reading process and keeps working after the table or term is deleted. The ets_stored_fun fixture checks funs at the top of a row, nested in a list, tuple and map, after update_element/3, read by tab2list/1 after the table is deleted, stored by a process that collects garbage, and through persistent_term, natively and on BEAM.
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.
Why
A fun stored in an ETS table or with persistent_term kept pointing into the heap of the process that stored it, so calling it after that process exited or collected garbage read freed memory.
What changed
Before and after
Before: The new fixture failed in 5 of 5 native runs with badfun on a freed closure; a fun stored by a process that collected garbage, and one put in persistent_term by a process that exited, also raised badfun in 3 of 3 runs each.
After: The fixture passes in 10 of 10 native runs and 10 of 10 runs on OTP 29.
Only the runtime's storage copy changed; nothing on screen changes.
Checked
How it was checked
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo test --workspace --locked --no-fail-fast
python3 scripts/test-allocator-config.py
nera test -j 1 --timeout 30
nera test ets_stored_fun, ten times natively
the same fixture ten times on OTP 29
Risk
Risk above low: Blast radius, medium — Every ETS and persistent_term write and copying read goes through the changed copier.
Full risk assessment
Not done
Bitstrings that are not a whole number of bytes still keep pointing into the storing heap when stored in ETS; that is tracked separately.