Skip to content

Conversation

@DemiMarie
Copy link
Contributor

If the evaluation of one of the arguments to caml_alloc_3 triggers a garbage collection, the other arguments will not be valid anymore.

This rule was not documented in the OCaml manual, so I added an explanation of the problem.

@DemiMarie DemiMarie force-pushed the fix-missing-gc-rule branch from 91638e7 to 57f8c74 Compare August 10, 2024 16:11
Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good catch, thanks!

I'm not fond of the proposed fixes.

  1. I think that changing the manual would need substantially more work. The style is inconsistent with the rest of the manual (you use concepts like "to hole" that are not used anywhere else), and I don't think that adding these as extra gc rules fits the purpose and scope of the rest of this manual section, which generally assumes that all temporaries are properly registered as roots. A minimal fix would be would to add an explicit comment to Rule 2 that temporaries are equivalent to local variables but cannot be declared with CAMLlocal, so temporaries of value type must be written to registered locals right away to "live in harmony with the GC".

  2. The code fix feels too clever and annoyingly asymmetric to me. I would be in favor of sticking to the boring style where we register everything, unless there are good performance reasons to do anything more complex. In this case I don't think there are reasons to do complex (obviously the performance of this code could be improved by returning a record of (unboxed) floats instead of a tuple, but no users expressed such a need so far), so I think you should do really simple and have three locals.

@DemiMarie DemiMarie force-pushed the fix-missing-gc-rule branch 2 times, most recently from cfb8aaa to 8a269e8 Compare August 13, 2024 22:48
@DemiMarie DemiMarie force-pushed the fix-missing-gc-rule branch from 8a269e8 to 2595012 Compare August 22, 2024 18:16
Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation extension is fine and the new code is correct yet simple. Approved. Thanks!

DemiMarie and others added 2 commits August 24, 2024 19:13
If the evaluation of one of the arguments to caml_alloc_3 triggers a
garbage collection, the other arguments will not be valid anymore.
@jmid
Copy link
Member

jmid commented Oct 25, 2024

@Octachron If you are rolling a 5.2.1 release, consider cherry-picking this bugfix PR too 🙏

@DemiMarie
Copy link
Contributor Author

@Octachron If you are rolling a 5.2.1 release, consider cherry-picking this bugfix PR too 🙏

Also for the 4.x LTS release if that is still supported.

Octachron pushed a commit that referenced this pull request Oct 29, 2024
Fix gc hole in runtime/gc_ctrl.c

(cherry picked from commit db586e0)
@Octachron
Copy link
Member

Cherry-picked to 5.2 as 57736d1

@DemiMarie , as far as I can see the 4.14 version does not have this issue, since

  res = caml_alloc_tuple (3);
  Store_field (res, 0, caml_copy_double (minwords));
  Store_field (res, 1, caml_copy_double (prowords));
  Store_field (res, 2, caml_copy_double (majwords));
  CAMLreturn (res);

stores every temporary in res as soon as it is allocated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants