This is an archive of the discontinued LLVM Phabricator instance.

[OCaml] Fix unsafe uses of Store_field
ClosedPublic

Authored by jberdine on Mar 28 2021, 3:28 PM.

Details

Summary

Using Store_field to initialize fields of blocks allocated with
caml_alloc_small is unsafe. The fields of blocks allocated by
caml_alloc_small are not initialized, and Store_field calls the
OCaml GC write barrier. If the uninitialized value of a field happens
to point into the OCaml heap, then it will e.g. be added to a conflict
set or followed and have what the GC thinks are color bits
changed. This leads to crashes or memory corruption.

This diff fixes a few (I think all) instances of this problem. Some of
these are creating option values. OCaml 4.12 has a dedicated
caml_alloc_some function for this, so this diff adds a compatible
function with a version check to avoid conflict. With that, macros for
accessing option values are also added.

Diff Detail

Event Timeline

jberdine created this revision.Mar 28 2021, 3:28 PM
jberdine requested review of this revision.Mar 28 2021, 3:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2021, 3:28 PM
vaivaswatha accepted this revision.Mar 29 2021, 9:53 AM

This fixes a potentially hard-to-discover bug ! The last thing that a user of the OCaml API would think of is "oh this function uses Store_field to initialize fields of blocks allocated with caml_alloc_small and that could lead to a hard to trace GC bug".

Thank you !.

This revision is now accepted and ready to land.Mar 29 2021, 9:53 AM
This revision was landed with ongoing or failed builds.Apr 5 2021, 2:59 AM
This revision was automatically updated to reflect the committed changes.