Fix segfault in GC finalisers - #18
Open
talex5 wants to merge 1 commit into
Open
Conversation
talex5
force-pushed
the
fix-gc
branch
3 times, most recently
from
August 8, 2025 15:26
2dc43c6 to
6f025cf
Compare
Octachron
reviewed
Aug 12, 2025
| ] | ||
| match exprs with | ||
| | [] -> body | ||
| | [x] -> [%expr Vk__helpers.keep_alive [%e Exp.array [x]] [%e owner]; [%e body]] |
Owner
There was a problem hiding this comment.
Even a size-one array can be pick up the flotarray optimized layout. I would suggest to use a ref or a ( (), x) tuple.
Contributor
Author
There was a problem hiding this comment.
Does it matter if it does? The optimised layout only causes a problem if it treats non-floats as floats.
Probably there should be a function to say whether a type needs to be kept alive and we could use that to skip floats completely.
Contributor
Author
There was a problem hiding this comment.
Actually, there's no reason to wrap a single value in anything. I've updated the PR.
talex5
force-pushed
the
fix-gc
branch
4 times, most recently
from
August 17, 2025 09:55
4c027ee to
154b334
Compare
When a C struct refers to other C memory, we need to keep the targets alive. This is done by attaching a finaliser to the OCaml value pointing at the first struct, where the finaliser holds an OCaml value that points at the targets. Previously, this was done using [| Obj.repr field1; Obj.repr field2 |]. However, this fails if `field1` is a float and `field2` isn't as OCaml will try to convert it to a float-array. This will cause a segfault. It now uses a tuple instead when there are multiple fields, and adds the missing `Sys.opaque_identity` (as suggested by Florian Angeletti). Fixes Octachron#17.
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.
When a C struct refers to other C memory, we need to keep the targets alive. This is done by attaching a finaliser to the OCaml value pointing at the first struct, where the finaliser holds an OCaml value that points at the targets.
Previously, this was done using
[| Obj.repr field1; Obj.repr field2 |]. However, this fails iffield1is a float andfield2isn't as OCaml will try to convert it to a float-array. This will cause a segfault.It now uses a tuple instead when there are multiple fields, and adds the missing
Sys.opaque_identity(as suggested by Florian Angeletti).The old code was also catching and ignoringAdded a comment about when this happens.Invalid_argument(6b636fb). I removed this as I don't think it can happen, and if it does we probably want to know about it.Fixes #17.