Skip to content

uname: register roots for intermediate strings to avoid GC corruption#6880

Merged
kit-ty-kate merged 1 commit into
ocaml:masterfrom
avsm:uname-fix
Apr 14, 2026
Merged

uname: register roots for intermediate strings to avoid GC corruption#6880
kit-ty-kate merged 1 commit into
ocaml:masterfrom
avsm:uname-fix

Conversation

@avsm

@avsm avsm commented Apr 12, 2026

Copy link
Copy Markdown
Member

uname: register roots for intermediate strings to avoid GC corruption

Reported by @andrew
Backported to 2.5 in #6893

@kit-ty-kate kit-ty-kate left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks! I have a couple of very minor comments but the core is of course good

Comment thread src/core/opamUnix.c Outdated
#include <sys/utsname.h>

CAMLprim value opam_uname(value _unit) {
CAMLparam1(_unit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

wouldn't CAMLparam0(); be sufficient here since it's unused?

Comment thread src/core/opamUnix.c Outdated

CAMLprim value opam_uname(value _unit) {
CAMLparam1(_unit);
CAMLlocal4(v_ret, v_sysname, v_release, v_machine);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

very minor, but i feel like the v_ prefix change here isn't necessary

@kit-ty-kate kit-ty-kate added this to the 2.6.0~alpha1 milestone Apr 13, 2026
@kit-ty-kate

Copy link
Copy Markdown
Member

actually i'm curious, why add new intermediate v_sysname etc. variables? I understand why ret needs to be registered but i'm unsure why the intermediate ones need to be as no other allocation is present on the same line. Is this to prevent some kind of reordering that would somehow be allowed by the C standard or something?

cc @gasche

@kit-ty-kate

Copy link
Copy Markdown
Member

I've chosen to simplify the change and only register the actually required GC root (ret). The resulting patch was verified by @Octachron.

Unless someone raises a veto in the meantime, i'll merge this in a couple hours.

@dra27 dra27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both versions absolutely fine - indeed, the problem with the original code was just that ret could move if any of the caml_copy_string calls resulted in a GC, the actual copied strings went straight into a block, so no issue.

…corruption

Reported-by: Andrew Nesbitt
Co-authored-by: Kate <kit-ty-kate@exn.st>
@kit-ty-kate

Copy link
Copy Markdown
Member

Rebased above master. Merging once CI is done.

@kit-ty-kate

Copy link
Copy Markdown
Member

Thanks a lot!

(ignoring the ocaml-benchmarks failure, opam update failed with error code 40 seems unrelated)

@kit-ty-kate kit-ty-kate merged commit 8512995 into ocaml:master Apr 14, 2026
39 of 40 checks passed
@avsm

avsm commented Apr 15, 2026

Copy link
Copy Markdown
Member Author

Just catching up after some travels; mine was just conservative as each caml_copy_string could go into the GC, but only if someone else modified the code later. Thanks for the simpler revision.

@avsm avsm deleted the uname-fix branch April 15, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants