Fix Weak.get_copy not darkening custom blocks - #13859
Merged
Merged
Conversation
This PR changes `ephe_get_field_copy` to darken the value it returns if it is a custom block. While looking at PRs related to ephemerons I noticed a suspicious code path in `ephe_get_field_copy`. So far I have not managed to trigger a crash, but figured I would send this PR for feedback anyhow. The scenario that worries me is if `ephe_get_field_copy` is called with a value that is not `caml_ephe_none` and is a custom block. In this case, a `Some` block will be allocated to wrap it, and returned. So the argument custom block value will be returned without being copied nor darkened. If `ephe_get_field` were called, it would be darkened. It makes sense that custom blocks are not copied, to avoid issues with calling finalizers multiple times. But I can not see how it is safe to not darken the returned value. Looking at the history, when custom blocks stopped being copied, they were darkened: ocaml#710 Unrelated but in the same code, `ephe_get_field_copy` has a retry loop to protect against an allocation changing the tag or size of the value it is being asked to copy. Isn't changing tags or sizes no longer possible with the ocaml 5 runtime? But I'm unsure about `Forward_tag` values. Signed-off-by: Josh Berdine <josh@berdine.net>
stedolan
approved these changes
Mar 10, 2025
Contributor
|
Thanks, well spotted! For concreteness, here's a program that hits the bug: let w = Weak.create 1
let major_obj () =
let n = Sys.opaque_identity 42 in
let v = Int64.of_int n in
Gc.minor ();
v
let () =
Weak.set w 0 (Some (major_obj ()));
Gc.major ();
let x = Option.get (Weak.get_copy w 0) in
Gc.major ();
Printf.printf "value: %Ld\n%!" x;
let junk = List.init 1_000_000 Fun.id in
Gc.minor ();
ignore (Sys.opaque_identity junk);
Printf.printf "value: %Ld\n%!" xOn my machine, I get: where the second print is corrupted because the memory has been reused as a cons cell. |
Member
|
The regression test could make a good testsuite addition? |
Signed-off-by: Josh Berdine <josh@berdine.net>
Member
|
There is a CI failure on macos-arm64 on I think that this is unrelated to the current PR -- the test is too time-consuming and times out on the CI machine -- and not a weird infinite loop that you introduced and only occurs in native and not in bytecode. I re-ran the job just to make sure. |
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.
This PR changes
ephe_get_field_copyto darken the value it returns if it is a custom block.While looking at PRs related to ephemerons I noticed a suspicious code path in
ephe_get_field_copy. So far I have not managed to trigger a crash, but figured I would send this PR for feedback anyhow.The scenario that worries me is if
ephe_get_field_copyis called with a value that is notcaml_ephe_noneand is a custom block. In this case, aSomeblock will be allocated to wrap it, and returned. So the argument custom block value will be returned without being copied nor darkened. Ifephe_get_fieldwere called, it would be darkened. It makes sense that custom blocks are not copied, to avoid issues with calling finalizers multiple times. But I can not see how it is safe to not darken the returned value.Looking at the history, when custom blocks stopped being copied, they were darkened: #710
Unrelated but in the same code,
ephe_get_field_copyhas a retry loop to protect against an allocation changing the tag or size of the value it is being asked to copy. Isn't changing tags or sizes no longer possible with the ocaml 5 runtime? But I'm unsure aboutForward_tagvalues.Signed-off-by: Josh Berdine josh@berdine.net